]>
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 | */ |
1da177e4 LT |
18 | #include "xfs.h" |
19 | #include "xfs_fs.h" | |
a844f451 | 20 | #include "xfs_bit.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_ag.h" | |
1da177e4 LT |
26 | #include "xfs_dir2.h" |
27 | #include "xfs_alloc.h" | |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_quota.h" | |
30 | #include "xfs_mount.h" | |
1da177e4 | 31 | #include "xfs_bmap_btree.h" |
a844f451 | 32 | #include "xfs_alloc_btree.h" |
1da177e4 | 33 | #include "xfs_ialloc_btree.h" |
1da177e4 | 34 | #include "xfs_dir2_sf.h" |
a844f451 | 35 | #include "xfs_attr_sf.h" |
1da177e4 LT |
36 | #include "xfs_dinode.h" |
37 | #include "xfs_inode.h" | |
a844f451 NS |
38 | #include "xfs_btree.h" |
39 | #include "xfs_ialloc.h" | |
40 | #include "xfs_itable.h" | |
1da177e4 LT |
41 | #include "xfs_rtalloc.h" |
42 | #include "xfs_error.h" | |
a844f451 | 43 | #include "xfs_bmap.h" |
1da177e4 LT |
44 | #include "xfs_rw.h" |
45 | #include "xfs_acl.h" | |
1da177e4 LT |
46 | #include "xfs_attr.h" |
47 | #include "xfs_buf_item.h" | |
48 | #include "xfs_trans_space.h" | |
49 | #include "xfs_utils.h" | |
1da177e4 LT |
50 | #include "xfs_qm.h" |
51 | ||
52 | /* | |
53 | * The global quota manager. There is only one of these for the entire | |
54 | * system, _not_ one per file system. XQM keeps track of the overall | |
55 | * quota functionality, including maintaining the freelist and hash | |
56 | * tables of dquots. | |
57 | */ | |
6b3f6b5b | 58 | mutex_t xfs_Gqm_lock; |
1da177e4 | 59 | struct xfs_qm *xfs_Gqm; |
6b3f6b5b | 60 | uint ndquot; |
1da177e4 LT |
61 | |
62 | kmem_zone_t *qm_dqzone; | |
63 | kmem_zone_t *qm_dqtrxzone; | |
1da177e4 | 64 | |
7989cb8e | 65 | static cred_t xfs_zerocr; |
dae81d47 | 66 | |
1da177e4 LT |
67 | STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int); |
68 | STATIC void xfs_qm_list_destroy(xfs_dqlist_t *); | |
69 | ||
ba0f32d4 CH |
70 | STATIC void xfs_qm_freelist_init(xfs_frlist_t *); |
71 | STATIC void xfs_qm_freelist_destroy(xfs_frlist_t *); | |
72 | STATIC int xfs_qm_mplist_nowait(xfs_mount_t *); | |
73 | STATIC int xfs_qm_dqhashlock_nowait(xfs_dquot_t *); | |
74 | ||
1da177e4 | 75 | STATIC int xfs_qm_init_quotainos(xfs_mount_t *); |
ba0f32d4 | 76 | STATIC int xfs_qm_init_quotainfo(xfs_mount_t *); |
51bfb75b | 77 | STATIC int xfs_qm_shake(int, gfp_t); |
1da177e4 | 78 | |
8e1f936b RR |
79 | static struct shrinker xfs_qm_shaker = { |
80 | .shrink = xfs_qm_shake, | |
81 | .seeks = DEFAULT_SEEKS, | |
82 | }; | |
83 | ||
1da177e4 LT |
84 | #ifdef DEBUG |
85 | extern mutex_t qcheck_lock; | |
86 | #endif | |
87 | ||
88 | #ifdef QUOTADEBUG | |
89 | #define XQM_LIST_PRINT(l, NXT, title) \ | |
90 | { \ | |
91 | xfs_dquot_t *dqp; int i = 0; \ | |
92 | cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \ | |
93 | for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \ | |
94 | cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \ | |
95 | "bcnt = %d, icnt = %d, refs = %d", \ | |
1149d96a | 96 | ++i, (int) be32_to_cpu(dqp->q_core.d_id), \ |
1da177e4 | 97 | DQFLAGTO_TYPESTR(dqp), \ |
1149d96a CH |
98 | (int) be64_to_cpu(dqp->q_core.d_bcount), \ |
99 | (int) be64_to_cpu(dqp->q_core.d_icount), \ | |
1da177e4 LT |
100 | (int) dqp->q_nrefs); } \ |
101 | } | |
102 | #else | |
103 | #define XQM_LIST_PRINT(l, NXT, title) do { } while (0) | |
104 | #endif | |
105 | ||
106 | /* | |
107 | * Initialize the XQM structure. | |
108 | * Note that there is not one quota manager per file system. | |
109 | */ | |
110 | STATIC struct xfs_qm * | |
111 | xfs_Gqm_init(void) | |
112 | { | |
6b3f6b5b NS |
113 | xfs_dqhash_t *udqhash, *gdqhash; |
114 | xfs_qm_t *xqm; | |
215101c3 NS |
115 | size_t hsize; |
116 | uint i; | |
1da177e4 LT |
117 | |
118 | /* | |
119 | * Initialize the dquot hash tables. | |
120 | */ | |
77e4635a | 121 | udqhash = kmem_zalloc_greedy(&hsize, |
5995cb7d ES |
122 | XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t), |
123 | XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t), | |
77e4635a NS |
124 | KM_SLEEP | KM_MAYFAIL | KM_LARGE); |
125 | gdqhash = kmem_zalloc(hsize, KM_SLEEP | KM_LARGE); | |
126 | hsize /= sizeof(xfs_dqhash_t); | |
6b3f6b5b | 127 | ndquot = hsize << 8; |
1da177e4 | 128 | |
6b3f6b5b NS |
129 | xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP); |
130 | xqm->qm_dqhashmask = hsize - 1; | |
131 | xqm->qm_usr_dqhtable = udqhash; | |
132 | xqm->qm_grp_dqhtable = gdqhash; | |
1da177e4 LT |
133 | ASSERT(xqm->qm_usr_dqhtable != NULL); |
134 | ASSERT(xqm->qm_grp_dqhtable != NULL); | |
135 | ||
136 | for (i = 0; i < hsize; i++) { | |
137 | xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i); | |
138 | xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i); | |
139 | } | |
140 | ||
141 | /* | |
142 | * Freelist of all dquots of all file systems | |
143 | */ | |
144 | xfs_qm_freelist_init(&(xqm->qm_dqfreelist)); | |
145 | ||
146 | /* | |
147 | * dquot zone. we register our own low-memory callback. | |
148 | */ | |
149 | if (!qm_dqzone) { | |
150 | xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t), | |
151 | "xfs_dquots"); | |
152 | qm_dqzone = xqm->qm_dqzone; | |
153 | } else | |
154 | xqm->qm_dqzone = qm_dqzone; | |
155 | ||
8e1f936b | 156 | register_shrinker(&xfs_qm_shaker); |
1da177e4 LT |
157 | |
158 | /* | |
159 | * The t_dqinfo portion of transactions. | |
160 | */ | |
161 | if (!qm_dqtrxzone) { | |
162 | xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t), | |
163 | "xfs_dqtrx"); | |
164 | qm_dqtrxzone = xqm->qm_dqtrxzone; | |
165 | } else | |
166 | xqm->qm_dqtrxzone = qm_dqtrxzone; | |
167 | ||
168 | atomic_set(&xqm->qm_totaldquots, 0); | |
169 | xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO; | |
170 | xqm->qm_nrefs = 0; | |
171 | #ifdef DEBUG | |
c2e81432 | 172 | mutex_init(&qcheck_lock); |
1da177e4 LT |
173 | #endif |
174 | return xqm; | |
175 | } | |
176 | ||
177 | /* | |
178 | * Destroy the global quota manager when its reference count goes to zero. | |
179 | */ | |
ba0f32d4 | 180 | STATIC void |
1da177e4 LT |
181 | xfs_qm_destroy( |
182 | struct xfs_qm *xqm) | |
183 | { | |
184 | int hsize, i; | |
185 | ||
186 | ASSERT(xqm != NULL); | |
187 | ASSERT(xqm->qm_nrefs == 0); | |
8e1f936b | 188 | unregister_shrinker(&xfs_qm_shaker); |
1da177e4 LT |
189 | hsize = xqm->qm_dqhashmask + 1; |
190 | for (i = 0; i < hsize; i++) { | |
191 | xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i])); | |
192 | xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i])); | |
193 | } | |
f0e2d93c DV |
194 | kmem_free(xqm->qm_usr_dqhtable); |
195 | kmem_free(xqm->qm_grp_dqhtable); | |
1da177e4 LT |
196 | xqm->qm_usr_dqhtable = NULL; |
197 | xqm->qm_grp_dqhtable = NULL; | |
198 | xqm->qm_dqhashmask = 0; | |
199 | xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist)); | |
200 | #ifdef DEBUG | |
201 | mutex_destroy(&qcheck_lock); | |
202 | #endif | |
f0e2d93c | 203 | kmem_free(xqm); |
1da177e4 LT |
204 | } |
205 | ||
206 | /* | |
207 | * Called at mount time to let XQM know that another file system is | |
208 | * starting quotas. This isn't crucial information as the individual mount | |
209 | * structures are pretty independent, but it helps the XQM keep a | |
210 | * global view of what's going on. | |
211 | */ | |
212 | /* ARGSUSED */ | |
213 | STATIC int | |
214 | xfs_qm_hold_quotafs_ref( | |
215 | struct xfs_mount *mp) | |
216 | { | |
217 | /* | |
218 | * Need to lock the xfs_Gqm structure for things like this. For example, | |
219 | * the structure could disappear between the entry to this routine and | |
220 | * a HOLD operation if not locked. | |
221 | */ | |
222 | XFS_QM_LOCK(xfs_Gqm); | |
223 | ||
224 | if (xfs_Gqm == NULL) | |
225 | xfs_Gqm = xfs_Gqm_init(); | |
226 | /* | |
227 | * We can keep a list of all filesystems with quotas mounted for | |
228 | * debugging and statistical purposes, but ... | |
229 | * Just take a reference and get out. | |
230 | */ | |
231 | XFS_QM_HOLD(xfs_Gqm); | |
232 | XFS_QM_UNLOCK(xfs_Gqm); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | ||
238 | /* | |
239 | * Release the reference that a filesystem took at mount time, | |
240 | * so that we know when we need to destroy the entire quota manager. | |
241 | */ | |
242 | /* ARGSUSED */ | |
243 | STATIC void | |
244 | xfs_qm_rele_quotafs_ref( | |
245 | struct xfs_mount *mp) | |
246 | { | |
247 | xfs_dquot_t *dqp, *nextdqp; | |
248 | ||
249 | ASSERT(xfs_Gqm); | |
250 | ASSERT(xfs_Gqm->qm_nrefs > 0); | |
251 | ||
252 | /* | |
253 | * Go thru the freelist and destroy all inactive dquots. | |
254 | */ | |
255 | xfs_qm_freelist_lock(xfs_Gqm); | |
256 | ||
257 | for (dqp = xfs_Gqm->qm_dqfreelist.qh_next; | |
258 | dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) { | |
259 | xfs_dqlock(dqp); | |
260 | nextdqp = dqp->dq_flnext; | |
261 | if (dqp->dq_flags & XFS_DQ_INACTIVE) { | |
262 | ASSERT(dqp->q_mount == NULL); | |
263 | ASSERT(! XFS_DQ_IS_DIRTY(dqp)); | |
264 | ASSERT(dqp->HL_PREVP == NULL); | |
265 | ASSERT(dqp->MPL_PREVP == NULL); | |
266 | XQM_FREELIST_REMOVE(dqp); | |
267 | xfs_dqunlock(dqp); | |
268 | xfs_qm_dqdestroy(dqp); | |
269 | } else { | |
270 | xfs_dqunlock(dqp); | |
271 | } | |
272 | dqp = nextdqp; | |
273 | } | |
274 | xfs_qm_freelist_unlock(xfs_Gqm); | |
275 | ||
276 | /* | |
277 | * Destroy the entire XQM. If somebody mounts with quotaon, this'll | |
278 | * be restarted. | |
279 | */ | |
280 | XFS_QM_LOCK(xfs_Gqm); | |
281 | XFS_QM_RELE(xfs_Gqm); | |
282 | if (xfs_Gqm->qm_nrefs == 0) { | |
283 | xfs_qm_destroy(xfs_Gqm); | |
284 | xfs_Gqm = NULL; | |
285 | } | |
286 | XFS_QM_UNLOCK(xfs_Gqm); | |
287 | } | |
288 | ||
1da177e4 LT |
289 | /* |
290 | * Just destroy the quotainfo structure. | |
291 | */ | |
292 | void | |
293 | xfs_qm_unmount_quotadestroy( | |
294 | xfs_mount_t *mp) | |
295 | { | |
296 | if (mp->m_quotainfo) | |
297 | xfs_qm_destroy_quotainfo(mp); | |
298 | } | |
299 | ||
300 | ||
301 | /* | |
302 | * This is called from xfs_mountfs to start quotas and initialize all | |
303 | * necessary data structures like quotainfo. This is also responsible for | |
304 | * running a quotacheck as necessary. We are guaranteed that the superblock | |
305 | * is consistently read in at this point. | |
53aa7915 DC |
306 | * |
307 | * If we fail here, the mount will continue with quota turned off. We don't | |
308 | * need to inidicate success or failure at all. | |
1da177e4 | 309 | */ |
53aa7915 | 310 | void |
1da177e4 | 311 | xfs_qm_mount_quotas( |
4249023a | 312 | xfs_mount_t *mp) |
1da177e4 | 313 | { |
1da177e4 LT |
314 | int error = 0; |
315 | uint sbf; | |
316 | ||
1da177e4 LT |
317 | /* |
318 | * If quotas on realtime volumes is not supported, we disable | |
319 | * quotas immediately. | |
320 | */ | |
321 | if (mp->m_sb.sb_rextents) { | |
322 | cmn_err(CE_NOTE, | |
323 | "Cannot turn on quotas for realtime filesystem %s", | |
324 | mp->m_fsname); | |
325 | mp->m_qflags = 0; | |
326 | goto write_changes; | |
327 | } | |
328 | ||
1da177e4 | 329 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
155ffd07 | 330 | |
1da177e4 LT |
331 | /* |
332 | * Allocate the quotainfo structure inside the mount struct, and | |
333 | * create quotainode(s), and change/rev superblock if necessary. | |
334 | */ | |
53aa7915 DC |
335 | error = xfs_qm_init_quotainfo(mp); |
336 | if (error) { | |
1da177e4 LT |
337 | /* |
338 | * We must turn off quotas. | |
339 | */ | |
340 | ASSERT(mp->m_quotainfo == NULL); | |
341 | mp->m_qflags = 0; | |
342 | goto write_changes; | |
343 | } | |
344 | /* | |
345 | * If any of the quotas are not consistent, do a quotacheck. | |
346 | */ | |
4249023a | 347 | if (XFS_QM_NEED_QUOTACHECK(mp)) { |
53aa7915 DC |
348 | error = xfs_qm_quotacheck(mp); |
349 | if (error) { | |
350 | /* Quotacheck failed and disabled quotas. */ | |
351 | return; | |
1da177e4 | 352 | } |
1da177e4 | 353 | } |
646d5bda DD |
354 | /* |
355 | * If one type of quotas is off, then it will lose its | |
356 | * quotachecked status, since we won't be doing accounting for | |
357 | * that type anymore. | |
358 | */ | |
53aa7915 | 359 | if (!XFS_IS_UQUOTA_ON(mp)) |
646d5bda | 360 | mp->m_qflags &= ~XFS_UQUOTA_CHKD; |
53aa7915 | 361 | if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp))) |
646d5bda | 362 | mp->m_qflags &= ~XFS_OQUOTA_CHKD; |
155ffd07 | 363 | |
1da177e4 LT |
364 | write_changes: |
365 | /* | |
3685c2a1 | 366 | * We actually don't have to acquire the m_sb_lock at all. |
1da177e4 LT |
367 | * This can only be called from mount, and that's single threaded. XXX |
368 | */ | |
3685c2a1 | 369 | spin_lock(&mp->m_sb_lock); |
1da177e4 LT |
370 | sbf = mp->m_sb.sb_qflags; |
371 | mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL; | |
3685c2a1 | 372 | spin_unlock(&mp->m_sb_lock); |
1da177e4 LT |
373 | |
374 | if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) { | |
375 | if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) { | |
376 | /* | |
377 | * We could only have been turning quotas off. | |
378 | * We aren't in very good shape actually because | |
379 | * the incore structures are convinced that quotas are | |
380 | * off, but the on disk superblock doesn't know that ! | |
381 | */ | |
382 | ASSERT(!(XFS_IS_QUOTA_RUNNING(mp))); | |
383 | xfs_fs_cmn_err(CE_ALERT, mp, | |
384 | "XFS mount_quotas: Superblock update failed!"); | |
385 | } | |
386 | } | |
387 | ||
388 | if (error) { | |
389 | xfs_fs_cmn_err(CE_WARN, mp, | |
390 | "Failed to initialize disk quotas."); | |
391 | } | |
53aa7915 | 392 | return; |
1da177e4 LT |
393 | } |
394 | ||
395 | /* | |
396 | * Called from the vfsops layer. | |
397 | */ | |
e57481dc | 398 | void |
1da177e4 LT |
399 | xfs_qm_unmount_quotas( |
400 | xfs_mount_t *mp) | |
401 | { | |
1da177e4 LT |
402 | /* |
403 | * Release the dquots that root inode, et al might be holding, | |
404 | * before we flush quotas and blow away the quotainfo structure. | |
405 | */ | |
406 | ASSERT(mp->m_rootip); | |
407 | xfs_qm_dqdetach(mp->m_rootip); | |
408 | if (mp->m_rbmip) | |
409 | xfs_qm_dqdetach(mp->m_rbmip); | |
410 | if (mp->m_rsumip) | |
411 | xfs_qm_dqdetach(mp->m_rsumip); | |
412 | ||
413 | /* | |
e57481dc | 414 | * Release the quota inodes. |
1da177e4 | 415 | */ |
1da177e4 | 416 | if (mp->m_quotainfo) { |
e57481dc CH |
417 | if (mp->m_quotainfo->qi_uquotaip) { |
418 | IRELE(mp->m_quotainfo->qi_uquotaip); | |
419 | mp->m_quotainfo->qi_uquotaip = NULL; | |
1da177e4 | 420 | } |
e57481dc CH |
421 | if (mp->m_quotainfo->qi_gquotaip) { |
422 | IRELE(mp->m_quotainfo->qi_gquotaip); | |
423 | mp->m_quotainfo->qi_gquotaip = NULL; | |
1da177e4 LT |
424 | } |
425 | } | |
1da177e4 LT |
426 | } |
427 | ||
428 | /* | |
429 | * Flush all dquots of the given file system to disk. The dquots are | |
430 | * _not_ purged from memory here, just their data written to disk. | |
431 | */ | |
ba0f32d4 | 432 | STATIC int |
1da177e4 LT |
433 | xfs_qm_dqflush_all( |
434 | xfs_mount_t *mp, | |
435 | int flags) | |
436 | { | |
437 | int recl; | |
438 | xfs_dquot_t *dqp; | |
439 | int niters; | |
440 | int error; | |
441 | ||
442 | if (mp->m_quotainfo == NULL) | |
014c2544 | 443 | return 0; |
1da177e4 LT |
444 | niters = 0; |
445 | again: | |
446 | xfs_qm_mplist_lock(mp); | |
447 | FOREACH_DQUOT_IN_MP(dqp, mp) { | |
448 | xfs_dqlock(dqp); | |
449 | if (! XFS_DQ_IS_DIRTY(dqp)) { | |
450 | xfs_dqunlock(dqp); | |
451 | continue; | |
452 | } | |
453 | xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY"); | |
454 | /* XXX a sentinel would be better */ | |
455 | recl = XFS_QI_MPLRECLAIMS(mp); | |
e1f49cf2 | 456 | if (!xfs_dqflock_nowait(dqp)) { |
1da177e4 LT |
457 | /* |
458 | * If we can't grab the flush lock then check | |
459 | * to see if the dquot has been flushed delayed | |
460 | * write. If so, grab its buffer and send it | |
461 | * out immediately. We'll be able to acquire | |
462 | * the flush lock when the I/O completes. | |
463 | */ | |
464 | xfs_qm_dqflock_pushbuf_wait(dqp); | |
465 | } | |
466 | /* | |
467 | * Let go of the mplist lock. We don't want to hold it | |
468 | * across a disk write. | |
469 | */ | |
470 | xfs_qm_mplist_unlock(mp); | |
471 | error = xfs_qm_dqflush(dqp, flags); | |
472 | xfs_dqunlock(dqp); | |
473 | if (error) | |
014c2544 | 474 | return error; |
1da177e4 LT |
475 | |
476 | xfs_qm_mplist_lock(mp); | |
477 | if (recl != XFS_QI_MPLRECLAIMS(mp)) { | |
478 | xfs_qm_mplist_unlock(mp); | |
479 | /* XXX restart limit */ | |
480 | goto again; | |
481 | } | |
482 | } | |
483 | ||
484 | xfs_qm_mplist_unlock(mp); | |
485 | /* return ! busy */ | |
014c2544 | 486 | return 0; |
1da177e4 LT |
487 | } |
488 | /* | |
489 | * Release the group dquot pointers the user dquots may be | |
490 | * carrying around as a hint. mplist is locked on entry and exit. | |
491 | */ | |
492 | STATIC void | |
493 | xfs_qm_detach_gdquots( | |
494 | xfs_mount_t *mp) | |
495 | { | |
496 | xfs_dquot_t *dqp, *gdqp; | |
497 | int nrecl; | |
498 | ||
499 | again: | |
500 | ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp)); | |
501 | dqp = XFS_QI_MPLNEXT(mp); | |
502 | while (dqp) { | |
503 | xfs_dqlock(dqp); | |
504 | if ((gdqp = dqp->q_gdquot)) { | |
505 | xfs_dqlock(gdqp); | |
506 | dqp->q_gdquot = NULL; | |
507 | } | |
508 | xfs_dqunlock(dqp); | |
509 | ||
510 | if (gdqp) { | |
511 | /* | |
512 | * Can't hold the mplist lock across a dqput. | |
513 | * XXXmust convert to marker based iterations here. | |
514 | */ | |
515 | nrecl = XFS_QI_MPLRECLAIMS(mp); | |
516 | xfs_qm_mplist_unlock(mp); | |
517 | xfs_qm_dqput(gdqp); | |
518 | ||
519 | xfs_qm_mplist_lock(mp); | |
520 | if (nrecl != XFS_QI_MPLRECLAIMS(mp)) | |
521 | goto again; | |
522 | } | |
523 | dqp = dqp->MPL_NEXT; | |
524 | } | |
525 | } | |
526 | ||
527 | /* | |
528 | * Go through all the incore dquots of this file system and take them | |
529 | * off the mplist and hashlist, if the dquot type matches the dqtype | |
530 | * parameter. This is used when turning off quota accounting for | |
531 | * users and/or groups, as well as when the filesystem is unmounting. | |
532 | */ | |
533 | STATIC int | |
534 | xfs_qm_dqpurge_int( | |
535 | xfs_mount_t *mp, | |
c8ad20ff | 536 | uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */ |
1da177e4 LT |
537 | { |
538 | xfs_dquot_t *dqp; | |
539 | uint dqtype; | |
540 | int nrecl; | |
541 | xfs_dquot_t *nextdqp; | |
542 | int nmisses; | |
543 | ||
544 | if (mp->m_quotainfo == NULL) | |
014c2544 | 545 | return 0; |
1da177e4 LT |
546 | |
547 | dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0; | |
c8ad20ff | 548 | dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0; |
1da177e4 LT |
549 | dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0; |
550 | ||
551 | xfs_qm_mplist_lock(mp); | |
552 | ||
553 | /* | |
554 | * In the first pass through all incore dquots of this filesystem, | |
555 | * we release the group dquot pointers the user dquots may be | |
556 | * carrying around as a hint. We need to do this irrespective of | |
557 | * what's being turned off. | |
558 | */ | |
559 | xfs_qm_detach_gdquots(mp); | |
560 | ||
561 | again: | |
562 | nmisses = 0; | |
563 | ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp)); | |
564 | /* | |
565 | * Try to get rid of all of the unwanted dquots. The idea is to | |
566 | * get them off mplist and hashlist, but leave them on freelist. | |
567 | */ | |
568 | dqp = XFS_QI_MPLNEXT(mp); | |
569 | while (dqp) { | |
570 | /* | |
571 | * It's OK to look at the type without taking dqlock here. | |
572 | * We're holding the mplist lock here, and that's needed for | |
573 | * a dqreclaim. | |
574 | */ | |
575 | if ((dqp->dq_flags & dqtype) == 0) { | |
576 | dqp = dqp->MPL_NEXT; | |
577 | continue; | |
578 | } | |
579 | ||
580 | if (! xfs_qm_dqhashlock_nowait(dqp)) { | |
581 | nrecl = XFS_QI_MPLRECLAIMS(mp); | |
582 | xfs_qm_mplist_unlock(mp); | |
583 | XFS_DQ_HASH_LOCK(dqp->q_hash); | |
584 | xfs_qm_mplist_lock(mp); | |
585 | ||
586 | /* | |
587 | * XXXTheoretically, we can get into a very long | |
588 | * ping pong game here. | |
589 | * No one can be adding dquots to the mplist at | |
590 | * this point, but somebody might be taking things off. | |
591 | */ | |
592 | if (nrecl != XFS_QI_MPLRECLAIMS(mp)) { | |
593 | XFS_DQ_HASH_UNLOCK(dqp->q_hash); | |
594 | goto again; | |
595 | } | |
596 | } | |
597 | ||
598 | /* | |
599 | * Take the dquot off the mplist and hashlist. It may remain on | |
600 | * freelist in INACTIVE state. | |
601 | */ | |
602 | nextdqp = dqp->MPL_NEXT; | |
4f0e8a98 | 603 | nmisses += xfs_qm_dqpurge(dqp); |
1da177e4 LT |
604 | dqp = nextdqp; |
605 | } | |
606 | xfs_qm_mplist_unlock(mp); | |
607 | return nmisses; | |
608 | } | |
609 | ||
610 | int | |
611 | xfs_qm_dqpurge_all( | |
612 | xfs_mount_t *mp, | |
613 | uint flags) | |
614 | { | |
615 | int ndquots; | |
616 | ||
617 | /* | |
618 | * Purge the dquot cache. | |
619 | * None of the dquots should really be busy at this point. | |
620 | */ | |
621 | if (mp->m_quotainfo) { | |
622 | while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) { | |
623 | delay(ndquots * 10); | |
624 | } | |
625 | } | |
626 | return 0; | |
627 | } | |
628 | ||
629 | STATIC int | |
630 | xfs_qm_dqattach_one( | |
631 | xfs_inode_t *ip, | |
632 | xfs_dqid_t id, | |
633 | uint type, | |
634 | uint doalloc, | |
1da177e4 LT |
635 | xfs_dquot_t *udqhint, /* hint */ |
636 | xfs_dquot_t **IO_idqpp) | |
637 | { | |
638 | xfs_dquot_t *dqp; | |
639 | int error; | |
640 | ||
579aa9ca | 641 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 | 642 | error = 0; |
8e9b6e7f | 643 | |
1da177e4 LT |
644 | /* |
645 | * See if we already have it in the inode itself. IO_idqpp is | |
646 | * &i_udquot or &i_gdquot. This made the code look weird, but | |
647 | * made the logic a lot simpler. | |
648 | */ | |
8e9b6e7f CH |
649 | dqp = *IO_idqpp; |
650 | if (dqp) { | |
1da177e4 | 651 | xfs_dqtrace_entry(dqp, "DQATTACH: found in ip"); |
8e9b6e7f | 652 | return 0; |
1da177e4 LT |
653 | } |
654 | ||
655 | /* | |
656 | * udqhint is the i_udquot field in inode, and is non-NULL only | |
c8ad20ff | 657 | * when the type arg is group/project. Its purpose is to save a |
1da177e4 LT |
658 | * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside |
659 | * the user dquot. | |
660 | */ | |
8e9b6e7f CH |
661 | if (udqhint) { |
662 | ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ); | |
1da177e4 LT |
663 | xfs_dqlock(udqhint); |
664 | ||
8e9b6e7f CH |
665 | /* |
666 | * No need to take dqlock to look at the id. | |
667 | * | |
668 | * The ID can't change until it gets reclaimed, and it won't | |
669 | * be reclaimed as long as we have a ref from inode and we | |
670 | * hold the ilock. | |
671 | */ | |
672 | dqp = udqhint->q_gdquot; | |
673 | if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) { | |
674 | xfs_dqlock(dqp); | |
675 | XFS_DQHOLD(dqp); | |
676 | ASSERT(*IO_idqpp == NULL); | |
677 | *IO_idqpp = dqp; | |
678 | ||
1da177e4 LT |
679 | xfs_dqunlock(dqp); |
680 | xfs_dqunlock(udqhint); | |
8e9b6e7f | 681 | return 0; |
1da177e4 | 682 | } |
8e9b6e7f CH |
683 | |
684 | /* | |
685 | * We can't hold a dquot lock when we call the dqget code. | |
686 | * We'll deadlock in no time, because of (not conforming to) | |
687 | * lock ordering - the inodelock comes before any dquot lock, | |
688 | * and we may drop and reacquire the ilock in xfs_qm_dqget(). | |
689 | */ | |
1da177e4 | 690 | xfs_dqunlock(udqhint); |
8e9b6e7f CH |
691 | } |
692 | ||
1da177e4 LT |
693 | /* |
694 | * Find the dquot from somewhere. This bumps the | |
695 | * reference count of dquot and returns it locked. | |
696 | * This can return ENOENT if dquot didn't exist on | |
697 | * disk and we didn't ask it to allocate; | |
698 | * ESRCH if quotas got turned off suddenly. | |
699 | */ | |
8e9b6e7f CH |
700 | error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp); |
701 | if (error) | |
702 | return error; | |
1da177e4 LT |
703 | |
704 | xfs_dqtrace_entry(dqp, "DQATTACH: found by dqget"); | |
8e9b6e7f | 705 | |
1da177e4 LT |
706 | /* |
707 | * dqget may have dropped and re-acquired the ilock, but it guarantees | |
708 | * that the dquot returned is the one that should go in the inode. | |
709 | */ | |
710 | *IO_idqpp = dqp; | |
8e9b6e7f CH |
711 | xfs_dqunlock(dqp); |
712 | return 0; | |
1da177e4 LT |
713 | } |
714 | ||
715 | ||
716 | /* | |
717 | * Given a udquot and gdquot, attach a ptr to the group dquot in the | |
718 | * udquot as a hint for future lookups. The idea sounds simple, but the | |
719 | * execution isn't, because the udquot might have a group dquot attached | |
c41564b5 | 720 | * already and getting rid of that gets us into lock ordering constraints. |
1da177e4 LT |
721 | * The process is complicated more by the fact that the dquots may or may not |
722 | * be locked on entry. | |
723 | */ | |
724 | STATIC void | |
725 | xfs_qm_dqattach_grouphint( | |
726 | xfs_dquot_t *udq, | |
8e9b6e7f | 727 | xfs_dquot_t *gdq) |
1da177e4 LT |
728 | { |
729 | xfs_dquot_t *tmp; | |
730 | ||
8e9b6e7f | 731 | xfs_dqlock(udq); |
1da177e4 LT |
732 | |
733 | if ((tmp = udq->q_gdquot)) { | |
734 | if (tmp == gdq) { | |
8e9b6e7f | 735 | xfs_dqunlock(udq); |
1da177e4 LT |
736 | return; |
737 | } | |
738 | ||
739 | udq->q_gdquot = NULL; | |
740 | /* | |
741 | * We can't keep any dqlocks when calling dqrele, | |
742 | * because the freelist lock comes before dqlocks. | |
743 | */ | |
744 | xfs_dqunlock(udq); | |
1da177e4 LT |
745 | /* |
746 | * we took a hard reference once upon a time in dqget, | |
747 | * so give it back when the udquot no longer points at it | |
748 | * dqput() does the unlocking of the dquot. | |
749 | */ | |
750 | xfs_qm_dqrele(tmp); | |
751 | ||
752 | xfs_dqlock(udq); | |
753 | xfs_dqlock(gdq); | |
754 | ||
755 | } else { | |
756 | ASSERT(XFS_DQ_IS_LOCKED(udq)); | |
8e9b6e7f | 757 | xfs_dqlock(gdq); |
1da177e4 LT |
758 | } |
759 | ||
760 | ASSERT(XFS_DQ_IS_LOCKED(udq)); | |
761 | ASSERT(XFS_DQ_IS_LOCKED(gdq)); | |
762 | /* | |
763 | * Somebody could have attached a gdquot here, | |
764 | * when we dropped the uqlock. If so, just do nothing. | |
765 | */ | |
766 | if (udq->q_gdquot == NULL) { | |
767 | XFS_DQHOLD(gdq); | |
768 | udq->q_gdquot = gdq; | |
769 | } | |
8e9b6e7f CH |
770 | |
771 | xfs_dqunlock(gdq); | |
772 | xfs_dqunlock(udq); | |
1da177e4 LT |
773 | } |
774 | ||
775 | ||
776 | /* | |
c8ad20ff NS |
777 | * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON |
778 | * into account. | |
1da177e4 | 779 | * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed. |
1da177e4 LT |
780 | * If XFS_QMOPT_ILOCKED, then inode sent is already locked EXCL. |
781 | * Inode may get unlocked and relocked in here, and the caller must deal with | |
782 | * the consequences. | |
783 | */ | |
784 | int | |
785 | xfs_qm_dqattach( | |
786 | xfs_inode_t *ip, | |
787 | uint flags) | |
788 | { | |
789 | xfs_mount_t *mp = ip->i_mount; | |
790 | uint nquotas = 0; | |
791 | int error = 0; | |
792 | ||
793 | if ((! XFS_IS_QUOTA_ON(mp)) || | |
794 | (! XFS_NOT_DQATTACHED(mp, ip)) || | |
795 | (ip->i_ino == mp->m_sb.sb_uquotino) || | |
796 | (ip->i_ino == mp->m_sb.sb_gquotino)) | |
014c2544 | 797 | return 0; |
1da177e4 LT |
798 | |
799 | ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 || | |
579aa9ca | 800 | xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
801 | |
802 | if (! (flags & XFS_QMOPT_ILOCKED)) | |
803 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
804 | ||
805 | if (XFS_IS_UQUOTA_ON(mp)) { | |
806 | error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER, | |
807 | flags & XFS_QMOPT_DQALLOC, | |
1da177e4 LT |
808 | NULL, &ip->i_udquot); |
809 | if (error) | |
810 | goto done; | |
811 | nquotas++; | |
812 | } | |
579aa9ca CH |
813 | |
814 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | |
c8ad20ff NS |
815 | if (XFS_IS_OQUOTA_ON(mp)) { |
816 | error = XFS_IS_GQUOTA_ON(mp) ? | |
817 | xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP, | |
818 | flags & XFS_QMOPT_DQALLOC, | |
c8ad20ff NS |
819 | ip->i_udquot, &ip->i_gdquot) : |
820 | xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ, | |
1da177e4 | 821 | flags & XFS_QMOPT_DQALLOC, |
1da177e4 LT |
822 | ip->i_udquot, &ip->i_gdquot); |
823 | /* | |
824 | * Don't worry about the udquot that we may have | |
825 | * attached above. It'll get detached, if not already. | |
826 | */ | |
827 | if (error) | |
828 | goto done; | |
829 | nquotas++; | |
830 | } | |
831 | ||
832 | /* | |
833 | * Attach this group quota to the user quota as a hint. | |
834 | * This WON'T, in general, result in a thrash. | |
835 | */ | |
836 | if (nquotas == 2) { | |
579aa9ca | 837 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
838 | ASSERT(ip->i_udquot); |
839 | ASSERT(ip->i_gdquot); | |
840 | ||
841 | /* | |
842 | * We may or may not have the i_udquot locked at this point, | |
843 | * but this check is OK since we don't depend on the i_gdquot to | |
844 | * be accurate 100% all the time. It is just a hint, and this | |
845 | * will succeed in general. | |
846 | */ | |
847 | if (ip->i_udquot->q_gdquot == ip->i_gdquot) | |
848 | goto done; | |
849 | /* | |
850 | * Attach i_gdquot to the gdquot hint inside the i_udquot. | |
851 | */ | |
8e9b6e7f | 852 | xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot); |
1da177e4 LT |
853 | } |
854 | ||
855 | done: | |
856 | ||
857 | #ifdef QUOTADEBUG | |
858 | if (! error) { | |
1da177e4 LT |
859 | if (XFS_IS_UQUOTA_ON(mp)) |
860 | ASSERT(ip->i_udquot); | |
c8ad20ff | 861 | if (XFS_IS_OQUOTA_ON(mp)) |
1da177e4 LT |
862 | ASSERT(ip->i_gdquot); |
863 | } | |
864 | #endif | |
865 | ||
866 | if (! (flags & XFS_QMOPT_ILOCKED)) | |
867 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
868 | ||
869 | #ifdef QUOTADEBUG | |
870 | else | |
579aa9ca | 871 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 | 872 | #endif |
014c2544 | 873 | return error; |
1da177e4 LT |
874 | } |
875 | ||
876 | /* | |
877 | * Release dquots (and their references) if any. | |
878 | * The inode should be locked EXCL except when this's called by | |
879 | * xfs_ireclaim. | |
880 | */ | |
881 | void | |
882 | xfs_qm_dqdetach( | |
883 | xfs_inode_t *ip) | |
884 | { | |
885 | if (!(ip->i_udquot || ip->i_gdquot)) | |
886 | return; | |
887 | ||
888 | ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino); | |
889 | ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino); | |
1da177e4 | 890 | if (ip->i_udquot) { |
c8ad20ff | 891 | xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip); |
1da177e4 LT |
892 | xfs_qm_dqrele(ip->i_udquot); |
893 | ip->i_udquot = NULL; | |
894 | } | |
895 | if (ip->i_gdquot) { | |
c8ad20ff | 896 | xfs_dqtrace_entry_ino(ip->i_gdquot, "DQDETTACH", ip); |
1da177e4 LT |
897 | xfs_qm_dqrele(ip->i_gdquot); |
898 | ip->i_gdquot = NULL; | |
899 | } | |
900 | } | |
901 | ||
902 | /* | |
a4e4c4f4 DC |
903 | * This is called to sync quotas. We can be told to use non-blocking |
904 | * semantics by either the SYNC_BDFLUSH flag or the absence of the | |
905 | * SYNC_WAIT flag. | |
1da177e4 | 906 | */ |
1da177e4 LT |
907 | int |
908 | xfs_qm_sync( | |
909 | xfs_mount_t *mp, | |
b09cc771 | 910 | int flags) |
1da177e4 LT |
911 | { |
912 | int recl, restarts; | |
913 | xfs_dquot_t *dqp; | |
914 | uint flush_flags; | |
915 | boolean_t nowait; | |
916 | int error; | |
917 | ||
d757762b DD |
918 | if (! XFS_IS_QUOTA_ON(mp)) |
919 | return 0; | |
920 | ||
1da177e4 LT |
921 | restarts = 0; |
922 | /* | |
923 | * We won't block unless we are asked to. | |
924 | */ | |
925 | nowait = (boolean_t)(flags & SYNC_BDFLUSH || (flags & SYNC_WAIT) == 0); | |
926 | ||
927 | again: | |
928 | xfs_qm_mplist_lock(mp); | |
929 | /* | |
930 | * dqpurge_all() also takes the mplist lock and iterate thru all dquots | |
931 | * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared | |
932 | * when we have the mplist lock, we know that dquots will be consistent | |
933 | * as long as we have it locked. | |
934 | */ | |
935 | if (! XFS_IS_QUOTA_ON(mp)) { | |
936 | xfs_qm_mplist_unlock(mp); | |
014c2544 | 937 | return 0; |
1da177e4 LT |
938 | } |
939 | FOREACH_DQUOT_IN_MP(dqp, mp) { | |
940 | /* | |
941 | * If this is vfs_sync calling, then skip the dquots that | |
942 | * don't 'seem' to be dirty. ie. don't acquire dqlock. | |
943 | * This is very similar to what xfs_sync does with inodes. | |
944 | */ | |
945 | if (flags & SYNC_BDFLUSH) { | |
946 | if (! XFS_DQ_IS_DIRTY(dqp)) | |
947 | continue; | |
948 | } | |
949 | ||
950 | if (nowait) { | |
951 | /* | |
952 | * Try to acquire the dquot lock. We are NOT out of | |
953 | * lock order, but we just don't want to wait for this | |
954 | * lock, unless somebody wanted us to. | |
955 | */ | |
956 | if (! xfs_qm_dqlock_nowait(dqp)) | |
957 | continue; | |
958 | } else { | |
959 | xfs_dqlock(dqp); | |
960 | } | |
961 | ||
962 | /* | |
963 | * Now, find out for sure if this dquot is dirty or not. | |
964 | */ | |
965 | if (! XFS_DQ_IS_DIRTY(dqp)) { | |
966 | xfs_dqunlock(dqp); | |
967 | continue; | |
968 | } | |
969 | ||
970 | /* XXX a sentinel would be better */ | |
971 | recl = XFS_QI_MPLRECLAIMS(mp); | |
e1f49cf2 | 972 | if (!xfs_dqflock_nowait(dqp)) { |
1da177e4 LT |
973 | if (nowait) { |
974 | xfs_dqunlock(dqp); | |
975 | continue; | |
976 | } | |
977 | /* | |
978 | * If we can't grab the flush lock then if the caller | |
c41564b5 | 979 | * really wanted us to give this our best shot, so |
1da177e4 LT |
980 | * see if we can give a push to the buffer before we wait |
981 | * on the flush lock. At this point, we know that | |
c41564b5 | 982 | * even though the dquot is being flushed, |
1da177e4 LT |
983 | * it has (new) dirty data. |
984 | */ | |
985 | xfs_qm_dqflock_pushbuf_wait(dqp); | |
986 | } | |
987 | /* | |
988 | * Let go of the mplist lock. We don't want to hold it | |
989 | * across a disk write | |
990 | */ | |
991 | flush_flags = (nowait) ? XFS_QMOPT_DELWRI : XFS_QMOPT_SYNC; | |
992 | xfs_qm_mplist_unlock(mp); | |
993 | xfs_dqtrace_entry(dqp, "XQM_SYNC: DQFLUSH"); | |
994 | error = xfs_qm_dqflush(dqp, flush_flags); | |
995 | xfs_dqunlock(dqp); | |
996 | if (error && XFS_FORCED_SHUTDOWN(mp)) | |
014c2544 | 997 | return 0; /* Need to prevent umount failure */ |
1da177e4 | 998 | else if (error) |
014c2544 | 999 | return error; |
1da177e4 LT |
1000 | |
1001 | xfs_qm_mplist_lock(mp); | |
1002 | if (recl != XFS_QI_MPLRECLAIMS(mp)) { | |
1003 | if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS) | |
1004 | break; | |
1005 | ||
1006 | xfs_qm_mplist_unlock(mp); | |
1007 | goto again; | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | xfs_qm_mplist_unlock(mp); | |
014c2544 | 1012 | return 0; |
1da177e4 LT |
1013 | } |
1014 | ||
a4edd1da CH |
1015 | /* |
1016 | * The hash chains and the mplist use the same xfs_dqhash structure as | |
1017 | * their list head, but we can take the mplist qh_lock and one of the | |
1018 | * hash qh_locks at the same time without any problem as they aren't | |
1019 | * related. | |
1020 | */ | |
1021 | static struct lock_class_key xfs_quota_mplist_class; | |
1da177e4 LT |
1022 | |
1023 | /* | |
1024 | * This initializes all the quota information that's kept in the | |
1025 | * mount structure | |
1026 | */ | |
ba0f32d4 | 1027 | STATIC int |
1da177e4 LT |
1028 | xfs_qm_init_quotainfo( |
1029 | xfs_mount_t *mp) | |
1030 | { | |
1031 | xfs_quotainfo_t *qinf; | |
1032 | int error; | |
1033 | xfs_dquot_t *dqp; | |
1034 | ||
1035 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); | |
1036 | ||
1037 | /* | |
1038 | * Tell XQM that we exist as soon as possible. | |
1039 | */ | |
1040 | if ((error = xfs_qm_hold_quotafs_ref(mp))) { | |
014c2544 | 1041 | return error; |
1da177e4 LT |
1042 | } |
1043 | ||
1044 | qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP); | |
1045 | ||
1046 | /* | |
1047 | * See if quotainodes are setup, and if not, allocate them, | |
1048 | * and change the superblock accordingly. | |
1049 | */ | |
1050 | if ((error = xfs_qm_init_quotainos(mp))) { | |
f0e2d93c | 1051 | kmem_free(qinf); |
1da177e4 | 1052 | mp->m_quotainfo = NULL; |
014c2544 | 1053 | return error; |
1da177e4 LT |
1054 | } |
1055 | ||
1da177e4 | 1056 | xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0); |
a4edd1da CH |
1057 | lockdep_set_class(&qinf->qi_dqlist.qh_lock, &xfs_quota_mplist_class); |
1058 | ||
1da177e4 LT |
1059 | qinf->qi_dqreclaims = 0; |
1060 | ||
1061 | /* mutex used to serialize quotaoffs */ | |
794ee1ba | 1062 | mutex_init(&qinf->qi_quotaofflock); |
1da177e4 LT |
1063 | |
1064 | /* Precalc some constants */ | |
1065 | qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB); | |
1066 | ASSERT(qinf->qi_dqchunklen); | |
1067 | qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen); | |
1068 | do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t)); | |
1069 | ||
1070 | mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD); | |
1071 | ||
1072 | /* | |
1073 | * We try to get the limits from the superuser's limits fields. | |
1074 | * This is quite hacky, but it is standard quota practice. | |
1075 | * We look at the USR dquot with id == 0 first, but if user quotas | |
1076 | * are not enabled we goto the GRP dquot with id == 0. | |
1077 | * We don't really care to keep separate default limits for user | |
1078 | * and group quotas, at least not at this point. | |
1079 | */ | |
1080 | error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0, | |
c8ad20ff NS |
1081 | XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER : |
1082 | (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP : | |
1083 | XFS_DQ_PROJ), | |
1da177e4 LT |
1084 | XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN, |
1085 | &dqp); | |
1086 | if (! error) { | |
1087 | xfs_disk_dquot_t *ddqp = &dqp->q_core; | |
1088 | ||
1089 | /* | |
1090 | * The warnings and timers set the grace period given to | |
1091 | * a user or group before he or she can not perform any | |
1092 | * more writing. If it is zero, a default is used. | |
1093 | */ | |
1149d96a CH |
1094 | qinf->qi_btimelimit = ddqp->d_btimer ? |
1095 | be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT; | |
1096 | qinf->qi_itimelimit = ddqp->d_itimer ? | |
1097 | be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT; | |
1098 | qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ? | |
1099 | be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT; | |
1100 | qinf->qi_bwarnlimit = ddqp->d_bwarns ? | |
1101 | be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT; | |
1102 | qinf->qi_iwarnlimit = ddqp->d_iwarns ? | |
1103 | be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT; | |
1104 | qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ? | |
1105 | be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT; | |
1106 | qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit); | |
1107 | qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit); | |
1108 | qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit); | |
1109 | qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit); | |
1110 | qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit); | |
1111 | qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit); | |
1da177e4 LT |
1112 | |
1113 | /* | |
1114 | * We sent the XFS_QMOPT_DQSUSER flag to dqget because | |
1115 | * we don't want this dquot cached. We haven't done a | |
1116 | * quotacheck yet, and quotacheck doesn't like incore dquots. | |
1117 | */ | |
1118 | xfs_qm_dqdestroy(dqp); | |
1119 | } else { | |
1120 | qinf->qi_btimelimit = XFS_QM_BTIMELIMIT; | |
1121 | qinf->qi_itimelimit = XFS_QM_ITIMELIMIT; | |
1122 | qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT; | |
1123 | qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT; | |
1124 | qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT; | |
06d10dd9 | 1125 | qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT; |
1da177e4 LT |
1126 | } |
1127 | ||
014c2544 | 1128 | return 0; |
1da177e4 LT |
1129 | } |
1130 | ||
1131 | ||
1132 | /* | |
1133 | * Gets called when unmounting a filesystem or when all quotas get | |
1134 | * turned off. | |
1135 | * This purges the quota inodes, destroys locks and frees itself. | |
1136 | */ | |
1137 | void | |
1138 | xfs_qm_destroy_quotainfo( | |
1139 | xfs_mount_t *mp) | |
1140 | { | |
1141 | xfs_quotainfo_t *qi; | |
1142 | ||
1143 | qi = mp->m_quotainfo; | |
1144 | ASSERT(qi != NULL); | |
1145 | ASSERT(xfs_Gqm != NULL); | |
1146 | ||
1147 | /* | |
1148 | * Release the reference that XQM kept, so that we know | |
1149 | * when the XQM structure should be freed. We cannot assume | |
1150 | * that xfs_Gqm is non-null after this point. | |
1151 | */ | |
1152 | xfs_qm_rele_quotafs_ref(mp); | |
1153 | ||
1da177e4 LT |
1154 | xfs_qm_list_destroy(&qi->qi_dqlist); |
1155 | ||
1156 | if (qi->qi_uquotaip) { | |
26cc0021 | 1157 | IRELE(qi->qi_uquotaip); |
1da177e4 LT |
1158 | qi->qi_uquotaip = NULL; /* paranoia */ |
1159 | } | |
1160 | if (qi->qi_gquotaip) { | |
26cc0021 | 1161 | IRELE(qi->qi_gquotaip); |
1da177e4 LT |
1162 | qi->qi_gquotaip = NULL; |
1163 | } | |
1164 | mutex_destroy(&qi->qi_quotaofflock); | |
f0e2d93c | 1165 | kmem_free(qi); |
1da177e4 LT |
1166 | mp->m_quotainfo = NULL; |
1167 | } | |
1168 | ||
1169 | ||
1170 | ||
1171 | /* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */ | |
1172 | ||
1173 | /* ARGSUSED */ | |
1174 | STATIC void | |
1175 | xfs_qm_list_init( | |
1176 | xfs_dqlist_t *list, | |
1177 | char *str, | |
1178 | int n) | |
1179 | { | |
794ee1ba | 1180 | mutex_init(&list->qh_lock); |
1da177e4 LT |
1181 | list->qh_next = NULL; |
1182 | list->qh_version = 0; | |
1183 | list->qh_nelems = 0; | |
1184 | } | |
1185 | ||
1186 | STATIC void | |
1187 | xfs_qm_list_destroy( | |
1188 | xfs_dqlist_t *list) | |
1189 | { | |
1190 | mutex_destroy(&(list->qh_lock)); | |
1191 | } | |
1192 | ||
1193 | ||
1194 | /* | |
1195 | * Stripped down version of dqattach. This doesn't attach, or even look at the | |
1196 | * dquots attached to the inode. The rationale is that there won't be any | |
1197 | * attached at the time this is called from quotacheck. | |
1198 | */ | |
1199 | STATIC int | |
1200 | xfs_qm_dqget_noattach( | |
1201 | xfs_inode_t *ip, | |
1202 | xfs_dquot_t **O_udqpp, | |
1203 | xfs_dquot_t **O_gdqpp) | |
1204 | { | |
1205 | int error; | |
1206 | xfs_mount_t *mp; | |
1207 | xfs_dquot_t *udqp, *gdqp; | |
1208 | ||
579aa9ca | 1209 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
1210 | mp = ip->i_mount; |
1211 | udqp = NULL; | |
1212 | gdqp = NULL; | |
1213 | ||
1214 | if (XFS_IS_UQUOTA_ON(mp)) { | |
1215 | ASSERT(ip->i_udquot == NULL); | |
1216 | /* | |
1217 | * We want the dquot allocated if it doesn't exist. | |
1218 | */ | |
1219 | if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER, | |
1220 | XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, | |
1221 | &udqp))) { | |
1222 | /* | |
1223 | * Shouldn't be able to turn off quotas here. | |
1224 | */ | |
1225 | ASSERT(error != ESRCH); | |
1226 | ASSERT(error != ENOENT); | |
014c2544 | 1227 | return error; |
1da177e4 LT |
1228 | } |
1229 | ASSERT(udqp); | |
1230 | } | |
1231 | ||
c8ad20ff | 1232 | if (XFS_IS_OQUOTA_ON(mp)) { |
1da177e4 LT |
1233 | ASSERT(ip->i_gdquot == NULL); |
1234 | if (udqp) | |
1235 | xfs_dqunlock(udqp); | |
c8ad20ff NS |
1236 | error = XFS_IS_GQUOTA_ON(mp) ? |
1237 | xfs_qm_dqget(mp, ip, | |
1238 | ip->i_d.di_gid, XFS_DQ_GROUP, | |
1239 | XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN, | |
1240 | &gdqp) : | |
1241 | xfs_qm_dqget(mp, ip, | |
1242 | ip->i_d.di_projid, XFS_DQ_PROJ, | |
1243 | XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN, | |
1244 | &gdqp); | |
1245 | if (error) { | |
1da177e4 LT |
1246 | if (udqp) |
1247 | xfs_qm_dqrele(udqp); | |
1248 | ASSERT(error != ESRCH); | |
1249 | ASSERT(error != ENOENT); | |
014c2544 | 1250 | return error; |
1da177e4 LT |
1251 | } |
1252 | ASSERT(gdqp); | |
1253 | ||
1254 | /* Reacquire the locks in the right order */ | |
1255 | if (udqp) { | |
1256 | if (! xfs_qm_dqlock_nowait(udqp)) { | |
1257 | xfs_dqunlock(gdqp); | |
1258 | xfs_dqlock(udqp); | |
1259 | xfs_dqlock(gdqp); | |
1260 | } | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | *O_udqpp = udqp; | |
1265 | *O_gdqpp = gdqp; | |
1266 | ||
1267 | #ifdef QUOTADEBUG | |
1268 | if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp)); | |
1269 | if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp)); | |
1270 | #endif | |
014c2544 | 1271 | return 0; |
1da177e4 LT |
1272 | } |
1273 | ||
1274 | /* | |
1275 | * Create an inode and return with a reference already taken, but unlocked | |
1276 | * This is how we create quota inodes | |
1277 | */ | |
1278 | STATIC int | |
1279 | xfs_qm_qino_alloc( | |
1280 | xfs_mount_t *mp, | |
1281 | xfs_inode_t **ip, | |
1282 | __int64_t sbfields, | |
1283 | uint flags) | |
1284 | { | |
1285 | xfs_trans_t *tp; | |
1286 | int error; | |
1da177e4 LT |
1287 | int committed; |
1288 | ||
061f7209 | 1289 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE); |
1da177e4 LT |
1290 | if ((error = xfs_trans_reserve(tp, |
1291 | XFS_QM_QINOCREATE_SPACE_RES(mp), | |
1292 | XFS_CREATE_LOG_RES(mp), 0, | |
1293 | XFS_TRANS_PERM_LOG_RES, | |
1294 | XFS_CREATE_LOG_COUNT))) { | |
1295 | xfs_trans_cancel(tp, 0); | |
014c2544 | 1296 | return error; |
1da177e4 | 1297 | } |
1da177e4 | 1298 | |
b11f94d5 | 1299 | if ((error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, |
dae81d47 | 1300 | &xfs_zerocr, 0, 1, ip, &committed))) { |
1da177e4 LT |
1301 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | |
1302 | XFS_TRANS_ABORT); | |
014c2544 | 1303 | return error; |
1da177e4 LT |
1304 | } |
1305 | ||
1306 | /* | |
1307 | * Keep an extra reference to this quota inode. This inode is | |
1308 | * locked exclusively and joined to the transaction already. | |
1309 | */ | |
579aa9ca | 1310 | ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL)); |
26cc0021 | 1311 | IHOLD(*ip); |
1da177e4 LT |
1312 | |
1313 | /* | |
1314 | * Make the changes in the superblock, and log those too. | |
1315 | * sbfields arg may contain fields other than *QUOTINO; | |
1316 | * VERSIONNUM for example. | |
1317 | */ | |
3685c2a1 | 1318 | spin_lock(&mp->m_sb_lock); |
1da177e4 LT |
1319 | if (flags & XFS_QMOPT_SBVERSION) { |
1320 | #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY) | |
1321 | unsigned oldv = mp->m_sb.sb_versionnum; | |
1322 | #endif | |
62118709 | 1323 | ASSERT(!xfs_sb_version_hasquota(&mp->m_sb)); |
1da177e4 LT |
1324 | ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO | |
1325 | XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) == | |
1326 | (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO | | |
1327 | XFS_SB_GQUOTINO | XFS_SB_QFLAGS)); | |
1328 | ||
62118709 | 1329 | xfs_sb_version_addquota(&mp->m_sb); |
1da177e4 LT |
1330 | mp->m_sb.sb_uquotino = NULLFSINO; |
1331 | mp->m_sb.sb_gquotino = NULLFSINO; | |
1332 | ||
1333 | /* qflags will get updated _after_ quotacheck */ | |
1334 | mp->m_sb.sb_qflags = 0; | |
1335 | #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY) | |
1336 | cmn_err(CE_NOTE, | |
1337 | "Old superblock version %x, converting to %x.", | |
1338 | oldv, mp->m_sb.sb_versionnum); | |
1339 | #endif | |
1340 | } | |
1341 | if (flags & XFS_QMOPT_UQUOTA) | |
1342 | mp->m_sb.sb_uquotino = (*ip)->i_ino; | |
1343 | else | |
1344 | mp->m_sb.sb_gquotino = (*ip)->i_ino; | |
3685c2a1 | 1345 | spin_unlock(&mp->m_sb_lock); |
1da177e4 LT |
1346 | xfs_mod_sb(tp, sbfields); |
1347 | ||
1c72bf90 | 1348 | if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) { |
1da177e4 | 1349 | xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!"); |
014c2544 | 1350 | return error; |
1da177e4 | 1351 | } |
014c2544 | 1352 | return 0; |
1da177e4 LT |
1353 | } |
1354 | ||
1355 | ||
5b139738 | 1356 | STATIC void |
1da177e4 LT |
1357 | xfs_qm_reset_dqcounts( |
1358 | xfs_mount_t *mp, | |
1359 | xfs_buf_t *bp, | |
1360 | xfs_dqid_t id, | |
1361 | uint type) | |
1362 | { | |
1363 | xfs_disk_dquot_t *ddq; | |
1364 | int j; | |
1365 | ||
1366 | xfs_buftrace("RESET DQUOTS", bp); | |
1367 | /* | |
1368 | * Reset all counters and timers. They'll be | |
1369 | * started afresh by xfs_qm_quotacheck. | |
1370 | */ | |
1371 | #ifdef DEBUG | |
1372 | j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB); | |
1373 | do_div(j, sizeof(xfs_dqblk_t)); | |
1374 | ASSERT(XFS_QM_DQPERBLK(mp) == j); | |
1375 | #endif | |
1376 | ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp); | |
1377 | for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) { | |
1378 | /* | |
1379 | * Do a sanity check, and if needed, repair the dqblk. Don't | |
1380 | * output any warnings because it's perfectly possible to | |
c41564b5 | 1381 | * find uninitialised dquot blks. See comment in xfs_qm_dqcheck. |
1da177e4 LT |
1382 | */ |
1383 | (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR, | |
1384 | "xfs_quotacheck"); | |
1149d96a CH |
1385 | ddq->d_bcount = 0; |
1386 | ddq->d_icount = 0; | |
1387 | ddq->d_rtbcount = 0; | |
1388 | ddq->d_btimer = 0; | |
1389 | ddq->d_itimer = 0; | |
1390 | ddq->d_rtbtimer = 0; | |
1391 | ddq->d_bwarns = 0; | |
1392 | ddq->d_iwarns = 0; | |
1393 | ddq->d_rtbwarns = 0; | |
1da177e4 LT |
1394 | ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1); |
1395 | } | |
1da177e4 LT |
1396 | } |
1397 | ||
1398 | STATIC int | |
1399 | xfs_qm_dqiter_bufs( | |
1400 | xfs_mount_t *mp, | |
1401 | xfs_dqid_t firstid, | |
1402 | xfs_fsblock_t bno, | |
1403 | xfs_filblks_t blkcnt, | |
1404 | uint flags) | |
1405 | { | |
1406 | xfs_buf_t *bp; | |
1407 | int error; | |
1408 | int notcommitted; | |
1409 | int incr; | |
c8ad20ff | 1410 | int type; |
1da177e4 LT |
1411 | |
1412 | ASSERT(blkcnt > 0); | |
1413 | notcommitted = 0; | |
1414 | incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ? | |
1415 | XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt; | |
c8ad20ff NS |
1416 | type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER : |
1417 | (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP); | |
1da177e4 LT |
1418 | error = 0; |
1419 | ||
1420 | /* | |
1421 | * Blkcnt arg can be a very big number, and might even be | |
1422 | * larger than the log itself. So, we have to break it up into | |
1423 | * manageable-sized transactions. | |
1424 | * Note that we don't start a permanent transaction here; we might | |
1425 | * not be able to get a log reservation for the whole thing up front, | |
1426 | * and we don't really care to either, because we just discard | |
1427 | * everything if we were to crash in the middle of this loop. | |
1428 | */ | |
1429 | while (blkcnt--) { | |
1430 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, | |
1431 | XFS_FSB_TO_DADDR(mp, bno), | |
1432 | (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp); | |
1433 | if (error) | |
1434 | break; | |
1435 | ||
5b139738 | 1436 | xfs_qm_reset_dqcounts(mp, bp, firstid, type); |
1da177e4 LT |
1437 | xfs_bdwrite(mp, bp); |
1438 | /* | |
1439 | * goto the next block. | |
1440 | */ | |
1441 | bno++; | |
1442 | firstid += XFS_QM_DQPERBLK(mp); | |
1443 | } | |
014c2544 | 1444 | return error; |
1da177e4 LT |
1445 | } |
1446 | ||
1447 | /* | |
c8ad20ff | 1448 | * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a |
1da177e4 LT |
1449 | * caller supplied function for every chunk of dquots that we find. |
1450 | */ | |
1451 | STATIC int | |
1452 | xfs_qm_dqiterate( | |
1453 | xfs_mount_t *mp, | |
1454 | xfs_inode_t *qip, | |
1455 | uint flags) | |
1456 | { | |
1457 | xfs_bmbt_irec_t *map; | |
1458 | int i, nmaps; /* number of map entries */ | |
1459 | int error; /* return value */ | |
1460 | xfs_fileoff_t lblkno; | |
1461 | xfs_filblks_t maxlblkcnt; | |
1462 | xfs_dqid_t firstid; | |
1463 | xfs_fsblock_t rablkno; | |
1464 | xfs_filblks_t rablkcnt; | |
1465 | ||
1466 | error = 0; | |
1467 | /* | |
c41564b5 | 1468 | * This looks racy, but we can't keep an inode lock across a |
1da177e4 LT |
1469 | * trans_reserve. But, this gets called during quotacheck, and that |
1470 | * happens only at mount time which is single threaded. | |
1471 | */ | |
1472 | if (qip->i_d.di_nblocks == 0) | |
014c2544 | 1473 | return 0; |
1da177e4 LT |
1474 | |
1475 | map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP); | |
1476 | ||
1477 | lblkno = 0; | |
1478 | maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); | |
1479 | do { | |
1480 | nmaps = XFS_DQITER_MAP_SIZE; | |
1481 | /* | |
1482 | * We aren't changing the inode itself. Just changing | |
1483 | * some of its data. No new blocks are added here, and | |
1484 | * the inode is never added to the transaction. | |
1485 | */ | |
1486 | xfs_ilock(qip, XFS_ILOCK_SHARED); | |
1487 | error = xfs_bmapi(NULL, qip, lblkno, | |
1488 | maxlblkcnt - lblkno, | |
1489 | XFS_BMAPI_METADATA, | |
1490 | NULL, | |
3e57ecf6 | 1491 | 0, map, &nmaps, NULL, NULL); |
1da177e4 LT |
1492 | xfs_iunlock(qip, XFS_ILOCK_SHARED); |
1493 | if (error) | |
1494 | break; | |
1495 | ||
1496 | ASSERT(nmaps <= XFS_DQITER_MAP_SIZE); | |
1497 | for (i = 0; i < nmaps; i++) { | |
1498 | ASSERT(map[i].br_startblock != DELAYSTARTBLOCK); | |
1499 | ASSERT(map[i].br_blockcount); | |
1500 | ||
1501 | ||
1502 | lblkno += map[i].br_blockcount; | |
1503 | ||
1504 | if (map[i].br_startblock == HOLESTARTBLOCK) | |
1505 | continue; | |
1506 | ||
1507 | firstid = (xfs_dqid_t) map[i].br_startoff * | |
1508 | XFS_QM_DQPERBLK(mp); | |
1509 | /* | |
1510 | * Do a read-ahead on the next extent. | |
1511 | */ | |
1512 | if ((i+1 < nmaps) && | |
1513 | (map[i+1].br_startblock != HOLESTARTBLOCK)) { | |
1514 | rablkcnt = map[i+1].br_blockcount; | |
1515 | rablkno = map[i+1].br_startblock; | |
1516 | while (rablkcnt--) { | |
1517 | xfs_baread(mp->m_ddev_targp, | |
1518 | XFS_FSB_TO_DADDR(mp, rablkno), | |
1519 | (int)XFS_QI_DQCHUNKLEN(mp)); | |
1520 | rablkno++; | |
1521 | } | |
1522 | } | |
1523 | /* | |
1524 | * Iterate thru all the blks in the extent and | |
1525 | * reset the counters of all the dquots inside them. | |
1526 | */ | |
1527 | if ((error = xfs_qm_dqiter_bufs(mp, | |
1528 | firstid, | |
1529 | map[i].br_startblock, | |
1530 | map[i].br_blockcount, | |
1531 | flags))) { | |
1532 | break; | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | if (error) | |
1537 | break; | |
1538 | } while (nmaps > 0); | |
1539 | ||
f0e2d93c | 1540 | kmem_free(map); |
1da177e4 | 1541 | |
014c2544 | 1542 | return error; |
1da177e4 LT |
1543 | } |
1544 | ||
1545 | /* | |
1546 | * Called by dqusage_adjust in doing a quotacheck. | |
1547 | * Given the inode, and a dquot (either USR or GRP, doesn't matter), | |
1548 | * this updates its incore copy as well as the buffer copy. This is | |
1549 | * so that once the quotacheck is done, we can just log all the buffers, | |
1550 | * as opposed to logging numerous updates to individual dquots. | |
1551 | */ | |
1552 | STATIC void | |
1553 | xfs_qm_quotacheck_dqadjust( | |
1554 | xfs_dquot_t *dqp, | |
1555 | xfs_qcnt_t nblks, | |
1556 | xfs_qcnt_t rtblks) | |
1557 | { | |
1558 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); | |
1559 | xfs_dqtrace_entry(dqp, "QCHECK DQADJUST"); | |
1560 | /* | |
1561 | * Adjust the inode count and the block count to reflect this inode's | |
1562 | * resource usage. | |
1563 | */ | |
413d57c9 | 1564 | be64_add_cpu(&dqp->q_core.d_icount, 1); |
1da177e4 LT |
1565 | dqp->q_res_icount++; |
1566 | if (nblks) { | |
413d57c9 | 1567 | be64_add_cpu(&dqp->q_core.d_bcount, nblks); |
1da177e4 LT |
1568 | dqp->q_res_bcount += nblks; |
1569 | } | |
1570 | if (rtblks) { | |
413d57c9 | 1571 | be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks); |
1da177e4 LT |
1572 | dqp->q_res_rtbcount += rtblks; |
1573 | } | |
1574 | ||
1575 | /* | |
1576 | * Set default limits, adjust timers (since we changed usages) | |
1577 | */ | |
1578 | if (! XFS_IS_SUSER_DQUOT(dqp)) { | |
1579 | xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core); | |
1580 | xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core); | |
1581 | } | |
1582 | ||
1583 | dqp->dq_flags |= XFS_DQ_DIRTY; | |
1584 | } | |
1585 | ||
1586 | STATIC int | |
1587 | xfs_qm_get_rtblks( | |
1588 | xfs_inode_t *ip, | |
1589 | xfs_qcnt_t *O_rtblks) | |
1590 | { | |
1591 | xfs_filblks_t rtblks; /* total rt blks */ | |
4eea22f0 | 1592 | xfs_extnum_t idx; /* extent record index */ |
1da177e4 LT |
1593 | xfs_ifork_t *ifp; /* inode fork pointer */ |
1594 | xfs_extnum_t nextents; /* number of extent entries */ | |
1da177e4 LT |
1595 | int error; |
1596 | ||
1597 | ASSERT(XFS_IS_REALTIME_INODE(ip)); | |
1598 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | |
1599 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
1600 | if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK))) | |
014c2544 | 1601 | return error; |
1da177e4 LT |
1602 | } |
1603 | rtblks = 0; | |
4eea22f0 | 1604 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
a6f64d4a CH |
1605 | for (idx = 0; idx < nextents; idx++) |
1606 | rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx)); | |
1da177e4 | 1607 | *O_rtblks = (xfs_qcnt_t)rtblks; |
014c2544 | 1608 | return 0; |
1da177e4 LT |
1609 | } |
1610 | ||
1611 | /* | |
1612 | * callback routine supplied to bulkstat(). Given an inumber, find its | |
1613 | * dquots and update them to account for resources taken by that inode. | |
1614 | */ | |
1615 | /* ARGSUSED */ | |
1616 | STATIC int | |
1617 | xfs_qm_dqusage_adjust( | |
1618 | xfs_mount_t *mp, /* mount point for filesystem */ | |
1619 | xfs_ino_t ino, /* inode number to get data for */ | |
1620 | void __user *buffer, /* not used */ | |
1621 | int ubsize, /* not used */ | |
1622 | void *private_data, /* not used */ | |
1623 | xfs_daddr_t bno, /* starting block of inode cluster */ | |
1624 | int *ubused, /* not used */ | |
1625 | void *dip, /* on-disk inode pointer (not used) */ | |
1626 | int *res) /* result code value */ | |
1627 | { | |
1628 | xfs_inode_t *ip; | |
1629 | xfs_dquot_t *udqp, *gdqp; | |
1630 | xfs_qcnt_t nblks, rtblks; | |
1631 | int error; | |
1632 | ||
1633 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); | |
1634 | ||
1635 | /* | |
1636 | * rootino must have its resources accounted for, not so with the quota | |
1637 | * inodes. | |
1638 | */ | |
1639 | if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) { | |
1640 | *res = BULKSTAT_RV_NOTHING; | |
1641 | return XFS_ERROR(EINVAL); | |
1642 | } | |
1643 | ||
1644 | /* | |
1645 | * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget | |
1646 | * interface expects the inode to be exclusively locked because that's | |
1647 | * the case in all other instances. It's OK that we do this because | |
1648 | * quotacheck is done only at mount time. | |
1649 | */ | |
1650 | if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) { | |
1651 | *res = BULKSTAT_RV_NOTHING; | |
014c2544 | 1652 | return error; |
1da177e4 LT |
1653 | } |
1654 | ||
1da177e4 LT |
1655 | /* |
1656 | * Obtain the locked dquots. In case of an error (eg. allocation | |
1657 | * fails for ENOSPC), we return the negative of the error number | |
1658 | * to bulkstat, so that it can get propagated to quotacheck() and | |
1659 | * making us disable quotas for the file system. | |
1660 | */ | |
1661 | if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) { | |
1662 | xfs_iput(ip, XFS_ILOCK_EXCL); | |
1663 | *res = BULKSTAT_RV_GIVEUP; | |
014c2544 | 1664 | return error; |
1da177e4 LT |
1665 | } |
1666 | ||
1667 | rtblks = 0; | |
1668 | if (! XFS_IS_REALTIME_INODE(ip)) { | |
1669 | nblks = (xfs_qcnt_t)ip->i_d.di_nblocks; | |
1670 | } else { | |
1671 | /* | |
1672 | * Walk thru the extent list and count the realtime blocks. | |
1673 | */ | |
1674 | if ((error = xfs_qm_get_rtblks(ip, &rtblks))) { | |
1675 | xfs_iput(ip, XFS_ILOCK_EXCL); | |
1676 | if (udqp) | |
1677 | xfs_qm_dqput(udqp); | |
1678 | if (gdqp) | |
1679 | xfs_qm_dqput(gdqp); | |
1680 | *res = BULKSTAT_RV_GIVEUP; | |
014c2544 | 1681 | return error; |
1da177e4 LT |
1682 | } |
1683 | nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks; | |
1684 | } | |
1685 | ASSERT(ip->i_delayed_blks == 0); | |
1686 | ||
1687 | /* | |
1688 | * We can't release the inode while holding its dquot locks. | |
1689 | * The inode can go into inactive and might try to acquire the dquotlocks. | |
1690 | * So, just unlock here and do a vn_rele at the end. | |
1691 | */ | |
1692 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1693 | ||
1694 | /* | |
1695 | * Add the (disk blocks and inode) resources occupied by this | |
1696 | * inode to its dquots. We do this adjustment in the incore dquot, | |
1697 | * and also copy the changes to its buffer. | |
1698 | * We don't care about putting these changes in a transaction | |
1699 | * envelope because if we crash in the middle of a 'quotacheck' | |
1700 | * we have to start from the beginning anyway. | |
1701 | * Once we're done, we'll log all the dquot bufs. | |
1702 | * | |
c41564b5 | 1703 | * The *QUOTA_ON checks below may look pretty racy, but quotachecks |
1da177e4 LT |
1704 | * and quotaoffs don't race. (Quotachecks happen at mount time only). |
1705 | */ | |
1706 | if (XFS_IS_UQUOTA_ON(mp)) { | |
1707 | ASSERT(udqp); | |
1708 | xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks); | |
1709 | xfs_qm_dqput(udqp); | |
1710 | } | |
c8ad20ff | 1711 | if (XFS_IS_OQUOTA_ON(mp)) { |
1da177e4 LT |
1712 | ASSERT(gdqp); |
1713 | xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks); | |
1714 | xfs_qm_dqput(gdqp); | |
1715 | } | |
1716 | /* | |
1717 | * Now release the inode. This will send it to 'inactive', and | |
1718 | * possibly even free blocks. | |
1719 | */ | |
43355099 | 1720 | IRELE(ip); |
1da177e4 LT |
1721 | |
1722 | /* | |
1723 | * Goto next inode. | |
1724 | */ | |
1725 | *res = BULKSTAT_RV_DIDONE; | |
014c2544 | 1726 | return 0; |
1da177e4 LT |
1727 | } |
1728 | ||
1729 | /* | |
1730 | * Walk thru all the filesystem inodes and construct a consistent view | |
1731 | * of the disk quota world. If the quotacheck fails, disable quotas. | |
1732 | */ | |
1733 | int | |
1734 | xfs_qm_quotacheck( | |
1735 | xfs_mount_t *mp) | |
1736 | { | |
1737 | int done, count, error; | |
1738 | xfs_ino_t lastino; | |
1739 | size_t structsz; | |
1740 | xfs_inode_t *uip, *gip; | |
1741 | uint flags; | |
1742 | ||
1743 | count = INT_MAX; | |
1744 | structsz = 1; | |
1745 | lastino = 0; | |
1746 | flags = 0; | |
1747 | ||
1748 | ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp)); | |
1749 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); | |
1750 | ||
1751 | /* | |
1752 | * There should be no cached dquots. The (simplistic) quotacheck | |
1753 | * algorithm doesn't like that. | |
1754 | */ | |
1755 | ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0); | |
1756 | ||
1757 | cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname); | |
1758 | ||
1759 | /* | |
c8ad20ff | 1760 | * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset |
1da177e4 LT |
1761 | * their counters to zero. We need a clean slate. |
1762 | * We don't log our changes till later. | |
1763 | */ | |
1764 | if ((uip = XFS_QI_UQIP(mp))) { | |
1765 | if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA))) | |
1766 | goto error_return; | |
1767 | flags |= XFS_UQUOTA_CHKD; | |
1768 | } | |
1769 | ||
1770 | if ((gip = XFS_QI_GQIP(mp))) { | |
c8ad20ff NS |
1771 | if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ? |
1772 | XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA))) | |
1da177e4 | 1773 | goto error_return; |
c8ad20ff | 1774 | flags |= XFS_OQUOTA_CHKD; |
1da177e4 LT |
1775 | } |
1776 | ||
1777 | do { | |
1778 | /* | |
1779 | * Iterate thru all the inodes in the file system, | |
1780 | * adjusting the corresponding dquot counters in core. | |
1781 | */ | |
1782 | if ((error = xfs_bulkstat(mp, &lastino, &count, | |
1783 | xfs_qm_dqusage_adjust, NULL, | |
9c48876a | 1784 | structsz, NULL, BULKSTAT_FG_IGET, &done))) |
1da177e4 LT |
1785 | break; |
1786 | ||
1787 | } while (! done); | |
1788 | ||
4b8879df DC |
1789 | /* |
1790 | * We've made all the changes that we need to make incore. | |
1791 | * Flush them down to disk buffers if everything was updated | |
1792 | * successfully. | |
1793 | */ | |
1794 | if (!error) | |
1795 | error = xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI); | |
1796 | ||
1da177e4 LT |
1797 | /* |
1798 | * We can get this error if we couldn't do a dquot allocation inside | |
1799 | * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the | |
1800 | * dirty dquots that might be cached, we just want to get rid of them | |
1801 | * and turn quotaoff. The dquots won't be attached to any of the inodes | |
1802 | * at this point (because we intentionally didn't in dqget_noattach). | |
1803 | */ | |
1804 | if (error) { | |
ee2a4f7c | 1805 | xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF); |
1da177e4 LT |
1806 | goto error_return; |
1807 | } | |
1da177e4 LT |
1808 | |
1809 | /* | |
1810 | * We didn't log anything, because if we crashed, we'll have to | |
1811 | * start the quotacheck from scratch anyway. However, we must make | |
1812 | * sure that our dquot changes are secure before we put the | |
1813 | * quotacheck'd stamp on the superblock. So, here we do a synchronous | |
1814 | * flush. | |
1815 | */ | |
1816 | XFS_bflush(mp->m_ddev_targp); | |
1817 | ||
1818 | /* | |
1819 | * If one type of quotas is off, then it will lose its | |
1820 | * quotachecked status, since we won't be doing accounting for | |
1821 | * that type anymore. | |
1822 | */ | |
c8ad20ff | 1823 | mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD); |
1da177e4 LT |
1824 | mp->m_qflags |= flags; |
1825 | ||
1826 | XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++"); | |
1827 | ||
1828 | error_return: | |
1829 | if (error) { | |
1830 | cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): " | |
1831 | "Disabling quotas.", | |
1832 | mp->m_fsname, error); | |
1833 | /* | |
1834 | * We must turn off quotas. | |
1835 | */ | |
1836 | ASSERT(mp->m_quotainfo != NULL); | |
1837 | ASSERT(xfs_Gqm != NULL); | |
1838 | xfs_qm_destroy_quotainfo(mp); | |
31d5577b DC |
1839 | if (xfs_mount_reset_sbqflags(mp)) { |
1840 | cmn_err(CE_WARN, "XFS quotacheck %s: " | |
1841 | "Failed to reset quota flags.", mp->m_fsname); | |
1842 | } | |
1da177e4 LT |
1843 | } else { |
1844 | cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname); | |
1845 | } | |
1846 | return (error); | |
1847 | } | |
1848 | ||
1849 | /* | |
1850 | * This is called after the superblock has been read in and we're ready to | |
1851 | * iget the quota inodes. | |
1852 | */ | |
1853 | STATIC int | |
1854 | xfs_qm_init_quotainos( | |
1855 | xfs_mount_t *mp) | |
1856 | { | |
1857 | xfs_inode_t *uip, *gip; | |
1858 | int error; | |
1859 | __int64_t sbflags; | |
1860 | uint flags; | |
1861 | ||
1862 | ASSERT(mp->m_quotainfo); | |
1863 | uip = gip = NULL; | |
1864 | sbflags = 0; | |
1865 | flags = 0; | |
1866 | ||
1867 | /* | |
1868 | * Get the uquota and gquota inodes | |
1869 | */ | |
62118709 | 1870 | if (xfs_sb_version_hasquota(&mp->m_sb)) { |
1da177e4 LT |
1871 | if (XFS_IS_UQUOTA_ON(mp) && |
1872 | mp->m_sb.sb_uquotino != NULLFSINO) { | |
1873 | ASSERT(mp->m_sb.sb_uquotino > 0); | |
1874 | if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, | |
1875 | 0, 0, &uip, 0))) | |
1876 | return XFS_ERROR(error); | |
1877 | } | |
c8ad20ff | 1878 | if (XFS_IS_OQUOTA_ON(mp) && |
1da177e4 LT |
1879 | mp->m_sb.sb_gquotino != NULLFSINO) { |
1880 | ASSERT(mp->m_sb.sb_gquotino > 0); | |
1881 | if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, | |
1882 | 0, 0, &gip, 0))) { | |
1883 | if (uip) | |
43355099 | 1884 | IRELE(uip); |
1da177e4 LT |
1885 | return XFS_ERROR(error); |
1886 | } | |
1887 | } | |
1888 | } else { | |
1889 | flags |= XFS_QMOPT_SBVERSION; | |
1890 | sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO | | |
1891 | XFS_SB_GQUOTINO | XFS_SB_QFLAGS); | |
1892 | } | |
1893 | ||
1894 | /* | |
1895 | * Create the two inodes, if they don't exist already. The changes | |
1896 | * made above will get added to a transaction and logged in one of | |
1897 | * the qino_alloc calls below. If the device is readonly, | |
1898 | * temporarily switch to read-write to do this. | |
1899 | */ | |
1900 | if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) { | |
1901 | if ((error = xfs_qm_qino_alloc(mp, &uip, | |
1902 | sbflags | XFS_SB_UQUOTINO, | |
1903 | flags | XFS_QMOPT_UQUOTA))) | |
1904 | return XFS_ERROR(error); | |
1905 | ||
1906 | flags &= ~XFS_QMOPT_SBVERSION; | |
1907 | } | |
c8ad20ff NS |
1908 | if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) { |
1909 | flags |= (XFS_IS_GQUOTA_ON(mp) ? | |
1910 | XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA); | |
1911 | error = xfs_qm_qino_alloc(mp, &gip, | |
1912 | sbflags | XFS_SB_GQUOTINO, flags); | |
1913 | if (error) { | |
1da177e4 | 1914 | if (uip) |
43355099 | 1915 | IRELE(uip); |
1da177e4 LT |
1916 | |
1917 | return XFS_ERROR(error); | |
1918 | } | |
1919 | } | |
1920 | ||
1921 | XFS_QI_UQIP(mp) = uip; | |
1922 | XFS_QI_GQIP(mp) = gip; | |
1923 | ||
014c2544 | 1924 | return 0; |
1da177e4 LT |
1925 | } |
1926 | ||
1927 | ||
1928 | /* | |
1929 | * Traverse the freelist of dquots and attempt to reclaim a maximum of | |
1930 | * 'howmany' dquots. This operation races with dqlookup(), and attempts to | |
1931 | * favor the lookup function ... | |
1932 | * XXXsup merge this with qm_reclaim_one(). | |
1933 | */ | |
1934 | STATIC int | |
1935 | xfs_qm_shake_freelist( | |
1936 | int howmany) | |
1937 | { | |
1938 | int nreclaimed; | |
1939 | xfs_dqhash_t *hash; | |
1940 | xfs_dquot_t *dqp, *nextdqp; | |
1941 | int restarts; | |
1942 | int nflushes; | |
1943 | ||
1944 | if (howmany <= 0) | |
014c2544 | 1945 | return 0; |
1da177e4 LT |
1946 | |
1947 | nreclaimed = 0; | |
1948 | restarts = 0; | |
1949 | nflushes = 0; | |
1950 | ||
1951 | #ifdef QUOTADEBUG | |
1952 | cmn_err(CE_DEBUG, "Shake free 0x%x", howmany); | |
1953 | #endif | |
1954 | /* lock order is : hashchainlock, freelistlock, mplistlock */ | |
1955 | tryagain: | |
1956 | xfs_qm_freelist_lock(xfs_Gqm); | |
1957 | ||
1958 | for (dqp = xfs_Gqm->qm_dqfreelist.qh_next; | |
1959 | ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) && | |
1960 | nreclaimed < howmany); ) { | |
1961 | xfs_dqlock(dqp); | |
1962 | ||
1963 | /* | |
1964 | * We are racing with dqlookup here. Naturally we don't | |
1965 | * want to reclaim a dquot that lookup wants. | |
1966 | */ | |
1967 | if (dqp->dq_flags & XFS_DQ_WANT) { | |
1968 | xfs_dqunlock(dqp); | |
1969 | xfs_qm_freelist_unlock(xfs_Gqm); | |
1970 | if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS) | |
014c2544 | 1971 | return nreclaimed; |
1da177e4 LT |
1972 | XQM_STATS_INC(xqmstats.xs_qm_dqwants); |
1973 | goto tryagain; | |
1974 | } | |
1975 | ||
1976 | /* | |
1977 | * If the dquot is inactive, we are assured that it is | |
1978 | * not on the mplist or the hashlist, and that makes our | |
1979 | * life easier. | |
1980 | */ | |
1981 | if (dqp->dq_flags & XFS_DQ_INACTIVE) { | |
1982 | ASSERT(dqp->q_mount == NULL); | |
1983 | ASSERT(! XFS_DQ_IS_DIRTY(dqp)); | |
1984 | ASSERT(dqp->HL_PREVP == NULL); | |
1985 | ASSERT(dqp->MPL_PREVP == NULL); | |
1986 | XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims); | |
1987 | nextdqp = dqp->dq_flnext; | |
1988 | goto off_freelist; | |
1989 | } | |
1990 | ||
1991 | ASSERT(dqp->MPL_PREVP); | |
1992 | /* | |
1993 | * Try to grab the flush lock. If this dquot is in the process of | |
1994 | * getting flushed to disk, we don't want to reclaim it. | |
1995 | */ | |
e1f49cf2 | 1996 | if (!xfs_dqflock_nowait(dqp)) { |
1da177e4 LT |
1997 | xfs_dqunlock(dqp); |
1998 | dqp = dqp->dq_flnext; | |
1999 | continue; | |
2000 | } | |
2001 | ||
2002 | /* | |
2003 | * We have the flush lock so we know that this is not in the | |
2004 | * process of being flushed. So, if this is dirty, flush it | |
2005 | * DELWRI so that we don't get a freelist infested with | |
2006 | * dirty dquots. | |
2007 | */ | |
2008 | if (XFS_DQ_IS_DIRTY(dqp)) { | |
3c56836f | 2009 | int error; |
1da177e4 LT |
2010 | xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY"); |
2011 | /* | |
2012 | * We flush it delayed write, so don't bother | |
2013 | * releasing the mplock. | |
2014 | */ | |
3c56836f DC |
2015 | error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI); |
2016 | if (error) { | |
2017 | xfs_fs_cmn_err(CE_WARN, dqp->q_mount, | |
2018 | "xfs_qm_dqflush_all: dquot %p flush failed", dqp); | |
2019 | } | |
1da177e4 LT |
2020 | xfs_dqunlock(dqp); /* dqflush unlocks dqflock */ |
2021 | dqp = dqp->dq_flnext; | |
2022 | continue; | |
2023 | } | |
2024 | /* | |
2025 | * We're trying to get the hashlock out of order. This races | |
2026 | * with dqlookup; so, we giveup and goto the next dquot if | |
2027 | * we couldn't get the hashlock. This way, we won't starve | |
2028 | * a dqlookup process that holds the hashlock that is | |
2029 | * waiting for the freelist lock. | |
2030 | */ | |
2031 | if (! xfs_qm_dqhashlock_nowait(dqp)) { | |
2032 | xfs_dqfunlock(dqp); | |
2033 | xfs_dqunlock(dqp); | |
2034 | dqp = dqp->dq_flnext; | |
2035 | continue; | |
2036 | } | |
2037 | /* | |
2038 | * This races with dquot allocation code as well as dqflush_all | |
2039 | * and reclaim code. So, if we failed to grab the mplist lock, | |
2040 | * giveup everything and start over. | |
2041 | */ | |
2042 | hash = dqp->q_hash; | |
2043 | ASSERT(hash); | |
2044 | if (! xfs_qm_mplist_nowait(dqp->q_mount)) { | |
2045 | /* XXX put a sentinel so that we can come back here */ | |
2046 | xfs_dqfunlock(dqp); | |
2047 | xfs_dqunlock(dqp); | |
2048 | XFS_DQ_HASH_UNLOCK(hash); | |
2049 | xfs_qm_freelist_unlock(xfs_Gqm); | |
2050 | if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS) | |
014c2544 | 2051 | return nreclaimed; |
1da177e4 LT |
2052 | goto tryagain; |
2053 | } | |
2054 | xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING"); | |
2055 | #ifdef QUOTADEBUG | |
2056 | cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n", | |
1149d96a | 2057 | dqp, be32_to_cpu(dqp->q_core.d_id)); |
1da177e4 LT |
2058 | #endif |
2059 | ASSERT(dqp->q_nrefs == 0); | |
2060 | nextdqp = dqp->dq_flnext; | |
2061 | XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp); | |
2062 | XQM_HASHLIST_REMOVE(hash, dqp); | |
2063 | xfs_dqfunlock(dqp); | |
2064 | xfs_qm_mplist_unlock(dqp->q_mount); | |
2065 | XFS_DQ_HASH_UNLOCK(hash); | |
2066 | ||
2067 | off_freelist: | |
2068 | XQM_FREELIST_REMOVE(dqp); | |
2069 | xfs_dqunlock(dqp); | |
2070 | nreclaimed++; | |
2071 | XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims); | |
2072 | xfs_qm_dqdestroy(dqp); | |
2073 | dqp = nextdqp; | |
2074 | } | |
2075 | xfs_qm_freelist_unlock(xfs_Gqm); | |
014c2544 | 2076 | return nreclaimed; |
1da177e4 LT |
2077 | } |
2078 | ||
2079 | ||
2080 | /* | |
2081 | * The kmem_shake interface is invoked when memory is running low. | |
2082 | */ | |
2083 | /* ARGSUSED */ | |
2084 | STATIC int | |
51bfb75b | 2085 | xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask) |
1da177e4 LT |
2086 | { |
2087 | int ndqused, nfree, n; | |
2088 | ||
2089 | if (!kmem_shake_allow(gfp_mask)) | |
014c2544 | 2090 | return 0; |
1da177e4 | 2091 | if (!xfs_Gqm) |
014c2544 | 2092 | return 0; |
1da177e4 LT |
2093 | |
2094 | nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */ | |
2095 | /* incore dquots in all f/s's */ | |
2096 | ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree; | |
2097 | ||
2098 | ASSERT(ndqused >= 0); | |
2099 | ||
2100 | if (nfree <= ndqused && nfree < ndquot) | |
014c2544 | 2101 | return 0; |
1da177e4 LT |
2102 | |
2103 | ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */ | |
2104 | n = nfree - ndqused - ndquot; /* # over target */ | |
2105 | ||
2106 | return xfs_qm_shake_freelist(MAX(nfree, n)); | |
2107 | } | |
2108 | ||
2109 | ||
2110 | /* | |
2111 | * Just pop the least recently used dquot off the freelist and | |
2112 | * recycle it. The returned dquot is locked. | |
2113 | */ | |
2114 | STATIC xfs_dquot_t * | |
2115 | xfs_qm_dqreclaim_one(void) | |
2116 | { | |
2117 | xfs_dquot_t *dqpout; | |
2118 | xfs_dquot_t *dqp; | |
2119 | int restarts; | |
2120 | int nflushes; | |
2121 | ||
2122 | restarts = 0; | |
2123 | dqpout = NULL; | |
2124 | nflushes = 0; | |
2125 | ||
2126 | /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */ | |
2127 | startagain: | |
2128 | xfs_qm_freelist_lock(xfs_Gqm); | |
2129 | ||
2130 | FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) { | |
2131 | xfs_dqlock(dqp); | |
2132 | ||
2133 | /* | |
2134 | * We are racing with dqlookup here. Naturally we don't | |
2135 | * want to reclaim a dquot that lookup wants. We release the | |
2136 | * freelist lock and start over, so that lookup will grab | |
2137 | * both the dquot and the freelistlock. | |
2138 | */ | |
2139 | if (dqp->dq_flags & XFS_DQ_WANT) { | |
2140 | ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE)); | |
2141 | xfs_dqtrace_entry(dqp, "DQRECLAIM: DQWANT"); | |
2142 | xfs_dqunlock(dqp); | |
2143 | xfs_qm_freelist_unlock(xfs_Gqm); | |
2144 | if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS) | |
014c2544 | 2145 | return NULL; |
1da177e4 LT |
2146 | XQM_STATS_INC(xqmstats.xs_qm_dqwants); |
2147 | goto startagain; | |
2148 | } | |
2149 | ||
2150 | /* | |
2151 | * If the dquot is inactive, we are assured that it is | |
2152 | * not on the mplist or the hashlist, and that makes our | |
2153 | * life easier. | |
2154 | */ | |
2155 | if (dqp->dq_flags & XFS_DQ_INACTIVE) { | |
2156 | ASSERT(dqp->q_mount == NULL); | |
2157 | ASSERT(! XFS_DQ_IS_DIRTY(dqp)); | |
2158 | ASSERT(dqp->HL_PREVP == NULL); | |
2159 | ASSERT(dqp->MPL_PREVP == NULL); | |
2160 | XQM_FREELIST_REMOVE(dqp); | |
2161 | xfs_dqunlock(dqp); | |
2162 | dqpout = dqp; | |
2163 | XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims); | |
2164 | break; | |
2165 | } | |
2166 | ||
2167 | ASSERT(dqp->q_hash); | |
2168 | ASSERT(dqp->MPL_PREVP); | |
2169 | ||
2170 | /* | |
2171 | * Try to grab the flush lock. If this dquot is in the process of | |
2172 | * getting flushed to disk, we don't want to reclaim it. | |
2173 | */ | |
e1f49cf2 | 2174 | if (!xfs_dqflock_nowait(dqp)) { |
1da177e4 LT |
2175 | xfs_dqunlock(dqp); |
2176 | continue; | |
2177 | } | |
2178 | ||
2179 | /* | |
2180 | * We have the flush lock so we know that this is not in the | |
2181 | * process of being flushed. So, if this is dirty, flush it | |
2182 | * DELWRI so that we don't get a freelist infested with | |
2183 | * dirty dquots. | |
2184 | */ | |
2185 | if (XFS_DQ_IS_DIRTY(dqp)) { | |
3c56836f | 2186 | int error; |
1da177e4 LT |
2187 | xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY"); |
2188 | /* | |
2189 | * We flush it delayed write, so don't bother | |
2190 | * releasing the freelist lock. | |
2191 | */ | |
3c56836f DC |
2192 | error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI); |
2193 | if (error) { | |
2194 | xfs_fs_cmn_err(CE_WARN, dqp->q_mount, | |
2195 | "xfs_qm_dqreclaim: dquot %p flush failed", dqp); | |
2196 | } | |
1da177e4 LT |
2197 | xfs_dqunlock(dqp); /* dqflush unlocks dqflock */ |
2198 | continue; | |
2199 | } | |
2200 | ||
2201 | if (! xfs_qm_mplist_nowait(dqp->q_mount)) { | |
2202 | xfs_dqfunlock(dqp); | |
2203 | xfs_dqunlock(dqp); | |
2204 | continue; | |
2205 | } | |
2206 | ||
2207 | if (! xfs_qm_dqhashlock_nowait(dqp)) | |
2208 | goto mplistunlock; | |
2209 | ||
2210 | ASSERT(dqp->q_nrefs == 0); | |
2211 | xfs_dqtrace_entry(dqp, "DQRECLAIM: UNLINKING"); | |
2212 | XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp); | |
2213 | XQM_HASHLIST_REMOVE(dqp->q_hash, dqp); | |
2214 | XQM_FREELIST_REMOVE(dqp); | |
2215 | dqpout = dqp; | |
2216 | XFS_DQ_HASH_UNLOCK(dqp->q_hash); | |
2217 | mplistunlock: | |
2218 | xfs_qm_mplist_unlock(dqp->q_mount); | |
2219 | xfs_dqfunlock(dqp); | |
2220 | xfs_dqunlock(dqp); | |
2221 | if (dqpout) | |
2222 | break; | |
2223 | } | |
2224 | ||
2225 | xfs_qm_freelist_unlock(xfs_Gqm); | |
014c2544 | 2226 | return dqpout; |
1da177e4 LT |
2227 | } |
2228 | ||
2229 | ||
2230 | /*------------------------------------------------------------------*/ | |
2231 | ||
2232 | /* | |
2233 | * Return a new incore dquot. Depending on the number of | |
2234 | * dquots in the system, we either allocate a new one on the kernel heap, | |
2235 | * or reclaim a free one. | |
2236 | * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed | |
2237 | * to reclaim an existing one from the freelist. | |
2238 | */ | |
2239 | boolean_t | |
2240 | xfs_qm_dqalloc_incore( | |
2241 | xfs_dquot_t **O_dqpp) | |
2242 | { | |
2243 | xfs_dquot_t *dqp; | |
2244 | ||
2245 | /* | |
2246 | * Check against high water mark to see if we want to pop | |
2247 | * a nincompoop dquot off the freelist. | |
2248 | */ | |
2249 | if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) { | |
2250 | /* | |
2251 | * Try to recycle a dquot from the freelist. | |
2252 | */ | |
2253 | if ((dqp = xfs_qm_dqreclaim_one())) { | |
2254 | XQM_STATS_INC(xqmstats.xs_qm_dqreclaims); | |
2255 | /* | |
2256 | * Just zero the core here. The rest will get | |
2257 | * reinitialized by caller. XXX we shouldn't even | |
2258 | * do this zero ... | |
2259 | */ | |
2260 | memset(&dqp->q_core, 0, sizeof(dqp->q_core)); | |
2261 | *O_dqpp = dqp; | |
014c2544 | 2262 | return B_FALSE; |
1da177e4 LT |
2263 | } |
2264 | XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses); | |
2265 | } | |
2266 | ||
2267 | /* | |
2268 | * Allocate a brand new dquot on the kernel heap and return it | |
2269 | * to the caller to initialize. | |
2270 | */ | |
2271 | ASSERT(xfs_Gqm->qm_dqzone != NULL); | |
2272 | *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP); | |
2273 | atomic_inc(&xfs_Gqm->qm_totaldquots); | |
2274 | ||
014c2544 | 2275 | return B_TRUE; |
1da177e4 LT |
2276 | } |
2277 | ||
2278 | ||
2279 | /* | |
2280 | * Start a transaction and write the incore superblock changes to | |
2281 | * disk. flags parameter indicates which fields have changed. | |
2282 | */ | |
2283 | int | |
2284 | xfs_qm_write_sb_changes( | |
2285 | xfs_mount_t *mp, | |
2286 | __int64_t flags) | |
2287 | { | |
2288 | xfs_trans_t *tp; | |
2289 | int error; | |
2290 | ||
2291 | #ifdef QUOTADEBUG | |
2292 | cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname); | |
2293 | #endif | |
2294 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE); | |
2295 | if ((error = xfs_trans_reserve(tp, 0, | |
2296 | mp->m_sb.sb_sectsize + 128, 0, | |
2297 | 0, | |
2298 | XFS_DEFAULT_LOG_COUNT))) { | |
2299 | xfs_trans_cancel(tp, 0); | |
014c2544 | 2300 | return error; |
1da177e4 LT |
2301 | } |
2302 | ||
2303 | xfs_mod_sb(tp, flags); | |
e5720eec | 2304 | error = xfs_trans_commit(tp, 0); |
1da177e4 | 2305 | |
e5720eec | 2306 | return error; |
1da177e4 LT |
2307 | } |
2308 | ||
2309 | ||
2310 | /* --------------- utility functions for vnodeops ---------------- */ | |
2311 | ||
2312 | ||
2313 | /* | |
2314 | * Given an inode, a uid and gid (from cred_t) make sure that we have | |
2315 | * allocated relevant dquot(s) on disk, and that we won't exceed inode | |
2316 | * quotas by creating this file. | |
2317 | * This also attaches dquot(s) to the given inode after locking it, | |
2318 | * and returns the dquots corresponding to the uid and/or gid. | |
2319 | * | |
2320 | * in : inode (unlocked) | |
2321 | * out : udquot, gdquot with references taken and unlocked | |
2322 | */ | |
2323 | int | |
2324 | xfs_qm_vop_dqalloc( | |
2325 | xfs_mount_t *mp, | |
2326 | xfs_inode_t *ip, | |
2327 | uid_t uid, | |
2328 | gid_t gid, | |
c8ad20ff | 2329 | prid_t prid, |
1da177e4 LT |
2330 | uint flags, |
2331 | xfs_dquot_t **O_udqpp, | |
2332 | xfs_dquot_t **O_gdqpp) | |
2333 | { | |
2334 | int error; | |
2335 | xfs_dquot_t *uq, *gq; | |
2336 | uint lockflags; | |
2337 | ||
2338 | if (!XFS_IS_QUOTA_ON(mp)) | |
2339 | return 0; | |
2340 | ||
2341 | lockflags = XFS_ILOCK_EXCL; | |
2342 | xfs_ilock(ip, lockflags); | |
2343 | ||
bd186aa9 | 2344 | if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip)) |
1da177e4 LT |
2345 | gid = ip->i_d.di_gid; |
2346 | ||
2347 | /* | |
2348 | * Attach the dquot(s) to this inode, doing a dquot allocation | |
2349 | * if necessary. The dquot(s) will not be locked. | |
2350 | */ | |
2351 | if (XFS_NOT_DQATTACHED(mp, ip)) { | |
2352 | if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC | | |
2353 | XFS_QMOPT_ILOCKED))) { | |
2354 | xfs_iunlock(ip, lockflags); | |
014c2544 | 2355 | return error; |
1da177e4 LT |
2356 | } |
2357 | } | |
2358 | ||
2359 | uq = gq = NULL; | |
c8ad20ff | 2360 | if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) { |
1da177e4 LT |
2361 | if (ip->i_d.di_uid != uid) { |
2362 | /* | |
2363 | * What we need is the dquot that has this uid, and | |
2364 | * if we send the inode to dqget, the uid of the inode | |
2365 | * takes priority over what's sent in the uid argument. | |
2366 | * We must unlock inode here before calling dqget if | |
2367 | * we're not sending the inode, because otherwise | |
2368 | * we'll deadlock by doing trans_reserve while | |
2369 | * holding ilock. | |
2370 | */ | |
2371 | xfs_iunlock(ip, lockflags); | |
2372 | if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid, | |
2373 | XFS_DQ_USER, | |
2374 | XFS_QMOPT_DQALLOC | | |
2375 | XFS_QMOPT_DOWARN, | |
2376 | &uq))) { | |
2377 | ASSERT(error != ENOENT); | |
014c2544 | 2378 | return error; |
1da177e4 LT |
2379 | } |
2380 | /* | |
2381 | * Get the ilock in the right order. | |
2382 | */ | |
2383 | xfs_dqunlock(uq); | |
2384 | lockflags = XFS_ILOCK_SHARED; | |
2385 | xfs_ilock(ip, lockflags); | |
2386 | } else { | |
2387 | /* | |
2388 | * Take an extra reference, because we'll return | |
2389 | * this to caller | |
2390 | */ | |
2391 | ASSERT(ip->i_udquot); | |
2392 | uq = ip->i_udquot; | |
2393 | xfs_dqlock(uq); | |
2394 | XFS_DQHOLD(uq); | |
2395 | xfs_dqunlock(uq); | |
2396 | } | |
2397 | } | |
c8ad20ff | 2398 | if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) { |
1da177e4 LT |
2399 | if (ip->i_d.di_gid != gid) { |
2400 | xfs_iunlock(ip, lockflags); | |
2401 | if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid, | |
2402 | XFS_DQ_GROUP, | |
2403 | XFS_QMOPT_DQALLOC | | |
2404 | XFS_QMOPT_DOWARN, | |
2405 | &gq))) { | |
2406 | if (uq) | |
2407 | xfs_qm_dqrele(uq); | |
2408 | ASSERT(error != ENOENT); | |
014c2544 | 2409 | return error; |
1da177e4 LT |
2410 | } |
2411 | xfs_dqunlock(gq); | |
2412 | lockflags = XFS_ILOCK_SHARED; | |
2413 | xfs_ilock(ip, lockflags); | |
2414 | } else { | |
2415 | ASSERT(ip->i_gdquot); | |
2416 | gq = ip->i_gdquot; | |
2417 | xfs_dqlock(gq); | |
2418 | XFS_DQHOLD(gq); | |
2419 | xfs_dqunlock(gq); | |
2420 | } | |
c8ad20ff NS |
2421 | } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) { |
2422 | if (ip->i_d.di_projid != prid) { | |
2423 | xfs_iunlock(ip, lockflags); | |
2424 | if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid, | |
2425 | XFS_DQ_PROJ, | |
2426 | XFS_QMOPT_DQALLOC | | |
2427 | XFS_QMOPT_DOWARN, | |
2428 | &gq))) { | |
2429 | if (uq) | |
2430 | xfs_qm_dqrele(uq); | |
2431 | ASSERT(error != ENOENT); | |
2432 | return (error); | |
2433 | } | |
2434 | xfs_dqunlock(gq); | |
2435 | lockflags = XFS_ILOCK_SHARED; | |
2436 | xfs_ilock(ip, lockflags); | |
2437 | } else { | |
2438 | ASSERT(ip->i_gdquot); | |
2439 | gq = ip->i_gdquot; | |
2440 | xfs_dqlock(gq); | |
2441 | XFS_DQHOLD(gq); | |
2442 | xfs_dqunlock(gq); | |
2443 | } | |
1da177e4 LT |
2444 | } |
2445 | if (uq) | |
2446 | xfs_dqtrace_entry_ino(uq, "DQALLOC", ip); | |
2447 | ||
2448 | xfs_iunlock(ip, lockflags); | |
2449 | if (O_udqpp) | |
2450 | *O_udqpp = uq; | |
2451 | else if (uq) | |
2452 | xfs_qm_dqrele(uq); | |
2453 | if (O_gdqpp) | |
2454 | *O_gdqpp = gq; | |
2455 | else if (gq) | |
2456 | xfs_qm_dqrele(gq); | |
014c2544 | 2457 | return 0; |
1da177e4 LT |
2458 | } |
2459 | ||
2460 | /* | |
2461 | * Actually transfer ownership, and do dquot modifications. | |
2462 | * These were already reserved. | |
2463 | */ | |
2464 | xfs_dquot_t * | |
2465 | xfs_qm_vop_chown( | |
2466 | xfs_trans_t *tp, | |
2467 | xfs_inode_t *ip, | |
2468 | xfs_dquot_t **IO_olddq, | |
2469 | xfs_dquot_t *newdq) | |
2470 | { | |
2471 | xfs_dquot_t *prevdq; | |
06d10dd9 NS |
2472 | uint bfield = XFS_IS_REALTIME_INODE(ip) ? |
2473 | XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT; | |
2474 | ||
579aa9ca | 2475 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
2476 | ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount)); |
2477 | ||
2478 | /* old dquot */ | |
2479 | prevdq = *IO_olddq; | |
2480 | ASSERT(prevdq); | |
2481 | ASSERT(prevdq != newdq); | |
2482 | ||
06d10dd9 NS |
2483 | xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks)); |
2484 | xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1); | |
1da177e4 LT |
2485 | |
2486 | /* the sparkling new dquot */ | |
06d10dd9 NS |
2487 | xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks); |
2488 | xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1); | |
1da177e4 LT |
2489 | |
2490 | /* | |
2491 | * Take an extra reference, because the inode | |
2492 | * is going to keep this dquot pointer even | |
2493 | * after the trans_commit. | |
2494 | */ | |
2495 | xfs_dqlock(newdq); | |
2496 | XFS_DQHOLD(newdq); | |
2497 | xfs_dqunlock(newdq); | |
2498 | *IO_olddq = newdq; | |
2499 | ||
014c2544 | 2500 | return prevdq; |
1da177e4 LT |
2501 | } |
2502 | ||
2503 | /* | |
c8ad20ff | 2504 | * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID). |
1da177e4 LT |
2505 | */ |
2506 | int | |
2507 | xfs_qm_vop_chown_reserve( | |
2508 | xfs_trans_t *tp, | |
2509 | xfs_inode_t *ip, | |
2510 | xfs_dquot_t *udqp, | |
2511 | xfs_dquot_t *gdqp, | |
2512 | uint flags) | |
2513 | { | |
2514 | int error; | |
2515 | xfs_mount_t *mp; | |
9a2a7de2 | 2516 | uint delblks, blkflags, prjflags = 0; |
1da177e4 LT |
2517 | xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq; |
2518 | ||
579aa9ca | 2519 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
1da177e4 LT |
2520 | mp = ip->i_mount; |
2521 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); | |
2522 | ||
2523 | delblks = ip->i_delayed_blks; | |
2524 | delblksudq = delblksgdq = unresudq = unresgdq = NULL; | |
06d10dd9 NS |
2525 | blkflags = XFS_IS_REALTIME_INODE(ip) ? |
2526 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS; | |
1da177e4 LT |
2527 | |
2528 | if (XFS_IS_UQUOTA_ON(mp) && udqp && | |
1149d96a | 2529 | ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) { |
1da177e4 LT |
2530 | delblksudq = udqp; |
2531 | /* | |
2532 | * If there are delayed allocation blocks, then we have to | |
2533 | * unreserve those from the old dquot, and add them to the | |
2534 | * new dquot. | |
2535 | */ | |
2536 | if (delblks) { | |
2537 | ASSERT(ip->i_udquot); | |
2538 | unresudq = ip->i_udquot; | |
2539 | } | |
2540 | } | |
c8ad20ff | 2541 | if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) { |
9a2a7de2 NS |
2542 | if (XFS_IS_PQUOTA_ON(ip->i_mount) && |
2543 | ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id)) | |
2544 | prjflags = XFS_QMOPT_ENOSPC; | |
2545 | ||
2546 | if (prjflags || | |
2547 | (XFS_IS_GQUOTA_ON(ip->i_mount) && | |
2548 | ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) { | |
c8ad20ff NS |
2549 | delblksgdq = gdqp; |
2550 | if (delblks) { | |
2551 | ASSERT(ip->i_gdquot); | |
2552 | unresgdq = ip->i_gdquot; | |
2553 | } | |
1da177e4 LT |
2554 | } |
2555 | } | |
2556 | ||
2557 | if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount, | |
2558 | delblksudq, delblksgdq, ip->i_d.di_nblocks, 1, | |
9a2a7de2 | 2559 | flags | blkflags | prjflags))) |
1da177e4 LT |
2560 | return (error); |
2561 | ||
2562 | /* | |
2563 | * Do the delayed blks reservations/unreservations now. Since, these | |
2564 | * are done without the help of a transaction, if a reservation fails | |
2565 | * its previous reservations won't be automatically undone by trans | |
2566 | * code. So, we have to do it manually here. | |
2567 | */ | |
2568 | if (delblks) { | |
2569 | /* | |
2570 | * Do the reservations first. Unreservation can't fail. | |
2571 | */ | |
2572 | ASSERT(delblksudq || delblksgdq); | |
2573 | ASSERT(unresudq || unresgdq); | |
2574 | if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount, | |
2575 | delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0, | |
9a2a7de2 | 2576 | flags | blkflags | prjflags))) |
1da177e4 LT |
2577 | return (error); |
2578 | xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount, | |
2579 | unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0, | |
06d10dd9 | 2580 | blkflags); |
1da177e4 LT |
2581 | } |
2582 | ||
2583 | return (0); | |
2584 | } | |
2585 | ||
2586 | int | |
2587 | xfs_qm_vop_rename_dqattach( | |
2588 | xfs_inode_t **i_tab) | |
2589 | { | |
2590 | xfs_inode_t *ip; | |
2591 | int i; | |
2592 | int error; | |
2593 | ||
2594 | ip = i_tab[0]; | |
2595 | ||
2596 | if (! XFS_IS_QUOTA_ON(ip->i_mount)) | |
014c2544 | 2597 | return 0; |
1da177e4 LT |
2598 | |
2599 | if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) { | |
2600 | error = xfs_qm_dqattach(ip, 0); | |
2601 | if (error) | |
014c2544 | 2602 | return error; |
1da177e4 LT |
2603 | } |
2604 | for (i = 1; (i < 4 && i_tab[i]); i++) { | |
2605 | /* | |
2606 | * Watch out for duplicate entries in the table. | |
2607 | */ | |
2608 | if ((ip = i_tab[i]) != i_tab[i-1]) { | |
2609 | if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) { | |
2610 | error = xfs_qm_dqattach(ip, 0); | |
2611 | if (error) | |
014c2544 | 2612 | return error; |
1da177e4 LT |
2613 | } |
2614 | } | |
2615 | } | |
014c2544 | 2616 | return 0; |
1da177e4 LT |
2617 | } |
2618 | ||
2619 | void | |
2620 | xfs_qm_vop_dqattach_and_dqmod_newinode( | |
2621 | xfs_trans_t *tp, | |
2622 | xfs_inode_t *ip, | |
2623 | xfs_dquot_t *udqp, | |
2624 | xfs_dquot_t *gdqp) | |
2625 | { | |
2626 | if (!XFS_IS_QUOTA_ON(tp->t_mountp)) | |
2627 | return; | |
2628 | ||
579aa9ca | 2629 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
2630 | ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp)); |
2631 | ||
2632 | if (udqp) { | |
2633 | xfs_dqlock(udqp); | |
2634 | XFS_DQHOLD(udqp); | |
2635 | xfs_dqunlock(udqp); | |
2636 | ASSERT(ip->i_udquot == NULL); | |
2637 | ip->i_udquot = udqp; | |
ee2a4f7c | 2638 | ASSERT(XFS_IS_UQUOTA_ON(tp->t_mountp)); |
1149d96a | 2639 | ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id)); |
1da177e4 LT |
2640 | xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1); |
2641 | } | |
2642 | if (gdqp) { | |
2643 | xfs_dqlock(gdqp); | |
2644 | XFS_DQHOLD(gdqp); | |
2645 | xfs_dqunlock(gdqp); | |
2646 | ASSERT(ip->i_gdquot == NULL); | |
2647 | ip->i_gdquot = gdqp; | |
ee2a4f7c NS |
2648 | ASSERT(XFS_IS_OQUOTA_ON(tp->t_mountp)); |
2649 | ASSERT((XFS_IS_GQUOTA_ON(tp->t_mountp) ? | |
2650 | ip->i_d.di_gid : ip->i_d.di_projid) == | |
2651 | be32_to_cpu(gdqp->q_core.d_id)); | |
1da177e4 LT |
2652 | xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1); |
2653 | } | |
2654 | } | |
2655 | ||
2656 | /* ------------- list stuff -----------------*/ | |
ba0f32d4 | 2657 | STATIC void |
1da177e4 LT |
2658 | xfs_qm_freelist_init(xfs_frlist_t *ql) |
2659 | { | |
2660 | ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql; | |
794ee1ba | 2661 | mutex_init(&ql->qh_lock); |
1da177e4 LT |
2662 | ql->qh_version = 0; |
2663 | ql->qh_nelems = 0; | |
2664 | } | |
2665 | ||
ba0f32d4 | 2666 | STATIC void |
1da177e4 LT |
2667 | xfs_qm_freelist_destroy(xfs_frlist_t *ql) |
2668 | { | |
2669 | xfs_dquot_t *dqp, *nextdqp; | |
2670 | ||
794ee1ba | 2671 | mutex_lock(&ql->qh_lock); |
1da177e4 LT |
2672 | for (dqp = ql->qh_next; |
2673 | dqp != (xfs_dquot_t *)ql; ) { | |
2674 | xfs_dqlock(dqp); | |
2675 | nextdqp = dqp->dq_flnext; | |
2676 | #ifdef QUOTADEBUG | |
2677 | cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp); | |
2678 | #endif | |
2679 | XQM_FREELIST_REMOVE(dqp); | |
2680 | xfs_dqunlock(dqp); | |
2681 | xfs_qm_dqdestroy(dqp); | |
2682 | dqp = nextdqp; | |
2683 | } | |
20722a91 | 2684 | mutex_unlock(&ql->qh_lock); |
1da177e4 LT |
2685 | mutex_destroy(&ql->qh_lock); |
2686 | ||
2687 | ASSERT(ql->qh_nelems == 0); | |
2688 | } | |
2689 | ||
ba0f32d4 | 2690 | STATIC void |
1da177e4 LT |
2691 | xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq) |
2692 | { | |
2693 | dq->dq_flnext = ql->qh_next; | |
2694 | dq->dq_flprev = (xfs_dquot_t *)ql; | |
2695 | ql->qh_next = dq; | |
2696 | dq->dq_flnext->dq_flprev = dq; | |
2697 | xfs_Gqm->qm_dqfreelist.qh_nelems++; | |
2698 | xfs_Gqm->qm_dqfreelist.qh_version++; | |
2699 | } | |
2700 | ||
2701 | void | |
2702 | xfs_qm_freelist_unlink(xfs_dquot_t *dq) | |
2703 | { | |
2704 | xfs_dquot_t *next = dq->dq_flnext; | |
2705 | xfs_dquot_t *prev = dq->dq_flprev; | |
2706 | ||
2707 | next->dq_flprev = prev; | |
2708 | prev->dq_flnext = next; | |
2709 | dq->dq_flnext = dq->dq_flprev = dq; | |
2710 | xfs_Gqm->qm_dqfreelist.qh_nelems--; | |
2711 | xfs_Gqm->qm_dqfreelist.qh_version++; | |
2712 | } | |
2713 | ||
2714 | void | |
2715 | xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq) | |
2716 | { | |
2717 | xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq); | |
2718 | } | |
2719 | ||
ba0f32d4 | 2720 | STATIC int |
1da177e4 LT |
2721 | xfs_qm_dqhashlock_nowait( |
2722 | xfs_dquot_t *dqp) | |
2723 | { | |
2724 | int locked; | |
2725 | ||
2726 | locked = mutex_trylock(&((dqp)->q_hash->qh_lock)); | |
014c2544 | 2727 | return locked; |
1da177e4 LT |
2728 | } |
2729 | ||
2730 | int | |
2731 | xfs_qm_freelist_lock_nowait( | |
2732 | xfs_qm_t *xqm) | |
2733 | { | |
2734 | int locked; | |
2735 | ||
2736 | locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock)); | |
014c2544 | 2737 | return locked; |
1da177e4 LT |
2738 | } |
2739 | ||
ba0f32d4 | 2740 | STATIC int |
1da177e4 LT |
2741 | xfs_qm_mplist_nowait( |
2742 | xfs_mount_t *mp) | |
2743 | { | |
2744 | int locked; | |
2745 | ||
2746 | ASSERT(mp->m_quotainfo); | |
2747 | locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp))); | |
014c2544 | 2748 | return locked; |
1da177e4 | 2749 | } |