Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * the_nilfs shared structure.
0004  *
0005  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
0006  *
0007  * Written by Ryusuke Konishi.
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 /* the_nilfs struct */
0027 enum {
0028     THE_NILFS_INIT = 0,     /* Information from super_block is set */
0029     THE_NILFS_DISCONTINUED, /* 'next' pointer chain has broken */
0030     THE_NILFS_GC_RUNNING,   /* gc process is running */
0031     THE_NILFS_SB_DIRTY, /* super block is dirty */
0032 };
0033 
0034 /**
0035  * struct the_nilfs - struct to supervise multiple nilfs mount points
0036  * @ns_flags: flags
0037  * @ns_flushed_device: flag indicating if all volatile data was flushed
0038  * @ns_sb: back pointer to super block instance
0039  * @ns_bdev: block device
0040  * @ns_sem: semaphore for shared states
0041  * @ns_snapshot_mount_mutex: mutex to protect snapshot mounts
0042  * @ns_sbh: buffer heads of on-disk super blocks
0043  * @ns_sbp: pointers to super block data
0044  * @ns_sbwtime: previous write time of super block
0045  * @ns_sbwcount: write count of super block
0046  * @ns_sbsize: size of valid data in super block
0047  * @ns_mount_state: file system state
0048  * @ns_sb_update_freq: interval of periodical update of superblocks (in seconds)
0049  * @ns_seg_seq: segment sequence counter
0050  * @ns_segnum: index number of the latest full segment.
0051  * @ns_nextnum: index number of the full segment index to be used next
0052  * @ns_pseg_offset: offset of next partial segment in the current full segment
0053  * @ns_cno: next checkpoint number
0054  * @ns_ctime: write time of the last segment
0055  * @ns_nongc_ctime: write time of the last segment not for cleaner operation
0056  * @ns_ndirtyblks: Number of dirty data blocks
0057  * @ns_last_segment_lock: lock protecting fields for the latest segment
0058  * @ns_last_pseg: start block number of the latest segment
0059  * @ns_last_seq: sequence value of the latest segment
0060  * @ns_last_cno: checkpoint number of the latest segment
0061  * @ns_prot_seq: least sequence number of segments which must not be reclaimed
0062  * @ns_prev_seq: base sequence number used to decide if advance log cursor
0063  * @ns_writer: log writer
0064  * @ns_segctor_sem: semaphore protecting log write
0065  * @ns_dat: DAT file inode
0066  * @ns_cpfile: checkpoint file inode
0067  * @ns_sufile: segusage file inode
0068  * @ns_cptree: rb-tree of all mounted checkpoints (nilfs_root)
0069  * @ns_cptree_lock: lock protecting @ns_cptree
0070  * @ns_dirty_files: list of dirty files
0071  * @ns_inode_lock: lock protecting @ns_dirty_files
0072  * @ns_gc_inodes: dummy inodes to keep live blocks
0073  * @ns_next_generation: next generation number for inodes
0074  * @ns_next_gen_lock: lock protecting @ns_next_generation
0075  * @ns_mount_opt: mount options
0076  * @ns_resuid: uid for reserved blocks
0077  * @ns_resgid: gid for reserved blocks
0078  * @ns_interval: checkpoint creation interval
0079  * @ns_watermark: watermark for the number of dirty buffers
0080  * @ns_blocksize_bits: bit length of block size
0081  * @ns_blocksize: block size
0082  * @ns_nsegments: number of segments in filesystem
0083  * @ns_blocks_per_segment: number of blocks per segment
0084  * @ns_r_segments_percentage: reserved segments percentage
0085  * @ns_nrsvsegs: number of reserved segments
0086  * @ns_first_data_block: block number of first data block
0087  * @ns_inode_size: size of on-disk inode
0088  * @ns_first_ino: first not-special inode number
0089  * @ns_crc_seed: seed value of CRC32 calculation
0090  * @ns_dev_kobj: /sys/fs/<nilfs>/<device>
0091  * @ns_dev_kobj_unregister: completion state
0092  * @ns_dev_subgroups: <device> subgroups pointer
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      * used for
0105      * - loading the latest checkpoint exclusively.
0106      * - allocating a new full segment.
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      * The following fields are updated by a writable FS-instance.
0118      * These fields are protected by ns_segctor_sem outside load_nilfs().
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      * The following fields hold information on the latest partial segment
0131      * written to disk with a super root.  These fields are protected by
0132      * ns_last_segment_lock.
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      * Following fields are lock free except for the period before
0146      * the_nilfs is initialized.
0147      */
0148     struct inode           *ns_dat;
0149     struct inode           *ns_cpfile;
0150     struct inode           *ns_sufile;
0151 
0152     /* Checkpoint tree */
0153     struct rb_root      ns_cptree;
0154     spinlock_t      ns_cptree_lock;
0155 
0156     /* Dirty inode list */
0157     struct list_head    ns_dirty_files;
0158     spinlock_t      ns_inode_lock;
0159 
0160     /* GC inode list */
0161     struct list_head    ns_gc_inodes;
0162 
0163     /* Inode allocator */
0164     u32         ns_next_generation;
0165     spinlock_t      ns_next_gen_lock;
0166 
0167     /* Mount options */
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     /* Disk layout information (static) */
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     /* /sys/fs/<nilfs>/<device> */
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  * Mount option operations
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  * struct nilfs_root - nilfs root object
0227  * @cno: checkpoint number
0228  * @rb_node: red-black tree node
0229  * @count: refcount of this structure
0230  * @nilfs: nilfs object
0231  * @ifile: inode file
0232  * @inodes_count: number of inodes
0233  * @blocks_count: number of blocks
0234  * @snapshot_kobj: /sys/fs/<nilfs>/<device>/mounted_snapshots/<snapshot>
0235  * @snapshot_kobj_unregister: completion state for kernel object
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     /* /sys/fs/<nilfs>/<device>/mounted_snapshots/<snapshot> */
0249     struct kobject snapshot_kobj;
0250     struct completion snapshot_kobj_unregister;
0251 };
0252 
0253 /* Special checkpoint number */
0254 #define NILFS_CPTREE_CURRENT_CNO    0
0255 
0256 /* Minimum interval of periodical update of superblocks (in seconds) */
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     /* terminate the current full segment (used in case of I/O-error) */
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     /* move forward with a full segment */
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      * the store to ns_flushed_device must not be reordered after
0374      * blkdev_issue_flush().
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 /* _THE_NILFS_H */