LCOV - code coverage report
Current view: top level - fs/xfs - xfs_discard.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwx @ Mon Jul 31 20:08:22 PDT 2023 Lines: 81 83 97.6 %
Date: 2023-07-31 20:08:22 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (C) 2010 Red Hat, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_shared.h"
       8             : #include "xfs_format.h"
       9             : #include "xfs_log_format.h"
      10             : #include "xfs_trans_resv.h"
      11             : #include "xfs_mount.h"
      12             : #include "xfs_btree.h"
      13             : #include "xfs_alloc_btree.h"
      14             : #include "xfs_alloc.h"
      15             : #include "xfs_discard.h"
      16             : #include "xfs_error.h"
      17             : #include "xfs_extent_busy.h"
      18             : #include "xfs_trace.h"
      19             : #include "xfs_log.h"
      20             : #include "xfs_ag.h"
      21             : 
      22             : STATIC int
      23       31710 : xfs_trim_extents(
      24             :         struct xfs_perag        *pag,
      25             :         xfs_daddr_t             start,
      26             :         xfs_daddr_t             end,
      27             :         xfs_daddr_t             minlen,
      28             :         uint64_t                *blocks_trimmed)
      29             : {
      30       31710 :         struct xfs_mount        *mp = pag->pag_mount;
      31       31710 :         struct block_device     *bdev = mp->m_ddev_targp->bt_bdev;
      32       31710 :         struct xfs_btree_cur    *cur;
      33       31710 :         struct xfs_buf          *agbp;
      34       31710 :         struct xfs_agf          *agf;
      35       31710 :         int                     error;
      36       31710 :         int                     i;
      37             : 
      38             :         /*
      39             :          * Force out the log.  This means any transactions that might have freed
      40             :          * space before we take the AGF buffer lock are now on disk, and the
      41             :          * volatile disk cache is flushed.
      42             :          */
      43       31710 :         xfs_log_force(mp, XFS_LOG_SYNC);
      44             : 
      45       31710 :         error = xfs_alloc_read_agf(pag, NULL, 0, &agbp);
      46       31710 :         if (error)
      47             :                 return error;
      48       31710 :         agf = agbp->b_addr;
      49             : 
      50       31710 :         cur = xfs_allocbt_init_cursor(mp, NULL, agbp, pag, XFS_BTNUM_CNT);
      51             : 
      52             :         /*
      53             :          * Look up the longest btree in the AGF and start with it.
      54             :          */
      55       31710 :         error = xfs_alloc_lookup_ge(cur, 0, be32_to_cpu(agf->agf_longest), &i);
      56       31709 :         if (error)
      57           0 :                 goto out_del_cursor;
      58             : 
      59             :         /*
      60             :          * Loop until we are done with all extents that are large
      61             :          * enough to be worth discarding.
      62             :          */
      63      268111 :         while (i) {
      64      254968 :                 xfs_agblock_t   fbno;
      65      254968 :                 xfs_extlen_t    flen;
      66      254968 :                 xfs_daddr_t     dbno;
      67      254968 :                 xfs_extlen_t    dlen;
      68             : 
      69      254968 :                 error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
      70      254967 :                 if (error)
      71             :                         break;
      72      254967 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
      73             :                         error = -EFSCORRUPTED;
      74             :                         break;
      75             :                 }
      76      254967 :                 ASSERT(flen <= be32_to_cpu(agf->agf_longest));
      77             : 
      78             :                 /*
      79             :                  * use daddr format for all range/len calculations as that is
      80             :                  * the format the range/len variables are supplied in by
      81             :                  * userspace.
      82             :                  */
      83      254967 :                 dbno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, fbno);
      84      254967 :                 dlen = XFS_FSB_TO_BB(mp, flen);
      85             : 
      86             :                 /*
      87             :                  * Too small?  Give up.
      88             :                  */
      89      254967 :                 if (dlen < minlen) {
      90       18556 :                         trace_xfs_discard_toosmall(mp, pag->pag_agno, fbno, flen);
      91       18556 :                         break;
      92             :                 }
      93             : 
      94             :                 /*
      95             :                  * If the extent is entirely outside of the range we are
      96             :                  * supposed to discard skip it.  Do not bother to trim
      97             :                  * down partially overlapping ranges for now.
      98             :                  */
      99      236411 :                 if (dbno + dlen < start || dbno > end) {
     100         120 :                         trace_xfs_discard_exclude(mp, pag->pag_agno, fbno, flen);
     101         120 :                         goto next_extent;
     102             :                 }
     103             : 
     104             :                 /*
     105             :                  * If any blocks in the range are still busy, skip the
     106             :                  * discard and try again the next time.
     107             :                  */
     108      236291 :                 if (xfs_extent_busy_search(mp, pag, fbno, flen)) {
     109         497 :                         trace_xfs_discard_busy(mp, pag->pag_agno, fbno, flen);
     110         497 :                         goto next_extent;
     111             :                 }
     112             : 
     113      235794 :                 trace_xfs_discard_extent(mp, pag->pag_agno, fbno, flen);
     114      235794 :                 error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS);
     115      235796 :                 if (error)
     116             :                         break;
     117      235796 :                 *blocks_trimmed += flen;
     118             : 
     119      236413 : next_extent:
     120      236413 :                 error = xfs_btree_decrement(cur, 0, &i);
     121      236413 :                 if (error)
     122             :                         break;
     123             : 
     124      236413 :                 if (fatal_signal_pending(current)) {
     125             :                         error = -ERESTARTSYS;
     126             :                         break;
     127             :                 }
     128             :         }
     129             : 
     130       13143 : out_del_cursor:
     131       31710 :         xfs_btree_del_cursor(cur, error);
     132       31710 :         xfs_buf_relse(agbp);
     133       31710 :         return error;
     134             : }
     135             : 
     136             : /*
     137             :  * trim a range of the filesystem.
     138             :  *
     139             :  * Note: the parameters passed from userspace are byte ranges into the
     140             :  * filesystem which does not match to the format we use for filesystem block
     141             :  * addressing. FSB addressing is sparse (AGNO|AGBNO), while the incoming format
     142             :  * is a linear address range. Hence we need to use DADDR based conversions and
     143             :  * comparisons for determining the correct offset and regions to trim.
     144             :  */
     145             : int
     146        6131 : xfs_ioc_trim(
     147             :         struct xfs_mount                *mp,
     148             :         struct fstrim_range __user      *urange)
     149             : {
     150        6131 :         struct xfs_perag        *pag;
     151        6131 :         unsigned int            granularity =
     152        6131 :                 bdev_discard_granularity(mp->m_ddev_targp->bt_bdev);
     153        6131 :         struct fstrim_range     range;
     154        6131 :         xfs_daddr_t             start, end, minlen;
     155        6131 :         xfs_agnumber_t          agno;
     156        6131 :         uint64_t                blocks_trimmed = 0;
     157        6131 :         int                     error, last_error = 0;
     158             : 
     159        6131 :         if (!capable(CAP_SYS_ADMIN))
     160             :                 return -EPERM;
     161        6131 :         if (!bdev_max_discard_sectors(mp->m_ddev_targp->bt_bdev))
     162             :                 return -EOPNOTSUPP;
     163             : 
     164             :         /*
     165             :          * We haven't recovered the log, so we cannot use our bnobt-guided
     166             :          * storage zapping commands.
     167             :          */
     168        6131 :         if (xfs_has_norecovery(mp))
     169             :                 return -EROFS;
     170             : 
     171        6120 :         if (copy_from_user(&range, urange, sizeof(range)))
     172             :                 return -EFAULT;
     173             : 
     174        6120 :         range.minlen = max_t(u64, granularity, range.minlen);
     175        6120 :         minlen = BTOBB(range.minlen);
     176             :         /*
     177             :          * Truncating down the len isn't actually quite correct, but using
     178             :          * BBTOB would mean we trivially get overflows for values
     179             :          * of ULLONG_MAX or slightly lower.  And ULLONG_MAX is the default
     180             :          * used by the fstrim application.  In the end it really doesn't
     181             :          * matter as trimming blocks is an advisory interface.
     182             :          */
     183        6120 :         if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
     184        6055 :             range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
     185        6055 :             range.len < mp->m_sb.sb_blocksize)
     186          87 :                 return -EINVAL;
     187             : 
     188        6033 :         start = BTOBB(range.start);
     189        6033 :         end = start + BTOBBT(range.len) - 1;
     190             : 
     191        6033 :         if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
     192        4124 :                 end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1;
     193             : 
     194        6033 :         agno = xfs_daddr_to_agno(mp, start);
     195       37732 :         for_each_perag_range(mp, agno, xfs_daddr_to_agno(mp, end), pag) {
     196       31710 :                 error = xfs_trim_extents(pag, start, end, minlen,
     197             :                                           &blocks_trimmed);
     198       31710 :                 if (error) {
     199          11 :                         last_error = error;
     200          11 :                         if (error == -ERESTARTSYS) {
     201          11 :                                 xfs_perag_rele(pag);
     202          11 :                                 break;
     203             :                         }
     204             :                 }
     205             :         }
     206             : 
     207        6033 :         if (last_error)
     208             :                 return last_error;
     209             : 
     210        6022 :         range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
     211        6022 :         if (copy_to_user(urange, &range, sizeof(range)))
     212           0 :                 return -EFAULT;
     213             :         return 0;
     214             : }

Generated by: LCOV version 1.14