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