1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs_platform.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_shared.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_inode.h"
16 #include "xfs_bmap.h"
17 #include "xfs_quota.h"
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"
27 #include "xfs_health.h"
28
29 /*
30 * Lock order:
31 *
32 * ip->i_lock
33 * qi->qi_tree_lock
34 * dquot->q_qlock
35 * dquot->q_flush (xfs_dqflock() and friends)
36 * qi->qi_lru_lock
37 *
38 * If two dquots need to be locked the order is user before group/project,
39 * otherwise by the lowest id first, see xfs_dqlock2.
40 */
41
42 struct kmem_cache *xfs_dqtrx_cache;
43 static struct kmem_cache *xfs_dquot_cache;
44
45 static struct lock_class_key xfs_dquot_group_class;
46 static struct lock_class_key xfs_dquot_project_class;
47
48 /* Record observations of quota corruption with the health tracking system. */
49 static void
xfs_dquot_mark_sick(struct xfs_dquot * dqp)50 xfs_dquot_mark_sick(
51 struct xfs_dquot *dqp)
52 {
53 struct xfs_mount *mp = dqp->q_mount;
54
55 switch (dqp->q_type) {
56 case XFS_DQTYPE_USER:
57 xfs_fs_mark_sick(mp, XFS_SICK_FS_UQUOTA);
58 break;
59 case XFS_DQTYPE_GROUP:
60 xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA);
61 break;
62 case XFS_DQTYPE_PROJ:
63 xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA);
64 break;
65 default:
66 ASSERT(0);
67 break;
68 }
69 }
70
71 /*
72 * Detach the dquot buffer if it's still attached, because we can get called
73 * through dqpurge after a log shutdown. Caller must hold the dqflock or have
74 * otherwise isolated the dquot.
75 */
76 void
xfs_dquot_detach_buf(struct xfs_dquot * dqp)77 xfs_dquot_detach_buf(
78 struct xfs_dquot *dqp)
79 {
80 struct xfs_dq_logitem *qlip = &dqp->q_logitem;
81 struct xfs_buf *bp = NULL;
82
83 spin_lock(&qlip->qli_lock);
84 if (qlip->qli_item.li_buf) {
85 bp = qlip->qli_item.li_buf;
86 qlip->qli_item.li_buf = NULL;
87 }
88 spin_unlock(&qlip->qli_lock);
89 if (bp) {
90 xfs_buf_lock(bp);
91 list_del_init(&qlip->qli_item.li_bio_list);
92 xfs_buf_relse(bp);
93 }
94 }
95
96 /*
97 * This is called to free all the memory associated with a dquot
98 */
99 void
xfs_qm_dqdestroy(struct xfs_dquot * dqp)100 xfs_qm_dqdestroy(
101 struct xfs_dquot *dqp)
102 {
103 ASSERT(list_empty(&dqp->q_lru));
104 ASSERT(dqp->q_logitem.qli_item.li_buf == NULL);
105
106 kvfree(dqp->q_logitem.qli_item.li_lv_shadow);
107 mutex_destroy(&dqp->q_qlock);
108
109 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
110 kmem_cache_free(xfs_dquot_cache, dqp);
111 }
112
113 /*
114 * If default limits are in force, push them into the dquot now.
115 * We overwrite the dquot limits only if they are zero and this
116 * is not the root dquot.
117 */
118 void
xfs_qm_adjust_dqlimits(struct xfs_dquot * dq)119 xfs_qm_adjust_dqlimits(
120 struct xfs_dquot *dq)
121 {
122 struct xfs_mount *mp = dq->q_mount;
123 struct xfs_quotainfo *q = mp->m_quotainfo;
124 struct xfs_def_quota *defq;
125 int prealloc = 0;
126
127 ASSERT(dq->q_id);
128 defq = xfs_get_defquota(q, xfs_dquot_type(dq));
129
130 if (!dq->q_blk.softlimit) {
131 dq->q_blk.softlimit = defq->blk.soft;
132 prealloc = 1;
133 }
134 if (!dq->q_blk.hardlimit) {
135 dq->q_blk.hardlimit = defq->blk.hard;
136 prealloc = 1;
137 }
138 if (!dq->q_ino.softlimit)
139 dq->q_ino.softlimit = defq->ino.soft;
140 if (!dq->q_ino.hardlimit)
141 dq->q_ino.hardlimit = defq->ino.hard;
142 if (!dq->q_rtb.softlimit) {
143 dq->q_rtb.softlimit = defq->rtb.soft;
144 prealloc = 1;
145 }
146 if (!dq->q_rtb.hardlimit) {
147 dq->q_rtb.hardlimit = defq->rtb.hard;
148 prealloc = 1;
149 }
150
151 if (prealloc)
152 xfs_dquot_set_prealloc_limits(dq);
153 }
154
155 /* Set the expiration time of a quota's grace period. */
156 time64_t
xfs_dquot_set_timeout(struct xfs_mount * mp,time64_t timeout)157 xfs_dquot_set_timeout(
158 struct xfs_mount *mp,
159 time64_t timeout)
160 {
161 struct xfs_quotainfo *qi = mp->m_quotainfo;
162
163 return clamp_t(time64_t, timeout, qi->qi_expiry_min,
164 qi->qi_expiry_max);
165 }
166
167 /* Set the length of the default grace period. */
168 time64_t
xfs_dquot_set_grace_period(time64_t grace)169 xfs_dquot_set_grace_period(
170 time64_t grace)
171 {
172 return clamp_t(time64_t, grace, XFS_DQ_GRACE_MIN, XFS_DQ_GRACE_MAX);
173 }
174
175 /*
176 * Determine if this quota counter is over either limit and set the quota
177 * timers as appropriate.
178 */
179 static inline void
xfs_qm_adjust_res_timer(struct xfs_mount * mp,struct xfs_dquot_res * res,struct xfs_quota_limits * qlim)180 xfs_qm_adjust_res_timer(
181 struct xfs_mount *mp,
182 struct xfs_dquot_res *res,
183 struct xfs_quota_limits *qlim)
184 {
185 ASSERT(res->hardlimit == 0 || res->softlimit <= res->hardlimit);
186
187 if ((res->softlimit && res->count > res->softlimit) ||
188 (res->hardlimit && res->count > res->hardlimit)) {
189 if (res->timer == 0)
190 res->timer = xfs_dquot_set_timeout(mp,
191 ktime_get_real_seconds() + qlim->time);
192 } else {
193 res->timer = 0;
194 }
195 }
196
197 /*
198 * Check the limits and timers of a dquot and start or reset timers
199 * if necessary.
200 * This gets called even when quota enforcement is OFF, which makes our
201 * life a little less complicated. (We just don't reject any quota
202 * reservations in that case, when enforcement is off).
203 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
204 * enforcement's off.
205 * In contrast, warnings are a little different in that they don't
206 * 'automatically' get started when limits get exceeded. They do
207 * get reset to zero, however, when we find the count to be under
208 * the soft limit (they are only ever set non-zero via userspace).
209 */
210 void
xfs_qm_adjust_dqtimers(struct xfs_dquot * dq)211 xfs_qm_adjust_dqtimers(
212 struct xfs_dquot *dq)
213 {
214 struct xfs_mount *mp = dq->q_mount;
215 struct xfs_quotainfo *qi = mp->m_quotainfo;
216 struct xfs_def_quota *defq;
217
218 ASSERT(dq->q_id);
219 defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
220
221 xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_blk, &defq->blk);
222 xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_ino, &defq->ino);
223 xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_rtb, &defq->rtb);
224 }
225
226 /*
227 * initialize a buffer full of dquots and log the whole thing
228 */
229 void
xfs_qm_init_dquot_blk(struct xfs_trans * tp,xfs_dqid_t id,xfs_dqtype_t type,struct xfs_buf * bp)230 xfs_qm_init_dquot_blk(
231 struct xfs_trans *tp,
232 xfs_dqid_t id,
233 xfs_dqtype_t type,
234 struct xfs_buf *bp)
235 {
236 struct xfs_mount *mp = tp->t_mountp;
237 struct xfs_quotainfo *q = mp->m_quotainfo;
238 struct xfs_dqblk *d;
239 xfs_dqid_t curid;
240 unsigned int qflag;
241 unsigned int blftype;
242 int i;
243
244 ASSERT(tp);
245 ASSERT(xfs_buf_islocked(bp));
246
247 switch (type) {
248 case XFS_DQTYPE_USER:
249 qflag = XFS_UQUOTA_CHKD;
250 blftype = XFS_BLF_UDQUOT_BUF;
251 break;
252 case XFS_DQTYPE_PROJ:
253 qflag = XFS_PQUOTA_CHKD;
254 blftype = XFS_BLF_PDQUOT_BUF;
255 break;
256 case XFS_DQTYPE_GROUP:
257 qflag = XFS_GQUOTA_CHKD;
258 blftype = XFS_BLF_GDQUOT_BUF;
259 break;
260 default:
261 ASSERT(0);
262 return;
263 }
264
265 d = bp->b_addr;
266
267 /*
268 * ID of the first dquot in the block - id's are zero based.
269 */
270 curid = id - (id % q->qi_dqperchunk);
271 memset(d, 0, BBTOB(q->qi_dqchunklen));
272 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
273 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
274 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
275 d->dd_diskdq.d_id = cpu_to_be32(curid);
276 d->dd_diskdq.d_type = type;
277 if (curid > 0 && xfs_has_bigtime(mp))
278 d->dd_diskdq.d_type |= XFS_DQTYPE_BIGTIME;
279 if (xfs_has_crc(mp)) {
280 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
281 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
282 XFS_DQUOT_CRC_OFF);
283 }
284 }
285
286 xfs_trans_dquot_buf(tp, bp, blftype);
287
288 /*
289 * quotacheck uses delayed writes to update all the dquots on disk in an
290 * efficient manner instead of logging the individual dquot changes as
291 * they are made. However if we log the buffer allocated here and crash
292 * after quotacheck while the logged initialisation is still in the
293 * active region of the log, log recovery can replay the dquot buffer
294 * initialisation over the top of the checked dquots and corrupt quota
295 * accounting.
296 *
297 * To avoid this problem, quotacheck cannot log the initialised buffer.
298 * We must still dirty the buffer and write it back before the
299 * allocation transaction clears the log. Therefore, mark the buffer as
300 * ordered instead of logging it directly. This is safe for quotacheck
301 * because it detects and repairs allocated but initialized dquot blocks
302 * in the quota inodes.
303 */
304 if (!(mp->m_qflags & qflag))
305 xfs_trans_ordered_buf(tp, bp);
306 else
307 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
308 }
309
310 static void
xfs_dquot_set_prealloc(struct xfs_dquot_pre * pre,const struct xfs_dquot_res * res)311 xfs_dquot_set_prealloc(
312 struct xfs_dquot_pre *pre,
313 const struct xfs_dquot_res *res)
314 {
315 xfs_qcnt_t space;
316
317 pre->q_prealloc_hi_wmark = res->hardlimit;
318 pre->q_prealloc_lo_wmark = res->softlimit;
319
320 space = div_u64(pre->q_prealloc_hi_wmark, 100);
321 if (!pre->q_prealloc_lo_wmark)
322 pre->q_prealloc_lo_wmark = space * 95;
323
324 pre->q_low_space[XFS_QLOWSP_1_PCNT] = space;
325 pre->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
326 pre->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
327 }
328
329 /*
330 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
331 * watermarks correspond to the soft and hard limits by default. If a soft limit
332 * is not specified, we use 95% of the hard limit.
333 */
334 void
xfs_dquot_set_prealloc_limits(struct xfs_dquot * dqp)335 xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
336 {
337 xfs_dquot_set_prealloc(&dqp->q_blk_prealloc, &dqp->q_blk);
338 xfs_dquot_set_prealloc(&dqp->q_rtb_prealloc, &dqp->q_rtb);
339 }
340
341 /*
342 * Ensure that the given in-core dquot has a buffer on disk backing it, and
343 * return the buffer locked and held. This is called when the bmapi finds a
344 * hole.
345 */
346 STATIC int
xfs_dquot_disk_alloc(struct xfs_dquot * dqp,struct xfs_buf ** bpp)347 xfs_dquot_disk_alloc(
348 struct xfs_dquot *dqp,
349 struct xfs_buf **bpp)
350 {
351 struct xfs_bmbt_irec map;
352 struct xfs_trans *tp;
353 struct xfs_mount *mp = dqp->q_mount;
354 struct xfs_buf *bp;
355 xfs_dqtype_t qtype = xfs_dquot_type(dqp);
356 struct xfs_inode *quotip = xfs_quota_inode(mp, qtype);
357 int nmaps = 1;
358 int error;
359
360 trace_xfs_dqalloc(dqp);
361
362 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
363 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
364 if (error)
365 return error;
366
367 xfs_ilock(quotip, XFS_ILOCK_EXCL);
368 xfs_trans_ijoin(tp, quotip, 0);
369
370 if (!xfs_this_quota_on(dqp->q_mount, qtype)) {
371 /*
372 * Return if this type of quotas is turned off while we didn't
373 * have an inode lock
374 */
375 error = -ESRCH;
376 goto err_cancel;
377 }
378
379 error = xfs_iext_count_extend(tp, quotip, XFS_DATA_FORK,
380 XFS_IEXT_ADD_NOSPLIT_CNT);
381 if (error)
382 goto err_cancel;
383
384 /* Create the block mapping. */
385 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
386 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
387 &nmaps);
388 if (error)
389 goto err_cancel;
390
391 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
392 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
393 (map.br_startblock != HOLESTARTBLOCK));
394
395 /*
396 * Keep track of the blkno to save a lookup later
397 */
398 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
399
400 /* now we can just get the buffer (there's nothing to read yet) */
401 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
402 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
403 if (error)
404 goto err_cancel;
405 bp->b_ops = &xfs_dquot_buf_ops;
406
407 /*
408 * Make a chunk of dquots out of this buffer and log
409 * the entire thing.
410 */
411 xfs_qm_init_dquot_blk(tp, dqp->q_id, qtype, bp);
412 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
413
414 /*
415 * Hold the buffer and join it to the dfops so that we'll still own
416 * the buffer when we return to the caller. The buffer disposal on
417 * error must be paid attention to very carefully, as it has been
418 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
419 * code when allocating a new dquot record" in 2005, and the later
420 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
421 * the buffer locked across the _defer_finish call. We can now do
422 * this correctly with xfs_defer_bjoin.
423 *
424 * Above, we allocated a disk block for the dquot information and used
425 * get_buf to initialize the dquot. If the _defer_finish fails, the old
426 * transaction is gone but the new buffer is not joined or held to any
427 * transaction, so we must _buf_relse it.
428 *
429 * If everything succeeds, the caller of this function is returned a
430 * buffer that is locked and held to the transaction. The caller
431 * is responsible for unlocking any buffer passed back, either
432 * manually or by committing the transaction. On error, the buffer is
433 * released and not passed back.
434 *
435 * Keep the quota inode ILOCKed until after the transaction commit to
436 * maintain the atomicity of bmap/rmap updates.
437 */
438 xfs_trans_bhold(tp, bp);
439 error = xfs_trans_commit(tp);
440 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
441 if (error) {
442 xfs_buf_relse(bp);
443 return error;
444 }
445
446 *bpp = bp;
447 return 0;
448
449 err_cancel:
450 xfs_trans_cancel(tp);
451 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
452 return error;
453 }
454
455 /*
456 * Read in the in-core dquot's on-disk metadata and return the buffer.
457 * Returns ENOENT to signal a hole.
458 */
459 STATIC int
xfs_dquot_disk_read(struct xfs_mount * mp,struct xfs_dquot * dqp,struct xfs_buf ** bpp)460 xfs_dquot_disk_read(
461 struct xfs_mount *mp,
462 struct xfs_dquot *dqp,
463 struct xfs_buf **bpp)
464 {
465 struct xfs_bmbt_irec map;
466 struct xfs_buf *bp;
467 xfs_dqtype_t qtype = xfs_dquot_type(dqp);
468 struct xfs_inode *quotip = xfs_quota_inode(mp, qtype);
469 uint lock_mode;
470 int nmaps = 1;
471 int error;
472
473 lock_mode = xfs_ilock_data_map_shared(quotip);
474 if (!xfs_this_quota_on(mp, qtype)) {
475 /*
476 * Return if this type of quotas is turned off while we
477 * didn't have the quota inode lock.
478 */
479 xfs_iunlock(quotip, lock_mode);
480 return -ESRCH;
481 }
482
483 /*
484 * Find the block map; no allocations yet
485 */
486 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
487 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
488 xfs_iunlock(quotip, lock_mode);
489 if (error)
490 return error;
491
492 ASSERT(nmaps == 1);
493 ASSERT(map.br_blockcount >= 1);
494 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
495 if (map.br_startblock == HOLESTARTBLOCK)
496 return -ENOENT;
497
498 trace_xfs_dqtobp_read(dqp);
499
500 /*
501 * store the blkno etc so that we don't have to do the
502 * mapping all the time
503 */
504 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
505
506 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
507 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
508 &xfs_dquot_buf_ops);
509 if (xfs_metadata_is_sick(error))
510 xfs_dquot_mark_sick(dqp);
511 if (error) {
512 ASSERT(bp == NULL);
513 return error;
514 }
515
516 ASSERT(xfs_buf_islocked(bp));
517 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
518 *bpp = bp;
519
520 return 0;
521 }
522
523 /* Allocate and initialize everything we need for an incore dquot. */
524 STATIC struct xfs_dquot *
xfs_dquot_alloc(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type)525 xfs_dquot_alloc(
526 struct xfs_mount *mp,
527 xfs_dqid_t id,
528 xfs_dqtype_t type)
529 {
530 struct xfs_dquot *dqp;
531
532 dqp = kmem_cache_zalloc(xfs_dquot_cache, GFP_KERNEL | __GFP_NOFAIL);
533
534 dqp->q_type = type;
535 dqp->q_id = id;
536 dqp->q_mount = mp;
537 INIT_LIST_HEAD(&dqp->q_lru);
538 mutex_init(&dqp->q_qlock);
539 init_waitqueue_head(&dqp->q_pinwait);
540 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
541 /*
542 * Offset of dquot in the (fixed sized) dquot chunk.
543 */
544 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
545 sizeof(struct xfs_dqblk);
546
547 /*
548 * Because we want to use a counting completion, complete
549 * the flush completion once to allow a single access to
550 * the flush completion without blocking.
551 */
552 init_completion(&dqp->q_flush);
553 complete(&dqp->q_flush);
554
555 /*
556 * Make sure group quotas have a different lock class than user
557 * quotas.
558 */
559 switch (type) {
560 case XFS_DQTYPE_USER:
561 /* uses the default lock class */
562 break;
563 case XFS_DQTYPE_GROUP:
564 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
565 break;
566 case XFS_DQTYPE_PROJ:
567 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
568 break;
569 default:
570 ASSERT(0);
571 break;
572 }
573
574 xfs_qm_dquot_logitem_init(dqp);
575
576 XFS_STATS_INC(mp, xs_qm_dquot);
577 return dqp;
578 }
579
580 /* Check the ondisk dquot's id and type match what the incore dquot expects. */
581 static bool
xfs_dquot_check_type(struct xfs_dquot * dqp,struct xfs_disk_dquot * ddqp)582 xfs_dquot_check_type(
583 struct xfs_dquot *dqp,
584 struct xfs_disk_dquot *ddqp)
585 {
586 uint8_t ddqp_type;
587 uint8_t dqp_type;
588
589 ddqp_type = ddqp->d_type & XFS_DQTYPE_REC_MASK;
590 dqp_type = xfs_dquot_type(dqp);
591
592 if (be32_to_cpu(ddqp->d_id) != dqp->q_id)
593 return false;
594
595 /*
596 * V5 filesystems always expect an exact type match. V4 filesystems
597 * expect an exact match for user dquots and for non-root group and
598 * project dquots.
599 */
600 if (xfs_has_crc(dqp->q_mount) ||
601 dqp_type == XFS_DQTYPE_USER || dqp->q_id != 0)
602 return ddqp_type == dqp_type;
603
604 /*
605 * V4 filesystems support either group or project quotas, but not both
606 * at the same time. The non-user quota file can be switched between
607 * group and project quota uses depending on the mount options, which
608 * means that we can encounter the other type when we try to load quota
609 * defaults. Quotacheck will soon reset the entire quota file
610 * (including the root dquot) anyway, but don't log scary corruption
611 * reports to dmesg.
612 */
613 return ddqp_type == XFS_DQTYPE_GROUP || ddqp_type == XFS_DQTYPE_PROJ;
614 }
615
616 /* Copy the in-core quota fields in from the on-disk buffer. */
617 STATIC int
xfs_dquot_from_disk(struct xfs_dquot * dqp,struct xfs_buf * bp)618 xfs_dquot_from_disk(
619 struct xfs_dquot *dqp,
620 struct xfs_buf *bp)
621 {
622 struct xfs_dqblk *dqb = xfs_buf_offset(bp, dqp->q_bufoffset);
623 struct xfs_disk_dquot *ddqp = &dqb->dd_diskdq;
624
625 /*
626 * Ensure that we got the type and ID we were looking for.
627 * Everything else was checked by the dquot buffer verifier.
628 */
629 if (!xfs_dquot_check_type(dqp, ddqp)) {
630 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR,
631 "Metadata corruption detected at %pS, quota %u",
632 __this_address, dqp->q_id);
633 xfs_alert(bp->b_mount, "Unmount and run xfs_repair");
634 xfs_dquot_mark_sick(dqp);
635 return -EFSCORRUPTED;
636 }
637
638 /* copy everything from disk dquot to the incore dquot */
639 dqp->q_type = ddqp->d_type;
640 dqp->q_blk.hardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
641 dqp->q_blk.softlimit = be64_to_cpu(ddqp->d_blk_softlimit);
642 dqp->q_ino.hardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
643 dqp->q_ino.softlimit = be64_to_cpu(ddqp->d_ino_softlimit);
644 dqp->q_rtb.hardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
645 dqp->q_rtb.softlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
646
647 dqp->q_blk.count = be64_to_cpu(ddqp->d_bcount);
648 dqp->q_ino.count = be64_to_cpu(ddqp->d_icount);
649 dqp->q_rtb.count = be64_to_cpu(ddqp->d_rtbcount);
650
651 dqp->q_blk.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_btimer);
652 dqp->q_ino.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_itimer);
653 dqp->q_rtb.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_rtbtimer);
654
655 /*
656 * Reservation counters are defined as reservation plus current usage
657 * to avoid having to add every time.
658 */
659 dqp->q_blk.reserved = dqp->q_blk.count;
660 dqp->q_ino.reserved = dqp->q_ino.count;
661 dqp->q_rtb.reserved = dqp->q_rtb.count;
662
663 /* initialize the dquot speculative prealloc thresholds */
664 xfs_dquot_set_prealloc_limits(dqp);
665 return 0;
666 }
667
668 /* Copy the in-core quota fields into the on-disk buffer. */
669 void
xfs_dquot_to_disk(struct xfs_disk_dquot * ddqp,struct xfs_dquot * dqp)670 xfs_dquot_to_disk(
671 struct xfs_disk_dquot *ddqp,
672 struct xfs_dquot *dqp)
673 {
674 ddqp->d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
675 ddqp->d_version = XFS_DQUOT_VERSION;
676 ddqp->d_type = dqp->q_type;
677 ddqp->d_id = cpu_to_be32(dqp->q_id);
678 ddqp->d_pad0 = 0;
679 ddqp->d_pad = 0;
680
681 ddqp->d_blk_hardlimit = cpu_to_be64(dqp->q_blk.hardlimit);
682 ddqp->d_blk_softlimit = cpu_to_be64(dqp->q_blk.softlimit);
683 ddqp->d_ino_hardlimit = cpu_to_be64(dqp->q_ino.hardlimit);
684 ddqp->d_ino_softlimit = cpu_to_be64(dqp->q_ino.softlimit);
685 ddqp->d_rtb_hardlimit = cpu_to_be64(dqp->q_rtb.hardlimit);
686 ddqp->d_rtb_softlimit = cpu_to_be64(dqp->q_rtb.softlimit);
687
688 ddqp->d_bcount = cpu_to_be64(dqp->q_blk.count);
689 ddqp->d_icount = cpu_to_be64(dqp->q_ino.count);
690 ddqp->d_rtbcount = cpu_to_be64(dqp->q_rtb.count);
691
692 ddqp->d_bwarns = 0;
693 ddqp->d_iwarns = 0;
694 ddqp->d_rtbwarns = 0;
695
696 ddqp->d_btimer = xfs_dquot_to_disk_ts(dqp, dqp->q_blk.timer);
697 ddqp->d_itimer = xfs_dquot_to_disk_ts(dqp, dqp->q_ino.timer);
698 ddqp->d_rtbtimer = xfs_dquot_to_disk_ts(dqp, dqp->q_rtb.timer);
699 }
700
701 /*
702 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
703 * and release the buffer immediately. If @can_alloc is true, fill any
704 * holes in the on-disk metadata.
705 */
706 static int
xfs_qm_dqread(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type,bool can_alloc,struct xfs_dquot ** dqpp)707 xfs_qm_dqread(
708 struct xfs_mount *mp,
709 xfs_dqid_t id,
710 xfs_dqtype_t type,
711 bool can_alloc,
712 struct xfs_dquot **dqpp)
713 {
714 struct xfs_dquot *dqp;
715 struct xfs_buf *bp;
716 int error;
717
718 dqp = xfs_dquot_alloc(mp, id, type);
719 trace_xfs_dqread(dqp);
720
721 /* Try to read the buffer, allocating if necessary. */
722 error = xfs_dquot_disk_read(mp, dqp, &bp);
723 if (error == -ENOENT && can_alloc)
724 error = xfs_dquot_disk_alloc(dqp, &bp);
725 if (error)
726 goto err;
727
728 /*
729 * At this point we should have a clean locked buffer. Copy the data
730 * to the incore dquot and release the buffer since the incore dquot
731 * has its own locking protocol so we needn't tie up the buffer any
732 * further.
733 */
734 ASSERT(xfs_buf_islocked(bp));
735 error = xfs_dquot_from_disk(dqp, bp);
736 xfs_buf_relse(bp);
737 if (error)
738 goto err;
739
740 *dqpp = dqp;
741 return error;
742
743 err:
744 trace_xfs_dqread_fail(dqp);
745 xfs_qm_dqdestroy(dqp);
746 *dqpp = NULL;
747 return error;
748 }
749
750 /*
751 * Advance to the next id in the current chunk, or if at the
752 * end of the chunk, skip ahead to first id in next allocated chunk
753 * using the SEEK_DATA interface.
754 */
755 static int
xfs_dq_get_next_id(struct xfs_mount * mp,xfs_dqtype_t type,xfs_dqid_t * id)756 xfs_dq_get_next_id(
757 struct xfs_mount *mp,
758 xfs_dqtype_t type,
759 xfs_dqid_t *id)
760 {
761 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
762 xfs_dqid_t next_id = *id + 1; /* simple advance */
763 uint lock_flags;
764 struct xfs_bmbt_irec got;
765 struct xfs_iext_cursor cur;
766 xfs_fsblock_t start;
767 int error = 0;
768
769 /* If we'd wrap past the max ID, stop */
770 if (next_id < *id)
771 return -ENOENT;
772
773 /* If new ID is within the current chunk, advancing it sufficed */
774 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
775 *id = next_id;
776 return 0;
777 }
778
779 /* Nope, next_id is now past the current chunk, so find the next one */
780 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
781
782 lock_flags = xfs_ilock_data_map_shared(quotip);
783 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
784 if (error)
785 goto out_unlock;
786
787 if (xfs_iext_lookup_extent(quotip, "ip->i_df, start, &cur, &got)) {
788 /* contiguous chunk, bump startoff for the id calculation */
789 if (got.br_startoff < start)
790 got.br_startoff = start;
791 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
792 } else {
793 error = -ENOENT;
794 }
795
796 out_unlock:
797 xfs_iunlock(quotip, lock_flags);
798
799 return error;
800 }
801
802 /*
803 * Look up the dquot in the in-core cache. If found, the dquot is returned
804 * locked and ready to go.
805 */
806 static struct xfs_dquot *
xfs_qm_dqget_cache_lookup(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type)807 xfs_qm_dqget_cache_lookup(
808 struct xfs_mount *mp,
809 xfs_dqid_t id,
810 xfs_dqtype_t type)
811 {
812 struct xfs_quotainfo *qi = mp->m_quotainfo;
813 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
814 struct xfs_dquot *dqp;
815
816 restart:
817 mutex_lock(&qi->qi_tree_lock);
818 dqp = radix_tree_lookup(tree, id);
819 if (!dqp) {
820 mutex_unlock(&qi->qi_tree_lock);
821 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
822 return NULL;
823 }
824
825 if (!lockref_get_not_dead(&dqp->q_lockref)) {
826 mutex_unlock(&qi->qi_tree_lock);
827 trace_xfs_dqget_freeing(dqp);
828 delay(1);
829 goto restart;
830 }
831 mutex_unlock(&qi->qi_tree_lock);
832
833 trace_xfs_dqget_hit(dqp);
834 XFS_STATS_INC(mp, xs_qm_dqcachehits);
835 return dqp;
836 }
837
838 /*
839 * Try to insert a new dquot into the in-core cache. If an error occurs the
840 * caller should throw away the dquot and start over. Otherwise, the dquot
841 * is returned (and held by the cache) as if there had been a cache hit.
842 *
843 * The insert needs to be done under memalloc_nofs context because the radix
844 * tree can do memory allocation during insert. The qi->qi_tree_lock is taken in
845 * memory reclaim when freeing unused dquots, so we cannot have the radix tree
846 * node allocation recursing into filesystem reclaim whilst we hold the
847 * qi_tree_lock.
848 */
849 static int
xfs_qm_dqget_cache_insert(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type,struct xfs_dquot * dqp)850 xfs_qm_dqget_cache_insert(
851 struct xfs_mount *mp,
852 xfs_dqid_t id,
853 xfs_dqtype_t type,
854 struct xfs_dquot *dqp)
855 {
856 struct xfs_quotainfo *qi = mp->m_quotainfo;
857 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
858 unsigned int nofs_flags;
859 int error;
860
861 nofs_flags = memalloc_nofs_save();
862 mutex_lock(&qi->qi_tree_lock);
863 error = radix_tree_insert(tree, id, dqp);
864 if (unlikely(error)) {
865 trace_xfs_dqget_dup(dqp);
866 goto out_unlock;
867 }
868
869 lockref_init(&dqp->q_lockref);
870 qi->qi_dquots++;
871
872 out_unlock:
873 mutex_unlock(&qi->qi_tree_lock);
874 memalloc_nofs_restore(nofs_flags);
875 return error;
876 }
877
878 /* Check our input parameters. */
879 static int
xfs_qm_dqget_checks(struct xfs_mount * mp,xfs_dqtype_t type)880 xfs_qm_dqget_checks(
881 struct xfs_mount *mp,
882 xfs_dqtype_t type)
883 {
884 switch (type) {
885 case XFS_DQTYPE_USER:
886 if (!XFS_IS_UQUOTA_ON(mp))
887 return -ESRCH;
888 return 0;
889 case XFS_DQTYPE_GROUP:
890 if (!XFS_IS_GQUOTA_ON(mp))
891 return -ESRCH;
892 return 0;
893 case XFS_DQTYPE_PROJ:
894 if (!XFS_IS_PQUOTA_ON(mp))
895 return -ESRCH;
896 return 0;
897 default:
898 WARN_ON_ONCE(0);
899 return -EINVAL;
900 }
901 }
902
903 /*
904 * Given the file system, id, and type (UDQUOT/GDQUOT/PDQUOT), return a
905 * dquot, doing an allocation (if requested) as needed.
906 */
907 int
xfs_qm_dqget(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type,bool can_alloc,struct xfs_dquot ** O_dqpp)908 xfs_qm_dqget(
909 struct xfs_mount *mp,
910 xfs_dqid_t id,
911 xfs_dqtype_t type,
912 bool can_alloc,
913 struct xfs_dquot **O_dqpp)
914 {
915 struct xfs_dquot *dqp;
916 int error;
917
918 error = xfs_qm_dqget_checks(mp, type);
919 if (error)
920 return error;
921
922 restart:
923 dqp = xfs_qm_dqget_cache_lookup(mp, id, type);
924 if (dqp)
925 goto found;
926
927 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
928 if (error)
929 return error;
930
931 error = xfs_qm_dqget_cache_insert(mp, id, type, dqp);
932 if (error) {
933 xfs_qm_dqdestroy(dqp);
934 if (error == -EEXIST) {
935 /*
936 * Duplicate found. Just throw away the new dquot and
937 * start over.
938 */
939 XFS_STATS_INC(mp, xs_qm_dquot_dups);
940 goto restart;
941 }
942 return error;
943 }
944
945 trace_xfs_dqget_miss(dqp);
946 found:
947 *O_dqpp = dqp;
948 return 0;
949 }
950
951 /*
952 * Given a dquot id and type, read and initialize a dquot from the on-disk
953 * metadata. This function is only for use during quota initialization so
954 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
955 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
956 */
957 int
xfs_qm_dqget_uncached(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type,struct xfs_dquot ** dqpp)958 xfs_qm_dqget_uncached(
959 struct xfs_mount *mp,
960 xfs_dqid_t id,
961 xfs_dqtype_t type,
962 struct xfs_dquot **dqpp)
963 {
964 int error;
965
966 error = xfs_qm_dqget_checks(mp, type);
967 if (error)
968 return error;
969
970 return xfs_qm_dqread(mp, id, type, 0, dqpp);
971 }
972
973 /* Return the quota id for a given inode and type. */
974 xfs_dqid_t
xfs_qm_id_for_quotatype(struct xfs_inode * ip,xfs_dqtype_t type)975 xfs_qm_id_for_quotatype(
976 struct xfs_inode *ip,
977 xfs_dqtype_t type)
978 {
979 switch (type) {
980 case XFS_DQTYPE_USER:
981 return i_uid_read(VFS_I(ip));
982 case XFS_DQTYPE_GROUP:
983 return i_gid_read(VFS_I(ip));
984 case XFS_DQTYPE_PROJ:
985 return ip->i_projid;
986 }
987 ASSERT(0);
988 return 0;
989 }
990
991 /*
992 * Return the dquot for a given inode and type. If @can_alloc is true, then
993 * allocate blocks if needed. The inode's ILOCK must be held and it must not
994 * have already had an inode attached.
995 */
996 int
xfs_qm_dqget_inode(struct xfs_inode * ip,xfs_dqtype_t type,bool can_alloc,struct xfs_dquot ** dqpp)997 xfs_qm_dqget_inode(
998 struct xfs_inode *ip,
999 xfs_dqtype_t type,
1000 bool can_alloc,
1001 struct xfs_dquot **dqpp)
1002 {
1003 struct xfs_mount *mp = ip->i_mount;
1004 struct xfs_dquot *dqp;
1005 xfs_dqid_t id;
1006 int error;
1007
1008 ASSERT(!*dqpp);
1009 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1010
1011 error = xfs_qm_dqget_checks(mp, type);
1012 if (error)
1013 return error;
1014
1015 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1016 ASSERT(xfs_inode_dquot(ip, type) == NULL);
1017 ASSERT(!xfs_is_metadir_inode(ip));
1018
1019 id = xfs_qm_id_for_quotatype(ip, type);
1020
1021 restart:
1022 dqp = xfs_qm_dqget_cache_lookup(mp, id, type);
1023 if (dqp)
1024 goto found;
1025
1026 /*
1027 * Dquot cache miss. We don't want to keep the inode lock across
1028 * a (potential) disk read. Also we don't want to deal with the lock
1029 * ordering between quotainode and this inode. OTOH, dropping the inode
1030 * lock here means dealing with a chown that can happen before
1031 * we re-acquire the lock.
1032 */
1033 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1034 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
1035 xfs_ilock(ip, XFS_ILOCK_EXCL);
1036 if (error)
1037 return error;
1038
1039 /*
1040 * A dquot could be attached to this inode by now, since we had
1041 * dropped the ilock.
1042 */
1043 if (xfs_this_quota_on(mp, type)) {
1044 struct xfs_dquot *dqp1;
1045
1046 dqp1 = xfs_inode_dquot(ip, type);
1047 if (dqp1) {
1048 xfs_qm_dqdestroy(dqp);
1049 dqp = dqp1;
1050 goto dqret;
1051 }
1052 } else {
1053 /* inode stays locked on return */
1054 xfs_qm_dqdestroy(dqp);
1055 return -ESRCH;
1056 }
1057
1058 error = xfs_qm_dqget_cache_insert(mp, id, type, dqp);
1059 if (error) {
1060 xfs_qm_dqdestroy(dqp);
1061 if (error == -EEXIST) {
1062 /*
1063 * Duplicate found. Just throw away the new dquot and
1064 * start over.
1065 */
1066 XFS_STATS_INC(mp, xs_qm_dquot_dups);
1067 goto restart;
1068 }
1069 return error;
1070 }
1071
1072 dqret:
1073 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1074 trace_xfs_dqget_miss(dqp);
1075 found:
1076 trace_xfs_dqattach_get(dqp);
1077 *dqpp = dqp;
1078 return 0;
1079 }
1080
1081 /*
1082 * Starting at @id and progressing upwards, look for an initialized incore
1083 * dquot, lock it, and return it.
1084 */
1085 int
xfs_qm_dqget_next(struct xfs_mount * mp,xfs_dqid_t id,xfs_dqtype_t type,struct xfs_dquot ** dqpp)1086 xfs_qm_dqget_next(
1087 struct xfs_mount *mp,
1088 xfs_dqid_t id,
1089 xfs_dqtype_t type,
1090 struct xfs_dquot **dqpp)
1091 {
1092 struct xfs_dquot *dqp;
1093 int error = 0;
1094
1095 *dqpp = NULL;
1096 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
1097 error = xfs_qm_dqget(mp, id, type, false, &dqp);
1098 if (error == -ENOENT)
1099 continue;
1100 else if (error != 0)
1101 break;
1102
1103 mutex_lock(&dqp->q_qlock);
1104 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
1105 *dqpp = dqp;
1106 return 0;
1107 }
1108
1109 mutex_unlock(&dqp->q_qlock);
1110 xfs_qm_dqrele(dqp);
1111 }
1112
1113 return error;
1114 }
1115
1116 /*
1117 * Release a reference to the dquot.
1118 */
1119 void
xfs_qm_dqrele(struct xfs_dquot * dqp)1120 xfs_qm_dqrele(
1121 struct xfs_dquot *dqp)
1122 {
1123 if (!dqp)
1124 return;
1125
1126 trace_xfs_dqrele(dqp);
1127
1128 if (lockref_put_or_lock(&dqp->q_lockref))
1129 return;
1130 if (!--dqp->q_lockref.count) {
1131 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1132
1133 trace_xfs_dqrele_free(dqp);
1134 if (list_lru_add_obj(&qi->qi_lru, &dqp->q_lru))
1135 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
1136 }
1137 spin_unlock(&dqp->q_lockref.lock);
1138 }
1139
1140 /*
1141 * This is the dquot flushing I/O completion routine. It is called
1142 * from interrupt level when the buffer containing the dquot is
1143 * flushed to disk. It is responsible for removing the dquot logitem
1144 * from the AIL if it has not been re-logged, and unlocking the dquot's
1145 * flush lock. This behavior is very similar to that of inodes..
1146 */
1147 static void
xfs_qm_dqflush_done(struct xfs_log_item * lip)1148 xfs_qm_dqflush_done(
1149 struct xfs_log_item *lip)
1150 {
1151 struct xfs_dq_logitem *qlip =
1152 container_of(lip, struct xfs_dq_logitem, qli_item);
1153 struct xfs_dquot *dqp = qlip->qli_dquot;
1154 struct xfs_ail *ailp = lip->li_ailp;
1155 struct xfs_buf *bp = NULL;
1156 xfs_lsn_t tail_lsn;
1157
1158 /*
1159 * We only want to pull the item from the AIL if its
1160 * location in the log has not changed since we started the flush.
1161 * Thus, we only bother if the dquot's lsn has
1162 * not changed. First we check the lsn outside the lock
1163 * since it's cheaper, and then we recheck while
1164 * holding the lock before removing the dquot from the AIL.
1165 */
1166 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
1167 (lip->li_lsn == qlip->qli_flush_lsn ||
1168 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
1169 spin_lock(&ailp->ail_lock);
1170 clear_bit(XFS_LI_FAILED, &lip->li_flags);
1171 if (lip->li_lsn == qlip->qli_flush_lsn) {
1172 /* xfs_ail_update_finish() drops the AIL lock */
1173 tail_lsn = xfs_ail_delete_one(ailp, lip);
1174 xfs_ail_update_finish(ailp, tail_lsn);
1175 } else {
1176 spin_unlock(&ailp->ail_lock);
1177 }
1178 }
1179
1180 /*
1181 * If this dquot hasn't been dirtied since initiating the last dqflush,
1182 * release the buffer reference. We already unlinked this dquot item
1183 * from the buffer.
1184 */
1185 spin_lock(&qlip->qli_lock);
1186 if (!qlip->qli_dirty) {
1187 bp = lip->li_buf;
1188 lip->li_buf = NULL;
1189 }
1190 spin_unlock(&qlip->qli_lock);
1191 if (bp)
1192 xfs_buf_rele(bp);
1193
1194 /*
1195 * Release the dq's flush lock since we're done with it.
1196 */
1197 xfs_dqfunlock(dqp);
1198 }
1199
1200 void
xfs_buf_dquot_iodone(struct xfs_buf * bp)1201 xfs_buf_dquot_iodone(
1202 struct xfs_buf *bp)
1203 {
1204 struct xfs_log_item *lip, *n;
1205
1206 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1207 list_del_init(&lip->li_bio_list);
1208 xfs_qm_dqflush_done(lip);
1209 }
1210 }
1211
1212 /* Check incore dquot for errors before we flush. */
1213 static xfs_failaddr_t
xfs_qm_dqflush_check(struct xfs_dquot * dqp)1214 xfs_qm_dqflush_check(
1215 struct xfs_dquot *dqp)
1216 {
1217 xfs_dqtype_t type = xfs_dquot_type(dqp);
1218
1219 if (type != XFS_DQTYPE_USER &&
1220 type != XFS_DQTYPE_GROUP &&
1221 type != XFS_DQTYPE_PROJ)
1222 return __this_address;
1223
1224 /* bigtime flag should never be set on root dquots */
1225 if (dqp->q_type & XFS_DQTYPE_BIGTIME) {
1226 if (!xfs_has_bigtime(dqp->q_mount))
1227 return __this_address;
1228 if (dqp->q_id == 0)
1229 return __this_address;
1230 }
1231
1232 if (dqp->q_id == 0)
1233 return NULL;
1234
1235 if (dqp->q_blk.softlimit && dqp->q_blk.count > dqp->q_blk.softlimit &&
1236 !dqp->q_blk.timer)
1237 return __this_address;
1238
1239 if (dqp->q_ino.softlimit && dqp->q_ino.count > dqp->q_ino.softlimit &&
1240 !dqp->q_ino.timer)
1241 return __this_address;
1242
1243 if (dqp->q_rtb.softlimit && dqp->q_rtb.count > dqp->q_rtb.softlimit &&
1244 !dqp->q_rtb.timer)
1245 return __this_address;
1246
1247 return NULL;
1248 }
1249
1250 /*
1251 * Get the buffer containing the on-disk dquot.
1252 *
1253 * Requires dquot flush lock, will clear the dirty flag, delete the quota log
1254 * item from the AIL, and shut down the system if something goes wrong.
1255 */
1256 static int
xfs_dquot_read_buf(struct xfs_trans * tp,struct xfs_dquot * dqp,struct xfs_buf ** bpp)1257 xfs_dquot_read_buf(
1258 struct xfs_trans *tp,
1259 struct xfs_dquot *dqp,
1260 struct xfs_buf **bpp)
1261 {
1262 struct xfs_mount *mp = dqp->q_mount;
1263 struct xfs_buf *bp = NULL;
1264 int error;
1265
1266 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, dqp->q_blkno,
1267 mp->m_quotainfo->qi_dqchunklen, 0,
1268 &bp, &xfs_dquot_buf_ops);
1269 if (xfs_metadata_is_sick(error))
1270 xfs_dquot_mark_sick(dqp);
1271 if (error)
1272 goto out_abort;
1273
1274 *bpp = bp;
1275 return 0;
1276
1277 out_abort:
1278 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
1279 xfs_trans_ail_delete(&dqp->q_logitem.qli_item, 0);
1280 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1281 return error;
1282 }
1283
1284 /*
1285 * Attach a dquot buffer to this dquot to avoid allocating a buffer during a
1286 * dqflush, since dqflush can be called from reclaim context. Caller must hold
1287 * the dqlock.
1288 */
1289 int
xfs_dquot_attach_buf(struct xfs_trans * tp,struct xfs_dquot * dqp)1290 xfs_dquot_attach_buf(
1291 struct xfs_trans *tp,
1292 struct xfs_dquot *dqp)
1293 {
1294 struct xfs_dq_logitem *qlip = &dqp->q_logitem;
1295 struct xfs_log_item *lip = &qlip->qli_item;
1296 int error;
1297
1298 spin_lock(&qlip->qli_lock);
1299 if (!lip->li_buf) {
1300 struct xfs_buf *bp = NULL;
1301
1302 spin_unlock(&qlip->qli_lock);
1303 error = xfs_dquot_read_buf(tp, dqp, &bp);
1304 if (error)
1305 return error;
1306
1307 /*
1308 * Hold the dquot buffer so that we retain our ref to it after
1309 * detaching it from the transaction, then give that ref to the
1310 * dquot log item so that the AIL does not have to read the
1311 * dquot buffer to push this item.
1312 */
1313 xfs_buf_hold(bp);
1314 xfs_trans_brelse(tp, bp);
1315
1316 spin_lock(&qlip->qli_lock);
1317 lip->li_buf = bp;
1318 }
1319 qlip->qli_dirty = true;
1320 spin_unlock(&qlip->qli_lock);
1321
1322 return 0;
1323 }
1324
1325 /*
1326 * Get a new reference the dquot buffer attached to this dquot for a dqflush
1327 * operation.
1328 *
1329 * Returns 0 and a NULL bp if none was attached to the dquot; 0 and a locked
1330 * bp; or -EAGAIN if the buffer could not be locked.
1331 */
1332 int
xfs_dquot_use_attached_buf(struct xfs_dquot * dqp,struct xfs_buf ** bpp)1333 xfs_dquot_use_attached_buf(
1334 struct xfs_dquot *dqp,
1335 struct xfs_buf **bpp)
1336 {
1337 struct xfs_buf *bp = dqp->q_logitem.qli_item.li_buf;
1338
1339 /*
1340 * A NULL buffer can happen if the dquot dirty flag was set but the
1341 * filesystem shut down before transaction commit happened. In that
1342 * case we're not going to flush anyway.
1343 */
1344 if (!bp) {
1345 ASSERT(xfs_is_shutdown(dqp->q_mount));
1346
1347 *bpp = NULL;
1348 return 0;
1349 }
1350
1351 if (!xfs_buf_trylock(bp))
1352 return -EAGAIN;
1353
1354 xfs_buf_hold(bp);
1355 *bpp = bp;
1356 return 0;
1357 }
1358
1359 /*
1360 * Write a modified dquot to disk.
1361 * The dquot must be locked and the flush lock too taken by caller.
1362 * The flush lock will not be unlocked until the dquot reaches the disk,
1363 * but the dquot is free to be unlocked and modified by the caller
1364 * in the interim. Dquot is still locked on return. This behavior is
1365 * identical to that of inodes.
1366 */
1367 int
xfs_qm_dqflush(struct xfs_dquot * dqp,struct xfs_buf * bp)1368 xfs_qm_dqflush(
1369 struct xfs_dquot *dqp,
1370 struct xfs_buf *bp)
1371 {
1372 struct xfs_mount *mp = dqp->q_mount;
1373 struct xfs_dq_logitem *qlip = &dqp->q_logitem;
1374 struct xfs_log_item *lip = &qlip->qli_item;
1375 struct xfs_dqblk *dqblk;
1376 xfs_failaddr_t fa;
1377 int error;
1378
1379 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1380 ASSERT(!completion_done(&dqp->q_flush));
1381 ASSERT(atomic_read(&dqp->q_pincount) == 0);
1382
1383 trace_xfs_dqflush(dqp);
1384 fa = xfs_qm_dqflush_check(dqp);
1385 if (fa) {
1386 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1387 dqp->q_id, fa);
1388 xfs_dquot_mark_sick(dqp);
1389 error = -EFSCORRUPTED;
1390 goto out_abort;
1391 }
1392
1393 /* Flush the incore dquot to the ondisk buffer. */
1394 dqblk = xfs_buf_offset(bp, dqp->q_bufoffset);
1395 xfs_dquot_to_disk(&dqblk->dd_diskdq, dqp);
1396
1397 /*
1398 * Clear the dirty field and remember the flush lsn for later use.
1399 */
1400 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
1401
1402 /*
1403 * We hold the dquot lock, so nobody can dirty it while we're
1404 * scheduling the write out. Clear the dirty-since-flush flag.
1405 */
1406 spin_lock(&qlip->qli_lock);
1407 qlip->qli_dirty = false;
1408 spin_unlock(&qlip->qli_lock);
1409
1410 xfs_trans_ail_copy_lsn(mp->m_ail, &qlip->qli_flush_lsn, &lip->li_lsn);
1411
1412 /*
1413 * copy the lsn into the on-disk dquot now while we have the in memory
1414 * dquot here. This can't be done later in the write verifier as we
1415 * can't get access to the log item at that point in time.
1416 *
1417 * We also calculate the CRC here so that the on-disk dquot in the
1418 * buffer always has a valid CRC. This ensures there is no possibility
1419 * of a dquot without an up-to-date CRC getting to disk.
1420 */
1421 if (xfs_has_crc(mp)) {
1422 dqblk->dd_lsn = cpu_to_be64(lip->li_lsn);
1423 xfs_update_cksum((char *)dqblk, sizeof(struct xfs_dqblk),
1424 XFS_DQUOT_CRC_OFF);
1425 }
1426
1427 /*
1428 * Attach the dquot to the buffer so that we can remove this dquot from
1429 * the AIL and release the flush lock once the dquot is synced to disk.
1430 */
1431 bp->b_iodone = xfs_buf_dquot_iodone;
1432 list_add_tail(&lip->li_bio_list, &bp->b_li_list);
1433
1434 /*
1435 * If the buffer is pinned then push on the log so we won't
1436 * get stuck waiting in the write for too long.
1437 */
1438 if (xfs_buf_ispinned(bp)) {
1439 trace_xfs_dqflush_force(dqp);
1440 xfs_log_force(mp, 0);
1441 }
1442
1443 trace_xfs_dqflush_done(dqp);
1444 return 0;
1445
1446 out_abort:
1447 /*
1448 * Shut down the log before removing the dquot item from the AIL.
1449 * Otherwise, the log tail may advance past this item's LSN while
1450 * log writes are still in progress, making these unflushed changes
1451 * unrecoverable on the next mount.
1452 */
1453 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1454 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
1455 xfs_trans_ail_delete(lip, 0);
1456 xfs_dqfunlock(dqp);
1457 return error;
1458 }
1459
1460 /*
1461 * Lock two xfs_dquot structures.
1462 *
1463 * To avoid deadlocks we always lock the quota structure with
1464 * the lowerd id first.
1465 */
1466 void
xfs_dqlock2(struct xfs_dquot * d1,struct xfs_dquot * d2)1467 xfs_dqlock2(
1468 struct xfs_dquot *d1,
1469 struct xfs_dquot *d2)
1470 {
1471 if (d1 && d2) {
1472 ASSERT(d1 != d2);
1473 if (d1->q_id > d2->q_id) {
1474 mutex_lock(&d2->q_qlock);
1475 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1476 } else {
1477 mutex_lock(&d1->q_qlock);
1478 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1479 }
1480 } else if (d1) {
1481 mutex_lock(&d1->q_qlock);
1482 } else if (d2) {
1483 mutex_lock(&d2->q_qlock);
1484 }
1485 }
1486
1487 static int
xfs_dqtrx_cmp(const void * a,const void * b)1488 xfs_dqtrx_cmp(
1489 const void *a,
1490 const void *b)
1491 {
1492 const struct xfs_dqtrx *qa = a;
1493 const struct xfs_dqtrx *qb = b;
1494
1495 if (qa->qt_dquot->q_id > qb->qt_dquot->q_id)
1496 return 1;
1497 if (qa->qt_dquot->q_id < qb->qt_dquot->q_id)
1498 return -1;
1499 return 0;
1500 }
1501
1502 void
xfs_dqlockn(struct xfs_dqtrx * q)1503 xfs_dqlockn(
1504 struct xfs_dqtrx *q)
1505 {
1506 unsigned int i;
1507
1508 BUILD_BUG_ON(XFS_QM_TRANS_MAXDQS > MAX_LOCKDEP_SUBCLASSES);
1509
1510 /* Sort in order of dquot id, do not allow duplicates */
1511 for (i = 0; i < XFS_QM_TRANS_MAXDQS && q[i].qt_dquot != NULL; i++) {
1512 unsigned int j;
1513
1514 for (j = 0; j < i; j++)
1515 ASSERT(q[i].qt_dquot != q[j].qt_dquot);
1516 }
1517 if (i == 0)
1518 return;
1519
1520 sort(q, i, sizeof(struct xfs_dqtrx), xfs_dqtrx_cmp, NULL);
1521
1522 mutex_lock(&q[0].qt_dquot->q_qlock);
1523 for (i = 1; i < XFS_QM_TRANS_MAXDQS && q[i].qt_dquot != NULL; i++)
1524 mutex_lock_nested(&q[i].qt_dquot->q_qlock,
1525 XFS_QLOCK_NESTED + i - 1);
1526 }
1527
1528 int __init
xfs_qm_init(void)1529 xfs_qm_init(void)
1530 {
1531 xfs_dquot_cache = kmem_cache_create("xfs_dquot",
1532 sizeof(struct xfs_dquot),
1533 0, 0, NULL);
1534 if (!xfs_dquot_cache)
1535 goto out;
1536
1537 xfs_dqtrx_cache = kmem_cache_create("xfs_dqtrx",
1538 sizeof(struct xfs_dquot_acct),
1539 0, 0, NULL);
1540 if (!xfs_dqtrx_cache)
1541 goto out_free_dquot_cache;
1542
1543 return 0;
1544
1545 out_free_dquot_cache:
1546 kmem_cache_destroy(xfs_dquot_cache);
1547 out:
1548 return -ENOMEM;
1549 }
1550
1551 void
xfs_qm_exit(void)1552 xfs_qm_exit(void)
1553 {
1554 kmem_cache_destroy(xfs_dqtrx_cache);
1555 kmem_cache_destroy(xfs_dquot_cache);
1556 }
1557