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