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