1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_defer.h"
13 #include "xfs_btree.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
18 #include "xfs_inode.h"
19 #include "xfs_inode_fork.h"
20 #include "xfs_alloc.h"
22 #include "xfs_quota.h"
24 #include "xfs_dquot.h"
25 #include "xfs_dquot_item.h"
26 #include "xfs_reflink.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_trans_space.h"
29 #include "scrub/xfs_scrub.h"
30 #include "scrub/scrub.h"
31 #include "scrub/common.h"
32 #include "scrub/quota.h"
33 #include "scrub/trace.h"
34 #include "scrub/repair.h"
40 * Quota repairs are fairly simplistic; we fix everything that the dquot
41 * verifiers complain about, cap any counters or limits that make no sense,
42 * and schedule a quotacheck if we had to fix anything. We also repair any
43 * data fork extent records that don't apply to metadata files.
46 struct xrep_quota_info
{
52 * Allocate a new block into a sparse hole in the quota file backing this
53 * dquot, initialize the block, and commit the whole mess.
56 xrep_quota_item_fill_bmap_hole(
59 struct xfs_bmbt_irec
*irec
)
62 struct xfs_mount
*mp
= sc
->mp
;
66 xfs_trans_ijoin(sc
->tp
, sc
->ip
, 0);
68 /* Map a block into the file. */
69 error
= xfs_trans_reserve_more(sc
->tp
, XFS_QM_DQALLOC_SPACE_RES(mp
),
74 error
= xfs_bmapi_write(sc
->tp
, sc
->ip
, dq
->q_fileoffset
,
75 XFS_DQUOT_CLUSTER_SIZE_FSB
, XFS_BMAPI_METADATA
, 0,
80 dq
->q_blkno
= XFS_FSB_TO_DADDR(mp
, irec
->br_startblock
);
82 trace_xrep_dquot_item_fill_bmap_hole(sc
->mp
, dq
->q_type
, dq
->q_id
);
84 /* Initialize the new block. */
85 error
= xfs_trans_get_buf(sc
->tp
, mp
->m_ddev_targp
, dq
->q_blkno
,
86 mp
->m_quotainfo
->qi_dqchunklen
, 0, &bp
);
89 bp
->b_ops
= &xfs_dquot_buf_ops
;
91 xfs_qm_init_dquot_blk(sc
->tp
, dq
->q_id
, dq
->q_type
, bp
);
92 xfs_buf_set_ref(bp
, XFS_DQUOT_REF
);
95 * Finish the mapping transactions and roll one more time to
96 * disconnect sc->ip from sc->tp.
98 error
= xrep_defer_finish(sc
);
101 return xfs_trans_roll(&sc
->tp
);
104 /* Make sure there's a written block backing this dquot */
106 xrep_quota_item_bmap(
107 struct xfs_scrub
*sc
,
108 struct xfs_dquot
*dq
,
111 struct xfs_bmbt_irec irec
;
112 struct xfs_mount
*mp
= sc
->mp
;
113 struct xfs_quotainfo
*qi
= mp
->m_quotainfo
;
114 xfs_fileoff_t offset
= dq
->q_id
/ qi
->qi_dqperchunk
;
118 /* The computed file offset should always be valid. */
119 if (!xfs_verify_fileoff(mp
, offset
)) {
120 ASSERT(xfs_verify_fileoff(mp
, offset
));
121 return -EFSCORRUPTED
;
123 dq
->q_fileoffset
= offset
;
125 error
= xfs_bmapi_read(sc
->ip
, offset
, 1, &irec
, &nmaps
, 0);
129 if (nmaps
< 1 || !xfs_bmap_is_real_extent(&irec
)) {
130 /* Hole/delalloc extent; allocate a real block. */
131 error
= xrep_quota_item_fill_bmap_hole(sc
, dq
, &irec
);
134 } else if (irec
.br_state
!= XFS_EXT_NORM
) {
135 /* Unwritten extent, which we already took care of? */
136 ASSERT(irec
.br_state
== XFS_EXT_NORM
);
137 return -EFSCORRUPTED
;
138 } else if (dq
->q_blkno
!= XFS_FSB_TO_DADDR(mp
, irec
.br_startblock
)) {
140 * If the cached daddr is incorrect, repair probably punched a
141 * hole out of the quota file and filled it back in with a new
142 * block. Update the block mapping in the dquot.
144 dq
->q_blkno
= XFS_FSB_TO_DADDR(mp
, irec
.br_startblock
);
151 /* Reset quota timers if incorrectly set. */
153 xrep_quota_item_timer(
154 struct xfs_scrub
*sc
,
155 const struct xfs_dquot_res
*res
,
158 if ((res
->softlimit
&& res
->count
> res
->softlimit
) ||
159 (res
->hardlimit
&& res
->count
> res
->hardlimit
)) {
168 /* Scrub the fields in an individual quota item. */
171 struct xrep_quota_info
*rqi
,
172 struct xfs_dquot
*dq
)
174 struct xfs_scrub
*sc
= rqi
->sc
;
175 struct xfs_mount
*mp
= sc
->mp
;
180 /* Last chance to abort before we start committing fixes. */
181 if (xchk_should_terminate(sc
, &error
))
185 * We might need to fix holes in the bmap record for the storage
186 * backing this dquot, so we need to lock the dquot and the quota file.
187 * dqiterate gave us a locked dquot, so drop the dquot lock to get the
191 xchk_ilock(sc
, XFS_ILOCK_EXCL
);
194 error
= xrep_quota_item_bmap(sc
, dq
, &dirty
);
195 xchk_iunlock(sc
, XFS_ILOCK_EXCL
);
199 /* Check the limits. */
200 if (dq
->q_blk
.softlimit
> dq
->q_blk
.hardlimit
) {
201 dq
->q_blk
.softlimit
= dq
->q_blk
.hardlimit
;
205 if (dq
->q_ino
.softlimit
> dq
->q_ino
.hardlimit
) {
206 dq
->q_ino
.softlimit
= dq
->q_ino
.hardlimit
;
210 if (dq
->q_rtb
.softlimit
> dq
->q_rtb
.hardlimit
) {
211 dq
->q_rtb
.softlimit
= dq
->q_rtb
.hardlimit
;
216 * Check that usage doesn't exceed physical limits. However, on
217 * a reflink filesystem we're allowed to exceed physical space
218 * if there are no quota limits. We don't know what the real number
219 * is, but we can make quotacheck find out for us.
221 if (!xfs_has_reflink(mp
) && dq
->q_blk
.count
> mp
->m_sb
.sb_dblocks
) {
222 dq
->q_blk
.reserved
-= dq
->q_blk
.count
;
223 dq
->q_blk
.reserved
+= mp
->m_sb
.sb_dblocks
;
224 dq
->q_blk
.count
= mp
->m_sb
.sb_dblocks
;
225 rqi
->need_quotacheck
= true;
228 fs_icount
= percpu_counter_sum(&mp
->m_icount
);
229 if (dq
->q_ino
.count
> fs_icount
) {
230 dq
->q_ino
.reserved
-= dq
->q_ino
.count
;
231 dq
->q_ino
.reserved
+= fs_icount
;
232 dq
->q_ino
.count
= fs_icount
;
233 rqi
->need_quotacheck
= true;
236 if (dq
->q_rtb
.count
> mp
->m_sb
.sb_rblocks
) {
237 dq
->q_rtb
.reserved
-= dq
->q_rtb
.count
;
238 dq
->q_rtb
.reserved
+= mp
->m_sb
.sb_rblocks
;
239 dq
->q_rtb
.count
= mp
->m_sb
.sb_rblocks
;
240 rqi
->need_quotacheck
= true;
244 xrep_quota_item_timer(sc
, &dq
->q_blk
, &dirty
);
245 xrep_quota_item_timer(sc
, &dq
->q_ino
, &dirty
);
246 xrep_quota_item_timer(sc
, &dq
->q_rtb
, &dirty
);
251 trace_xrep_dquot_item(sc
->mp
, dq
->q_type
, dq
->q_id
);
253 dq
->q_flags
|= XFS_DQFLAG_DIRTY
;
254 xfs_trans_dqjoin(sc
->tp
, dq
);
256 xfs_qm_adjust_dqlimits(dq
);
257 xfs_qm_adjust_dqtimers(dq
);
259 xfs_trans_log_dquot(sc
->tp
, dq
);
260 error
= xfs_trans_roll(&sc
->tp
);
265 /* Fix a quota timer so that we can pass the verifier. */
267 xrep_quota_fix_timer(
268 struct xfs_mount
*mp
,
269 const struct xfs_disk_dquot
*ddq
,
275 uint64_t soft
= be64_to_cpu(softlimit
);
276 uint64_t count
= be64_to_cpu(countnow
);
280 if (!soft
|| count
<= soft
|| *timer
!= 0)
283 new_timer
= xfs_dquot_set_timeout(mp
,
284 ktime_get_real_seconds() + timelimit
);
285 if (ddq
->d_type
& XFS_DQTYPE_BIGTIME
)
286 t
= xfs_dq_unix_to_bigtime(new_timer
);
290 *timer
= cpu_to_be32(t
);
293 /* Fix anything the verifiers complain about. */
296 struct xfs_scrub
*sc
,
301 struct xfs_dqblk
*dqblk
;
302 struct xfs_disk_dquot
*ddq
;
303 struct xfs_quotainfo
*qi
= sc
->mp
->m_quotainfo
;
304 struct xfs_def_quota
*defq
= xfs_get_defquota(qi
, dqtype
);
305 struct xfs_buf
*bp
= NULL
;
306 enum xfs_blft buftype
= 0;
310 error
= xfs_trans_read_buf(sc
->mp
, sc
->tp
, sc
->mp
->m_ddev_targp
, daddr
,
311 qi
->qi_dqchunklen
, 0, &bp
, &xfs_dquot_buf_ops
);
315 /* Failed verifier, retry read with no ops. */
316 error
= xfs_trans_read_buf(sc
->mp
, sc
->tp
,
317 sc
->mp
->m_ddev_targp
, daddr
, qi
->qi_dqchunklen
,
324 ddq
= &dqblk
[0].dd_diskdq
;
327 * If there's nothing that would impede a dqiterate, we're
330 if ((ddq
->d_type
& XFS_DQTYPE_REC_MASK
) != dqtype
||
331 id
== be32_to_cpu(ddq
->d_id
)) {
332 xfs_trans_brelse(sc
->tp
, bp
);
340 /* Something's wrong with the block, fix the whole thing. */
342 bp
->b_ops
= &xfs_dquot_buf_ops
;
343 for (i
= 0; i
< qi
->qi_dqperchunk
; i
++, dqblk
++) {
344 ddq
= &dqblk
->dd_diskdq
;
346 trace_xrep_disk_dquot(sc
->mp
, dqtype
, id
+ i
);
348 ddq
->d_magic
= cpu_to_be16(XFS_DQUOT_MAGIC
);
349 ddq
->d_version
= XFS_DQUOT_VERSION
;
350 ddq
->d_type
= dqtype
;
351 ddq
->d_id
= cpu_to_be32(id
+ i
);
353 if (xfs_has_bigtime(sc
->mp
) && ddq
->d_id
)
354 ddq
->d_type
|= XFS_DQTYPE_BIGTIME
;
356 xrep_quota_fix_timer(sc
->mp
, ddq
, ddq
->d_blk_softlimit
,
357 ddq
->d_bcount
, &ddq
->d_btimer
,
360 xrep_quota_fix_timer(sc
->mp
, ddq
, ddq
->d_ino_softlimit
,
361 ddq
->d_icount
, &ddq
->d_itimer
,
364 xrep_quota_fix_timer(sc
->mp
, ddq
, ddq
->d_rtb_softlimit
,
365 ddq
->d_rtbcount
, &ddq
->d_rtbtimer
,
368 /* We only support v5 filesystems so always set these. */
369 uuid_copy(&dqblk
->dd_uuid
, &sc
->mp
->m_sb
.sb_meta_uuid
);
370 xfs_update_cksum((char *)dqblk
, sizeof(struct xfs_dqblk
),
375 case XFS_DQTYPE_USER
:
376 buftype
= XFS_BLFT_UDQUOT_BUF
;
378 case XFS_DQTYPE_GROUP
:
379 buftype
= XFS_BLFT_GDQUOT_BUF
;
381 case XFS_DQTYPE_PROJ
:
382 buftype
= XFS_BLFT_PDQUOT_BUF
;
385 xfs_trans_buf_set_type(sc
->tp
, bp
, buftype
);
386 xfs_trans_log_buf(sc
->tp
, bp
, 0, BBTOB(bp
->b_length
) - 1);
387 return xrep_roll_trans(sc
);
391 * Repair a quota file's data fork. The function returns with the inode
395 xrep_quota_data_fork(
396 struct xfs_scrub
*sc
,
399 struct xfs_bmbt_irec irec
= { 0 };
400 struct xfs_iext_cursor icur
;
401 struct xfs_quotainfo
*qi
= sc
->mp
->m_quotainfo
;
402 struct xfs_ifork
*ifp
;
403 xfs_fileoff_t max_dqid_off
;
406 bool truncate
= false;
410 error
= xrep_metadata_inode_forks(sc
);
414 /* Check for data fork problems that apply only to quota files. */
415 max_dqid_off
= XFS_DQ_ID_MAX
/ qi
->qi_dqperchunk
;
416 ifp
= xfs_ifork_ptr(sc
->ip
, XFS_DATA_FORK
);
417 for_each_xfs_iext(ifp
, &icur
, &irec
) {
418 if (isnullstartblock(irec
.br_startblock
)) {
419 error
= -EFSCORRUPTED
;
423 if (irec
.br_startoff
> max_dqid_off
||
424 irec
.br_startoff
+ irec
.br_blockcount
- 1 > max_dqid_off
) {
429 /* Convert unwritten extents to real ones. */
430 if (irec
.br_state
== XFS_EXT_UNWRITTEN
) {
431 struct xfs_bmbt_irec nrec
;
435 xfs_trans_ijoin(sc
->tp
, sc
->ip
, 0);
439 error
= xfs_bmapi_write(sc
->tp
, sc
->ip
,
440 irec
.br_startoff
, irec
.br_blockcount
,
441 XFS_BMAPI_CONVERT
, 0, &nrec
, &nmap
);
444 ASSERT(nrec
.br_startoff
== irec
.br_startoff
);
445 ASSERT(nrec
.br_blockcount
== irec
.br_blockcount
);
447 error
= xfs_defer_finish(&sc
->tp
);
454 xfs_trans_ijoin(sc
->tp
, sc
->ip
, 0);
459 /* Erase everything after the block containing the max dquot */
460 error
= xfs_bunmapi_range(&sc
->tp
, sc
->ip
, 0,
461 max_dqid_off
* sc
->mp
->m_sb
.sb_blocksize
,
466 /* Remove all CoW reservations. */
467 error
= xfs_reflink_cancel_cow_blocks(sc
->ip
, &sc
->tp
, 0,
468 XFS_MAX_FILEOFF
, true);
471 sc
->ip
->i_diflags2
&= ~XFS_DIFLAG2_REFLINK
;
474 * Always re-log the inode so that our permanent transaction
475 * can keep on rolling it forward in the log.
477 xfs_trans_log_inode(sc
->tp
, sc
->ip
, XFS_ILOG_CORE
);
480 /* Now go fix anything that fails the verifiers. */
481 for_each_xfs_iext(ifp
, &icur
, &irec
) {
482 for (fsbno
= irec
.br_startblock
, off
= irec
.br_startoff
;
483 fsbno
< irec
.br_startblock
+ irec
.br_blockcount
;
484 fsbno
+= XFS_DQUOT_CLUSTER_SIZE_FSB
,
485 off
+= XFS_DQUOT_CLUSTER_SIZE_FSB
) {
486 error
= xrep_quota_block(sc
,
487 XFS_FSB_TO_DADDR(sc
->mp
, fsbno
),
488 dqtype
, off
* qi
->qi_dqperchunk
);
499 * Go fix anything in the quota items that we could have been mad about. Now
500 * that we've checked the quota inode data fork we have to drop ILOCK_EXCL to
501 * use the regular dquot functions.
505 struct xfs_scrub
*sc
,
508 struct xchk_dqiter cursor
= { };
509 struct xrep_quota_info rqi
= { .sc
= sc
};
510 struct xfs_dquot
*dq
;
513 xchk_dqiter_init(&cursor
, sc
, dqtype
);
514 while ((error
= xchk_dquot_iter(&cursor
, &dq
)) == 1) {
515 error
= xrep_quota_item(&rqi
, dq
);
523 /* Make a quotacheck happen. */
524 if (rqi
.need_quotacheck
)
525 xrep_force_quotacheck(sc
, dqtype
);
529 /* Repair all of a quota type's items. */
532 struct xfs_scrub
*sc
)
537 dqtype
= xchk_quota_to_dqtype(sc
);
540 * Re-take the ILOCK so that we can fix any problems that we found
541 * with the data fork mappings, or with the dquot bufs themselves.
543 if (!(sc
->ilock_flags
& XFS_ILOCK_EXCL
))
544 xchk_ilock(sc
, XFS_ILOCK_EXCL
);
545 error
= xrep_quota_data_fork(sc
, dqtype
);
550 * Finish deferred items and roll the transaction to unjoin the quota
551 * inode from transaction so that we can unlock the quota inode; we
552 * play only with dquots from now on.
554 error
= xrep_defer_finish(sc
);
557 error
= xfs_trans_roll(&sc
->tp
);
560 xchk_iunlock(sc
, sc
->ilock_flags
);
562 /* Fix anything the dquot verifiers don't complain about. */
563 error
= xrep_quota_problems(sc
, dqtype
);
567 return xrep_trans_commit(sc
);