]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/xfs/quota/xfs_dquot.c
[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / quota / xfs_dquot.c
1 /*
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include "xfs.h"
34 #include "xfs_fs.h"
35 #include "xfs_bit.h"
36 #include "xfs_log.h"
37 #include "xfs_inum.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_alloc.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_quota.h"
46 #include "xfs_mount.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_alloc_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_dir_sf.h"
51 #include "xfs_dir2_sf.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dinode.h"
54 #include "xfs_inode.h"
55 #include "xfs_btree.h"
56 #include "xfs_ialloc.h"
57 #include "xfs_bmap.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
61 #include "xfs_rw.h"
62 #include "xfs_acl.h"
63 #include "xfs_cap.h"
64 #include "xfs_mac.h"
65 #include "xfs_attr.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_trans_space.h"
68 #include "xfs_trans_priv.h"
69
70 #include "xfs_qm.h"
71
72
73 /*
74 LOCK ORDER
75
76 inode lock (ilock)
77 dquot hash-chain lock (hashlock)
78 xqm dquot freelist lock (freelistlock
79 mount's dquot list lock (mplistlock)
80 user dquot lock - lock ordering among dquots is based on the uid or gid
81 group dquot lock - similar to udquots. Between the two dquots, the udquot
82 has to be locked first.
83 pin lock - the dquot lock must be held to take this lock.
84 flush lock - ditto.
85 */
86
87 STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
88
89 #ifdef DEBUG
90 xfs_buftarg_t *xfs_dqerror_target;
91 int xfs_do_dqerror;
92 int xfs_dqreq_num;
93 int xfs_dqerror_mod = 33;
94 #endif
95
96 /*
97 * Allocate and initialize a dquot. We don't always allocate fresh memory;
98 * we try to reclaim a free dquot if the number of incore dquots are above
99 * a threshold.
100 * The only field inside the core that gets initialized at this point
101 * is the d_id field. The idea is to fill in the entire q_core
102 * when we read in the on disk dquot.
103 */
104 STATIC xfs_dquot_t *
105 xfs_qm_dqinit(
106 xfs_mount_t *mp,
107 xfs_dqid_t id,
108 uint type)
109 {
110 xfs_dquot_t *dqp;
111 boolean_t brandnewdquot;
112
113 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
114 dqp->dq_flags = type;
115 INT_SET(dqp->q_core.d_id, ARCH_CONVERT, id);
116 dqp->q_mount = mp;
117
118 /*
119 * No need to re-initialize these if this is a reclaimed dquot.
120 */
121 if (brandnewdquot) {
122 dqp->dq_flnext = dqp->dq_flprev = dqp;
123 mutex_init(&dqp->q_qlock, MUTEX_DEFAULT, "xdq");
124 initnsema(&dqp->q_flock, 1, "fdq");
125 sv_init(&dqp->q_pinwait, SV_DEFAULT, "pdq");
126
127 #ifdef XFS_DQUOT_TRACE
128 dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_SLEEP);
129 xfs_dqtrace_entry(dqp, "DQINIT");
130 #endif
131 } else {
132 /*
133 * Only the q_core portion was zeroed in dqreclaim_one().
134 * So, we need to reset others.
135 */
136 dqp->q_nrefs = 0;
137 dqp->q_blkno = 0;
138 dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
139 dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
140 dqp->q_bufoffset = 0;
141 dqp->q_fileoffset = 0;
142 dqp->q_transp = NULL;
143 dqp->q_gdquot = NULL;
144 dqp->q_res_bcount = 0;
145 dqp->q_res_icount = 0;
146 dqp->q_res_rtbcount = 0;
147 dqp->q_pincount = 0;
148 dqp->q_hash = NULL;
149 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
150
151 #ifdef XFS_DQUOT_TRACE
152 ASSERT(dqp->q_trace);
153 xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
154 #endif
155 }
156
157 /*
158 * log item gets initialized later
159 */
160 return (dqp);
161 }
162
163 /*
164 * This is called to free all the memory associated with a dquot
165 */
166 void
167 xfs_qm_dqdestroy(
168 xfs_dquot_t *dqp)
169 {
170 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
171
172 mutex_destroy(&dqp->q_qlock);
173 freesema(&dqp->q_flock);
174 sv_destroy(&dqp->q_pinwait);
175
176 #ifdef XFS_DQUOT_TRACE
177 if (dqp->q_trace)
178 ktrace_free(dqp->q_trace);
179 dqp->q_trace = NULL;
180 #endif
181 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
182 atomic_dec(&xfs_Gqm->qm_totaldquots);
183 }
184
185 /*
186 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
187 */
188 STATIC void
189 xfs_qm_dqinit_core(
190 xfs_dqid_t id,
191 uint type,
192 xfs_dqblk_t *d)
193 {
194 /*
195 * Caller has zero'd the entire dquot 'chunk' already.
196 */
197 INT_SET(d->dd_diskdq.d_magic, ARCH_CONVERT, XFS_DQUOT_MAGIC);
198 INT_SET(d->dd_diskdq.d_version, ARCH_CONVERT, XFS_DQUOT_VERSION);
199 INT_SET(d->dd_diskdq.d_id, ARCH_CONVERT, id);
200 INT_SET(d->dd_diskdq.d_flags, ARCH_CONVERT, type);
201 }
202
203
204 #ifdef XFS_DQUOT_TRACE
205 /*
206 * Dquot tracing for debugging.
207 */
208 /* ARGSUSED */
209 void
210 __xfs_dqtrace_entry(
211 xfs_dquot_t *dqp,
212 char *func,
213 void *retaddr,
214 xfs_inode_t *ip)
215 {
216 xfs_dquot_t *udqp = NULL;
217 xfs_ino_t ino = 0;
218
219 ASSERT(dqp->q_trace);
220 if (ip) {
221 ino = ip->i_ino;
222 udqp = ip->i_udquot;
223 }
224 ktrace_enter(dqp->q_trace,
225 (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
226 (void *)func,
227 (void *)(__psint_t)dqp->q_nrefs,
228 (void *)(__psint_t)dqp->dq_flags,
229 (void *)(__psint_t)dqp->q_res_bcount,
230 (void *)(__psint_t)INT_GET(dqp->q_core.d_bcount,
231 ARCH_CONVERT),
232 (void *)(__psint_t)INT_GET(dqp->q_core.d_icount,
233 ARCH_CONVERT),
234 (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_hardlimit,
235 ARCH_CONVERT),
236 (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_softlimit,
237 ARCH_CONVERT),
238 (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_hardlimit,
239 ARCH_CONVERT),
240 (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_softlimit,
241 ARCH_CONVERT),
242 (void *)(__psint_t)INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
243 (void *)(__psint_t)current_pid(),
244 (void *)(__psint_t)ino,
245 (void *)(__psint_t)retaddr,
246 (void *)(__psint_t)udqp);
247 return;
248 }
249 #endif
250
251
252 /*
253 * If default limits are in force, push them into the dquot now.
254 * We overwrite the dquot limits only if they are zero and this
255 * is not the root dquot.
256 */
257 void
258 xfs_qm_adjust_dqlimits(
259 xfs_mount_t *mp,
260 xfs_disk_dquot_t *d)
261 {
262 xfs_quotainfo_t *q = mp->m_quotainfo;
263
264 ASSERT(d->d_id);
265
266 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
267 INT_SET(d->d_blk_softlimit, ARCH_CONVERT, q->qi_bsoftlimit);
268 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
269 INT_SET(d->d_blk_hardlimit, ARCH_CONVERT, q->qi_bhardlimit);
270 if (q->qi_isoftlimit && !d->d_ino_softlimit)
271 INT_SET(d->d_ino_softlimit, ARCH_CONVERT, q->qi_isoftlimit);
272 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
273 INT_SET(d->d_ino_hardlimit, ARCH_CONVERT, q->qi_ihardlimit);
274 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
275 INT_SET(d->d_rtb_softlimit, ARCH_CONVERT, q->qi_rtbsoftlimit);
276 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
277 INT_SET(d->d_rtb_hardlimit, ARCH_CONVERT, q->qi_rtbhardlimit);
278 }
279
280 /*
281 * Check the limits and timers of a dquot and start or reset timers
282 * if necessary.
283 * This gets called even when quota enforcement is OFF, which makes our
284 * life a little less complicated. (We just don't reject any quota
285 * reservations in that case, when enforcement is off).
286 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
287 * enforcement's off.
288 * In contrast, warnings are a little different in that they don't
289 * 'automatically' get started when limits get exceeded. They do
290 * get reset to zero, however, when we find the count to be under
291 * the soft limit (they are only ever set non-zero via userspace).
292 */
293 void
294 xfs_qm_adjust_dqtimers(
295 xfs_mount_t *mp,
296 xfs_disk_dquot_t *d)
297 {
298 ASSERT(d->d_id);
299
300 #ifdef QUOTADEBUG
301 if (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT))
302 ASSERT(INT_GET(d->d_blk_softlimit, ARCH_CONVERT) <=
303 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT));
304 if (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT))
305 ASSERT(INT_GET(d->d_ino_softlimit, ARCH_CONVERT) <=
306 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT));
307 if (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT))
308 ASSERT(INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) <=
309 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT));
310 #endif
311 if (!d->d_btimer) {
312 if ((INT_GET(d->d_blk_softlimit, ARCH_CONVERT) &&
313 (INT_GET(d->d_bcount, ARCH_CONVERT) >=
314 INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) ||
315 (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT) &&
316 (INT_GET(d->d_bcount, ARCH_CONVERT) >=
317 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
318 INT_SET(d->d_btimer, ARCH_CONVERT,
319 get_seconds() + XFS_QI_BTIMELIMIT(mp));
320 } else {
321 d->d_bwarns = 0;
322 }
323 } else {
324 if ((!d->d_blk_softlimit ||
325 (INT_GET(d->d_bcount, ARCH_CONVERT) <
326 INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) &&
327 (!d->d_blk_hardlimit ||
328 (INT_GET(d->d_bcount, ARCH_CONVERT) <
329 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
330 d->d_btimer = 0;
331 }
332 }
333
334 if (!d->d_itimer) {
335 if ((INT_GET(d->d_ino_softlimit, ARCH_CONVERT) &&
336 (INT_GET(d->d_icount, ARCH_CONVERT) >=
337 INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) ||
338 (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT) &&
339 (INT_GET(d->d_icount, ARCH_CONVERT) >=
340 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
341 INT_SET(d->d_itimer, ARCH_CONVERT,
342 get_seconds() + XFS_QI_ITIMELIMIT(mp));
343 } else {
344 d->d_iwarns = 0;
345 }
346 } else {
347 if ((!d->d_ino_softlimit ||
348 (INT_GET(d->d_icount, ARCH_CONVERT) <
349 INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) &&
350 (!d->d_ino_hardlimit ||
351 (INT_GET(d->d_icount, ARCH_CONVERT) <
352 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
353 d->d_itimer = 0;
354 }
355 }
356
357 if (!d->d_rtbtimer) {
358 if ((INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) &&
359 (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
360 INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) ||
361 (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT) &&
362 (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
363 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
364 INT_SET(d->d_rtbtimer, ARCH_CONVERT,
365 get_seconds() + XFS_QI_RTBTIMELIMIT(mp));
366 } else {
367 d->d_rtbwarns = 0;
368 }
369 } else {
370 if ((!d->d_rtb_softlimit ||
371 (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
372 INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) &&
373 (!d->d_rtb_hardlimit ||
374 (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
375 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
376 d->d_rtbtimer = 0;
377 }
378 }
379 }
380
381 /*
382 * initialize a buffer full of dquots and log the whole thing
383 */
384 STATIC void
385 xfs_qm_init_dquot_blk(
386 xfs_trans_t *tp,
387 xfs_mount_t *mp,
388 xfs_dqid_t id,
389 uint type,
390 xfs_buf_t *bp)
391 {
392 xfs_dqblk_t *d;
393 int curid, i;
394
395 ASSERT(tp);
396 ASSERT(XFS_BUF_ISBUSY(bp));
397 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
398
399 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
400
401 /*
402 * ID of the first dquot in the block - id's are zero based.
403 */
404 curid = id - (id % XFS_QM_DQPERBLK(mp));
405 ASSERT(curid >= 0);
406 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
407 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
408 xfs_qm_dqinit_core(curid, type, d);
409 xfs_trans_dquot_buf(tp, bp,
410 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
411 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
412 XFS_BLI_GDQUOT_BUF)));
413 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
414 }
415
416
417
418 /*
419 * Allocate a block and fill it with dquots.
420 * This is called when the bmapi finds a hole.
421 */
422 STATIC int
423 xfs_qm_dqalloc(
424 xfs_trans_t **tpp,
425 xfs_mount_t *mp,
426 xfs_dquot_t *dqp,
427 xfs_inode_t *quotip,
428 xfs_fileoff_t offset_fsb,
429 xfs_buf_t **O_bpp)
430 {
431 xfs_fsblock_t firstblock;
432 xfs_bmap_free_t flist;
433 xfs_bmbt_irec_t map;
434 int nmaps, error, committed;
435 xfs_buf_t *bp;
436 xfs_trans_t *tp = *tpp;
437
438 ASSERT(tp != NULL);
439 xfs_dqtrace_entry(dqp, "DQALLOC");
440
441 /*
442 * Initialize the bmap freelist prior to calling bmapi code.
443 */
444 XFS_BMAP_INIT(&flist, &firstblock);
445 xfs_ilock(quotip, XFS_ILOCK_EXCL);
446 /*
447 * Return if this type of quotas is turned off while we didn't
448 * have an inode lock
449 */
450 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
451 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
452 return (ESRCH);
453 }
454
455 /*
456 * xfs_trans_commit normally decrements the vnode ref count
457 * when it unlocks the inode. Since we want to keep the quota
458 * inode around, we bump the vnode ref count now.
459 */
460 VN_HOLD(XFS_ITOV(quotip));
461
462 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
463 nmaps = 1;
464 if ((error = xfs_bmapi(tp, quotip,
465 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
466 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
467 &firstblock,
468 XFS_QM_DQALLOC_SPACE_RES(mp),
469 &map, &nmaps, &flist))) {
470 goto error0;
471 }
472 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
473 ASSERT(nmaps == 1);
474 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
475 (map.br_startblock != HOLESTARTBLOCK));
476
477 /*
478 * Keep track of the blkno to save a lookup later
479 */
480 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
481
482 /* now we can just get the buffer (there's nothing to read yet) */
483 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
484 dqp->q_blkno,
485 XFS_QI_DQCHUNKLEN(mp),
486 0);
487 if (!bp || (error = XFS_BUF_GETERROR(bp)))
488 goto error1;
489 /*
490 * Make a chunk of dquots out of this buffer and log
491 * the entire thing.
492 */
493 xfs_qm_init_dquot_blk(tp, mp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
494 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
495
496 /*
497 * xfs_bmap_finish() may commit the current transaction and
498 * start a second transaction if the freelist is not empty.
499 *
500 * Since we still want to modify this buffer, we need to
501 * ensure that the buffer is not released on commit of
502 * the first transaction and ensure the buffer is added to the
503 * second transaction.
504 *
505 * If there is only one transaction then don't stop the buffer
506 * from being released when it commits later on.
507 */
508
509 xfs_trans_bhold(tp, bp);
510
511 if ((error = xfs_bmap_finish(tpp, &flist, firstblock, &committed))) {
512 goto error1;
513 }
514
515 if (committed) {
516 tp = *tpp;
517 xfs_trans_bjoin(tp, bp);
518 } else {
519 xfs_trans_bhold_release(tp, bp);
520 }
521
522 *O_bpp = bp;
523 return 0;
524
525 error1:
526 xfs_bmap_cancel(&flist);
527 error0:
528 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
529
530 return (error);
531 }
532
533 /*
534 * Maps a dquot to the buffer containing its on-disk version.
535 * This returns a ptr to the buffer containing the on-disk dquot
536 * in the bpp param, and a ptr to the on-disk dquot within that buffer
537 */
538 STATIC int
539 xfs_qm_dqtobp(
540 xfs_trans_t **tpp,
541 xfs_dquot_t *dqp,
542 xfs_disk_dquot_t **O_ddpp,
543 xfs_buf_t **O_bpp,
544 uint flags)
545 {
546 xfs_bmbt_irec_t map;
547 int nmaps, error;
548 xfs_buf_t *bp;
549 xfs_inode_t *quotip;
550 xfs_mount_t *mp;
551 xfs_disk_dquot_t *ddq;
552 xfs_dqid_t id;
553 boolean_t newdquot;
554 xfs_trans_t *tp = (tpp ? *tpp : NULL);
555
556 mp = dqp->q_mount;
557 id = INT_GET(dqp->q_core.d_id, ARCH_CONVERT);
558 nmaps = 1;
559 newdquot = B_FALSE;
560
561 /*
562 * If we don't know where the dquot lives, find out.
563 */
564 if (dqp->q_blkno == (xfs_daddr_t) 0) {
565 /* We use the id as an index */
566 dqp->q_fileoffset = (xfs_fileoff_t)id / XFS_QM_DQPERBLK(mp);
567 nmaps = 1;
568 quotip = XFS_DQ_TO_QIP(dqp);
569 xfs_ilock(quotip, XFS_ILOCK_SHARED);
570 /*
571 * Return if this type of quotas is turned off while we didn't
572 * have an inode lock
573 */
574 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
575 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
576 return (ESRCH);
577 }
578 /*
579 * Find the block map; no allocations yet
580 */
581 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
582 XFS_DQUOT_CLUSTER_SIZE_FSB,
583 XFS_BMAPI_METADATA,
584 NULL, 0, &map, &nmaps, NULL);
585
586 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
587 if (error)
588 return (error);
589 ASSERT(nmaps == 1);
590 ASSERT(map.br_blockcount == 1);
591
592 /*
593 * offset of dquot in the (fixed sized) dquot chunk.
594 */
595 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
596 sizeof(xfs_dqblk_t);
597 if (map.br_startblock == HOLESTARTBLOCK) {
598 /*
599 * We don't allocate unless we're asked to
600 */
601 if (!(flags & XFS_QMOPT_DQALLOC))
602 return (ENOENT);
603
604 ASSERT(tp);
605 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
606 dqp->q_fileoffset, &bp)))
607 return (error);
608 tp = *tpp;
609 newdquot = B_TRUE;
610 } else {
611 /*
612 * store the blkno etc so that we don't have to do the
613 * mapping all the time
614 */
615 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
616 }
617 }
618 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
619 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
620
621 /*
622 * Read in the buffer, unless we've just done the allocation
623 * (in which case we already have the buf).
624 */
625 if (! newdquot) {
626 xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
627 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
628 dqp->q_blkno,
629 XFS_QI_DQCHUNKLEN(mp),
630 0, &bp))) {
631 return (error);
632 }
633 if (error || !bp)
634 return XFS_ERROR(error);
635 }
636 ASSERT(XFS_BUF_ISBUSY(bp));
637 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
638
639 /*
640 * calculate the location of the dquot inside the buffer.
641 */
642 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
643
644 /*
645 * A simple sanity check in case we got a corrupted dquot...
646 */
647 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
648 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
649 "dqtobp")) {
650 if (!(flags & XFS_QMOPT_DQREPAIR)) {
651 xfs_trans_brelse(tp, bp);
652 return XFS_ERROR(EIO);
653 }
654 XFS_BUF_BUSY(bp); /* We dirtied this */
655 }
656
657 *O_bpp = bp;
658 *O_ddpp = ddq;
659
660 return (0);
661 }
662
663
664 /*
665 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
666 * and release the buffer immediately.
667 *
668 */
669 /* ARGSUSED */
670 STATIC int
671 xfs_qm_dqread(
672 xfs_trans_t **tpp,
673 xfs_dqid_t id,
674 xfs_dquot_t *dqp, /* dquot to get filled in */
675 uint flags)
676 {
677 xfs_disk_dquot_t *ddqp;
678 xfs_buf_t *bp;
679 int error;
680 xfs_trans_t *tp;
681
682 ASSERT(tpp);
683
684 /*
685 * get a pointer to the on-disk dquot and the buffer containing it
686 * dqp already knows its own type (GROUP/USER).
687 */
688 xfs_dqtrace_entry(dqp, "DQREAD");
689 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
690 return (error);
691 }
692 tp = *tpp;
693
694 /* copy everything from disk dquot to the incore dquot */
695 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
696 ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
697 xfs_qm_dquot_logitem_init(dqp);
698
699 /*
700 * Reservation counters are defined as reservation plus current usage
701 * to avoid having to add everytime.
702 */
703 dqp->q_res_bcount = INT_GET(ddqp->d_bcount, ARCH_CONVERT);
704 dqp->q_res_icount = INT_GET(ddqp->d_icount, ARCH_CONVERT);
705 dqp->q_res_rtbcount = INT_GET(ddqp->d_rtbcount, ARCH_CONVERT);
706
707 /* Mark the buf so that this will stay incore a little longer */
708 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
709
710 /*
711 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
712 * So we need to release with xfs_trans_brelse().
713 * The strategy here is identical to that of inodes; we lock
714 * the dquot in xfs_qm_dqget() before making it accessible to
715 * others. This is because dquots, like inodes, need a good level of
716 * concurrency, and we don't want to take locks on the entire buffers
717 * for dquot accesses.
718 * Note also that the dquot buffer may even be dirty at this point, if
719 * this particular dquot was repaired. We still aren't afraid to
720 * brelse it because we have the changes incore.
721 */
722 ASSERT(XFS_BUF_ISBUSY(bp));
723 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
724 xfs_trans_brelse(tp, bp);
725
726 return (error);
727 }
728
729
730 /*
731 * allocate an incore dquot from the kernel heap,
732 * and fill its core with quota information kept on disk.
733 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
734 * if it wasn't already allocated.
735 */
736 STATIC int
737 xfs_qm_idtodq(
738 xfs_mount_t *mp,
739 xfs_dqid_t id, /* gid or uid, depending on type */
740 uint type, /* UDQUOT or GDQUOT */
741 uint flags, /* DQALLOC, DQREPAIR */
742 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
743 {
744 xfs_dquot_t *dqp;
745 int error;
746 xfs_trans_t *tp;
747 int cancelflags=0;
748
749 dqp = xfs_qm_dqinit(mp, id, type);
750 tp = NULL;
751 if (flags & XFS_QMOPT_DQALLOC) {
752 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
753 if ((error = xfs_trans_reserve(tp,
754 XFS_QM_DQALLOC_SPACE_RES(mp),
755 XFS_WRITE_LOG_RES(mp) +
756 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
757 128,
758 0,
759 XFS_TRANS_PERM_LOG_RES,
760 XFS_WRITE_LOG_COUNT))) {
761 cancelflags = 0;
762 goto error0;
763 }
764 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
765 }
766
767 /*
768 * Read it from disk; xfs_dqread() takes care of
769 * all the necessary initialization of dquot's fields (locks, etc)
770 */
771 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
772 /*
773 * This can happen if quotas got turned off (ESRCH),
774 * or if the dquot didn't exist on disk and we ask to
775 * allocate (ENOENT).
776 */
777 xfs_dqtrace_entry(dqp, "DQREAD FAIL");
778 cancelflags |= XFS_TRANS_ABORT;
779 goto error0;
780 }
781 if (tp) {
782 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
783 NULL)))
784 goto error1;
785 }
786
787 *O_dqpp = dqp;
788 return (0);
789
790 error0:
791 ASSERT(error);
792 if (tp)
793 xfs_trans_cancel(tp, cancelflags);
794 error1:
795 xfs_qm_dqdestroy(dqp);
796 *O_dqpp = NULL;
797 return (error);
798 }
799
800 /*
801 * Lookup a dquot in the incore dquot hashtable. We keep two separate
802 * hashtables for user and group dquots; and, these are global tables
803 * inside the XQM, not per-filesystem tables.
804 * The hash chain must be locked by caller, and it is left locked
805 * on return. Returning dquot is locked.
806 */
807 STATIC int
808 xfs_qm_dqlookup(
809 xfs_mount_t *mp,
810 xfs_dqid_t id,
811 xfs_dqhash_t *qh,
812 xfs_dquot_t **O_dqpp)
813 {
814 xfs_dquot_t *dqp;
815 uint flist_locked;
816 xfs_dquot_t *d;
817
818 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
819
820 flist_locked = B_FALSE;
821
822 /*
823 * Traverse the hashchain looking for a match
824 */
825 for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
826 /*
827 * We already have the hashlock. We don't need the
828 * dqlock to look at the id field of the dquot, since the
829 * id can't be modified without the hashlock anyway.
830 */
831 if (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id && dqp->q_mount == mp) {
832 xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
833 /*
834 * All in core dquots must be on the dqlist of mp
835 */
836 ASSERT(dqp->MPL_PREVP != NULL);
837
838 xfs_dqlock(dqp);
839 if (dqp->q_nrefs == 0) {
840 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
841 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
842 xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
843
844 /*
845 * We may have raced with dqreclaim_one()
846 * (and lost). So, flag that we don't
847 * want the dquot to be reclaimed.
848 */
849 dqp->dq_flags |= XFS_DQ_WANT;
850 xfs_dqunlock(dqp);
851 xfs_qm_freelist_lock(xfs_Gqm);
852 xfs_dqlock(dqp);
853 dqp->dq_flags &= ~(XFS_DQ_WANT);
854 }
855 flist_locked = B_TRUE;
856 }
857
858 /*
859 * id couldn't have changed; we had the hashlock all
860 * along
861 */
862 ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
863
864 if (flist_locked) {
865 if (dqp->q_nrefs != 0) {
866 xfs_qm_freelist_unlock(xfs_Gqm);
867 flist_locked = B_FALSE;
868 } else {
869 /*
870 * take it off the freelist
871 */
872 xfs_dqtrace_entry(dqp,
873 "DQLOOKUP: TAKEOFF FL");
874 XQM_FREELIST_REMOVE(dqp);
875 /* xfs_qm_freelist_print(&(xfs_Gqm->
876 qm_dqfreelist),
877 "after removal"); */
878 }
879 }
880
881 /*
882 * grab a reference
883 */
884 XFS_DQHOLD(dqp);
885
886 if (flist_locked)
887 xfs_qm_freelist_unlock(xfs_Gqm);
888 /*
889 * move the dquot to the front of the hashchain
890 */
891 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
892 if (dqp->HL_PREVP != &qh->qh_next) {
893 xfs_dqtrace_entry(dqp,
894 "DQLOOKUP: HASH MOVETOFRONT");
895 if ((d = dqp->HL_NEXT))
896 d->HL_PREVP = dqp->HL_PREVP;
897 *(dqp->HL_PREVP) = d;
898 d = qh->qh_next;
899 d->HL_PREVP = &dqp->HL_NEXT;
900 dqp->HL_NEXT = d;
901 dqp->HL_PREVP = &qh->qh_next;
902 qh->qh_next = dqp;
903 }
904 xfs_dqtrace_entry(dqp, "LOOKUP END");
905 *O_dqpp = dqp;
906 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
907 return (0);
908 }
909 }
910
911 *O_dqpp = NULL;
912 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
913 return (1);
914 }
915
916 /*
917 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
918 * a locked dquot, doing an allocation (if requested) as needed.
919 * When both an inode and an id are given, the inode's id takes precedence.
920 * That is, if the id changes while we don't hold the ilock inside this
921 * function, the new dquot is returned, not necessarily the one requested
922 * in the id argument.
923 */
924 int
925 xfs_qm_dqget(
926 xfs_mount_t *mp,
927 xfs_inode_t *ip, /* locked inode (optional) */
928 xfs_dqid_t id, /* uid/projid/gid depending on type */
929 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
930 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
931 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
932 {
933 xfs_dquot_t *dqp;
934 xfs_dqhash_t *h;
935 uint version;
936 int error;
937
938 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
939 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
940 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
941 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
942 return (ESRCH);
943 }
944 h = XFS_DQ_HASH(mp, id, type);
945
946 #ifdef DEBUG
947 if (xfs_do_dqerror) {
948 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
949 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
950 cmn_err(CE_DEBUG, "Returning error in dqget");
951 return (EIO);
952 }
953 }
954 #endif
955
956 again:
957
958 #ifdef DEBUG
959 ASSERT(type == XFS_DQ_USER ||
960 type == XFS_DQ_PROJ ||
961 type == XFS_DQ_GROUP);
962 if (ip) {
963 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
964 if (type == XFS_DQ_USER)
965 ASSERT(ip->i_udquot == NULL);
966 else
967 ASSERT(ip->i_gdquot == NULL);
968 }
969 #endif
970 XFS_DQ_HASH_LOCK(h);
971
972 /*
973 * Look in the cache (hashtable).
974 * The chain is kept locked during lookup.
975 */
976 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
977 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
978 /*
979 * The dquot was found, moved to the front of the chain,
980 * taken off the freelist if it was on it, and locked
981 * at this point. Just unlock the hashchain and return.
982 */
983 ASSERT(*O_dqpp);
984 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
985 XFS_DQ_HASH_UNLOCK(h);
986 xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
987 return (0); /* success */
988 }
989 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
990
991 /*
992 * Dquot cache miss. We don't want to keep the inode lock across
993 * a (potential) disk read. Also we don't want to deal with the lock
994 * ordering between quotainode and this inode. OTOH, dropping the inode
995 * lock here means dealing with a chown that can happen before
996 * we re-acquire the lock.
997 */
998 if (ip)
999 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1000 /*
1001 * Save the hashchain version stamp, and unlock the chain, so that
1002 * we don't keep the lock across a disk read
1003 */
1004 version = h->qh_version;
1005 XFS_DQ_HASH_UNLOCK(h);
1006
1007 /*
1008 * Allocate the dquot on the kernel heap, and read the ondisk
1009 * portion off the disk. Also, do all the necessary initialization
1010 * This can return ENOENT if dquot didn't exist on disk and we didn't
1011 * ask it to allocate; ESRCH if quotas got turned off suddenly.
1012 */
1013 if ((error = xfs_qm_idtodq(mp, id, type,
1014 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
1015 XFS_QMOPT_DOWARN),
1016 &dqp))) {
1017 if (ip)
1018 xfs_ilock(ip, XFS_ILOCK_EXCL);
1019 return (error);
1020 }
1021
1022 /*
1023 * See if this is mount code calling to look at the overall quota limits
1024 * which are stored in the id == 0 user or group's dquot.
1025 * Since we may not have done a quotacheck by this point, just return
1026 * the dquot without attaching it to any hashtables, lists, etc, or even
1027 * taking a reference.
1028 * The caller must dqdestroy this once done.
1029 */
1030 if (flags & XFS_QMOPT_DQSUSER) {
1031 ASSERT(id == 0);
1032 ASSERT(! ip);
1033 goto dqret;
1034 }
1035
1036 /*
1037 * Dquot lock comes after hashlock in the lock ordering
1038 */
1039 if (ip) {
1040 xfs_ilock(ip, XFS_ILOCK_EXCL);
1041 if (! XFS_IS_DQTYPE_ON(mp, type)) {
1042 /* inode stays locked on return */
1043 xfs_qm_dqdestroy(dqp);
1044 return XFS_ERROR(ESRCH);
1045 }
1046 /*
1047 * A dquot could be attached to this inode by now, since
1048 * we had dropped the ilock.
1049 */
1050 if (type == XFS_DQ_USER) {
1051 if (ip->i_udquot) {
1052 xfs_qm_dqdestroy(dqp);
1053 dqp = ip->i_udquot;
1054 xfs_dqlock(dqp);
1055 goto dqret;
1056 }
1057 } else {
1058 if (ip->i_gdquot) {
1059 xfs_qm_dqdestroy(dqp);
1060 dqp = ip->i_gdquot;
1061 xfs_dqlock(dqp);
1062 goto dqret;
1063 }
1064 }
1065 }
1066
1067 /*
1068 * Hashlock comes after ilock in lock order
1069 */
1070 XFS_DQ_HASH_LOCK(h);
1071 if (version != h->qh_version) {
1072 xfs_dquot_t *tmpdqp;
1073 /*
1074 * Now, see if somebody else put the dquot in the
1075 * hashtable before us. This can happen because we didn't
1076 * keep the hashchain lock. We don't have to worry about
1077 * lock order between the two dquots here since dqp isn't
1078 * on any findable lists yet.
1079 */
1080 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1081 /*
1082 * Duplicate found. Just throw away the new dquot
1083 * and start over.
1084 */
1085 xfs_qm_dqput(tmpdqp);
1086 XFS_DQ_HASH_UNLOCK(h);
1087 xfs_qm_dqdestroy(dqp);
1088 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1089 goto again;
1090 }
1091 }
1092
1093 /*
1094 * Put the dquot at the beginning of the hash-chain and mp's list
1095 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1096 */
1097 ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
1098 dqp->q_hash = h;
1099 XQM_HASHLIST_INSERT(h, dqp);
1100
1101 /*
1102 * Attach this dquot to this filesystem's list of all dquots,
1103 * kept inside the mount structure in m_quotainfo field
1104 */
1105 xfs_qm_mplist_lock(mp);
1106
1107 /*
1108 * We return a locked dquot to the caller, with a reference taken
1109 */
1110 xfs_dqlock(dqp);
1111 dqp->q_nrefs = 1;
1112
1113 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
1114
1115 xfs_qm_mplist_unlock(mp);
1116 XFS_DQ_HASH_UNLOCK(h);
1117 dqret:
1118 ASSERT((ip == NULL) || XFS_ISLOCKED_INODE_EXCL(ip));
1119 xfs_dqtrace_entry(dqp, "DQGET DONE");
1120 *O_dqpp = dqp;
1121 return (0);
1122 }
1123
1124
1125 /*
1126 * Release a reference to the dquot (decrement ref-count)
1127 * and unlock it. If there is a group quota attached to this
1128 * dquot, carefully release that too without tripping over
1129 * deadlocks'n'stuff.
1130 */
1131 void
1132 xfs_qm_dqput(
1133 xfs_dquot_t *dqp)
1134 {
1135 xfs_dquot_t *gdqp;
1136
1137 ASSERT(dqp->q_nrefs > 0);
1138 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1139 xfs_dqtrace_entry(dqp, "DQPUT");
1140
1141 if (dqp->q_nrefs != 1) {
1142 dqp->q_nrefs--;
1143 xfs_dqunlock(dqp);
1144 return;
1145 }
1146
1147 /*
1148 * drop the dqlock and acquire the freelist and dqlock
1149 * in the right order; but try to get it out-of-order first
1150 */
1151 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
1152 xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
1153 xfs_dqunlock(dqp);
1154 xfs_qm_freelist_lock(xfs_Gqm);
1155 xfs_dqlock(dqp);
1156 }
1157
1158 while (1) {
1159 gdqp = NULL;
1160
1161 /* We can't depend on nrefs being == 1 here */
1162 if (--dqp->q_nrefs == 0) {
1163 xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
1164 /*
1165 * insert at end of the freelist.
1166 */
1167 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1168
1169 /*
1170 * If we just added a udquot to the freelist, then
1171 * we want to release the gdquot reference that
1172 * it (probably) has. Otherwise it'll keep the
1173 * gdquot from getting reclaimed.
1174 */
1175 if ((gdqp = dqp->q_gdquot)) {
1176 /*
1177 * Avoid a recursive dqput call
1178 */
1179 xfs_dqlock(gdqp);
1180 dqp->q_gdquot = NULL;
1181 }
1182
1183 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1184 "@@@@@++ Free list (after append) @@@@@+");
1185 */
1186 }
1187 xfs_dqunlock(dqp);
1188
1189 /*
1190 * If we had a group quota inside the user quota as a hint,
1191 * release it now.
1192 */
1193 if (! gdqp)
1194 break;
1195 dqp = gdqp;
1196 }
1197 xfs_qm_freelist_unlock(xfs_Gqm);
1198 }
1199
1200 /*
1201 * Release a dquot. Flush it if dirty, then dqput() it.
1202 * dquot must not be locked.
1203 */
1204 void
1205 xfs_qm_dqrele(
1206 xfs_dquot_t *dqp)
1207 {
1208 ASSERT(dqp);
1209 xfs_dqtrace_entry(dqp, "DQRELE");
1210
1211 xfs_dqlock(dqp);
1212 /*
1213 * We don't care to flush it if the dquot is dirty here.
1214 * That will create stutters that we want to avoid.
1215 * Instead we do a delayed write when we try to reclaim
1216 * a dirty dquot. Also xfs_sync will take part of the burden...
1217 */
1218 xfs_qm_dqput(dqp);
1219 }
1220
1221
1222 /*
1223 * Write a modified dquot to disk.
1224 * The dquot must be locked and the flush lock too taken by caller.
1225 * The flush lock will not be unlocked until the dquot reaches the disk,
1226 * but the dquot is free to be unlocked and modified by the caller
1227 * in the interim. Dquot is still locked on return. This behavior is
1228 * identical to that of inodes.
1229 */
1230 int
1231 xfs_qm_dqflush(
1232 xfs_dquot_t *dqp,
1233 uint flags)
1234 {
1235 xfs_mount_t *mp;
1236 xfs_buf_t *bp;
1237 xfs_disk_dquot_t *ddqp;
1238 int error;
1239 SPLDECL(s);
1240
1241 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1242 ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp));
1243 xfs_dqtrace_entry(dqp, "DQFLUSH");
1244
1245 /*
1246 * If not dirty, nada.
1247 */
1248 if (!XFS_DQ_IS_DIRTY(dqp)) {
1249 xfs_dqfunlock(dqp);
1250 return (0);
1251 }
1252
1253 /*
1254 * Cant flush a pinned dquot. Wait for it.
1255 */
1256 xfs_qm_dqunpin_wait(dqp);
1257
1258 /*
1259 * This may have been unpinned because the filesystem is shutting
1260 * down forcibly. If that's the case we must not write this dquot
1261 * to disk, because the log record didn't make it to disk!
1262 */
1263 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1264 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1265 xfs_dqfunlock(dqp);
1266 return XFS_ERROR(EIO);
1267 }
1268
1269 /*
1270 * Get the buffer containing the on-disk dquot
1271 * We don't need a transaction envelope because we know that the
1272 * the ondisk-dquot has already been allocated for.
1273 */
1274 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1275 xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
1276 ASSERT(error != ENOENT);
1277 /*
1278 * Quotas could have gotten turned off (ESRCH)
1279 */
1280 xfs_dqfunlock(dqp);
1281 return (error);
1282 }
1283
1284 if (xfs_qm_dqcheck(&dqp->q_core, INT_GET(ddqp->d_id, ARCH_CONVERT),
1285 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
1286 xfs_force_shutdown(dqp->q_mount, XFS_CORRUPT_INCORE);
1287 return XFS_ERROR(EIO);
1288 }
1289
1290 /* This is the only portion of data that needs to persist */
1291 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1292
1293 /*
1294 * Clear the dirty field and remember the flush lsn for later use.
1295 */
1296 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1297 mp = dqp->q_mount;
1298
1299 /* lsn is 64 bits */
1300 AIL_LOCK(mp, s);
1301 dqp->q_logitem.qli_flush_lsn = dqp->q_logitem.qli_item.li_lsn;
1302 AIL_UNLOCK(mp, s);
1303
1304 /*
1305 * Attach an iodone routine so that we can remove this dquot from the
1306 * AIL and release the flush lock once the dquot is synced to disk.
1307 */
1308 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1309 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1310 /*
1311 * If the buffer is pinned then push on the log so we won't
1312 * get stuck waiting in the write for too long.
1313 */
1314 if (XFS_BUF_ISPINNED(bp)) {
1315 xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
1316 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1317 }
1318
1319 if (flags & XFS_QMOPT_DELWRI) {
1320 xfs_bdwrite(mp, bp);
1321 } else if (flags & XFS_QMOPT_ASYNC) {
1322 xfs_bawrite(mp, bp);
1323 } else {
1324 error = xfs_bwrite(mp, bp);
1325 }
1326 xfs_dqtrace_entry(dqp, "DQFLUSH END");
1327 /*
1328 * dqp is still locked, but caller is free to unlock it now.
1329 */
1330 return (error);
1331
1332 }
1333
1334 /*
1335 * This is the dquot flushing I/O completion routine. It is called
1336 * from interrupt level when the buffer containing the dquot is
1337 * flushed to disk. It is responsible for removing the dquot logitem
1338 * from the AIL if it has not been re-logged, and unlocking the dquot's
1339 * flush lock. This behavior is very similar to that of inodes..
1340 */
1341 /*ARGSUSED*/
1342 STATIC void
1343 xfs_qm_dqflush_done(
1344 xfs_buf_t *bp,
1345 xfs_dq_logitem_t *qip)
1346 {
1347 xfs_dquot_t *dqp;
1348 SPLDECL(s);
1349
1350 dqp = qip->qli_dquot;
1351
1352 /*
1353 * We only want to pull the item from the AIL if its
1354 * location in the log has not changed since we started the flush.
1355 * Thus, we only bother if the dquot's lsn has
1356 * not changed. First we check the lsn outside the lock
1357 * since it's cheaper, and then we recheck while
1358 * holding the lock before removing the dquot from the AIL.
1359 */
1360 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1361 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1362
1363 AIL_LOCK(dqp->q_mount, s);
1364 /*
1365 * xfs_trans_delete_ail() drops the AIL lock.
1366 */
1367 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
1368 xfs_trans_delete_ail(dqp->q_mount,
1369 (xfs_log_item_t*)qip, s);
1370 else
1371 AIL_UNLOCK(dqp->q_mount, s);
1372 }
1373
1374 /*
1375 * Release the dq's flush lock since we're done with it.
1376 */
1377 xfs_dqfunlock(dqp);
1378 }
1379
1380
1381 int
1382 xfs_qm_dqflock_nowait(
1383 xfs_dquot_t *dqp)
1384 {
1385 int locked;
1386
1387 locked = cpsema(&((dqp)->q_flock));
1388
1389 /* XXX ifdef these out */
1390 if (locked)
1391 (dqp)->dq_flags |= XFS_DQ_FLOCKED;
1392 return (locked);
1393 }
1394
1395
1396 int
1397 xfs_qm_dqlock_nowait(
1398 xfs_dquot_t *dqp)
1399 {
1400 return (mutex_trylock(&((dqp)->q_qlock)));
1401 }
1402
1403 void
1404 xfs_dqlock(
1405 xfs_dquot_t *dqp)
1406 {
1407 mutex_lock(&(dqp->q_qlock), PINOD);
1408 }
1409
1410 void
1411 xfs_dqunlock(
1412 xfs_dquot_t *dqp)
1413 {
1414 mutex_unlock(&(dqp->q_qlock));
1415 if (dqp->q_logitem.qli_dquot == dqp) {
1416 /* Once was dqp->q_mount, but might just have been cleared */
1417 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_mountp,
1418 (xfs_log_item_t*)&(dqp->q_logitem));
1419 }
1420 }
1421
1422
1423 void
1424 xfs_dqunlock_nonotify(
1425 xfs_dquot_t *dqp)
1426 {
1427 mutex_unlock(&(dqp->q_qlock));
1428 }
1429
1430 void
1431 xfs_dqlock2(
1432 xfs_dquot_t *d1,
1433 xfs_dquot_t *d2)
1434 {
1435 if (d1 && d2) {
1436 ASSERT(d1 != d2);
1437 if (INT_GET(d1->q_core.d_id, ARCH_CONVERT) >
1438 INT_GET(d2->q_core.d_id, ARCH_CONVERT)) {
1439 xfs_dqlock(d2);
1440 xfs_dqlock(d1);
1441 } else {
1442 xfs_dqlock(d1);
1443 xfs_dqlock(d2);
1444 }
1445 } else {
1446 if (d1) {
1447 xfs_dqlock(d1);
1448 } else if (d2) {
1449 xfs_dqlock(d2);
1450 }
1451 }
1452 }
1453
1454
1455 /*
1456 * Take a dquot out of the mount's dqlist as well as the hashlist.
1457 * This is called via unmount as well as quotaoff, and the purge
1458 * will always succeed unless there are soft (temp) references
1459 * outstanding.
1460 *
1461 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1462 * that we're returning! XXXsup - not cool.
1463 */
1464 /* ARGSUSED */
1465 int
1466 xfs_qm_dqpurge(
1467 xfs_dquot_t *dqp,
1468 uint flags)
1469 {
1470 xfs_dqhash_t *thishash;
1471 xfs_mount_t *mp;
1472
1473 mp = dqp->q_mount;
1474
1475 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
1476 ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
1477
1478 xfs_dqlock(dqp);
1479 /*
1480 * We really can't afford to purge a dquot that is
1481 * referenced, because these are hard refs.
1482 * It shouldn't happen in general because we went thru _all_ inodes in
1483 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1484 * However it is possible that we have dquots with temporary
1485 * references that are not attached to an inode. e.g. see xfs_setattr().
1486 */
1487 if (dqp->q_nrefs != 0) {
1488 xfs_dqunlock(dqp);
1489 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
1490 return (1);
1491 }
1492
1493 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1494
1495 /*
1496 * If we're turning off quotas, we have to make sure that, for
1497 * example, we don't delete quota disk blocks while dquots are
1498 * in the process of getting written to those disk blocks.
1499 * This dquot might well be on AIL, and we can't leave it there
1500 * if we're turning off quotas. Basically, we need this flush
1501 * lock, and are willing to block on it.
1502 */
1503 if (! xfs_qm_dqflock_nowait(dqp)) {
1504 /*
1505 * Block on the flush lock after nudging dquot buffer,
1506 * if it is incore.
1507 */
1508 xfs_qm_dqflock_pushbuf_wait(dqp);
1509 }
1510
1511 /*
1512 * XXXIf we're turning this type of quotas off, we don't care
1513 * about the dirty metadata sitting in this dquot. OTOH, if
1514 * we're unmounting, we do care, so we flush it and wait.
1515 */
1516 if (XFS_DQ_IS_DIRTY(dqp)) {
1517 xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
1518 /* dqflush unlocks dqflock */
1519 /*
1520 * Given that dqpurge is a very rare occurrence, it is OK
1521 * that we're holding the hashlist and mplist locks
1522 * across the disk write. But, ... XXXsup
1523 *
1524 * We don't care about getting disk errors here. We need
1525 * to purge this dquot anyway, so we go ahead regardless.
1526 */
1527 (void) xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
1528 xfs_dqflock(dqp);
1529 }
1530 ASSERT(dqp->q_pincount == 0);
1531 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1532 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1533
1534 thishash = dqp->q_hash;
1535 XQM_HASHLIST_REMOVE(thishash, dqp);
1536 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
1537 /*
1538 * XXX Move this to the front of the freelist, if we can get the
1539 * freelist lock.
1540 */
1541 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1542
1543 dqp->q_mount = NULL;
1544 dqp->q_hash = NULL;
1545 dqp->dq_flags = XFS_DQ_INACTIVE;
1546 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1547 xfs_dqfunlock(dqp);
1548 xfs_dqunlock(dqp);
1549 XFS_DQ_HASH_UNLOCK(thishash);
1550 return (0);
1551 }
1552
1553
1554 #ifdef QUOTADEBUG
1555 void
1556 xfs_qm_dqprint(xfs_dquot_t *dqp)
1557 {
1558 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1559 cmn_err(CE_DEBUG, "---- dquotID = %d",
1560 (int)INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
1561 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
1562 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1563 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1564 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1565 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
1566 INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT),
1567 (int) INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT));
1568 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
1569 INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT),
1570 (int)INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT));
1571 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
1572 INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT),
1573 (int)INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT));
1574 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
1575 INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT),
1576 (int)INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT));
1577 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1578 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
1579 (int)INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
1580 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1581 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
1582 (int)INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
1583 cmn_err(CE_DEBUG, "---- btimer = %d",
1584 (int)INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT));
1585 cmn_err(CE_DEBUG, "---- itimer = %d",
1586 (int)INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT));
1587 cmn_err(CE_DEBUG, "---------------------------");
1588 }
1589 #endif
1590
1591 /*
1592 * Give the buffer a little push if it is incore and
1593 * wait on the flush lock.
1594 */
1595 void
1596 xfs_qm_dqflock_pushbuf_wait(
1597 xfs_dquot_t *dqp)
1598 {
1599 xfs_buf_t *bp;
1600
1601 /*
1602 * Check to see if the dquot has been flushed delayed
1603 * write. If so, grab its buffer and send it
1604 * out immediately. We'll be able to acquire
1605 * the flush lock when the I/O completes.
1606 */
1607 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
1608 XFS_QI_DQCHUNKLEN(dqp->q_mount),
1609 XFS_INCORE_TRYLOCK);
1610 if (bp != NULL) {
1611 if (XFS_BUF_ISDELAYWRITE(bp)) {
1612 if (XFS_BUF_ISPINNED(bp)) {
1613 xfs_log_force(dqp->q_mount,
1614 (xfs_lsn_t)0,
1615 XFS_LOG_FORCE);
1616 }
1617 xfs_bawrite(dqp->q_mount, bp);
1618 } else {
1619 xfs_buf_relse(bp);
1620 }
1621 }
1622 xfs_dqflock(dqp);
1623 }