Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include "ctree.h"
0004 #include "delalloc-space.h"
0005 #include "block-rsv.h"
0006 #include "btrfs_inode.h"
0007 #include "space-info.h"
0008 #include "transaction.h"
0009 #include "qgroup.h"
0010 #include "block-group.h"
0011 
0012 /*
0013  * HOW DOES THIS WORK
0014  *
0015  * There are two stages to data reservations, one for data and one for metadata
0016  * to handle the new extents and checksums generated by writing data.
0017  *
0018  *
0019  * DATA RESERVATION
0020  *   The general flow of the data reservation is as follows
0021  *
0022  *   -> Reserve
0023  *     We call into btrfs_reserve_data_bytes() for the user request bytes that
0024  *     they wish to write.  We make this reservation and add it to
0025  *     space_info->bytes_may_use.  We set EXTENT_DELALLOC on the inode io_tree
0026  *     for the range and carry on if this is buffered, or follow up trying to
0027  *     make a real allocation if we are pre-allocating or doing O_DIRECT.
0028  *
0029  *   -> Use
0030  *     At writepages()/prealloc/O_DIRECT time we will call into
0031  *     btrfs_reserve_extent() for some part or all of this range of bytes.  We
0032  *     will make the allocation and subtract space_info->bytes_may_use by the
0033  *     original requested length and increase the space_info->bytes_reserved by
0034  *     the allocated length.  This distinction is important because compression
0035  *     may allocate a smaller on disk extent than we previously reserved.
0036  *
0037  *   -> Allocation
0038  *     finish_ordered_io() will insert the new file extent item for this range,
0039  *     and then add a delayed ref update for the extent tree.  Once that delayed
0040  *     ref is written the extent size is subtracted from
0041  *     space_info->bytes_reserved and added to space_info->bytes_used.
0042  *
0043  *   Error handling
0044  *
0045  *   -> By the reservation maker
0046  *     This is the simplest case, we haven't completed our operation and we know
0047  *     how much we reserved, we can simply call
0048  *     btrfs_free_reserved_data_space*() and it will be removed from
0049  *     space_info->bytes_may_use.
0050  *
0051  *   -> After the reservation has been made, but before cow_file_range()
0052  *     This is specifically for the delalloc case.  You must clear
0053  *     EXTENT_DELALLOC with the EXTENT_CLEAR_DATA_RESV bit, and the range will
0054  *     be subtracted from space_info->bytes_may_use.
0055  *
0056  * METADATA RESERVATION
0057  *   The general metadata reservation lifetimes are discussed elsewhere, this
0058  *   will just focus on how it is used for delalloc space.
0059  *
0060  *   We keep track of two things on a per inode bases
0061  *
0062  *   ->outstanding_extents
0063  *     This is the number of file extent items we'll need to handle all of the
0064  *     outstanding DELALLOC space we have in this inode.  We limit the maximum
0065  *     size of an extent, so a large contiguous dirty area may require more than
0066  *     one outstanding_extent, which is why count_max_extents() is used to
0067  *     determine how many outstanding_extents get added.
0068  *
0069  *   ->csum_bytes
0070  *     This is essentially how many dirty bytes we have for this inode, so we
0071  *     can calculate the number of checksum items we would have to add in order
0072  *     to checksum our outstanding data.
0073  *
0074  *   We keep a per-inode block_rsv in order to make it easier to keep track of
0075  *   our reservation.  We use btrfs_calculate_inode_block_rsv_size() to
0076  *   calculate the current theoretical maximum reservation we would need for the
0077  *   metadata for this inode.  We call this and then adjust our reservation as
0078  *   necessary, either by attempting to reserve more space, or freeing up excess
0079  *   space.
0080  *
0081  * OUTSTANDING_EXTENTS HANDLING
0082  *
0083  *  ->outstanding_extents is used for keeping track of how many extents we will
0084  *  need to use for this inode, and it will fluctuate depending on where you are
0085  *  in the life cycle of the dirty data.  Consider the following normal case for
0086  *  a completely clean inode, with a num_bytes < our maximum allowed extent size
0087  *
0088  *  -> reserve
0089  *    ->outstanding_extents += 1 (current value is 1)
0090  *
0091  *  -> set_delalloc
0092  *    ->outstanding_extents += 1 (current value is 2)
0093  *
0094  *  -> btrfs_delalloc_release_extents()
0095  *    ->outstanding_extents -= 1 (current value is 1)
0096  *
0097  *    We must call this once we are done, as we hold our reservation for the
0098  *    duration of our operation, and then assume set_delalloc will update the
0099  *    counter appropriately.
0100  *
0101  *  -> add ordered extent
0102  *    ->outstanding_extents += 1 (current value is 2)
0103  *
0104  *  -> btrfs_clear_delalloc_extent
0105  *    ->outstanding_extents -= 1 (current value is 1)
0106  *
0107  *  -> finish_ordered_io/btrfs_remove_ordered_extent
0108  *    ->outstanding_extents -= 1 (current value is 0)
0109  *
0110  *  Each stage is responsible for their own accounting of the extent, thus
0111  *  making error handling and cleanup easier.
0112  */
0113 
0114 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
0115 {
0116     struct btrfs_root *root = inode->root;
0117     struct btrfs_fs_info *fs_info = root->fs_info;
0118     enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
0119 
0120     /* Make sure bytes are sectorsize aligned */
0121     bytes = ALIGN(bytes, fs_info->sectorsize);
0122 
0123     if (btrfs_is_free_space_inode(inode))
0124         flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
0125 
0126     return btrfs_reserve_data_bytes(fs_info, bytes, flush);
0127 }
0128 
0129 int btrfs_check_data_free_space(struct btrfs_inode *inode,
0130             struct extent_changeset **reserved, u64 start, u64 len)
0131 {
0132     struct btrfs_fs_info *fs_info = inode->root->fs_info;
0133     int ret;
0134 
0135     /* align the range */
0136     len = round_up(start + len, fs_info->sectorsize) -
0137           round_down(start, fs_info->sectorsize);
0138     start = round_down(start, fs_info->sectorsize);
0139 
0140     ret = btrfs_alloc_data_chunk_ondemand(inode, len);
0141     if (ret < 0)
0142         return ret;
0143 
0144     /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
0145     ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
0146     if (ret < 0) {
0147         btrfs_free_reserved_data_space_noquota(fs_info, len);
0148         extent_changeset_free(*reserved);
0149         *reserved = NULL;
0150     } else {
0151         ret = 0;
0152     }
0153     return ret;
0154 }
0155 
0156 /*
0157  * Called if we need to clear a data reservation for this inode
0158  * Normally in a error case.
0159  *
0160  * This one will *NOT* use accurate qgroup reserved space API, just for case
0161  * which we can't sleep and is sure it won't affect qgroup reserved space.
0162  * Like clear_bit_hook().
0163  */
0164 void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
0165                         u64 len)
0166 {
0167     struct btrfs_space_info *data_sinfo;
0168 
0169     ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
0170 
0171     data_sinfo = fs_info->data_sinfo;
0172     btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
0173 }
0174 
0175 /*
0176  * Called if we need to clear a data reservation for this inode
0177  * Normally in a error case.
0178  *
0179  * This one will handle the per-inode data rsv map for accurate reserved
0180  * space framework.
0181  */
0182 void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
0183             struct extent_changeset *reserved, u64 start, u64 len)
0184 {
0185     struct btrfs_fs_info *fs_info = inode->root->fs_info;
0186 
0187     /* Make sure the range is aligned to sectorsize */
0188     len = round_up(start + len, fs_info->sectorsize) -
0189           round_down(start, fs_info->sectorsize);
0190     start = round_down(start, fs_info->sectorsize);
0191 
0192     btrfs_free_reserved_data_space_noquota(fs_info, len);
0193     btrfs_qgroup_free_data(inode, reserved, start, len);
0194 }
0195 
0196 /**
0197  * Release any excessive reservation
0198  *
0199  * @inode:       the inode we need to release from
0200  * @qgroup_free: free or convert qgroup meta. Unlike normal operation, qgroup
0201  *               meta reservation needs to know if we are freeing qgroup
0202  *               reservation or just converting it into per-trans.  Normally
0203  *               @qgroup_free is true for error handling, and false for normal
0204  *               release.
0205  *
0206  * This is the same as btrfs_block_rsv_release, except that it handles the
0207  * tracepoint for the reservation.
0208  */
0209 static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
0210 {
0211     struct btrfs_fs_info *fs_info = inode->root->fs_info;
0212     struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
0213     u64 released = 0;
0214     u64 qgroup_to_release = 0;
0215 
0216     /*
0217      * Since we statically set the block_rsv->size we just want to say we
0218      * are releasing 0 bytes, and then we'll just get the reservation over
0219      * the size free'd.
0220      */
0221     released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
0222                        &qgroup_to_release);
0223     if (released > 0)
0224         trace_btrfs_space_reservation(fs_info, "delalloc",
0225                           btrfs_ino(inode), released, 0);
0226     if (qgroup_free)
0227         btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
0228     else
0229         btrfs_qgroup_convert_reserved_meta(inode->root,
0230                            qgroup_to_release);
0231 }
0232 
0233 static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
0234                          struct btrfs_inode *inode)
0235 {
0236     struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
0237     u64 reserve_size = 0;
0238     u64 qgroup_rsv_size = 0;
0239     u64 csum_leaves;
0240     unsigned outstanding_extents;
0241 
0242     lockdep_assert_held(&inode->lock);
0243     outstanding_extents = inode->outstanding_extents;
0244 
0245     /*
0246      * Insert size for the number of outstanding extents, 1 normal size for
0247      * updating the inode.
0248      */
0249     if (outstanding_extents) {
0250         reserve_size = btrfs_calc_insert_metadata_size(fs_info,
0251                         outstanding_extents);
0252         reserve_size += btrfs_calc_metadata_size(fs_info, 1);
0253     }
0254     csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
0255                          inode->csum_bytes);
0256     reserve_size += btrfs_calc_insert_metadata_size(fs_info,
0257                             csum_leaves);
0258     /*
0259      * For qgroup rsv, the calculation is very simple:
0260      * account one nodesize for each outstanding extent
0261      *
0262      * This is overestimating in most cases.
0263      */
0264     qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
0265 
0266     spin_lock(&block_rsv->lock);
0267     block_rsv->size = reserve_size;
0268     block_rsv->qgroup_rsv_size = qgroup_rsv_size;
0269     spin_unlock(&block_rsv->lock);
0270 }
0271 
0272 static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
0273                     u64 num_bytes, u64 disk_num_bytes,
0274                     u64 *meta_reserve, u64 *qgroup_reserve)
0275 {
0276     u64 nr_extents = count_max_extents(fs_info, num_bytes);
0277     u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes);
0278     u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
0279 
0280     *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
0281                         nr_extents + csum_leaves);
0282 
0283     /*
0284      * finish_ordered_io has to update the inode, so add the space required
0285      * for an inode update.
0286      */
0287     *meta_reserve += inode_update;
0288     *qgroup_reserve = nr_extents * fs_info->nodesize;
0289 }
0290 
0291 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
0292                     u64 disk_num_bytes, bool noflush)
0293 {
0294     struct btrfs_root *root = inode->root;
0295     struct btrfs_fs_info *fs_info = root->fs_info;
0296     struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
0297     u64 meta_reserve, qgroup_reserve;
0298     unsigned nr_extents;
0299     enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
0300     int ret = 0;
0301 
0302     /*
0303      * If we are a free space inode we need to not flush since we will be in
0304      * the middle of a transaction commit.  We also don't need the delalloc
0305      * mutex since we won't race with anybody.  We need this mostly to make
0306      * lockdep shut its filthy mouth.
0307      *
0308      * If we have a transaction open (can happen if we call truncate_block
0309      * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
0310      */
0311     if (noflush || btrfs_is_free_space_inode(inode)) {
0312         flush = BTRFS_RESERVE_NO_FLUSH;
0313     } else {
0314         if (current->journal_info)
0315             flush = BTRFS_RESERVE_FLUSH_LIMIT;
0316 
0317         if (btrfs_transaction_in_commit(fs_info))
0318             schedule_timeout(1);
0319     }
0320 
0321     num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
0322     disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
0323 
0324     /*
0325      * We always want to do it this way, every other way is wrong and ends
0326      * in tears.  Pre-reserving the amount we are going to add will always
0327      * be the right way, because otherwise if we have enough parallelism we
0328      * could end up with thousands of inodes all holding little bits of
0329      * reservations they were able to make previously and the only way to
0330      * reclaim that space is to ENOSPC out the operations and clear
0331      * everything out and try again, which is bad.  This way we just
0332      * over-reserve slightly, and clean up the mess when we are done.
0333      */
0334     calc_inode_reservations(fs_info, num_bytes, disk_num_bytes,
0335                 &meta_reserve, &qgroup_reserve);
0336     ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
0337                          noflush);
0338     if (ret)
0339         return ret;
0340     ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, meta_reserve, flush);
0341     if (ret) {
0342         btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
0343         return ret;
0344     }
0345 
0346     /*
0347      * Now we need to update our outstanding extents and csum bytes _first_
0348      * and then add the reservation to the block_rsv.  This keeps us from
0349      * racing with an ordered completion or some such that would think it
0350      * needs to free the reservation we just made.
0351      */
0352     spin_lock(&inode->lock);
0353     nr_extents = count_max_extents(fs_info, num_bytes);
0354     btrfs_mod_outstanding_extents(inode, nr_extents);
0355     inode->csum_bytes += disk_num_bytes;
0356     btrfs_calculate_inode_block_rsv_size(fs_info, inode);
0357     spin_unlock(&inode->lock);
0358 
0359     /* Now we can safely add our space to our block rsv */
0360     btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
0361     trace_btrfs_space_reservation(root->fs_info, "delalloc",
0362                       btrfs_ino(inode), meta_reserve, 1);
0363 
0364     spin_lock(&block_rsv->lock);
0365     block_rsv->qgroup_rsv_reserved += qgroup_reserve;
0366     spin_unlock(&block_rsv->lock);
0367 
0368     return 0;
0369 }
0370 
0371 /**
0372  * Release a metadata reservation for an inode
0373  *
0374  * @inode: the inode to release the reservation for.
0375  * @num_bytes: the number of bytes we are releasing.
0376  * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
0377  *
0378  * This will release the metadata reservation for an inode.  This can be called
0379  * once we complete IO for a given set of bytes to release their metadata
0380  * reservations, or on error for the same reason.
0381  */
0382 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
0383                      bool qgroup_free)
0384 {
0385     struct btrfs_fs_info *fs_info = inode->root->fs_info;
0386 
0387     num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
0388     spin_lock(&inode->lock);
0389     inode->csum_bytes -= num_bytes;
0390     btrfs_calculate_inode_block_rsv_size(fs_info, inode);
0391     spin_unlock(&inode->lock);
0392 
0393     if (btrfs_is_testing(fs_info))
0394         return;
0395 
0396     btrfs_inode_rsv_release(inode, qgroup_free);
0397 }
0398 
0399 /**
0400  * btrfs_delalloc_release_extents - release our outstanding_extents
0401  * @inode: the inode to balance the reservation for.
0402  * @num_bytes: the number of bytes we originally reserved with
0403  *
0404  * When we reserve space we increase outstanding_extents for the extents we may
0405  * add.  Once we've set the range as delalloc or created our ordered extents we
0406  * have outstanding_extents to track the real usage, so we use this to free our
0407  * temporarily tracked outstanding_extents.  This _must_ be used in conjunction
0408  * with btrfs_delalloc_reserve_metadata.
0409  */
0410 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
0411 {
0412     struct btrfs_fs_info *fs_info = inode->root->fs_info;
0413     unsigned num_extents;
0414 
0415     spin_lock(&inode->lock);
0416     num_extents = count_max_extents(fs_info, num_bytes);
0417     btrfs_mod_outstanding_extents(inode, -num_extents);
0418     btrfs_calculate_inode_block_rsv_size(fs_info, inode);
0419     spin_unlock(&inode->lock);
0420 
0421     if (btrfs_is_testing(fs_info))
0422         return;
0423 
0424     btrfs_inode_rsv_release(inode, true);
0425 }
0426 
0427 /**
0428  * btrfs_delalloc_reserve_space - reserve data and metadata space for
0429  * delalloc
0430  * @inode: inode we're writing to
0431  * @start: start range we are writing to
0432  * @len: how long the range we are writing to
0433  * @reserved: mandatory parameter, record actually reserved qgroup ranges of
0434  *        current reservation.
0435  *
0436  * This will do the following things
0437  *
0438  * - reserve space in data space info for num bytes
0439  *   and reserve precious corresponding qgroup space
0440  *   (Done in check_data_free_space)
0441  *
0442  * - reserve space for metadata space, based on the number of outstanding
0443  *   extents and how much csums will be needed
0444  *   also reserve metadata space in a per root over-reserve method.
0445  * - add to the inodes->delalloc_bytes
0446  * - add it to the fs_info's delalloc inodes list.
0447  *   (Above 3 all done in delalloc_reserve_metadata)
0448  *
0449  * Return 0 for success
0450  * Return <0 for error(-ENOSPC or -EQUOT)
0451  */
0452 int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
0453             struct extent_changeset **reserved, u64 start, u64 len)
0454 {
0455     int ret;
0456 
0457     ret = btrfs_check_data_free_space(inode, reserved, start, len);
0458     if (ret < 0)
0459         return ret;
0460     ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
0461     if (ret < 0) {
0462         btrfs_free_reserved_data_space(inode, *reserved, start, len);
0463         extent_changeset_free(*reserved);
0464         *reserved = NULL;
0465     }
0466     return ret;
0467 }
0468 
0469 /**
0470  * Release data and metadata space for delalloc
0471  *
0472  * @inode:       inode we're releasing space for
0473  * @reserved:    list of changed/reserved ranges
0474  * @start:       start position of the space already reserved
0475  * @len:         length of the space already reserved
0476  * @qgroup_free: should qgroup reserved-space also be freed
0477  *
0478  * This function will release the metadata space that was not used and will
0479  * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
0480  * list if there are no delalloc bytes left.
0481  * Also it will handle the qgroup reserved space.
0482  */
0483 void btrfs_delalloc_release_space(struct btrfs_inode *inode,
0484                   struct extent_changeset *reserved,
0485                   u64 start, u64 len, bool qgroup_free)
0486 {
0487     btrfs_delalloc_release_metadata(inode, len, qgroup_free);
0488     btrfs_free_reserved_data_space(inode, reserved, start, len);
0489 }