Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (c) 2018 Red Hat, Inc.
0004  * All rights reserved.
0005  */
0006 
0007 #ifndef __LIBXFS_AG_H
0008 #define __LIBXFS_AG_H 1
0009 
0010 struct xfs_mount;
0011 struct xfs_trans;
0012 struct xfs_perag;
0013 
0014 /*
0015  * Per-ag infrastructure
0016  */
0017 
0018 /* per-AG block reservation data structures*/
0019 struct xfs_ag_resv {
0020     /* number of blocks originally reserved here */
0021     xfs_extlen_t            ar_orig_reserved;
0022     /* number of blocks reserved here */
0023     xfs_extlen_t            ar_reserved;
0024     /* number of blocks originally asked for */
0025     xfs_extlen_t            ar_asked;
0026 };
0027 
0028 /*
0029  * Per-ag incore structure, copies of information in agf and agi, to improve the
0030  * performance of allocation group selection.
0031  */
0032 struct xfs_perag {
0033     struct xfs_mount *pag_mount;    /* owner filesystem */
0034     xfs_agnumber_t  pag_agno;   /* AG this structure belongs to */
0035     atomic_t    pag_ref;    /* perag reference count */
0036     char        pagf_init;  /* this agf's entry is initialized */
0037     char        pagi_init;  /* this agi's entry is initialized */
0038     char        pagf_metadata;  /* the agf is preferred to be metadata */
0039     char        pagi_inodeok;   /* The agi is ok for inodes */
0040     uint8_t     pagf_levels[XFS_BTNUM_AGF];
0041                     /* # of levels in bno & cnt btree */
0042     bool        pagf_agflreset; /* agfl requires reset before use */
0043     uint32_t    pagf_flcount;   /* count of blocks in freelist */
0044     xfs_extlen_t    pagf_freeblks;  /* total free blocks */
0045     xfs_extlen_t    pagf_longest;   /* longest free space */
0046     uint32_t    pagf_btreeblks; /* # of blocks held in AGF btrees */
0047     xfs_agino_t pagi_freecount; /* number of free inodes */
0048     xfs_agino_t pagi_count; /* number of allocated inodes */
0049 
0050     /*
0051      * Inode allocation search lookup optimisation.
0052      * If the pagino matches, the search for new inodes
0053      * doesn't need to search the near ones again straight away
0054      */
0055     xfs_agino_t pagl_pagino;
0056     xfs_agino_t pagl_leftrec;
0057     xfs_agino_t pagl_rightrec;
0058 
0059     int     pagb_count; /* pagb slots in use */
0060     uint8_t     pagf_refcount_level; /* recount btree height */
0061 
0062     /* Blocks reserved for all kinds of metadata. */
0063     struct xfs_ag_resv  pag_meta_resv;
0064     /* Blocks reserved for the reverse mapping btree. */
0065     struct xfs_ag_resv  pag_rmapbt_resv;
0066 
0067     /* for rcu-safe freeing */
0068     struct rcu_head rcu_head;
0069 
0070     /* Precalculated geometry info */
0071     xfs_agblock_t       block_count;
0072     xfs_agblock_t       min_block;
0073     xfs_agino_t     agino_min;
0074     xfs_agino_t     agino_max;
0075 
0076 #ifdef __KERNEL__
0077     /* -- kernel only structures below this line -- */
0078 
0079     /*
0080      * Bitsets of per-ag metadata that have been checked and/or are sick.
0081      * Callers should hold pag_state_lock before accessing this field.
0082      */
0083     uint16_t    pag_checked;
0084     uint16_t    pag_sick;
0085     spinlock_t  pag_state_lock;
0086 
0087     spinlock_t  pagb_lock;  /* lock for pagb_tree */
0088     struct rb_root  pagb_tree;  /* ordered tree of busy extents */
0089     unsigned int    pagb_gen;   /* generation count for pagb_tree */
0090     wait_queue_head_t pagb_wait;    /* woken when pagb_gen changes */
0091 
0092     atomic_t        pagf_fstrms;    /* # of filestreams active in this AG */
0093 
0094     spinlock_t  pag_ici_lock;   /* incore inode cache lock */
0095     struct radix_tree_root pag_ici_root;    /* incore inode cache root */
0096     int     pag_ici_reclaimable;    /* reclaimable inodes */
0097     unsigned long   pag_ici_reclaim_cursor; /* reclaim restart point */
0098 
0099     /* buffer cache index */
0100     spinlock_t  pag_buf_lock;   /* lock for pag_buf_hash */
0101     struct rhashtable pag_buf_hash;
0102 
0103     /* background prealloc block trimming */
0104     struct delayed_work pag_blockgc_work;
0105 
0106 #endif /* __KERNEL__ */
0107 };
0108 
0109 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
0110             xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
0111 int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
0112 void xfs_free_perag(struct xfs_mount *mp);
0113 
0114 struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
0115 struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *mp, xfs_agnumber_t agno,
0116         unsigned int tag);
0117 void xfs_perag_put(struct xfs_perag *pag);
0118 
0119 /*
0120  * Per-ag geometry infomation and validation
0121  */
0122 xfs_agblock_t xfs_ag_block_count(struct xfs_mount *mp, xfs_agnumber_t agno);
0123 void xfs_agino_range(struct xfs_mount *mp, xfs_agnumber_t agno,
0124         xfs_agino_t *first, xfs_agino_t *last);
0125 
0126 static inline bool
0127 xfs_verify_agbno(struct xfs_perag *pag, xfs_agblock_t agbno)
0128 {
0129     if (agbno >= pag->block_count)
0130         return false;
0131     if (agbno <= pag->min_block)
0132         return false;
0133     return true;
0134 }
0135 
0136 /*
0137  * Verify that an AG inode number pointer neither points outside the AG
0138  * nor points at static metadata.
0139  */
0140 static inline bool
0141 xfs_verify_agino(struct xfs_perag *pag, xfs_agino_t agino)
0142 {
0143     if (agino < pag->agino_min)
0144         return false;
0145     if (agino > pag->agino_max)
0146         return false;
0147     return true;
0148 }
0149 
0150 /*
0151  * Verify that an AG inode number pointer neither points outside the AG
0152  * nor points at static metadata, or is NULLAGINO.
0153  */
0154 static inline bool
0155 xfs_verify_agino_or_null(struct xfs_perag *pag, xfs_agino_t agino)
0156 {
0157     if (agino == NULLAGINO)
0158         return true;
0159     return xfs_verify_agino(pag, agino);
0160 }
0161 
0162 static inline bool
0163 xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
0164 {
0165     return mp->m_sb.sb_logstart > 0 &&
0166            agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
0167 }
0168 
0169 /*
0170  * Perag iteration APIs
0171  */
0172 static inline struct xfs_perag *
0173 xfs_perag_next(
0174     struct xfs_perag    *pag,
0175     xfs_agnumber_t      *agno,
0176     xfs_agnumber_t      end_agno)
0177 {
0178     struct xfs_mount    *mp = pag->pag_mount;
0179 
0180     *agno = pag->pag_agno + 1;
0181     xfs_perag_put(pag);
0182     if (*agno > end_agno)
0183         return NULL;
0184     return xfs_perag_get(mp, *agno);
0185 }
0186 
0187 #define for_each_perag_range(mp, agno, end_agno, pag) \
0188     for ((pag) = xfs_perag_get((mp), (agno)); \
0189         (pag) != NULL; \
0190         (pag) = xfs_perag_next((pag), &(agno), (end_agno)))
0191 
0192 #define for_each_perag_from(mp, agno, pag) \
0193     for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
0194 
0195 
0196 #define for_each_perag(mp, agno, pag) \
0197     (agno) = 0; \
0198     for_each_perag_from((mp), (agno), (pag))
0199 
0200 #define for_each_perag_tag(mp, agno, pag, tag) \
0201     for ((agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \
0202         (pag) != NULL; \
0203         (agno) = (pag)->pag_agno + 1, \
0204         xfs_perag_put(pag), \
0205         (pag) = xfs_perag_get_tag((mp), (agno), (tag)))
0206 
0207 struct aghdr_init_data {
0208     /* per ag data */
0209     xfs_agblock_t       agno;       /* ag to init */
0210     xfs_extlen_t        agsize;     /* new AG size */
0211     struct list_head    buffer_list;    /* buffer writeback list */
0212     xfs_rfsblock_t      nfree;      /* cumulative new free space */
0213 
0214     /* per header data */
0215     xfs_daddr_t     daddr;      /* header location */
0216     size_t          numblks;    /* size of header */
0217     xfs_btnum_t     type;       /* type of btree root block */
0218 };
0219 
0220 int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
0221 int xfs_ag_shrink_space(struct xfs_perag *pag, struct xfs_trans **tpp,
0222             xfs_extlen_t delta);
0223 int xfs_ag_extend_space(struct xfs_perag *pag, struct xfs_trans *tp,
0224             xfs_extlen_t len);
0225 int xfs_ag_get_geometry(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
0226 
0227 #endif /* __LIBXFS_AG_H */