~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/xfs/quota/xfs_dquot.c

Version: ~ [ linux-5.0-rc6 ] ~ [ linux-4.20.10 ] ~ [ linux-4.19.23 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.101 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.158 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.174 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.134 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp