1 /*
2  * segment.h - NILFS Segment constructor prototypes and definitions
3  *
4  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Written by Ryusuke Konishi <ryusuke@osrg.net>
21  *
22  */
23 #ifndef _NILFS_SEGMENT_H
24 #define _NILFS_SEGMENT_H
25 
26 #include <linux/types.h>
27 #include <linux/fs.h>
28 #include <linux/buffer_head.h>
29 #include <linux/nilfs2_fs.h>
30 #include "nilfs.h"
31 
32 struct nilfs_root;
33 
34 /**
35  * struct nilfs_recovery_info - Recovery information
36  * @ri_need_recovery: Recovery status
37  * @ri_super_root: Block number of the last super root
38  * @ri_ri_cno: Number of the last checkpoint
39  * @ri_lsegs_start: Region for roll-forwarding (start block number)
40  * @ri_lsegs_end: Region for roll-forwarding (end block number)
41  * @ri_lseg_start_seq: Sequence value of the segment at ri_lsegs_start
42  * @ri_used_segments: List of segments to be mark active
43  * @ri_pseg_start: Block number of the last partial segment
44  * @ri_seq: Sequence number on the last partial segment
45  * @ri_segnum: Segment number on the last partial segment
46  * @ri_nextnum: Next segment number on the last partial segment
47  */
48 struct nilfs_recovery_info {
49 	int			ri_need_recovery;
50 	sector_t		ri_super_root;
51 	__u64			ri_cno;
52 
53 	sector_t		ri_lsegs_start;
54 	sector_t		ri_lsegs_end;
55 	u64			ri_lsegs_start_seq;
56 	struct list_head	ri_used_segments;
57 	sector_t		ri_pseg_start;
58 	u64			ri_seq;
59 	__u64			ri_segnum;
60 	__u64			ri_nextnum;
61 };
62 
63 /* ri_need_recovery */
64 #define NILFS_RECOVERY_SR_UPDATED	 1  /* The super root was updated */
65 #define NILFS_RECOVERY_ROLLFORWARD_DONE	 2  /* Rollforward was carried out */
66 
67 /**
68  * struct nilfs_cstage - Context of collection stage
69  * @scnt: Stage count
70  * @flags: State flags
71  * @dirty_file_ptr: Pointer on dirty_files list, or inode of a target file
72  * @gc_inode_ptr: Pointer on the list of gc-inodes
73  */
74 struct nilfs_cstage {
75 	int			scnt;
76 	unsigned		flags;
77 	struct nilfs_inode_info *dirty_file_ptr;
78 	struct nilfs_inode_info *gc_inode_ptr;
79 };
80 
81 struct nilfs_segment_buffer;
82 
83 struct nilfs_segsum_pointer {
84 	struct buffer_head     *bh;
85 	unsigned		offset; /* offset in bytes */
86 };
87 
88 /**
89  * struct nilfs_sc_info - Segment constructor information
90  * @sc_super: Back pointer to super_block struct
91  * @sc_root: root object of the current filesystem tree
92  * @sc_nblk_inc: Block count of current generation
93  * @sc_dirty_files: List of files to be written
94  * @sc_gc_inodes: List of GC inodes having blocks to be written
95  * @sc_freesegs: array of segment numbers to be freed
96  * @sc_nfreesegs: number of segments on @sc_freesegs
97  * @sc_dsync_inode: inode whose data pages are written for a sync operation
98  * @sc_dsync_start: start byte offset of data pages
99  * @sc_dsync_end: end byte offset of data pages (inclusive)
100  * @sc_segbufs: List of segment buffers
101  * @sc_write_logs: List of segment buffers to hold logs under writing
102  * @sc_segbuf_nblocks: Number of available blocks in segment buffers.
103  * @sc_curseg: Current segment buffer
104  * @sc_stage: Collection stage
105  * @sc_finfo_ptr: pointer to the current finfo struct in the segment summary
106  * @sc_binfo_ptr: pointer to the current binfo struct in the segment summary
107  * @sc_blk_cnt:	Block count of a file
108  * @sc_datablk_cnt: Data block count of a file
109  * @sc_nblk_this_inc: Number of blocks included in the current logical segment
110  * @sc_seg_ctime: Creation time
111  * @sc_cno: checkpoint number of current log
112  * @sc_flags: Internal flags
113  * @sc_state_lock: spinlock for sc_state and so on
114  * @sc_state: Segctord state flags
115  * @sc_flush_request: inode bitmap of metadata files to be flushed
116  * @sc_wait_request: Client request queue
117  * @sc_wait_daemon: Daemon wait queue
118  * @sc_wait_task: Start/end wait queue to control segctord task
119  * @sc_seq_request: Request counter
120  * @sc_seq_accept: Accepted request count
121  * @sc_seq_done: Completion counter
122  * @sc_sync: Request of explicit sync operation
123  * @sc_interval: Timeout value of background construction
124  * @sc_mjcp_freq: Frequency of creating checkpoints
125  * @sc_lseg_stime: Start time of the latest logical segment
126  * @sc_watermark: Watermark for the number of dirty buffers
127  * @sc_timer: Timer for segctord
128  * @sc_task: current thread of segctord
129  */
130 struct nilfs_sc_info {
131 	struct super_block     *sc_super;
132 	struct nilfs_root      *sc_root;
133 
134 	unsigned long		sc_nblk_inc;
135 
136 	struct list_head	sc_dirty_files;
137 	struct list_head	sc_gc_inodes;
138 
139 	__u64		       *sc_freesegs;
140 	size_t			sc_nfreesegs;
141 
142 	struct nilfs_inode_info *sc_dsync_inode;
143 	loff_t			sc_dsync_start;
144 	loff_t			sc_dsync_end;
145 
146 	/* Segment buffers */
147 	struct list_head	sc_segbufs;
148 	struct list_head	sc_write_logs;
149 	unsigned long		sc_segbuf_nblocks;
150 	struct nilfs_segment_buffer *sc_curseg;
151 
152 	struct nilfs_cstage	sc_stage;
153 
154 	struct nilfs_segsum_pointer sc_finfo_ptr;
155 	struct nilfs_segsum_pointer sc_binfo_ptr;
156 	unsigned long		sc_blk_cnt;
157 	unsigned long		sc_datablk_cnt;
158 	unsigned long		sc_nblk_this_inc;
159 	time_t			sc_seg_ctime;
160 	__u64			sc_cno;
161 	unsigned long		sc_flags;
162 
163 	spinlock_t		sc_state_lock;
164 	unsigned long		sc_state;
165 	unsigned long		sc_flush_request;
166 
167 	wait_queue_head_t	sc_wait_request;
168 	wait_queue_head_t	sc_wait_daemon;
169 	wait_queue_head_t	sc_wait_task;
170 
171 	__u32			sc_seq_request;
172 	__u32			sc_seq_accepted;
173 	__u32			sc_seq_done;
174 
175 	int			sc_sync;
176 	unsigned long		sc_interval;
177 	unsigned long		sc_mjcp_freq;
178 	unsigned long		sc_lseg_stime;	/* in 1/HZ seconds */
179 	unsigned long		sc_watermark;
180 
181 	struct timer_list	sc_timer;
182 	struct task_struct     *sc_task;
183 };
184 
185 /* sc_flags */
186 enum {
187 	NILFS_SC_DIRTY,		/* One or more dirty meta-data blocks exist */
188 	NILFS_SC_UNCLOSED,	/* Logical segment is not closed */
189 	NILFS_SC_SUPER_ROOT,	/* The latest segment has a super root */
190 	NILFS_SC_PRIOR_FLUSH,	/* Requesting immediate flush without making a
191 				   checkpoint */
192 	NILFS_SC_HAVE_DELTA,	/* Next checkpoint will have update of files
193 				   other than DAT, cpfile, sufile, or files
194 				   moved by GC */
195 };
196 
197 /* sc_state */
198 #define NILFS_SEGCTOR_QUIT	    0x0001  /* segctord is being destroyed */
199 #define NILFS_SEGCTOR_COMMIT	    0x0004  /* committed transaction exists */
200 
201 /*
202  * Constant parameters
203  */
204 #define NILFS_SC_CLEANUP_RETRY	    3  /* Retry count of construction when
205 					  destroying segctord */
206 
207 /*
208  * Default values of timeout, in seconds.
209  */
210 #define NILFS_SC_DEFAULT_TIMEOUT    5   /* Timeout value of dirty blocks.
211 					   It triggers construction of a
212 					   logical segment with a super root */
213 #define NILFS_SC_DEFAULT_SR_FREQ    30  /* Maximum frequency of super root
214 					   creation */
215 
216 /*
217  * The default threshold amount of data, in block counts.
218  */
219 #define NILFS_SC_DEFAULT_WATERMARK  3600
220 
221 /* super.c */
222 extern struct kmem_cache *nilfs_transaction_cachep;
223 
224 /* segment.c */
225 extern void nilfs_relax_pressure_in_lock(struct super_block *);
226 
227 extern int nilfs_construct_segment(struct super_block *);
228 extern int nilfs_construct_dsync_segment(struct super_block *, struct inode *,
229 					 loff_t, loff_t);
230 extern void nilfs_flush_segment(struct super_block *, ino_t);
231 extern int nilfs_clean_segments(struct super_block *, struct nilfs_argv *,
232 				void **);
233 
234 int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root);
235 void nilfs_detach_log_writer(struct super_block *sb);
236 
237 /* recovery.c */
238 extern int nilfs_read_super_root_block(struct the_nilfs *, sector_t,
239 				       struct buffer_head **, int);
240 extern int nilfs_search_super_root(struct the_nilfs *,
241 				   struct nilfs_recovery_info *);
242 int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs, struct super_block *sb,
243 			      struct nilfs_recovery_info *ri);
244 extern void nilfs_dispose_segment_list(struct list_head *);
245 
246 #endif /* _NILFS_SEGMENT_H */
247