0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef _THE_NILFS_H
0012 #define _THE_NILFS_H
0013
0014 #include <linux/types.h>
0015 #include <linux/buffer_head.h>
0016 #include <linux/rbtree.h>
0017 #include <linux/fs.h>
0018 #include <linux/blkdev.h>
0019 #include <linux/backing-dev.h>
0020 #include <linux/slab.h>
0021 #include <linux/refcount.h>
0022
0023 struct nilfs_sc_info;
0024 struct nilfs_sysfs_dev_subgroups;
0025
0026
0027 enum {
0028 THE_NILFS_INIT = 0,
0029 THE_NILFS_DISCONTINUED,
0030 THE_NILFS_GC_RUNNING,
0031 THE_NILFS_SB_DIRTY,
0032 };
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 struct the_nilfs {
0095 unsigned long ns_flags;
0096 int ns_flushed_device;
0097
0098 struct super_block *ns_sb;
0099 struct block_device *ns_bdev;
0100 struct rw_semaphore ns_sem;
0101 struct mutex ns_snapshot_mount_mutex;
0102
0103
0104
0105
0106
0107
0108 struct buffer_head *ns_sbh[2];
0109 struct nilfs_super_block *ns_sbp[2];
0110 time64_t ns_sbwtime;
0111 unsigned int ns_sbwcount;
0112 unsigned int ns_sbsize;
0113 unsigned int ns_mount_state;
0114 unsigned int ns_sb_update_freq;
0115
0116
0117
0118
0119
0120 u64 ns_seg_seq;
0121 __u64 ns_segnum;
0122 __u64 ns_nextnum;
0123 unsigned long ns_pseg_offset;
0124 __u64 ns_cno;
0125 time64_t ns_ctime;
0126 time64_t ns_nongc_ctime;
0127 atomic_t ns_ndirtyblks;
0128
0129
0130
0131
0132
0133
0134 spinlock_t ns_last_segment_lock;
0135 sector_t ns_last_pseg;
0136 u64 ns_last_seq;
0137 __u64 ns_last_cno;
0138 u64 ns_prot_seq;
0139 u64 ns_prev_seq;
0140
0141 struct nilfs_sc_info *ns_writer;
0142 struct rw_semaphore ns_segctor_sem;
0143
0144
0145
0146
0147
0148 struct inode *ns_dat;
0149 struct inode *ns_cpfile;
0150 struct inode *ns_sufile;
0151
0152
0153 struct rb_root ns_cptree;
0154 spinlock_t ns_cptree_lock;
0155
0156
0157 struct list_head ns_dirty_files;
0158 spinlock_t ns_inode_lock;
0159
0160
0161 struct list_head ns_gc_inodes;
0162
0163
0164 u32 ns_next_generation;
0165 spinlock_t ns_next_gen_lock;
0166
0167
0168 unsigned long ns_mount_opt;
0169
0170 uid_t ns_resuid;
0171 gid_t ns_resgid;
0172 unsigned long ns_interval;
0173 unsigned long ns_watermark;
0174
0175
0176 unsigned int ns_blocksize_bits;
0177 unsigned int ns_blocksize;
0178 unsigned long ns_nsegments;
0179 unsigned long ns_blocks_per_segment;
0180 unsigned long ns_r_segments_percentage;
0181 unsigned long ns_nrsvsegs;
0182 unsigned long ns_first_data_block;
0183 int ns_inode_size;
0184 int ns_first_ino;
0185 u32 ns_crc_seed;
0186
0187
0188 struct kobject ns_dev_kobj;
0189 struct completion ns_dev_kobj_unregister;
0190 struct nilfs_sysfs_dev_subgroups *ns_dev_subgroups;
0191 };
0192
0193 #define THE_NILFS_FNS(bit, name) \
0194 static inline void set_nilfs_##name(struct the_nilfs *nilfs) \
0195 { \
0196 set_bit(THE_NILFS_##bit, &(nilfs)->ns_flags); \
0197 } \
0198 static inline void clear_nilfs_##name(struct the_nilfs *nilfs) \
0199 { \
0200 clear_bit(THE_NILFS_##bit, &(nilfs)->ns_flags); \
0201 } \
0202 static inline int nilfs_##name(struct the_nilfs *nilfs) \
0203 { \
0204 return test_bit(THE_NILFS_##bit, &(nilfs)->ns_flags); \
0205 }
0206
0207 THE_NILFS_FNS(INIT, init)
0208 THE_NILFS_FNS(DISCONTINUED, discontinued)
0209 THE_NILFS_FNS(GC_RUNNING, gc_running)
0210 THE_NILFS_FNS(SB_DIRTY, sb_dirty)
0211
0212
0213
0214
0215 #define nilfs_clear_opt(nilfs, opt) \
0216 ((nilfs)->ns_mount_opt &= ~NILFS_MOUNT_##opt)
0217 #define nilfs_set_opt(nilfs, opt) \
0218 ((nilfs)->ns_mount_opt |= NILFS_MOUNT_##opt)
0219 #define nilfs_test_opt(nilfs, opt) ((nilfs)->ns_mount_opt & NILFS_MOUNT_##opt)
0220 #define nilfs_write_opt(nilfs, mask, opt) \
0221 ((nilfs)->ns_mount_opt = \
0222 (((nilfs)->ns_mount_opt & ~NILFS_MOUNT_##mask) | \
0223 NILFS_MOUNT_##opt)) \
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 struct nilfs_root {
0238 __u64 cno;
0239 struct rb_node rb_node;
0240
0241 refcount_t count;
0242 struct the_nilfs *nilfs;
0243 struct inode *ifile;
0244
0245 atomic64_t inodes_count;
0246 atomic64_t blocks_count;
0247
0248
0249 struct kobject snapshot_kobj;
0250 struct completion snapshot_kobj_unregister;
0251 };
0252
0253
0254 #define NILFS_CPTREE_CURRENT_CNO 0
0255
0256
0257 #define NILFS_SB_FREQ 10
0258
0259 static inline int nilfs_sb_need_update(struct the_nilfs *nilfs)
0260 {
0261 u64 t = ktime_get_real_seconds();
0262
0263 return t < nilfs->ns_sbwtime ||
0264 t > nilfs->ns_sbwtime + nilfs->ns_sb_update_freq;
0265 }
0266
0267 static inline int nilfs_sb_will_flip(struct the_nilfs *nilfs)
0268 {
0269 int flip_bits = nilfs->ns_sbwcount & 0x0FL;
0270
0271 return (flip_bits != 0x08 && flip_bits != 0x0F);
0272 }
0273
0274 void nilfs_set_last_segment(struct the_nilfs *, sector_t, u64, __u64);
0275 struct the_nilfs *alloc_nilfs(struct super_block *sb);
0276 void destroy_nilfs(struct the_nilfs *nilfs);
0277 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data);
0278 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb);
0279 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs);
0280 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs);
0281 int nilfs_discard_segments(struct the_nilfs *, __u64 *, size_t);
0282 int nilfs_count_free_blocks(struct the_nilfs *, sector_t *);
0283 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno);
0284 struct nilfs_root *nilfs_find_or_create_root(struct the_nilfs *nilfs,
0285 __u64 cno);
0286 void nilfs_put_root(struct nilfs_root *root);
0287 int nilfs_near_disk_full(struct the_nilfs *);
0288 void nilfs_fall_back_super_block(struct the_nilfs *);
0289 void nilfs_swap_super_block(struct the_nilfs *);
0290
0291
0292 static inline void nilfs_get_root(struct nilfs_root *root)
0293 {
0294 refcount_inc(&root->count);
0295 }
0296
0297 static inline int nilfs_valid_fs(struct the_nilfs *nilfs)
0298 {
0299 unsigned int valid_fs;
0300
0301 down_read(&nilfs->ns_sem);
0302 valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
0303 up_read(&nilfs->ns_sem);
0304 return valid_fs;
0305 }
0306
0307 static inline void
0308 nilfs_get_segment_range(struct the_nilfs *nilfs, __u64 segnum,
0309 sector_t *seg_start, sector_t *seg_end)
0310 {
0311 *seg_start = (sector_t)nilfs->ns_blocks_per_segment * segnum;
0312 *seg_end = *seg_start + nilfs->ns_blocks_per_segment - 1;
0313 if (segnum == 0)
0314 *seg_start = nilfs->ns_first_data_block;
0315 }
0316
0317 static inline sector_t
0318 nilfs_get_segment_start_blocknr(struct the_nilfs *nilfs, __u64 segnum)
0319 {
0320 return (segnum == 0) ? nilfs->ns_first_data_block :
0321 (sector_t)nilfs->ns_blocks_per_segment * segnum;
0322 }
0323
0324 static inline __u64
0325 nilfs_get_segnum_of_block(struct the_nilfs *nilfs, sector_t blocknr)
0326 {
0327 sector_t segnum = blocknr;
0328
0329 sector_div(segnum, nilfs->ns_blocks_per_segment);
0330 return segnum;
0331 }
0332
0333 static inline void
0334 nilfs_terminate_segment(struct the_nilfs *nilfs, sector_t seg_start,
0335 sector_t seg_end)
0336 {
0337
0338 nilfs->ns_pseg_offset = seg_end - seg_start + 1;
0339 }
0340
0341 static inline void nilfs_shift_to_next_segment(struct the_nilfs *nilfs)
0342 {
0343
0344 nilfs->ns_segnum = nilfs->ns_nextnum;
0345 nilfs->ns_pseg_offset = 0;
0346 nilfs->ns_seg_seq++;
0347 }
0348
0349 static inline __u64 nilfs_last_cno(struct the_nilfs *nilfs)
0350 {
0351 __u64 cno;
0352
0353 spin_lock(&nilfs->ns_last_segment_lock);
0354 cno = nilfs->ns_last_cno;
0355 spin_unlock(&nilfs->ns_last_segment_lock);
0356 return cno;
0357 }
0358
0359 static inline int nilfs_segment_is_active(struct the_nilfs *nilfs, __u64 n)
0360 {
0361 return n == nilfs->ns_segnum || n == nilfs->ns_nextnum;
0362 }
0363
0364 static inline int nilfs_flush_device(struct the_nilfs *nilfs)
0365 {
0366 int err;
0367
0368 if (!nilfs_test_opt(nilfs, BARRIER) || nilfs->ns_flushed_device)
0369 return 0;
0370
0371 nilfs->ns_flushed_device = 1;
0372
0373
0374
0375
0376 smp_wmb();
0377
0378 err = blkdev_issue_flush(nilfs->ns_bdev);
0379 if (err != -EIO)
0380 err = 0;
0381 return err;
0382 }
0383
0384 #endif