xref: /openbmc/linux/fs/nilfs2/segbuf.h (revision 94ee1d91)
1ae98043fSRyusuke Konishi /* SPDX-License-Identifier: GPL-2.0+ */
264b5a32eSRyusuke Konishi /*
3*94ee1d91SRyusuke Konishi  * NILFS Segment buffer prototypes and definitions
464b5a32eSRyusuke Konishi  *
564b5a32eSRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
664b5a32eSRyusuke Konishi  *
74b420ab4SRyusuke Konishi  * Written by Ryusuke Konishi.
864b5a32eSRyusuke Konishi  *
964b5a32eSRyusuke Konishi  */
1064b5a32eSRyusuke Konishi #ifndef _NILFS_SEGBUF_H
1164b5a32eSRyusuke Konishi #define _NILFS_SEGBUF_H
1264b5a32eSRyusuke Konishi 
1364b5a32eSRyusuke Konishi #include <linux/fs.h>
1464b5a32eSRyusuke Konishi #include <linux/buffer_head.h>
1564b5a32eSRyusuke Konishi #include <linux/bio.h>
1664b5a32eSRyusuke Konishi #include <linux/completion.h>
1764b5a32eSRyusuke Konishi 
1864b5a32eSRyusuke Konishi /**
1964b5a32eSRyusuke Konishi  * struct nilfs_segsum_info - On-memory segment summary
2064b5a32eSRyusuke Konishi  * @flags: Flags
2164b5a32eSRyusuke Konishi  * @nfinfo: Number of file information structures
2264b5a32eSRyusuke Konishi  * @nblocks: Number of blocks included in the partial segment
2364b5a32eSRyusuke Konishi  * @nsumblk: Number of summary blocks
2464b5a32eSRyusuke Konishi  * @sumbytes: Byte count of segment summary
2564b5a32eSRyusuke Konishi  * @nfileblk: Total number of file blocks
2664b5a32eSRyusuke Konishi  * @seg_seq: Segment sequence number
2750614bcfSRyusuke Konishi  * @cno: Checkpoint number
2864b5a32eSRyusuke Konishi  * @ctime: Creation time
2964b5a32eSRyusuke Konishi  * @next: Block number of the next full segment
3064b5a32eSRyusuke Konishi  */
3164b5a32eSRyusuke Konishi struct nilfs_segsum_info {
3264b5a32eSRyusuke Konishi 	unsigned int		flags;
3364b5a32eSRyusuke Konishi 	unsigned long		nfinfo;
3464b5a32eSRyusuke Konishi 	unsigned long		nblocks;
3564b5a32eSRyusuke Konishi 	unsigned long		nsumblk;
3664b5a32eSRyusuke Konishi 	unsigned long		sumbytes;
3764b5a32eSRyusuke Konishi 	unsigned long		nfileblk;
3864b5a32eSRyusuke Konishi 	u64			seg_seq;
3950614bcfSRyusuke Konishi 	__u64			cno;
40fb04b91bSArnd Bergmann 	time64_t		ctime;
4164b5a32eSRyusuke Konishi 	sector_t		next;
4264b5a32eSRyusuke Konishi };
4364b5a32eSRyusuke Konishi 
4464b5a32eSRyusuke Konishi /**
4564b5a32eSRyusuke Konishi  * struct nilfs_segment_buffer - Segment buffer
4664b5a32eSRyusuke Konishi  * @sb_super: back pointer to a superblock struct
4764b5a32eSRyusuke Konishi  * @sb_list: List head to chain this structure
4864b5a32eSRyusuke Konishi  * @sb_sum: On-memory segment summary
4964b5a32eSRyusuke Konishi  * @sb_segnum: Index number of the full segment
5064b5a32eSRyusuke Konishi  * @sb_nextnum: Index number of the next full segment
5164b5a32eSRyusuke Konishi  * @sb_fseg_start: Start block number of the full segment
5264b5a32eSRyusuke Konishi  * @sb_fseg_end: End block number of the full segment
5364b5a32eSRyusuke Konishi  * @sb_pseg_start: Disk block number of partial segment
5464b5a32eSRyusuke Konishi  * @sb_rest_blocks: Number of residual blocks in the current segment
5564b5a32eSRyusuke Konishi  * @sb_segsum_buffers: List of buffers for segment summaries
5664b5a32eSRyusuke Konishi  * @sb_payload_buffers: List of buffers for segment payload
571e2b68bfSRyusuke Konishi  * @sb_super_root: Pointer to buffer storing a super root block (if exists)
589284ad2aSRyusuke Konishi  * @sb_nbio: Number of flying bio requests
599284ad2aSRyusuke Konishi  * @sb_err: I/O error status
609284ad2aSRyusuke Konishi  * @sb_bio_event: Completion event of log writing
6164b5a32eSRyusuke Konishi  */
6264b5a32eSRyusuke Konishi struct nilfs_segment_buffer {
6364b5a32eSRyusuke Konishi 	struct super_block     *sb_super;
6464b5a32eSRyusuke Konishi 	struct list_head	sb_list;
6564b5a32eSRyusuke Konishi 
6664b5a32eSRyusuke Konishi 	/* Segment information */
6764b5a32eSRyusuke Konishi 	struct nilfs_segsum_info sb_sum;
6864b5a32eSRyusuke Konishi 	__u64			sb_segnum;
6964b5a32eSRyusuke Konishi 	__u64			sb_nextnum;
7064b5a32eSRyusuke Konishi 	sector_t		sb_fseg_start, sb_fseg_end;
7164b5a32eSRyusuke Konishi 	sector_t		sb_pseg_start;
720c6c44cbSRyusuke Konishi 	unsigned int		sb_rest_blocks;
7364b5a32eSRyusuke Konishi 
7464b5a32eSRyusuke Konishi 	/* Buffers */
7564b5a32eSRyusuke Konishi 	struct list_head	sb_segsum_buffers;
7664b5a32eSRyusuke Konishi 	struct list_head	sb_payload_buffers; /* including super root */
771e2b68bfSRyusuke Konishi 	struct buffer_head     *sb_super_root;
7864b5a32eSRyusuke Konishi 
7964b5a32eSRyusuke Konishi 	/* io status */
809284ad2aSRyusuke Konishi 	int			sb_nbio;
819284ad2aSRyusuke Konishi 	atomic_t		sb_err;
829284ad2aSRyusuke Konishi 	struct completion	sb_bio_event;
8364b5a32eSRyusuke Konishi };
8464b5a32eSRyusuke Konishi 
8564b5a32eSRyusuke Konishi #define NILFS_LIST_SEGBUF(head)  \
8664b5a32eSRyusuke Konishi 	list_entry((head), struct nilfs_segment_buffer, sb_list)
8764b5a32eSRyusuke Konishi #define NILFS_NEXT_SEGBUF(segbuf)  NILFS_LIST_SEGBUF((segbuf)->sb_list.next)
8864b5a32eSRyusuke Konishi #define NILFS_PREV_SEGBUF(segbuf)  NILFS_LIST_SEGBUF((segbuf)->sb_list.prev)
8964b5a32eSRyusuke Konishi #define NILFS_LAST_SEGBUF(head)    NILFS_LIST_SEGBUF((head)->prev)
9064b5a32eSRyusuke Konishi #define NILFS_FIRST_SEGBUF(head)   NILFS_LIST_SEGBUF((head)->next)
9164b5a32eSRyusuke Konishi #define NILFS_SEGBUF_IS_LAST(segbuf, head)  ((segbuf)->sb_list.next == (head))
9264b5a32eSRyusuke Konishi 
9364b5a32eSRyusuke Konishi #define nilfs_for_each_segbuf_before(s, t, h) \
9464b5a32eSRyusuke Konishi 	for ((s) = NILFS_FIRST_SEGBUF(h); (s) != (t); \
9564b5a32eSRyusuke Konishi 	     (s) = NILFS_NEXT_SEGBUF(s))
9664b5a32eSRyusuke Konishi 
9764b5a32eSRyusuke Konishi #define NILFS_SEGBUF_FIRST_BH(head)  \
9864b5a32eSRyusuke Konishi 	(list_entry((head)->next, struct buffer_head, b_assoc_buffers))
9964b5a32eSRyusuke Konishi #define NILFS_SEGBUF_NEXT_BH(bh)  \
10064b5a32eSRyusuke Konishi 	(list_entry((bh)->b_assoc_buffers.next, struct buffer_head, \
10164b5a32eSRyusuke Konishi 		    b_assoc_buffers))
10264b5a32eSRyusuke Konishi #define NILFS_SEGBUF_BH_IS_LAST(bh, head)  ((bh)->b_assoc_buffers.next == head)
10364b5a32eSRyusuke Konishi 
10441c88bd7SLi Hong extern struct kmem_cache *nilfs_segbuf_cachep;
10564b5a32eSRyusuke Konishi 
10664b5a32eSRyusuke Konishi struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *);
10764b5a32eSRyusuke Konishi void nilfs_segbuf_free(struct nilfs_segment_buffer *);
108cece5520SRyusuke Konishi void nilfs_segbuf_map(struct nilfs_segment_buffer *, __u64, unsigned long,
10964b5a32eSRyusuke Konishi 		      struct the_nilfs *);
110a694291aSRyusuke Konishi void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
111a694291aSRyusuke Konishi 			   struct nilfs_segment_buffer *prev);
11264b5a32eSRyusuke Konishi void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *, __u64,
11364b5a32eSRyusuke Konishi 				  struct the_nilfs *);
114fb04b91bSArnd Bergmann int nilfs_segbuf_reset(struct nilfs_segment_buffer *, unsigned int, time64_t,
1150c6c44cbSRyusuke Konishi 		       __u64);
11664b5a32eSRyusuke Konishi int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *);
11764b5a32eSRyusuke Konishi int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *,
11864b5a32eSRyusuke Konishi 				struct buffer_head **);
11964b5a32eSRyusuke Konishi void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *);
12064b5a32eSRyusuke Konishi 
nilfs_segbuf_simplex(struct nilfs_segment_buffer * segbuf)1214762077cSRyusuke Konishi static inline int nilfs_segbuf_simplex(struct nilfs_segment_buffer *segbuf)
1224762077cSRyusuke Konishi {
1234762077cSRyusuke Konishi 	unsigned int flags = segbuf->sb_sum.flags;
1244762077cSRyusuke Konishi 
1254762077cSRyusuke Konishi 	return (flags & (NILFS_SS_LOGBGN | NILFS_SS_LOGEND)) ==
1264762077cSRyusuke Konishi 		(NILFS_SS_LOGBGN | NILFS_SS_LOGEND);
1274762077cSRyusuke Konishi }
1284762077cSRyusuke Konishi 
nilfs_segbuf_empty(struct nilfs_segment_buffer * segbuf)1294762077cSRyusuke Konishi static inline int nilfs_segbuf_empty(struct nilfs_segment_buffer *segbuf)
1304762077cSRyusuke Konishi {
1314762077cSRyusuke Konishi 	return segbuf->sb_sum.nblocks == segbuf->sb_sum.nsumblk;
1324762077cSRyusuke Konishi }
1334762077cSRyusuke Konishi 
13464b5a32eSRyusuke Konishi static inline void
nilfs_segbuf_add_segsum_buffer(struct nilfs_segment_buffer * segbuf,struct buffer_head * bh)13564b5a32eSRyusuke Konishi nilfs_segbuf_add_segsum_buffer(struct nilfs_segment_buffer *segbuf,
13664b5a32eSRyusuke Konishi 			       struct buffer_head *bh)
13764b5a32eSRyusuke Konishi {
13864b5a32eSRyusuke Konishi 	list_add_tail(&bh->b_assoc_buffers, &segbuf->sb_segsum_buffers);
13964b5a32eSRyusuke Konishi 	segbuf->sb_sum.nblocks++;
14064b5a32eSRyusuke Konishi 	segbuf->sb_sum.nsumblk++;
14164b5a32eSRyusuke Konishi }
14264b5a32eSRyusuke Konishi 
14364b5a32eSRyusuke Konishi static inline void
nilfs_segbuf_add_payload_buffer(struct nilfs_segment_buffer * segbuf,struct buffer_head * bh)14464b5a32eSRyusuke Konishi nilfs_segbuf_add_payload_buffer(struct nilfs_segment_buffer *segbuf,
14564b5a32eSRyusuke Konishi 				struct buffer_head *bh)
14664b5a32eSRyusuke Konishi {
14764b5a32eSRyusuke Konishi 	list_add_tail(&bh->b_assoc_buffers, &segbuf->sb_payload_buffers);
14864b5a32eSRyusuke Konishi 	segbuf->sb_sum.nblocks++;
14964b5a32eSRyusuke Konishi }
15064b5a32eSRyusuke Konishi 
15164b5a32eSRyusuke Konishi static inline void
nilfs_segbuf_add_file_buffer(struct nilfs_segment_buffer * segbuf,struct buffer_head * bh)15264b5a32eSRyusuke Konishi nilfs_segbuf_add_file_buffer(struct nilfs_segment_buffer *segbuf,
15364b5a32eSRyusuke Konishi 			     struct buffer_head *bh)
15464b5a32eSRyusuke Konishi {
15564b5a32eSRyusuke Konishi 	get_bh(bh);
15664b5a32eSRyusuke Konishi 	nilfs_segbuf_add_payload_buffer(segbuf, bh);
15764b5a32eSRyusuke Konishi 	segbuf->sb_sum.nfileblk++;
15864b5a32eSRyusuke Konishi }
15964b5a32eSRyusuke Konishi 
160e29df395SRyusuke Konishi void nilfs_clear_logs(struct list_head *logs);
161e29df395SRyusuke Konishi void nilfs_truncate_logs(struct list_head *logs,
162e29df395SRyusuke Konishi 			 struct nilfs_segment_buffer *last);
163d1c6b72aSRyusuke Konishi int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs);
164e29df395SRyusuke Konishi int nilfs_wait_on_logs(struct list_head *logs);
165aaed1d5bSRyusuke Konishi void nilfs_add_checksums_on_logs(struct list_head *logs, u32 seed);
166e29df395SRyusuke Konishi 
nilfs_destroy_logs(struct list_head * logs)167e29df395SRyusuke Konishi static inline void nilfs_destroy_logs(struct list_head *logs)
168e29df395SRyusuke Konishi {
169e29df395SRyusuke Konishi 	nilfs_truncate_logs(logs, NULL);
170e29df395SRyusuke Konishi }
171e29df395SRyusuke Konishi 
17264b5a32eSRyusuke Konishi #endif /* _NILFS_SEGBUF_H */
173