xfs_dquot.c (3b5d1afd1f13bcab85eaa28223ad396694f929e3) xfs_dquot.c (b63da6c8dfa9b2ab3554e8c59ef294d1f28bb9bd)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_format.h"

--- 9 unchanged lines hidden (view full) ---

18#include "xfs_trans.h"
19#include "xfs_buf_item.h"
20#include "xfs_trans_space.h"
21#include "xfs_trans_priv.h"
22#include "xfs_qm.h"
23#include "xfs_trace.h"
24#include "xfs_log.h"
25#include "xfs_bmap_btree.h"
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_format.h"

--- 9 unchanged lines hidden (view full) ---

18#include "xfs_trans.h"
19#include "xfs_buf_item.h"
20#include "xfs_trans_space.h"
21#include "xfs_trans_priv.h"
22#include "xfs_qm.h"
23#include "xfs_trace.h"
24#include "xfs_log.h"
25#include "xfs_bmap_btree.h"
26#include "xfs_error.h"
26
27/*
28 * Lock order:
29 *
30 * ip->i_lock
31 * qi->qi_tree_lock
32 * dquot->q_qlock (xfs_dqlock() and friends)
33 * dquot->q_flush (xfs_dqflock() and friends)

--- 27 unchanged lines hidden (view full) ---

61
62/*
63 * If default limits are in force, push them into the dquot now.
64 * We overwrite the dquot limits only if they are zero and this
65 * is not the root dquot.
66 */
67void
68xfs_qm_adjust_dqlimits(
27
28/*
29 * Lock order:
30 *
31 * ip->i_lock
32 * qi->qi_tree_lock
33 * dquot->q_qlock (xfs_dqlock() and friends)
34 * dquot->q_flush (xfs_dqflock() and friends)

--- 27 unchanged lines hidden (view full) ---

62
63/*
64 * If default limits are in force, push them into the dquot now.
65 * We overwrite the dquot limits only if they are zero and this
66 * is not the root dquot.
67 */
68void
69xfs_qm_adjust_dqlimits(
69 struct xfs_mount *mp,
70 struct xfs_dquot *dq)
71{
70 struct xfs_dquot *dq)
71{
72 struct xfs_mount *mp = dq->q_mount;
72 struct xfs_quotainfo *q = mp->m_quotainfo;
73 struct xfs_quotainfo *q = mp->m_quotainfo;
73 struct xfs_disk_dquot *d = &dq->q_core;
74 struct xfs_def_quota *defq;
75 int prealloc = 0;
76
74 struct xfs_def_quota *defq;
75 int prealloc = 0;
76
77 ASSERT(d->d_id);
77 ASSERT(dq->q_id);
78 defq = xfs_get_defquota(q, xfs_dquot_type(dq));
79
78 defq = xfs_get_defquota(q, xfs_dquot_type(dq));
79
80 if (defq->bsoftlimit && !d->d_blk_softlimit) {
81 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
80 if (!dq->q_blk.softlimit) {
81 dq->q_blk.softlimit = defq->blk.soft;
82 prealloc = 1;
83 }
82 prealloc = 1;
83 }
84 if (defq->bhardlimit && !d->d_blk_hardlimit) {
85 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
84 if (!dq->q_blk.hardlimit) {
85 dq->q_blk.hardlimit = defq->blk.hard;
86 prealloc = 1;
87 }
86 prealloc = 1;
87 }
88 if (defq->isoftlimit && !d->d_ino_softlimit)
89 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
90 if (defq->ihardlimit && !d->d_ino_hardlimit)
91 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
92 if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
93 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
94 if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
95 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
88 if (!dq->q_ino.softlimit)
89 dq->q_ino.softlimit = defq->ino.soft;
90 if (!dq->q_ino.hardlimit)
91 dq->q_ino.hardlimit = defq->ino.hard;
92 if (!dq->q_rtb.softlimit)
93 dq->q_rtb.softlimit = defq->rtb.soft;
94 if (!dq->q_rtb.hardlimit)
95 dq->q_rtb.hardlimit = defq->rtb.hard;
96
97 if (prealloc)
98 xfs_dquot_set_prealloc_limits(dq);
99}
100
101/*
96
97 if (prealloc)
98 xfs_dquot_set_prealloc_limits(dq);
99}
100
101/*
102 * Determine if this quota counter is over either limit and set the quota
103 * timers as appropriate.
104 */
105static inline void
106xfs_qm_adjust_res_timer(
107 struct xfs_dquot_res *res,
108 struct xfs_quota_limits *qlim)
109{
110 ASSERT(res->hardlimit == 0 || res->softlimit <= res->hardlimit);
111
112 if ((res->softlimit && res->count > res->softlimit) ||
113 (res->hardlimit && res->count > res->hardlimit)) {
114 if (res->timer == 0)
115 res->timer = ktime_get_real_seconds() + qlim->time;
116 } else {
117 if (res->timer == 0)
118 res->warnings = 0;
119 else
120 res->timer = 0;
121 }
122}
123
124/*
102 * Check the limits and timers of a dquot and start or reset timers
103 * if necessary.
104 * This gets called even when quota enforcement is OFF, which makes our
105 * life a little less complicated. (We just don't reject any quota
106 * reservations in that case, when enforcement is off).
107 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
108 * enforcement's off.
109 * In contrast, warnings are a little different in that they don't
110 * 'automatically' get started when limits get exceeded. They do
111 * get reset to zero, however, when we find the count to be under
112 * the soft limit (they are only ever set non-zero via userspace).
113 */
114void
115xfs_qm_adjust_dqtimers(
125 * Check the limits and timers of a dquot and start or reset timers
126 * if necessary.
127 * This gets called even when quota enforcement is OFF, which makes our
128 * life a little less complicated. (We just don't reject any quota
129 * reservations in that case, when enforcement is off).
130 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
131 * enforcement's off.
132 * In contrast, warnings are a little different in that they don't
133 * 'automatically' get started when limits get exceeded. They do
134 * get reset to zero, however, when we find the count to be under
135 * the soft limit (they are only ever set non-zero via userspace).
136 */
137void
138xfs_qm_adjust_dqtimers(
116 struct xfs_mount *mp,
117 struct xfs_dquot *dq)
118{
139 struct xfs_dquot *dq)
140{
141 struct xfs_mount *mp = dq->q_mount;
119 struct xfs_quotainfo *qi = mp->m_quotainfo;
142 struct xfs_quotainfo *qi = mp->m_quotainfo;
120 struct xfs_disk_dquot *d = &dq->q_core;
121 struct xfs_def_quota *defq;
122
143 struct xfs_def_quota *defq;
144
123 ASSERT(d->d_id);
145 ASSERT(dq->q_id);
124 defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
125
146 defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
147
126#ifdef DEBUG
127 if (d->d_blk_hardlimit)
128 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
129 be64_to_cpu(d->d_blk_hardlimit));
130 if (d->d_ino_hardlimit)
131 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
132 be64_to_cpu(d->d_ino_hardlimit));
133 if (d->d_rtb_hardlimit)
134 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
135 be64_to_cpu(d->d_rtb_hardlimit));
136#endif
137
138 if (!d->d_btimer) {
139 if ((d->d_blk_softlimit &&
140 (be64_to_cpu(d->d_bcount) >
141 be64_to_cpu(d->d_blk_softlimit))) ||
142 (d->d_blk_hardlimit &&
143 (be64_to_cpu(d->d_bcount) >
144 be64_to_cpu(d->d_blk_hardlimit)))) {
145 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() +
146 defq->btimelimit);
147 } else {
148 d->d_bwarns = 0;
149 }
150 } else {
151 if ((!d->d_blk_softlimit ||
152 (be64_to_cpu(d->d_bcount) <=
153 be64_to_cpu(d->d_blk_softlimit))) &&
154 (!d->d_blk_hardlimit ||
155 (be64_to_cpu(d->d_bcount) <=
156 be64_to_cpu(d->d_blk_hardlimit)))) {
157 d->d_btimer = 0;
158 }
159 }
160
161 if (!d->d_itimer) {
162 if ((d->d_ino_softlimit &&
163 (be64_to_cpu(d->d_icount) >
164 be64_to_cpu(d->d_ino_softlimit))) ||
165 (d->d_ino_hardlimit &&
166 (be64_to_cpu(d->d_icount) >
167 be64_to_cpu(d->d_ino_hardlimit)))) {
168 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() +
169 defq->itimelimit);
170 } else {
171 d->d_iwarns = 0;
172 }
173 } else {
174 if ((!d->d_ino_softlimit ||
175 (be64_to_cpu(d->d_icount) <=
176 be64_to_cpu(d->d_ino_softlimit))) &&
177 (!d->d_ino_hardlimit ||
178 (be64_to_cpu(d->d_icount) <=
179 be64_to_cpu(d->d_ino_hardlimit)))) {
180 d->d_itimer = 0;
181 }
182 }
183
184 if (!d->d_rtbtimer) {
185 if ((d->d_rtb_softlimit &&
186 (be64_to_cpu(d->d_rtbcount) >
187 be64_to_cpu(d->d_rtb_softlimit))) ||
188 (d->d_rtb_hardlimit &&
189 (be64_to_cpu(d->d_rtbcount) >
190 be64_to_cpu(d->d_rtb_hardlimit)))) {
191 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() +
192 defq->rtbtimelimit);
193 } else {
194 d->d_rtbwarns = 0;
195 }
196 } else {
197 if ((!d->d_rtb_softlimit ||
198 (be64_to_cpu(d->d_rtbcount) <=
199 be64_to_cpu(d->d_rtb_softlimit))) &&
200 (!d->d_rtb_hardlimit ||
201 (be64_to_cpu(d->d_rtbcount) <=
202 be64_to_cpu(d->d_rtb_hardlimit)))) {
203 d->d_rtbtimer = 0;
204 }
205 }
148 xfs_qm_adjust_res_timer(&dq->q_blk, &defq->blk);
149 xfs_qm_adjust_res_timer(&dq->q_ino, &defq->ino);
150 xfs_qm_adjust_res_timer(&dq->q_rtb, &defq->rtb);
206}
207
208/*
209 * initialize a buffer full of dquots and log the whole thing
210 */
211STATIC void
212xfs_qm_init_dquot_blk(
213 struct xfs_trans *tp,
214 struct xfs_mount *mp,
215 xfs_dqid_t id,
151}
152
153/*
154 * initialize a buffer full of dquots and log the whole thing
155 */
156STATIC void
157xfs_qm_init_dquot_blk(
158 struct xfs_trans *tp,
159 struct xfs_mount *mp,
160 xfs_dqid_t id,
216 uint type,
161 xfs_dqtype_t type,
217 struct xfs_buf *bp)
218{
219 struct xfs_quotainfo *q = mp->m_quotainfo;
220 struct xfs_dqblk *d;
221 xfs_dqid_t curid;
222 unsigned int qflag;
223 unsigned int blftype;
224 int i;
225
226 ASSERT(tp);
227 ASSERT(xfs_buf_islocked(bp));
228
162 struct xfs_buf *bp)
163{
164 struct xfs_quotainfo *q = mp->m_quotainfo;
165 struct xfs_dqblk *d;
166 xfs_dqid_t curid;
167 unsigned int qflag;
168 unsigned int blftype;
169 int i;
170
171 ASSERT(tp);
172 ASSERT(xfs_buf_islocked(bp));
173
174 switch (type) {
175 case XFS_DQTYPE_USER:
176 qflag = XFS_UQUOTA_CHKD;
177 blftype = XFS_BLF_UDQUOT_BUF;
178 break;
179 case XFS_DQTYPE_PROJ:
180 qflag = XFS_PQUOTA_CHKD;
181 blftype = XFS_BLF_PDQUOT_BUF;
182 break;
183 case XFS_DQTYPE_GROUP:
184 qflag = XFS_GQUOTA_CHKD;
185 blftype = XFS_BLF_GDQUOT_BUF;
186 break;
187 default:
188 ASSERT(0);
189 return;
190 }
191
229 d = bp->b_addr;
230
231 /*
232 * ID of the first dquot in the block - id's are zero based.
233 */
234 curid = id - (id % q->qi_dqperchunk);
235 memset(d, 0, BBTOB(q->qi_dqchunklen));
236 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
237 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
238 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
239 d->dd_diskdq.d_id = cpu_to_be32(curid);
192 d = bp->b_addr;
193
194 /*
195 * ID of the first dquot in the block - id's are zero based.
196 */
197 curid = id - (id % q->qi_dqperchunk);
198 memset(d, 0, BBTOB(q->qi_dqchunklen));
199 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
200 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
201 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
202 d->dd_diskdq.d_id = cpu_to_be32(curid);
240 d->dd_diskdq.d_flags = type;
203 d->dd_diskdq.d_type = type;
241 if (xfs_sb_version_hascrc(&mp->m_sb)) {
242 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
243 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
244 XFS_DQUOT_CRC_OFF);
245 }
246 }
247
204 if (xfs_sb_version_hascrc(&mp->m_sb)) {
205 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
206 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
207 XFS_DQUOT_CRC_OFF);
208 }
209 }
210
248 if (type & XFS_DQ_USER) {
249 qflag = XFS_UQUOTA_CHKD;
250 blftype = XFS_BLF_UDQUOT_BUF;
251 } else if (type & XFS_DQ_PROJ) {
252 qflag = XFS_PQUOTA_CHKD;
253 blftype = XFS_BLF_PDQUOT_BUF;
254 } else {
255 qflag = XFS_GQUOTA_CHKD;
256 blftype = XFS_BLF_GDQUOT_BUF;
257 }
258
259 xfs_trans_dquot_buf(tp, bp, blftype);
260
261 /*
262 * quotacheck uses delayed writes to update all the dquots on disk in an
263 * efficient manner instead of logging the individual dquot changes as
264 * they are made. However if we log the buffer allocated here and crash
265 * after quotacheck while the logged initialisation is still in the
266 * active region of the log, log recovery can replay the dquot buffer

--- 18 unchanged lines hidden (view full) ---

285 * watermarks correspond to the soft and hard limits by default. If a soft limit
286 * is not specified, we use 95% of the hard limit.
287 */
288void
289xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
290{
291 uint64_t space;
292
211 xfs_trans_dquot_buf(tp, bp, blftype);
212
213 /*
214 * quotacheck uses delayed writes to update all the dquots on disk in an
215 * efficient manner instead of logging the individual dquot changes as
216 * they are made. However if we log the buffer allocated here and crash
217 * after quotacheck while the logged initialisation is still in the
218 * active region of the log, log recovery can replay the dquot buffer

--- 18 unchanged lines hidden (view full) ---

237 * watermarks correspond to the soft and hard limits by default. If a soft limit
238 * is not specified, we use 95% of the hard limit.
239 */
240void
241xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
242{
243 uint64_t space;
244
293 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
294 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
245 dqp->q_prealloc_hi_wmark = dqp->q_blk.hardlimit;
246 dqp->q_prealloc_lo_wmark = dqp->q_blk.softlimit;
295 if (!dqp->q_prealloc_lo_wmark) {
296 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
297 do_div(dqp->q_prealloc_lo_wmark, 100);
298 dqp->q_prealloc_lo_wmark *= 95;
299 }
300
301 space = dqp->q_prealloc_hi_wmark;
302

--- 13 unchanged lines hidden (view full) ---

316 struct xfs_trans **tpp,
317 struct xfs_dquot *dqp,
318 struct xfs_buf **bpp)
319{
320 struct xfs_bmbt_irec map;
321 struct xfs_trans *tp = *tpp;
322 struct xfs_mount *mp = tp->t_mountp;
323 struct xfs_buf *bp;
247 if (!dqp->q_prealloc_lo_wmark) {
248 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
249 do_div(dqp->q_prealloc_lo_wmark, 100);
250 dqp->q_prealloc_lo_wmark *= 95;
251 }
252
253 space = dqp->q_prealloc_hi_wmark;
254

--- 13 unchanged lines hidden (view full) ---

268 struct xfs_trans **tpp,
269 struct xfs_dquot *dqp,
270 struct xfs_buf **bpp)
271{
272 struct xfs_bmbt_irec map;
273 struct xfs_trans *tp = *tpp;
274 struct xfs_mount *mp = tp->t_mountp;
275 struct xfs_buf *bp;
324 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
276 xfs_dqtype_t qtype = xfs_dquot_type(dqp);
277 struct xfs_inode *quotip = xfs_quota_inode(mp, qtype);
325 int nmaps = 1;
326 int error;
327
328 trace_xfs_dqalloc(dqp);
329
330 xfs_ilock(quotip, XFS_ILOCK_EXCL);
278 int nmaps = 1;
279 int error;
280
281 trace_xfs_dqalloc(dqp);
282
283 xfs_ilock(quotip, XFS_ILOCK_EXCL);
331 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
284 if (!xfs_this_quota_on(dqp->q_mount, qtype)) {
332 /*
333 * Return if this type of quotas is turned off while we didn't
334 * have an inode lock
335 */
336 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
337 return -ESRCH;
338 }
339

--- 20 unchanged lines hidden (view full) ---

360 if (error)
361 return error;
362 bp->b_ops = &xfs_dquot_buf_ops;
363
364 /*
365 * Make a chunk of dquots out of this buffer and log
366 * the entire thing.
367 */
285 /*
286 * Return if this type of quotas is turned off while we didn't
287 * have an inode lock
288 */
289 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
290 return -ESRCH;
291 }
292

--- 20 unchanged lines hidden (view full) ---

313 if (error)
314 return error;
315 bp->b_ops = &xfs_dquot_buf_ops;
316
317 /*
318 * Make a chunk of dquots out of this buffer and log
319 * the entire thing.
320 */
368 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
369 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
321 xfs_qm_init_dquot_blk(tp, mp, dqp->q_id, qtype, bp);
370 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
371
372 /*
373 * Hold the buffer and join it to the dfops so that we'll still own
374 * the buffer when we return to the caller. The buffer disposal on
375 * error must be paid attention to very carefully, as it has been
376 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
377 * code when allocating a new dquot record" in 2005, and the later

--- 30 unchanged lines hidden (view full) ---

408STATIC int
409xfs_dquot_disk_read(
410 struct xfs_mount *mp,
411 struct xfs_dquot *dqp,
412 struct xfs_buf **bpp)
413{
414 struct xfs_bmbt_irec map;
415 struct xfs_buf *bp;
322 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
323
324 /*
325 * Hold the buffer and join it to the dfops so that we'll still own
326 * the buffer when we return to the caller. The buffer disposal on
327 * error must be paid attention to very carefully, as it has been
328 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
329 * code when allocating a new dquot record" in 2005, and the later

--- 30 unchanged lines hidden (view full) ---

360STATIC int
361xfs_dquot_disk_read(
362 struct xfs_mount *mp,
363 struct xfs_dquot *dqp,
364 struct xfs_buf **bpp)
365{
366 struct xfs_bmbt_irec map;
367 struct xfs_buf *bp;
416 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
368 xfs_dqtype_t qtype = xfs_dquot_type(dqp);
369 struct xfs_inode *quotip = xfs_quota_inode(mp, qtype);
417 uint lock_mode;
418 int nmaps = 1;
419 int error;
420
421 lock_mode = xfs_ilock_data_map_shared(quotip);
370 uint lock_mode;
371 int nmaps = 1;
372 int error;
373
374 lock_mode = xfs_ilock_data_map_shared(quotip);
422 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
375 if (!xfs_this_quota_on(mp, qtype)) {
423 /*
424 * Return if this type of quotas is turned off while we
425 * didn't have the quota inode lock.
426 */
427 xfs_iunlock(quotip, lock_mode);
428 return -ESRCH;
429 }
430

