1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
10 #include "xfs_shared.h"
11 #include "xfs_format.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans_resv.h"
15 #include "xfs_mount.h"
16 #include "xfs_inode.h"
17 #include "xfs_trans.h"
18 #include "xfs_quota.h"
20 #include "xfs_icache.h"
23 xfs_qm_scall_quotaoff(
28 * No file system can have quotas enabled on disk but not in core.
29 * Note that quota utilities (like quotaoff) _expect_
30 * errno == -EEXIST here.
32 if ((mp->m_qflags & flags) == 0)
36 * We do not support actually turning off quota accounting any more.
37 * Just log a warning and ignore the accounting related flags.
39 if (flags & XFS_ALL_QUOTA_ACCT)
40 xfs_info(mp, "disabling of quota accounting not supported.");
42 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
43 mp->m_qflags &= ~(flags & XFS_ALL_QUOTA_ENFD);
44 spin_lock(&mp->m_sb_lock);
45 mp->m_sb.sb_qflags = mp->m_qflags;
46 spin_unlock(&mp->m_sb_lock);
47 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
49 /* XXX what to do if error ? Revert back to old vals incore ? */
50 return xfs_sync_sb(mp, false);
54 xfs_qm_scall_trunc_qfile(
65 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
69 xfs_ilock(ip, XFS_IOLOCK_EXCL);
71 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
73 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
77 xfs_ilock(ip, XFS_ILOCK_EXCL);
78 xfs_trans_ijoin(tp, ip, 0);
81 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
83 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
89 ASSERT(ip->i_df.if_nextents == 0);
91 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
92 error = xfs_trans_commit(tp);
95 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
102 xfs_qm_scall_trunc_qfiles(
108 if (!xfs_has_quota(mp) || flags == 0 ||
109 (flags & ~XFS_QMOPT_QUOTALL)) {
110 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
111 __func__, flags, mp->m_qflags);
115 if (flags & XFS_QMOPT_UQUOTA) {
116 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
120 if (flags & XFS_QMOPT_GQUOTA) {
121 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
125 if (flags & XFS_QMOPT_PQUOTA)
126 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
132 * Switch on (a given) quota enforcement for a filesystem. This takes
133 * effect immediately.
134 * (Switching on quota accounting must be done at mount time.)
137 xfs_qm_scall_quotaon(
145 * Switching on quota accounting must be done at mount time,
146 * only consider quota enforcement stuff here.
148 flags &= XFS_ALL_QUOTA_ENFD;
151 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
152 __func__, mp->m_qflags);
157 * Can't enforce without accounting. We check the superblock
158 * qflags here instead of m_qflags because rootfs can have
159 * quota acct on ondisk without m_qflags' knowing.
161 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
162 (flags & XFS_UQUOTA_ENFD)) ||
163 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
164 (flags & XFS_GQUOTA_ENFD)) ||
165 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
166 (flags & XFS_PQUOTA_ENFD))) {
168 "%s: Can't enforce without acct, flags=%x sbflags=%x",
169 __func__, flags, mp->m_sb.sb_qflags);
173 * If everything's up to-date incore, then don't waste time.
175 if ((mp->m_qflags & flags) == flags)
179 * Change sb_qflags on disk but not incore mp->qflags
180 * if this is the root filesystem.
182 spin_lock(&mp->m_sb_lock);
183 qf = mp->m_sb.sb_qflags;
184 mp->m_sb.sb_qflags = qf | flags;
185 spin_unlock(&mp->m_sb_lock);
188 * There's nothing to change if it's the same.
190 if ((qf & flags) == flags)
193 error = xfs_sync_sb(mp, false);
197 * If we aren't trying to switch on quota enforcement, we are done.
199 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
200 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
201 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
202 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
203 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
204 (mp->m_qflags & XFS_GQUOTA_ACCT)))
207 if (!XFS_IS_QUOTA_ON(mp))
211 * Switch on quota enforcement in core.
213 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
214 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
215 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
220 #define XFS_QC_MASK \
221 (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
224 * Adjust limits of this quota, and the defaults if passed in. Returns true
225 * if the new limits made sense and were applied, false otherwise.
229 struct xfs_mount *mp,
230 struct xfs_dquot_res *res,
231 struct xfs_quota_limits *qlim,
236 /* The hard limit can't be less than the soft limit. */
237 if (hard != 0 && hard < soft) {
238 xfs_debug(mp, "%shard %lld < %ssoft %lld", tag, hard, tag,
243 res->hardlimit = hard;
244 res->softlimit = soft;
255 struct xfs_dquot_res *res,
256 struct xfs_quota_limits *qlim,
259 res->warnings = warns;
266 struct xfs_mount *mp,
267 struct xfs_dquot_res *res,
268 struct xfs_quota_limits *qlim,
272 /* Set the length of the default grace period. */
273 res->timer = xfs_dquot_set_grace_period(timer);
274 qlim->time = res->timer;
276 /* Set the grace period expiration on a quota. */
277 res->timer = xfs_dquot_set_timeout(mp, timer);
282 * Adjust quota limits, and start/stop timers accordingly.
285 xfs_qm_scall_setqlim(
286 struct xfs_mount *mp,
289 struct qc_dqblk *newlim)
291 struct xfs_quotainfo *q = mp->m_quotainfo;
292 struct xfs_dquot *dqp;
293 struct xfs_trans *tp;
294 struct xfs_def_quota *defq;
295 struct xfs_dquot_res *res;
296 struct xfs_quota_limits *qlim;
298 xfs_qcnt_t hard, soft;
300 if (newlim->d_fieldmask & ~XFS_QC_MASK)
302 if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
306 * We don't want to race with a quotaoff so take the quotaoff lock.
307 * We don't hold an inode lock, so there's nothing else to stop
308 * a quotaoff from happening.
310 mutex_lock(&q->qi_quotaofflock);
313 * Get the dquot (locked) before we start, as we need to do a
314 * transaction to allocate it if it doesn't exist. Once we have the
315 * dquot, unlock it so we can start the next transaction safely. We hold
316 * a reference to the dquot, so it's safe to do this unlock/lock without
317 * it being reclaimed in the mean time.
319 error = xfs_qm_dqget(mp, id, type, true, &dqp);
321 ASSERT(error != -ENOENT);
325 defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
328 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp);
333 xfs_trans_dqjoin(tp, dqp);
336 * Update quota limits, warnings, and timers, and the defaults
337 * if we're touching id == 0.
339 * Make sure that hardlimits are >= soft limits before changing.
341 * Update warnings counter(s) if requested.
343 * Timelimits for the super user set the relative time the other users
344 * can be over quota for this file system. If it is zero a default is
345 * used. Ditto for the default soft and hard limit values (already
346 * done, above), and for warnings.
348 * For other IDs, userspace can bump out the grace period if over
352 /* Blocks on the data device. */
353 hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
354 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
355 dqp->q_blk.hardlimit;
356 soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
357 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
358 dqp->q_blk.softlimit;
360 qlim = id == 0 ? &defq->blk : NULL;
362 if (xfs_setqlim_limits(mp, res, qlim, hard, soft, "blk"))
363 xfs_dquot_set_prealloc_limits(dqp);
364 if (newlim->d_fieldmask & QC_SPC_WARNS)
365 xfs_setqlim_warns(res, qlim, newlim->d_spc_warns);
366 if (newlim->d_fieldmask & QC_SPC_TIMER)
367 xfs_setqlim_timer(mp, res, qlim, newlim->d_spc_timer);
369 /* Blocks on the realtime device. */
370 hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
371 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
372 dqp->q_rtb.hardlimit;
373 soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
374 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
375 dqp->q_rtb.softlimit;
377 qlim = id == 0 ? &defq->rtb : NULL;
379 xfs_setqlim_limits(mp, res, qlim, hard, soft, "rtb");
380 if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
381 xfs_setqlim_warns(res, qlim, newlim->d_rt_spc_warns);
382 if (newlim->d_fieldmask & QC_RT_SPC_TIMER)
383 xfs_setqlim_timer(mp, res, qlim, newlim->d_rt_spc_timer);
386 hard = (newlim->d_fieldmask & QC_INO_HARD) ?
387 (xfs_qcnt_t) newlim->d_ino_hardlimit :
388 dqp->q_ino.hardlimit;
389 soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
390 (xfs_qcnt_t) newlim->d_ino_softlimit :
391 dqp->q_ino.softlimit;
393 qlim = id == 0 ? &defq->ino : NULL;
395 xfs_setqlim_limits(mp, res, qlim, hard, soft, "ino");
396 if (newlim->d_fieldmask & QC_INO_WARNS)
397 xfs_setqlim_warns(res, qlim, newlim->d_ino_warns);
398 if (newlim->d_fieldmask & QC_INO_TIMER)
399 xfs_setqlim_timer(mp, res, qlim, newlim->d_ino_timer);
403 * If the user is now over quota, start the timelimit.
404 * The user will not be 'warned'.
405 * Note that we keep the timers ticking, whether enforcement
406 * is on or off. We don't really want to bother with iterating
407 * over all ondisk dquots and turning the timers on/off.
409 xfs_qm_adjust_dqtimers(dqp);
411 dqp->q_flags |= XFS_DQFLAG_DIRTY;
412 xfs_trans_log_dquot(tp, dqp);
414 error = xfs_trans_commit(tp);
419 mutex_unlock(&q->qi_quotaofflock);
423 /* Fill out the quota context. */
425 xfs_qm_scall_getquota_fill_qc(
426 struct xfs_mount *mp,
428 const struct xfs_dquot *dqp,
429 struct qc_dqblk *dst)
431 memset(dst, 0, sizeof(*dst));
432 dst->d_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_blk.hardlimit);
433 dst->d_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_blk.softlimit);
434 dst->d_ino_hardlimit = dqp->q_ino.hardlimit;
435 dst->d_ino_softlimit = dqp->q_ino.softlimit;
436 dst->d_space = XFS_FSB_TO_B(mp, dqp->q_blk.reserved);
437 dst->d_ino_count = dqp->q_ino.reserved;
438 dst->d_spc_timer = dqp->q_blk.timer;
439 dst->d_ino_timer = dqp->q_ino.timer;
440 dst->d_ino_warns = dqp->q_ino.warnings;
441 dst->d_spc_warns = dqp->q_blk.warnings;
442 dst->d_rt_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.hardlimit);
443 dst->d_rt_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.softlimit);
444 dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_rtb.reserved);
445 dst->d_rt_spc_timer = dqp->q_rtb.timer;
446 dst->d_rt_spc_warns = dqp->q_rtb.warnings;
449 * Internally, we don't reset all the timers when quota enforcement
450 * gets turned off. No need to confuse the user level code,
451 * so return zeroes in that case.
453 if (!xfs_dquot_is_enforced(dqp)) {
454 dst->d_spc_timer = 0;
455 dst->d_ino_timer = 0;
456 dst->d_rt_spc_timer = 0;
460 if (xfs_dquot_is_enforced(dqp) && dqp->q_id != 0) {
461 if ((dst->d_space > dst->d_spc_softlimit) &&
462 (dst->d_spc_softlimit > 0)) {
463 ASSERT(dst->d_spc_timer != 0);
465 if ((dst->d_ino_count > dqp->q_ino.softlimit) &&
466 (dqp->q_ino.softlimit > 0)) {
467 ASSERT(dst->d_ino_timer != 0);
473 /* Return the quota information for the dquot matching id. */
475 xfs_qm_scall_getquota(
476 struct xfs_mount *mp,
479 struct qc_dqblk *dst)
481 struct xfs_dquot *dqp;
484 /* Flush inodegc work at the start of a quota reporting scan. */
486 xfs_inodegc_flush(mp);
489 * Try to get the dquot. We don't want it allocated on disk, so don't
490 * set doalloc. If it doesn't exist, we'll get ENOENT back.
492 error = xfs_qm_dqget(mp, id, type, false, &dqp);
497 * If everything's NULL, this dquot doesn't quite exist as far as
498 * our utility programs are concerned.
500 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
505 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
513 * Return the quota information for the first initialized dquot whose id
514 * is at least as high as id.
517 xfs_qm_scall_getquota_next(
518 struct xfs_mount *mp,
521 struct qc_dqblk *dst)
523 struct xfs_dquot *dqp;
526 /* Flush inodegc work at the start of a quota reporting scan. */
528 xfs_inodegc_flush(mp);
530 error = xfs_qm_dqget_next(mp, *id, type, &dqp);
534 /* Fill in the ID we actually read from disk */
537 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);