1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
35struct nilfs_sc_info;
36struct nilfs_sysfs_dev_subgroups;
37
38
39enum {
40 THE_NILFS_INIT = 0,
41 THE_NILFS_DISCONTINUED,
42 THE_NILFS_GC_RUNNING,
43 THE_NILFS_SB_DIRTY,
44};
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105struct the_nilfs {
106 unsigned long ns_flags;
107 int ns_flushed_device;
108
109 struct block_device *ns_bdev;
110 struct rw_semaphore ns_sem;
111 struct mutex ns_snapshot_mount_mutex;
112
113
114
115
116
117
118 struct buffer_head *ns_sbh[2];
119 struct nilfs_super_block *ns_sbp[2];
120 time_t ns_sbwtime;
121 unsigned ns_sbwcount;
122 unsigned ns_sbsize;
123 unsigned ns_mount_state;
124 unsigned ns_sb_update_freq;
125
126
127
128
129
130
131
132
133 u64 ns_seg_seq;
134 __u64 ns_segnum;
135 __u64 ns_nextnum;
136 unsigned long ns_pseg_offset;
137 __u64 ns_cno;
138 time_t ns_ctime;
139 time_t ns_nongc_ctime;
140 atomic_t ns_ndirtyblks;
141
142
143
144
145
146
147 spinlock_t ns_last_segment_lock;
148 sector_t ns_last_pseg;
149 u64 ns_last_seq;
150 __u64 ns_last_cno;
151 u64 ns_prot_seq;
152 u64 ns_prev_seq;
153
154 struct nilfs_sc_info *ns_writer;
155 struct rw_semaphore ns_segctor_sem;
156
157
158
159
160
161 struct inode *ns_dat;
162 struct inode *ns_cpfile;
163 struct inode *ns_sufile;
164
165
166 struct rb_root ns_cptree;
167 spinlock_t ns_cptree_lock;
168
169
170 struct list_head ns_dirty_files;
171 spinlock_t ns_inode_lock;
172
173
174 struct list_head ns_gc_inodes;
175
176
177 u32 ns_next_generation;
178 spinlock_t ns_next_gen_lock;
179
180
181 unsigned long ns_mount_opt;
182
183 uid_t ns_resuid;
184 gid_t ns_resgid;
185 unsigned long ns_interval;
186 unsigned long ns_watermark;
187
188
189 unsigned int ns_blocksize_bits;
190 unsigned int ns_blocksize;
191 unsigned long ns_nsegments;
192 unsigned long ns_blocks_per_segment;
193 unsigned long ns_r_segments_percentage;
194 unsigned long ns_nrsvsegs;
195 unsigned long ns_first_data_block;
196 int ns_inode_size;
197 int ns_first_ino;
198 u32 ns_crc_seed;
199
200
201 struct kobject ns_dev_kobj;
202 struct completion ns_dev_kobj_unregister;
203 struct nilfs_sysfs_dev_subgroups *ns_dev_subgroups;
204};
205
206#define THE_NILFS_FNS(bit, name) \
207static inline void set_nilfs_##name(struct the_nilfs *nilfs) \
208{ \
209 set_bit(THE_NILFS_##bit, &(nilfs)->ns_flags); \
210} \
211static inline void clear_nilfs_##name(struct the_nilfs *nilfs) \
212{ \
213 clear_bit(THE_NILFS_##bit, &(nilfs)->ns_flags); \
214} \
215static inline int nilfs_##name(struct the_nilfs *nilfs) \
216{ \
217 return test_bit(THE_NILFS_##bit, &(nilfs)->ns_flags); \
218}
219
220THE_NILFS_FNS(INIT, init)
221THE_NILFS_FNS(DISCONTINUED, discontinued)
222THE_NILFS_FNS(GC_RUNNING, gc_running)
223THE_NILFS_FNS(SB_DIRTY, sb_dirty)
224
225
226
227
228#define nilfs_clear_opt(nilfs, opt) \
229 do { (nilfs)->ns_mount_opt &= ~NILFS_MOUNT_##opt; } while (0)
230#define nilfs_set_opt(nilfs, opt) \
231 do { (nilfs)->ns_mount_opt |= NILFS_MOUNT_##opt; } while (0)
232#define nilfs_test_opt(nilfs, opt) ((nilfs)->ns_mount_opt & NILFS_MOUNT_##opt)
233#define nilfs_write_opt(nilfs, mask, opt) \
234 do { (nilfs)->ns_mount_opt = \
235 (((nilfs)->ns_mount_opt & ~NILFS_MOUNT_##mask) | \
236 NILFS_MOUNT_##opt); \
237 } while (0)
238
239
240
241
242
243
244
245
246
247
248
249
250
251struct nilfs_root {
252 __u64 cno;
253 struct rb_node rb_node;
254
255 atomic_t count;
256 struct the_nilfs *nilfs;
257 struct inode *ifile;
258
259 atomic64_t inodes_count;
260 atomic64_t blocks_count;
261
262
263 struct kobject snapshot_kobj;
264 struct completion snapshot_kobj_unregister;
265};
266
267
268#define NILFS_CPTREE_CURRENT_CNO 0
269
270
271#define NILFS_SB_FREQ 10
272
273static inline int nilfs_sb_need_update(struct the_nilfs *nilfs)
274{
275 u64 t = get_seconds();
276 return t < nilfs->ns_sbwtime ||
277 t > nilfs->ns_sbwtime + nilfs->ns_sb_update_freq;
278}
279
280static inline int nilfs_sb_will_flip(struct the_nilfs *nilfs)
281{
282 int flip_bits = nilfs->ns_sbwcount & 0x0FL;
283 return (flip_bits != 0x08 && flip_bits != 0x0F);
284}
285
286void nilfs_set_last_segment(struct the_nilfs *, sector_t, u64, __u64);
287struct the_nilfs *alloc_nilfs(struct block_device *bdev);
288void destroy_nilfs(struct the_nilfs *nilfs);
289int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data);
290int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb);
291unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs);
292void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs);
293int nilfs_discard_segments(struct the_nilfs *, __u64 *, size_t);
294int nilfs_count_free_blocks(struct the_nilfs *, sector_t *);
295struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno);
296struct nilfs_root *nilfs_find_or_create_root(struct the_nilfs *nilfs,
297 __u64 cno);
298void nilfs_put_root(struct nilfs_root *root);
299int nilfs_near_disk_full(struct the_nilfs *);
300void nilfs_fall_back_super_block(struct the_nilfs *);
301void nilfs_swap_super_block(struct the_nilfs *);
302
303
304static inline void nilfs_get_root(struct nilfs_root *root)
305{
306 atomic_inc(&root->count);
307}
308
309static inline int nilfs_valid_fs(struct the_nilfs *nilfs)
310{
311 unsigned valid_fs;
312
313 down_read(&nilfs->ns_sem);
314 valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
315 up_read(&nilfs->ns_sem);
316 return valid_fs;
317}
318
319static inline void
320nilfs_get_segment_range(struct the_nilfs *nilfs, __u64 segnum,
321 sector_t *seg_start, sector_t *seg_end)
322{
323 *seg_start = (sector_t)nilfs->ns_blocks_per_segment * segnum;
324 *seg_end = *seg_start + nilfs->ns_blocks_per_segment - 1;
325 if (segnum == 0)
326 *seg_start = nilfs->ns_first_data_block;
327}
328
329static inline sector_t
330nilfs_get_segment_start_blocknr(struct the_nilfs *nilfs, __u64 segnum)
331{
332 return (segnum == 0) ? nilfs->ns_first_data_block :
333 (sector_t)nilfs->ns_blocks_per_segment * segnum;
334}
335
336static inline __u64
337nilfs_get_segnum_of_block(struct the_nilfs *nilfs, sector_t blocknr)
338{
339 sector_t segnum = blocknr;
340
341 sector_div(segnum, nilfs->ns_blocks_per_segment);
342 return segnum;
343}
344
345static inline void
346nilfs_terminate_segment(struct the_nilfs *nilfs, sector_t seg_start,
347 sector_t seg_end)
348{
349
350 nilfs->ns_pseg_offset = seg_end - seg_start + 1;
351}
352
353static inline void nilfs_shift_to_next_segment(struct the_nilfs *nilfs)
354{
355
356 nilfs->ns_segnum = nilfs->ns_nextnum;
357 nilfs->ns_pseg_offset = 0;
358 nilfs->ns_seg_seq++;
359}
360
361static inline __u64 nilfs_last_cno(struct the_nilfs *nilfs)
362{
363 __u64 cno;
364
365 spin_lock(&nilfs->ns_last_segment_lock);
366 cno = nilfs->ns_last_cno;
367 spin_unlock(&nilfs->ns_last_segment_lock);
368 return cno;
369}
370
371static inline int nilfs_segment_is_active(struct the_nilfs *nilfs, __u64 n)
372{
373 return n == nilfs->ns_segnum || n == nilfs->ns_nextnum;
374}
375
376static inline int nilfs_flush_device(struct the_nilfs *nilfs)
377{
378 int err;
379
380 if (!nilfs_test_opt(nilfs, BARRIER) || nilfs->ns_flushed_device)
381 return 0;
382
383 nilfs->ns_flushed_device = 1;
384
385
386
387
388 smp_wmb();
389
390 err = blkdev_issue_flush(nilfs->ns_bdev, GFP_KERNEL, NULL);
391 if (err != -EIO)
392 err = 0;
393 return err;
394}
395
396#endif
397