xref: /linux/include/linux/jbd2.h (revision c84d3e3130dfe1058cb27dc78e7ad8bd36f0545a)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * linux/include/linux/jbd2.h
4  *
5  * Written by Stephen C. Tweedie <sct@redhat.com>
6  *
7  * Copyright 1998-2000 Red Hat, Inc --- All Rights Reserved
8  *
9  * Definitions for transaction data structures for the buffer cache
10  * filesystem journaling support.
11  */
12 
13 #ifndef _LINUX_JBD2_H
14 #define _LINUX_JBD2_H
15 
16 /* Allow this file to be included directly into e2fsprogs */
17 #ifndef __KERNEL__
18 #include "jfs_compat.h"
19 #define JBD2_DEBUG
20 #else
21 
22 #include <linux/types.h>
23 #include <linux/buffer_head.h>
24 #include <linux/journal-head.h>
25 #include <linux/stddef.h>
26 #include <linux/mutex.h>
27 #include <linux/timer.h>
28 #include <linux/slab.h>
29 #include <linux/bit_spinlock.h>
30 #include <linux/blkdev.h>
31 #include <linux/crc32c.h>
32 #endif
33 
34 #define journal_oom_retry 1
35 
36 /*
37  * Define JBD2_PARANIOD_IOFAIL to cause a kernel BUG() if ext4 finds
38  * certain classes of error which can occur due to failed IOs.  Under
39  * normal use we want ext4 to continue after such errors, because
40  * hardware _can_ fail, but for debugging purposes when running tests on
41  * known-good hardware we may want to trap these errors.
42  */
43 #undef JBD2_PARANOID_IOFAIL
44 
45 /*
46  * The default maximum commit age, in seconds.
47  */
48 #define JBD2_DEFAULT_MAX_COMMIT_AGE 5
49 
50 #ifdef CONFIG_JBD2_DEBUG
51 /*
52  * Define JBD2_EXPENSIVE_CHECKING to enable more expensive internal
53  * consistency checks.  By default we don't do this unless
54  * CONFIG_JBD2_DEBUG is on.
55  */
56 #define JBD2_EXPENSIVE_CHECKING
57 void __jbd2_debug(int level, const char *file, const char *func,
58 		  unsigned int line, const char *fmt, ...);
59 
60 #define jbd2_debug(n, fmt, a...) \
61 	__jbd2_debug((n), __FILE__, __func__, __LINE__, (fmt), ##a)
62 #else
63 #define jbd2_debug(n, fmt, a...)  no_printk(fmt, ##a)
64 #endif
65 
66 #define JBD2_MIN_JOURNAL_BLOCKS 1024
67 #define JBD2_DEFAULT_FAST_COMMIT_BLOCKS 256
68 
69 #ifdef __KERNEL__
70 
71 /**
72  * typedef handle_t - The handle_t type represents a single atomic update being performed by some process.
73  *
74  * All filesystem modifications made by the process go
75  * through this handle.  Recursive operations (such as quota operations)
76  * are gathered into a single update.
77  *
78  * The buffer credits field is used to account for journaled buffers
79  * being modified by the running process.  To ensure that there is
80  * enough log space for all outstanding operations, we need to limit the
81  * number of outstanding buffers possible at any time.  When the
82  * operation completes, any buffer credits not used are credited back to
83  * the transaction, so that at all times we know how many buffers the
84  * outstanding updates on a transaction might possibly touch.
85  *
86  * This is an opaque datatype.
87  **/
88 typedef struct jbd2_journal_handle handle_t;	/* Atomic operation type */
89 
90 
91 /**
92  * typedef journal_t - The journal_t maintains all of the journaling state information for a single filesystem.
93  *
94  * journal_t is linked to from the fs superblock structure.
95  *
96  * We use the journal_t to keep track of all outstanding transaction
97  * activity on the filesystem, and to manage the state of the log
98  * writing process.
99  *
100  * This is an opaque datatype.
101  **/
102 typedef struct journal_s	journal_t;	/* Journal control structure */
103 #endif
104 
105 /*
106  * Internal structures used by the logging mechanism:
107  */
108 
109 #define JBD2_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
110 
111 /*
112  * On-disk structures
113  */
114 
115 /*
116  * Descriptor block types:
117  */
118 
119 #define JBD2_DESCRIPTOR_BLOCK	1
120 #define JBD2_COMMIT_BLOCK	2
121 #define JBD2_SUPERBLOCK_V1	3
122 #define JBD2_SUPERBLOCK_V2	4
123 #define JBD2_REVOKE_BLOCK	5
124 
125 /*
126  * Standard header for all descriptor blocks:
127  */
128 typedef struct journal_header_s
129 {
130 	__be32		h_magic;
131 	__be32		h_blocktype;
132 	__be32		h_sequence;
133 } journal_header_t;
134 
135 /*
136  * Checksum types.
137  */
138 #define JBD2_CRC32_CHKSUM   1
139 #define JBD2_MD5_CHKSUM     2
140 #define JBD2_SHA1_CHKSUM    3
141 #define JBD2_CRC32C_CHKSUM  4
142 
143 #define JBD2_CRC32_CHKSUM_SIZE 4
144 
145 #define JBD2_CHECKSUM_BYTES (32 / sizeof(u32))
146 /*
147  * Commit block header for storing transactional checksums:
148  *
149  * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum*
150  * fields are used to store a checksum of the descriptor and data blocks.
151  *
152  * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum
153  * field is used to store crc32c(uuid+commit_block).  Each journal metadata
154  * block gets its own checksum, and data block checksums are stored in
155  * journal_block_tag (in the descriptor).  The other h_chksum* fields are
156  * not used.
157  *
158  * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses
159  * journal_block_tag3_t to store a full 32-bit checksum.  Everything else
160  * is the same as v2.
161  *
162  * Checksum v1, v2, and v3 are mutually exclusive features.
163  */
164 struct commit_header {
165 	__be32		h_magic;
166 	__be32          h_blocktype;
167 	__be32          h_sequence;
168 	unsigned char   h_chksum_type;
169 	unsigned char   h_chksum_size;
170 	unsigned char 	h_padding[2];
171 	__be32 		h_chksum[JBD2_CHECKSUM_BYTES];
172 	__be64		h_commit_sec;
173 	__be32		h_commit_nsec;
174 };
175 
176 /*
177  * The block tag: used to describe a single buffer in the journal.
178  * t_blocknr_high is only used if INCOMPAT_64BIT is set, so this
179  * raw struct shouldn't be used for pointer math or sizeof() - use
180  * journal_tag_bytes(journal) instead to compute this.
181  */
182 typedef struct journal_block_tag3_s
183 {
184 	__be32		t_blocknr;	/* The on-disk block number */
185 	__be32		t_flags;	/* See below */
186 	__be32		t_blocknr_high; /* most-significant high 32bits. */
187 	__be32		t_checksum;	/* crc32c(uuid+seq+block) */
188 } journal_block_tag3_t;
189 
190 typedef struct journal_block_tag_s
191 {
192 	__be32		t_blocknr;	/* The on-disk block number */
193 	__be16		t_checksum;	/* truncated crc32c(uuid+seq+block) */
194 	__be16		t_flags;	/* See below */
195 	__be32		t_blocknr_high; /* most-significant high 32bits. */
196 } journal_block_tag_t;
197 
198 /* Tail of descriptor or revoke block, for checksumming */
199 struct jbd2_journal_block_tail {
200 	__be32		t_checksum;	/* crc32c(uuid+descr_block) */
201 };
202 
203 /*
204  * The revoke descriptor: used on disk to describe a series of blocks to
205  * be revoked from the log
206  */
207 typedef struct jbd2_journal_revoke_header_s
208 {
209 	journal_header_t r_header;
210 	__be32		 r_count;	/* Count of bytes used in the block */
211 } jbd2_journal_revoke_header_t;
212 
213 /* Definitions for the journal tag flags word: */
214 #define JBD2_FLAG_ESCAPE		1	/* on-disk block is escaped */
215 #define JBD2_FLAG_SAME_UUID	2	/* block has same uuid as previous */
216 #define JBD2_FLAG_DELETED	4	/* block deleted by this transaction */
217 #define JBD2_FLAG_LAST_TAG	8	/* last tag in this descriptor block */
218 
219 
220 /*
221  * The journal superblock.  All fields are in big-endian byte order.
222  */
223 typedef struct journal_superblock_s
224 {
225 /* 0x0000 */
226 	journal_header_t s_header;
227 
228 /* 0x000C */
229 	/* Static information describing the journal */
230 	__be32	s_blocksize;		/* journal device blocksize */
231 	__be32	s_maxlen;		/* total blocks in journal file */
232 	__be32	s_first;		/* first block of log information */
233 
234 /* 0x0018 */
235 	/* Dynamic information describing the current state of the log */
236 	__be32	s_sequence;		/* first commit ID expected in log */
237 	__be32	s_start;		/* blocknr of start of log */
238 
239 /* 0x0020 */
240 	/* Error value, as set by jbd2_journal_abort(). */
241 	__be32	s_errno;
242 
243 /* 0x0024 */
244 	/* Remaining fields are only valid in a version-2 superblock */
245 	__be32	s_feature_compat;	/* compatible feature set */
246 	__be32	s_feature_incompat;	/* incompatible feature set */
247 	__be32	s_feature_ro_compat;	/* readonly-compatible feature set */
248 /* 0x0030 */
249 	__u8	s_uuid[16];		/* 128-bit uuid for journal */
250 
251 /* 0x0040 */
252 	__be32	s_nr_users;		/* Nr of filesystems sharing log */
253 
254 	__be32	s_dynsuper;		/* Blocknr of dynamic superblock copy*/
255 
256 /* 0x0048 */
257 	__be32	s_max_transaction;	/* Limit of journal blocks per trans.*/
258 	__be32	s_max_trans_data;	/* Limit of data blocks per trans. */
259 
260 /* 0x0050 */
261 	__u8	s_checksum_type;	/* checksum type */
262 	__u8	s_padding2[3];
263 /* 0x0054 */
264 	__be32	s_num_fc_blks;		/* Number of fast commit blocks */
265 	__be32	s_head;			/* blocknr of head of log, only uptodate
266 					 * while the filesystem is clean */
267 /* 0x005C */
268 	__u32	s_padding[40];
269 	__be32	s_checksum;		/* crc32c(superblock) */
270 
271 /* 0x0100 */
272 	__u8	s_users[16*48];		/* ids of all fs'es sharing the log */
273 /* 0x0400 */
274 } journal_superblock_t;
275 
276 #define JBD2_FEATURE_COMPAT_CHECKSUM		0x00000001
277 
278 #define JBD2_FEATURE_INCOMPAT_REVOKE		0x00000001
279 #define JBD2_FEATURE_INCOMPAT_64BIT		0x00000002
280 #define JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT	0x00000004
281 #define JBD2_FEATURE_INCOMPAT_CSUM_V2		0x00000008
282 #define JBD2_FEATURE_INCOMPAT_CSUM_V3		0x00000010
283 #define JBD2_FEATURE_INCOMPAT_FAST_COMMIT	0x00000020
284 
285 /* See "journal feature predicate functions" below */
286 
287 /* Features known to this kernel version: */
288 #define JBD2_KNOWN_COMPAT_FEATURES	JBD2_FEATURE_COMPAT_CHECKSUM
289 #define JBD2_KNOWN_ROCOMPAT_FEATURES	0
290 #define JBD2_KNOWN_INCOMPAT_FEATURES	(JBD2_FEATURE_INCOMPAT_REVOKE | \
291 					JBD2_FEATURE_INCOMPAT_64BIT | \
292 					JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT | \
293 					JBD2_FEATURE_INCOMPAT_CSUM_V2 | \
294 					JBD2_FEATURE_INCOMPAT_CSUM_V3 | \
295 					JBD2_FEATURE_INCOMPAT_FAST_COMMIT)
296 
297 #ifdef __KERNEL__
298 
299 #include <linux/fs.h>
300 #include <linux/sched.h>
301 
302 enum jbd_state_bits {
303 	BH_JBD			/* Has an attached ext3 journal_head */
304 	  = BH_PrivateStart,
305 	BH_JWrite,		/* Being written to log (@@@ DEBUGGING) */
306 	BH_Freed,		/* Has been freed (truncated) */
307 	BH_Revoked,		/* Has been revoked from the log */
308 	BH_RevokeValid,		/* Revoked flag is valid */
309 	BH_JBDDirty,		/* Is dirty but journaled */
310 	BH_JournalHead,		/* Pins bh->b_private and jh->b_bh */
311 	BH_Shadow,		/* IO on shadow buffer is running */
312 	BH_Verified,		/* Metadata block has been verified ok */
313 	BH_JBDPrivateStart,	/* First bit available for private use by FS */
314 };
315 
BUFFER_FNS(JBD,jbd)316 BUFFER_FNS(JBD, jbd)
317 BUFFER_FNS(JWrite, jwrite)
318 BUFFER_FNS(JBDDirty, jbddirty)
319 TAS_BUFFER_FNS(JBDDirty, jbddirty)
320 BUFFER_FNS(Revoked, revoked)
321 TAS_BUFFER_FNS(Revoked, revoked)
322 BUFFER_FNS(RevokeValid, revokevalid)
323 TAS_BUFFER_FNS(RevokeValid, revokevalid)
324 BUFFER_FNS(Freed, freed)
325 BUFFER_FNS(Shadow, shadow)
326 BUFFER_FNS(Verified, verified)
327 
328 static inline struct buffer_head *jh2bh(struct journal_head *jh)
329 {
330 	return jh->b_bh;
331 }
332 
bh2jh(struct buffer_head * bh)333 static inline struct journal_head *bh2jh(struct buffer_head *bh)
334 {
335 	return bh->b_private;
336 }
337 
jbd_lock_bh_journal_head(struct buffer_head * bh)338 static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
339 {
340 	bit_spin_lock(BH_JournalHead, &bh->b_state);
341 }
342 
jbd_unlock_bh_journal_head(struct buffer_head * bh)343 static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
344 {
345 	bit_spin_unlock(BH_JournalHead, &bh->b_state);
346 }
347 
348 #define J_ASSERT(assert)	BUG_ON(!(assert))
349 
350 #define J_ASSERT_BH(bh, expr)	J_ASSERT(expr)
351 #define J_ASSERT_JH(jh, expr)	J_ASSERT(expr)
352 
353 #if defined(JBD2_PARANOID_IOFAIL)
354 #define J_EXPECT(expr, why...)		J_ASSERT(expr)
355 #define J_EXPECT_BH(bh, expr, why...)	J_ASSERT_BH(bh, expr)
356 #define J_EXPECT_JH(jh, expr, why...)	J_ASSERT_JH(jh, expr)
357 #else
358 #define __journal_expect(expr, why...)					     \
359 	({								     \
360 		int val = (expr);					     \
361 		if (!val) {						     \
362 			printk(KERN_ERR					     \
363 			       "JBD2 unexpected failure: %s: %s;\n",	     \
364 			       __func__, #expr);			     \
365 			printk(KERN_ERR why "\n");			     \
366 		}							     \
367 		val;							     \
368 	})
369 #define J_EXPECT(expr, why...)		__journal_expect(expr, ## why)
370 #define J_EXPECT_BH(bh, expr, why...)	__journal_expect(expr, ## why)
371 #define J_EXPECT_JH(jh, expr, why...)	__journal_expect(expr, ## why)
372 #endif
373 
374 /* Flags in jbd_inode->i_flags */
375 #define __JI_COMMIT_RUNNING 0
376 #define __JI_WRITE_DATA 1
377 #define __JI_WAIT_DATA 2
378 
379 /*
380  * Commit of the inode data in progress. We use this flag to protect us from
381  * concurrent deletion of inode. We cannot use reference to inode for this
382  * since we cannot afford doing last iput() on behalf of kjournald
383  */
384 #define JI_COMMIT_RUNNING (1 << __JI_COMMIT_RUNNING)
385 /* Write allocated dirty buffers in this inode before commit */
386 #define JI_WRITE_DATA (1 << __JI_WRITE_DATA)
387 /* Wait for outstanding data writes for this inode before commit */
388 #define JI_WAIT_DATA (1 << __JI_WAIT_DATA)
389 
390 /**
391  * struct jbd2_inode - The jbd_inode type is the structure linking inodes in
392  * ordered mode present in a transaction so that we can sync them during commit.
393  */
394 struct jbd2_inode {
395 	/**
396 	 * @i_transaction:
397 	 *
398 	 * Which transaction does this inode belong to? Either the running
399 	 * transaction or the committing one. [j_list_lock]
400 	 */
401 	transaction_t *i_transaction;
402 
403 	/**
404 	 * @i_next_transaction:
405 	 *
406 	 * Pointer to the running transaction modifying inode's data in case
407 	 * there is already a committing transaction touching it. [j_list_lock]
408 	 */
409 	transaction_t *i_next_transaction;
410 
411 	/**
412 	 * @i_list: List of inodes in the i_transaction [j_list_lock]
413 	 */
414 	struct list_head i_list;
415 
416 	/**
417 	 * @i_vfs_inode:
418 	 *
419 	 * VFS inode this inode belongs to [constant for lifetime of structure]
420 	 */
421 	struct inode *i_vfs_inode;
422 
423 	/**
424 	 * @i_flags: Flags of inode [j_list_lock]
425 	 */
426 	unsigned long i_flags;
427 
428 	/**
429 	 * @i_dirty_start_page:
430 	 *
431 	 * Dirty range start in PAGE_SIZE units.
432 	 *
433 	 * The dirty range is empty if @i_dirty_start_page is greater than or
434 	 * equal to @i_dirty_end_page.
435 	 *
436 	 * [j_list_lock]
437 	 */
438 	pgoff_t i_dirty_start_page;
439 
440 	/**
441 	 * @i_dirty_end_page:
442 	 *
443 	 * Dirty range end in PAGE_SIZE units (exclusive).
444 	 *
445 	 * [j_list_lock]
446 	 */
447 	pgoff_t i_dirty_end_page;
448 };
449 
450 /*
451  * Lockless readers treat start_page >= end_page as an empty range.
452  * Writers publish a new non-empty range by storing i_dirty_end_page before
453  * i_dirty_start_page.
454  */
jbd2_jinode_get_dirty_range(const struct jbd2_inode * jinode,loff_t * start,loff_t * end)455 static inline bool jbd2_jinode_get_dirty_range(const struct jbd2_inode *jinode,
456 					       loff_t *start, loff_t *end)
457 {
458 	pgoff_t start_page = READ_ONCE(jinode->i_dirty_start_page);
459 	pgoff_t end_page = READ_ONCE(jinode->i_dirty_end_page);
460 
461 	if (start_page >= end_page)
462 		return false;
463 
464 	*start = (loff_t)start_page << PAGE_SHIFT;
465 	*end = ((loff_t)end_page << PAGE_SHIFT) - 1;
466 	return true;
467 }
468 
469 struct jbd2_revoke_table_s;
470 
471 /**
472  * struct jbd2_journal_handle - The jbd2_journal_handle type is the concrete
473  *     type associated with handle_t.
474  * @h_transaction: Which compound transaction is this update a part of?
475  * @h_journal: Which journal handle belongs to - used iff h_reserved set.
476  * @h_rsv_handle: Handle reserved for finishing the logical operation.
477  * @h_total_credits: Number of remaining buffers we are allowed to add to
478  *	journal. These are dirty buffers and revoke descriptor blocks.
479  * @h_revoke_credits: Number of remaining revoke records available for handle
480  * @h_ref: Reference count on this handle.
481  * @h_err: Field for caller's use to track errors through large fs operations.
482  * @h_sync: Flag for sync-on-close.
483  * @h_reserved: Flag for handle for reserved credits.
484  * @h_aborted: Flag indicating fatal error on handle.
485  * @h_type: For handle statistics.
486  * @h_line_no: For handle statistics.
487  * @h_start_jiffies: Handle Start time.
488  * @h_requested_credits: Holds @h_total_credits after handle is started.
489  * @h_revoke_credits_requested: Holds @h_revoke_credits after handle is started.
490  * @saved_alloc_context: Saved context while transaction is open.
491  **/
492 
493 /* Docbook can't yet cope with the bit fields, but will leave the documentation
494  * in so it can be fixed later.
495  */
496 
497 struct jbd2_journal_handle
498 {
499 	union {
500 		transaction_t	*h_transaction;
501 		/* Which journal handle belongs to - used iff h_reserved set */
502 		journal_t	*h_journal;
503 	};
504 
505 	handle_t		*h_rsv_handle;
506 	int			h_total_credits;
507 	int			h_revoke_credits;
508 	int			h_revoke_credits_requested;
509 	int			h_ref;
510 	int			h_err;
511 
512 	/* Flags [no locking] */
513 	unsigned char	h_sync:		1;
514 	unsigned char	h_reserved:	1;
515 	unsigned char	h_aborted:	1;
516 	unsigned char	h_invalid:	1;
517 	unsigned char	h_type;
518 	unsigned short	h_line_no;
519 
520 	unsigned long		h_start_jiffies;
521 	unsigned int		h_requested_credits;
522 
523 	unsigned int		saved_alloc_context;
524 };
525 
526 
527 /*
528  * Some stats for checkpoint phase
529  */
530 struct transaction_chp_stats_s {
531 	unsigned long		cs_chp_time;
532 	__u32			cs_forced_to_close;
533 	__u32			cs_written;
534 	__u32			cs_dropped;
535 };
536 
537 /* The transaction_t type is the guts of the journaling mechanism.  It
538  * tracks a compound transaction through its various states:
539  *
540  * RUNNING:	accepting new updates
541  * LOCKED:	Updates still running but we don't accept new ones
542  * RUNDOWN:	Updates are tidying up but have finished requesting
543  *		new buffers to modify (state not used for now)
544  * FLUSH:       All updates complete, but we are still writing to disk
545  * COMMIT:      All data on disk, writing commit record
546  * FINISHED:	We still have to keep the transaction for checkpointing.
547  *
548  * The transaction keeps track of all of the buffers modified by a
549  * running transaction, and all of the buffers committed but not yet
550  * flushed to home for finished transactions.
551  * (Locking Documentation improved by LockDoc)
552  */
553 
554 /*
555  * Lock ranking:
556  *
557  *    j_list_lock
558  *      ->jbd_lock_bh_journal_head()	(This is "innermost")
559  *
560  *    j_state_lock
561  *    ->b_state_lock
562  *
563  *    b_state_lock
564  *    ->j_list_lock
565  *
566  *    j_state_lock
567  *    ->j_list_lock			(journal_unmap_buffer)
568  *
569  */
570 
571 struct transaction_s
572 {
573 	/* Pointer to the journal for this transaction. [no locking] */
574 	journal_t		*t_journal;
575 
576 	/* Sequence number for this transaction [no locking] */
577 	tid_t			t_tid;
578 
579 	/*
580 	 * Transaction's current state
581 	 * [no locking - only kjournald2 alters this]
582 	 * [j_list_lock] guards transition of a transaction into T_FINISHED
583 	 * state and subsequent call of __jbd2_journal_drop_transaction()
584 	 * FIXME: needs barriers
585 	 * KLUDGE: [use j_state_lock]
586 	 */
587 	enum {
588 		T_RUNNING,
589 		T_LOCKED,
590 		T_SWITCH,
591 		T_FLUSH,
592 		T_COMMIT,
593 		T_COMMIT_DFLUSH,
594 		T_COMMIT_JFLUSH,
595 		T_COMMIT_CALLBACK,
596 		T_FINISHED
597 	}			t_state;
598 
599 	/*
600 	 * Where in the log does this transaction's commit start? [no locking]
601 	 */
602 	unsigned long		t_log_start;
603 
604 	/*
605 	 * Number of buffers on the t_buffers list [j_list_lock, no locks
606 	 * needed for jbd2 thread]
607 	 */
608 	int			t_nr_buffers;
609 
610 	/*
611 	 * Doubly-linked circular list of all buffers reserved but not yet
612 	 * modified by this transaction [j_list_lock, no locks needed fo
613 	 * jbd2 thread]
614 	 */
615 	struct journal_head	*t_reserved_list;
616 
617 	/*
618 	 * Doubly-linked circular list of all metadata buffers owned by this
619 	 * transaction [j_list_lock, no locks needed for jbd2 thread]
620 	 */
621 	struct journal_head	*t_buffers;
622 
623 	/*
624 	 * Doubly-linked circular list of all forget buffers (superseded
625 	 * buffers which we can un-checkpoint once this transaction commits)
626 	 * [j_list_lock]
627 	 */
628 	struct journal_head	*t_forget;
629 
630 	/*
631 	 * Doubly-linked circular list of all buffers still to be flushed before
632 	 * this transaction can be checkpointed. [j_list_lock]
633 	 */
634 	struct journal_head	*t_checkpoint_list;
635 
636 	/*
637 	 * Doubly-linked circular list of metadata buffers being
638 	 * shadowed by log IO.  The IO buffers on the iobuf list and
639 	 * the shadow buffers on this list match each other one for
640 	 * one at all times. [j_list_lock, no locks needed for jbd2
641 	 * thread]
642 	 */
643 	struct journal_head	*t_shadow_list;
644 
645 	/*
646 	 * List of inodes associated with the transaction; e.g., ext4 uses
647 	 * this to track inodes in data=ordered and data=journal mode that
648 	 * need special handling on transaction commit; also used by ocfs2.
649 	 * [j_list_lock]
650 	 */
651 	struct list_head	t_inode_list;
652 
653 	/*
654 	 * Longest time some handle had to wait for running transaction
655 	 */
656 	unsigned long		t_max_wait;
657 
658 	/*
659 	 * When transaction started
660 	 */
661 	unsigned long		t_start;
662 
663 	/*
664 	 * When commit was requested [j_state_lock]
665 	 */
666 	unsigned long		t_requested;
667 
668 	/*
669 	 * Checkpointing stats [j_list_lock]
670 	 */
671 	struct transaction_chp_stats_s t_chp_stats;
672 
673 	/*
674 	 * Number of outstanding updates running on this transaction
675 	 * [none]
676 	 */
677 	atomic_t		t_updates;
678 
679 	/*
680 	 * Number of blocks reserved for this transaction in the journal.
681 	 * This is including all credits reserved when starting transaction
682 	 * handles as well as all journal descriptor blocks needed for this
683 	 * transaction. [none]
684 	 */
685 	atomic_t		t_outstanding_credits;
686 
687 	/*
688 	 * Number of revoke records for this transaction added by already
689 	 * stopped handles. [none]
690 	 */
691 	atomic_t		t_outstanding_revokes;
692 
693 	/*
694 	 * How many handles used this transaction? [none]
695 	 */
696 	atomic_t		t_handle_count;
697 
698 	/*
699 	 * Forward and backward links for the circular list of all transactions
700 	 * awaiting checkpoint. [j_list_lock]
701 	 */
702 	transaction_t		*t_cpnext, *t_cpprev;
703 
704 	/*
705 	 * When will the transaction expire (become due for commit), in jiffies?
706 	 * [no locking]
707 	 */
708 	unsigned long		t_expires;
709 
710 	/*
711 	 * When this transaction started, in nanoseconds [no locking]
712 	 */
713 	ktime_t			t_start_time;
714 
715 	/*
716 	 * This transaction is being forced and some process is
717 	 * waiting for it to finish.
718 	 */
719 	unsigned int t_synchronous_commit:1;
720 
721 	/* Disk flush needs to be sent to fs partition [no locking] */
722 	int			t_need_data_flush;
723 };
724 
725 struct transaction_run_stats_s {
726 	unsigned long		rs_wait;
727 	unsigned long		rs_request_delay;
728 	unsigned long		rs_running;
729 	unsigned long		rs_locked;
730 	unsigned long		rs_flushing;
731 	unsigned long		rs_logging;
732 
733 	__u32			rs_handle_count;
734 	__u32			rs_blocks;
735 	__u32			rs_blocks_logged;
736 };
737 
738 struct transaction_stats_s {
739 	unsigned long		ts_tid;
740 	unsigned long		ts_requested;
741 	struct transaction_run_stats_s run;
742 };
743 
744 static inline unsigned long
jbd2_time_diff(unsigned long start,unsigned long end)745 jbd2_time_diff(unsigned long start, unsigned long end)
746 {
747 	if (end >= start)
748 		return end - start;
749 
750 	return end + (MAX_JIFFY_OFFSET - start);
751 }
752 
753 #define JBD2_NR_BATCH	64
754 
755 enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
756 
757 #define JBD2_FC_REPLAY_STOP	0
758 #define JBD2_FC_REPLAY_CONTINUE	1
759 
760 /**
761  * struct journal_s - The journal_s type is the concrete type associated with
762  *     journal_t.
763  */
764 struct journal_s
765 {
766 	/**
767 	 * @j_flags: General journaling state flags [j_state_lock,
768 	 * no lock for quick racy checks]
769 	 */
770 	unsigned long		j_flags;
771 
772 	/**
773 	 * @j_errno:
774 	 *
775 	 * Is there an outstanding uncleared error on the journal (from a prior
776 	 * abort)? [j_state_lock]
777 	 */
778 	int			j_errno;
779 
780 	/**
781 	 * @j_abort_mutex: Lock the whole aborting procedure.
782 	 */
783 	struct mutex		j_abort_mutex;
784 
785 	/**
786 	 * @j_sb_buffer: The first part of the superblock buffer.
787 	 */
788 	struct buffer_head	*j_sb_buffer;
789 
790 	/**
791 	 * @j_superblock: The second part of the superblock buffer.
792 	 */
793 	journal_superblock_t	*j_superblock;
794 
795 	/**
796 	 * @j_state_lock: Protect the various scalars in the journal.
797 	 */
798 	rwlock_t		j_state_lock;
799 
800 	/**
801 	 * @j_barrier_count:
802 	 *
803 	 * Number of processes waiting to create a barrier lock [j_state_lock,
804 	 * no lock for quick racy checks]
805 	 */
806 	int			j_barrier_count;
807 
808 	/**
809 	 * @j_barrier: The barrier lock itself.
810 	 */
811 	struct mutex		j_barrier;
812 
813 	/**
814 	 * @j_running_transaction:
815 	 *
816 	 * Transactions: The current running transaction...
817 	 * [j_state_lock, no lock for quick racy checks] [caller holding
818 	 * open handle]
819 	 */
820 	transaction_t		*j_running_transaction;
821 
822 	/**
823 	 * @j_committing_transaction:
824 	 *
825 	 * the transaction we are pushing to disk
826 	 * [j_state_lock] [caller holding open handle]
827 	 */
828 	transaction_t		*j_committing_transaction;
829 
830 	/**
831 	 * @j_checkpoint_transactions:
832 	 *
833 	 * ... and a linked circular list of all transactions waiting for
834 	 * checkpointing. [j_list_lock]
835 	 */
836 	transaction_t		*j_checkpoint_transactions;
837 
838 	/**
839 	 * @j_wait_transaction_locked:
840 	 *
841 	 * Wait queue for waiting for a locked transaction to start committing,
842 	 * or for a barrier lock to be released.
843 	 */
844 	wait_queue_head_t	j_wait_transaction_locked;
845 
846 	/**
847 	 * @j_wait_done_commit: Wait queue for waiting for commit to complete.
848 	 */
849 	wait_queue_head_t	j_wait_done_commit;
850 
851 	/**
852 	 * @j_wait_commit: Wait queue to trigger commit.
853 	 */
854 	wait_queue_head_t	j_wait_commit;
855 
856 	/**
857 	 * @j_wait_updates: Wait queue to wait for updates to complete.
858 	 */
859 	wait_queue_head_t	j_wait_updates;
860 
861 	/**
862 	 * @j_wait_reserved:
863 	 *
864 	 * Wait queue to wait for reserved buffer credits to drop.
865 	 */
866 	wait_queue_head_t	j_wait_reserved;
867 
868 	/**
869 	 * @j_fc_wait:
870 	 *
871 	 * Wait queue to wait for completion of async fast commits.
872 	 */
873 	wait_queue_head_t	j_fc_wait;
874 
875 	/**
876 	 * @j_checkpoint_mutex:
877 	 *
878 	 * Semaphore for locking against concurrent checkpoints.
879 	 */
880 	struct mutex		j_checkpoint_mutex;
881 
882 	/**
883 	 * @j_chkpt_bhs:
884 	 *
885 	 * List of buffer heads used by the checkpoint routine.  This
886 	 * was moved from jbd2_log_do_checkpoint() to reduce stack
887 	 * usage.  Access to this array is controlled by the
888 	 * @j_checkpoint_mutex.  [j_checkpoint_mutex]
889 	 */
890 	struct buffer_head	*j_chkpt_bhs[JBD2_NR_BATCH];
891 
892 	/**
893 	 * @j_shrinker:
894 	 *
895 	 * Journal head shrinker, reclaim buffer's journal head which
896 	 * has been written back.
897 	 */
898 	struct shrinker		*j_shrinker;
899 
900 	/**
901 	 * @j_checkpoint_jh_count:
902 	 *
903 	 * Number of journal buffers on the checkpoint list. [j_list_lock]
904 	 */
905 	struct percpu_counter	j_checkpoint_jh_count;
906 
907 	/**
908 	 * @j_shrink_transaction:
909 	 *
910 	 * Record next transaction will shrink on the checkpoint list.
911 	 * [j_list_lock]
912 	 */
913 	transaction_t		*j_shrink_transaction;
914 
915 	/**
916 	 * @j_head:
917 	 *
918 	 * Journal head: identifies the first unused block in the journal.
919 	 * [j_state_lock]
920 	 */
921 	unsigned long		j_head;
922 
923 	/**
924 	 * @j_tail:
925 	 *
926 	 * Journal tail: identifies the oldest still-used block in the journal.
927 	 * [j_state_lock]
928 	 */
929 	unsigned long		j_tail;
930 
931 	/**
932 	 * @j_free:
933 	 *
934 	 * Journal free: how many free blocks are there in the journal?
935 	 * [j_state_lock]
936 	 */
937 	unsigned long		j_free;
938 
939 	/**
940 	 * @j_first:
941 	 *
942 	 * The block number of the first usable block in the journal
943 	 * [j_state_lock].
944 	 */
945 	unsigned long		j_first;
946 
947 	/**
948 	 * @j_last:
949 	 *
950 	 * The block number one beyond the last usable block in the journal
951 	 * [j_state_lock].
952 	 */
953 	unsigned long		j_last;
954 
955 	/**
956 	 * @j_fc_first:
957 	 *
958 	 * The block number of the first fast commit block in the journal
959 	 * [j_state_lock].
960 	 */
961 	unsigned long		j_fc_first;
962 
963 	/**
964 	 * @j_fc_off:
965 	 *
966 	 * Number of fast commit blocks currently allocated. Accessed only
967 	 * during fast commit. Currently only process can do fast commit, so
968 	 * this field is not protected by any lock.
969 	 */
970 	unsigned long		j_fc_off;
971 
972 	/**
973 	 * @j_fc_last:
974 	 *
975 	 * The block number one beyond the last fast commit block in the journal
976 	 * [j_state_lock].
977 	 */
978 	unsigned long		j_fc_last;
979 
980 	/**
981 	 * @j_dev: Device where we store the journal.
982 	 */
983 	struct block_device	*j_dev;
984 
985 	/**
986 	 * @j_blocksize: Block size for the location where we store the journal.
987 	 */
988 	int			j_blocksize;
989 
990 	/**
991 	 * @j_blk_offset:
992 	 *
993 	 * Starting block offset into the device where we store the journal.
994 	 */
995 	unsigned long long	j_blk_offset;
996 
997 	/**
998 	 * @j_devname: Journal device name.
999 	 */
1000 	char			j_devname[BDEVNAME_SIZE+24];
1001 
1002 	/**
1003 	 * @j_fs_dev:
1004 	 *
1005 	 * Device which holds the client fs.  For internal journal this will be
1006 	 * equal to j_dev.
1007 	 */
1008 	struct block_device	*j_fs_dev;
1009 
1010 	/**
1011 	 * @j_fs_dev_wb_err:
1012 	 *
1013 	 * Records the errseq of the client fs's backing block device.
1014 	 */
1015 	errseq_t		j_fs_dev_wb_err;
1016 
1017 	/**
1018 	 * @j_total_len: Total maximum capacity of the journal region on disk.
1019 	 */
1020 	unsigned int		j_total_len;
1021 
1022 	/**
1023 	 * @j_reserved_credits:
1024 	 *
1025 	 * Number of buffers reserved from the running transaction.
1026 	 */
1027 	atomic_t		j_reserved_credits;
1028 
1029 	/**
1030 	 * @j_list_lock: Protects the buffer lists and internal buffer state.
1031 	 */
1032 	spinlock_t		j_list_lock;
1033 
1034 	/**
1035 	 * @j_inode:
1036 	 *
1037 	 * Optional inode where we store the journal.  If present, all
1038 	 * journal block numbers are mapped into this inode via bmap().
1039 	 */
1040 	struct inode		*j_inode;
1041 
1042 	/**
1043 	 * @j_tail_sequence:
1044 	 *
1045 	 * Sequence number of the oldest transaction in the log [j_state_lock]
1046 	 */
1047 	tid_t			j_tail_sequence;
1048 
1049 	/**
1050 	 * @j_transaction_sequence:
1051 	 *
1052 	 * Sequence number of the next transaction to grant [j_state_lock]
1053 	 */
1054 	tid_t			j_transaction_sequence;
1055 
1056 	/**
1057 	 * @j_commit_sequence:
1058 	 *
1059 	 * Sequence number of the most recently committed transaction
1060 	 * [j_state_lock, no lock for quick racy checks]
1061 	 */
1062 	tid_t			j_commit_sequence;
1063 
1064 	/**
1065 	 * @j_commit_request:
1066 	 *
1067 	 * Sequence number of the most recent transaction wanting commit
1068 	 * [j_state_lock, no lock for quick racy checks]
1069 	 */
1070 	tid_t			j_commit_request;
1071 
1072 	/**
1073 	 * @j_uuid:
1074 	 *
1075 	 * Journal uuid: identifies the object (filesystem, LVM volume etc)
1076 	 * backed by this journal.  This will eventually be replaced by an array
1077 	 * of uuids, allowing us to index multiple devices within a single
1078 	 * journal and to perform atomic updates across them.
1079 	 */
1080 	__u8			j_uuid[16];
1081 
1082 	/**
1083 	 * @j_task: Pointer to the current commit thread for this journal.
1084 	 */
1085 	struct task_struct	*j_task;
1086 
1087 	/**
1088 	 * @j_max_transaction_buffers:
1089 	 *
1090 	 * Maximum number of metadata buffers to allow in a single compound
1091 	 * commit transaction.
1092 	 */
1093 	int			j_max_transaction_buffers;
1094 
1095 	/**
1096 	 * @j_revoke_records_per_block:
1097 	 *
1098 	 * Number of revoke records that fit in one descriptor block.
1099 	 */
1100 	int			j_revoke_records_per_block;
1101 
1102 	/**
1103 	 * @j_transaction_overhead_buffers:
1104 	 *
1105 	 * Number of blocks each transaction needs for its own bookkeeping
1106 	 */
1107 	int			j_transaction_overhead_buffers;
1108 
1109 	/**
1110 	 * @j_commit_interval:
1111 	 *
1112 	 * What is the maximum transaction lifetime before we begin a commit?
1113 	 */
1114 	unsigned long		j_commit_interval;
1115 
1116 	/**
1117 	 * @j_commit_timer: The timer used to wakeup the commit thread.
1118 	 */
1119 	struct timer_list	j_commit_timer;
1120 
1121 	/**
1122 	 * @j_revoke_lock: Protect the revoke table.
1123 	 */
1124 	spinlock_t		j_revoke_lock;
1125 
1126 	/**
1127 	 * @j_revoke:
1128 	 *
1129 	 * The revoke table - maintains the list of revoked blocks in the
1130 	 * current transaction.
1131 	 */
1132 	struct jbd2_revoke_table_s *j_revoke;
1133 
1134 	/**
1135 	 * @j_revoke_table: Alternate revoke tables for j_revoke.
1136 	 */
1137 	struct jbd2_revoke_table_s *j_revoke_table[2];
1138 
1139 	/**
1140 	 * @j_wbuf: Array of bhs for jbd2_journal_commit_transaction.
1141 	 */
1142 	struct buffer_head	**j_wbuf;
1143 
1144 	/**
1145 	 * @j_fc_wbuf: Array of fast commit bhs for fast commit. Accessed only
1146 	 * during a fast commit. Currently only process can do fast commit, so
1147 	 * this field is not protected by any lock.
1148 	 */
1149 	struct buffer_head	**j_fc_wbuf;
1150 
1151 	/**
1152 	 * @j_wbufsize:
1153 	 *
1154 	 * Size of @j_wbuf array.
1155 	 */
1156 	int			j_wbufsize;
1157 
1158 	/**
1159 	 * @j_fc_wbufsize:
1160 	 *
1161 	 * Size of @j_fc_wbuf array.
1162 	 */
1163 	int			j_fc_wbufsize;
1164 
1165 	/**
1166 	 * @j_last_sync_writer:
1167 	 *
1168 	 * The pid of the last person to run a synchronous operation
1169 	 * through the journal.
1170 	 */
1171 	pid_t			j_last_sync_writer;
1172 
1173 	/**
1174 	 * @j_average_commit_time:
1175 	 *
1176 	 * The average amount of time in nanoseconds it takes to commit a
1177 	 * transaction to disk. [j_state_lock]
1178 	 */
1179 	u64			j_average_commit_time;
1180 
1181 	/**
1182 	 * @j_min_batch_time:
1183 	 *
1184 	 * Minimum time that we should wait for additional filesystem operations
1185 	 * to get batched into a synchronous handle in microseconds.
1186 	 */
1187 	u32			j_min_batch_time;
1188 
1189 	/**
1190 	 * @j_max_batch_time:
1191 	 *
1192 	 * Maximum time that we should wait for additional filesystem operations
1193 	 * to get batched into a synchronous handle in microseconds.
1194 	 */
1195 	u32			j_max_batch_time;
1196 
1197 	/**
1198 	 * @j_commit_callback:
1199 	 *
1200 	 * This function is called when a transaction is closed.
1201 	 */
1202 	void			(*j_commit_callback)(journal_t *,
1203 						     transaction_t *);
1204 
1205 	/**
1206 	 * @j_submit_inode_data_buffers:
1207 	 *
1208 	 * This function is called for all inodes associated with the
1209 	 * committing transaction marked with JI_WRITE_DATA flag
1210 	 * before we start to write out the transaction to the journal.
1211 	 */
1212 	int			(*j_submit_inode_data_buffers)
1213 					(struct jbd2_inode *);
1214 
1215 	/**
1216 	 * @j_finish_inode_data_buffers:
1217 	 *
1218 	 * This function is called for all inodes associated with the
1219 	 * committing transaction marked with JI_WAIT_DATA flag
1220 	 * after we have written the transaction to the journal
1221 	 * but before we write out the commit block.
1222 	 */
1223 	int			(*j_finish_inode_data_buffers)
1224 					(struct jbd2_inode *);
1225 
1226 	/*
1227 	 * Journal statistics
1228 	 */
1229 
1230 	/**
1231 	 * @j_history_lock: Protect the transactions statistics history.
1232 	 */
1233 	spinlock_t		j_history_lock;
1234 
1235 	/**
1236 	 * @j_proc_entry: procfs entry for the jbd statistics directory.
1237 	 */
1238 	struct proc_dir_entry	*j_proc_entry;
1239 
1240 	/**
1241 	 * @j_stats: Overall statistics.
1242 	 */
1243 	struct transaction_stats_s j_stats;
1244 
1245 	/**
1246 	 * @j_failed_commit: Failed journal commit ID.
1247 	 */
1248 	unsigned int		j_failed_commit;
1249 
1250 	/**
1251 	 * @j_private:
1252 	 *
1253 	 * An opaque pointer to fs-private information.  ext3 puts its
1254 	 * superblock pointer here.
1255 	 */
1256 	void *j_private;
1257 
1258 	/**
1259 	 * @j_csum_seed:
1260 	 *
1261 	 * Precomputed journal UUID checksum for seeding other checksums.
1262 	 */
1263 	__u32 j_csum_seed;
1264 
1265 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1266 	/**
1267 	 * @j_trans_commit_map:
1268 	 *
1269 	 * Lockdep entity to track transaction commit dependencies. Handles
1270 	 * hold this "lock" for read, when we wait for commit, we acquire the
1271 	 * "lock" for writing. This matches the properties of jbd2 journalling
1272 	 * where the running transaction has to wait for all handles to be
1273 	 * dropped to commit that transaction and also acquiring a handle may
1274 	 * require transaction commit to finish.
1275 	 */
1276 	struct lockdep_map	j_trans_commit_map;
1277 #endif
1278 	/**
1279 	 * @jbd2_trans_commit_key:
1280 	 *
1281 	 * "struct lock_class_key" for @j_trans_commit_map
1282 	 */
1283 	struct lock_class_key	jbd2_trans_commit_key;
1284 
1285 	/**
1286 	 * @j_fc_cleanup_callback:
1287 	 *
1288 	 * Clean-up after fast commit or full commit. JBD2 calls this function
1289 	 * after every commit operation.
1290 	 */
1291 	void (*j_fc_cleanup_callback)(struct journal_s *journal, int full, tid_t tid);
1292 
1293 	/**
1294 	 * @j_fc_replay_callback:
1295 	 *
1296 	 * File-system specific function that performs replay of a fast
1297 	 * commit. JBD2 calls this function for each fast commit block found in
1298 	 * the journal. This function should return JBD2_FC_REPLAY_CONTINUE
1299 	 * to indicate that the block was processed correctly and more fast
1300 	 * commit replay should continue. Return value of JBD2_FC_REPLAY_STOP
1301 	 * indicates the end of replay (no more blocks remaining). A negative
1302 	 * return value indicates error.
1303 	 */
1304 	int (*j_fc_replay_callback)(struct journal_s *journal,
1305 				    struct buffer_head *bh,
1306 				    enum passtype pass, int off,
1307 				    tid_t expected_commit_id);
1308 
1309 	/**
1310 	 * @j_bmap:
1311 	 *
1312 	 * Bmap function that should be used instead of the generic
1313 	 * VFS bmap function.
1314 	 */
1315 	int (*j_bmap)(struct journal_s *journal, sector_t *block);
1316 };
1317 
1318 #define jbd2_might_wait_for_commit(j) \
1319 	do { \
1320 		rwsem_acquire(&j->j_trans_commit_map, 0, 0, _THIS_IP_); \
1321 		rwsem_release(&j->j_trans_commit_map, _THIS_IP_); \
1322 	} while (0)
1323 
1324 /*
1325  * We can support any known requested features iff the
1326  * superblock is not in version 1.  Otherwise we fail to support any
1327  * extended sb features.
1328  */
jbd2_format_support_feature(journal_t * j)1329 static inline bool jbd2_format_support_feature(journal_t *j)
1330 {
1331 	return j->j_superblock->s_header.h_blocktype !=
1332 					cpu_to_be32(JBD2_SUPERBLOCK_V1);
1333 }
1334 
1335 /* journal feature predicate functions */
1336 #define JBD2_FEATURE_COMPAT_FUNCS(name, flagname) \
1337 static inline bool jbd2_has_feature_##name(journal_t *j) \
1338 { \
1339 	return (jbd2_format_support_feature(j) && \
1340 		((j)->j_superblock->s_feature_compat & \
1341 		 cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname)) != 0); \
1342 } \
1343 static inline void jbd2_set_feature_##name(journal_t *j) \
1344 { \
1345 	(j)->j_superblock->s_feature_compat |= \
1346 		cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname); \
1347 } \
1348 static inline void jbd2_clear_feature_##name(journal_t *j) \
1349 { \
1350 	(j)->j_superblock->s_feature_compat &= \
1351 		~cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname); \
1352 }
1353 
1354 #define JBD2_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
1355 static inline bool jbd2_has_feature_##name(journal_t *j) \
1356 { \
1357 	return (jbd2_format_support_feature(j) && \
1358 		((j)->j_superblock->s_feature_ro_compat & \
1359 		 cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname)) != 0); \
1360 } \
1361 static inline void jbd2_set_feature_##name(journal_t *j) \
1362 { \
1363 	(j)->j_superblock->s_feature_ro_compat |= \
1364 		cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname); \
1365 } \
1366 static inline void jbd2_clear_feature_##name(journal_t *j) \
1367 { \
1368 	(j)->j_superblock->s_feature_ro_compat &= \
1369 		~cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname); \
1370 }
1371 
1372 #define JBD2_FEATURE_INCOMPAT_FUNCS(name, flagname) \
1373 static inline bool jbd2_has_feature_##name(journal_t *j) \
1374 { \
1375 	return (jbd2_format_support_feature(j) && \
1376 		((j)->j_superblock->s_feature_incompat & \
1377 		 cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname)) != 0); \
1378 } \
1379 static inline void jbd2_set_feature_##name(journal_t *j) \
1380 { \
1381 	(j)->j_superblock->s_feature_incompat |= \
1382 		cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname); \
1383 } \
1384 static inline void jbd2_clear_feature_##name(journal_t *j) \
1385 { \
1386 	(j)->j_superblock->s_feature_incompat &= \
1387 		~cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname); \
1388 }
1389 
1390 JBD2_FEATURE_COMPAT_FUNCS(checksum,		CHECKSUM)
1391 
1392 JBD2_FEATURE_INCOMPAT_FUNCS(revoke,		REVOKE)
1393 JBD2_FEATURE_INCOMPAT_FUNCS(64bit,		64BIT)
1394 JBD2_FEATURE_INCOMPAT_FUNCS(async_commit,	ASYNC_COMMIT)
1395 JBD2_FEATURE_INCOMPAT_FUNCS(csum2,		CSUM_V2)
1396 JBD2_FEATURE_INCOMPAT_FUNCS(csum3,		CSUM_V3)
1397 JBD2_FEATURE_INCOMPAT_FUNCS(fast_commit,	FAST_COMMIT)
1398 
1399 /* Journal high priority write IO operation flags */
1400 #define JBD2_JOURNAL_REQ_FLAGS		(REQ_META | REQ_SYNC | REQ_IDLE)
1401 
1402 /*
1403  * Journal flag definitions
1404  */
1405 #define JBD2_UNMOUNT	0x001	/* Journal thread is being destroyed */
1406 #define JBD2_ABORT	0x002	/* Journaling has been aborted for errors. */
1407 #define JBD2_ACK_ERR	0x004	/* The errno in the sb has been acked */
1408 #define JBD2_FLUSHED	0x008	/* The journal superblock has been flushed */
1409 #define JBD2_LOADED	0x010	/* The journal superblock has been loaded */
1410 #define JBD2_BARRIER	0x020	/* Use IDE barriers */
1411 #define JBD2_CYCLE_RECORD		0x080	/* Journal cycled record log on
1412 						 * clean and empty filesystem
1413 						 * logging area */
1414 #define JBD2_FAST_COMMIT_ONGOING	0x100	/* Fast commit is ongoing */
1415 #define JBD2_FULL_COMMIT_ONGOING	0x200	/* Full commit is ongoing */
1416 #define JBD2_JOURNAL_FLUSH_DISCARD	0x0001
1417 #define JBD2_JOURNAL_FLUSH_ZEROOUT	0x0002
1418 #define JBD2_JOURNAL_FLUSH_VALID	(JBD2_JOURNAL_FLUSH_DISCARD | \
1419 					JBD2_JOURNAL_FLUSH_ZEROOUT)
1420 
1421 /*
1422  * Function declarations for the journaling transaction and buffer
1423  * management
1424  */
1425 
1426 /* Filing buffers */
1427 extern bool __jbd2_journal_refile_buffer(struct journal_head *);
1428 extern void jbd2_journal_refile_buffer(journal_t *, struct journal_head *);
1429 extern void __jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
1430 extern void jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
jbd2_file_log_bh(struct list_head * head,struct buffer_head * bh)1431 static inline void jbd2_file_log_bh(struct list_head *head, struct buffer_head *bh)
1432 {
1433 	list_add_tail(&bh->b_assoc_buffers, head);
1434 }
jbd2_unfile_log_bh(struct buffer_head * bh)1435 static inline void jbd2_unfile_log_bh(struct buffer_head *bh)
1436 {
1437 	list_del_init(&bh->b_assoc_buffers);
1438 }
1439 
1440 /* Log buffer allocation */
1441 struct buffer_head *jbd2_journal_get_descriptor_buffer(transaction_t *, int);
1442 void jbd2_descriptor_block_csum_set(journal_t *, struct buffer_head *);
1443 int jbd2_journal_next_log_block(journal_t *, unsigned long long *);
1444 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
1445 			      unsigned long *block);
1446 int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block);
1447 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block);
1448 
1449 /* Commit management */
1450 extern void jbd2_journal_commit_transaction(journal_t *);
1451 
1452 /* Checkpoint list management */
1453 enum jbd2_shrink_type {JBD2_SHRINK_DESTROY, JBD2_SHRINK_BUSY_STOP, JBD2_SHRINK_BUSY_SKIP};
1454 
1455 void __jbd2_journal_clean_checkpoint_list(journal_t *journal, enum jbd2_shrink_type type);
1456 unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal, unsigned long *nr_to_scan);
1457 int __jbd2_journal_remove_checkpoint(struct journal_head *);
1458 int jbd2_journal_try_remove_checkpoint(struct journal_head *jh);
1459 void jbd2_journal_destroy_checkpoint(journal_t *journal);
1460 void __jbd2_journal_insert_checkpoint(struct journal_head *, transaction_t *);
1461 
1462 
1463 /*
1464  * Triggers
1465  */
1466 
1467 struct jbd2_buffer_trigger_type {
1468 	/*
1469 	 * Fired a the moment data to write to the journal are known to be
1470 	 * stable - so either at the moment b_frozen_data is created or just
1471 	 * before a buffer is written to the journal.  mapped_data is a mapped
1472 	 * buffer that is the frozen data for commit.
1473 	 */
1474 	void (*t_frozen)(struct jbd2_buffer_trigger_type *type,
1475 			 struct buffer_head *bh, void *mapped_data,
1476 			 size_t size);
1477 
1478 	/*
1479 	 * Fired during journal abort for dirty buffers that will not be
1480 	 * committed.
1481 	 */
1482 	void (*t_abort)(struct jbd2_buffer_trigger_type *type,
1483 			struct buffer_head *bh);
1484 };
1485 
1486 extern void jbd2_buffer_frozen_trigger(struct journal_head *jh,
1487 				       void *mapped_data,
1488 				       struct jbd2_buffer_trigger_type *triggers);
1489 extern void jbd2_buffer_abort_trigger(struct journal_head *jh,
1490 				      struct jbd2_buffer_trigger_type *triggers);
1491 
1492 /* Buffer IO */
1493 extern int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
1494 					      struct journal_head *jh_in,
1495 					      struct buffer_head **bh_out,
1496 					      sector_t blocknr);
1497 
1498 /* Transaction cache support */
1499 extern void jbd2_journal_destroy_transaction_cache(void);
1500 extern int __init jbd2_journal_init_transaction_cache(void);
1501 extern void jbd2_journal_free_transaction(transaction_t *);
1502 
1503 /*
1504  * Journal locking.
1505  *
1506  * We need to lock the journal during transaction state changes so that nobody
1507  * ever tries to take a handle on the running transaction while we are in the
1508  * middle of moving it to the commit phase.  j_state_lock does this.
1509  *
1510  * Note that the locking is completely interrupt unsafe.  We never touch
1511  * journal structures from interrupts.
1512  */
1513 
journal_current_handle(void)1514 static inline handle_t *journal_current_handle(void)
1515 {
1516 	return current->journal_info;
1517 }
1518 
1519 /* The journaling code user interface:
1520  *
1521  * Create and destroy handles
1522  * Register buffer modifications against the current transaction.
1523  */
1524 
1525 extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
1526 extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
1527 				     int revoke_records, gfp_t gfp_mask,
1528 				     unsigned int type, unsigned int line_no);
1529 extern int	 jbd2_journal_restart(handle_t *, int nblocks);
1530 extern int	 jbd2__journal_restart(handle_t *, int nblocks,
1531 				       int revoke_records, gfp_t gfp_mask);
1532 extern int	 jbd2_journal_start_reserved(handle_t *handle,
1533 				unsigned int type, unsigned int line_no);
1534 extern void	 jbd2_journal_free_reserved(handle_t *handle);
1535 extern int	 jbd2_journal_extend(handle_t *handle, int nblocks,
1536 				     int revoke_records);
1537 extern int	 jbd2_journal_get_write_access(handle_t *, struct buffer_head *);
1538 extern int	 jbd2_journal_get_create_access (handle_t *, struct buffer_head *);
1539 extern int	 jbd2_journal_get_undo_access(handle_t *, struct buffer_head *);
1540 void		 jbd2_journal_set_triggers(struct buffer_head *,
1541 					   struct jbd2_buffer_trigger_type *type);
1542 extern int	 jbd2_journal_dirty_metadata (handle_t *, struct buffer_head *);
1543 extern int	 jbd2_journal_forget (handle_t *, struct buffer_head *);
1544 int jbd2_journal_invalidate_folio(journal_t *, struct folio *,
1545 					size_t offset, size_t length);
1546 bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio);
1547 extern int	 jbd2_journal_stop(handle_t *);
1548 extern int	 jbd2_journal_flush(journal_t *journal, unsigned int flags);
1549 extern void	 jbd2_journal_lock_updates (journal_t *);
1550 extern void	 jbd2_journal_unlock_updates (journal_t *);
1551 
1552 void jbd2_journal_wait_updates(journal_t *);
1553 
1554 extern journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1555 				struct block_device *fs_dev,
1556 				unsigned long long start, int len, int bsize);
1557 extern journal_t * jbd2_journal_init_inode (struct inode *);
1558 extern int	   jbd2_journal_update_format (journal_t *);
1559 extern int	   jbd2_journal_check_used_features
1560 		   (journal_t *, unsigned long, unsigned long, unsigned long);
1561 extern int	   jbd2_journal_check_available_features
1562 		   (journal_t *, unsigned long, unsigned long, unsigned long);
1563 extern int	   jbd2_journal_set_features
1564 		   (journal_t *, unsigned long, unsigned long, unsigned long);
1565 extern void	   jbd2_journal_clear_features
1566 		   (journal_t *, unsigned long, unsigned long, unsigned long);
1567 extern int	   jbd2_journal_load       (journal_t *journal);
1568 extern int	   jbd2_journal_destroy    (journal_t *);
1569 extern int	   jbd2_journal_recover    (journal_t *journal);
1570 extern int	   jbd2_journal_wipe       (journal_t *, int);
1571 extern int	   jbd2_journal_skip_recovery	(journal_t *);
1572 extern void	   jbd2_journal_update_sb_errno(journal_t *);
1573 extern int	   jbd2_journal_update_sb_log_tail	(journal_t *, tid_t,
1574 				unsigned long, blk_opf_t);
1575 extern void	   jbd2_journal_abort      (journal_t *, int);
1576 extern int	   jbd2_journal_errno      (journal_t *);
1577 extern void	   jbd2_journal_ack_err    (journal_t *);
1578 extern int	   jbd2_journal_clear_err  (journal_t *);
1579 extern int	   jbd2_journal_bmap(journal_t *, unsigned long, unsigned long long *);
1580 extern int	   jbd2_journal_force_commit(journal_t *);
1581 extern int	   jbd2_journal_force_commit_nested(journal_t *);
1582 extern int	   jbd2_journal_inode_ranged_write(handle_t *handle,
1583 			struct jbd2_inode *inode, loff_t start_byte,
1584 			loff_t length);
1585 extern int	   jbd2_journal_inode_ranged_wait(handle_t *handle,
1586 			struct jbd2_inode *inode, loff_t start_byte,
1587 			loff_t length);
1588 extern int	   jbd2_journal_finish_inode_data_buffers(
1589 			struct jbd2_inode *jinode);
1590 extern int	   jbd2_journal_begin_ordered_truncate(journal_t *journal,
1591 				struct jbd2_inode *inode, loff_t new_size);
1592 extern void	   jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode);
1593 extern void	   jbd2_journal_release_jbd_inode(journal_t *journal, struct jbd2_inode *jinode);
1594 
1595 /*
1596  * journal_head management
1597  */
1598 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh);
1599 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh);
1600 void jbd2_journal_put_journal_head(struct journal_head *jh);
1601 
1602 /*
1603  * handle management
1604  */
1605 extern struct kmem_cache *jbd2_handle_cache;
1606 
1607 /*
1608  * This specialized allocator has to be a macro for its allocations to be
1609  * accounted separately (to have a separate alloc_tag). The typecast is
1610  * intentional to enforce typesafety.
1611  */
1612 #define jbd2_alloc_handle(_gfp_flags)	\
1613 		((handle_t *)kmem_cache_zalloc(jbd2_handle_cache, _gfp_flags))
1614 
jbd2_free_handle(handle_t * handle)1615 static inline void jbd2_free_handle(handle_t *handle)
1616 {
1617 	kmem_cache_free(jbd2_handle_cache, handle);
1618 }
1619 
1620 /*
1621  * jbd2_inode management (optional, for those file systems that want to use
1622  * dynamically allocated jbd2_inode structures)
1623  */
1624 extern struct kmem_cache *jbd2_inode_cache;
1625 
1626 /*
1627  * This specialized allocator has to be a macro for its allocations to be
1628  * accounted separately (to have a separate alloc_tag). The typecast is
1629  * intentional to enforce typesafety.
1630  */
1631 #define jbd2_alloc_inode(_gfp_flags)	\
1632 		((struct jbd2_inode *)kmem_cache_alloc(jbd2_inode_cache, _gfp_flags))
1633 
jbd2_free_inode(struct jbd2_inode * jinode)1634 static inline void jbd2_free_inode(struct jbd2_inode *jinode)
1635 {
1636 	kmem_cache_free(jbd2_inode_cache, jinode);
1637 }
1638 
1639 /* Primary revoke support */
1640 #define JOURNAL_REVOKE_DEFAULT_HASH 256
1641 extern int	   jbd2_journal_init_revoke(journal_t *, int);
1642 extern void	   jbd2_journal_destroy_revoke_record_cache(void);
1643 extern void	   jbd2_journal_destroy_revoke_table_cache(void);
1644 extern int __init jbd2_journal_init_revoke_record_cache(void);
1645 extern int __init jbd2_journal_init_revoke_table_cache(void);
1646 struct jbd2_revoke_table_s *jbd2_journal_init_revoke_table(int hash_size);
1647 void jbd2_journal_destroy_revoke_table(struct jbd2_revoke_table_s *table);
1648 
1649 extern void	   jbd2_journal_destroy_revoke(journal_t *);
1650 extern int	   jbd2_journal_revoke (handle_t *, unsigned long long, struct buffer_head *);
1651 extern void	   jbd2_journal_cancel_revoke(handle_t *, struct journal_head *);
1652 extern void	   jbd2_journal_write_revoke_records(transaction_t *transaction,
1653 						     struct list_head *log_bufs);
1654 
1655 /* Recovery revoke support */
1656 extern int	jbd2_journal_set_revoke(journal_t *, unsigned long long, tid_t);
1657 extern int	jbd2_journal_test_revoke(journal_t *, unsigned long long, tid_t);
1658 extern void	jbd2_journal_clear_revoke(journal_t *);
1659 extern void	jbd2_journal_switch_revoke_table(journal_t *journal);
1660 extern void	jbd2_clear_buffer_revoked_flags(journal_t *journal);
1661 
1662 /*
1663  * The log thread user interface:
1664  *
1665  * Request space in the current transaction, and force transaction commit
1666  * transitions on demand.
1667  */
1668 
1669 int jbd2_log_start_commit(journal_t *journal, tid_t tid);
1670 int jbd2_journal_start_commit(journal_t *journal, tid_t *tid);
1671 int jbd2_log_wait_commit(journal_t *journal, tid_t tid);
1672 int jbd2_transaction_committed(journal_t *journal, tid_t tid);
1673 int jbd2_complete_transaction(journal_t *journal, tid_t tid);
1674 int jbd2_log_do_checkpoint(journal_t *journal);
1675 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid);
1676 
1677 void __jbd2_log_wait_for_space(journal_t *journal);
1678 extern void __jbd2_journal_drop_transaction(journal_t *, transaction_t *);
1679 extern int jbd2_cleanup_journal_tail(journal_t *);
1680 
1681 /* Fast commit related APIs */
1682 int jbd2_fc_begin_commit(journal_t *journal, tid_t tid);
1683 int jbd2_fc_end_commit(journal_t *journal);
1684 int jbd2_fc_end_commit_fallback(journal_t *journal);
1685 int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out);
1686 int jbd2_submit_inode_data(journal_t *journal, struct jbd2_inode *jinode);
1687 int jbd2_wait_inode_data(journal_t *journal, struct jbd2_inode *jinode);
1688 int jbd2_fc_wait_bufs(journal_t *journal, int num_blks);
1689 void jbd2_fc_release_bufs(journal_t *journal);
1690 
1691 /*
1692  * is_journal_abort
1693  *
1694  * Simple test wrapper function to test the JBD2_ABORT state flag.  This
1695  * bit, when set, indicates that we have had a fatal error somewhere,
1696  * either inside the journaling layer or indicated to us by the client
1697  * (eg. ext3), and that we and should not commit any further
1698  * transactions.
1699  */
1700 
is_journal_aborted(journal_t * journal)1701 static inline int is_journal_aborted(journal_t *journal)
1702 {
1703 	return journal->j_flags & JBD2_ABORT;
1704 }
1705 
is_handle_aborted(handle_t * handle)1706 static inline int is_handle_aborted(handle_t *handle)
1707 {
1708 	if (handle->h_aborted || !handle->h_transaction)
1709 		return 1;
1710 	return is_journal_aborted(handle->h_transaction->t_journal);
1711 }
1712 
jbd2_journal_abort_handle(handle_t * handle)1713 static inline void jbd2_journal_abort_handle(handle_t *handle)
1714 {
1715 	handle->h_aborted = 1;
1716 }
1717 
jbd2_init_fs_dev_write_error(journal_t * journal)1718 static inline void jbd2_init_fs_dev_write_error(journal_t *journal)
1719 {
1720 	struct address_space *mapping = journal->j_fs_dev->bd_mapping;
1721 
1722 	/*
1723 	 * Save the original wb_err value of client fs's bdev mapping which
1724 	 * could be used to detect the client fs's metadata async write error.
1725 	 */
1726 	errseq_check_and_advance(&mapping->wb_err, &journal->j_fs_dev_wb_err);
1727 }
1728 
jbd2_check_fs_dev_write_error(journal_t * journal)1729 static inline int jbd2_check_fs_dev_write_error(journal_t *journal)
1730 {
1731 	struct address_space *mapping = journal->j_fs_dev->bd_mapping;
1732 
1733 	return errseq_check(&mapping->wb_err,
1734 			    READ_ONCE(journal->j_fs_dev_wb_err));
1735 }
1736 
1737 #endif /* __KERNEL__   */
1738 
1739 /* Comparison functions for transaction IDs: perform comparisons using
1740  * modulo arithmetic so that they work over sequence number wraps. */
1741 
tid_gt(tid_t x,tid_t y)1742 static inline int tid_gt(tid_t x, tid_t y)
1743 {
1744 	int difference = (x - y);
1745 	return (difference > 0);
1746 }
1747 
tid_geq(tid_t x,tid_t y)1748 static inline int tid_geq(tid_t x, tid_t y)
1749 {
1750 	int difference = (x - y);
1751 	return (difference >= 0);
1752 }
1753 
1754 extern int jbd2_journal_blocks_per_folio(struct inode *inode);
1755 extern size_t journal_tag_bytes(journal_t *journal);
1756 
jbd2_journal_has_csum_v2or3(journal_t * journal)1757 static inline int jbd2_journal_has_csum_v2or3(journal_t *journal)
1758 {
1759 	return jbd2_has_feature_csum2(journal) ||
1760 	       jbd2_has_feature_csum3(journal);
1761 }
1762 
jbd2_journal_get_num_fc_blks(journal_superblock_t * jsb)1763 static inline int jbd2_journal_get_num_fc_blks(journal_superblock_t *jsb)
1764 {
1765 	int num_fc_blocks = be32_to_cpu(jsb->s_num_fc_blks);
1766 
1767 	return num_fc_blocks ? num_fc_blocks : JBD2_DEFAULT_FAST_COMMIT_BLOCKS;
1768 }
1769 
1770 /*
1771  * Return number of free blocks in the log. Must be called under j_state_lock.
1772  */
jbd2_log_space_left(journal_t * journal)1773 static inline unsigned long jbd2_log_space_left(journal_t *journal)
1774 {
1775 	/* Allow for rounding errors */
1776 	long free = journal->j_free - 32;
1777 
1778 	if (journal->j_committing_transaction) {
1779 		free -= atomic_read(&journal->
1780                         j_committing_transaction->t_outstanding_credits);
1781 	}
1782 	return max_t(long, free, 0);
1783 }
1784 
1785 /*
1786  * Definitions which augment the buffer_head layer
1787  */
1788 
1789 /* journaling buffer types */
1790 #define BJ_None		0	/* Not journaled */
1791 #define BJ_Metadata	1	/* Normal journaled metadata */
1792 #define BJ_Forget	2	/* Buffer superseded by this transaction */
1793 #define BJ_Shadow	3	/* Buffer contents being shadowed to the log */
1794 #define BJ_Reserved	4	/* Buffer is reserved for access by journal */
1795 #define BJ_Types	5
1796 
jbd2_chksum(u32 crc,const void * address,unsigned int length)1797 static inline u32 jbd2_chksum(u32 crc, const void *address, unsigned int length)
1798 {
1799 	return crc32c(crc, address, length);
1800 }
1801 
1802 /* Return most recent uncommitted transaction */
jbd2_get_latest_transaction(journal_t * journal)1803 static inline tid_t  jbd2_get_latest_transaction(journal_t *journal)
1804 {
1805 	tid_t tid;
1806 
1807 	read_lock(&journal->j_state_lock);
1808 	tid = journal->j_commit_request;
1809 	if (journal->j_running_transaction)
1810 		tid = journal->j_running_transaction->t_tid;
1811 	read_unlock(&journal->j_state_lock);
1812 	return tid;
1813 }
1814 
jbd2_handle_buffer_credits(handle_t * handle)1815 static inline int jbd2_handle_buffer_credits(handle_t *handle)
1816 {
1817 	journal_t *journal;
1818 
1819 	if (!handle->h_reserved)
1820 		journal = handle->h_transaction->t_journal;
1821 	else
1822 		journal = handle->h_journal;
1823 
1824 	return handle->h_total_credits -
1825 		DIV_ROUND_UP(handle->h_revoke_credits_requested,
1826 			     journal->j_revoke_records_per_block);
1827 }
1828 
1829 #ifdef __KERNEL__
1830 
1831 #define buffer_trace_init(bh)	do {} while (0)
1832 #define print_buffer_fields(bh)	do {} while (0)
1833 #define print_buffer_trace(bh)	do {} while (0)
1834 #define BUFFER_TRACE(bh, info)	do {} while (0)
1835 #define BUFFER_TRACE2(bh, bh2, info)	do {} while (0)
1836 #define JBUFFER_TRACE(jh, info)	do {} while (0)
1837 
1838 #endif	/* __KERNEL__ */
1839 
1840 #endif	/* _LINUX_JBD2_H */
1841