Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * fs/f2fs/node.h
0004  *
0005  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
0006  *             http://www.samsung.com/
0007  */
0008 /* start node id of a node block dedicated to the given node id */
0009 #define START_NID(nid) (((nid) / NAT_ENTRY_PER_BLOCK) * NAT_ENTRY_PER_BLOCK)
0010 
0011 /* node block offset on the NAT area dedicated to the given start node id */
0012 #define NAT_BLOCK_OFFSET(start_nid) ((start_nid) / NAT_ENTRY_PER_BLOCK)
0013 
0014 /* # of pages to perform synchronous readahead before building free nids */
0015 #define FREE_NID_PAGES  8
0016 #define MAX_FREE_NIDS   (NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES)
0017 
0018 /* size of free nid batch when shrinking */
0019 #define SHRINK_NID_BATCH_SIZE   8
0020 
0021 #define DEF_RA_NID_PAGES    0   /* # of nid pages to be readaheaded */
0022 
0023 /* maximum readahead size for node during getting data blocks */
0024 #define MAX_RA_NODE     128
0025 
0026 /* control the memory footprint threshold (10MB per 1GB ram) */
0027 #define DEF_RAM_THRESHOLD   1
0028 
0029 /* control dirty nats ratio threshold (default: 10% over max nid count) */
0030 #define DEF_DIRTY_NAT_RATIO_THRESHOLD       10
0031 /* control total # of nats */
0032 #define DEF_NAT_CACHE_THRESHOLD         100000
0033 
0034 /* control total # of node writes used for roll-fowrad recovery */
0035 #define DEF_RF_NODE_BLOCKS          0
0036 
0037 /* vector size for gang look-up from nat cache that consists of radix tree */
0038 #define NATVEC_SIZE 64
0039 #define SETVEC_SIZE 32
0040 
0041 /* return value for read_node_page */
0042 #define LOCKED_PAGE 1
0043 
0044 /* check pinned file's alignment status of physical blocks */
0045 #define FILE_NOT_ALIGNED    1
0046 
0047 /* For flag in struct node_info */
0048 enum {
0049     IS_CHECKPOINTED,    /* is it checkpointed before? */
0050     HAS_FSYNCED_INODE,  /* is the inode fsynced before? */
0051     HAS_LAST_FSYNC,     /* has the latest node fsync mark? */
0052     IS_DIRTY,       /* this nat entry is dirty? */
0053     IS_PREALLOC,        /* nat entry is preallocated */
0054 };
0055 
0056 /*
0057  * For node information
0058  */
0059 struct node_info {
0060     nid_t nid;      /* node id */
0061     nid_t ino;      /* inode number of the node's owner */
0062     block_t blk_addr;   /* block address of the node */
0063     unsigned char version;  /* version of the node */
0064     unsigned char flag; /* for node information bits */
0065 };
0066 
0067 struct nat_entry {
0068     struct list_head list;  /* for clean or dirty nat list */
0069     struct node_info ni;    /* in-memory node information */
0070 };
0071 
0072 #define nat_get_nid(nat)        ((nat)->ni.nid)
0073 #define nat_set_nid(nat, n)     ((nat)->ni.nid = (n))
0074 #define nat_get_blkaddr(nat)        ((nat)->ni.blk_addr)
0075 #define nat_set_blkaddr(nat, b)     ((nat)->ni.blk_addr = (b))
0076 #define nat_get_ino(nat)        ((nat)->ni.ino)
0077 #define nat_set_ino(nat, i)     ((nat)->ni.ino = (i))
0078 #define nat_get_version(nat)        ((nat)->ni.version)
0079 #define nat_set_version(nat, v)     ((nat)->ni.version = (v))
0080 
0081 #define inc_node_version(version)   (++(version))
0082 
0083 static inline void copy_node_info(struct node_info *dst,
0084                         struct node_info *src)
0085 {
0086     dst->nid = src->nid;
0087     dst->ino = src->ino;
0088     dst->blk_addr = src->blk_addr;
0089     dst->version = src->version;
0090     /* should not copy flag here */
0091 }
0092 
0093 static inline void set_nat_flag(struct nat_entry *ne,
0094                 unsigned int type, bool set)
0095 {
0096     unsigned char mask = 0x01 << type;
0097     if (set)
0098         ne->ni.flag |= mask;
0099     else
0100         ne->ni.flag &= ~mask;
0101 }
0102 
0103 static inline bool get_nat_flag(struct nat_entry *ne, unsigned int type)
0104 {
0105     unsigned char mask = 0x01 << type;
0106     return ne->ni.flag & mask;
0107 }
0108 
0109 static inline void nat_reset_flag(struct nat_entry *ne)
0110 {
0111     /* these states can be set only after checkpoint was done */
0112     set_nat_flag(ne, IS_CHECKPOINTED, true);
0113     set_nat_flag(ne, HAS_FSYNCED_INODE, false);
0114     set_nat_flag(ne, HAS_LAST_FSYNC, true);
0115 }
0116 
0117 static inline void node_info_from_raw_nat(struct node_info *ni,
0118                         struct f2fs_nat_entry *raw_ne)
0119 {
0120     ni->ino = le32_to_cpu(raw_ne->ino);
0121     ni->blk_addr = le32_to_cpu(raw_ne->block_addr);
0122     ni->version = raw_ne->version;
0123 }
0124 
0125 static inline void raw_nat_from_node_info(struct f2fs_nat_entry *raw_ne,
0126                         struct node_info *ni)
0127 {
0128     raw_ne->ino = cpu_to_le32(ni->ino);
0129     raw_ne->block_addr = cpu_to_le32(ni->blk_addr);
0130     raw_ne->version = ni->version;
0131 }
0132 
0133 static inline bool excess_dirty_nats(struct f2fs_sb_info *sbi)
0134 {
0135     return NM_I(sbi)->nat_cnt[DIRTY_NAT] >= NM_I(sbi)->max_nid *
0136                     NM_I(sbi)->dirty_nats_ratio / 100;
0137 }
0138 
0139 static inline bool excess_cached_nats(struct f2fs_sb_info *sbi)
0140 {
0141     return NM_I(sbi)->nat_cnt[TOTAL_NAT] >= DEF_NAT_CACHE_THRESHOLD;
0142 }
0143 
0144 enum mem_type {
0145     FREE_NIDS,  /* indicates the free nid list */
0146     NAT_ENTRIES,    /* indicates the cached nat entry */
0147     DIRTY_DENTS,    /* indicates dirty dentry pages */
0148     INO_ENTRIES,    /* indicates inode entries */
0149     EXTENT_CACHE,   /* indicates extent cache */
0150     DISCARD_CACHE,  /* indicates memory of cached discard cmds */
0151     COMPRESS_PAGE,  /* indicates memory of cached compressed pages */
0152     BASE_CHECK, /* check kernel status */
0153 };
0154 
0155 struct nat_entry_set {
0156     struct list_head set_list;  /* link with other nat sets */
0157     struct list_head entry_list;    /* link with dirty nat entries */
0158     nid_t set;          /* set number*/
0159     unsigned int entry_cnt;     /* the # of nat entries in set */
0160 };
0161 
0162 struct free_nid {
0163     struct list_head list;  /* for free node id list */
0164     nid_t nid;      /* node id */
0165     int state;      /* in use or not: FREE_NID or PREALLOC_NID */
0166 };
0167 
0168 static inline void next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
0169 {
0170     struct f2fs_nm_info *nm_i = NM_I(sbi);
0171     struct free_nid *fnid;
0172 
0173     spin_lock(&nm_i->nid_list_lock);
0174     if (nm_i->nid_cnt[FREE_NID] <= 0) {
0175         spin_unlock(&nm_i->nid_list_lock);
0176         return;
0177     }
0178     fnid = list_first_entry(&nm_i->free_nid_list, struct free_nid, list);
0179     *nid = fnid->nid;
0180     spin_unlock(&nm_i->nid_list_lock);
0181 }
0182 
0183 /*
0184  * inline functions
0185  */
0186 static inline void get_nat_bitmap(struct f2fs_sb_info *sbi, void *addr)
0187 {
0188     struct f2fs_nm_info *nm_i = NM_I(sbi);
0189 
0190 #ifdef CONFIG_F2FS_CHECK_FS
0191     if (memcmp(nm_i->nat_bitmap, nm_i->nat_bitmap_mir,
0192                         nm_i->bitmap_size))
0193         f2fs_bug_on(sbi, 1);
0194 #endif
0195     memcpy(addr, nm_i->nat_bitmap, nm_i->bitmap_size);
0196 }
0197 
0198 static inline pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start)
0199 {
0200     struct f2fs_nm_info *nm_i = NM_I(sbi);
0201     pgoff_t block_off;
0202     pgoff_t block_addr;
0203 
0204     /*
0205      * block_off = segment_off * 512 + off_in_segment
0206      * OLD = (segment_off * 512) * 2 + off_in_segment
0207      * NEW = 2 * (segment_off * 512 + off_in_segment) - off_in_segment
0208      */
0209     block_off = NAT_BLOCK_OFFSET(start);
0210 
0211     block_addr = (pgoff_t)(nm_i->nat_blkaddr +
0212         (block_off << 1) -
0213         (block_off & (sbi->blocks_per_seg - 1)));
0214 
0215     if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
0216         block_addr += sbi->blocks_per_seg;
0217 
0218     return block_addr;
0219 }
0220 
0221 static inline pgoff_t next_nat_addr(struct f2fs_sb_info *sbi,
0222                         pgoff_t block_addr)
0223 {
0224     struct f2fs_nm_info *nm_i = NM_I(sbi);
0225 
0226     block_addr -= nm_i->nat_blkaddr;
0227     block_addr ^= 1 << sbi->log_blocks_per_seg;
0228     return block_addr + nm_i->nat_blkaddr;
0229 }
0230 
0231 static inline void set_to_next_nat(struct f2fs_nm_info *nm_i, nid_t start_nid)
0232 {
0233     unsigned int block_off = NAT_BLOCK_OFFSET(start_nid);
0234 
0235     f2fs_change_bit(block_off, nm_i->nat_bitmap);
0236 #ifdef CONFIG_F2FS_CHECK_FS
0237     f2fs_change_bit(block_off, nm_i->nat_bitmap_mir);
0238 #endif
0239 }
0240 
0241 static inline nid_t ino_of_node(struct page *node_page)
0242 {
0243     struct f2fs_node *rn = F2FS_NODE(node_page);
0244     return le32_to_cpu(rn->footer.ino);
0245 }
0246 
0247 static inline nid_t nid_of_node(struct page *node_page)
0248 {
0249     struct f2fs_node *rn = F2FS_NODE(node_page);
0250     return le32_to_cpu(rn->footer.nid);
0251 }
0252 
0253 static inline unsigned int ofs_of_node(struct page *node_page)
0254 {
0255     struct f2fs_node *rn = F2FS_NODE(node_page);
0256     unsigned flag = le32_to_cpu(rn->footer.flag);
0257     return flag >> OFFSET_BIT_SHIFT;
0258 }
0259 
0260 static inline __u64 cpver_of_node(struct page *node_page)
0261 {
0262     struct f2fs_node *rn = F2FS_NODE(node_page);
0263     return le64_to_cpu(rn->footer.cp_ver);
0264 }
0265 
0266 static inline block_t next_blkaddr_of_node(struct page *node_page)
0267 {
0268     struct f2fs_node *rn = F2FS_NODE(node_page);
0269     return le32_to_cpu(rn->footer.next_blkaddr);
0270 }
0271 
0272 static inline void fill_node_footer(struct page *page, nid_t nid,
0273                 nid_t ino, unsigned int ofs, bool reset)
0274 {
0275     struct f2fs_node *rn = F2FS_NODE(page);
0276     unsigned int old_flag = 0;
0277 
0278     if (reset)
0279         memset(rn, 0, sizeof(*rn));
0280     else
0281         old_flag = le32_to_cpu(rn->footer.flag);
0282 
0283     rn->footer.nid = cpu_to_le32(nid);
0284     rn->footer.ino = cpu_to_le32(ino);
0285 
0286     /* should remain old flag bits such as COLD_BIT_SHIFT */
0287     rn->footer.flag = cpu_to_le32((ofs << OFFSET_BIT_SHIFT) |
0288                     (old_flag & OFFSET_BIT_MASK));
0289 }
0290 
0291 static inline void copy_node_footer(struct page *dst, struct page *src)
0292 {
0293     struct f2fs_node *src_rn = F2FS_NODE(src);
0294     struct f2fs_node *dst_rn = F2FS_NODE(dst);
0295     memcpy(&dst_rn->footer, &src_rn->footer, sizeof(struct node_footer));
0296 }
0297 
0298 static inline void fill_node_footer_blkaddr(struct page *page, block_t blkaddr)
0299 {
0300     struct f2fs_checkpoint *ckpt = F2FS_CKPT(F2FS_P_SB(page));
0301     struct f2fs_node *rn = F2FS_NODE(page);
0302     __u64 cp_ver = cur_cp_version(ckpt);
0303 
0304     if (__is_set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG))
0305         cp_ver |= (cur_cp_crc(ckpt) << 32);
0306 
0307     rn->footer.cp_ver = cpu_to_le64(cp_ver);
0308     rn->footer.next_blkaddr = cpu_to_le32(blkaddr);
0309 }
0310 
0311 static inline bool is_recoverable_dnode(struct page *page)
0312 {
0313     struct f2fs_checkpoint *ckpt = F2FS_CKPT(F2FS_P_SB(page));
0314     __u64 cp_ver = cur_cp_version(ckpt);
0315 
0316     /* Don't care crc part, if fsck.f2fs sets it. */
0317     if (__is_set_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG))
0318         return (cp_ver << 32) == (cpver_of_node(page) << 32);
0319 
0320     if (__is_set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG))
0321         cp_ver |= (cur_cp_crc(ckpt) << 32);
0322 
0323     return cp_ver == cpver_of_node(page);
0324 }
0325 
0326 /*
0327  * f2fs assigns the following node offsets described as (num).
0328  * N = NIDS_PER_BLOCK
0329  *
0330  *  Inode block (0)
0331  *    |- direct node (1)
0332  *    |- direct node (2)
0333  *    |- indirect node (3)
0334  *    |            `- direct node (4 => 4 + N - 1)
0335  *    |- indirect node (4 + N)
0336  *    |            `- direct node (5 + N => 5 + 2N - 1)
0337  *    `- double indirect node (5 + 2N)
0338  *                 `- indirect node (6 + 2N)
0339  *                       `- direct node
0340  *                 ......
0341  *                 `- indirect node ((6 + 2N) + x(N + 1))
0342  *                       `- direct node
0343  *                 ......
0344  *                 `- indirect node ((6 + 2N) + (N - 1)(N + 1))
0345  *                       `- direct node
0346  */
0347 static inline bool IS_DNODE(struct page *node_page)
0348 {
0349     unsigned int ofs = ofs_of_node(node_page);
0350 
0351     if (f2fs_has_xattr_block(ofs))
0352         return true;
0353 
0354     if (ofs == 3 || ofs == 4 + NIDS_PER_BLOCK ||
0355             ofs == 5 + 2 * NIDS_PER_BLOCK)
0356         return false;
0357     if (ofs >= 6 + 2 * NIDS_PER_BLOCK) {
0358         ofs -= 6 + 2 * NIDS_PER_BLOCK;
0359         if (!((long int)ofs % (NIDS_PER_BLOCK + 1)))
0360             return false;
0361     }
0362     return true;
0363 }
0364 
0365 static inline int set_nid(struct page *p, int off, nid_t nid, bool i)
0366 {
0367     struct f2fs_node *rn = F2FS_NODE(p);
0368 
0369     f2fs_wait_on_page_writeback(p, NODE, true, true);
0370 
0371     if (i)
0372         rn->i.i_nid[off - NODE_DIR1_BLOCK] = cpu_to_le32(nid);
0373     else
0374         rn->in.nid[off] = cpu_to_le32(nid);
0375     return set_page_dirty(p);
0376 }
0377 
0378 static inline nid_t get_nid(struct page *p, int off, bool i)
0379 {
0380     struct f2fs_node *rn = F2FS_NODE(p);
0381 
0382     if (i)
0383         return le32_to_cpu(rn->i.i_nid[off - NODE_DIR1_BLOCK]);
0384     return le32_to_cpu(rn->in.nid[off]);
0385 }
0386 
0387 /*
0388  * Coldness identification:
0389  *  - Mark cold files in f2fs_inode_info
0390  *  - Mark cold node blocks in their node footer
0391  *  - Mark cold data pages in page cache
0392  */
0393 
0394 static inline int is_node(struct page *page, int type)
0395 {
0396     struct f2fs_node *rn = F2FS_NODE(page);
0397     return le32_to_cpu(rn->footer.flag) & (1 << type);
0398 }
0399 
0400 #define is_cold_node(page)  is_node(page, COLD_BIT_SHIFT)
0401 #define is_fsync_dnode(page)    is_node(page, FSYNC_BIT_SHIFT)
0402 #define is_dent_dnode(page) is_node(page, DENT_BIT_SHIFT)
0403 
0404 static inline void set_cold_node(struct page *page, bool is_dir)
0405 {
0406     struct f2fs_node *rn = F2FS_NODE(page);
0407     unsigned int flag = le32_to_cpu(rn->footer.flag);
0408 
0409     if (is_dir)
0410         flag &= ~(0x1 << COLD_BIT_SHIFT);
0411     else
0412         flag |= (0x1 << COLD_BIT_SHIFT);
0413     rn->footer.flag = cpu_to_le32(flag);
0414 }
0415 
0416 static inline void set_mark(struct page *page, int mark, int type)
0417 {
0418     struct f2fs_node *rn = F2FS_NODE(page);
0419     unsigned int flag = le32_to_cpu(rn->footer.flag);
0420     if (mark)
0421         flag |= (0x1 << type);
0422     else
0423         flag &= ~(0x1 << type);
0424     rn->footer.flag = cpu_to_le32(flag);
0425 
0426 #ifdef CONFIG_F2FS_CHECK_FS
0427     f2fs_inode_chksum_set(F2FS_P_SB(page), page);
0428 #endif
0429 }
0430 #define set_dentry_mark(page, mark) set_mark(page, mark, DENT_BIT_SHIFT)
0431 #define set_fsync_mark(page, mark)  set_mark(page, mark, FSYNC_BIT_SHIFT)