db2cb5e4197b90a420715fb776766ca7fd7a014f
[platform/kernel/linux-rpi.git] / fs / xfs / xfs_dquot_item_recover.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_quota.h"
15 #include "xfs_trans.h"
16 #include "xfs_buf_item.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_qm.h"
19 #include "xfs_log.h"
20 #include "xfs_log_priv.h"
21 #include "xfs_log_recover.h"
22
23 STATIC void
24 xlog_recover_dquot_ra_pass2(
25         struct xlog                     *log,
26         struct xlog_recover_item        *item)
27 {
28         struct xfs_mount        *mp = log->l_mp;
29         struct xfs_disk_dquot   *recddq;
30         struct xfs_dq_logformat *dq_f;
31         uint                    type;
32
33         if (mp->m_qflags == 0)
34                 return;
35
36         recddq = item->ri_buf[1].i_addr;
37         if (recddq == NULL)
38                 return;
39         if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
40                 return;
41
42         type = recddq->d_type & XFS_DQTYPE_REC_MASK;
43         ASSERT(type);
44         if (log->l_quotaoffs_flag & type)
45                 return;
46
47         dq_f = item->ri_buf[0].i_addr;
48         ASSERT(dq_f);
49         ASSERT(dq_f->qlf_len == 1);
50
51         xlog_buf_readahead(log, dq_f->qlf_blkno,
52                         XFS_FSB_TO_BB(mp, dq_f->qlf_len),
53                         &xfs_dquot_buf_ra_ops);
54 }
55
56 /*
57  * Recover a dquot record
58  */
59 STATIC int
60 xlog_recover_dquot_commit_pass2(
61         struct xlog                     *log,
62         struct list_head                *buffer_list,
63         struct xlog_recover_item        *item,
64         xfs_lsn_t                       current_lsn)
65 {
66         struct xfs_mount                *mp = log->l_mp;
67         struct xfs_buf                  *bp;
68         struct xfs_dqblk                *dqb;
69         struct xfs_disk_dquot           *ddq, *recddq;
70         struct xfs_dq_logformat         *dq_f;
71         xfs_failaddr_t                  fa;
72         int                             error;
73         uint                            type;
74
75         /*
76          * Filesystems are required to send in quota flags at mount time.
77          */
78         if (mp->m_qflags == 0)
79                 return 0;
80
81         recddq = item->ri_buf[1].i_addr;
82         if (recddq == NULL) {
83                 xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
84                 return -EFSCORRUPTED;
85         }
86         if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot)) {
87                 xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
88                         item->ri_buf[1].i_len, __func__);
89                 return -EFSCORRUPTED;
90         }
91
92         /*
93          * This type of quotas was turned off, so ignore this record.
94          */
95         type = recddq->d_type & XFS_DQTYPE_REC_MASK;
96         ASSERT(type);
97         if (log->l_quotaoffs_flag & type)
98                 return 0;
99
100         /*
101          * At this point we know that quota was _not_ turned off.
102          * Since the mount flags are not indicating to us otherwise, this
103          * must mean that quota is on, and the dquot needs to be replayed.
104          * Remember that we may not have fully recovered the superblock yet,
105          * so we can't do the usual trick of looking at the SB quota bits.
106          *
107          * The other possibility, of course, is that the quota subsystem was
108          * removed since the last mount - ENOSYS.
109          */
110         dq_f = item->ri_buf[0].i_addr;
111         ASSERT(dq_f);
112         fa = xfs_dquot_verify(mp, recddq, dq_f->qlf_id);
113         if (fa) {
114                 xfs_alert(mp, "corrupt dquot ID 0x%x in log at %pS",
115                                 dq_f->qlf_id, fa);
116                 return -EFSCORRUPTED;
117         }
118         ASSERT(dq_f->qlf_len == 1);
119
120         /*
121          * At this point we are assuming that the dquots have been allocated
122          * and hence the buffer has valid dquots stamped in it. It should,
123          * therefore, pass verifier validation. If the dquot is bad, then the
124          * we'll return an error here, so we don't need to specifically check
125          * the dquot in the buffer after the verifier has run.
126          */
127         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
128                                    XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
129                                    &xfs_dquot_buf_ops);
130         if (error)
131                 return error;
132
133         ASSERT(bp);
134         dqb = xfs_buf_offset(bp, dq_f->qlf_boffset);
135         ddq = &dqb->dd_diskdq;
136
137         /*
138          * If the dquot has an LSN in it, recover the dquot only if it's less
139          * than the lsn of the transaction we are replaying.
140          */
141         if (xfs_has_crc(mp)) {
142                 xfs_lsn_t       lsn = be64_to_cpu(dqb->dd_lsn);
143
144                 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
145                         goto out_release;
146                 }
147         }
148
149         memcpy(ddq, recddq, item->ri_buf[1].i_len);
150         if (xfs_has_crc(mp)) {
151                 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
152                                  XFS_DQUOT_CRC_OFF);
153         }
154
155         ASSERT(dq_f->qlf_size == 2);
156         ASSERT(bp->b_mount == mp);
157         bp->b_flags |= _XBF_LOGRECOVERY;
158         xfs_buf_delwri_queue(bp, buffer_list);
159
160 out_release:
161         xfs_buf_relse(bp);
162         return 0;
163 }
164
165 const struct xlog_recover_item_ops xlog_dquot_item_ops = {
166         .item_type              = XFS_LI_DQUOT,
167         .ra_pass2               = xlog_recover_dquot_ra_pass2,
168         .commit_pass2           = xlog_recover_dquot_commit_pass2,
169 };
170
171 /*
172  * Recover QUOTAOFF records. We simply make a note of it in the xlog
173  * structure, so that we know not to do any dquot item or dquot buffer recovery,
174  * of that type.
175  */
176 STATIC int
177 xlog_recover_quotaoff_commit_pass1(
178         struct xlog                     *log,
179         struct xlog_recover_item        *item)
180 {
181         struct xfs_qoff_logformat       *qoff_f = item->ri_buf[0].i_addr;
182         ASSERT(qoff_f);
183
184         /*
185          * The logitem format's flag tells us if this was user quotaoff,
186          * group/project quotaoff or both.
187          */
188         if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
189                 log->l_quotaoffs_flag |= XFS_DQTYPE_USER;
190         if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
191                 log->l_quotaoffs_flag |= XFS_DQTYPE_PROJ;
192         if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
193                 log->l_quotaoffs_flag |= XFS_DQTYPE_GROUP;
194
195         return 0;
196 }
197
198 const struct xlog_recover_item_ops xlog_quotaoff_item_ops = {
199         .item_type              = XFS_LI_QUOTAOFF,
200         .commit_pass1           = xlog_recover_quotaoff_commit_pass1,
201         /* nothing to commit in pass2 */
202 };