1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_shared.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_inode.h"
17 #include "xfs_bmap_util.h"
18 #include "xfs_alloc.h"
19 #include "xfs_quota.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_buf_item.h"
23 #include "xfs_trans_space.h"
24 #include "xfs_trans_priv.h"
26 #include "xfs_cksum.h"
27 #include "xfs_trace.h"
29 #include "xfs_bmap_btree.h"
36 * dquot->q_qlock (xfs_dqlock() and friends)
37 * dquot->q_flush (xfs_dqflock() and friends)
40 * If two dquots need to be locked the order is user before group/project,
41 * otherwise by the lowest id first, see xfs_dqlock2.
44 struct kmem_zone *xfs_qm_dqtrxzone;
45 static struct kmem_zone *xfs_qm_dqzone;
47 static struct lock_class_key xfs_dquot_group_class;
48 static struct lock_class_key xfs_dquot_project_class;
51 * This is called to free all the memory associated with a dquot
57 ASSERT(list_empty(&dqp->q_lru));
59 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
60 mutex_destroy(&dqp->q_qlock);
62 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
63 kmem_zone_free(xfs_qm_dqzone, dqp);
67 * If default limits are in force, push them into the dquot now.
68 * We overwrite the dquot limits only if they are zero and this
69 * is not the root dquot.
72 xfs_qm_adjust_dqlimits(
76 struct xfs_quotainfo *q = mp->m_quotainfo;
77 struct xfs_disk_dquot *d = &dq->q_core;
78 struct xfs_def_quota *defq;
82 defq = xfs_get_defquota(dq, q);
84 if (defq->bsoftlimit && !d->d_blk_softlimit) {
85 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
88 if (defq->bhardlimit && !d->d_blk_hardlimit) {
89 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
92 if (defq->isoftlimit && !d->d_ino_softlimit)
93 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
94 if (defq->ihardlimit && !d->d_ino_hardlimit)
95 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
96 if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
97 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
98 if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
99 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
102 xfs_dquot_set_prealloc_limits(dq);
106 * Check the limits and timers of a dquot and start or reset timers
108 * This gets called even when quota enforcement is OFF, which makes our
109 * life a little less complicated. (We just don't reject any quota
110 * reservations in that case, when enforcement is off).
111 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
113 * In contrast, warnings are a little different in that they don't
114 * 'automatically' get started when limits get exceeded. They do
115 * get reset to zero, however, when we find the count to be under
116 * the soft limit (they are only ever set non-zero via userspace).
119 xfs_qm_adjust_dqtimers(
126 if (d->d_blk_hardlimit)
127 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
128 be64_to_cpu(d->d_blk_hardlimit));
129 if (d->d_ino_hardlimit)
130 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
131 be64_to_cpu(d->d_ino_hardlimit));
132 if (d->d_rtb_hardlimit)
133 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
134 be64_to_cpu(d->d_rtb_hardlimit));
138 if ((d->d_blk_softlimit &&
139 (be64_to_cpu(d->d_bcount) >
140 be64_to_cpu(d->d_blk_softlimit))) ||
141 (d->d_blk_hardlimit &&
142 (be64_to_cpu(d->d_bcount) >
143 be64_to_cpu(d->d_blk_hardlimit)))) {
144 d->d_btimer = cpu_to_be32(get_seconds() +
145 mp->m_quotainfo->qi_btimelimit);
150 if ((!d->d_blk_softlimit ||
151 (be64_to_cpu(d->d_bcount) <=
152 be64_to_cpu(d->d_blk_softlimit))) &&
153 (!d->d_blk_hardlimit ||
154 (be64_to_cpu(d->d_bcount) <=
155 be64_to_cpu(d->d_blk_hardlimit)))) {
161 if ((d->d_ino_softlimit &&
162 (be64_to_cpu(d->d_icount) >
163 be64_to_cpu(d->d_ino_softlimit))) ||
164 (d->d_ino_hardlimit &&
165 (be64_to_cpu(d->d_icount) >
166 be64_to_cpu(d->d_ino_hardlimit)))) {
167 d->d_itimer = cpu_to_be32(get_seconds() +
168 mp->m_quotainfo->qi_itimelimit);
173 if ((!d->d_ino_softlimit ||
174 (be64_to_cpu(d->d_icount) <=
175 be64_to_cpu(d->d_ino_softlimit))) &&
176 (!d->d_ino_hardlimit ||
177 (be64_to_cpu(d->d_icount) <=
178 be64_to_cpu(d->d_ino_hardlimit)))) {
183 if (!d->d_rtbtimer) {
184 if ((d->d_rtb_softlimit &&
185 (be64_to_cpu(d->d_rtbcount) >
186 be64_to_cpu(d->d_rtb_softlimit))) ||
187 (d->d_rtb_hardlimit &&
188 (be64_to_cpu(d->d_rtbcount) >
189 be64_to_cpu(d->d_rtb_hardlimit)))) {
190 d->d_rtbtimer = cpu_to_be32(get_seconds() +
191 mp->m_quotainfo->qi_rtbtimelimit);
196 if ((!d->d_rtb_softlimit ||
197 (be64_to_cpu(d->d_rtbcount) <=
198 be64_to_cpu(d->d_rtb_softlimit))) &&
199 (!d->d_rtb_hardlimit ||
200 (be64_to_cpu(d->d_rtbcount) <=
201 be64_to_cpu(d->d_rtb_hardlimit)))) {
208 * initialize a buffer full of dquots and log the whole thing
211 xfs_qm_init_dquot_blk(
218 struct xfs_quotainfo *q = mp->m_quotainfo;
224 ASSERT(xfs_buf_islocked(bp));
229 * ID of the first dquot in the block - id's are zero based.
231 curid = id - (id % q->qi_dqperchunk);
232 memset(d, 0, BBTOB(q->qi_dqchunklen));
233 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
234 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
235 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
236 d->dd_diskdq.d_id = cpu_to_be32(curid);
237 d->dd_diskdq.d_flags = type;
238 if (xfs_sb_version_hascrc(&mp->m_sb)) {
239 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
240 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
245 xfs_trans_dquot_buf(tp, bp,
246 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
247 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
248 XFS_BLF_GDQUOT_BUF)));
249 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
253 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
254 * watermarks correspond to the soft and hard limits by default. If a soft limit
255 * is not specified, we use 95% of the hard limit.
258 xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
262 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
263 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
264 if (!dqp->q_prealloc_lo_wmark) {
265 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
266 do_div(dqp->q_prealloc_lo_wmark, 100);
267 dqp->q_prealloc_lo_wmark *= 95;
270 space = dqp->q_prealloc_hi_wmark;
273 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
274 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
275 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
279 * Ensure that the given in-core dquot has a buffer on disk backing it, and
280 * return the buffer. This is called when the bmapi finds a hole.
283 xfs_dquot_disk_alloc(
284 struct xfs_trans **tpp,
285 struct xfs_dquot *dqp,
286 struct xfs_buf **bpp)
288 struct xfs_bmbt_irec map;
289 struct xfs_defer_ops dfops;
290 struct xfs_mount *mp = (*tpp)->t_mountp;
292 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
293 xfs_fsblock_t firstblock;
297 trace_xfs_dqalloc(dqp);
299 xfs_defer_init(&dfops, &firstblock);
300 xfs_ilock(quotip, XFS_ILOCK_EXCL);
301 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
303 * Return if this type of quotas is turned off while we didn't
306 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
310 /* Create the block mapping. */
311 xfs_trans_ijoin(*tpp, quotip, XFS_ILOCK_EXCL);
312 error = xfs_bmapi_write(*tpp, quotip, dqp->q_fileoffset,
313 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
314 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
315 &map, &nmaps, &dfops);
318 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
320 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
321 (map.br_startblock != HOLESTARTBLOCK));
324 * Keep track of the blkno to save a lookup later
326 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
328 /* now we can just get the buffer (there's nothing to read yet) */
329 bp = xfs_trans_get_buf(*tpp, mp->m_ddev_targp, dqp->q_blkno,
330 mp->m_quotainfo->qi_dqchunklen, 0);
335 bp->b_ops = &xfs_dquot_buf_ops;
338 * Make a chunk of dquots out of this buffer and log
341 xfs_qm_init_dquot_blk(*tpp, mp, be32_to_cpu(dqp->q_core.d_id),
342 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
343 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
346 * Hold the buffer and join it to the dfops so that we'll still own
347 * the buffer when we return to the caller. The buffer disposal on
348 * error must be paid attention to very carefully, as it has been
349 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
350 * code when allocating a new dquot record" in 2005, and the later
351 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
352 * the buffer locked across the _defer_finish call. We can now do
353 * this correctly with xfs_defer_bjoin.
355 * Above, we allocated a disk block for the dquot information and
356 * used get_buf to initialize the dquot. If the _defer_bjoin fails,
357 * the buffer is still locked to *tpp, so we must _bhold_release and
358 * then _trans_brelse the buffer. If the _defer_finish fails, the old
359 * transaction is gone but the new buffer is not joined or held to any
360 * transaction, so we must _buf_relse it.
362 * If everything succeeds, the caller of this function is returned a
363 * buffer that is locked and held to the transaction. The caller
364 * is responsible for unlocking any buffer passed back, either
365 * manually or by committing the transaction.
367 xfs_trans_bhold(*tpp, bp);
368 error = xfs_defer_bjoin(&dfops, bp);
370 xfs_trans_bhold_release(*tpp, bp);
371 xfs_trans_brelse(*tpp, bp);
374 error = xfs_defer_finish(tpp, &dfops);
383 xfs_defer_cancel(&dfops);
389 * Read in the in-core dquot's on-disk metadata and return the buffer.
390 * Returns ENOENT to signal a hole.
394 struct xfs_mount *mp,
395 struct xfs_dquot *dqp,
396 struct xfs_buf **bpp)
398 struct xfs_bmbt_irec map;
400 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
405 lock_mode = xfs_ilock_data_map_shared(quotip);
406 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
408 * Return if this type of quotas is turned off while we
409 * didn't have the quota inode lock.
411 xfs_iunlock(quotip, lock_mode);
416 * Find the block map; no allocations yet
418 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
419 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
420 xfs_iunlock(quotip, lock_mode);
425 ASSERT(map.br_blockcount >= 1);
426 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
427 if (map.br_startblock == HOLESTARTBLOCK)
430 trace_xfs_dqtobp_read(dqp);
433 * store the blkno etc so that we don't have to do the
434 * mapping all the time
436 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
438 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
439 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
446 ASSERT(xfs_buf_islocked(bp));
447 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
453 /* Allocate and initialize everything we need for an incore dquot. */
454 STATIC struct xfs_dquot *
456 struct xfs_mount *mp,
460 struct xfs_dquot *dqp;
462 dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
464 dqp->dq_flags = type;
465 dqp->q_core.d_id = cpu_to_be32(id);
467 INIT_LIST_HEAD(&dqp->q_lru);
468 mutex_init(&dqp->q_qlock);
469 init_waitqueue_head(&dqp->q_pinwait);
470 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
472 * Offset of dquot in the (fixed sized) dquot chunk.
474 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
478 * Because we want to use a counting completion, complete
479 * the flush completion once to allow a single access to
480 * the flush completion without blocking.
482 init_completion(&dqp->q_flush);
483 complete(&dqp->q_flush);
486 * Make sure group quotas have a different lock class than user
491 /* uses the default lock class */
494 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
497 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
504 xfs_qm_dquot_logitem_init(dqp);
506 XFS_STATS_INC(mp, xs_qm_dquot);
510 /* Copy the in-core quota fields in from the on-disk buffer. */
513 struct xfs_dquot *dqp,
516 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
518 /* copy everything from disk dquot to the incore dquot */
519 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
522 * Reservation counters are defined as reservation plus current usage
523 * to avoid having to add every time.
525 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
526 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
527 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
529 /* initialize the dquot speculative prealloc thresholds */
530 xfs_dquot_set_prealloc_limits(dqp);
533 /* Allocate and initialize the dquot buffer for this in-core dquot. */
536 struct xfs_mount *mp,
537 struct xfs_dquot *dqp,
538 struct xfs_buf **bpp)
540 struct xfs_trans *tp;
544 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
545 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
549 error = xfs_dquot_disk_alloc(&tp, dqp, &bp);
553 error = xfs_trans_commit(tp);
556 * Buffer was held to the transaction, so we have to unlock it
557 * manually here because we're not passing it back.
566 xfs_trans_cancel(tp);
572 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
573 * and release the buffer immediately. If @can_alloc is true, fill any
574 * holes in the on-disk metadata.
578 struct xfs_mount *mp,
582 struct xfs_dquot **dqpp)
584 struct xfs_dquot *dqp;
588 dqp = xfs_dquot_alloc(mp, id, type);
589 trace_xfs_dqread(dqp);
591 /* Try to read the buffer, allocating if necessary. */
592 error = xfs_dquot_disk_read(mp, dqp, &bp);
593 if (error == -ENOENT && can_alloc)
594 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
599 * At this point we should have a clean locked buffer. Copy the data
600 * to the incore dquot and release the buffer since the incore dquot
601 * has its own locking protocol so we needn't tie up the buffer any
604 ASSERT(xfs_buf_islocked(bp));
605 xfs_dquot_from_disk(dqp, bp);
612 trace_xfs_dqread_fail(dqp);
613 xfs_qm_dqdestroy(dqp);
619 * Advance to the next id in the current chunk, or if at the
620 * end of the chunk, skip ahead to first id in next allocated chunk
621 * using the SEEK_DATA interface.
625 struct xfs_mount *mp,
629 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
630 xfs_dqid_t next_id = *id + 1; /* simple advance */
632 struct xfs_bmbt_irec got;
633 struct xfs_iext_cursor cur;
637 /* If we'd wrap past the max ID, stop */
641 /* If new ID is within the current chunk, advancing it sufficed */
642 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
647 /* Nope, next_id is now past the current chunk, so find the next one */
648 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
650 lock_flags = xfs_ilock_data_map_shared(quotip);
651 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
652 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
657 if (xfs_iext_lookup_extent(quotip, "ip->i_df, start, &cur, &got)) {
658 /* contiguous chunk, bump startoff for the id calculation */
659 if (got.br_startoff < start)
660 got.br_startoff = start;
661 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
666 xfs_iunlock(quotip, lock_flags);
672 * Look up the dquot in the in-core cache. If found, the dquot is returned
673 * locked and ready to go.
675 static struct xfs_dquot *
676 xfs_qm_dqget_cache_lookup(
677 struct xfs_mount *mp,
678 struct xfs_quotainfo *qi,
679 struct radix_tree_root *tree,
682 struct xfs_dquot *dqp;
685 mutex_lock(&qi->qi_tree_lock);
686 dqp = radix_tree_lookup(tree, id);
688 mutex_unlock(&qi->qi_tree_lock);
689 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
694 if (dqp->dq_flags & XFS_DQ_FREEING) {
696 mutex_unlock(&qi->qi_tree_lock);
697 trace_xfs_dqget_freeing(dqp);
703 mutex_unlock(&qi->qi_tree_lock);
705 trace_xfs_dqget_hit(dqp);
706 XFS_STATS_INC(mp, xs_qm_dqcachehits);
711 * Try to insert a new dquot into the in-core cache. If an error occurs the
712 * caller should throw away the dquot and start over. Otherwise, the dquot
713 * is returned locked (and held by the cache) as if there had been a cache
717 xfs_qm_dqget_cache_insert(
718 struct xfs_mount *mp,
719 struct xfs_quotainfo *qi,
720 struct radix_tree_root *tree,
722 struct xfs_dquot *dqp)
726 mutex_lock(&qi->qi_tree_lock);
727 error = radix_tree_insert(tree, id, dqp);
728 if (unlikely(error)) {
729 /* Duplicate found! Caller must try again. */
730 WARN_ON(error != -EEXIST);
731 mutex_unlock(&qi->qi_tree_lock);
732 trace_xfs_dqget_dup(dqp);
736 /* Return a locked dquot to the caller, with a reference taken. */
741 mutex_unlock(&qi->qi_tree_lock);
746 /* Check our input parameters. */
749 struct xfs_mount *mp,
752 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
757 if (!XFS_IS_UQUOTA_ON(mp))
761 if (!XFS_IS_GQUOTA_ON(mp))
765 if (!XFS_IS_PQUOTA_ON(mp))
775 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
776 * dquot, doing an allocation (if requested) as needed.
780 struct xfs_mount *mp,
784 struct xfs_dquot **O_dqpp)
786 struct xfs_quotainfo *qi = mp->m_quotainfo;
787 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
788 struct xfs_dquot *dqp;
791 error = xfs_qm_dqget_checks(mp, type);
796 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
802 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
806 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
809 * Duplicate found. Just throw away the new dquot and start
812 xfs_qm_dqdestroy(dqp);
813 XFS_STATS_INC(mp, xs_qm_dquot_dups);
817 trace_xfs_dqget_miss(dqp);
823 * Given a dquot id and type, read and initialize a dquot from the on-disk
824 * metadata. This function is only for use during quota initialization so
825 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
826 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
829 xfs_qm_dqget_uncached(
830 struct xfs_mount *mp,
833 struct xfs_dquot **dqpp)
837 error = xfs_qm_dqget_checks(mp, type);
841 return xfs_qm_dqread(mp, id, type, 0, dqpp);
844 /* Return the quota id for a given inode and type. */
846 xfs_qm_id_for_quotatype(
847 struct xfs_inode *ip,
852 return ip->i_d.di_uid;
854 return ip->i_d.di_gid;
856 return xfs_get_projid(ip);
863 * Return the dquot for a given inode and type. If @can_alloc is true, then
864 * allocate blocks if needed. The inode's ILOCK must be held and it must not
865 * have already had an inode attached.
869 struct xfs_inode *ip,
872 struct xfs_dquot **O_dqpp)
874 struct xfs_mount *mp = ip->i_mount;
875 struct xfs_quotainfo *qi = mp->m_quotainfo;
876 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
877 struct xfs_dquot *dqp;
881 error = xfs_qm_dqget_checks(mp, type);
885 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
886 ASSERT(xfs_inode_dquot(ip, type) == NULL);
888 id = xfs_qm_id_for_quotatype(ip, type);
891 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
898 * Dquot cache miss. We don't want to keep the inode lock across
899 * a (potential) disk read. Also we don't want to deal with the lock
900 * ordering between quotainode and this inode. OTOH, dropping the inode
901 * lock here means dealing with a chown that can happen before
902 * we re-acquire the lock.
904 xfs_iunlock(ip, XFS_ILOCK_EXCL);
905 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
906 xfs_ilock(ip, XFS_ILOCK_EXCL);
911 * A dquot could be attached to this inode by now, since we had
914 if (xfs_this_quota_on(mp, type)) {
915 struct xfs_dquot *dqp1;
917 dqp1 = xfs_inode_dquot(ip, type);
919 xfs_qm_dqdestroy(dqp);
925 /* inode stays locked on return */
926 xfs_qm_dqdestroy(dqp);
930 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
933 * Duplicate found. Just throw away the new dquot and start
936 xfs_qm_dqdestroy(dqp);
937 XFS_STATS_INC(mp, xs_qm_dquot_dups);
942 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
943 trace_xfs_dqget_miss(dqp);
949 * Starting at @id and progressing upwards, look for an initialized incore
950 * dquot, lock it, and return it.
954 struct xfs_mount *mp,
957 struct xfs_dquot **dqpp)
959 struct xfs_dquot *dqp;
963 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
964 error = xfs_qm_dqget(mp, id, type, false, &dqp);
965 if (error == -ENOENT)
970 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
982 * Release a reference to the dquot (decrement ref-count) and unlock it.
984 * If there is a group quota attached to this dquot, carefully release that
985 * too without tripping over deadlocks'n'stuff.
989 struct xfs_dquot *dqp)
991 ASSERT(dqp->q_nrefs > 0);
992 ASSERT(XFS_DQ_IS_LOCKED(dqp));
994 trace_xfs_dqput(dqp);
996 if (--dqp->q_nrefs == 0) {
997 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
998 trace_xfs_dqput_free(dqp);
1000 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
1001 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
1007 * Release a dquot. Flush it if dirty, then dqput() it.
1008 * dquot must not be locked.
1017 trace_xfs_dqrele(dqp);
1021 * We don't care to flush it if the dquot is dirty here.
1022 * That will create stutters that we want to avoid.
1023 * Instead we do a delayed write when we try to reclaim
1024 * a dirty dquot. Also xfs_sync will take part of the burden...
1030 * This is the dquot flushing I/O completion routine. It is called
1031 * from interrupt level when the buffer containing the dquot is
1032 * flushed to disk. It is responsible for removing the dquot logitem
1033 * from the AIL if it has not been re-logged, and unlocking the dquot's
1034 * flush lock. This behavior is very similar to that of inodes..
1037 xfs_qm_dqflush_done(
1039 struct xfs_log_item *lip)
1041 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
1042 xfs_dquot_t *dqp = qip->qli_dquot;
1043 struct xfs_ail *ailp = lip->li_ailp;
1046 * We only want to pull the item from the AIL if its
1047 * location in the log has not changed since we started the flush.
1048 * Thus, we only bother if the dquot's lsn has
1049 * not changed. First we check the lsn outside the lock
1050 * since it's cheaper, and then we recheck while
1051 * holding the lock before removing the dquot from the AIL.
1053 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
1054 ((lip->li_lsn == qip->qli_flush_lsn) ||
1055 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
1057 /* xfs_trans_ail_delete() drops the AIL lock. */
1058 spin_lock(&ailp->ail_lock);
1059 if (lip->li_lsn == qip->qli_flush_lsn) {
1060 xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
1063 * Clear the failed state since we are about to drop the
1066 xfs_clear_li_failed(lip);
1067 spin_unlock(&ailp->ail_lock);
1072 * Release the dq's flush lock since we're done with it.
1078 * Write a modified dquot to disk.
1079 * The dquot must be locked and the flush lock too taken by caller.
1080 * The flush lock will not be unlocked until the dquot reaches the disk,
1081 * but the dquot is free to be unlocked and modified by the caller
1082 * in the interim. Dquot is still locked on return. This behavior is
1083 * identical to that of inodes.
1087 struct xfs_dquot *dqp,
1088 struct xfs_buf **bpp)
1090 struct xfs_mount *mp = dqp->q_mount;
1092 struct xfs_dqblk *dqb;
1093 struct xfs_disk_dquot *ddqp;
1097 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1098 ASSERT(!completion_done(&dqp->q_flush));
1100 trace_xfs_dqflush(dqp);
1104 xfs_qm_dqunpin_wait(dqp);
1107 * This may have been unpinned because the filesystem is shutting
1108 * down forcibly. If that's the case we must not write this dquot
1109 * to disk, because the log record didn't make it to disk.
1111 * We also have to remove the log item from the AIL in this case,
1112 * as we wait for an emptry AIL as part of the unmount process.
1114 if (XFS_FORCED_SHUTDOWN(mp)) {
1115 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
1116 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1118 xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);
1125 * Get the buffer containing the on-disk dquot
1127 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1128 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
1129 &xfs_dquot_buf_ops);
1134 * Calculate the location of the dquot inside the buffer.
1136 dqb = bp->b_addr + dqp->q_bufoffset;
1137 ddqp = &dqb->dd_diskdq;
1140 * A simple sanity check in case we got a corrupted dquot.
1142 fa = xfs_dqblk_verify(mp, dqb, be32_to_cpu(ddqp->d_id), 0);
1144 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1145 be32_to_cpu(ddqp->d_id), fa);
1148 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1152 /* This is the only portion of data that needs to persist */
1153 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
1156 * Clear the dirty field and remember the flush lsn for later use.
1158 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1160 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1161 &dqp->q_logitem.qli_item.li_lsn);
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.
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.
1172 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),
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.
1182 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1183 &dqp->q_logitem.qli_item);
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.
1189 if (xfs_buf_ispinned(bp)) {
1190 trace_xfs_dqflush_force(dqp);
1191 xfs_log_force(mp, 0);
1194 trace_xfs_dqflush_done(dqp);
1204 * Lock two xfs_dquot structures.
1206 * To avoid deadlocks we always lock the quota structure with
1207 * the lowerd id first.
1216 if (be32_to_cpu(d1->q_core.d_id) >
1217 be32_to_cpu(d2->q_core.d_id)) {
1218 mutex_lock(&d2->q_qlock);
1219 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1221 mutex_lock(&d1->q_qlock);
1222 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1225 mutex_lock(&d1->q_qlock);
1227 mutex_lock(&d2->q_qlock);
1235 kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
1240 kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
1241 if (!xfs_qm_dqtrxzone)
1242 goto out_free_dqzone;
1247 kmem_zone_destroy(xfs_qm_dqzone);
1255 kmem_zone_destroy(xfs_qm_dqtrxzone);
1256 kmem_zone_destroy(xfs_qm_dqzone);
1260 * Iterate every dquot of a particular type. The caller must ensure that the
1261 * particular quota type is active. iter_fn can return negative error codes,
1262 * or XFS_BTREE_QUERY_RANGE_ABORT to indicate that it wants to stop iterating.
1266 struct xfs_mount *mp,
1268 xfs_qm_dqiterate_fn iter_fn,
1271 struct xfs_dquot *dq;
1276 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1277 if (error == -ENOENT)
1282 error = iter_fn(dq, dqtype, priv);
1283 id = be32_to_cpu(dq->q_core.d_id);
1286 } while (error == 0 && id != 0);