blob: 4e4276b956e8c5f925490dec8e4dfb320ed071a2 [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"
52
53#ifdef DEBUG
54# define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
55#else
56# define qdprintk(s, args...) do { } while (0)
57#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
60STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
61 uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC uint xfs_qm_export_flags(uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC uint xfs_qm_export_qtype_flags(uint);
64STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
65 fs_disk_quota_t *);
66
67
68/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * Turn off quota accounting and/or enforcement for all udquots and/or
70 * gdquots. Called only at unmount time.
71 *
72 * This assumes that there are no dquots of this file system cached
73 * incore, and modifies the ondisk dquot directly. Therefore, for example,
74 * it is an error to call this twice, without purging the cache.
75 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +010076int
Linus Torvalds1da177e2005-04-16 15:20:36 -070077xfs_qm_scall_quotaoff(
78 xfs_mount_t *mp,
Christoph Hellwigfcafb712009-02-09 08:47:34 +010079 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 uint dqtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int error;
83 uint inactivate_flags;
84 xfs_qoff_logitem_t *qoffstart;
85 int nculprits;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /*
88 * No file system can have quotas enabled on disk but not in core.
89 * Note that quota utilities (like quotaoff) _expect_
90 * errno == EEXIST here.
91 */
92 if ((mp->m_qflags & flags) == 0)
93 return XFS_ERROR(EEXIST);
94 error = 0;
95
96 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
97
98 /*
99 * We don't want to deal with two quotaoffs messing up each other,
100 * so we're going to serialize it. quotaoff isn't exactly a performance
101 * critical thing.
102 * If quotaoff, then we must be dealing with the root filesystem.
103 */
104 ASSERT(mp->m_quotainfo);
105 if (mp->m_quotainfo)
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800106 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 ASSERT(mp->m_quotainfo);
109
110 /*
111 * If we're just turning off quota enforcement, change mp and go.
112 */
113 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
114 mp->m_qflags &= ~(flags);
115
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000116 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 mp->m_sb.sb_qflags = mp->m_qflags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000118 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
120
121 /* XXX what to do if error ? Revert back to old vals incore ? */
122 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
123 return (error);
124 }
125
126 dqtype = 0;
127 inactivate_flags = 0;
128 /*
129 * If accounting is off, we must turn enforcement off, clear the
130 * quota 'CHKD' certificate to make it known that we have to
131 * do a quotacheck the next time this quota is turned on.
132 */
133 if (flags & XFS_UQUOTA_ACCT) {
134 dqtype |= XFS_QMOPT_UQUOTA;
135 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
136 inactivate_flags |= XFS_UQUOTA_ACTIVE;
137 }
138 if (flags & XFS_GQUOTA_ACCT) {
139 dqtype |= XFS_QMOPT_GQUOTA;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000140 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 inactivate_flags |= XFS_GQUOTA_ACTIVE;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000142 } else if (flags & XFS_PQUOTA_ACCT) {
143 dqtype |= XFS_QMOPT_PQUOTA;
144 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
145 inactivate_flags |= XFS_PQUOTA_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 /*
149 * Nothing to do? Don't complain. This happens when we're just
150 * turning off quota enforcement.
151 */
152 if ((mp->m_qflags & flags) == 0) {
153 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
154 return (0);
155 }
156
157 /*
158 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
David Chinnercb6edc22008-04-10 12:20:45 +1000159 * and synchronously. If we fail to write, we should abort the
160 * operation as it cannot be recovered safely if we crash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
David Chinnercb6edc22008-04-10 12:20:45 +1000162 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
163 if (error)
164 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /*
167 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
168 * to take care of the race between dqget and quotaoff. We don't take
169 * any special locks to reset these bits. All processes need to check
170 * these bits *after* taking inode lock(s) to see if the particular
171 * quota type is in the process of being turned off. If *ACTIVE, it is
172 * guaranteed that all dquot structures and all quotainode ptrs will all
173 * stay valid as long as that inode is kept locked.
174 *
175 * There is no turning back after this.
176 */
177 mp->m_qflags &= ~inactivate_flags;
178
179 /*
180 * Give back all the dquot reference(s) held by inodes.
181 * Here we go thru every single incore inode in this file system, and
182 * do a dqrele on the i_udquot/i_gdquot that it may have.
183 * Essentially, as long as somebody has an inode locked, this guarantees
184 * that quotas will not be turned off. This is handy because in a
185 * transaction once we lock the inode(s) and check for quotaon, we can
186 * depend on the quota inodes (and other things) being valid as long as
187 * we keep the lock(s).
188 */
189 xfs_qm_dqrele_all_inodes(mp, flags);
190
191 /*
192 * Next we make the changes in the quota flag in the mount struct.
193 * This isn't protected by a particular lock directly, because we
194 * don't want to take a mrlock everytime we depend on quotas being on.
195 */
196 mp->m_qflags &= ~(flags);
197
198 /*
199 * Go through all the dquots of this file system and purge them,
200 * according to what was turned off. We may not be able to get rid
201 * of all dquots, because dquots can have temporary references that
202 * are not attached to inodes. eg. xfs_setattr, xfs_create.
203 * So, if we couldn't purge all the dquots from the filesystem,
204 * we can't get rid of the incore data structures.
205 */
206 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
207 delay(10 * nculprits);
208
209 /*
210 * Transactions that had started before ACTIVE state bit was cleared
211 * could have logged many dquots, so they'd have higher LSNs than
212 * the first QUOTAOFF log record does. If we happen to crash when
213 * the tail of the log has gone past the QUOTAOFF record, but
214 * before the last dquot modification, those dquots __will__
215 * recover, and that's not good.
216 *
217 * So, we have QUOTAOFF start and end logitems; the start
218 * logitem won't get overwritten until the end logitem appears...
219 */
David Chinnercb6edc22008-04-10 12:20:45 +1000220 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
221 if (error) {
222 /* We're screwed now. Shutdown is the only option. */
223 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
224 goto out_error;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /*
228 * If quotas is completely disabled, close shop.
229 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000230 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
231 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
233 xfs_qm_destroy_quotainfo(mp);
234 return (0);
235 }
236
237 /*
238 * Release our quotainode references, and vn_purge them,
239 * if we don't need them anymore.
240 */
241 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000242 IRELE(XFS_QI_UQIP(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 XFS_QI_UQIP(mp) = NULL;
244 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000245 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000246 IRELE(XFS_QI_GQIP(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 XFS_QI_GQIP(mp) = NULL;
248 }
David Chinnercb6edc22008-04-10 12:20:45 +1000249out_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
251
252 return (error);
253}
254
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100255int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256xfs_qm_scall_trunc_qfiles(
257 xfs_mount_t *mp,
258 uint flags)
259{
David Chinner88ab0202008-04-10 12:20:51 +1000260 int error = 0, error2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 xfs_inode_t *qip;
262
Eric Sandeen62118702008-03-06 13:44:28 +1100263 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
265 return XFS_ERROR(EINVAL);
266 }
267
268 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
269 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
David Chinner88ab0202008-04-10 12:20:51 +1000270 if (!error) {
271 error = xfs_truncate_file(mp, qip);
Christoph Hellwig43355092008-03-27 18:01:08 +1100272 IRELE(qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 }
275
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000276 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
277 mp->m_sb.sb_gquotino != NULLFSINO) {
David Chinner88ab0202008-04-10 12:20:51 +1000278 error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
279 if (!error2) {
280 error2 = xfs_truncate_file(mp, qip);
Christoph Hellwig43355092008-03-27 18:01:08 +1100281 IRELE(qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
284
David Chinner88ab0202008-04-10 12:20:51 +1000285 return error ? error : error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288
289/*
290 * Switch on (a given) quota enforcement for a filesystem. This takes
291 * effect immediately.
292 * (Switching on quota accounting must be done at mount time.)
293 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100294int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295xfs_qm_scall_quotaon(
296 xfs_mount_t *mp,
297 uint flags)
298{
299 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 uint qf;
301 uint accflags;
302 __int64_t sbflags;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
305 /*
306 * Switching on quota accounting must be done at mount time.
307 */
308 accflags = flags & XFS_ALL_QUOTA_ACCT;
309 flags &= ~(XFS_ALL_QUOTA_ACCT);
310
311 sbflags = 0;
312
313 if (flags == 0) {
314 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
315 return XFS_ERROR(EINVAL);
316 }
317
318 /* No fs can turn on quotas with a delayed effect */
319 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
320
321 /*
322 * Can't enforce without accounting. We check the superblock
323 * qflags here instead of m_qflags because rootfs can have
324 * quota acct on ondisk without m_qflags' knowing.
325 */
326 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
327 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
328 (flags & XFS_UQUOTA_ENFD))
329 ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000330 ((flags & XFS_PQUOTA_ACCT) == 0 &&
331 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
Donald Douwsma424ea91b2007-05-08 13:49:15 +1000332 (flags & XFS_GQUOTA_ACCT) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000334 (flags & XFS_OQUOTA_ENFD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
336 flags, mp->m_sb.sb_qflags);
337 return XFS_ERROR(EINVAL);
338 }
339 /*
340 * If everything's upto-date incore, then don't waste time.
341 */
342 if ((mp->m_qflags & flags) == flags)
343 return XFS_ERROR(EEXIST);
344
345 /*
346 * Change sb_qflags on disk but not incore mp->qflags
347 * if this is the root filesystem.
348 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000349 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 qf = mp->m_sb.sb_qflags;
351 mp->m_sb.sb_qflags = qf | flags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000352 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
355 * There's nothing to change if it's the same.
356 */
357 if ((qf & flags) == flags && sbflags == 0)
358 return XFS_ERROR(EEXIST);
359 sbflags |= XFS_SB_QFLAGS;
360
361 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
362 return (error);
363 /*
364 * If we aren't trying to switch on quota enforcement, we are done.
365 */
366 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
367 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000368 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
369 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
370 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
371 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 (flags & XFS_ALL_QUOTA_ENFD) == 0)
373 return (0);
374
375 if (! XFS_IS_QUOTA_RUNNING(mp))
376 return XFS_ERROR(ESRCH);
377
378 /*
379 * Switch on quota enforcement in core.
380 */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800381 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
383 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
384
385 return (0);
386}
387
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * Return quota status information, such as uquota-off, enforcements, etc.
391 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100392int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393xfs_qm_scall_getqstat(
394 xfs_mount_t *mp,
395 fs_quota_stat_t *out)
396{
397 xfs_inode_t *uip, *gip;
398 boolean_t tempuqip, tempgqip;
399
400 uip = gip = NULL;
401 tempuqip = tempgqip = B_FALSE;
402 memset(out, 0, sizeof(fs_quota_stat_t));
403
404 out->qs_version = FS_QSTAT_VERSION;
Eric Sandeen62118702008-03-06 13:44:28 +1100405 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 out->qs_uquota.qfs_ino = NULLFSINO;
407 out->qs_gquota.qfs_ino = NULLFSINO;
408 return (0);
409 }
410 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
411 (XFS_ALL_QUOTA_ACCT|
412 XFS_ALL_QUOTA_ENFD));
413 out->qs_pad = 0;
414 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
415 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
416
417 if (mp->m_quotainfo) {
418 uip = mp->m_quotainfo->qi_uquotaip;
419 gip = mp->m_quotainfo->qi_gquotaip;
420 }
421 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
422 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
423 0, 0, &uip, 0) == 0)
424 tempuqip = B_TRUE;
425 }
426 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
427 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
428 0, 0, &gip, 0) == 0)
429 tempgqip = B_TRUE;
430 }
431 if (uip) {
432 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
433 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
434 if (tempuqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100435 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 if (gip) {
438 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
439 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
440 if (tempgqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100441 IRELE(gip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 if (mp->m_quotainfo) {
444 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
445 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
446 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
447 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
448 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
449 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
450 }
451 return (0);
452}
453
454/*
455 * Adjust quota limits, and start/stop timers accordingly.
456 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100457int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458xfs_qm_scall_setqlim(
459 xfs_mount_t *mp,
460 xfs_dqid_t id,
461 uint type,
462 fs_disk_quota_t *newlim)
463{
464 xfs_disk_dquot_t *ddq;
465 xfs_dquot_t *dqp;
466 xfs_trans_t *tp;
467 int error;
468 xfs_qcnt_t hard, soft;
469
Nathan Scott754002b2005-06-21 15:49:06 +1000470 if ((newlim->d_fieldmask &
471 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return (0);
473
474 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
475 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
476 0, 0, XFS_DEFAULT_LOG_COUNT))) {
477 xfs_trans_cancel(tp, 0);
478 return (error);
479 }
480
481 /*
482 * We don't want to race with a quotaoff so take the quotaoff lock.
483 * (We don't hold an inode lock, so there's nothing else to stop
484 * a quotaoff from happening). (XXXThis doesn't currently happen
485 * because we take the vfslock before calling xfs_qm_sysent).
486 */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800487 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /*
490 * Get the dquot (locked), and join it to the transaction.
491 * Allocate the dquot if this doesn't exist.
492 */
493 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
494 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
495 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
496 ASSERT(error != ENOENT);
497 return (error);
498 }
499 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
500 xfs_trans_dqjoin(tp, dqp);
501 ddq = &dqp->q_core;
502
503 /*
504 * Make sure that hardlimits are >= soft limits before changing.
505 */
506 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
507 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100508 be64_to_cpu(ddq->d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
510 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100511 be64_to_cpu(ddq->d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100513 ddq->d_blk_hardlimit = cpu_to_be64(hard);
514 ddq->d_blk_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (id == 0) {
516 mp->m_quotainfo->qi_bhardlimit = hard;
517 mp->m_quotainfo->qi_bsoftlimit = soft;
518 }
519 } else {
520 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
521 }
522 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
523 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100524 be64_to_cpu(ddq->d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
526 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100527 be64_to_cpu(ddq->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100529 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
530 ddq->d_rtb_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (id == 0) {
532 mp->m_quotainfo->qi_rtbhardlimit = hard;
533 mp->m_quotainfo->qi_rtbsoftlimit = soft;
534 }
535 } else {
536 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
537 }
538
539 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
540 (xfs_qcnt_t) newlim->d_ino_hardlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100541 be64_to_cpu(ddq->d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
543 (xfs_qcnt_t) newlim->d_ino_softlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100544 be64_to_cpu(ddq->d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100546 ddq->d_ino_hardlimit = cpu_to_be64(hard);
547 ddq->d_ino_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (id == 0) {
549 mp->m_quotainfo->qi_ihardlimit = hard;
550 mp->m_quotainfo->qi_isoftlimit = soft;
551 }
552 } else {
553 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
554 }
555
Nathan Scott754002b2005-06-21 15:49:06 +1000556 /*
557 * Update warnings counter(s) if requested
558 */
559 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100560 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000561 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100562 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000563 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100564 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (id == 0) {
567 /*
568 * Timelimits for the super user set the relative time
569 * the other users can be over quota for this file system.
570 * If it is zero a default is used. Ditto for the default
Nathan Scott754002b2005-06-21 15:49:06 +1000571 * soft and hard limit values (already done, above), and
572 * for warnings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
574 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
575 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100576 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
579 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100580 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
583 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100584 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Nathan Scott754002b2005-06-21 15:49:06 +1000586 if (newlim->d_fieldmask & FS_DQ_BWARNS)
587 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
588 if (newlim->d_fieldmask & FS_DQ_IWARNS)
589 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
590 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
591 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
592 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
594 * If the user is now over quota, start the timelimit.
595 * The user will not be 'warned'.
596 * Note that we keep the timers ticking, whether enforcement
597 * is on or off. We don't really want to bother with iterating
598 * over all ondisk dquots and turning the timers on/off.
599 */
600 xfs_qm_adjust_dqtimers(mp, ddq);
601 }
602 dqp->dq_flags |= XFS_DQ_DIRTY;
603 xfs_trans_log_dquot(tp, dqp);
604
605 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
David Chinnere5720ee2008-04-10 12:21:18 +1000606 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 xfs_qm_dqprint(dqp);
608 xfs_qm_dqrele(dqp);
609 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
610
David Chinnere5720ee2008-04-10 12:21:18 +1000611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100614int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615xfs_qm_scall_getquota(
616 xfs_mount_t *mp,
617 xfs_dqid_t id,
618 uint type,
619 fs_disk_quota_t *out)
620{
621 xfs_dquot_t *dqp;
622 int error;
623
624 /*
625 * Try to get the dquot. We don't want it allocated on disk, so
626 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
627 * exist, we'll get ENOENT back.
628 */
629 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
630 return (error);
631 }
632
633 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
634 /*
635 * If everything's NULL, this dquot doesn't quite exist as far as
636 * our utility programs are concerned.
637 */
638 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
639 xfs_qm_dqput(dqp);
640 return XFS_ERROR(ENOENT);
641 }
642 /* xfs_qm_dqprint(dqp); */
643 /*
644 * Convert the disk dquot to the exportable format
645 */
646 xfs_qm_export_dquot(mp, &dqp->q_core, out);
647 xfs_qm_dqput(dqp);
648 return (error ? XFS_ERROR(EFAULT) : 0);
649}
650
651
652STATIC int
653xfs_qm_log_quotaoff_end(
654 xfs_mount_t *mp,
655 xfs_qoff_logitem_t *startqoff,
656 uint flags)
657{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000658 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000660 xfs_qoff_logitem_t *qoffi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
663
664 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
665 0, 0, XFS_DEFAULT_LOG_COUNT))) {
666 xfs_trans_cancel(tp, 0);
667 return (error);
668 }
669
670 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
671 flags & XFS_ALL_QUOTA_ACCT);
672 xfs_trans_log_quotaoff_item(tp, qoffi);
673
674 /*
675 * We have to make sure that the transaction is secure on disk before we
676 * return and actually stop quota accounting. So, make it synchronous.
677 * We don't care about quotoff's performance.
678 */
679 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000680 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return (error);
682}
683
684
685STATIC int
686xfs_qm_log_quotaoff(
687 xfs_mount_t *mp,
688 xfs_qoff_logitem_t **qoffstartp,
689 uint flags)
690{
691 xfs_trans_t *tp;
692 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 xfs_qoff_logitem_t *qoffi=NULL;
694 uint oldsbqflag=0;
695
696 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
697 if ((error = xfs_trans_reserve(tp, 0,
698 sizeof(xfs_qoff_logitem_t) * 2 +
699 mp->m_sb.sb_sectsize + 128,
700 0,
701 0,
702 XFS_DEFAULT_LOG_COUNT))) {
703 goto error0;
704 }
705
706 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
707 xfs_trans_log_quotaoff_item(tp, qoffi);
708
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000709 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 oldsbqflag = mp->m_sb.sb_qflags;
711 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000712 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 xfs_mod_sb(tp, XFS_SB_QFLAGS);
715
716 /*
717 * We have to make sure that the transaction is secure on disk before we
718 * return and actually stop quota accounting. So, make it synchronous.
719 * We don't care about quotoff's performance.
720 */
721 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000722 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724error0:
725 if (error) {
726 xfs_trans_cancel(tp, 0);
727 /*
728 * No one else is modifying sb_qflags, so this is OK.
729 * We still hold the quotaofflock.
730 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000731 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 mp->m_sb.sb_qflags = oldsbqflag;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000733 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 *qoffstartp = qoffi;
736 return (error);
737}
738
739
740/*
741 * Translate an internal style on-disk-dquot to the exportable format.
742 * The main differences are that the counters/limits are all in Basic
743 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
744 * to be converted to the native endianness.
745 */
746STATIC void
747xfs_qm_export_dquot(
748 xfs_mount_t *mp,
749 xfs_disk_dquot_t *src,
750 struct fs_disk_quota *dst)
751{
752 memset(dst, 0, sizeof(*dst));
753 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100754 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
755 dst->d_id = be32_to_cpu(src->d_id);
756 dst->d_blk_hardlimit =
757 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
758 dst->d_blk_softlimit =
759 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
760 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
761 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
762 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
763 dst->d_icount = be64_to_cpu(src->d_icount);
764 dst->d_btimer = be32_to_cpu(src->d_btimer);
765 dst->d_itimer = be32_to_cpu(src->d_itimer);
766 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
767 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
768 dst->d_rtb_hardlimit =
769 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
770 dst->d_rtb_softlimit =
771 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
772 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
773 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
774 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /*
777 * Internally, we don't reset all the timers when quota enforcement
Nathan Scottc41564b2006-03-29 08:55:14 +1000778 * gets turned off. No need to confuse the user level code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * so return zeroes in that case.
780 */
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000781 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
782 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
783 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 dst->d_btimer = 0;
785 dst->d_itimer = 0;
786 dst->d_rtbtimer = 0;
787 }
788
789#ifdef DEBUG
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000790 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
791 (XFS_IS_OQUOTA_ENFORCED(mp) &&
792 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
793 dst->d_id != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
795 (dst->d_blk_softlimit > 0)) {
796 ASSERT(dst->d_btimer != 0);
797 }
798 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
799 (dst->d_ino_softlimit > 0)) {
800 ASSERT(dst->d_itimer != 0);
801 }
802 }
803#endif
804}
805
806STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807xfs_qm_export_qtype_flags(
808 uint flags)
809{
810 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000811 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000813 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
814 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
815 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
816 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
817 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
818 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
819 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return (flags & XFS_DQ_USER) ?
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000822 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
823 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827xfs_qm_export_flags(
828 uint flags)
829{
830 uint uflags;
831
832 uflags = 0;
833 if (flags & XFS_UQUOTA_ACCT)
834 uflags |= XFS_QUOTA_UDQ_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000835 if (flags & XFS_PQUOTA_ACCT)
836 uflags |= XFS_QUOTA_PDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (flags & XFS_GQUOTA_ACCT)
838 uflags |= XFS_QUOTA_GDQ_ACCT;
839 if (flags & XFS_UQUOTA_ENFD)
840 uflags |= XFS_QUOTA_UDQ_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000841 if (flags & (XFS_OQUOTA_ENFD)) {
842 uflags |= (flags & XFS_GQUOTA_ACCT) ?
843 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return (uflags);
846}
847
848
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200849STATIC int
850xfs_dqrele_inode(
851 struct xfs_inode *ip,
852 struct xfs_perag *pag,
853 int flags)
David Chinner5b4d89a2008-10-30 17:08:03 +1100854{
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200855 int error;
David Chinner5b4d89a2008-10-30 17:08:03 +1100856
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200857 /* skip quota inodes */
858 if (ip == XFS_QI_UQIP(ip->i_mount) || ip == XFS_QI_GQIP(ip->i_mount)) {
859 ASSERT(ip->i_udquot == NULL);
860 ASSERT(ip->i_gdquot == NULL);
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100861 read_unlock(&pag->pag_ici_lock);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200862 return 0;
863 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100864
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200865 error = xfs_sync_inode_valid(ip, pag);
866 if (error)
867 return error;
Dave Chinner63070912008-11-10 17:13:23 +1100868
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200869 xfs_ilock(ip, XFS_ILOCK_EXCL);
870 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
871 xfs_qm_dqrele(ip->i_udquot);
872 ip->i_udquot = NULL;
873 }
874 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
875 xfs_qm_dqrele(ip->i_gdquot);
876 ip->i_gdquot = NULL;
877 }
878 xfs_iput(ip, XFS_ILOCK_EXCL);
879 IRELE(ip);
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100880
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200881 return 0;
David Chinner5b4d89a2008-10-30 17:08:03 +1100882}
883
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200884
David Chinner5b4d89a2008-10-30 17:08:03 +1100885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Go thru all the inodes in the file system, releasing their dquots.
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200887 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * Note that the mount structure gets modified to indicate that quotas are off
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200889 * AFTER this, in the case of quotaoff.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891void
892xfs_qm_dqrele_all_inodes(
893 struct xfs_mount *mp,
894 uint flags)
895{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 ASSERT(mp->m_quotainfo);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200897 xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, XFS_ICI_NO_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*------------------------------------------------------------------------*/
901#ifdef DEBUG
902/*
903 * This contains all the test functions for XFS disk quotas.
904 * Currently it does a quota accounting check. ie. it walks through
905 * all inodes in the file system, calculating the dquot accounting fields,
906 * and prints out any inconsistencies.
907 */
908xfs_dqhash_t *qmtest_udqtab;
909xfs_dqhash_t *qmtest_gdqtab;
910int qmtest_hashmask;
911int qmtest_nfails;
Christoph Hellwiga0b0b8a2009-03-29 09:51:00 +0200912struct mutex qcheck_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914#define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
915 (__psunsigned_t)(id)) & \
916 (qmtest_hashmask - 1))
917
918#define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
919 (qmtest_udqtab + \
920 DQTEST_HASHVAL(mp, id)) : \
921 (qmtest_gdqtab + \
922 DQTEST_HASHVAL(mp, id)))
923
924#define DQTEST_LIST_PRINT(l, NXT, title) \
925{ \
926 xfs_dqtest_t *dqp; int i = 0;\
927 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
928 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
929 dqp = (xfs_dqtest_t *)dqp->NXT) { \
930 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
931 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
932 dqp->d_bcount, dqp->d_icount); } \
933}
934
935typedef struct dqtest {
936 xfs_dqmarker_t q_lists;
937 xfs_dqhash_t *q_hash; /* the hashchain header */
938 xfs_mount_t *q_mount; /* filesystem this relates to */
939 xfs_dqid_t d_id; /* user id or group id */
940 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
941 xfs_qcnt_t d_icount; /* # inodes owned by the user */
942} xfs_dqtest_t;
943
944STATIC void
945xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
946{
947 xfs_dquot_t *d;
948 if (((d) = (h)->qh_next))
949 (d)->HL_PREVP = &((dqp)->HL_NEXT);
950 (dqp)->HL_NEXT = d;
951 (dqp)->HL_PREVP = &((h)->qh_next);
952 (h)->qh_next = (xfs_dquot_t *)dqp;
953 (h)->qh_version++;
954 (h)->qh_nelems++;
955}
956STATIC void
957xfs_qm_dqtest_print(
958 xfs_dqtest_t *d)
959{
960 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
961 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
963 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
964 d->d_bcount, (int)d->d_bcount);
965 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
966 d->d_icount, (int)d->d_icount);
967 cmn_err(CE_DEBUG, "---------------------------");
968}
969
970STATIC void
971xfs_qm_dqtest_failed(
972 xfs_dqtest_t *d,
973 xfs_dquot_t *dqp,
974 char *reason,
975 xfs_qcnt_t a,
976 xfs_qcnt_t b,
977 int error)
978{
979 qmtest_nfails++;
980 if (error)
981 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100982 d->d_id, error, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 else
984 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100985 d->d_id, reason, (int)a, (int)b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 xfs_qm_dqtest_print(d);
987 if (dqp)
988 xfs_qm_dqprint(dqp);
989}
990
991STATIC int
992xfs_dqtest_cmp2(
993 xfs_dqtest_t *d,
994 xfs_dquot_t *dqp)
995{
996 int err = 0;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100997 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +1100999 be64_to_cpu(dqp->q_core.d_icount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 d->d_icount, 0);
1001 err++;
1002 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001003 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001005 be64_to_cpu(dqp->q_core.d_bcount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 d->d_bcount, 0);
1007 err++;
1008 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001009 if (dqp->q_core.d_blk_softlimit &&
1010 be64_to_cpu(dqp->q_core.d_bcount) >=
1011 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1013 cmn_err(CE_DEBUG,
1014 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1015 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1016 err++;
1017 }
1018 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001019 if (dqp->q_core.d_ino_softlimit &&
1020 be64_to_cpu(dqp->q_core.d_icount) >=
1021 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1023 cmn_err(CE_DEBUG,
1024 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1025 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1026 err++;
1027 }
1028 }
1029#ifdef QUOTADEBUG
1030 if (!err) {
1031 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001032 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034#endif
1035 return (err);
1036}
1037
1038STATIC void
1039xfs_dqtest_cmp(
1040 xfs_dqtest_t *d)
1041{
1042 xfs_dquot_t *dqp;
1043 int error;
1044
1045 /* xfs_qm_dqtest_print(d); */
1046 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1047 &dqp))) {
1048 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1049 return;
1050 }
1051 xfs_dqtest_cmp2(d, dqp);
1052 xfs_qm_dqput(dqp);
1053}
1054
1055STATIC int
1056xfs_qm_internalqcheck_dqget(
1057 xfs_mount_t *mp,
1058 xfs_dqid_t id,
1059 uint type,
1060 xfs_dqtest_t **O_dq)
1061{
1062 xfs_dqtest_t *d;
1063 xfs_dqhash_t *h;
1064
1065 h = DQTEST_HASH(mp, id, type);
1066 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1067 d = (xfs_dqtest_t *) d->HL_NEXT) {
1068 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1069 if (d->d_id == id && mp == d->q_mount) {
1070 *O_dq = d;
1071 return (0);
1072 }
1073 }
1074 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1075 d->dq_flags = type;
1076 d->d_id = id;
1077 d->q_mount = mp;
1078 d->q_hash = h;
1079 xfs_qm_hashinsert(h, d);
1080 *O_dq = d;
1081 return (0);
1082}
1083
1084STATIC void
1085xfs_qm_internalqcheck_get_dquots(
1086 xfs_mount_t *mp,
1087 xfs_dqid_t uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001088 xfs_dqid_t projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 xfs_dqid_t gid,
1090 xfs_dqtest_t **ud,
1091 xfs_dqtest_t **gd)
1092{
1093 if (XFS_IS_UQUOTA_ON(mp))
1094 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1095 if (XFS_IS_GQUOTA_ON(mp))
1096 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001097 else if (XFS_IS_PQUOTA_ON(mp))
1098 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101
1102STATIC void
1103xfs_qm_internalqcheck_dqadjust(
1104 xfs_inode_t *ip,
1105 xfs_dqtest_t *d)
1106{
1107 d->d_icount++;
1108 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1109}
1110
1111STATIC int
1112xfs_qm_internalqcheck_adjust(
1113 xfs_mount_t *mp, /* mount point for filesystem */
1114 xfs_ino_t ino, /* inode number to get data for */
1115 void __user *buffer, /* not used */
1116 int ubsize, /* not used */
1117 void *private_data, /* not used */
1118 xfs_daddr_t bno, /* starting block of inode cluster */
1119 int *ubused, /* not used */
1120 void *dip, /* not used */
1121 int *res) /* bulkstat result code */
1122{
1123 xfs_inode_t *ip;
1124 xfs_dqtest_t *ud, *gd;
1125 uint lock_flags;
1126 boolean_t ipreleased;
1127 int error;
1128
1129 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1130
1131 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1132 *res = BULKSTAT_RV_NOTHING;
1133 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1134 (unsigned long long) ino,
1135 (unsigned long long) mp->m_sb.sb_uquotino,
1136 (unsigned long long) mp->m_sb.sb_gquotino);
1137 return XFS_ERROR(EINVAL);
1138 }
1139 ipreleased = B_FALSE;
1140 again:
1141 lock_flags = XFS_ILOCK_SHARED;
1142 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1143 *res = BULKSTAT_RV_NOTHING;
1144 return (error);
1145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /*
1148 * This inode can have blocks after eof which can get released
1149 * when we send it to inactive. Since we don't check the dquot
1150 * until the after all our calculations are done, we must get rid
1151 * of those now.
1152 */
1153 if (! ipreleased) {
1154 xfs_iput(ip, lock_flags);
1155 ipreleased = B_TRUE;
1156 goto again;
1157 }
1158 xfs_qm_internalqcheck_get_dquots(mp,
1159 (xfs_dqid_t) ip->i_d.di_uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001160 (xfs_dqid_t) ip->i_d.di_projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 (xfs_dqid_t) ip->i_d.di_gid,
1162 &ud, &gd);
1163 if (XFS_IS_UQUOTA_ON(mp)) {
1164 ASSERT(ud);
1165 xfs_qm_internalqcheck_dqadjust(ip, ud);
1166 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001167 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ASSERT(gd);
1169 xfs_qm_internalqcheck_dqadjust(ip, gd);
1170 }
1171 xfs_iput(ip, lock_flags);
1172 *res = BULKSTAT_RV_DIDONE;
1173 return (0);
1174}
1175
1176
1177/* PRIVATE, debugging */
1178int
1179xfs_qm_internalqcheck(
1180 xfs_mount_t *mp)
1181{
1182 xfs_ino_t lastino;
1183 int done, count;
1184 int i;
1185 xfs_dqtest_t *d, *e;
1186 xfs_dqhash_t *h1;
1187 int error;
1188
1189 lastino = 0;
1190 qmtest_hashmask = 32;
1191 count = 5;
1192 done = 0;
1193 qmtest_nfails = 0;
1194
1195 if (! XFS_IS_QUOTA_ON(mp))
1196 return XFS_ERROR(ESRCH);
1197
1198 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1199 XFS_bflush(mp->m_ddev_targp);
1200 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1201 XFS_bflush(mp->m_ddev_targp);
1202
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001203 mutex_lock(&qcheck_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* There should be absolutely no quota activity while this
1205 is going on. */
1206 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1207 sizeof(xfs_dqhash_t), KM_SLEEP);
1208 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1209 sizeof(xfs_dqhash_t), KM_SLEEP);
1210 do {
1211 /*
1212 * Iterate thru all the inodes in the file system,
1213 * adjusting the corresponding dquot counters
1214 */
1215 if ((error = xfs_bulkstat(mp, &lastino, &count,
1216 xfs_qm_internalqcheck_adjust, NULL,
1217 0, NULL, BULKSTAT_FG_IGET, &done))) {
1218 break;
1219 }
1220 } while (! done);
1221 if (error) {
1222 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1223 }
1224 cmn_err(CE_DEBUG, "Checking results against system dquots");
1225 for (i = 0; i < qmtest_hashmask; i++) {
1226 h1 = &qmtest_udqtab[i];
1227 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1228 xfs_dqtest_cmp(d);
1229 e = (xfs_dqtest_t *) d->HL_NEXT;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001230 kmem_free(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 d = e;
1232 }
1233 h1 = &qmtest_gdqtab[i];
1234 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1235 xfs_dqtest_cmp(d);
1236 e = (xfs_dqtest_t *) d->HL_NEXT;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001237 kmem_free(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 d = e;
1239 }
1240 }
1241
1242 if (qmtest_nfails) {
1243 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1244 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1245 } else {
1246 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1247 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001248 kmem_free(qmtest_udqtab);
1249 kmem_free(qmtest_gdqtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 mutex_unlock(&qcheck_lock);
1251 return (qmtest_nfails);
1252}
1253
1254#endif /* DEBUG */