Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * NILFS block mapping.
0004  *
0005  * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
0006  *
0007  * Written by Koji Sato.
0008  */
0009 
0010 #ifndef _NILFS_BMAP_H
0011 #define _NILFS_BMAP_H
0012 
0013 #include <linux/types.h>
0014 #include <linux/fs.h>
0015 #include <linux/buffer_head.h>
0016 #include <linux/nilfs2_ondisk.h>    /* nilfs_binfo, nilfs_inode, etc */
0017 #include "alloc.h"
0018 #include "dat.h"
0019 
0020 #define NILFS_BMAP_INVALID_PTR  0
0021 
0022 #define nilfs_bmap_keydiff_abs(diff)    ((diff) < 0 ? -(diff) : (diff))
0023 
0024 
0025 struct nilfs_bmap;
0026 
0027 /**
0028  * union nilfs_bmap_ptr_req - request for bmap ptr
0029  * @bpr_ptr: bmap pointer
0030  * @bpr_req: request for persistent allocator
0031  */
0032 union nilfs_bmap_ptr_req {
0033     __u64 bpr_ptr;
0034     struct nilfs_palloc_req bpr_req;
0035 };
0036 
0037 /**
0038  * struct nilfs_bmap_stats - bmap statistics
0039  * @bs_nblocks: number of blocks created or deleted
0040  */
0041 struct nilfs_bmap_stats {
0042     unsigned int bs_nblocks;
0043 };
0044 
0045 /**
0046  * struct nilfs_bmap_operations - bmap operation table
0047  */
0048 struct nilfs_bmap_operations {
0049     int (*bop_lookup)(const struct nilfs_bmap *, __u64, int, __u64 *);
0050     int (*bop_lookup_contig)(const struct nilfs_bmap *, __u64, __u64 *,
0051                  unsigned int);
0052     int (*bop_insert)(struct nilfs_bmap *, __u64, __u64);
0053     int (*bop_delete)(struct nilfs_bmap *, __u64);
0054     void (*bop_clear)(struct nilfs_bmap *);
0055 
0056     int (*bop_propagate)(struct nilfs_bmap *, struct buffer_head *);
0057     void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
0058                      struct list_head *);
0059 
0060     int (*bop_assign)(struct nilfs_bmap *,
0061               struct buffer_head **,
0062               sector_t,
0063               union nilfs_binfo *);
0064     int (*bop_mark)(struct nilfs_bmap *, __u64, int);
0065 
0066     int (*bop_seek_key)(const struct nilfs_bmap *, __u64, __u64 *);
0067     int (*bop_last_key)(const struct nilfs_bmap *, __u64 *);
0068 
0069     /* The following functions are internal use only. */
0070     int (*bop_check_insert)(const struct nilfs_bmap *, __u64);
0071     int (*bop_check_delete)(struct nilfs_bmap *, __u64);
0072     int (*bop_gather_data)(struct nilfs_bmap *, __u64 *, __u64 *, int);
0073 };
0074 
0075 
0076 #define NILFS_BMAP_SIZE     (NILFS_INODE_BMAP_SIZE * sizeof(__le64))
0077 #define NILFS_BMAP_KEY_BIT  (sizeof(unsigned long) * 8 /* CHAR_BIT */)
0078 #define NILFS_BMAP_NEW_PTR_INIT \
0079     (1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
0080 
0081 static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
0082 {
0083     return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
0084 }
0085 
0086 
0087 /**
0088  * struct nilfs_bmap - bmap structure
0089  * @b_u: raw data
0090  * @b_sem: semaphore
0091  * @b_inode: owner of bmap
0092  * @b_ops: bmap operation table
0093  * @b_last_allocated_key: last allocated key for data block
0094  * @b_last_allocated_ptr: last allocated ptr for data block
0095  * @b_ptr_type: pointer type
0096  * @b_state: state
0097  * @b_nchildren_per_block: maximum number of child nodes for non-root nodes
0098  */
0099 struct nilfs_bmap {
0100     union {
0101         __u8 u_flags;
0102         __le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
0103     } b_u;
0104     struct rw_semaphore b_sem;
0105     struct inode *b_inode;
0106     const struct nilfs_bmap_operations *b_ops;
0107     __u64 b_last_allocated_key;
0108     __u64 b_last_allocated_ptr;
0109     int b_ptr_type;
0110     int b_state;
0111     __u16 b_nchildren_per_block;
0112 };
0113 
0114 /* pointer type */
0115 #define NILFS_BMAP_PTR_P    0   /* physical block number (i.e. LBN) */
0116 #define NILFS_BMAP_PTR_VS   1   /*
0117                      * virtual block number (single
0118                      * version)
0119                      */
0120 #define NILFS_BMAP_PTR_VM   2   /*
0121                      * virtual block number (has multiple
0122                      * versions)
0123                      */
0124 #define NILFS_BMAP_PTR_U    (-1)    /* never perform pointer operations */
0125 
0126 #define NILFS_BMAP_USE_VBN(bmap)    ((bmap)->b_ptr_type > 0)
0127 
0128 /* state */
0129 #define NILFS_BMAP_DIRTY    0x00000001
0130 
0131 /**
0132  * struct nilfs_bmap_store - shadow copy of bmap state
0133  * @data: cached raw block mapping of on-disk inode
0134  * @last_allocated_key: cached value of last allocated key for data block
0135  * @last_allocated_ptr: cached value of last allocated ptr for data block
0136  * @state: cached value of state field of bmap structure
0137  */
0138 struct nilfs_bmap_store {
0139     __le64 data[NILFS_BMAP_SIZE / sizeof(__le64)];
0140     __u64 last_allocated_key;
0141     __u64 last_allocated_ptr;
0142     int state;
0143 };
0144 
0145 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
0146 int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
0147 void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
0148 int nilfs_bmap_lookup_contig(struct nilfs_bmap *, __u64, __u64 *, unsigned int);
0149 int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec);
0150 int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key);
0151 int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp);
0152 int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp);
0153 int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key);
0154 void nilfs_bmap_clear(struct nilfs_bmap *);
0155 int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
0156 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
0157 int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
0158               unsigned long, union nilfs_binfo *);
0159 int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
0160 int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
0161 
0162 void nilfs_bmap_init_gc(struct nilfs_bmap *);
0163 
0164 void nilfs_bmap_save(const struct nilfs_bmap *, struct nilfs_bmap_store *);
0165 void nilfs_bmap_restore(struct nilfs_bmap *, const struct nilfs_bmap_store *);
0166 
0167 static inline int nilfs_bmap_lookup(struct nilfs_bmap *bmap, __u64 key,
0168                     __u64 *ptr)
0169 {
0170     return nilfs_bmap_lookup_at_level(bmap, key, 1, ptr);
0171 }
0172 
0173 /*
0174  * Internal use only
0175  */
0176 struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *);
0177 
0178 static inline int nilfs_bmap_prepare_alloc_ptr(struct nilfs_bmap *bmap,
0179                            union nilfs_bmap_ptr_req *req,
0180                            struct inode *dat)
0181 {
0182     if (dat)
0183         return nilfs_dat_prepare_alloc(dat, &req->bpr_req);
0184     /* ignore target ptr */
0185     req->bpr_ptr = bmap->b_last_allocated_ptr++;
0186     return 0;
0187 }
0188 
0189 static inline void nilfs_bmap_commit_alloc_ptr(struct nilfs_bmap *bmap,
0190                            union nilfs_bmap_ptr_req *req,
0191                            struct inode *dat)
0192 {
0193     if (dat)
0194         nilfs_dat_commit_alloc(dat, &req->bpr_req);
0195 }
0196 
0197 static inline void nilfs_bmap_abort_alloc_ptr(struct nilfs_bmap *bmap,
0198                           union nilfs_bmap_ptr_req *req,
0199                           struct inode *dat)
0200 {
0201     if (dat)
0202         nilfs_dat_abort_alloc(dat, &req->bpr_req);
0203     else
0204         bmap->b_last_allocated_ptr--;
0205 }
0206 
0207 static inline int nilfs_bmap_prepare_end_ptr(struct nilfs_bmap *bmap,
0208                          union nilfs_bmap_ptr_req *req,
0209                          struct inode *dat)
0210 {
0211     return dat ? nilfs_dat_prepare_end(dat, &req->bpr_req) : 0;
0212 }
0213 
0214 static inline void nilfs_bmap_commit_end_ptr(struct nilfs_bmap *bmap,
0215                          union nilfs_bmap_ptr_req *req,
0216                          struct inode *dat)
0217 {
0218     if (dat)
0219         nilfs_dat_commit_end(dat, &req->bpr_req,
0220                      bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
0221 }
0222 
0223 static inline void nilfs_bmap_abort_end_ptr(struct nilfs_bmap *bmap,
0224                         union nilfs_bmap_ptr_req *req,
0225                         struct inode *dat)
0226 {
0227     if (dat)
0228         nilfs_dat_abort_end(dat, &req->bpr_req);
0229 }
0230 
0231 static inline void nilfs_bmap_set_target_v(struct nilfs_bmap *bmap, __u64 key,
0232                        __u64 ptr)
0233 {
0234     bmap->b_last_allocated_key = key;
0235     bmap->b_last_allocated_ptr = ptr;
0236 }
0237 
0238 __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
0239                   const struct buffer_head *);
0240 
0241 __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
0242 __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
0243 
0244 
0245 /* Assume that bmap semaphore is locked. */
0246 static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
0247 {
0248     return !!(bmap->b_state & NILFS_BMAP_DIRTY);
0249 }
0250 
0251 /* Assume that bmap semaphore is locked. */
0252 static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
0253 {
0254     bmap->b_state |= NILFS_BMAP_DIRTY;
0255 }
0256 
0257 /* Assume that bmap semaphore is locked. */
0258 static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
0259 {
0260     bmap->b_state &= ~NILFS_BMAP_DIRTY;
0261 }
0262 
0263 
0264 #define NILFS_BMAP_LARGE    0x1
0265 
0266 #define NILFS_BMAP_SMALL_LOW    NILFS_DIRECT_KEY_MIN
0267 #define NILFS_BMAP_SMALL_HIGH   NILFS_DIRECT_KEY_MAX
0268 #define NILFS_BMAP_LARGE_LOW    NILFS_BTREE_ROOT_NCHILDREN_MAX
0269 #define NILFS_BMAP_LARGE_HIGH   NILFS_BTREE_KEY_MAX
0270 
0271 #endif  /* _NILFS_BMAP_H */