--- 35 unchanged lines hidden (view full) ---

466 return 0;
467}
468
469/* Allocate and initialize everything we need for an incore dquot. */
470STATIC struct xfs_dquot *
471xfs_dquot_alloc(
472 struct xfs_mount *mp,
473 xfs_dqid_t id,
376 /*
377 * Return if this type of quotas is turned off while we
378 * didn't have the quota inode lock.
379 */
380 xfs_iunlock(quotip, lock_mode);
381 return -ESRCH;
382 }
383

--- 35 unchanged lines hidden (view full) ---

419 return 0;
420}
421
422/* Allocate and initialize everything we need for an incore dquot. */
423STATIC struct xfs_dquot *
424xfs_dquot_alloc(
425 struct xfs_mount *mp,
426 xfs_dqid_t id,
474 uint type)
427 xfs_dqtype_t type)
475{
476 struct xfs_dquot *dqp;
477
428{
429 struct xfs_dquot *dqp;
430
478 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
431 dqp = kmem_cache_zalloc(xfs_qm_dqzone, GFP_KERNEL | __GFP_NOFAIL);
479
432
480 dqp->dq_flags = type;
481 dqp->q_core.d_id = cpu_to_be32(id);
433 dqp->q_type = type;
434 dqp->q_id = id;
482 dqp->q_mount = mp;
483 INIT_LIST_HEAD(&dqp->q_lru);
484 mutex_init(&dqp->q_qlock);
485 init_waitqueue_head(&dqp->q_pinwait);
486 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
487 /*
488 * Offset of dquot in the (fixed sized) dquot chunk.
489 */

--- 8 unchanged lines hidden (view full) ---

498 init_completion(&dqp->q_flush);
499 complete(&dqp->q_flush);
500
501 /*
502 * Make sure group quotas have a different lock class than user
503 * quotas.
504 */
505 switch (type) {
435 dqp->q_mount = mp;
436 INIT_LIST_HEAD(&dqp->q_lru);
437 mutex_init(&dqp->q_qlock);
438 init_waitqueue_head(&dqp->q_pinwait);
439 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
440 /*
441 * Offset of dquot in the (fixed sized) dquot chunk.
442 */

--- 8 unchanged lines hidden (view full) ---

451 init_completion(&dqp->q_flush);
452 complete(&dqp->q_flush);
453
454 /*
455 * Make sure group quotas have a different lock class than user
456 * quotas.
457 */
458 switch (type) {
506 case XFS_DQ_USER:
459 case XFS_DQTYPE_USER:
507 /* uses the default lock class */
508 break;
460 /* uses the default lock class */
461 break;
509 case XFS_DQ_GROUP:
462 case XFS_DQTYPE_GROUP:
510 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
511 break;
463 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
464 break;
512 case XFS_DQ_PROJ:
465 case XFS_DQTYPE_PROJ:
513 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
514 break;
515 default:
516 ASSERT(0);
517 break;
518 }
519
520 xfs_qm_dquot_logitem_init(dqp);
521
522 XFS_STATS_INC(mp, xs_qm_dquot);
523 return dqp;
524}
525
526/* Copy the in-core quota fields in from the on-disk buffer. */
466 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
467 break;
468 default:
469 ASSERT(0);
470 break;
471 }
472
473 xfs_qm_dquot_logitem_init(dqp);
474
475 XFS_STATS_INC(mp, xs_qm_dquot);
476 return dqp;
477}
478
479/* Copy the in-core quota fields in from the on-disk buffer. */
527STATIC void
480STATIC int
528xfs_dquot_from_disk(
529 struct xfs_dquot *dqp,
530 struct xfs_buf *bp)
531{
532 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
533
481xfs_dquot_from_disk(
482 struct xfs_dquot *dqp,
483 struct xfs_buf *bp)
484{
485 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
486
487 /*
488 * Ensure that we got the type and ID we were looking for.
489 * Everything else was checked by the dquot buffer verifier.
490 */
491 if ((ddqp->d_type & XFS_DQTYPE_REC_MASK) != xfs_dquot_type(dqp) ||
492 be32_to_cpu(ddqp->d_id) != dqp->q_id) {
493 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR,
494 "Metadata corruption detected at %pS, quota %u",
495 __this_address, dqp->q_id);
496 xfs_alert(bp->b_mount, "Unmount and run xfs_repair");
497 return -EFSCORRUPTED;
498 }
499
534 /* copy everything from disk dquot to the incore dquot */
500 /* copy everything from disk dquot to the incore dquot */
535 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot));
501 dqp->q_type = ddqp->d_type;
502 dqp->q_blk.hardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
503 dqp->q_blk.softlimit = be64_to_cpu(ddqp->d_blk_softlimit);
504 dqp->q_ino.hardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
505 dqp->q_ino.softlimit = be64_to_cpu(ddqp->d_ino_softlimit);
506 dqp->q_rtb.hardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
507 dqp->q_rtb.softlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
536
508
509 dqp->q_blk.count = be64_to_cpu(ddqp->d_bcount);
510 dqp->q_ino.count = be64_to_cpu(ddqp->d_icount);
511 dqp->q_rtb.count = be64_to_cpu(ddqp->d_rtbcount);
512
513 dqp->q_blk.warnings = be16_to_cpu(ddqp->d_bwarns);
514 dqp->q_ino.warnings = be16_to_cpu(ddqp->d_iwarns);
515 dqp->q_rtb.warnings = be16_to_cpu(ddqp->d_rtbwarns);
516
517 dqp->q_blk.timer = be32_to_cpu(ddqp->d_btimer);
518 dqp->q_ino.timer = be32_to_cpu(ddqp->d_itimer);
519 dqp->q_rtb.timer = be32_to_cpu(ddqp->d_rtbtimer);
520
537 /*
538 * Reservation counters are defined as reservation plus current usage
539 * to avoid having to add every time.
540 */
521 /*
522 * Reservation counters are defined as reservation plus current usage
523 * to avoid having to add every time.
524 */
541 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
542 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
543 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
525 dqp->q_blk.reserved = dqp->q_blk.count;
526 dqp->q_ino.reserved = dqp->q_ino.count;
527 dqp->q_rtb.reserved = dqp->q_rtb.count;
544
545 /* initialize the dquot speculative prealloc thresholds */
546 xfs_dquot_set_prealloc_limits(dqp);
528
529 /* initialize the dquot speculative prealloc thresholds */
530 xfs_dquot_set_prealloc_limits(dqp);
531 return 0;
547}
548
532}
533
534/* Copy the in-core quota fields into the on-disk buffer. */
535void
536xfs_dquot_to_disk(
537 struct xfs_disk_dquot *ddqp,
538 struct xfs_dquot *dqp)
539{
540 ddqp->d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
541 ddqp->d_version = XFS_DQUOT_VERSION;
542 ddqp->d_type = dqp->q_type;
543 ddqp->d_id = cpu_to_be32(dqp->q_id);
544 ddqp->d_pad0 = 0;
545 ddqp->d_pad = 0;
546
547 ddqp->d_blk_hardlimit = cpu_to_be64(dqp->q_blk.hardlimit);
548 ddqp->d_blk_softlimit = cpu_to_be64(dqp->q_blk.softlimit);
549 ddqp->d_ino_hardlimit = cpu_to_be64(dqp->q_ino.hardlimit);
550 ddqp->d_ino_softlimit = cpu_to_be64(dqp->q_ino.softlimit);
551 ddqp->d_rtb_hardlimit = cpu_to_be64(dqp->q_rtb.hardlimit);
552 ddqp->d_rtb_softlimit = cpu_to_be64(dqp->q_rtb.softlimit);
553
554 ddqp->d_bcount = cpu_to_be64(dqp->q_blk.count);
555 ddqp->d_icount = cpu_to_be64(dqp->q_ino.count);
556 ddqp->d_rtbcount = cpu_to_be64(dqp->q_rtb.count);
557
558 ddqp->d_bwarns = cpu_to_be16(dqp->q_blk.warnings);
559 ddqp->d_iwarns = cpu_to_be16(dqp->q_ino.warnings);
560 ddqp->d_rtbwarns = cpu_to_be16(dqp->q_rtb.warnings);
561
562 ddqp->d_btimer = cpu_to_be32(dqp->q_blk.timer);
563 ddqp->d_itimer = cpu_to_be32(dqp->q_ino.timer);
564 ddqp->d_rtbtimer = cpu_to_be32(dqp->q_rtb.timer);
565}
566
549/* Allocate and initialize the dquot buffer for this in-core dquot. */
550static int
551xfs_qm_dqread_alloc(
552 struct xfs_mount *mp,
553 struct xfs_dquot *dqp,
554 struct xfs_buf **bpp)
555{
556 struct xfs_trans *tp;

--- 30 unchanged lines hidden (view full) ---

587 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
588 * and release the buffer immediately. If @can_alloc is true, fill any
589 * holes in the on-disk metadata.
590 */
591static int
592xfs_qm_dqread(
593 struct xfs_mount *mp,
594 xfs_dqid_t id,
567/* Allocate and initialize the dquot buffer for this in-core dquot. */
568static int
569xfs_qm_dqread_alloc(
570 struct xfs_mount *mp,
571 struct xfs_dquot *dqp,
572 struct xfs_buf **bpp)
573{
574 struct xfs_trans *tp;

--- 30 unchanged lines hidden (view full) ---

605 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
606 * and release the buffer immediately. If @can_alloc is true, fill any
607 * holes in the on-disk metadata.
608 */
609static int
610xfs_qm_dqread(
611 struct xfs_mount *mp,
612 xfs_dqid_t id,
595 uint type,
613 xfs_dqtype_t type,
596 bool can_alloc,
597 struct xfs_dquot **dqpp)
598{
599 struct xfs_dquot *dqp;
600 struct xfs_buf *bp;
601 int error;
602
603 dqp = xfs_dquot_alloc(mp, id, type);

--- 8 unchanged lines hidden (view full) ---

612
613 /*
614 * At this point we should have a clean locked buffer. Copy the data
615 * to the incore dquot and release the buffer since the incore dquot
616 * has its own locking protocol so we needn't tie up the buffer any
617 * further.
618 */
619 ASSERT(xfs_buf_islocked(bp));
614 bool can_alloc,
615 struct xfs_dquot **dqpp)
616{
617 struct xfs_dquot *dqp;
618 struct xfs_buf *bp;
619 int error;
620
621 dqp = xfs_dquot_alloc(mp, id, type);

--- 8 unchanged lines hidden (view full) ---

630
631 /*
632 * At this point we should have a clean locked buffer. Copy the data
633 * to the incore dquot and release the buffer since the incore dquot
634 * has its own locking protocol so we needn't tie up the buffer any
635 * further.
636 */
637 ASSERT(xfs_buf_islocked(bp));
620 xfs_dquot_from_disk(dqp, bp);
621
638 error = xfs_dquot_from_disk(dqp, bp);
622 xfs_buf_relse(bp);
639 xfs_buf_relse(bp);
640 if (error)
641 goto err;
642
623 *dqpp = dqp;
624 return error;
625
626err:
627 trace_xfs_dqread_fail(dqp);
628 xfs_qm_dqdestroy(dqp);
629 *dqpp = NULL;
630 return error;
631}
632
633/*
634 * Advance to the next id in the current chunk, or if at the
635 * end of the chunk, skip ahead to first id in next allocated chunk
636 * using the SEEK_DATA interface.
637 */
638static int
639xfs_dq_get_next_id(
640 struct xfs_mount *mp,
643 *dqpp = dqp;
644 return error;
645
646err:
647 trace_xfs_dqread_fail(dqp);
648 xfs_qm_dqdestroy(dqp);
649 *dqpp = NULL;
650 return error;
651}
652
653/*
654 * Advance to the next id in the current chunk, or if at the
655 * end of the chunk, skip ahead to first id in next allocated chunk
656 * using the SEEK_DATA interface.
657 */
658static int
659xfs_dq_get_next_id(
660 struct xfs_mount *mp,
641 uint type,
661 xfs_dqtype_t type,
642 xfs_dqid_t *id)
643{
644 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
645 xfs_dqid_t next_id = *id + 1; /* simple advance */
646 uint lock_flags;
647 struct xfs_bmbt_irec got;
648 struct xfs_iext_cursor cur;
649 xfs_fsblock_t start;

--- 51 unchanged lines hidden (view full) ---

701 dqp = radix_tree_lookup(tree, id);
702 if (!dqp) {
703 mutex_unlock(&qi->qi_tree_lock);
704 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
705 return NULL;
706 }
707
708 xfs_dqlock(dqp);
662 xfs_dqid_t *id)
663{
664 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
665 xfs_dqid_t next_id = *id + 1; /* simple advance */
666 uint lock_flags;
667 struct xfs_bmbt_irec got;
668 struct xfs_iext_cursor cur;
669 xfs_fsblock_t start;

--- 51 unchanged lines hidden (view full) ---

721 dqp = radix_tree_lookup(tree, id);
722 if (!dqp) {
723 mutex_unlock(&qi->qi_tree_lock);
724 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
725 return NULL;
726 }
727
728 xfs_dqlock(dqp);
709 if (dqp->dq_flags & XFS_DQ_FREEING) {
729 if (dqp->q_flags & XFS_DQFLAG_FREEING) {
710 xfs_dqunlock(dqp);
711 mutex_unlock(&qi->qi_tree_lock);
712 trace_xfs_dqget_freeing(dqp);
713 delay(1);
714 goto restart;
715 }
716
717 dqp->q_nrefs++;

--- 39 unchanged lines hidden (view full) ---

757
758 return 0;
759}
760
761/* Check our input parameters. */
762static int
763xfs_qm_dqget_checks(
764 struct xfs_mount *mp,
730 xfs_dqunlock(dqp);
731 mutex_unlock(&qi->qi_tree_lock);
732 trace_xfs_dqget_freeing(dqp);
733 delay(1);
734 goto restart;
735 }
736
737 dqp->q_nrefs++;

--- 39 unchanged lines hidden (view full) ---

777
778 return 0;
779}
780
781/* Check our input parameters. */
782static int
783xfs_qm_dqget_checks(
784 struct xfs_mount *mp,
765 uint type)
785 xfs_dqtype_t type)
766{
767 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
768 return -ESRCH;
769
770 switch (type) {
786{
787 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
788 return -ESRCH;
789
790 switch (type) {
771 case XFS_DQ_USER:
791 case XFS_DQTYPE_USER:
772 if (!XFS_IS_UQUOTA_ON(mp))
773 return -ESRCH;
774 return 0;
792 if (!XFS_IS_UQUOTA_ON(mp))
793 return -ESRCH;
794 return 0;
775 case XFS_DQ_GROUP:
795 case XFS_DQTYPE_GROUP:
776 if (!XFS_IS_GQUOTA_ON(mp))
777 return -ESRCH;
778 return 0;
796 if (!XFS_IS_GQUOTA_ON(mp))
797 return -ESRCH;
798 return 0;
779 case XFS_DQ_PROJ:
799 case XFS_DQTYPE_PROJ:
780 if (!XFS_IS_PQUOTA_ON(mp))
781 return -ESRCH;
782 return 0;
783 default:
784 WARN_ON_ONCE(0);
785 return -EINVAL;
786 }
787}
788
789/*
800 if (!XFS_IS_PQUOTA_ON(mp))
801 return -ESRCH;
802 return 0;
803 default:
804 WARN_ON_ONCE(0);
805 return -EINVAL;
806 }
807}
808
809/*
790 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
810 * Given the file system, id, and type (UDQUOT/GDQUOT), return a locked
791 * dquot, doing an allocation (if requested) as needed.
792 */
793int
794xfs_qm_dqget(
795 struct xfs_mount *mp,
796 xfs_dqid_t id,
811 * dquot, doing an allocation (if requested) as needed.
812 */
813int
814xfs_qm_dqget(
815 struct xfs_mount *mp,
816 xfs_dqid_t id,
797 uint type,
817 xfs_dqtype_t type,
798 bool can_alloc,
799 struct xfs_dquot **O_dqpp)
800{
801 struct xfs_quotainfo *qi = mp->m_quotainfo;
802 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
803 struct xfs_dquot *dqp;
804 int error;
805

--- 33 unchanged lines hidden (view full) ---

839 * metadata. This function is only for use during quota initialization so
840 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
841 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
842 */
843int
844xfs_qm_dqget_uncached(
845 struct xfs_mount *mp,
846 xfs_dqid_t id,
818 bool can_alloc,
819 struct xfs_dquot **O_dqpp)
820{
821 struct xfs_quotainfo *qi = mp->m_quotainfo;
822 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
823 struct xfs_dquot *dqp;
824 int error;
825

--- 33 unchanged lines hidden (view full) ---

859 * metadata. This function is only for use during quota initialization so
860 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
861 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
862 */
863int
864xfs_qm_dqget_uncached(
865 struct xfs_mount *mp,
866 xfs_dqid_t id,
847 uint type,
867 xfs_dqtype_t type,
848 struct xfs_dquot **dqpp)
849{
850 int error;
851
852 error = xfs_qm_dqget_checks(mp, type);
853 if (error)
854 return error;
855
856 return xfs_qm_dqread(mp, id, type, 0, dqpp);
857}
858
859/* Return the quota id for a given inode and type. */
860xfs_dqid_t
861xfs_qm_id_for_quotatype(
862 struct xfs_inode *ip,
868 struct xfs_dquot **dqpp)
869{
870 int error;
871
872 error = xfs_qm_dqget_checks(mp, type);
873 if (error)
874 return error;
875
876 return xfs_qm_dqread(mp, id, type, 0, dqpp);
877}
878
879/* Return the quota id for a given inode and type. */
880xfs_dqid_t
881xfs_qm_id_for_quotatype(
882 struct xfs_inode *ip,
863 uint type)
883 xfs_dqtype_t type)
864{
865 switch (type) {
884{
885 switch (type) {
866 case XFS_DQ_USER:
886 case XFS_DQTYPE_USER:
867 return i_uid_read(VFS_I(ip));
887 return i_uid_read(VFS_I(ip));
868 case XFS_DQ_GROUP:
888 case XFS_DQTYPE_GROUP:
869 return i_gid_read(VFS_I(ip));
889 return i_gid_read(VFS_I(ip));
870 case XFS_DQ_PROJ:
890 case XFS_DQTYPE_PROJ:
871 return ip->i_d.di_projid;
872 }
873 ASSERT(0);
874 return 0;
875}
876
877/*
878 * Return the dquot for a given inode and type. If @can_alloc is true, then
879 * allocate blocks if needed. The inode's ILOCK must be held and it must not
880 * have already had an inode attached.
881 */
882int
883xfs_qm_dqget_inode(
884 struct xfs_inode *ip,
891 return ip->i_d.di_projid;
892 }
893 ASSERT(0);
894 return 0;
895}
896
897/*
898 * Return the dquot for a given inode and type. If @can_alloc is true, then
899 * allocate blocks if needed. The inode's ILOCK must be held and it must not
900 * have already had an inode attached.
901 */
902int
903xfs_qm_dqget_inode(
904 struct xfs_inode *ip,
885 uint type,
905 xfs_dqtype_t type,
886 bool can_alloc,
887 struct xfs_dquot **O_dqpp)
888{
889 struct xfs_mount *mp = ip->i_mount;
890 struct xfs_quotainfo *qi = mp->m_quotainfo;
891 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
892 struct xfs_dquot *dqp;
893 xfs_dqid_t id;

--- 69 unchanged lines hidden (view full) ---

963/*
964 * Starting at @id and progressing upwards, look for an initialized incore
965 * dquot, lock it, and return it.
966 */
967int
968xfs_qm_dqget_next(
969 struct xfs_mount *mp,
970 xfs_dqid_t id,
906 bool can_alloc,
907 struct xfs_dquot **O_dqpp)
908{
909 struct xfs_mount *mp = ip->i_mount;
910 struct xfs_quotainfo *qi = mp->m_quotainfo;
911 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
912 struct xfs_dquot *dqp;
913 xfs_dqid_t id;

--- 69 unchanged lines hidden (view full) ---

983/*
984 * Starting at @id and progressing upwards, look for an initialized incore
985 * dquot, lock it, and return it.
986 */
987int
988xfs_qm_dqget_next(
989 struct xfs_mount *mp,
990 xfs_dqid_t id,
971 uint type,
991 xfs_dqtype_t type,
972 struct xfs_dquot **dqpp)
973{
974 struct xfs_dquot *dqp;
975 int error = 0;
976
977 *dqpp = NULL;
978 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
979 error = xfs_qm_dqget(mp, id, type, false, &dqp);

--- 63 unchanged lines hidden (view full) ---

1043
1044/*
1045 * This is the dquot flushing I/O completion routine. It is called
1046 * from interrupt level when the buffer containing the dquot is
1047 * flushed to disk. It is responsible for removing the dquot logitem
1048 * from the AIL if it has not been re-logged, and unlocking the dquot's
1049 * flush lock. This behavior is very similar to that of inodes..
1050 */
992 struct xfs_dquot **dqpp)
993{
994 struct xfs_dquot *dqp;
995 int error = 0;
996
997 *dqpp = NULL;
998 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
999 error = xfs_qm_dqget(mp, id, type, false, &dqp);

--- 63 unchanged lines hidden (view full) ---

1063
1064/*
1065 * This is the dquot flushing I/O completion routine. It is called
1066 * from interrupt level when the buffer containing the dquot is
1067 * flushed to disk. It is responsible for removing the dquot logitem
1068 * from the AIL if it has not been re-logged, and unlocking the dquot's
1069 * flush lock. This behavior is very similar to that of inodes..
1070 */
1051STATIC void
1071static void
1052xfs_qm_dqflush_done(
1072xfs_qm_dqflush_done(
1053 struct xfs_buf *bp,
1054 struct xfs_log_item *lip)
1055{
1056 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
1057 struct xfs_dquot *dqp = qip->qli_dquot;
1058 struct xfs_ail *ailp = lip->li_ailp;
1059 xfs_lsn_t tail_lsn;
1060
1061 /*

--- 4 unchanged lines hidden (view full) ---

1066 * since it's cheaper, and then we recheck while
1067 * holding the lock before removing the dquot from the AIL.
1068 */
1069 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
1070 ((lip->li_lsn == qip->qli_flush_lsn) ||
1071 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
1072
1073 spin_lock(&ailp->ail_lock);
1073 struct xfs_log_item *lip)
1074{
1075 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
1076 struct xfs_dquot *dqp = qip->qli_dquot;
1077 struct xfs_ail *ailp = lip->li_ailp;
1078 xfs_lsn_t tail_lsn;
1079
1080 /*

--- 4 unchanged lines hidden (view full) ---

1085 * since it's cheaper, and then we recheck while
1086 * holding the lock before removing the dquot from the AIL.
1087 */
1088 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
1089 ((lip->li_lsn == qip->qli_flush_lsn) ||
1090 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
1091
1092 spin_lock(&ailp->ail_lock);
1093 xfs_clear_li_failed(lip);
1074 if (lip->li_lsn == qip->qli_flush_lsn) {
1075 /* xfs_ail_update_finish() drops the AIL lock */
1076 tail_lsn = xfs_ail_delete_one(ailp, lip);
1077 xfs_ail_update_finish(ailp, tail_lsn);
1078 } else {
1094 if (lip->li_lsn == qip->qli_flush_lsn) {
1095 /* xfs_ail_update_finish() drops the AIL lock */
1096 tail_lsn = xfs_ail_delete_one(ailp, lip);
1097 xfs_ail_update_finish(ailp, tail_lsn);
1098 } else {
1079 /*
1080 * Clear the failed state since we are about to drop the
1081 * flush lock
1082 */
1083 xfs_clear_li_failed(lip);
1084 spin_unlock(&ailp->ail_lock);
1085 }
1086 }
1087
1088 /*
1089 * Release the dq's flush lock since we're done with it.
1090 */
1091 xfs_dqfunlock(dqp);
1092}
1093
1099 spin_unlock(&ailp->ail_lock);
1100 }
1101 }
1102
1103 /*
1104 * Release the dq's flush lock since we're done with it.
1105 */
1106 xfs_dqfunlock(dqp);
1107}
1108
1109void
1110xfs_dquot_done(
1111 struct xfs_buf *bp)
1112{
1113 struct xfs_log_item *lip, *n;
1114
1115 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1116 list_del_init(&lip->li_bio_list);
1117 xfs_qm_dqflush_done(lip);
1118 }
1119}
1120
1121/* Check incore dquot for errors before we flush. */
1122static xfs_failaddr_t
1123xfs_qm_dqflush_check(
1124 struct xfs_dquot *dqp)
1125{
1126 xfs_dqtype_t type = xfs_dquot_type(dqp);
1127
1128 if (type != XFS_DQTYPE_USER &&
1129 type != XFS_DQTYPE_GROUP &&
1130 type != XFS_DQTYPE_PROJ)
1131 return __this_address;
1132
1133 if (dqp->q_id == 0)
1134 return NULL;
1135
1136 if (dqp->q_blk.softlimit && dqp->q_blk.count > dqp->q_blk.softlimit &&
1137 !dqp->q_blk.timer)
1138 return __this_address;
1139
1140 if (dqp->q_ino.softlimit && dqp->q_ino.count > dqp->q_ino.softlimit &&
1141 !dqp->q_ino.timer)
1142 return __this_address;
1143
1144 if (dqp->q_rtb.softlimit && dqp->q_rtb.count > dqp->q_rtb.softlimit &&
1145 !dqp->q_rtb.timer)
1146 return __this_address;
1147
1148 return NULL;
1149}
1150
1094/*
1095 * Write a modified dquot to disk.
1096 * The dquot must be locked and the flush lock too taken by caller.
1097 * The flush lock will not be unlocked until the dquot reaches the disk,
1098 * but the dquot is free to be unlocked and modified by the caller
1099 * in the interim. Dquot is still locked on return. This behavior is
1100 * identical to that of inodes.
1101 */
1102int
1103xfs_qm_dqflush(
1104 struct xfs_dquot *dqp,
1105 struct xfs_buf **bpp)
1106{
1107 struct xfs_mount *mp = dqp->q_mount;
1108 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
1109 struct xfs_buf *bp;
1151/*
1152 * Write a modified dquot to disk.
1153 * The dquot must be locked and the flush lock too taken by caller.
1154 * The flush lock will not be unlocked until the dquot reaches the disk,
1155 * but the dquot is free to be unlocked and modified by the caller
1156 * in the interim. Dquot is still locked on return. This behavior is
1157 * identical to that of inodes.
1158 */
1159int
1160xfs_qm_dqflush(
1161 struct xfs_dquot *dqp,
1162 struct xfs_buf **bpp)
1163{
1164 struct xfs_mount *mp = dqp->q_mount;
1165 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
1166 struct xfs_buf *bp;
1110 struct xfs_dqblk *dqb;
1111 struct xfs_disk_dquot *ddqp;
1167 struct xfs_dqblk *dqblk;
1112 xfs_failaddr_t fa;
1113 int error;
1114
1115 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1116 ASSERT(!completion_done(&dqp->q_flush));
1117
1118 trace_xfs_dqflush(dqp);
1119

--- 7 unchanged lines hidden (view full) ---

1127 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1128 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1129 &bp, &xfs_dquot_buf_ops);
1130 if (error == -EAGAIN)
1131 goto out_unlock;
1132 if (error)
1133 goto out_abort;
1134
1168 xfs_failaddr_t fa;
1169 int error;
1170
1171 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1172 ASSERT(!completion_done(&dqp->q_flush));
1173
1174 trace_xfs_dqflush(dqp);
1175

--- 7 unchanged lines hidden (view full) ---

1183 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1184 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1185 &bp, &xfs_dquot_buf_ops);
1186 if (error == -EAGAIN)
1187 goto out_unlock;
1188 if (error)
1189 goto out_abort;
1190
1135 /*
1136 * Calculate the location of the dquot inside the buffer.
1137 */
1138 dqb = bp->b_addr + dqp->q_bufoffset;
1139 ddqp = &dqb->dd_diskdq;
1140
1141 /* sanity check the in-core structure before we flush */
1142 fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id),
1143 0);
1191 fa = xfs_qm_dqflush_check(dqp);
1144 if (fa) {
1145 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1192 if (fa) {
1193 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1146 be32_to_cpu(dqp->q_core.d_id), fa);
1194 dqp->q_id, fa);
1147 xfs_buf_relse(bp);
1148 error = -EFSCORRUPTED;
1149 goto out_abort;
1150 }
1151
1195 xfs_buf_relse(bp);
1196 error = -EFSCORRUPTED;
1197 goto out_abort;
1198 }
1199
1152 /* This is the only portion of data that needs to persist */
1153 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
1200 /* Flush the incore dquot to the ondisk buffer. */
1201 dqblk = bp->b_addr + dqp->q_bufoffset;
1202 xfs_dquot_to_disk(&dqblk->dd_diskdq, dqp);
1154
1155 /*
1156 * Clear the dirty field and remember the flush lsn for later use.
1157 */
1203
1204 /*
1205 * Clear the dirty field and remember the flush lsn for later use.
1206 */
1158 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1207 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
1159
1160 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1161 &dqp->q_logitem.qli_item.li_lsn);
1162
1163 /*
1164 * copy the lsn into the on-disk dquot now while we have the in memory
1165 * dquot here. This can't be done later in the write verifier as we
1166 * can't get access to the log item at that point in time.
1167 *
1168 * We also calculate the CRC here so that the on-disk dquot in the
1169 * buffer always has a valid CRC. This ensures there is no possibility
1170 * of a dquot without an up-to-date CRC getting to disk.
1171 */
1172 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1208
1209 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1210 &dqp->q_logitem.qli_item.li_lsn);
1211
1212 /*
1213 * copy the lsn into the on-disk dquot now while we have the in memory
1214 * dquot here. This can't be done later in the write verifier as we
1215 * can't get access to the log item at that point in time.
1216 *
1217 * We also calculate the CRC here so that the on-disk dquot in the
1218 * buffer always has a valid CRC. This ensures there is no possibility
1219 * of a dquot without an up-to-date CRC getting to disk.
1220 */
1221 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1173 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
1174 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1222 dqblk->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
1223 xfs_update_cksum((char *)dqblk, sizeof(struct xfs_dqblk),
1175 XFS_DQUOT_CRC_OFF);
1176 }
1177
1178 /*
1224 XFS_DQUOT_CRC_OFF);
1225 }
1226
1227 /*
1179 * Attach an iodone routine so that we can remove this dquot from the
1180 * AIL and release the flush lock once the dquot is synced to disk.
1228 * Attach the dquot to the buffer so that we can remove this dquot from
1229 * the AIL and release the flush lock once the dquot is synced to disk.
1181 */
1230 */
1182 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1183 &dqp->q_logitem.qli_item);
1231 bp->b_flags |= _XBF_DQUOTS;
1232 list_add_tail(&dqp->q_logitem.qli_item.li_bio_list, &bp->b_li_list);
1184
1185 /*
1186 * If the buffer is pinned then push on the log so we won't
1187 * get stuck waiting in the write for too long.
1188 */
1189 if (xfs_buf_ispinned(bp)) {
1190 trace_xfs_dqflush_force(dqp);
1191 xfs_log_force(mp, 0);
1192 }
1193
1194 trace_xfs_dqflush_done(dqp);
1195 *bpp = bp;
1196 return 0;
1197
1198out_abort:
1233
1234 /*
1235 * If the buffer is pinned then push on the log so we won't
1236 * get stuck waiting in the write for too long.
1237 */
1238 if (xfs_buf_ispinned(bp)) {
1239 trace_xfs_dqflush_force(dqp);
1240 xfs_log_force(mp, 0);
1241 }
1242
1243 trace_xfs_dqflush_done(dqp);
1244 *bpp = bp;
1245 return 0;
1246
1247out_abort:
1199 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1248 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
1200 xfs_trans_ail_delete(lip, 0);
1201 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1202out_unlock:
1203 xfs_dqfunlock(dqp);
1204 return error;
1205}
1206
1207/*

--- 4 unchanged lines hidden (view full) ---

1212 */
1213void
1214xfs_dqlock2(
1215 struct xfs_dquot *d1,
1216 struct xfs_dquot *d2)
1217{
1218 if (d1 && d2) {
1219 ASSERT(d1 != d2);
1249 xfs_trans_ail_delete(lip, 0);
1250 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1251out_unlock:
1252 xfs_dqfunlock(dqp);
1253 return error;
1254}
1255
1256/*

--- 4 unchanged lines hidden (view full) ---

1261 */
1262void
1263xfs_dqlock2(
1264 struct xfs_dquot *d1,
1265 struct xfs_dquot *d2)
1266{
1267 if (d1 && d2) {
1268 ASSERT(d1 != d2);
1220 if (be32_to_cpu(d1->q_core.d_id) >
1221 be32_to_cpu(d2->q_core.d_id)) {
1269 if (d1->q_id > d2->q_id) {
1222 mutex_lock(&d2->q_qlock);
1223 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1224 } else {
1225 mutex_lock(&d1->q_qlock);
1226 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1227 }
1228 } else if (d1) {
1229 mutex_lock(&d1->q_qlock);

--- 35 unchanged lines hidden (view full) ---

1265/*
1266 * Iterate every dquot of a particular type. The caller must ensure that the
1267 * particular quota type is active. iter_fn can return negative error codes,
1268 * or -ECANCELED to indicate that it wants to stop iterating.
1269 */
1270int
1271xfs_qm_dqiterate(
1272 struct xfs_mount *mp,
1270 mutex_lock(&d2->q_qlock);
1271 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1272 } else {
1273 mutex_lock(&d1->q_qlock);
1274 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1275 }
1276 } else if (d1) {
1277 mutex_lock(&d1->q_qlock);

--- 35 unchanged lines hidden (view full) ---

1313/*
1314 * Iterate every dquot of a particular type. The caller must ensure that the
1315 * particular quota type is active. iter_fn can return negative error codes,
1316 * or -ECANCELED to indicate that it wants to stop iterating.
1317 */
1318int
1319xfs_qm_dqiterate(
1320 struct xfs_mount *mp,
1273 uint dqtype,
1321 xfs_dqtype_t type,
1274 xfs_qm_dqiterate_fn iter_fn,
1275 void *priv)
1276{
1277 struct xfs_dquot *dq;
1278 xfs_dqid_t id = 0;
1279 int error;
1280
1281 do {
1322 xfs_qm_dqiterate_fn iter_fn,
1323 void *priv)
1324{
1325 struct xfs_dquot *dq;
1326 xfs_dqid_t id = 0;
1327 int error;
1328
1329 do {
1282 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1330 error = xfs_qm_dqget_next(mp, id, type, &dq);
1283 if (error == -ENOENT)
1284 return 0;
1285 if (error)
1286 return error;
1287
1331 if (error == -ENOENT)
1332 return 0;
1333 if (error)
1334 return error;
1335
1288 error = iter_fn(dq, dqtype, priv);
1289 id = be32_to_cpu(dq->q_core.d_id);
1336 error = iter_fn(dq, type, priv);
1337 id = dq->q_id;
1290 xfs_qm_dqput(dq);
1338 xfs_qm_dqput(dq);
1291 id++;
1292 } while (error == 0 && id != 0);
1293
1294 return error;
1295}
1339 } while (error == 0 && id != 0);
1340
1341 return error;
1342}