blob: 0688019984bbc46f2ec1bb415b3d3bac2fa1ef49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
19#include <linux/capability.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs.h"
22#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_trans.h"
27#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dir2.h"
30#include "xfs_alloc.h"
31#include "xfs_dmapi.h"
32#include "xfs_quota.h"
33#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_dinode.h"
40#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_ialloc.h"
42#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110044#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rtalloc.h"
46#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_attr.h"
49#include "xfs_buf_item.h"
50#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000052#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#ifdef DEBUG
55# define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
56#else
57# define qdprintk(s, args...) do { } while (0)
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
61STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
62 uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC uint xfs_qm_export_flags(uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064STATIC uint xfs_qm_export_qtype_flags(uint);
65STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
66 fs_disk_quota_t *);
67
68
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * Turn off quota accounting and/or enforcement for all udquots and/or
71 * gdquots. Called only at unmount time.
72 *
73 * This assumes that there are no dquots of this file system cached
74 * incore, and modifies the ondisk dquot directly. Therefore, for example,
75 * it is an error to call this twice, without purging the cache.
76 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +010077int
Linus Torvalds1da177e2005-04-16 15:20:36 -070078xfs_qm_scall_quotaoff(
79 xfs_mount_t *mp,
Christoph Hellwigfcafb712009-02-09 08:47:34 +010080 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +100082 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 uint dqtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int error;
85 uint inactivate_flags;
86 xfs_qoff_logitem_t *qoffstart;
87 int nculprits;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /*
90 * No file system can have quotas enabled on disk but not in core.
91 * Note that quota utilities (like quotaoff) _expect_
92 * errno == EEXIST here.
93 */
94 if ((mp->m_qflags & flags) == 0)
95 return XFS_ERROR(EEXIST);
96 error = 0;
97
98 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
99
100 /*
101 * We don't want to deal with two quotaoffs messing up each other,
102 * so we're going to serialize it. quotaoff isn't exactly a performance
103 * critical thing.
104 * If quotaoff, then we must be dealing with the root filesystem.
105 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000106 ASSERT(q);
107 mutex_lock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /*
110 * If we're just turning off quota enforcement, change mp and go.
111 */
112 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
113 mp->m_qflags &= ~(flags);
114
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000115 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 mp->m_sb.sb_qflags = mp->m_qflags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000117 spin_unlock(&mp->m_sb_lock);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000118 mutex_unlock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* XXX what to do if error ? Revert back to old vals incore ? */
121 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
122 return (error);
123 }
124
125 dqtype = 0;
126 inactivate_flags = 0;
127 /*
128 * If accounting is off, we must turn enforcement off, clear the
129 * quota 'CHKD' certificate to make it known that we have to
130 * do a quotacheck the next time this quota is turned on.
131 */
132 if (flags & XFS_UQUOTA_ACCT) {
133 dqtype |= XFS_QMOPT_UQUOTA;
134 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
135 inactivate_flags |= XFS_UQUOTA_ACTIVE;
136 }
137 if (flags & XFS_GQUOTA_ACCT) {
138 dqtype |= XFS_QMOPT_GQUOTA;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000139 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 inactivate_flags |= XFS_GQUOTA_ACTIVE;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000141 } else if (flags & XFS_PQUOTA_ACCT) {
142 dqtype |= XFS_QMOPT_PQUOTA;
143 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
144 inactivate_flags |= XFS_PQUOTA_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 /*
148 * Nothing to do? Don't complain. This happens when we're just
149 * turning off quota enforcement.
150 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000151 if ((mp->m_qflags & flags) == 0)
152 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /*
155 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
David Chinnercb6edc22008-04-10 12:20:45 +1000156 * and synchronously. If we fail to write, we should abort the
157 * operation as it cannot be recovered safely if we crash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
David Chinnercb6edc22008-04-10 12:20:45 +1000159 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
160 if (error)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000161 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /*
164 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
165 * to take care of the race between dqget and quotaoff. We don't take
166 * any special locks to reset these bits. All processes need to check
167 * these bits *after* taking inode lock(s) to see if the particular
168 * quota type is in the process of being turned off. If *ACTIVE, it is
169 * guaranteed that all dquot structures and all quotainode ptrs will all
170 * stay valid as long as that inode is kept locked.
171 *
172 * There is no turning back after this.
173 */
174 mp->m_qflags &= ~inactivate_flags;
175
176 /*
177 * Give back all the dquot reference(s) held by inodes.
178 * Here we go thru every single incore inode in this file system, and
179 * do a dqrele on the i_udquot/i_gdquot that it may have.
180 * Essentially, as long as somebody has an inode locked, this guarantees
181 * that quotas will not be turned off. This is handy because in a
182 * transaction once we lock the inode(s) and check for quotaon, we can
183 * depend on the quota inodes (and other things) being valid as long as
184 * we keep the lock(s).
185 */
186 xfs_qm_dqrele_all_inodes(mp, flags);
187
188 /*
189 * Next we make the changes in the quota flag in the mount struct.
190 * This isn't protected by a particular lock directly, because we
191 * don't want to take a mrlock everytime we depend on quotas being on.
192 */
193 mp->m_qflags &= ~(flags);
194
195 /*
196 * Go through all the dquots of this file system and purge them,
197 * according to what was turned off. We may not be able to get rid
198 * of all dquots, because dquots can have temporary references that
199 * are not attached to inodes. eg. xfs_setattr, xfs_create.
200 * So, if we couldn't purge all the dquots from the filesystem,
201 * we can't get rid of the incore data structures.
202 */
203 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
204 delay(10 * nculprits);
205
206 /*
207 * Transactions that had started before ACTIVE state bit was cleared
208 * could have logged many dquots, so they'd have higher LSNs than
209 * the first QUOTAOFF log record does. If we happen to crash when
210 * the tail of the log has gone past the QUOTAOFF record, but
211 * before the last dquot modification, those dquots __will__
212 * recover, and that's not good.
213 *
214 * So, we have QUOTAOFF start and end logitems; the start
215 * logitem won't get overwritten until the end logitem appears...
216 */
David Chinnercb6edc22008-04-10 12:20:45 +1000217 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
218 if (error) {
219 /* We're screwed now. Shutdown is the only option. */
220 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000221 goto out_unlock;
David Chinnercb6edc22008-04-10 12:20:45 +1000222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /*
225 * If quotas is completely disabled, close shop.
226 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000227 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
228 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000229 mutex_unlock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_qm_destroy_quotainfo(mp);
231 return (0);
232 }
233
234 /*
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000235 * Release our quotainode references if we don't need them anymore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000237 if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
238 IRELE(q->qi_uquotaip);
239 q->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000241 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && q->qi_gquotaip) {
242 IRELE(q->qi_gquotaip);
243 q->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000246out_unlock:
247 mutex_unlock(&q->qi_quotaofflock);
248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100251int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252xfs_qm_scall_trunc_qfiles(
253 xfs_mount_t *mp,
254 uint flags)
255{
David Chinner88ab0202008-04-10 12:20:51 +1000256 int error = 0, error2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_inode_t *qip;
258
Eric Sandeen62118702008-03-06 13:44:28 +1100259 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
261 return XFS_ERROR(EINVAL);
262 }
263
264 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
265 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
David Chinner88ab0202008-04-10 12:20:51 +1000266 if (!error) {
267 error = xfs_truncate_file(mp, qip);
Christoph Hellwig43355092008-03-27 18:01:08 +1100268 IRELE(qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
271
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000272 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
273 mp->m_sb.sb_gquotino != NULLFSINO) {
David Chinner88ab0202008-04-10 12:20:51 +1000274 error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
275 if (!error2) {
276 error2 = xfs_truncate_file(mp, qip);
Christoph Hellwig43355092008-03-27 18:01:08 +1100277 IRELE(qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 }
280
David Chinner88ab0202008-04-10 12:20:51 +1000281 return error ? error : error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284
285/*
286 * Switch on (a given) quota enforcement for a filesystem. This takes
287 * effect immediately.
288 * (Switching on quota accounting must be done at mount time.)
289 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100290int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291xfs_qm_scall_quotaon(
292 xfs_mount_t *mp,
293 uint flags)
294{
295 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 uint qf;
297 uint accflags;
298 __int64_t sbflags;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
301 /*
302 * Switching on quota accounting must be done at mount time.
303 */
304 accflags = flags & XFS_ALL_QUOTA_ACCT;
305 flags &= ~(XFS_ALL_QUOTA_ACCT);
306
307 sbflags = 0;
308
309 if (flags == 0) {
310 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
311 return XFS_ERROR(EINVAL);
312 }
313
314 /* No fs can turn on quotas with a delayed effect */
315 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
316
317 /*
318 * Can't enforce without accounting. We check the superblock
319 * qflags here instead of m_qflags because rootfs can have
320 * quota acct on ondisk without m_qflags' knowing.
321 */
322 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
323 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
324 (flags & XFS_UQUOTA_ENFD))
325 ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000326 ((flags & XFS_PQUOTA_ACCT) == 0 &&
327 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
Donald Douwsma424ea912007-05-08 13:49:15 +1000328 (flags & XFS_GQUOTA_ACCT) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000330 (flags & XFS_OQUOTA_ENFD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
332 flags, mp->m_sb.sb_qflags);
333 return XFS_ERROR(EINVAL);
334 }
335 /*
336 * If everything's upto-date incore, then don't waste time.
337 */
338 if ((mp->m_qflags & flags) == flags)
339 return XFS_ERROR(EEXIST);
340
341 /*
342 * Change sb_qflags on disk but not incore mp->qflags
343 * if this is the root filesystem.
344 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000345 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 qf = mp->m_sb.sb_qflags;
347 mp->m_sb.sb_qflags = qf | flags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000348 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /*
351 * There's nothing to change if it's the same.
352 */
353 if ((qf & flags) == flags && sbflags == 0)
354 return XFS_ERROR(EEXIST);
355 sbflags |= XFS_SB_QFLAGS;
356
357 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
358 return (error);
359 /*
360 * If we aren't trying to switch on quota enforcement, we are done.
361 */
362 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
363 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000364 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
365 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
366 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
367 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (flags & XFS_ALL_QUOTA_ENFD) == 0)
369 return (0);
370
371 if (! XFS_IS_QUOTA_RUNNING(mp))
372 return XFS_ERROR(ESRCH);
373
374 /*
375 * Switch on quota enforcement in core.
376 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000377 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000379 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return (0);
382}
383
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
386 * Return quota status information, such as uquota-off, enforcements, etc.
387 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100388int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389xfs_qm_scall_getqstat(
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000390 struct xfs_mount *mp,
391 struct fs_quota_stat *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000393 struct xfs_quotainfo *q = mp->m_quotainfo;
394 struct xfs_inode *uip, *gip;
395 boolean_t tempuqip, tempgqip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 uip = gip = NULL;
398 tempuqip = tempgqip = B_FALSE;
399 memset(out, 0, sizeof(fs_quota_stat_t));
400
401 out->qs_version = FS_QSTAT_VERSION;
Eric Sandeen62118702008-03-06 13:44:28 +1100402 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 out->qs_uquota.qfs_ino = NULLFSINO;
404 out->qs_gquota.qfs_ino = NULLFSINO;
405 return (0);
406 }
407 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
408 (XFS_ALL_QUOTA_ACCT|
409 XFS_ALL_QUOTA_ENFD));
410 out->qs_pad = 0;
411 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
412 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
413
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000414 if (q) {
415 uip = q->qi_uquotaip;
416 gip = q->qi_gquotaip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
419 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
420 0, 0, &uip, 0) == 0)
421 tempuqip = B_TRUE;
422 }
423 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
424 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
425 0, 0, &gip, 0) == 0)
426 tempgqip = B_TRUE;
427 }
428 if (uip) {
429 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
430 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
431 if (tempuqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100432 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 if (gip) {
435 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
436 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
437 if (tempgqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100438 IRELE(gip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000440 if (q) {
441 out->qs_incoredqs = q->qi_dquots;
442 out->qs_btimelimit = q->qi_btimelimit;
443 out->qs_itimelimit = q->qi_itimelimit;
444 out->qs_rtbtimelimit = q->qi_rtbtimelimit;
445 out->qs_bwarnlimit = q->qi_bwarnlimit;
446 out->qs_iwarnlimit = q->qi_iwarnlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451/*
452 * Adjust quota limits, and start/stop timers accordingly.
453 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100454int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455xfs_qm_scall_setqlim(
456 xfs_mount_t *mp,
457 xfs_dqid_t id,
458 uint type,
459 fs_disk_quota_t *newlim)
460{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000461 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xfs_disk_dquot_t *ddq;
463 xfs_dquot_t *dqp;
464 xfs_trans_t *tp;
465 int error;
466 xfs_qcnt_t hard, soft;
467
Nathan Scott754002b2005-06-21 15:49:06 +1000468 if ((newlim->d_fieldmask &
469 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return (0);
471
472 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
473 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
474 0, 0, XFS_DEFAULT_LOG_COUNT))) {
475 xfs_trans_cancel(tp, 0);
476 return (error);
477 }
478
479 /*
480 * We don't want to race with a quotaoff so take the quotaoff lock.
481 * (We don't hold an inode lock, so there's nothing else to stop
482 * a quotaoff from happening). (XXXThis doesn't currently happen
483 * because we take the vfslock before calling xfs_qm_sysent).
484 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000485 mutex_lock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * Get the dquot (locked), and join it to the transaction.
489 * Allocate the dquot if this doesn't exist.
490 */
491 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
492 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ASSERT(error != ENOENT);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000494 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 xfs_trans_dqjoin(tp, dqp);
497 ddq = &dqp->q_core;
498
499 /*
500 * Make sure that hardlimits are >= soft limits before changing.
501 */
502 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
503 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100504 be64_to_cpu(ddq->d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
506 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100507 be64_to_cpu(ddq->d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100509 ddq->d_blk_hardlimit = cpu_to_be64(hard);
510 ddq->d_blk_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (id == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000512 q->qi_bhardlimit = hard;
513 q->qi_bsoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 } else {
516 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
517 }
518 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
519 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100520 be64_to_cpu(ddq->d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
522 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100523 be64_to_cpu(ddq->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100525 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
526 ddq->d_rtb_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (id == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000528 q->qi_rtbhardlimit = hard;
529 q->qi_rtbsoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 } else {
532 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
533 }
534
535 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
536 (xfs_qcnt_t) newlim->d_ino_hardlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100537 be64_to_cpu(ddq->d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
539 (xfs_qcnt_t) newlim->d_ino_softlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100540 be64_to_cpu(ddq->d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100542 ddq->d_ino_hardlimit = cpu_to_be64(hard);
543 ddq->d_ino_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (id == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000545 q->qi_ihardlimit = hard;
546 q->qi_isoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 } else {
549 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
550 }
551
Nathan Scott754002b2005-06-21 15:49:06 +1000552 /*
553 * Update warnings counter(s) if requested
554 */
555 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100556 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000557 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100558 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000559 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100560 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (id == 0) {
563 /*
564 * Timelimits for the super user set the relative time
565 * the other users can be over quota for this file system.
566 * If it is zero a default is used. Ditto for the default
Nathan Scott754002b2005-06-21 15:49:06 +1000567 * soft and hard limit values (already done, above), and
568 * for warnings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
570 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000571 q->qi_btimelimit = newlim->d_btimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100572 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000575 q->qi_itimelimit = newlim->d_itimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100576 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000579 q->qi_rtbtimelimit = newlim->d_rtbtimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100580 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Nathan Scott754002b2005-06-21 15:49:06 +1000582 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000583 q->qi_bwarnlimit = newlim->d_bwarns;
Nathan Scott754002b2005-06-21 15:49:06 +1000584 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000585 q->qi_iwarnlimit = newlim->d_iwarns;
Nathan Scott754002b2005-06-21 15:49:06 +1000586 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000587 q->qi_rtbwarnlimit = newlim->d_rtbwarns;
Nathan Scott754002b2005-06-21 15:49:06 +1000588 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
590 * If the user is now over quota, start the timelimit.
591 * The user will not be 'warned'.
592 * Note that we keep the timers ticking, whether enforcement
593 * is on or off. We don't really want to bother with iterating
594 * over all ondisk dquots and turning the timers on/off.
595 */
596 xfs_qm_adjust_dqtimers(mp, ddq);
597 }
598 dqp->dq_flags |= XFS_DQ_DIRTY;
599 xfs_trans_log_dquot(tp, dqp);
600
David Chinnere5720ee2008-04-10 12:21:18 +1000601 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfs_qm_dqprint(dqp);
603 xfs_qm_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000605 out_unlock:
606 mutex_unlock(&q->qi_quotaofflock);
David Chinnere5720ee2008-04-10 12:21:18 +1000607 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100610int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611xfs_qm_scall_getquota(
612 xfs_mount_t *mp,
613 xfs_dqid_t id,
614 uint type,
615 fs_disk_quota_t *out)
616{
617 xfs_dquot_t *dqp;
618 int error;
619
620 /*
621 * Try to get the dquot. We don't want it allocated on disk, so
622 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
623 * exist, we'll get ENOENT back.
624 */
625 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
626 return (error);
627 }
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * If everything's NULL, this dquot doesn't quite exist as far as
631 * our utility programs are concerned.
632 */
633 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
634 xfs_qm_dqput(dqp);
635 return XFS_ERROR(ENOENT);
636 }
637 /* xfs_qm_dqprint(dqp); */
638 /*
639 * Convert the disk dquot to the exportable format
640 */
641 xfs_qm_export_dquot(mp, &dqp->q_core, out);
642 xfs_qm_dqput(dqp);
643 return (error ? XFS_ERROR(EFAULT) : 0);
644}
645
646
647STATIC int
648xfs_qm_log_quotaoff_end(
649 xfs_mount_t *mp,
650 xfs_qoff_logitem_t *startqoff,
651 uint flags)
652{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000653 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000655 xfs_qoff_logitem_t *qoffi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
658
659 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
660 0, 0, XFS_DEFAULT_LOG_COUNT))) {
661 xfs_trans_cancel(tp, 0);
662 return (error);
663 }
664
665 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
666 flags & XFS_ALL_QUOTA_ACCT);
667 xfs_trans_log_quotaoff_item(tp, qoffi);
668
669 /*
670 * We have to make sure that the transaction is secure on disk before we
671 * return and actually stop quota accounting. So, make it synchronous.
672 * We don't care about quotoff's performance.
673 */
674 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000675 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return (error);
677}
678
679
680STATIC int
681xfs_qm_log_quotaoff(
682 xfs_mount_t *mp,
683 xfs_qoff_logitem_t **qoffstartp,
684 uint flags)
685{
686 xfs_trans_t *tp;
687 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 xfs_qoff_logitem_t *qoffi=NULL;
689 uint oldsbqflag=0;
690
691 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
692 if ((error = xfs_trans_reserve(tp, 0,
693 sizeof(xfs_qoff_logitem_t) * 2 +
694 mp->m_sb.sb_sectsize + 128,
695 0,
696 0,
697 XFS_DEFAULT_LOG_COUNT))) {
698 goto error0;
699 }
700
701 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
702 xfs_trans_log_quotaoff_item(tp, qoffi);
703
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000704 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 oldsbqflag = mp->m_sb.sb_qflags;
706 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000707 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 xfs_mod_sb(tp, XFS_SB_QFLAGS);
710
711 /*
712 * We have to make sure that the transaction is secure on disk before we
713 * return and actually stop quota accounting. So, make it synchronous.
714 * We don't care about quotoff's performance.
715 */
716 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000717 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719error0:
720 if (error) {
721 xfs_trans_cancel(tp, 0);
722 /*
723 * No one else is modifying sb_qflags, so this is OK.
724 * We still hold the quotaofflock.
725 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000726 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 mp->m_sb.sb_qflags = oldsbqflag;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000728 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 *qoffstartp = qoffi;
731 return (error);
732}
733
734
735/*
736 * Translate an internal style on-disk-dquot to the exportable format.
737 * The main differences are that the counters/limits are all in Basic
738 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
739 * to be converted to the native endianness.
740 */
741STATIC void
742xfs_qm_export_dquot(
743 xfs_mount_t *mp,
744 xfs_disk_dquot_t *src,
745 struct fs_disk_quota *dst)
746{
747 memset(dst, 0, sizeof(*dst));
748 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100749 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
750 dst->d_id = be32_to_cpu(src->d_id);
751 dst->d_blk_hardlimit =
752 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
753 dst->d_blk_softlimit =
754 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
755 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
756 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
757 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
758 dst->d_icount = be64_to_cpu(src->d_icount);
759 dst->d_btimer = be32_to_cpu(src->d_btimer);
760 dst->d_itimer = be32_to_cpu(src->d_itimer);
761 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
762 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
763 dst->d_rtb_hardlimit =
764 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
765 dst->d_rtb_softlimit =
766 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
767 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
768 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
769 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /*
772 * Internally, we don't reset all the timers when quota enforcement
Nathan Scottc41564b2006-03-29 08:55:14 +1000773 * gets turned off. No need to confuse the user level code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * so return zeroes in that case.
775 */
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000776 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
777 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
778 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 dst->d_btimer = 0;
780 dst->d_itimer = 0;
781 dst->d_rtbtimer = 0;
782 }
783
784#ifdef DEBUG
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000785 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
786 (XFS_IS_OQUOTA_ENFORCED(mp) &&
787 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
788 dst->d_id != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
790 (dst->d_blk_softlimit > 0)) {
791 ASSERT(dst->d_btimer != 0);
792 }
793 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
794 (dst->d_ino_softlimit > 0)) {
795 ASSERT(dst->d_itimer != 0);
796 }
797 }
798#endif
799}
800
801STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802xfs_qm_export_qtype_flags(
803 uint flags)
804{
805 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000806 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000808 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
809 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
810 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
811 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
812 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
813 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
814 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 return (flags & XFS_DQ_USER) ?
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000817 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
818 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822xfs_qm_export_flags(
823 uint flags)
824{
825 uint uflags;
826
827 uflags = 0;
828 if (flags & XFS_UQUOTA_ACCT)
829 uflags |= XFS_QUOTA_UDQ_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000830 if (flags & XFS_PQUOTA_ACCT)
831 uflags |= XFS_QUOTA_PDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (flags & XFS_GQUOTA_ACCT)
833 uflags |= XFS_QUOTA_GDQ_ACCT;
834 if (flags & XFS_UQUOTA_ENFD)
835 uflags |= XFS_QUOTA_UDQ_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000836 if (flags & (XFS_OQUOTA_ENFD)) {
837 uflags |= (flags & XFS_GQUOTA_ACCT) ?
838 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return (uflags);
841}
842
843
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200844STATIC int
845xfs_dqrele_inode(
846 struct xfs_inode *ip,
847 struct xfs_perag *pag,
848 int flags)
David Chinner5b4d89a2008-10-30 17:08:03 +1100849{
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200850 int error;
David Chinner5b4d89a2008-10-30 17:08:03 +1100851
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200852 /* skip quota inodes */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000853 if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
854 ip == ip->i_mount->m_quotainfo->qi_gquotaip) {
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200855 ASSERT(ip->i_udquot == NULL);
856 ASSERT(ip->i_gdquot == NULL);
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100857 read_unlock(&pag->pag_ici_lock);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200858 return 0;
859 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100860
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200861 error = xfs_sync_inode_valid(ip, pag);
862 if (error)
863 return error;
Dave Chinner63070912008-11-10 17:13:23 +1100864
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200865 xfs_ilock(ip, XFS_ILOCK_EXCL);
866 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
867 xfs_qm_dqrele(ip->i_udquot);
868 ip->i_udquot = NULL;
869 }
870 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
871 xfs_qm_dqrele(ip->i_gdquot);
872 ip->i_gdquot = NULL;
873 }
874 xfs_iput(ip, XFS_ILOCK_EXCL);
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100875
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200876 return 0;
David Chinner5b4d89a2008-10-30 17:08:03 +1100877}
878
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200879
David Chinner5b4d89a2008-10-30 17:08:03 +1100880/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * Go thru all the inodes in the file system, releasing their dquots.
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200882 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Note that the mount structure gets modified to indicate that quotas are off
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200884 * AFTER this, in the case of quotaoff.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
886void
887xfs_qm_dqrele_all_inodes(
888 struct xfs_mount *mp,
889 uint flags)
890{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 ASSERT(mp->m_quotainfo);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000892 xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags,
893 XFS_ICI_NO_TAG, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*------------------------------------------------------------------------*/
897#ifdef DEBUG
898/*
899 * This contains all the test functions for XFS disk quotas.
900 * Currently it does a quota accounting check. ie. it walks through
901 * all inodes in the file system, calculating the dquot accounting fields,
902 * and prints out any inconsistencies.
903 */
904xfs_dqhash_t *qmtest_udqtab;
905xfs_dqhash_t *qmtest_gdqtab;
906int qmtest_hashmask;
907int qmtest_nfails;
Christoph Hellwiga0b0b8a2009-03-29 09:51:00 +0200908struct mutex qcheck_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910#define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
911 (__psunsigned_t)(id)) & \
912 (qmtest_hashmask - 1))
913
914#define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
915 (qmtest_udqtab + \
916 DQTEST_HASHVAL(mp, id)) : \
917 (qmtest_gdqtab + \
918 DQTEST_HASHVAL(mp, id)))
919
920#define DQTEST_LIST_PRINT(l, NXT, title) \
921{ \
922 xfs_dqtest_t *dqp; int i = 0;\
923 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
924 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
925 dqp = (xfs_dqtest_t *)dqp->NXT) { \
926 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
927 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
928 dqp->d_bcount, dqp->d_icount); } \
929}
930
931typedef struct dqtest {
Christoph Hellwig74457cf2010-04-13 15:06:53 +1000932 uint dq_flags; /* various flags (XFS_DQ_*) */
Dave Chinnere6a81f12010-04-13 15:06:51 +1000933 struct list_head q_hashlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 xfs_dqhash_t *q_hash; /* the hashchain header */
935 xfs_mount_t *q_mount; /* filesystem this relates to */
936 xfs_dqid_t d_id; /* user id or group id */
937 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
938 xfs_qcnt_t d_icount; /* # inodes owned by the user */
939} xfs_dqtest_t;
940
941STATIC void
942xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
943{
Dave Chinnere6a81f12010-04-13 15:06:51 +1000944 list_add(&dqp->q_hashlist, &h->qh_list);
945 h->qh_version++;
946 h->qh_nelems++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948STATIC void
949xfs_qm_dqtest_print(
950 xfs_dqtest_t *d)
951{
952 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
953 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
955 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
956 d->d_bcount, (int)d->d_bcount);
957 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
958 d->d_icount, (int)d->d_icount);
959 cmn_err(CE_DEBUG, "---------------------------");
960}
961
962STATIC void
963xfs_qm_dqtest_failed(
964 xfs_dqtest_t *d,
965 xfs_dquot_t *dqp,
966 char *reason,
967 xfs_qcnt_t a,
968 xfs_qcnt_t b,
969 int error)
970{
971 qmtest_nfails++;
972 if (error)
973 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100974 d->d_id, error, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 else
976 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100977 d->d_id, reason, (int)a, (int)b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 xfs_qm_dqtest_print(d);
979 if (dqp)
980 xfs_qm_dqprint(dqp);
981}
982
983STATIC int
984xfs_dqtest_cmp2(
985 xfs_dqtest_t *d,
986 xfs_dquot_t *dqp)
987{
988 int err = 0;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100989 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100991 be64_to_cpu(dqp->q_core.d_icount),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 d->d_icount, 0);
993 err++;
994 }
Christoph Hellwig1149d962005-11-02 15:01:12 +1100995 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100997 be64_to_cpu(dqp->q_core.d_bcount),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 d->d_bcount, 0);
999 err++;
1000 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001001 if (dqp->q_core.d_blk_softlimit &&
1002 be64_to_cpu(dqp->q_core.d_bcount) >=
1003 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1005 cmn_err(CE_DEBUG,
1006 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1007 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1008 err++;
1009 }
1010 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001011 if (dqp->q_core.d_ino_softlimit &&
1012 be64_to_cpu(dqp->q_core.d_icount) >=
1013 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1015 cmn_err(CE_DEBUG,
1016 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1017 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1018 err++;
1019 }
1020 }
1021#ifdef QUOTADEBUG
1022 if (!err) {
1023 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001024 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026#endif
1027 return (err);
1028}
1029
1030STATIC void
1031xfs_dqtest_cmp(
1032 xfs_dqtest_t *d)
1033{
1034 xfs_dquot_t *dqp;
1035 int error;
1036
1037 /* xfs_qm_dqtest_print(d); */
1038 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1039 &dqp))) {
1040 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1041 return;
1042 }
1043 xfs_dqtest_cmp2(d, dqp);
1044 xfs_qm_dqput(dqp);
1045}
1046
1047STATIC int
1048xfs_qm_internalqcheck_dqget(
1049 xfs_mount_t *mp,
1050 xfs_dqid_t id,
1051 uint type,
1052 xfs_dqtest_t **O_dq)
1053{
1054 xfs_dqtest_t *d;
1055 xfs_dqhash_t *h;
1056
1057 h = DQTEST_HASH(mp, id, type);
Dave Chinnere6a81f12010-04-13 15:06:51 +10001058 list_for_each_entry(d, &h->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (d->d_id == id && mp == d->q_mount) {
1060 *O_dq = d;
1061 return (0);
1062 }
1063 }
1064 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1065 d->dq_flags = type;
1066 d->d_id = id;
1067 d->q_mount = mp;
1068 d->q_hash = h;
Dave Chinnere6a81f12010-04-13 15:06:51 +10001069 INIT_LIST_HEAD(&d->q_hashlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 xfs_qm_hashinsert(h, d);
1071 *O_dq = d;
1072 return (0);
1073}
1074
1075STATIC void
1076xfs_qm_internalqcheck_get_dquots(
1077 xfs_mount_t *mp,
1078 xfs_dqid_t uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001079 xfs_dqid_t projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 xfs_dqid_t gid,
1081 xfs_dqtest_t **ud,
1082 xfs_dqtest_t **gd)
1083{
1084 if (XFS_IS_UQUOTA_ON(mp))
1085 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1086 if (XFS_IS_GQUOTA_ON(mp))
1087 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001088 else if (XFS_IS_PQUOTA_ON(mp))
1089 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092
1093STATIC void
1094xfs_qm_internalqcheck_dqadjust(
1095 xfs_inode_t *ip,
1096 xfs_dqtest_t *d)
1097{
1098 d->d_icount++;
1099 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1100}
1101
1102STATIC int
1103xfs_qm_internalqcheck_adjust(
1104 xfs_mount_t *mp, /* mount point for filesystem */
1105 xfs_ino_t ino, /* inode number to get data for */
1106 void __user *buffer, /* not used */
1107 int ubsize, /* not used */
1108 void *private_data, /* not used */
1109 xfs_daddr_t bno, /* starting block of inode cluster */
1110 int *ubused, /* not used */
1111 void *dip, /* not used */
1112 int *res) /* bulkstat result code */
1113{
1114 xfs_inode_t *ip;
1115 xfs_dqtest_t *ud, *gd;
1116 uint lock_flags;
1117 boolean_t ipreleased;
1118 int error;
1119
1120 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1121
1122 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1123 *res = BULKSTAT_RV_NOTHING;
1124 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1125 (unsigned long long) ino,
1126 (unsigned long long) mp->m_sb.sb_uquotino,
1127 (unsigned long long) mp->m_sb.sb_gquotino);
1128 return XFS_ERROR(EINVAL);
1129 }
1130 ipreleased = B_FALSE;
1131 again:
1132 lock_flags = XFS_ILOCK_SHARED;
1133 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1134 *res = BULKSTAT_RV_NOTHING;
1135 return (error);
1136 }
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
1139 * This inode can have blocks after eof which can get released
1140 * when we send it to inactive. Since we don't check the dquot
1141 * until the after all our calculations are done, we must get rid
1142 * of those now.
1143 */
1144 if (! ipreleased) {
1145 xfs_iput(ip, lock_flags);
1146 ipreleased = B_TRUE;
1147 goto again;
1148 }
1149 xfs_qm_internalqcheck_get_dquots(mp,
1150 (xfs_dqid_t) ip->i_d.di_uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001151 (xfs_dqid_t) ip->i_d.di_projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 (xfs_dqid_t) ip->i_d.di_gid,
1153 &ud, &gd);
1154 if (XFS_IS_UQUOTA_ON(mp)) {
1155 ASSERT(ud);
1156 xfs_qm_internalqcheck_dqadjust(ip, ud);
1157 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001158 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ASSERT(gd);
1160 xfs_qm_internalqcheck_dqadjust(ip, gd);
1161 }
1162 xfs_iput(ip, lock_flags);
1163 *res = BULKSTAT_RV_DIDONE;
1164 return (0);
1165}
1166
1167
1168/* PRIVATE, debugging */
1169int
1170xfs_qm_internalqcheck(
1171 xfs_mount_t *mp)
1172{
1173 xfs_ino_t lastino;
1174 int done, count;
1175 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int error;
1177
1178 lastino = 0;
1179 qmtest_hashmask = 32;
1180 count = 5;
1181 done = 0;
1182 qmtest_nfails = 0;
1183
1184 if (! XFS_IS_QUOTA_ON(mp))
1185 return XFS_ERROR(ESRCH);
1186
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001187 xfs_log_force(mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 XFS_bflush(mp->m_ddev_targp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001189 xfs_log_force(mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 XFS_bflush(mp->m_ddev_targp);
1191
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001192 mutex_lock(&qcheck_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* There should be absolutely no quota activity while this
1194 is going on. */
1195 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1196 sizeof(xfs_dqhash_t), KM_SLEEP);
1197 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1198 sizeof(xfs_dqhash_t), KM_SLEEP);
1199 do {
1200 /*
1201 * Iterate thru all the inodes in the file system,
1202 * adjusting the corresponding dquot counters
1203 */
1204 if ((error = xfs_bulkstat(mp, &lastino, &count,
1205 xfs_qm_internalqcheck_adjust, NULL,
1206 0, NULL, BULKSTAT_FG_IGET, &done))) {
1207 break;
1208 }
1209 } while (! done);
1210 if (error) {
1211 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1212 }
1213 cmn_err(CE_DEBUG, "Checking results against system dquots");
1214 for (i = 0; i < qmtest_hashmask; i++) {
Dave Chinnere6a81f12010-04-13 15:06:51 +10001215 xfs_dqtest_t *d, *n;
1216 xfs_dqhash_t *h;
1217
1218 h = &qmtest_udqtab[i];
1219 list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 xfs_dqtest_cmp(d);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001221 kmem_free(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Dave Chinnere6a81f12010-04-13 15:06:51 +10001223 h = &qmtest_gdqtab[i];
1224 list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xfs_dqtest_cmp(d);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001226 kmem_free(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 }
1229
1230 if (qmtest_nfails) {
1231 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1232 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1233 } else {
1234 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1235 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001236 kmem_free(qmtest_udqtab);
1237 kmem_free(qmtest_gdqtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 mutex_unlock(&qcheck_lock);
1239 return (qmtest_nfails);
1240}
1241
1242#endif /* DEBUG */