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