2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_inode.h"
29 #include "xfs_bmap_util.h"
30 #include "xfs_alloc.h"
31 #include "xfs_quota.h"
32 #include "xfs_error.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_trans_space.h"
36 #include "xfs_trans_priv.h"
38 #include "xfs_cksum.h"
39 #include "xfs_trace.h"
41 #include "xfs_bmap_btree.h"
48 * dquot->q_qlock (xfs_dqlock() and friends)
49 * dquot->q_flush (xfs_dqflock() and friends)
52 * If two dquots need to be locked the order is user before group/project,
53 * otherwise by the lowest id first, see xfs_dqlock2.
56 struct kmem_zone *xfs_qm_dqtrxzone;
57 static struct kmem_zone *xfs_qm_dqzone;
59 static struct lock_class_key xfs_dquot_group_class;
60 static struct lock_class_key xfs_dquot_project_class;
63 * This is called to free all the memory associated with a dquot
69 ASSERT(list_empty(&dqp->q_lru));
71 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
72 mutex_destroy(&dqp->q_qlock);
74 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
75 kmem_zone_free(xfs_qm_dqzone, dqp);
79 * If default limits are in force, push them into the dquot now.
80 * We overwrite the dquot limits only if they are zero and this
81 * is not the root dquot.
84 xfs_qm_adjust_dqlimits(
88 struct xfs_quotainfo *q = mp->m_quotainfo;
89 struct xfs_disk_dquot *d = &dq->q_core;
90 struct xfs_def_quota *defq;
94 defq = xfs_get_defquota(dq, q);
96 if (defq->bsoftlimit && !d->d_blk_softlimit) {
97 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
100 if (defq->bhardlimit && !d->d_blk_hardlimit) {
101 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
104 if (defq->isoftlimit && !d->d_ino_softlimit)
105 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
106 if (defq->ihardlimit && !d->d_ino_hardlimit)
107 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
108 if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
109 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
110 if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
111 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
114 xfs_dquot_set_prealloc_limits(dq);
118 * Check the limits and timers of a dquot and start or reset timers
120 * This gets called even when quota enforcement is OFF, which makes our
121 * life a little less complicated. (We just don't reject any quota
122 * reservations in that case, when enforcement is off).
123 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
125 * In contrast, warnings are a little different in that they don't
126 * 'automatically' get started when limits get exceeded. They do
127 * get reset to zero, however, when we find the count to be under
128 * the soft limit (they are only ever set non-zero via userspace).
131 xfs_qm_adjust_dqtimers(
138 if (d->d_blk_hardlimit)
139 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
140 be64_to_cpu(d->d_blk_hardlimit));
141 if (d->d_ino_hardlimit)
142 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
143 be64_to_cpu(d->d_ino_hardlimit));
144 if (d->d_rtb_hardlimit)
145 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
146 be64_to_cpu(d->d_rtb_hardlimit));
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)))) {
156 d->d_btimer = cpu_to_be32(get_seconds() +
157 mp->m_quotainfo->qi_btimelimit);
162 if ((!d->d_blk_softlimit ||
163 (be64_to_cpu(d->d_bcount) <=
164 be64_to_cpu(d->d_blk_softlimit))) &&
165 (!d->d_blk_hardlimit ||
166 (be64_to_cpu(d->d_bcount) <=
167 be64_to_cpu(d->d_blk_hardlimit)))) {
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)))) {
179 d->d_itimer = cpu_to_be32(get_seconds() +
180 mp->m_quotainfo->qi_itimelimit);
185 if ((!d->d_ino_softlimit ||
186 (be64_to_cpu(d->d_icount) <=
187 be64_to_cpu(d->d_ino_softlimit))) &&
188 (!d->d_ino_hardlimit ||
189 (be64_to_cpu(d->d_icount) <=
190 be64_to_cpu(d->d_ino_hardlimit)))) {
195 if (!d->d_rtbtimer) {
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)))) {
202 d->d_rtbtimer = cpu_to_be32(get_seconds() +
203 mp->m_quotainfo->qi_rtbtimelimit);
208 if ((!d->d_rtb_softlimit ||
209 (be64_to_cpu(d->d_rtbcount) <=
210 be64_to_cpu(d->d_rtb_softlimit))) &&
211 (!d->d_rtb_hardlimit ||
212 (be64_to_cpu(d->d_rtbcount) <=
213 be64_to_cpu(d->d_rtb_hardlimit)))) {
220 * initialize a buffer full of dquots and log the whole thing
223 xfs_qm_init_dquot_blk(
230 struct xfs_quotainfo *q = mp->m_quotainfo;
236 ASSERT(xfs_buf_islocked(bp));
241 * ID of the first dquot in the block - id's are zero based.
243 curid = id - (id % q->qi_dqperchunk);
244 memset(d, 0, BBTOB(q->qi_dqchunklen));
245 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
246 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
247 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
248 d->dd_diskdq.d_id = cpu_to_be32(curid);
249 d->dd_diskdq.d_flags = type;
250 if (xfs_sb_version_hascrc(&mp->m_sb)) {
251 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
252 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
257 xfs_trans_dquot_buf(tp, bp,
258 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
259 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
260 XFS_BLF_GDQUOT_BUF)));
261 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
265 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
266 * watermarks correspond to the soft and hard limits by default. If a soft limit
267 * is not specified, we use 95% of the hard limit.
270 xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
274 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
275 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
276 if (!dqp->q_prealloc_lo_wmark) {
277 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
278 do_div(dqp->q_prealloc_lo_wmark, 100);
279 dqp->q_prealloc_lo_wmark *= 95;
282 space = dqp->q_prealloc_hi_wmark;
285 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
286 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
287 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
291 * Ensure that the given in-core dquot has a buffer on disk backing it, and
292 * return the buffer. This is called when the bmapi finds a hole.
295 xfs_dquot_disk_alloc(
296 struct xfs_trans **tpp,
297 struct xfs_dquot *dqp,
298 struct xfs_buf **bpp)
300 struct xfs_bmbt_irec map;
301 struct xfs_defer_ops dfops;
302 struct xfs_mount *mp = (*tpp)->t_mountp;
304 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
305 xfs_fsblock_t firstblock;
309 trace_xfs_dqalloc(dqp);
311 xfs_defer_init(&dfops, &firstblock);
312 xfs_ilock(quotip, XFS_ILOCK_EXCL);
313 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
315 * Return if this type of quotas is turned off while we didn't
318 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
322 /* Create the block mapping. */
323 xfs_trans_ijoin(*tpp, quotip, XFS_ILOCK_EXCL);
324 error = xfs_bmapi_write(*tpp, quotip, dqp->q_fileoffset,
325 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
326 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
327 &map, &nmaps, &dfops);
330 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
332 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
333 (map.br_startblock != HOLESTARTBLOCK));
336 * Keep track of the blkno to save a lookup later
338 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
340 /* now we can just get the buffer (there's nothing to read yet) */
341 bp = xfs_trans_get_buf(*tpp, mp->m_ddev_targp, dqp->q_blkno,
342 mp->m_quotainfo->qi_dqchunklen, 0);
347 bp->b_ops = &xfs_dquot_buf_ops;
350 * Make a chunk of dquots out of this buffer and log
353 xfs_qm_init_dquot_blk(*tpp, mp, be32_to_cpu(dqp->q_core.d_id),
354 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
355 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
358 * Hold the buffer and join it to the dfops so that we'll still own
359 * the buffer when we return to the caller. The buffer disposal on
360 * error must be paid attention to very carefully, as it has been
361 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
362 * code when allocating a new dquot record" in 2005, and the later
363 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
364 * the buffer locked across the _defer_finish call. We can now do
365 * this correctly with xfs_defer_bjoin.
367 * Above, we allocated a disk block for the dquot information and
368 * used get_buf to initialize the dquot. If the _defer_bjoin fails,
369 * the buffer is still locked to *tpp, so we must _bhold_release and
370 * then _trans_brelse the buffer. If the _defer_finish fails, the old
371 * transaction is gone but the new buffer is not joined or held to any
372 * transaction, so we must _buf_relse it.
374 * If everything succeeds, the caller of this function is returned a
375 * buffer that is locked and held to the transaction. The caller
376 * is responsible for unlocking any buffer passed back, either
377 * manually or by committing the transaction.
379 xfs_trans_bhold(*tpp, bp);
380 error = xfs_defer_bjoin(&dfops, bp);
382 xfs_trans_bhold_release(*tpp, bp);
383 xfs_trans_brelse(*tpp, bp);
386 error = xfs_defer_finish(tpp, &dfops);
395 xfs_defer_cancel(&dfops);
401 * Read in the in-core dquot's on-disk metadata and return the buffer.
402 * Returns ENOENT to signal a hole.
406 struct xfs_mount *mp,
407 struct xfs_dquot *dqp,
408 struct xfs_buf **bpp)
410 struct xfs_bmbt_irec map;
412 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
417 lock_mode = xfs_ilock_data_map_shared(quotip);
418 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
420 * Return if this type of quotas is turned off while we
421 * didn't have the quota inode lock.
423 xfs_iunlock(quotip, lock_mode);
428 * Find the block map; no allocations yet
430 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
431 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
432 xfs_iunlock(quotip, lock_mode);
437 ASSERT(map.br_blockcount >= 1);
438 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
439 if (map.br_startblock == HOLESTARTBLOCK)
442 trace_xfs_dqtobp_read(dqp);
445 * store the blkno etc so that we don't have to do the
446 * mapping all the time
448 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
450 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
451 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
458 ASSERT(xfs_buf_islocked(bp));
459 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
465 /* Allocate and initialize everything we need for an incore dquot. */
466 STATIC struct xfs_dquot *
468 struct xfs_mount *mp,
472 struct xfs_dquot *dqp;
474 dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
476 dqp->dq_flags = type;
477 dqp->q_core.d_id = cpu_to_be32(id);
479 INIT_LIST_HEAD(&dqp->q_lru);
480 mutex_init(&dqp->q_qlock);
481 init_waitqueue_head(&dqp->q_pinwait);
482 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
484 * Offset of dquot in the (fixed sized) dquot chunk.
486 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
490 * Because we want to use a counting completion, complete
491 * the flush completion once to allow a single access to
492 * the flush completion without blocking.
494 init_completion(&dqp->q_flush);
495 complete(&dqp->q_flush);
498 * Make sure group quotas have a different lock class than user
503 /* uses the default lock class */
506 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
509 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
516 xfs_qm_dquot_logitem_init(dqp);
518 XFS_STATS_INC(mp, xs_qm_dquot);
522 /* Copy the in-core quota fields in from the on-disk buffer. */
525 struct xfs_dquot *dqp,
528 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
530 /* copy everything from disk dquot to the incore dquot */
531 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
534 * Reservation counters are defined as reservation plus current usage
535 * to avoid having to add every time.
537 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
538 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
539 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
541 /* initialize the dquot speculative prealloc thresholds */
542 xfs_dquot_set_prealloc_limits(dqp);
545 /* Allocate and initialize the dquot buffer for this in-core dquot. */
548 struct xfs_mount *mp,
549 struct xfs_dquot *dqp,
550 struct xfs_buf **bpp)
552 struct xfs_trans *tp;
556 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
557 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
561 error = xfs_dquot_disk_alloc(&tp, dqp, &bp);
565 error = xfs_trans_commit(tp);
568 * Buffer was held to the transaction, so we have to unlock it
569 * manually here because we're not passing it back.
578 xfs_trans_cancel(tp);
584 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
585 * and release the buffer immediately. If @can_alloc is true, fill any
586 * holes in the on-disk metadata.
590 struct xfs_mount *mp,
594 struct xfs_dquot **dqpp)
596 struct xfs_dquot *dqp;
600 dqp = xfs_dquot_alloc(mp, id, type);
601 trace_xfs_dqread(dqp);
603 /* Try to read the buffer, allocating if necessary. */
604 error = xfs_dquot_disk_read(mp, dqp, &bp);
605 if (error == -ENOENT && can_alloc)
606 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
611 * At this point we should have a clean locked buffer. Copy the data
612 * to the incore dquot and release the buffer since the incore dquot
613 * has its own locking protocol so we needn't tie up the buffer any
616 ASSERT(xfs_buf_islocked(bp));
617 xfs_dquot_from_disk(dqp, bp);
624 trace_xfs_dqread_fail(dqp);
625 xfs_qm_dqdestroy(dqp);
631 * Advance to the next id in the current chunk, or if at the
632 * end of the chunk, skip ahead to first id in next allocated chunk
633 * using the SEEK_DATA interface.
637 struct xfs_mount *mp,
641 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
642 xfs_dqid_t next_id = *id + 1; /* simple advance */
644 struct xfs_bmbt_irec got;
645 struct xfs_iext_cursor cur;
649 /* If we'd wrap past the max ID, stop */
653 /* If new ID is within the current chunk, advancing it sufficed */
654 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
659 /* Nope, next_id is now past the current chunk, so find the next one */
660 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
662 lock_flags = xfs_ilock_data_map_shared(quotip);
663 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
664 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
669 if (xfs_iext_lookup_extent(quotip, "ip->i_df, start, &cur, &got)) {
670 /* contiguous chunk, bump startoff for the id calculation */
671 if (got.br_startoff < start)
672 got.br_startoff = start;
673 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
678 xfs_iunlock(quotip, lock_flags);
684 * Look up the dquot in the in-core cache. If found, the dquot is returned
685 * locked and ready to go.
687 static struct xfs_dquot *
688 xfs_qm_dqget_cache_lookup(
689 struct xfs_mount *mp,
690 struct xfs_quotainfo *qi,
691 struct radix_tree_root *tree,
694 struct xfs_dquot *dqp;
697 mutex_lock(&qi->qi_tree_lock);
698 dqp = radix_tree_lookup(tree, id);
700 mutex_unlock(&qi->qi_tree_lock);
701 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
706 if (dqp->dq_flags & XFS_DQ_FREEING) {
708 mutex_unlock(&qi->qi_tree_lock);
709 trace_xfs_dqget_freeing(dqp);
715 mutex_unlock(&qi->qi_tree_lock);
717 trace_xfs_dqget_hit(dqp);
718 XFS_STATS_INC(mp, xs_qm_dqcachehits);
723 * Try to insert a new dquot into the in-core cache. If an error occurs the
724 * caller should throw away the dquot and start over. Otherwise, the dquot
725 * is returned locked (and held by the cache) as if there had been a cache
729 xfs_qm_dqget_cache_insert(
730 struct xfs_mount *mp,
731 struct xfs_quotainfo *qi,
732 struct radix_tree_root *tree,
734 struct xfs_dquot *dqp)
738 mutex_lock(&qi->qi_tree_lock);
739 error = radix_tree_insert(tree, id, dqp);
740 if (unlikely(error)) {
741 /* Duplicate found! Caller must try again. */
742 WARN_ON(error != -EEXIST);
743 mutex_unlock(&qi->qi_tree_lock);
744 trace_xfs_dqget_dup(dqp);
748 /* Return a locked dquot to the caller, with a reference taken. */
753 mutex_unlock(&qi->qi_tree_lock);
758 /* Check our input parameters. */
761 struct xfs_mount *mp,
764 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
769 if (!XFS_IS_UQUOTA_ON(mp))
773 if (!XFS_IS_GQUOTA_ON(mp))
777 if (!XFS_IS_PQUOTA_ON(mp))
787 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
788 * dquot, doing an allocation (if requested) as needed.
792 struct xfs_mount *mp,
796 struct xfs_dquot **O_dqpp)
798 struct xfs_quotainfo *qi = mp->m_quotainfo;
799 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
800 struct xfs_dquot *dqp;
803 error = xfs_qm_dqget_checks(mp, type);
808 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
814 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
818 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
821 * Duplicate found. Just throw away the new dquot and start
824 xfs_qm_dqdestroy(dqp);
825 XFS_STATS_INC(mp, xs_qm_dquot_dups);
829 trace_xfs_dqget_miss(dqp);
835 * Given a dquot id and type, read and initialize a dquot from the on-disk
836 * metadata. This function is only for use during quota initialization so
837 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
838 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
841 xfs_qm_dqget_uncached(
842 struct xfs_mount *mp,
845 struct xfs_dquot **dqpp)
849 error = xfs_qm_dqget_checks(mp, type);
853 return xfs_qm_dqread(mp, id, type, 0, dqpp);
856 /* Return the quota id for a given inode and type. */
858 xfs_qm_id_for_quotatype(
859 struct xfs_inode *ip,
864 return ip->i_d.di_uid;
866 return ip->i_d.di_gid;
868 return xfs_get_projid(ip);
875 * Return the dquot for a given inode and type. If @can_alloc is true, then
876 * allocate blocks if needed. The inode's ILOCK must be held and it must not
877 * have already had an inode attached.
881 struct xfs_inode *ip,
884 struct xfs_dquot **O_dqpp)
886 struct xfs_mount *mp = ip->i_mount;
887 struct xfs_quotainfo *qi = mp->m_quotainfo;
888 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
889 struct xfs_dquot *dqp;
893 error = xfs_qm_dqget_checks(mp, type);
897 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
898 ASSERT(xfs_inode_dquot(ip, type) == NULL);
900 id = xfs_qm_id_for_quotatype(ip, type);
903 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
910 * Dquot cache miss. We don't want to keep the inode lock across
911 * a (potential) disk read. Also we don't want to deal with the lock
912 * ordering between quotainode and this inode. OTOH, dropping the inode
913 * lock here means dealing with a chown that can happen before
914 * we re-acquire the lock.
916 xfs_iunlock(ip, XFS_ILOCK_EXCL);
917 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
918 xfs_ilock(ip, XFS_ILOCK_EXCL);
923 * A dquot could be attached to this inode by now, since we had
926 if (xfs_this_quota_on(mp, type)) {
927 struct xfs_dquot *dqp1;
929 dqp1 = xfs_inode_dquot(ip, type);
931 xfs_qm_dqdestroy(dqp);
937 /* inode stays locked on return */
938 xfs_qm_dqdestroy(dqp);
942 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
945 * Duplicate found. Just throw away the new dquot and start
948 xfs_qm_dqdestroy(dqp);
949 XFS_STATS_INC(mp, xs_qm_dquot_dups);
954 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
955 trace_xfs_dqget_miss(dqp);
961 * Starting at @id and progressing upwards, look for an initialized incore
962 * dquot, lock it, and return it.
966 struct xfs_mount *mp,
969 struct xfs_dquot **dqpp)
971 struct xfs_dquot *dqp;
975 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
976 error = xfs_qm_dqget(mp, id, type, false, &dqp);
977 if (error == -ENOENT)
982 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
994 * Release a reference to the dquot (decrement ref-count) and unlock it.
996 * If there is a group quota attached to this dquot, carefully release that
997 * too without tripping over deadlocks'n'stuff.
1001 struct xfs_dquot *dqp)
1003 ASSERT(dqp->q_nrefs > 0);
1004 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1006 trace_xfs_dqput(dqp);
1008 if (--dqp->q_nrefs == 0) {
1009 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1010 trace_xfs_dqput_free(dqp);
1012 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
1013 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
1019 * Release a dquot. Flush it if dirty, then dqput() it.
1020 * dquot must not be locked.
1029 trace_xfs_dqrele(dqp);
1033 * We don't care to flush it if the dquot is dirty here.
1034 * That will create stutters that we want to avoid.
1035 * Instead we do a delayed write when we try to reclaim
1036 * a dirty dquot. Also xfs_sync will take part of the burden...
1042 * This is the dquot flushing I/O completion routine. It is called
1043 * from interrupt level when the buffer containing the dquot is
1044 * flushed to disk. It is responsible for removing the dquot logitem
1045 * from the AIL if it has not been re-logged, and unlocking the dquot's
1046 * flush lock. This behavior is very similar to that of inodes..
1049 xfs_qm_dqflush_done(
1051 struct xfs_log_item *lip)
1053 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
1054 xfs_dquot_t *dqp = qip->qli_dquot;
1055 struct xfs_ail *ailp = lip->li_ailp;
1058 * We only want to pull the item from the AIL if its
1059 * location in the log has not changed since we started the flush.
1060 * Thus, we only bother if the dquot's lsn has
1061 * not changed. First we check the lsn outside the lock
1062 * since it's cheaper, and then we recheck while
1063 * holding the lock before removing the dquot from the AIL.
1065 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
1066 ((lip->li_lsn == qip->qli_flush_lsn) ||
1067 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
1069 /* xfs_trans_ail_delete() drops the AIL lock. */
1070 spin_lock(&ailp->ail_lock);
1071 if (lip->li_lsn == qip->qli_flush_lsn) {
1072 xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
1075 * Clear the failed state since we are about to drop the
1078 xfs_clear_li_failed(lip);
1079 spin_unlock(&ailp->ail_lock);
1084 * Release the dq's flush lock since we're done with it.
1090 * Write a modified dquot to disk.
1091 * The dquot must be locked and the flush lock too taken by caller.
1092 * The flush lock will not be unlocked until the dquot reaches the disk,
1093 * but the dquot is free to be unlocked and modified by the caller
1094 * in the interim. Dquot is still locked on return. This behavior is
1095 * identical to that of inodes.
1099 struct xfs_dquot *dqp,
1100 struct xfs_buf **bpp)
1102 struct xfs_mount *mp = dqp->q_mount;
1104 struct xfs_dqblk *dqb;
1105 struct xfs_disk_dquot *ddqp;
1109 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1110 ASSERT(!completion_done(&dqp->q_flush));
1112 trace_xfs_dqflush(dqp);
1116 xfs_qm_dqunpin_wait(dqp);
1119 * This may have been unpinned because the filesystem is shutting
1120 * down forcibly. If that's the case we must not write this dquot
1121 * to disk, because the log record didn't make it to disk.
1123 * We also have to remove the log item from the AIL in this case,
1124 * as we wait for an emptry AIL as part of the unmount process.
1126 if (XFS_FORCED_SHUTDOWN(mp)) {
1127 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
1128 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1130 xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);
1137 * Get the buffer containing the on-disk dquot
1139 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1140 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
1141 &xfs_dquot_buf_ops);
1146 * Calculate the location of the dquot inside the buffer.
1148 dqb = bp->b_addr + dqp->q_bufoffset;
1149 ddqp = &dqb->dd_diskdq;
1152 * A simple sanity check in case we got a corrupted dquot.
1154 fa = xfs_dqblk_verify(mp, dqb, be32_to_cpu(ddqp->d_id), 0);
1156 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1157 be32_to_cpu(ddqp->d_id), fa);
1160 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1164 /* This is the only portion of data that needs to persist */
1165 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
1168 * Clear the dirty field and remember the flush lsn for later use.
1170 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1172 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1173 &dqp->q_logitem.qli_item.li_lsn);
1176 * copy the lsn into the on-disk dquot now while we have the in memory
1177 * dquot here. This can't be done later in the write verifier as we
1178 * can't get access to the log item at that point in time.
1180 * We also calculate the CRC here so that the on-disk dquot in the
1181 * buffer always has a valid CRC. This ensures there is no possibility
1182 * of a dquot without an up-to-date CRC getting to disk.
1184 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1185 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
1186 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1191 * Attach an iodone routine so that we can remove this dquot from the
1192 * AIL and release the flush lock once the dquot is synced to disk.
1194 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1195 &dqp->q_logitem.qli_item);
1198 * If the buffer is pinned then push on the log so we won't
1199 * get stuck waiting in the write for too long.
1201 if (xfs_buf_ispinned(bp)) {
1202 trace_xfs_dqflush_force(dqp);
1203 xfs_log_force(mp, 0);
1206 trace_xfs_dqflush_done(dqp);
1216 * Lock two xfs_dquot structures.
1218 * To avoid deadlocks we always lock the quota structure with
1219 * the lowerd id first.
1228 if (be32_to_cpu(d1->q_core.d_id) >
1229 be32_to_cpu(d2->q_core.d_id)) {
1230 mutex_lock(&d2->q_qlock);
1231 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1233 mutex_lock(&d1->q_qlock);
1234 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1237 mutex_lock(&d1->q_qlock);
1239 mutex_lock(&d2->q_qlock);
1247 kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
1252 kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
1253 if (!xfs_qm_dqtrxzone)
1254 goto out_free_dqzone;
1259 kmem_zone_destroy(xfs_qm_dqzone);
1267 kmem_zone_destroy(xfs_qm_dqtrxzone);
1268 kmem_zone_destroy(xfs_qm_dqzone);
1272 * Iterate every dquot of a particular type. The caller must ensure that the
1273 * particular quota type is active. iter_fn can return negative error codes,
1274 * or XFS_BTREE_QUERY_RANGE_ABORT to indicate that it wants to stop iterating.
1278 struct xfs_mount *mp,
1280 xfs_qm_dqiterate_fn iter_fn,
1283 struct xfs_dquot *dq;
1288 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1289 if (error == -ENOENT)
1294 error = iter_fn(dq, dqtype, priv);
1295 id = be32_to_cpu(dq->q_core.d_id);
1298 } while (error == 0 && id != 0);