1 /*
2  * the_nilfs.h - the_nilfs shared structure.
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 
24 #ifndef _THE_NILFS_H
25 #define _THE_NILFS_H
26 
27 #include <linux/types.h>
28 #include <linux/buffer_head.h>
29 #include <linux/rbtree.h>
30 #include <linux/fs.h>
31 #include <linux/blkdev.h>
32 #include <linux/backing-dev.h>
33 #include <linux/slab.h>
34 
35 struct nilfs_sc_info;
36 
37 /* the_nilfs struct */
38 enum {
39 	THE_NILFS_INIT = 0,     /* Information from super_block is set */
40 	THE_NILFS_DISCONTINUED,	/* 'next' pointer chain has broken */
41 	THE_NILFS_GC_RUNNING,	/* gc process is running */
42 	THE_NILFS_SB_DIRTY,	/* super block is dirty */
43 };
44 
45 /**
46  * struct the_nilfs - struct to supervise multiple nilfs mount points
47  * @ns_flags: flags
48  * @ns_bdev: block device
49  * @ns_sem: semaphore for shared states
50  * @ns_snapshot_mount_mutex: mutex to protect snapshot mounts
51  * @ns_sbh: buffer heads of on-disk super blocks
52  * @ns_sbp: pointers to super block data
53  * @ns_sbwtime: previous write time of super block
54  * @ns_sbwcount: write count of super block
55  * @ns_sbsize: size of valid data in super block
56  * @ns_seg_seq: segment sequence counter
57  * @ns_segnum: index number of the latest full segment.
58  * @ns_nextnum: index number of the full segment index to be used next
59  * @ns_pseg_offset: offset of next partial segment in the current full segment
60  * @ns_cno: next checkpoint number
61  * @ns_ctime: write time of the last segment
62  * @ns_nongc_ctime: write time of the last segment not for cleaner operation
63  * @ns_ndirtyblks: Number of dirty data blocks
64  * @ns_last_segment_lock: lock protecting fields for the latest segment
65  * @ns_last_pseg: start block number of the latest segment
66  * @ns_last_seq: sequence value of the latest segment
67  * @ns_last_cno: checkpoint number of the latest segment
68  * @ns_prot_seq: least sequence number of segments which must not be reclaimed
69  * @ns_prev_seq: base sequence number used to decide if advance log cursor
70  * @ns_writer: log writer
71  * @ns_segctor_sem: semaphore protecting log write
72  * @ns_dat: DAT file inode
73  * @ns_cpfile: checkpoint file inode
74  * @ns_sufile: segusage file inode
75  * @ns_cptree: rb-tree of all mounted checkpoints (nilfs_root)
76  * @ns_cptree_lock: lock protecting @ns_cptree
77  * @ns_dirty_files: list of dirty files
78  * @ns_inode_lock: lock protecting @ns_dirty_files
79  * @ns_gc_inodes: dummy inodes to keep live blocks
80  * @ns_next_generation: next generation number for inodes
81  * @ns_next_gen_lock: lock protecting @ns_next_generation
82  * @ns_mount_opt: mount options
83  * @ns_resuid: uid for reserved blocks
84  * @ns_resgid: gid for reserved blocks
85  * @ns_interval: checkpoint creation interval
86  * @ns_watermark: watermark for the number of dirty buffers
87  * @ns_blocksize_bits: bit length of block size
88  * @ns_blocksize: block size
89  * @ns_nsegments: number of segments in filesystem
90  * @ns_blocks_per_segment: number of blocks per segment
91  * @ns_r_segments_percentage: reserved segments percentage
92  * @ns_nrsvsegs: number of reserved segments
93  * @ns_first_data_block: block number of first data block
94  * @ns_inode_size: size of on-disk inode
95  * @ns_first_ino: first not-special inode number
96  * @ns_crc_seed: seed value of CRC32 calculation
97  */
98 struct the_nilfs {
99 	unsigned long		ns_flags;
100 
101 	struct block_device    *ns_bdev;
102 	struct rw_semaphore	ns_sem;
103 	struct mutex		ns_snapshot_mount_mutex;
104 
105 	/*
106 	 * used for
107 	 * - loading the latest checkpoint exclusively.
108 	 * - allocating a new full segment.
109 	 * - protecting s_dirt in the super_block struct
110 	 *   (see nilfs_write_super) and the following fields.
111 	 */
112 	struct buffer_head     *ns_sbh[2];
113 	struct nilfs_super_block *ns_sbp[2];
114 	time_t			ns_sbwtime;
115 	unsigned		ns_sbwcount;
116 	unsigned		ns_sbsize;
117 	unsigned		ns_mount_state;
118 
119 	/*
120 	 * Following fields are dedicated to a writable FS-instance.
121 	 * Except for the period seeking checkpoint, code outside the segment
122 	 * constructor must lock a segment semaphore while accessing these
123 	 * fields.
124 	 * The writable FS-instance is sole during a lifetime of the_nilfs.
125 	 */
126 	u64			ns_seg_seq;
127 	__u64			ns_segnum;
128 	__u64			ns_nextnum;
129 	unsigned long		ns_pseg_offset;
130 	__u64			ns_cno;
131 	time_t			ns_ctime;
132 	time_t			ns_nongc_ctime;
133 	atomic_t		ns_ndirtyblks;
134 
135 	/*
136 	 * The following fields hold information on the latest partial segment
137 	 * written to disk with a super root.  These fields are protected by
138 	 * ns_last_segment_lock.
139 	 */
140 	spinlock_t		ns_last_segment_lock;
141 	sector_t		ns_last_pseg;
142 	u64			ns_last_seq;
143 	__u64			ns_last_cno;
144 	u64			ns_prot_seq;
145 	u64			ns_prev_seq;
146 
147 	struct nilfs_sc_info   *ns_writer;
148 	struct rw_semaphore	ns_segctor_sem;
149 
150 	/*
151 	 * Following fields are lock free except for the period before
152 	 * the_nilfs is initialized.
153 	 */
154 	struct inode	       *ns_dat;
155 	struct inode	       *ns_cpfile;
156 	struct inode	       *ns_sufile;
157 
158 	/* Checkpoint tree */
159 	struct rb_root		ns_cptree;
160 	spinlock_t		ns_cptree_lock;
161 
162 	/* Dirty inode list */
163 	struct list_head	ns_dirty_files;
164 	spinlock_t		ns_inode_lock;
165 
166 	/* GC inode list */
167 	struct list_head	ns_gc_inodes;
168 
169 	/* Inode allocator */
170 	u32			ns_next_generation;
171 	spinlock_t		ns_next_gen_lock;
172 
173 	/* Mount options */
174 	unsigned long		ns_mount_opt;
175 
176 	uid_t			ns_resuid;
177 	gid_t			ns_resgid;
178 	unsigned long		ns_interval;
179 	unsigned long		ns_watermark;
180 
181 	/* Disk layout information (static) */
182 	unsigned int		ns_blocksize_bits;
183 	unsigned int		ns_blocksize;
184 	unsigned long		ns_nsegments;
185 	unsigned long		ns_blocks_per_segment;
186 	unsigned long		ns_r_segments_percentage;
187 	unsigned long		ns_nrsvsegs;
188 	unsigned long		ns_first_data_block;
189 	int			ns_inode_size;
190 	int			ns_first_ino;
191 	u32			ns_crc_seed;
192 };
193 
194 #define THE_NILFS_FNS(bit, name)					\
195 static inline void set_nilfs_##name(struct the_nilfs *nilfs)		\
196 {									\
197 	set_bit(THE_NILFS_##bit, &(nilfs)->ns_flags);			\
198 }									\
199 static inline void clear_nilfs_##name(struct the_nilfs *nilfs)		\
200 {									\
201 	clear_bit(THE_NILFS_##bit, &(nilfs)->ns_flags);			\
202 }									\
203 static inline int nilfs_##name(struct the_nilfs *nilfs)			\
204 {									\
205 	return test_bit(THE_NILFS_##bit, &(nilfs)->ns_flags);		\
206 }
207 
208 THE_NILFS_FNS(INIT, init)
209 THE_NILFS_FNS(DISCONTINUED, discontinued)
210 THE_NILFS_FNS(GC_RUNNING, gc_running)
211 THE_NILFS_FNS(SB_DIRTY, sb_dirty)
212 
213 /*
214  * Mount option operations
215  */
216 #define nilfs_clear_opt(nilfs, opt)  \
217 	do { (nilfs)->ns_mount_opt &= ~NILFS_MOUNT_##opt; } while (0)
218 #define nilfs_set_opt(nilfs, opt)  \
219 	do { (nilfs)->ns_mount_opt |= NILFS_MOUNT_##opt; } while (0)
220 #define nilfs_test_opt(nilfs, opt) ((nilfs)->ns_mount_opt & NILFS_MOUNT_##opt)
221 #define nilfs_write_opt(nilfs, mask, opt)				\
222 	do { (nilfs)->ns_mount_opt =					\
223 		(((nilfs)->ns_mount_opt & ~NILFS_MOUNT_##mask) |	\
224 		 NILFS_MOUNT_##opt);					\
225 	} while (0)
226 
227 /**
228  * struct nilfs_root - nilfs root object
229  * @cno: checkpoint number
230  * @rb_node: red-black tree node
231  * @count: refcount of this structure
232  * @nilfs: nilfs object
233  * @ifile: inode file
234  * @root: root inode
235  * @inodes_count: number of inodes
236  * @blocks_count: number of blocks (Reserved)
237  */
238 struct nilfs_root {
239 	__u64 cno;
240 	struct rb_node rb_node;
241 
242 	atomic_t count;
243 	struct the_nilfs *nilfs;
244 	struct inode *ifile;
245 
246 	atomic_t inodes_count;
247 	atomic_t blocks_count;
248 };
249 
250 /* Special checkpoint number */
251 #define NILFS_CPTREE_CURRENT_CNO	0
252 
253 /* Minimum interval of periodical update of superblocks (in seconds) */
254 #define NILFS_SB_FREQ		10
255 
nilfs_sb_need_update(struct the_nilfs * nilfs)256 static inline int nilfs_sb_need_update(struct the_nilfs *nilfs)
257 {
258 	u64 t = get_seconds();
259 	return t < nilfs->ns_sbwtime || t > nilfs->ns_sbwtime + NILFS_SB_FREQ;
260 }
261 
nilfs_sb_will_flip(struct the_nilfs * nilfs)262 static inline int nilfs_sb_will_flip(struct the_nilfs *nilfs)
263 {
264 	int flip_bits = nilfs->ns_sbwcount & 0x0FL;
265 	return (flip_bits != 0x08 && flip_bits != 0x0F);
266 }
267 
268 void nilfs_set_last_segment(struct the_nilfs *, sector_t, u64, __u64);
269 struct the_nilfs *alloc_nilfs(struct block_device *bdev);
270 void destroy_nilfs(struct the_nilfs *nilfs);
271 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data);
272 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb);
273 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs);
274 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs);
275 int nilfs_discard_segments(struct the_nilfs *, __u64 *, size_t);
276 int nilfs_count_free_blocks(struct the_nilfs *, sector_t *);
277 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno);
278 struct nilfs_root *nilfs_find_or_create_root(struct the_nilfs *nilfs,
279 					     __u64 cno);
280 void nilfs_put_root(struct nilfs_root *root);
281 int nilfs_near_disk_full(struct the_nilfs *);
282 void nilfs_fall_back_super_block(struct the_nilfs *);
283 void nilfs_swap_super_block(struct the_nilfs *);
284 
285 
nilfs_get_root(struct nilfs_root * root)286 static inline void nilfs_get_root(struct nilfs_root *root)
287 {
288 	atomic_inc(&root->count);
289 }
290 
nilfs_valid_fs(struct the_nilfs * nilfs)291 static inline int nilfs_valid_fs(struct the_nilfs *nilfs)
292 {
293 	unsigned valid_fs;
294 
295 	down_read(&nilfs->ns_sem);
296 	valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
297 	up_read(&nilfs->ns_sem);
298 	return valid_fs;
299 }
300 
301 static inline void
nilfs_get_segment_range(struct the_nilfs * nilfs,__u64 segnum,sector_t * seg_start,sector_t * seg_end)302 nilfs_get_segment_range(struct the_nilfs *nilfs, __u64 segnum,
303 			sector_t *seg_start, sector_t *seg_end)
304 {
305 	*seg_start = (sector_t)nilfs->ns_blocks_per_segment * segnum;
306 	*seg_end = *seg_start + nilfs->ns_blocks_per_segment - 1;
307 	if (segnum == 0)
308 		*seg_start = nilfs->ns_first_data_block;
309 }
310 
311 static inline sector_t
nilfs_get_segment_start_blocknr(struct the_nilfs * nilfs,__u64 segnum)312 nilfs_get_segment_start_blocknr(struct the_nilfs *nilfs, __u64 segnum)
313 {
314 	return (segnum == 0) ? nilfs->ns_first_data_block :
315 		(sector_t)nilfs->ns_blocks_per_segment * segnum;
316 }
317 
318 static inline __u64
nilfs_get_segnum_of_block(struct the_nilfs * nilfs,sector_t blocknr)319 nilfs_get_segnum_of_block(struct the_nilfs *nilfs, sector_t blocknr)
320 {
321 	sector_t segnum = blocknr;
322 
323 	sector_div(segnum, nilfs->ns_blocks_per_segment);
324 	return segnum;
325 }
326 
327 static inline void
nilfs_terminate_segment(struct the_nilfs * nilfs,sector_t seg_start,sector_t seg_end)328 nilfs_terminate_segment(struct the_nilfs *nilfs, sector_t seg_start,
329 			sector_t seg_end)
330 {
331 	/* terminate the current full segment (used in case of I/O-error) */
332 	nilfs->ns_pseg_offset = seg_end - seg_start + 1;
333 }
334 
nilfs_shift_to_next_segment(struct the_nilfs * nilfs)335 static inline void nilfs_shift_to_next_segment(struct the_nilfs *nilfs)
336 {
337 	/* move forward with a full segment */
338 	nilfs->ns_segnum = nilfs->ns_nextnum;
339 	nilfs->ns_pseg_offset = 0;
340 	nilfs->ns_seg_seq++;
341 }
342 
nilfs_last_cno(struct the_nilfs * nilfs)343 static inline __u64 nilfs_last_cno(struct the_nilfs *nilfs)
344 {
345 	__u64 cno;
346 
347 	spin_lock(&nilfs->ns_last_segment_lock);
348 	cno = nilfs->ns_last_cno;
349 	spin_unlock(&nilfs->ns_last_segment_lock);
350 	return cno;
351 }
352 
nilfs_segment_is_active(struct the_nilfs * nilfs,__u64 n)353 static inline int nilfs_segment_is_active(struct the_nilfs *nilfs, __u64 n)
354 {
355 	return n == nilfs->ns_segnum || n == nilfs->ns_nextnum;
356 }
357 
358 #endif /* _THE_NILFS_H */
359