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