Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Block Translation Table library
0004  * Copyright (c) 2014-2015, Intel Corporation.
0005  */
0006 
0007 #ifndef _LINUX_BTT_H
0008 #define _LINUX_BTT_H
0009 
0010 #include <linux/types.h>
0011 
0012 #define BTT_SIG_LEN 16
0013 #define BTT_SIG "BTT_ARENA_INFO\0"
0014 #define MAP_ENT_SIZE 4
0015 #define MAP_TRIM_SHIFT 31
0016 #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
0017 #define MAP_ERR_SHIFT 30
0018 #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
0019 #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
0020 #define MAP_ENT_NORMAL 0xC0000000
0021 #define LOG_GRP_SIZE sizeof(struct log_group)
0022 #define LOG_ENT_SIZE sizeof(struct log_entry)
0023 #define ARENA_MIN_SIZE (1UL << 24)  /* 16 MB */
0024 #define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */
0025 #define RTT_VALID (1UL << 31)
0026 #define RTT_INVALID 0
0027 #define BTT_PG_SIZE 4096
0028 #define BTT_DEFAULT_NFREE ND_MAX_LANES
0029 #define LOG_SEQ_INIT 1
0030 
0031 #define IB_FLAG_ERROR 0x00000001
0032 #define IB_FLAG_ERROR_MASK 0x00000001
0033 
0034 #define ent_lba(ent) (ent & MAP_LBA_MASK)
0035 #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK))
0036 #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK))
0037 #define set_e_flag(ent) (ent |= MAP_ERR_MASK)
0038 /* 'normal' is both e and z flags set */
0039 #define ent_normal(ent) (ent_e_flag(ent) && ent_z_flag(ent))
0040 
0041 enum btt_init_state {
0042     INIT_UNCHECKED = 0,
0043     INIT_NOTFOUND,
0044     INIT_READY
0045 };
0046 
0047 /*
0048  * A log group represents one log 'lane', and consists of four log entries.
0049  * Two of the four entries are valid entries, and the remaining two are
0050  * padding. Due to an old bug in the padding location, we need to perform a
0051  * test to determine the padding scheme being used, and use that scheme
0052  * thereafter.
0053  *
0054  * In kernels prior to 4.15, 'log group' would have actual log entries at
0055  * indices (0, 2) and padding at indices (1, 3), where as the correct/updated
0056  * format has log entries at indices (0, 1) and padding at indices (2, 3).
0057  *
0058  * Old (pre 4.15) format:
0059  * +-----------------+-----------------+
0060  * |      ent[0]     |      ent[1]     |
0061  * |       16B       |       16B       |
0062  * | lba/old/new/seq |       pad       |
0063  * +-----------------------------------+
0064  * |      ent[2]     |      ent[3]     |
0065  * |       16B       |       16B       |
0066  * | lba/old/new/seq |       pad       |
0067  * +-----------------+-----------------+
0068  *
0069  * New format:
0070  * +-----------------+-----------------+
0071  * |      ent[0]     |      ent[1]     |
0072  * |       16B       |       16B       |
0073  * | lba/old/new/seq | lba/old/new/seq |
0074  * +-----------------------------------+
0075  * |      ent[2]     |      ent[3]     |
0076  * |       16B       |       16B       |
0077  * |       pad       |       pad       |
0078  * +-----------------+-----------------+
0079  *
0080  * We detect during start-up which format is in use, and set
0081  * arena->log_index[(0, 1)] with the detected format.
0082  */
0083 
0084 struct log_entry {
0085     __le32 lba;
0086     __le32 old_map;
0087     __le32 new_map;
0088     __le32 seq;
0089 };
0090 
0091 struct log_group {
0092     struct log_entry ent[4];
0093 };
0094 
0095 struct btt_sb {
0096     u8 signature[BTT_SIG_LEN];
0097     u8 uuid[16];
0098     u8 parent_uuid[16];
0099     __le32 flags;
0100     __le16 version_major;
0101     __le16 version_minor;
0102     __le32 external_lbasize;
0103     __le32 external_nlba;
0104     __le32 internal_lbasize;
0105     __le32 internal_nlba;
0106     __le32 nfree;
0107     __le32 infosize;
0108     __le64 nextoff;
0109     __le64 dataoff;
0110     __le64 mapoff;
0111     __le64 logoff;
0112     __le64 info2off;
0113     u8 padding[3968];
0114     __le64 checksum;
0115 };
0116 
0117 struct free_entry {
0118     u32 block;
0119     u8 sub;
0120     u8 seq;
0121     u8 has_err;
0122 };
0123 
0124 struct aligned_lock {
0125     union {
0126         spinlock_t lock;
0127         u8 cacheline_padding[L1_CACHE_BYTES];
0128     };
0129 };
0130 
0131 /**
0132  * struct arena_info - handle for an arena
0133  * @size:       Size in bytes this arena occupies on the raw device.
0134  *          This includes arena metadata.
0135  * @external_lba_start: The first external LBA in this arena.
0136  * @internal_nlba:  Number of internal blocks available in the arena
0137  *          including nfree reserved blocks
0138  * @internal_lbasize:   Internal and external lba sizes may be different as
0139  *          we can round up 'odd' external lbasizes such as 520B
0140  *          to be aligned.
0141  * @external_nlba:  Number of blocks contributed by the arena to the number
0142  *          reported to upper layers. (internal_nlba - nfree)
0143  * @external_lbasize:   LBA size as exposed to upper layers.
0144  * @nfree:      A reserve number of 'free' blocks that is used to
0145  *          handle incoming writes.
0146  * @version_major:  Metadata layout version major.
0147  * @version_minor:  Metadata layout version minor.
0148  * @sector_size:    The Linux sector size - 512 or 4096
0149  * @nextoff:        Offset in bytes to the start of the next arena.
0150  * @infooff:        Offset in bytes to the info block of this arena.
0151  * @dataoff:        Offset in bytes to the data area of this arena.
0152  * @mapoff:     Offset in bytes to the map area of this arena.
0153  * @logoff:     Offset in bytes to the log area of this arena.
0154  * @info2off:       Offset in bytes to the backup info block of this arena.
0155  * @freelist:       Pointer to in-memory list of free blocks
0156  * @rtt:        Pointer to in-memory "Read Tracking Table"
0157  * @map_locks:      Spinlocks protecting concurrent map writes
0158  * @nd_btt:     Pointer to parent nd_btt structure.
0159  * @list:       List head for list of arenas
0160  * @debugfs_dir:    Debugfs dentry
0161  * @flags:      Arena flags - may signify error states.
0162  * @err_lock:       Mutex for synchronizing error clearing.
0163  * @log_index:      Indices of the valid log entries in a log_group
0164  *
0165  * arena_info is a per-arena handle. Once an arena is narrowed down for an
0166  * IO, this struct is passed around for the duration of the IO.
0167  */
0168 struct arena_info {
0169     u64 size;           /* Total bytes for this arena */
0170     u64 external_lba_start;
0171     u32 internal_nlba;
0172     u32 internal_lbasize;
0173     u32 external_nlba;
0174     u32 external_lbasize;
0175     u32 nfree;
0176     u16 version_major;
0177     u16 version_minor;
0178     u32 sector_size;
0179     /* Byte offsets to the different on-media structures */
0180     u64 nextoff;
0181     u64 infooff;
0182     u64 dataoff;
0183     u64 mapoff;
0184     u64 logoff;
0185     u64 info2off;
0186     /* Pointers to other in-memory structures for this arena */
0187     struct free_entry *freelist;
0188     u32 *rtt;
0189     struct aligned_lock *map_locks;
0190     struct nd_btt *nd_btt;
0191     struct list_head list;
0192     struct dentry *debugfs_dir;
0193     /* Arena flags */
0194     u32 flags;
0195     struct mutex err_lock;
0196     int log_index[2];
0197 };
0198 
0199 struct badblocks;
0200 
0201 /**
0202  * struct btt - handle for a BTT instance
0203  * @btt_disk:       Pointer to the gendisk for BTT device
0204  * @arena_list:     Head of the list of arenas
0205  * @debugfs_dir:    Debugfs dentry
0206  * @nd_btt:     Parent nd_btt struct
0207  * @nlba:       Number of logical blocks exposed to the upper layers
0208  *          after removing the amount of space needed by metadata
0209  * @rawsize:        Total size in bytes of the available backing device
0210  * @lbasize:        LBA size as requested and presented to upper layers.
0211  *          This is sector_size + size of any metadata.
0212  * @sector_size:    The Linux sector size - 512 or 4096
0213  * @lanes:      Per-lane spinlocks
0214  * @init_lock:      Mutex used for the BTT initialization
0215  * @init_state:     Flag describing the initialization state for the BTT
0216  * @num_arenas:     Number of arenas in the BTT instance
0217  * @phys_bb:        Pointer to the namespace's badblocks structure
0218  */
0219 struct btt {
0220     struct gendisk *btt_disk;
0221     struct list_head arena_list;
0222     struct dentry *debugfs_dir;
0223     struct nd_btt *nd_btt;
0224     u64 nlba;
0225     unsigned long long rawsize;
0226     u32 lbasize;
0227     u32 sector_size;
0228     struct nd_region *nd_region;
0229     struct mutex init_lock;
0230     int init_state;
0231     int num_arenas;
0232     struct badblocks *phys_bb;
0233 };
0234 
0235 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
0236 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
0237         struct btt_sb *btt_sb);
0238 
0239 #endif