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