Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
4ce3121f NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
4ce3121f NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
4ce3121f NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
4ce3121f NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
16f7e0fe RD |
18 | |
19 | #include <linux/capability.h> | |
20 | ||
1da177e4 LT |
21 | #include "xfs.h" |
22 | #include "xfs_fs.h" | |
a844f451 | 23 | #include "xfs_bit.h" |
1da177e4 | 24 | #include "xfs_log.h" |
a844f451 | 25 | #include "xfs_inum.h" |
1da177e4 LT |
26 | #include "xfs_trans.h" |
27 | #include "xfs_sb.h" | |
a844f451 | 28 | #include "xfs_ag.h" |
1da177e4 | 29 | #include "xfs_alloc.h" |
1da177e4 LT |
30 | #include "xfs_quota.h" |
31 | #include "xfs_mount.h" | |
1da177e4 | 32 | #include "xfs_bmap_btree.h" |
1da177e4 | 33 | #include "xfs_inode.h" |
a844f451 | 34 | #include "xfs_itable.h" |
1da177e4 | 35 | #include "xfs_bmap.h" |
1da177e4 LT |
36 | #include "xfs_rtalloc.h" |
37 | #include "xfs_error.h" | |
1da177e4 LT |
38 | #include "xfs_attr.h" |
39 | #include "xfs_buf_item.h" | |
40 | #include "xfs_utils.h" | |
1da177e4 | 41 | #include "xfs_qm.h" |
0b1b213f | 42 | #include "xfs_trace.h" |
1da177e4 LT |
43 | |
44 | #ifdef DEBUG | |
45 | # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args) | |
46 | #else | |
47 | # define qdprintk(s, args...) do { } while (0) | |
48 | #endif | |
49 | ||
1da177e4 LT |
50 | STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint); |
51 | STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *, | |
52 | uint); | |
1da177e4 | 53 | STATIC uint xfs_qm_export_flags(uint); |
1da177e4 LT |
54 | STATIC uint xfs_qm_export_qtype_flags(uint); |
55 | STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *, | |
56 | fs_disk_quota_t *); | |
57 | ||
58 | ||
1da177e4 LT |
59 | /* |
60 | * Turn off quota accounting and/or enforcement for all udquots and/or | |
61 | * gdquots. Called only at unmount time. | |
62 | * | |
63 | * This assumes that there are no dquots of this file system cached | |
64 | * incore, and modifies the ondisk dquot directly. Therefore, for example, | |
65 | * it is an error to call this twice, without purging the cache. | |
66 | */ | |
fcafb71b | 67 | int |
1da177e4 LT |
68 | xfs_qm_scall_quotaoff( |
69 | xfs_mount_t *mp, | |
fcafb71b | 70 | uint flags) |
1da177e4 | 71 | { |
8a7b8a89 | 72 | struct xfs_quotainfo *q = mp->m_quotainfo; |
1da177e4 | 73 | uint dqtype; |
1da177e4 LT |
74 | int error; |
75 | uint inactivate_flags; | |
76 | xfs_qoff_logitem_t *qoffstart; | |
77 | int nculprits; | |
78 | ||
1da177e4 LT |
79 | /* |
80 | * No file system can have quotas enabled on disk but not in core. | |
81 | * Note that quota utilities (like quotaoff) _expect_ | |
82 | * errno == EEXIST here. | |
83 | */ | |
84 | if ((mp->m_qflags & flags) == 0) | |
85 | return XFS_ERROR(EEXIST); | |
86 | error = 0; | |
87 | ||
88 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); | |
89 | ||
90 | /* | |
91 | * We don't want to deal with two quotaoffs messing up each other, | |
92 | * so we're going to serialize it. quotaoff isn't exactly a performance | |
93 | * critical thing. | |
94 | * If quotaoff, then we must be dealing with the root filesystem. | |
95 | */ | |
8a7b8a89 CH |
96 | ASSERT(q); |
97 | mutex_lock(&q->qi_quotaofflock); | |
1da177e4 LT |
98 | |
99 | /* | |
100 | * If we're just turning off quota enforcement, change mp and go. | |
101 | */ | |
102 | if ((flags & XFS_ALL_QUOTA_ACCT) == 0) { | |
103 | mp->m_qflags &= ~(flags); | |
104 | ||
3685c2a1 | 105 | spin_lock(&mp->m_sb_lock); |
1da177e4 | 106 | mp->m_sb.sb_qflags = mp->m_qflags; |
3685c2a1 | 107 | spin_unlock(&mp->m_sb_lock); |
8a7b8a89 | 108 | mutex_unlock(&q->qi_quotaofflock); |
1da177e4 LT |
109 | |
110 | /* XXX what to do if error ? Revert back to old vals incore ? */ | |
111 | error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS); | |
112 | return (error); | |
113 | } | |
114 | ||
115 | dqtype = 0; | |
116 | inactivate_flags = 0; | |
117 | /* | |
118 | * If accounting is off, we must turn enforcement off, clear the | |
119 | * quota 'CHKD' certificate to make it known that we have to | |
120 | * do a quotacheck the next time this quota is turned on. | |
121 | */ | |
122 | if (flags & XFS_UQUOTA_ACCT) { | |
123 | dqtype |= XFS_QMOPT_UQUOTA; | |
124 | flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD); | |
125 | inactivate_flags |= XFS_UQUOTA_ACTIVE; | |
126 | } | |
127 | if (flags & XFS_GQUOTA_ACCT) { | |
128 | dqtype |= XFS_QMOPT_GQUOTA; | |
c8ad20ff | 129 | flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD); |
1da177e4 | 130 | inactivate_flags |= XFS_GQUOTA_ACTIVE; |
c8ad20ff NS |
131 | } else if (flags & XFS_PQUOTA_ACCT) { |
132 | dqtype |= XFS_QMOPT_PQUOTA; | |
133 | flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD); | |
134 | inactivate_flags |= XFS_PQUOTA_ACTIVE; | |
1da177e4 LT |
135 | } |
136 | ||
137 | /* | |
138 | * Nothing to do? Don't complain. This happens when we're just | |
139 | * turning off quota enforcement. | |
140 | */ | |
8a7b8a89 CH |
141 | if ((mp->m_qflags & flags) == 0) |
142 | goto out_unlock; | |
1da177e4 LT |
143 | |
144 | /* | |
145 | * Write the LI_QUOTAOFF log record, and do SB changes atomically, | |
cb6edc26 DC |
146 | * and synchronously. If we fail to write, we should abort the |
147 | * operation as it cannot be recovered safely if we crash. | |
1da177e4 | 148 | */ |
cb6edc26 DC |
149 | error = xfs_qm_log_quotaoff(mp, &qoffstart, flags); |
150 | if (error) | |
8a7b8a89 | 151 | goto out_unlock; |
1da177e4 LT |
152 | |
153 | /* | |
154 | * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct | |
155 | * to take care of the race between dqget and quotaoff. We don't take | |
156 | * any special locks to reset these bits. All processes need to check | |
157 | * these bits *after* taking inode lock(s) to see if the particular | |
158 | * quota type is in the process of being turned off. If *ACTIVE, it is | |
159 | * guaranteed that all dquot structures and all quotainode ptrs will all | |
160 | * stay valid as long as that inode is kept locked. | |
161 | * | |
162 | * There is no turning back after this. | |
163 | */ | |
164 | mp->m_qflags &= ~inactivate_flags; | |
165 | ||
166 | /* | |
167 | * Give back all the dquot reference(s) held by inodes. | |
168 | * Here we go thru every single incore inode in this file system, and | |
169 | * do a dqrele on the i_udquot/i_gdquot that it may have. | |
170 | * Essentially, as long as somebody has an inode locked, this guarantees | |
171 | * that quotas will not be turned off. This is handy because in a | |
172 | * transaction once we lock the inode(s) and check for quotaon, we can | |
173 | * depend on the quota inodes (and other things) being valid as long as | |
174 | * we keep the lock(s). | |
175 | */ | |
176 | xfs_qm_dqrele_all_inodes(mp, flags); | |
177 | ||
178 | /* | |
179 | * Next we make the changes in the quota flag in the mount struct. | |
180 | * This isn't protected by a particular lock directly, because we | |
181 | * don't want to take a mrlock everytime we depend on quotas being on. | |
182 | */ | |
183 | mp->m_qflags &= ~(flags); | |
184 | ||
185 | /* | |
186 | * Go through all the dquots of this file system and purge them, | |
187 | * according to what was turned off. We may not be able to get rid | |
188 | * of all dquots, because dquots can have temporary references that | |
189 | * are not attached to inodes. eg. xfs_setattr, xfs_create. | |
190 | * So, if we couldn't purge all the dquots from the filesystem, | |
191 | * we can't get rid of the incore data structures. | |
192 | */ | |
8112e9dc | 193 | while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype))) |
1da177e4 LT |
194 | delay(10 * nculprits); |
195 | ||
196 | /* | |
197 | * Transactions that had started before ACTIVE state bit was cleared | |
198 | * could have logged many dquots, so they'd have higher LSNs than | |
199 | * the first QUOTAOFF log record does. If we happen to crash when | |
200 | * the tail of the log has gone past the QUOTAOFF record, but | |
201 | * before the last dquot modification, those dquots __will__ | |
202 | * recover, and that's not good. | |
203 | * | |
204 | * So, we have QUOTAOFF start and end logitems; the start | |
205 | * logitem won't get overwritten until the end logitem appears... | |
206 | */ | |
cb6edc26 DC |
207 | error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags); |
208 | if (error) { | |
209 | /* We're screwed now. Shutdown is the only option. */ | |
210 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); | |
8a7b8a89 | 211 | goto out_unlock; |
cb6edc26 | 212 | } |
1da177e4 LT |
213 | |
214 | /* | |
215 | * If quotas is completely disabled, close shop. | |
216 | */ | |
c8ad20ff NS |
217 | if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) || |
218 | ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) { | |
8a7b8a89 | 219 | mutex_unlock(&q->qi_quotaofflock); |
1da177e4 LT |
220 | xfs_qm_destroy_quotainfo(mp); |
221 | return (0); | |
222 | } | |
223 | ||
224 | /* | |
8a7b8a89 | 225 | * Release our quotainode references if we don't need them anymore. |
1da177e4 | 226 | */ |
8a7b8a89 CH |
227 | if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) { |
228 | IRELE(q->qi_uquotaip); | |
229 | q->qi_uquotaip = NULL; | |
1da177e4 | 230 | } |
8a7b8a89 CH |
231 | if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && q->qi_gquotaip) { |
232 | IRELE(q->qi_gquotaip); | |
233 | q->qi_gquotaip = NULL; | |
1da177e4 | 234 | } |
1da177e4 | 235 | |
8a7b8a89 CH |
236 | out_unlock: |
237 | mutex_unlock(&q->qi_quotaofflock); | |
238 | return error; | |
1da177e4 LT |
239 | } |
240 | ||
fcafb71b | 241 | int |
1da177e4 LT |
242 | xfs_qm_scall_trunc_qfiles( |
243 | xfs_mount_t *mp, | |
244 | uint flags) | |
245 | { | |
88ab0208 | 246 | int error = 0, error2 = 0; |
1da177e4 LT |
247 | xfs_inode_t *qip; |
248 | ||
62118709 | 249 | if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) { |
1da177e4 LT |
250 | qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags); |
251 | return XFS_ERROR(EINVAL); | |
252 | } | |
253 | ||
254 | if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) { | |
7b6259e7 | 255 | error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip); |
88ab0208 DC |
256 | if (!error) { |
257 | error = xfs_truncate_file(mp, qip); | |
43355099 | 258 | IRELE(qip); |
1da177e4 LT |
259 | } |
260 | } | |
261 | ||
c8ad20ff NS |
262 | if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) && |
263 | mp->m_sb.sb_gquotino != NULLFSINO) { | |
7b6259e7 | 264 | error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip); |
88ab0208 DC |
265 | if (!error2) { |
266 | error2 = xfs_truncate_file(mp, qip); | |
43355099 | 267 | IRELE(qip); |
1da177e4 LT |
268 | } |
269 | } | |
270 | ||
88ab0208 | 271 | return error ? error : error2; |
1da177e4 LT |
272 | } |
273 | ||
274 | ||
275 | /* | |
276 | * Switch on (a given) quota enforcement for a filesystem. This takes | |
277 | * effect immediately. | |
278 | * (Switching on quota accounting must be done at mount time.) | |
279 | */ | |
fcafb71b | 280 | int |
1da177e4 LT |
281 | xfs_qm_scall_quotaon( |
282 | xfs_mount_t *mp, | |
283 | uint flags) | |
284 | { | |
285 | int error; | |
1da177e4 LT |
286 | uint qf; |
287 | uint accflags; | |
288 | __int64_t sbflags; | |
289 | ||
1da177e4 LT |
290 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); |
291 | /* | |
292 | * Switching on quota accounting must be done at mount time. | |
293 | */ | |
294 | accflags = flags & XFS_ALL_QUOTA_ACCT; | |
295 | flags &= ~(XFS_ALL_QUOTA_ACCT); | |
296 | ||
297 | sbflags = 0; | |
298 | ||
299 | if (flags == 0) { | |
300 | qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags); | |
301 | return XFS_ERROR(EINVAL); | |
302 | } | |
303 | ||
304 | /* No fs can turn on quotas with a delayed effect */ | |
305 | ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0); | |
306 | ||
307 | /* | |
308 | * Can't enforce without accounting. We check the superblock | |
309 | * qflags here instead of m_qflags because rootfs can have | |
310 | * quota acct on ondisk without m_qflags' knowing. | |
311 | */ | |
312 | if (((flags & XFS_UQUOTA_ACCT) == 0 && | |
313 | (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 && | |
314 | (flags & XFS_UQUOTA_ENFD)) | |
315 | || | |
c8ad20ff NS |
316 | ((flags & XFS_PQUOTA_ACCT) == 0 && |
317 | (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 && | |
424ea91b | 318 | (flags & XFS_GQUOTA_ACCT) == 0 && |
1da177e4 | 319 | (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 && |
c8ad20ff | 320 | (flags & XFS_OQUOTA_ENFD))) { |
1da177e4 LT |
321 | qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n", |
322 | flags, mp->m_sb.sb_qflags); | |
323 | return XFS_ERROR(EINVAL); | |
324 | } | |
325 | /* | |
326 | * If everything's upto-date incore, then don't waste time. | |
327 | */ | |
328 | if ((mp->m_qflags & flags) == flags) | |
329 | return XFS_ERROR(EEXIST); | |
330 | ||
331 | /* | |
332 | * Change sb_qflags on disk but not incore mp->qflags | |
333 | * if this is the root filesystem. | |
334 | */ | |
3685c2a1 | 335 | spin_lock(&mp->m_sb_lock); |
1da177e4 LT |
336 | qf = mp->m_sb.sb_qflags; |
337 | mp->m_sb.sb_qflags = qf | flags; | |
3685c2a1 | 338 | spin_unlock(&mp->m_sb_lock); |
1da177e4 LT |
339 | |
340 | /* | |
341 | * There's nothing to change if it's the same. | |
342 | */ | |
343 | if ((qf & flags) == flags && sbflags == 0) | |
344 | return XFS_ERROR(EEXIST); | |
345 | sbflags |= XFS_SB_QFLAGS; | |
346 | ||
347 | if ((error = xfs_qm_write_sb_changes(mp, sbflags))) | |
348 | return (error); | |
349 | /* | |
350 | * If we aren't trying to switch on quota enforcement, we are done. | |
351 | */ | |
352 | if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) != | |
353 | (mp->m_qflags & XFS_UQUOTA_ACCT)) || | |
c8ad20ff NS |
354 | ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) != |
355 | (mp->m_qflags & XFS_PQUOTA_ACCT)) || | |
356 | ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) != | |
357 | (mp->m_qflags & XFS_GQUOTA_ACCT)) || | |
1da177e4 LT |
358 | (flags & XFS_ALL_QUOTA_ENFD) == 0) |
359 | return (0); | |
360 | ||
361 | if (! XFS_IS_QUOTA_RUNNING(mp)) | |
362 | return XFS_ERROR(ESRCH); | |
363 | ||
364 | /* | |
365 | * Switch on quota enforcement in core. | |
366 | */ | |
8a7b8a89 | 367 | mutex_lock(&mp->m_quotainfo->qi_quotaofflock); |
1da177e4 | 368 | mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD); |
8a7b8a89 | 369 | mutex_unlock(&mp->m_quotainfo->qi_quotaofflock); |
1da177e4 LT |
370 | |
371 | return (0); | |
372 | } | |
373 | ||
374 | ||
1da177e4 LT |
375 | /* |
376 | * Return quota status information, such as uquota-off, enforcements, etc. | |
377 | */ | |
fcafb71b | 378 | int |
1da177e4 | 379 | xfs_qm_scall_getqstat( |
8a7b8a89 CH |
380 | struct xfs_mount *mp, |
381 | struct fs_quota_stat *out) | |
1da177e4 | 382 | { |
8a7b8a89 CH |
383 | struct xfs_quotainfo *q = mp->m_quotainfo; |
384 | struct xfs_inode *uip, *gip; | |
385 | boolean_t tempuqip, tempgqip; | |
1da177e4 LT |
386 | |
387 | uip = gip = NULL; | |
388 | tempuqip = tempgqip = B_FALSE; | |
389 | memset(out, 0, sizeof(fs_quota_stat_t)); | |
390 | ||
391 | out->qs_version = FS_QSTAT_VERSION; | |
62118709 | 392 | if (!xfs_sb_version_hasquota(&mp->m_sb)) { |
1da177e4 LT |
393 | out->qs_uquota.qfs_ino = NULLFSINO; |
394 | out->qs_gquota.qfs_ino = NULLFSINO; | |
395 | return (0); | |
396 | } | |
397 | out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags & | |
398 | (XFS_ALL_QUOTA_ACCT| | |
399 | XFS_ALL_QUOTA_ENFD)); | |
400 | out->qs_pad = 0; | |
401 | out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino; | |
402 | out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino; | |
403 | ||
8a7b8a89 CH |
404 | if (q) { |
405 | uip = q->qi_uquotaip; | |
406 | gip = q->qi_gquotaip; | |
1da177e4 LT |
407 | } |
408 | if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) { | |
409 | if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, | |
7b6259e7 | 410 | 0, 0, &uip) == 0) |
1da177e4 LT |
411 | tempuqip = B_TRUE; |
412 | } | |
413 | if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) { | |
414 | if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, | |
7b6259e7 | 415 | 0, 0, &gip) == 0) |
1da177e4 LT |
416 | tempgqip = B_TRUE; |
417 | } | |
418 | if (uip) { | |
419 | out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks; | |
420 | out->qs_uquota.qfs_nextents = uip->i_d.di_nextents; | |
421 | if (tempuqip) | |
43355099 | 422 | IRELE(uip); |
1da177e4 LT |
423 | } |
424 | if (gip) { | |
425 | out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks; | |
426 | out->qs_gquota.qfs_nextents = gip->i_d.di_nextents; | |
427 | if (tempgqip) | |
43355099 | 428 | IRELE(gip); |
1da177e4 | 429 | } |
8a7b8a89 CH |
430 | if (q) { |
431 | out->qs_incoredqs = q->qi_dquots; | |
432 | out->qs_btimelimit = q->qi_btimelimit; | |
433 | out->qs_itimelimit = q->qi_itimelimit; | |
434 | out->qs_rtbtimelimit = q->qi_rtbtimelimit; | |
435 | out->qs_bwarnlimit = q->qi_bwarnlimit; | |
436 | out->qs_iwarnlimit = q->qi_iwarnlimit; | |
1da177e4 | 437 | } |
8a7b8a89 | 438 | return 0; |
1da177e4 LT |
439 | } |
440 | ||
c472b432 CH |
441 | #define XFS_DQ_MASK \ |
442 | (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_WARNS_MASK) | |
443 | ||
1da177e4 LT |
444 | /* |
445 | * Adjust quota limits, and start/stop timers accordingly. | |
446 | */ | |
fcafb71b | 447 | int |
1da177e4 LT |
448 | xfs_qm_scall_setqlim( |
449 | xfs_mount_t *mp, | |
450 | xfs_dqid_t id, | |
451 | uint type, | |
452 | fs_disk_quota_t *newlim) | |
453 | { | |
8a7b8a89 | 454 | struct xfs_quotainfo *q = mp->m_quotainfo; |
1da177e4 LT |
455 | xfs_disk_dquot_t *ddq; |
456 | xfs_dquot_t *dqp; | |
457 | xfs_trans_t *tp; | |
458 | int error; | |
459 | xfs_qcnt_t hard, soft; | |
460 | ||
c472b432 CH |
461 | if (newlim->d_fieldmask & ~XFS_DQ_MASK) |
462 | return EINVAL; | |
463 | if ((newlim->d_fieldmask & XFS_DQ_MASK) == 0) | |
464 | return 0; | |
1da177e4 LT |
465 | |
466 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM); | |
467 | if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128, | |
468 | 0, 0, XFS_DEFAULT_LOG_COUNT))) { | |
469 | xfs_trans_cancel(tp, 0); | |
470 | return (error); | |
471 | } | |
472 | ||
473 | /* | |
474 | * We don't want to race with a quotaoff so take the quotaoff lock. | |
475 | * (We don't hold an inode lock, so there's nothing else to stop | |
476 | * a quotaoff from happening). (XXXThis doesn't currently happen | |
477 | * because we take the vfslock before calling xfs_qm_sysent). | |
478 | */ | |
8a7b8a89 | 479 | mutex_lock(&q->qi_quotaofflock); |
1da177e4 LT |
480 | |
481 | /* | |
482 | * Get the dquot (locked), and join it to the transaction. | |
483 | * Allocate the dquot if this doesn't exist. | |
484 | */ | |
485 | if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) { | |
486 | xfs_trans_cancel(tp, XFS_TRANS_ABORT); | |
1da177e4 | 487 | ASSERT(error != ENOENT); |
8a7b8a89 | 488 | goto out_unlock; |
1da177e4 | 489 | } |
1da177e4 LT |
490 | xfs_trans_dqjoin(tp, dqp); |
491 | ddq = &dqp->q_core; | |
492 | ||
493 | /* | |
494 | * Make sure that hardlimits are >= soft limits before changing. | |
495 | */ | |
496 | hard = (newlim->d_fieldmask & FS_DQ_BHARD) ? | |
497 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) : | |
1149d96a | 498 | be64_to_cpu(ddq->d_blk_hardlimit); |
1da177e4 LT |
499 | soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ? |
500 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) : | |
1149d96a | 501 | be64_to_cpu(ddq->d_blk_softlimit); |
1da177e4 | 502 | if (hard == 0 || hard >= soft) { |
1149d96a CH |
503 | ddq->d_blk_hardlimit = cpu_to_be64(hard); |
504 | ddq->d_blk_softlimit = cpu_to_be64(soft); | |
1da177e4 | 505 | if (id == 0) { |
8a7b8a89 CH |
506 | q->qi_bhardlimit = hard; |
507 | q->qi_bsoftlimit = soft; | |
1da177e4 LT |
508 | } |
509 | } else { | |
510 | qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft); | |
511 | } | |
512 | hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ? | |
513 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) : | |
1149d96a | 514 | be64_to_cpu(ddq->d_rtb_hardlimit); |
1da177e4 LT |
515 | soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ? |
516 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) : | |
1149d96a | 517 | be64_to_cpu(ddq->d_rtb_softlimit); |
1da177e4 | 518 | if (hard == 0 || hard >= soft) { |
1149d96a CH |
519 | ddq->d_rtb_hardlimit = cpu_to_be64(hard); |
520 | ddq->d_rtb_softlimit = cpu_to_be64(soft); | |
1da177e4 | 521 | if (id == 0) { |
8a7b8a89 CH |
522 | q->qi_rtbhardlimit = hard; |
523 | q->qi_rtbsoftlimit = soft; | |
1da177e4 LT |
524 | } |
525 | } else { | |
526 | qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft); | |
527 | } | |
528 | ||
529 | hard = (newlim->d_fieldmask & FS_DQ_IHARD) ? | |
530 | (xfs_qcnt_t) newlim->d_ino_hardlimit : | |
1149d96a | 531 | be64_to_cpu(ddq->d_ino_hardlimit); |
1da177e4 LT |
532 | soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ? |
533 | (xfs_qcnt_t) newlim->d_ino_softlimit : | |
1149d96a | 534 | be64_to_cpu(ddq->d_ino_softlimit); |
1da177e4 | 535 | if (hard == 0 || hard >= soft) { |
1149d96a CH |
536 | ddq->d_ino_hardlimit = cpu_to_be64(hard); |
537 | ddq->d_ino_softlimit = cpu_to_be64(soft); | |
1da177e4 | 538 | if (id == 0) { |
8a7b8a89 CH |
539 | q->qi_ihardlimit = hard; |
540 | q->qi_isoftlimit = soft; | |
1da177e4 LT |
541 | } |
542 | } else { | |
543 | qdprintk("ihard %Ld < isoft %Ld\n", hard, soft); | |
544 | } | |
545 | ||
754002b4 NS |
546 | /* |
547 | * Update warnings counter(s) if requested | |
548 | */ | |
549 | if (newlim->d_fieldmask & FS_DQ_BWARNS) | |
1149d96a | 550 | ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns); |
754002b4 | 551 | if (newlim->d_fieldmask & FS_DQ_IWARNS) |
1149d96a | 552 | ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns); |
754002b4 | 553 | if (newlim->d_fieldmask & FS_DQ_RTBWARNS) |
1149d96a | 554 | ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns); |
754002b4 | 555 | |
1da177e4 LT |
556 | if (id == 0) { |
557 | /* | |
558 | * Timelimits for the super user set the relative time | |
559 | * the other users can be over quota for this file system. | |
560 | * If it is zero a default is used. Ditto for the default | |
754002b4 NS |
561 | * soft and hard limit values (already done, above), and |
562 | * for warnings. | |
1da177e4 LT |
563 | */ |
564 | if (newlim->d_fieldmask & FS_DQ_BTIMER) { | |
8a7b8a89 | 565 | q->qi_btimelimit = newlim->d_btimer; |
1149d96a | 566 | ddq->d_btimer = cpu_to_be32(newlim->d_btimer); |
1da177e4 LT |
567 | } |
568 | if (newlim->d_fieldmask & FS_DQ_ITIMER) { | |
8a7b8a89 | 569 | q->qi_itimelimit = newlim->d_itimer; |
1149d96a | 570 | ddq->d_itimer = cpu_to_be32(newlim->d_itimer); |
1da177e4 LT |
571 | } |
572 | if (newlim->d_fieldmask & FS_DQ_RTBTIMER) { | |
8a7b8a89 | 573 | q->qi_rtbtimelimit = newlim->d_rtbtimer; |
1149d96a | 574 | ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer); |
1da177e4 | 575 | } |
754002b4 | 576 | if (newlim->d_fieldmask & FS_DQ_BWARNS) |
8a7b8a89 | 577 | q->qi_bwarnlimit = newlim->d_bwarns; |
754002b4 | 578 | if (newlim->d_fieldmask & FS_DQ_IWARNS) |
8a7b8a89 | 579 | q->qi_iwarnlimit = newlim->d_iwarns; |
754002b4 | 580 | if (newlim->d_fieldmask & FS_DQ_RTBWARNS) |
8a7b8a89 | 581 | q->qi_rtbwarnlimit = newlim->d_rtbwarns; |
754002b4 | 582 | } else { |
1da177e4 LT |
583 | /* |
584 | * If the user is now over quota, start the timelimit. | |
585 | * The user will not be 'warned'. | |
586 | * Note that we keep the timers ticking, whether enforcement | |
587 | * is on or off. We don't really want to bother with iterating | |
588 | * over all ondisk dquots and turning the timers on/off. | |
589 | */ | |
590 | xfs_qm_adjust_dqtimers(mp, ddq); | |
591 | } | |
592 | dqp->dq_flags |= XFS_DQ_DIRTY; | |
593 | xfs_trans_log_dquot(tp, dqp); | |
594 | ||
e5720eec | 595 | error = xfs_trans_commit(tp, 0); |
1da177e4 LT |
596 | xfs_qm_dqprint(dqp); |
597 | xfs_qm_dqrele(dqp); | |
1da177e4 | 598 | |
8a7b8a89 CH |
599 | out_unlock: |
600 | mutex_unlock(&q->qi_quotaofflock); | |
e5720eec | 601 | return error; |
1da177e4 LT |
602 | } |
603 | ||
fcafb71b | 604 | int |
1da177e4 LT |
605 | xfs_qm_scall_getquota( |
606 | xfs_mount_t *mp, | |
607 | xfs_dqid_t id, | |
608 | uint type, | |
609 | fs_disk_quota_t *out) | |
610 | { | |
611 | xfs_dquot_t *dqp; | |
612 | int error; | |
613 | ||
614 | /* | |
615 | * Try to get the dquot. We don't want it allocated on disk, so | |
616 | * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't | |
617 | * exist, we'll get ENOENT back. | |
618 | */ | |
619 | if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) { | |
620 | return (error); | |
621 | } | |
622 | ||
1da177e4 LT |
623 | /* |
624 | * If everything's NULL, this dquot doesn't quite exist as far as | |
625 | * our utility programs are concerned. | |
626 | */ | |
627 | if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) { | |
628 | xfs_qm_dqput(dqp); | |
629 | return XFS_ERROR(ENOENT); | |
630 | } | |
631 | /* xfs_qm_dqprint(dqp); */ | |
632 | /* | |
633 | * Convert the disk dquot to the exportable format | |
634 | */ | |
635 | xfs_qm_export_dquot(mp, &dqp->q_core, out); | |
636 | xfs_qm_dqput(dqp); | |
637 | return (error ? XFS_ERROR(EFAULT) : 0); | |
638 | } | |
639 | ||
640 | ||
641 | STATIC int | |
642 | xfs_qm_log_quotaoff_end( | |
643 | xfs_mount_t *mp, | |
644 | xfs_qoff_logitem_t *startqoff, | |
645 | uint flags) | |
646 | { | |
c8ad20ff | 647 | xfs_trans_t *tp; |
1da177e4 | 648 | int error; |
c8ad20ff | 649 | xfs_qoff_logitem_t *qoffi; |
1da177e4 LT |
650 | |
651 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END); | |
652 | ||
653 | if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2, | |
654 | 0, 0, XFS_DEFAULT_LOG_COUNT))) { | |
655 | xfs_trans_cancel(tp, 0); | |
656 | return (error); | |
657 | } | |
658 | ||
659 | qoffi = xfs_trans_get_qoff_item(tp, startqoff, | |
660 | flags & XFS_ALL_QUOTA_ACCT); | |
661 | xfs_trans_log_quotaoff_item(tp, qoffi); | |
662 | ||
663 | /* | |
664 | * We have to make sure that the transaction is secure on disk before we | |
665 | * return and actually stop quota accounting. So, make it synchronous. | |
666 | * We don't care about quotoff's performance. | |
667 | */ | |
668 | xfs_trans_set_sync(tp); | |
1c72bf90 | 669 | error = xfs_trans_commit(tp, 0); |
1da177e4 LT |
670 | return (error); |
671 | } | |
672 | ||
673 | ||
674 | STATIC int | |
675 | xfs_qm_log_quotaoff( | |
676 | xfs_mount_t *mp, | |
677 | xfs_qoff_logitem_t **qoffstartp, | |
678 | uint flags) | |
679 | { | |
680 | xfs_trans_t *tp; | |
681 | int error; | |
1da177e4 LT |
682 | xfs_qoff_logitem_t *qoffi=NULL; |
683 | uint oldsbqflag=0; | |
684 | ||
685 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF); | |
686 | if ((error = xfs_trans_reserve(tp, 0, | |
687 | sizeof(xfs_qoff_logitem_t) * 2 + | |
688 | mp->m_sb.sb_sectsize + 128, | |
689 | 0, | |
690 | 0, | |
691 | XFS_DEFAULT_LOG_COUNT))) { | |
692 | goto error0; | |
693 | } | |
694 | ||
695 | qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT); | |
696 | xfs_trans_log_quotaoff_item(tp, qoffi); | |
697 | ||
3685c2a1 | 698 | spin_lock(&mp->m_sb_lock); |
1da177e4 LT |
699 | oldsbqflag = mp->m_sb.sb_qflags; |
700 | mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL; | |
3685c2a1 | 701 | spin_unlock(&mp->m_sb_lock); |
1da177e4 LT |
702 | |
703 | xfs_mod_sb(tp, XFS_SB_QFLAGS); | |
704 | ||
705 | /* | |
706 | * We have to make sure that the transaction is secure on disk before we | |
707 | * return and actually stop quota accounting. So, make it synchronous. | |
708 | * We don't care about quotoff's performance. | |
709 | */ | |
710 | xfs_trans_set_sync(tp); | |
1c72bf90 | 711 | error = xfs_trans_commit(tp, 0); |
1da177e4 LT |
712 | |
713 | error0: | |
714 | if (error) { | |
715 | xfs_trans_cancel(tp, 0); | |
716 | /* | |
717 | * No one else is modifying sb_qflags, so this is OK. | |
718 | * We still hold the quotaofflock. | |
719 | */ | |
3685c2a1 | 720 | spin_lock(&mp->m_sb_lock); |
1da177e4 | 721 | mp->m_sb.sb_qflags = oldsbqflag; |
3685c2a1 | 722 | spin_unlock(&mp->m_sb_lock); |
1da177e4 LT |
723 | } |
724 | *qoffstartp = qoffi; | |
725 | return (error); | |
726 | } | |
727 | ||
728 | ||
729 | /* | |
730 | * Translate an internal style on-disk-dquot to the exportable format. | |
731 | * The main differences are that the counters/limits are all in Basic | |
732 | * Blocks (BBs) instead of the internal FSBs, and all on-disk data has | |
733 | * to be converted to the native endianness. | |
734 | */ | |
735 | STATIC void | |
736 | xfs_qm_export_dquot( | |
737 | xfs_mount_t *mp, | |
738 | xfs_disk_dquot_t *src, | |
739 | struct fs_disk_quota *dst) | |
740 | { | |
741 | memset(dst, 0, sizeof(*dst)); | |
742 | dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */ | |
1149d96a CH |
743 | dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags); |
744 | dst->d_id = be32_to_cpu(src->d_id); | |
745 | dst->d_blk_hardlimit = | |
746 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit)); | |
747 | dst->d_blk_softlimit = | |
748 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit)); | |
749 | dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit); | |
750 | dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit); | |
751 | dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount)); | |
752 | dst->d_icount = be64_to_cpu(src->d_icount); | |
753 | dst->d_btimer = be32_to_cpu(src->d_btimer); | |
754 | dst->d_itimer = be32_to_cpu(src->d_itimer); | |
755 | dst->d_iwarns = be16_to_cpu(src->d_iwarns); | |
756 | dst->d_bwarns = be16_to_cpu(src->d_bwarns); | |
757 | dst->d_rtb_hardlimit = | |
758 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit)); | |
759 | dst->d_rtb_softlimit = | |
760 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit)); | |
761 | dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount)); | |
762 | dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer); | |
763 | dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns); | |
1da177e4 LT |
764 | |
765 | /* | |
766 | * Internally, we don't reset all the timers when quota enforcement | |
c41564b5 | 767 | * gets turned off. No need to confuse the user level code, |
1da177e4 LT |
768 | * so return zeroes in that case. |
769 | */ | |
e6d29426 KO |
770 | if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) || |
771 | (!XFS_IS_OQUOTA_ENFORCED(mp) && | |
772 | (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) { | |
1da177e4 LT |
773 | dst->d_btimer = 0; |
774 | dst->d_itimer = 0; | |
775 | dst->d_rtbtimer = 0; | |
776 | } | |
777 | ||
778 | #ifdef DEBUG | |
e6d29426 KO |
779 | if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) || |
780 | (XFS_IS_OQUOTA_ENFORCED(mp) && | |
781 | (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) && | |
782 | dst->d_id != 0) { | |
1da177e4 LT |
783 | if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) && |
784 | (dst->d_blk_softlimit > 0)) { | |
785 | ASSERT(dst->d_btimer != 0); | |
786 | } | |
787 | if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) && | |
788 | (dst->d_ino_softlimit > 0)) { | |
789 | ASSERT(dst->d_itimer != 0); | |
790 | } | |
791 | } | |
792 | #endif | |
793 | } | |
794 | ||
1da177e4 LT |
795 | STATIC uint |
796 | xfs_qm_export_qtype_flags( | |
797 | uint flags) | |
798 | { | |
799 | /* | |
c8ad20ff | 800 | * Can't be more than one, or none. |
1da177e4 | 801 | */ |
c8ad20ff NS |
802 | ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) != |
803 | (XFS_PROJ_QUOTA | XFS_USER_QUOTA)); | |
804 | ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) != | |
805 | (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)); | |
806 | ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) != | |
807 | (XFS_USER_QUOTA | XFS_GROUP_QUOTA)); | |
808 | ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0); | |
1da177e4 LT |
809 | |
810 | return (flags & XFS_DQ_USER) ? | |
c8ad20ff NS |
811 | XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ? |
812 | XFS_PROJ_QUOTA : XFS_GROUP_QUOTA; | |
1da177e4 LT |
813 | } |
814 | ||
1da177e4 LT |
815 | STATIC uint |
816 | xfs_qm_export_flags( | |
817 | uint flags) | |
818 | { | |
819 | uint uflags; | |
820 | ||
821 | uflags = 0; | |
822 | if (flags & XFS_UQUOTA_ACCT) | |
823 | uflags |= XFS_QUOTA_UDQ_ACCT; | |
c8ad20ff NS |
824 | if (flags & XFS_PQUOTA_ACCT) |
825 | uflags |= XFS_QUOTA_PDQ_ACCT; | |
1da177e4 LT |
826 | if (flags & XFS_GQUOTA_ACCT) |
827 | uflags |= XFS_QUOTA_GDQ_ACCT; | |
828 | if (flags & XFS_UQUOTA_ENFD) | |
829 | uflags |= XFS_QUOTA_UDQ_ENFD; | |
c8ad20ff NS |
830 | if (flags & (XFS_OQUOTA_ENFD)) { |
831 | uflags |= (flags & XFS_GQUOTA_ACCT) ? | |
832 | XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD; | |
833 | } | |
1da177e4 LT |
834 | return (uflags); |
835 | } | |
836 | ||
837 | ||
fe588ed3 CH |
838 | STATIC int |
839 | xfs_dqrele_inode( | |
840 | struct xfs_inode *ip, | |
841 | struct xfs_perag *pag, | |
842 | int flags) | |
1da177e4 | 843 | { |
fe588ed3 | 844 | int error; |
cb4f0d1d | 845 | |
fe588ed3 | 846 | /* skip quota inodes */ |
8a7b8a89 CH |
847 | if (ip == ip->i_mount->m_quotainfo->qi_uquotaip || |
848 | ip == ip->i_mount->m_quotainfo->qi_gquotaip) { | |
fe588ed3 CH |
849 | ASSERT(ip->i_udquot == NULL); |
850 | ASSERT(ip->i_gdquot == NULL); | |
cb4f0d1d | 851 | read_unlock(&pag->pag_ici_lock); |
fe588ed3 CH |
852 | return 0; |
853 | } | |
cb4f0d1d | 854 | |
fe588ed3 CH |
855 | error = xfs_sync_inode_valid(ip, pag); |
856 | if (error) | |
857 | return error; | |
6307091f | 858 | |
fe588ed3 CH |
859 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
860 | if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) { | |
861 | xfs_qm_dqrele(ip->i_udquot); | |
862 | ip->i_udquot = NULL; | |
863 | } | |
864 | if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) { | |
865 | xfs_qm_dqrele(ip->i_gdquot); | |
866 | ip->i_gdquot = NULL; | |
867 | } | |
f2d67614 | 868 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
cb4f0d1d | 869 | |
f2d67614 | 870 | IRELE(ip); |
fe588ed3 | 871 | return 0; |
5b4d89ae DC |
872 | } |
873 | ||
fe588ed3 | 874 | |
5b4d89ae DC |
875 | /* |
876 | * Go thru all the inodes in the file system, releasing their dquots. | |
fe588ed3 | 877 | * |
5b4d89ae | 878 | * Note that the mount structure gets modified to indicate that quotas are off |
fe588ed3 | 879 | * AFTER this, in the case of quotaoff. |
5b4d89ae DC |
880 | */ |
881 | void | |
882 | xfs_qm_dqrele_all_inodes( | |
883 | struct xfs_mount *mp, | |
884 | uint flags) | |
885 | { | |
5b4d89ae | 886 | ASSERT(mp->m_quotainfo); |
9bf729c0 DC |
887 | xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, |
888 | XFS_ICI_NO_TAG, 0, NULL); | |
1da177e4 LT |
889 | } |
890 | ||
891 | /*------------------------------------------------------------------------*/ | |
892 | #ifdef DEBUG | |
893 | /* | |
894 | * This contains all the test functions for XFS disk quotas. | |
895 | * Currently it does a quota accounting check. ie. it walks through | |
896 | * all inodes in the file system, calculating the dquot accounting fields, | |
897 | * and prints out any inconsistencies. | |
898 | */ | |
899 | xfs_dqhash_t *qmtest_udqtab; | |
900 | xfs_dqhash_t *qmtest_gdqtab; | |
901 | int qmtest_hashmask; | |
902 | int qmtest_nfails; | |
a0b0b8a5 | 903 | struct mutex qcheck_lock; |
1da177e4 LT |
904 | |
905 | #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \ | |
906 | (__psunsigned_t)(id)) & \ | |
907 | (qmtest_hashmask - 1)) | |
908 | ||
909 | #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \ | |
910 | (qmtest_udqtab + \ | |
911 | DQTEST_HASHVAL(mp, id)) : \ | |
912 | (qmtest_gdqtab + \ | |
913 | DQTEST_HASHVAL(mp, id))) | |
914 | ||
915 | #define DQTEST_LIST_PRINT(l, NXT, title) \ | |
916 | { \ | |
917 | xfs_dqtest_t *dqp; int i = 0;\ | |
918 | cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \ | |
919 | for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \ | |
920 | dqp = (xfs_dqtest_t *)dqp->NXT) { \ | |
921 | cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \ | |
922 | ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \ | |
923 | dqp->d_bcount, dqp->d_icount); } \ | |
924 | } | |
925 | ||
926 | typedef struct dqtest { | |
74457cf4 | 927 | uint dq_flags; /* various flags (XFS_DQ_*) */ |
e6a81f13 | 928 | struct list_head q_hashlist; |
1da177e4 LT |
929 | xfs_dqhash_t *q_hash; /* the hashchain header */ |
930 | xfs_mount_t *q_mount; /* filesystem this relates to */ | |
931 | xfs_dqid_t d_id; /* user id or group id */ | |
932 | xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */ | |
933 | xfs_qcnt_t d_icount; /* # inodes owned by the user */ | |
934 | } xfs_dqtest_t; | |
935 | ||
936 | STATIC void | |
937 | xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp) | |
938 | { | |
e6a81f13 DC |
939 | list_add(&dqp->q_hashlist, &h->qh_list); |
940 | h->qh_version++; | |
941 | h->qh_nelems++; | |
1da177e4 LT |
942 | } |
943 | STATIC void | |
944 | xfs_qm_dqtest_print( | |
945 | xfs_dqtest_t *d) | |
946 | { | |
947 | cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------"); | |
948 | cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id); | |
1da177e4 LT |
949 | cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount); |
950 | cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)", | |
951 | d->d_bcount, (int)d->d_bcount); | |
952 | cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)", | |
953 | d->d_icount, (int)d->d_icount); | |
954 | cmn_err(CE_DEBUG, "---------------------------"); | |
955 | } | |
956 | ||
957 | STATIC void | |
958 | xfs_qm_dqtest_failed( | |
959 | xfs_dqtest_t *d, | |
960 | xfs_dquot_t *dqp, | |
961 | char *reason, | |
962 | xfs_qcnt_t a, | |
963 | xfs_qcnt_t b, | |
964 | int error) | |
965 | { | |
966 | qmtest_nfails++; | |
967 | if (error) | |
968 | cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s", | |
1149d96a | 969 | d->d_id, error, reason); |
1da177e4 LT |
970 | else |
971 | cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]", | |
1149d96a | 972 | d->d_id, reason, (int)a, (int)b); |
1da177e4 LT |
973 | xfs_qm_dqtest_print(d); |
974 | if (dqp) | |
975 | xfs_qm_dqprint(dqp); | |
976 | } | |
977 | ||
978 | STATIC int | |
979 | xfs_dqtest_cmp2( | |
980 | xfs_dqtest_t *d, | |
981 | xfs_dquot_t *dqp) | |
982 | { | |
983 | int err = 0; | |
1149d96a | 984 | if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) { |
1da177e4 | 985 | xfs_qm_dqtest_failed(d, dqp, "icount mismatch", |
1149d96a | 986 | be64_to_cpu(dqp->q_core.d_icount), |
1da177e4 LT |
987 | d->d_icount, 0); |
988 | err++; | |
989 | } | |
1149d96a | 990 | if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) { |
1da177e4 | 991 | xfs_qm_dqtest_failed(d, dqp, "bcount mismatch", |
1149d96a | 992 | be64_to_cpu(dqp->q_core.d_bcount), |
1da177e4 LT |
993 | d->d_bcount, 0); |
994 | err++; | |
995 | } | |
1149d96a CH |
996 | if (dqp->q_core.d_blk_softlimit && |
997 | be64_to_cpu(dqp->q_core.d_bcount) >= | |
998 | be64_to_cpu(dqp->q_core.d_blk_softlimit)) { | |
1da177e4 LT |
999 | if (!dqp->q_core.d_btimer && dqp->q_core.d_id) { |
1000 | cmn_err(CE_DEBUG, | |
1001 | "%d [%s] [0x%p] BLK TIMER NOT STARTED", | |
1002 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); | |
1003 | err++; | |
1004 | } | |
1005 | } | |
1149d96a CH |
1006 | if (dqp->q_core.d_ino_softlimit && |
1007 | be64_to_cpu(dqp->q_core.d_icount) >= | |
1008 | be64_to_cpu(dqp->q_core.d_ino_softlimit)) { | |
1da177e4 LT |
1009 | if (!dqp->q_core.d_itimer && dqp->q_core.d_id) { |
1010 | cmn_err(CE_DEBUG, | |
1011 | "%d [%s] [0x%p] INO TIMER NOT STARTED", | |
1012 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); | |
1013 | err++; | |
1014 | } | |
1015 | } | |
1016 | #ifdef QUOTADEBUG | |
1017 | if (!err) { | |
1018 | cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked", | |
c8ad20ff | 1019 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
1da177e4 LT |
1020 | } |
1021 | #endif | |
1022 | return (err); | |
1023 | } | |
1024 | ||
1025 | STATIC void | |
1026 | xfs_dqtest_cmp( | |
1027 | xfs_dqtest_t *d) | |
1028 | { | |
1029 | xfs_dquot_t *dqp; | |
1030 | int error; | |
1031 | ||
1032 | /* xfs_qm_dqtest_print(d); */ | |
1033 | if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0, | |
1034 | &dqp))) { | |
1035 | xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error); | |
1036 | return; | |
1037 | } | |
1038 | xfs_dqtest_cmp2(d, dqp); | |
1039 | xfs_qm_dqput(dqp); | |
1040 | } | |
1041 | ||
1042 | STATIC int | |
1043 | xfs_qm_internalqcheck_dqget( | |
1044 | xfs_mount_t *mp, | |
1045 | xfs_dqid_t id, | |
1046 | uint type, | |
1047 | xfs_dqtest_t **O_dq) | |
1048 | { | |
1049 | xfs_dqtest_t *d; | |
1050 | xfs_dqhash_t *h; | |
1051 | ||
1052 | h = DQTEST_HASH(mp, id, type); | |
e6a81f13 | 1053 | list_for_each_entry(d, &h->qh_list, q_hashlist) { |
1da177e4 LT |
1054 | if (d->d_id == id && mp == d->q_mount) { |
1055 | *O_dq = d; | |
1056 | return (0); | |
1057 | } | |
1058 | } | |
1059 | d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP); | |
1060 | d->dq_flags = type; | |
1061 | d->d_id = id; | |
1062 | d->q_mount = mp; | |
1063 | d->q_hash = h; | |
e6a81f13 | 1064 | INIT_LIST_HEAD(&d->q_hashlist); |
1da177e4 LT |
1065 | xfs_qm_hashinsert(h, d); |
1066 | *O_dq = d; | |
1067 | return (0); | |
1068 | } | |
1069 | ||
1070 | STATIC void | |
1071 | xfs_qm_internalqcheck_get_dquots( | |
1072 | xfs_mount_t *mp, | |
1073 | xfs_dqid_t uid, | |
c8ad20ff | 1074 | xfs_dqid_t projid, |
1da177e4 LT |
1075 | xfs_dqid_t gid, |
1076 | xfs_dqtest_t **ud, | |
1077 | xfs_dqtest_t **gd) | |
1078 | { | |
1079 | if (XFS_IS_UQUOTA_ON(mp)) | |
1080 | xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud); | |
1081 | if (XFS_IS_GQUOTA_ON(mp)) | |
1082 | xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd); | |
c8ad20ff NS |
1083 | else if (XFS_IS_PQUOTA_ON(mp)) |
1084 | xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd); | |
1da177e4 LT |
1085 | } |
1086 | ||
1087 | ||
1088 | STATIC void | |
1089 | xfs_qm_internalqcheck_dqadjust( | |
1090 | xfs_inode_t *ip, | |
1091 | xfs_dqtest_t *d) | |
1092 | { | |
1093 | d->d_icount++; | |
1094 | d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks; | |
1095 | } | |
1096 | ||
1097 | STATIC int | |
1098 | xfs_qm_internalqcheck_adjust( | |
1099 | xfs_mount_t *mp, /* mount point for filesystem */ | |
1100 | xfs_ino_t ino, /* inode number to get data for */ | |
1101 | void __user *buffer, /* not used */ | |
1102 | int ubsize, /* not used */ | |
1da177e4 | 1103 | int *ubused, /* not used */ |
1da177e4 LT |
1104 | int *res) /* bulkstat result code */ |
1105 | { | |
1106 | xfs_inode_t *ip; | |
1107 | xfs_dqtest_t *ud, *gd; | |
1108 | uint lock_flags; | |
1109 | boolean_t ipreleased; | |
1110 | int error; | |
1111 | ||
1112 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); | |
1113 | ||
1114 | if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) { | |
1115 | *res = BULKSTAT_RV_NOTHING; | |
1116 | qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n", | |
1117 | (unsigned long long) ino, | |
1118 | (unsigned long long) mp->m_sb.sb_uquotino, | |
1119 | (unsigned long long) mp->m_sb.sb_gquotino); | |
1120 | return XFS_ERROR(EINVAL); | |
1121 | } | |
1122 | ipreleased = B_FALSE; | |
1123 | again: | |
1124 | lock_flags = XFS_ILOCK_SHARED; | |
7b6259e7 | 1125 | if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip))) { |
1da177e4 LT |
1126 | *res = BULKSTAT_RV_NOTHING; |
1127 | return (error); | |
1128 | } | |
1129 | ||
1da177e4 LT |
1130 | /* |
1131 | * This inode can have blocks after eof which can get released | |
1132 | * when we send it to inactive. Since we don't check the dquot | |
1133 | * until the after all our calculations are done, we must get rid | |
1134 | * of those now. | |
1135 | */ | |
1136 | if (! ipreleased) { | |
f2d67614 CH |
1137 | xfs_iunlock(ip, lock_flags); |
1138 | IRELE(ip); | |
1da177e4 LT |
1139 | ipreleased = B_TRUE; |
1140 | goto again; | |
1141 | } | |
1142 | xfs_qm_internalqcheck_get_dquots(mp, | |
1143 | (xfs_dqid_t) ip->i_d.di_uid, | |
c8ad20ff | 1144 | (xfs_dqid_t) ip->i_d.di_projid, |
1da177e4 LT |
1145 | (xfs_dqid_t) ip->i_d.di_gid, |
1146 | &ud, &gd); | |
1147 | if (XFS_IS_UQUOTA_ON(mp)) { | |
1148 | ASSERT(ud); | |
1149 | xfs_qm_internalqcheck_dqadjust(ip, ud); | |
1150 | } | |
c8ad20ff | 1151 | if (XFS_IS_OQUOTA_ON(mp)) { |
1da177e4 LT |
1152 | ASSERT(gd); |
1153 | xfs_qm_internalqcheck_dqadjust(ip, gd); | |
1154 | } | |
f2d67614 CH |
1155 | xfs_iunlock(ip, lock_flags); |
1156 | IRELE(ip); | |
1da177e4 LT |
1157 | *res = BULKSTAT_RV_DIDONE; |
1158 | return (0); | |
1159 | } | |
1160 | ||
1161 | ||
1162 | /* PRIVATE, debugging */ | |
1163 | int | |
1164 | xfs_qm_internalqcheck( | |
1165 | xfs_mount_t *mp) | |
1166 | { | |
1167 | xfs_ino_t lastino; | |
1168 | int done, count; | |
1169 | int i; | |
1da177e4 LT |
1170 | int error; |
1171 | ||
1172 | lastino = 0; | |
1173 | qmtest_hashmask = 32; | |
1174 | count = 5; | |
1175 | done = 0; | |
1176 | qmtest_nfails = 0; | |
1177 | ||
1178 | if (! XFS_IS_QUOTA_ON(mp)) | |
1179 | return XFS_ERROR(ESRCH); | |
1180 | ||
a14a348b | 1181 | xfs_log_force(mp, XFS_LOG_SYNC); |
1da177e4 | 1182 | XFS_bflush(mp->m_ddev_targp); |
a14a348b | 1183 | xfs_log_force(mp, XFS_LOG_SYNC); |
1da177e4 LT |
1184 | XFS_bflush(mp->m_ddev_targp); |
1185 | ||
794ee1ba | 1186 | mutex_lock(&qcheck_lock); |
1da177e4 LT |
1187 | /* There should be absolutely no quota activity while this |
1188 | is going on. */ | |
1189 | qmtest_udqtab = kmem_zalloc(qmtest_hashmask * | |
1190 | sizeof(xfs_dqhash_t), KM_SLEEP); | |
1191 | qmtest_gdqtab = kmem_zalloc(qmtest_hashmask * | |
1192 | sizeof(xfs_dqhash_t), KM_SLEEP); | |
1193 | do { | |
1194 | /* | |
1195 | * Iterate thru all the inodes in the file system, | |
1196 | * adjusting the corresponding dquot counters | |
1197 | */ | |
7dce11db CH |
1198 | error = xfs_bulkstat(mp, &lastino, &count, |
1199 | xfs_qm_internalqcheck_adjust, | |
1200 | 0, NULL, &done); | |
1201 | if (error) { | |
1202 | cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error); | |
1da177e4 LT |
1203 | break; |
1204 | } | |
7dce11db CH |
1205 | } while (!done); |
1206 | ||
1da177e4 LT |
1207 | cmn_err(CE_DEBUG, "Checking results against system dquots"); |
1208 | for (i = 0; i < qmtest_hashmask; i++) { | |
e6a81f13 DC |
1209 | xfs_dqtest_t *d, *n; |
1210 | xfs_dqhash_t *h; | |
1211 | ||
1212 | h = &qmtest_udqtab[i]; | |
1213 | list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) { | |
1da177e4 | 1214 | xfs_dqtest_cmp(d); |
f0e2d93c | 1215 | kmem_free(d); |
1da177e4 | 1216 | } |
e6a81f13 DC |
1217 | h = &qmtest_gdqtab[i]; |
1218 | list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) { | |
1da177e4 | 1219 | xfs_dqtest_cmp(d); |
f0e2d93c | 1220 | kmem_free(d); |
1da177e4 LT |
1221 | } |
1222 | } | |
1223 | ||
1224 | if (qmtest_nfails) { | |
1225 | cmn_err(CE_DEBUG, "******** quotacheck failed ********"); | |
1226 | cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails); | |
1227 | } else { | |
1228 | cmn_err(CE_DEBUG, "******** quotacheck successful! ********"); | |
1229 | } | |
f0e2d93c DV |
1230 | kmem_free(qmtest_udqtab); |
1231 | kmem_free(qmtest_gdqtab); | |
1da177e4 LT |
1232 | mutex_unlock(&qcheck_lock); |
1233 | return (qmtest_nfails); | |
1234 | } | |
1235 | ||
1236 | #endif /* DEBUG */ |