Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef BTRFS_SPACE_INFO_H
0004 #define BTRFS_SPACE_INFO_H
0005 
0006 #include "volumes.h"
0007 
0008 struct btrfs_space_info {
0009     spinlock_t lock;
0010 
0011     u64 total_bytes;    /* total bytes in the space,
0012                    this doesn't take mirrors into account */
0013     u64 bytes_used;     /* total bytes used,
0014                    this doesn't take mirrors into account */
0015     u64 bytes_pinned;   /* total bytes pinned, will be freed when the
0016                    transaction finishes */
0017     u64 bytes_reserved; /* total bytes the allocator has reserved for
0018                    current allocations */
0019     u64 bytes_may_use;  /* number of bytes that may be used for
0020                    delalloc/allocations */
0021     u64 bytes_readonly; /* total bytes that are read only */
0022     /* Total bytes in the space, but only accounts active block groups. */
0023     u64 active_total_bytes;
0024     u64 bytes_zone_unusable;    /* total bytes that are unusable until
0025                        resetting the device zone */
0026 
0027     u64 max_extent_size;    /* This will hold the maximum extent size of
0028                    the space info if we had an ENOSPC in the
0029                    allocator. */
0030     /* Chunk size in bytes */
0031     u64 chunk_size;
0032 
0033     /*
0034      * Once a block group drops below this threshold (percents) we'll
0035      * schedule it for reclaim.
0036      */
0037     int bg_reclaim_threshold;
0038 
0039     int clamp;      /* Used to scale our threshold for preemptive
0040                    flushing. The value is >> clamp, so turns
0041                    out to be a 2^clamp divisor. */
0042 
0043     unsigned int full:1;    /* indicates that we cannot allocate any more
0044                    chunks for this space */
0045     unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
0046 
0047     unsigned int flush:1;       /* set if we are trying to make space */
0048 
0049     unsigned int force_alloc;   /* set if we need to force a chunk
0050                        alloc for this space */
0051 
0052     u64 disk_used;      /* total bytes used on disk */
0053     u64 disk_total;     /* total bytes on disk, takes mirrors into
0054                    account */
0055 
0056     u64 flags;
0057 
0058     struct list_head list;
0059     /* Protected by the spinlock 'lock'. */
0060     struct list_head ro_bgs;
0061     struct list_head priority_tickets;
0062     struct list_head tickets;
0063 
0064     /*
0065      * Size of space that needs to be reclaimed in order to satisfy pending
0066      * tickets
0067      */
0068     u64 reclaim_size;
0069 
0070     /*
0071      * tickets_id just indicates the next ticket will be handled, so note
0072      * it's not stored per ticket.
0073      */
0074     u64 tickets_id;
0075 
0076     struct rw_semaphore groups_sem;
0077     /* for block groups in our same type */
0078     struct list_head block_groups[BTRFS_NR_RAID_TYPES];
0079 
0080     struct kobject kobj;
0081     struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
0082 };
0083 
0084 struct reserve_ticket {
0085     u64 bytes;
0086     int error;
0087     bool steal;
0088     struct list_head list;
0089     wait_queue_head_t wait;
0090 };
0091 
0092 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
0093 {
0094     return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
0095         (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
0096 }
0097 
0098 /*
0099  *
0100  * Declare a helper function to detect underflow of various space info members
0101  */
0102 #define DECLARE_SPACE_INFO_UPDATE(name, trace_name)         \
0103 static inline void                          \
0104 btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info,       \
0105                    struct btrfs_space_info *sinfo,      \
0106                    s64 bytes)               \
0107 {                                   \
0108     const u64 abs_bytes = (bytes < 0) ? -bytes : bytes;     \
0109     lockdep_assert_held(&sinfo->lock);              \
0110     trace_update_##name(fs_info, sinfo, sinfo->name, bytes);    \
0111     trace_btrfs_space_reservation(fs_info, trace_name,      \
0112                       sinfo->flags, abs_bytes,      \
0113                       bytes > 0);           \
0114     if (bytes < 0 && sinfo->name < -bytes) {            \
0115         WARN_ON(1);                     \
0116         sinfo->name = 0;                    \
0117         return;                         \
0118     }                               \
0119     sinfo->name += bytes;                       \
0120 }
0121 
0122 DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
0123 DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
0124 
0125 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
0126 void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
0127                  u64 total_bytes, u64 bytes_used,
0128                  u64 bytes_readonly, u64 bytes_zone_unusable,
0129                  bool active, struct btrfs_space_info **space_info);
0130 void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
0131                     u64 chunk_size);
0132 struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
0133                            u64 flags);
0134 u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
0135               bool may_use_included);
0136 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
0137 void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
0138                struct btrfs_space_info *info, u64 bytes,
0139                int dump_block_groups);
0140 int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
0141                  struct btrfs_block_rsv *block_rsv,
0142                  u64 orig_bytes,
0143                  enum btrfs_reserve_flush_enum flush);
0144 void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
0145                 struct btrfs_space_info *space_info);
0146 int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
0147              struct btrfs_space_info *space_info, u64 bytes,
0148              enum btrfs_reserve_flush_enum flush);
0149 
0150 static inline void btrfs_space_info_free_bytes_may_use(
0151                 struct btrfs_fs_info *fs_info,
0152                 struct btrfs_space_info *space_info,
0153                 u64 num_bytes)
0154 {
0155     spin_lock(&space_info->lock);
0156     btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
0157     btrfs_try_granting_tickets(fs_info, space_info);
0158     spin_unlock(&space_info->lock);
0159 }
0160 int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
0161                  enum btrfs_reserve_flush_enum flush);
0162 #endif /* BTRFS_SPACE_INFO_H */