Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-microblaze.git] / fs / xfs / xfs_dquot.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_inode.h"
28 #include "xfs_bmap.h"
29 #include "xfs_bmap_util.h"
30 #include "xfs_alloc.h"
31 #include "xfs_quota.h"
32 #include "xfs_error.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_trans_space.h"
36 #include "xfs_trans_priv.h"
37 #include "xfs_qm.h"
38 #include "xfs_cksum.h"
39 #include "xfs_trace.h"
40 #include "xfs_log.h"
41 #include "xfs_bmap_btree.h"
42
43 /*
44  * Lock order:
45  *
46  * ip->i_lock
47  *   qi->qi_tree_lock
48  *     dquot->q_qlock (xfs_dqlock() and friends)
49  *       dquot->q_flush (xfs_dqflock() and friends)
50  *       qi->qi_lru_lock
51  *
52  * If two dquots need to be locked the order is user before group/project,
53  * otherwise by the lowest id first, see xfs_dqlock2.
54  */
55
56 struct kmem_zone                *xfs_qm_dqtrxzone;
57 static struct kmem_zone         *xfs_qm_dqzone;
58
59 static struct lock_class_key xfs_dquot_group_class;
60 static struct lock_class_key xfs_dquot_project_class;
61
62 /*
63  * This is called to free all the memory associated with a dquot
64  */
65 void
66 xfs_qm_dqdestroy(
67         xfs_dquot_t     *dqp)
68 {
69         ASSERT(list_empty(&dqp->q_lru));
70
71         kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
72         mutex_destroy(&dqp->q_qlock);
73
74         XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
75         kmem_zone_free(xfs_qm_dqzone, dqp);
76 }
77
78 /*
79  * If default limits are in force, push them into the dquot now.
80  * We overwrite the dquot limits only if they are zero and this
81  * is not the root dquot.
82  */
83 void
84 xfs_qm_adjust_dqlimits(
85         struct xfs_mount        *mp,
86         struct xfs_dquot        *dq)
87 {
88         struct xfs_quotainfo    *q = mp->m_quotainfo;
89         struct xfs_disk_dquot   *d = &dq->q_core;
90         struct xfs_def_quota    *defq;
91         int                     prealloc = 0;
92
93         ASSERT(d->d_id);
94         defq = xfs_get_defquota(dq, q);
95
96         if (defq->bsoftlimit && !d->d_blk_softlimit) {
97                 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
98                 prealloc = 1;
99         }
100         if (defq->bhardlimit && !d->d_blk_hardlimit) {
101                 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
102                 prealloc = 1;
103         }
104         if (defq->isoftlimit && !d->d_ino_softlimit)
105                 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
106         if (defq->ihardlimit && !d->d_ino_hardlimit)
107                 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
108         if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
109                 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
110         if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
111                 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
112
113         if (prealloc)
114                 xfs_dquot_set_prealloc_limits(dq);
115 }
116
117 /*
118  * Check the limits and timers of a dquot and start or reset timers
119  * if necessary.
120  * This gets called even when quota enforcement is OFF, which makes our
121  * life a little less complicated. (We just don't reject any quota
122  * reservations in that case, when enforcement is off).
123  * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
124  * enforcement's off.
125  * In contrast, warnings are a little different in that they don't
126  * 'automatically' get started when limits get exceeded.  They do
127  * get reset to zero, however, when we find the count to be under
128  * the soft limit (they are only ever set non-zero via userspace).
129  */
130 void
131 xfs_qm_adjust_dqtimers(
132         xfs_mount_t             *mp,
133         xfs_disk_dquot_t        *d)
134 {
135         ASSERT(d->d_id);
136
137 #ifdef DEBUG
138         if (d->d_blk_hardlimit)
139                 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
140                        be64_to_cpu(d->d_blk_hardlimit));
141         if (d->d_ino_hardlimit)
142                 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
143                        be64_to_cpu(d->d_ino_hardlimit));
144         if (d->d_rtb_hardlimit)
145                 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
146                        be64_to_cpu(d->d_rtb_hardlimit));
147 #endif
148
149         if (!d->d_btimer) {
150                 if ((d->d_blk_softlimit &&
151                      (be64_to_cpu(d->d_bcount) >
152                       be64_to_cpu(d->d_blk_softlimit))) ||
153                     (d->d_blk_hardlimit &&
154                      (be64_to_cpu(d->d_bcount) >
155                       be64_to_cpu(d->d_blk_hardlimit)))) {
156                         d->d_btimer = cpu_to_be32(get_seconds() +
157                                         mp->m_quotainfo->qi_btimelimit);
158                 } else {
159                         d->d_bwarns = 0;
160                 }
161         } else {
162                 if ((!d->d_blk_softlimit ||
163                      (be64_to_cpu(d->d_bcount) <=
164                       be64_to_cpu(d->d_blk_softlimit))) &&
165                     (!d->d_blk_hardlimit ||
166                     (be64_to_cpu(d->d_bcount) <=
167                      be64_to_cpu(d->d_blk_hardlimit)))) {
168                         d->d_btimer = 0;
169                 }
170         }
171
172         if (!d->d_itimer) {
173                 if ((d->d_ino_softlimit &&
174                      (be64_to_cpu(d->d_icount) >
175                       be64_to_cpu(d->d_ino_softlimit))) ||
176                     (d->d_ino_hardlimit &&
177                      (be64_to_cpu(d->d_icount) >
178                       be64_to_cpu(d->d_ino_hardlimit)))) {
179                         d->d_itimer = cpu_to_be32(get_seconds() +
180                                         mp->m_quotainfo->qi_itimelimit);
181                 } else {
182                         d->d_iwarns = 0;
183                 }
184         } else {
185                 if ((!d->d_ino_softlimit ||
186                      (be64_to_cpu(d->d_icount) <=
187                       be64_to_cpu(d->d_ino_softlimit)))  &&
188                     (!d->d_ino_hardlimit ||
189                      (be64_to_cpu(d->d_icount) <=
190                       be64_to_cpu(d->d_ino_hardlimit)))) {
191                         d->d_itimer = 0;
192                 }
193         }
194
195         if (!d->d_rtbtimer) {
196                 if ((d->d_rtb_softlimit &&
197                      (be64_to_cpu(d->d_rtbcount) >
198                       be64_to_cpu(d->d_rtb_softlimit))) ||
199                     (d->d_rtb_hardlimit &&
200                      (be64_to_cpu(d->d_rtbcount) >
201                       be64_to_cpu(d->d_rtb_hardlimit)))) {
202                         d->d_rtbtimer = cpu_to_be32(get_seconds() +
203                                         mp->m_quotainfo->qi_rtbtimelimit);
204                 } else {
205                         d->d_rtbwarns = 0;
206                 }
207         } else {
208                 if ((!d->d_rtb_softlimit ||
209                      (be64_to_cpu(d->d_rtbcount) <=
210                       be64_to_cpu(d->d_rtb_softlimit))) &&
211                     (!d->d_rtb_hardlimit ||
212                      (be64_to_cpu(d->d_rtbcount) <=
213                       be64_to_cpu(d->d_rtb_hardlimit)))) {
214                         d->d_rtbtimer = 0;
215                 }
216         }
217 }
218
219 /*
220  * initialize a buffer full of dquots and log the whole thing
221  */
222 STATIC void
223 xfs_qm_init_dquot_blk(
224         xfs_trans_t     *tp,
225         xfs_mount_t     *mp,
226         xfs_dqid_t      id,
227         uint            type,
228         xfs_buf_t       *bp)
229 {
230         struct xfs_quotainfo    *q = mp->m_quotainfo;
231         xfs_dqblk_t     *d;
232         xfs_dqid_t      curid;
233         int             i;
234
235         ASSERT(tp);
236         ASSERT(xfs_buf_islocked(bp));
237
238         d = bp->b_addr;
239
240         /*
241          * ID of the first dquot in the block - id's are zero based.
242          */
243         curid = id - (id % q->qi_dqperchunk);
244         memset(d, 0, BBTOB(q->qi_dqchunklen));
245         for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
246                 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
247                 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
248                 d->dd_diskdq.d_id = cpu_to_be32(curid);
249                 d->dd_diskdq.d_flags = type;
250                 if (xfs_sb_version_hascrc(&mp->m_sb)) {
251                         uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
252                         xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
253                                          XFS_DQUOT_CRC_OFF);
254                 }
255         }
256
257         xfs_trans_dquot_buf(tp, bp,
258                             (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
259                             ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
260                              XFS_BLF_GDQUOT_BUF)));
261         xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
262 }
263
264 /*
265  * Initialize the dynamic speculative preallocation thresholds. The lo/hi
266  * watermarks correspond to the soft and hard limits by default. If a soft limit
267  * is not specified, we use 95% of the hard limit.
268  */
269 void
270 xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
271 {
272         uint64_t space;
273
274         dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
275         dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
276         if (!dqp->q_prealloc_lo_wmark) {
277                 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
278                 do_div(dqp->q_prealloc_lo_wmark, 100);
279                 dqp->q_prealloc_lo_wmark *= 95;
280         }
281
282         space = dqp->q_prealloc_hi_wmark;
283
284         do_div(space, 100);
285         dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
286         dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
287         dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
288 }
289
290 /*
291  * Ensure that the given in-core dquot has a buffer on disk backing it, and
292  * return the buffer. This is called when the bmapi finds a hole.
293  */
294 STATIC int
295 xfs_dquot_disk_alloc(
296         struct xfs_trans        **tpp,
297         struct xfs_dquot        *dqp,
298         struct xfs_buf          **bpp)
299 {
300         struct xfs_bmbt_irec    map;
301         struct xfs_defer_ops    dfops;
302         struct xfs_mount        *mp = (*tpp)->t_mountp;
303         struct xfs_buf          *bp;
304         struct xfs_inode        *quotip = xfs_quota_inode(mp, dqp->dq_flags);
305         xfs_fsblock_t           firstblock;
306         int                     nmaps = 1;
307         int                     error;
308
309         trace_xfs_dqalloc(dqp);
310
311         xfs_defer_init(&dfops, &firstblock);
312         xfs_ilock(quotip, XFS_ILOCK_EXCL);
313         if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
314                 /*
315                  * Return if this type of quotas is turned off while we didn't
316                  * have an inode lock
317                  */
318                 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
319                 return -ESRCH;
320         }
321
322         /* Create the block mapping. */
323         xfs_trans_ijoin(*tpp, quotip, XFS_ILOCK_EXCL);
324         error = xfs_bmapi_write(*tpp, quotip, dqp->q_fileoffset,
325                         XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
326                         &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
327                         &map, &nmaps, &dfops);
328         if (error)
329                 goto error0;
330         ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
331         ASSERT(nmaps == 1);
332         ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
333                (map.br_startblock != HOLESTARTBLOCK));
334
335         /*
336          * Keep track of the blkno to save a lookup later
337          */
338         dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
339
340         /* now we can just get the buffer (there's nothing to read yet) */
341         bp = xfs_trans_get_buf(*tpp, mp->m_ddev_targp, dqp->q_blkno,
342                         mp->m_quotainfo->qi_dqchunklen, 0);
343         if (!bp) {
344                 error = -ENOMEM;
345                 goto error1;
346         }
347         bp->b_ops = &xfs_dquot_buf_ops;
348
349         /*
350          * Make a chunk of dquots out of this buffer and log
351          * the entire thing.
352          */
353         xfs_qm_init_dquot_blk(*tpp, mp, be32_to_cpu(dqp->q_core.d_id),
354                               dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
355         xfs_buf_set_ref(bp, XFS_DQUOT_REF);
356
357         /*
358          * Hold the buffer and join it to the dfops so that we'll still own
359          * the buffer when we return to the caller.  The buffer disposal on
360          * error must be paid attention to very carefully, as it has been
361          * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
362          * code when allocating a new dquot record" in 2005, and the later
363          * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
364          * the buffer locked across the _defer_finish call.  We can now do
365          * this correctly with xfs_defer_bjoin.
366          *
367          * Above, we allocated a disk block for the dquot information and
368          * used get_buf to initialize the dquot.  If the _defer_bjoin fails,
369          * the buffer is still locked to *tpp, so we must _bhold_release and
370          * then _trans_brelse the buffer.  If the _defer_finish fails, the old
371          * transaction is gone but the new buffer is not joined or held to any
372          * transaction, so we must _buf_relse it.
373          *
374          * If everything succeeds, the caller of this function is returned a
375          * buffer that is locked and held to the transaction.  The caller
376          * is responsible for unlocking any buffer passed back, either
377          * manually or by committing the transaction.
378          */
379         xfs_trans_bhold(*tpp, bp);
380         error = xfs_defer_bjoin(&dfops, bp);
381         if (error) {
382                 xfs_trans_bhold_release(*tpp, bp);
383                 xfs_trans_brelse(*tpp, bp);
384                 goto error1;
385         }
386         error = xfs_defer_finish(tpp, &dfops);
387         if (error) {
388                 xfs_buf_relse(bp);
389                 goto error1;
390         }
391         *bpp = bp;
392         return 0;
393
394 error1:
395         xfs_defer_cancel(&dfops);
396 error0:
397         return error;
398 }
399
400 /*
401  * Read in the in-core dquot's on-disk metadata and return the buffer.
402  * Returns ENOENT to signal a hole.
403  */
404 STATIC int
405 xfs_dquot_disk_read(
406         struct xfs_mount        *mp,
407         struct xfs_dquot        *dqp,
408         struct xfs_buf          **bpp)
409 {
410         struct xfs_bmbt_irec    map;
411         struct xfs_buf          *bp;
412         struct xfs_inode        *quotip = xfs_quota_inode(mp, dqp->dq_flags);
413         uint                    lock_mode;
414         int                     nmaps = 1;
415         int                     error;
416
417         lock_mode = xfs_ilock_data_map_shared(quotip);
418         if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
419                 /*
420                  * Return if this type of quotas is turned off while we
421                  * didn't have the quota inode lock.
422                  */
423                 xfs_iunlock(quotip, lock_mode);
424                 return -ESRCH;
425         }
426
427         /*
428          * Find the block map; no allocations yet
429          */
430         error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
431                         XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
432         xfs_iunlock(quotip, lock_mode);
433         if (error)
434                 return error;
435
436         ASSERT(nmaps == 1);
437         ASSERT(map.br_blockcount >= 1);
438         ASSERT(map.br_startblock != DELAYSTARTBLOCK);
439         if (map.br_startblock == HOLESTARTBLOCK)
440                 return -ENOENT;
441
442         trace_xfs_dqtobp_read(dqp);
443
444         /*
445          * store the blkno etc so that we don't have to do the
446          * mapping all the time
447          */
448         dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
449
450         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
451                         mp->m_quotainfo->qi_dqchunklen, 0, &bp,
452                         &xfs_dquot_buf_ops);
453         if (error) {
454                 ASSERT(bp == NULL);
455                 return error;
456         }
457
458         ASSERT(xfs_buf_islocked(bp));
459         xfs_buf_set_ref(bp, XFS_DQUOT_REF);
460         *bpp = bp;
461
462         return 0;
463 }
464
465 /* Allocate and initialize everything we need for an incore dquot. */
466 STATIC struct xfs_dquot *
467 xfs_dquot_alloc(
468         struct xfs_mount        *mp,
469         xfs_dqid_t              id,
470         uint                    type)
471 {
472         struct xfs_dquot        *dqp;
473
474         dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
475
476         dqp->dq_flags = type;
477         dqp->q_core.d_id = cpu_to_be32(id);
478         dqp->q_mount = mp;
479         INIT_LIST_HEAD(&dqp->q_lru);
480         mutex_init(&dqp->q_qlock);
481         init_waitqueue_head(&dqp->q_pinwait);
482         dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
483         /*
484          * Offset of dquot in the (fixed sized) dquot chunk.
485          */
486         dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
487                         sizeof(xfs_dqblk_t);
488
489         /*
490          * Because we want to use a counting completion, complete
491          * the flush completion once to allow a single access to
492          * the flush completion without blocking.
493          */
494         init_completion(&dqp->q_flush);
495         complete(&dqp->q_flush);
496
497         /*
498          * Make sure group quotas have a different lock class than user
499          * quotas.
500          */
501         switch (type) {
502         case XFS_DQ_USER:
503                 /* uses the default lock class */
504                 break;
505         case XFS_DQ_GROUP:
506                 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
507                 break;
508         case XFS_DQ_PROJ:
509                 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
510                 break;
511         default:
512                 ASSERT(0);
513                 break;
514         }
515
516         xfs_qm_dquot_logitem_init(dqp);
517
518         XFS_STATS_INC(mp, xs_qm_dquot);
519         return dqp;
520 }
521
522 /* Copy the in-core quota fields in from the on-disk buffer. */
523 STATIC void
524 xfs_dquot_from_disk(
525         struct xfs_dquot        *dqp,
526         struct xfs_buf          *bp)
527 {
528         struct xfs_disk_dquot   *ddqp = bp->b_addr + dqp->q_bufoffset;
529
530         /* copy everything from disk dquot to the incore dquot */
531         memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
532
533         /*
534          * Reservation counters are defined as reservation plus current usage
535          * to avoid having to add every time.
536          */
537         dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
538         dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
539         dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
540
541         /* initialize the dquot speculative prealloc thresholds */
542         xfs_dquot_set_prealloc_limits(dqp);
543 }
544
545 /* Allocate and initialize the dquot buffer for this in-core dquot. */
546 static int
547 xfs_qm_dqread_alloc(
548         struct xfs_mount        *mp,
549         struct xfs_dquot        *dqp,
550         struct xfs_buf          **bpp)
551 {
552         struct xfs_trans        *tp;
553         struct xfs_buf          *bp;
554         int                     error;
555
556         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
557                         XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
558         if (error)
559                 goto err;
560
561         error = xfs_dquot_disk_alloc(&tp, dqp, &bp);
562         if (error)
563                 goto err_cancel;
564
565         error = xfs_trans_commit(tp);
566         if (error) {
567                 /*
568                  * Buffer was held to the transaction, so we have to unlock it
569                  * manually here because we're not passing it back.
570                  */
571                 xfs_buf_relse(bp);
572                 goto err;
573         }
574         *bpp = bp;
575         return 0;
576
577 err_cancel:
578         xfs_trans_cancel(tp);
579 err:
580         return error;
581 }
582
583 /*
584  * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
585  * and release the buffer immediately.  If @can_alloc is true, fill any
586  * holes in the on-disk metadata.
587  */
588 static int
589 xfs_qm_dqread(
590         struct xfs_mount        *mp,
591         xfs_dqid_t              id,
592         uint                    type,
593         bool                    can_alloc,
594         struct xfs_dquot        **dqpp)
595 {
596         struct xfs_dquot        *dqp;
597         struct xfs_buf          *bp;
598         int                     error;
599
600         dqp = xfs_dquot_alloc(mp, id, type);
601         trace_xfs_dqread(dqp);
602
603         /* Try to read the buffer, allocating if necessary. */
604         error = xfs_dquot_disk_read(mp, dqp, &bp);
605         if (error == -ENOENT && can_alloc)
606                 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
607         if (error)
608                 goto err;
609
610         /*
611          * At this point we should have a clean locked buffer.  Copy the data
612          * to the incore dquot and release the buffer since the incore dquot
613          * has its own locking protocol so we needn't tie up the buffer any
614          * further.
615          */
616         ASSERT(xfs_buf_islocked(bp));
617         xfs_dquot_from_disk(dqp, bp);
618
619         xfs_buf_relse(bp);
620         *dqpp = dqp;
621         return error;
622
623 err:
624         trace_xfs_dqread_fail(dqp);
625         xfs_qm_dqdestroy(dqp);
626         *dqpp = NULL;
627         return error;
628 }
629
630 /*
631  * Advance to the next id in the current chunk, or if at the
632  * end of the chunk, skip ahead to first id in next allocated chunk
633  * using the SEEK_DATA interface.
634  */
635 static int
636 xfs_dq_get_next_id(
637         struct xfs_mount        *mp,
638         uint                    type,
639         xfs_dqid_t              *id)
640 {
641         struct xfs_inode        *quotip = xfs_quota_inode(mp, type);
642         xfs_dqid_t              next_id = *id + 1; /* simple advance */
643         uint                    lock_flags;
644         struct xfs_bmbt_irec    got;
645         struct xfs_iext_cursor  cur;
646         xfs_fsblock_t           start;
647         int                     error = 0;
648
649         /* If we'd wrap past the max ID, stop */
650         if (next_id < *id)
651                 return -ENOENT;
652
653         /* If new ID is within the current chunk, advancing it sufficed */
654         if (next_id % mp->m_quotainfo->qi_dqperchunk) {
655                 *id = next_id;
656                 return 0;
657         }
658
659         /* Nope, next_id is now past the current chunk, so find the next one */
660         start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
661
662         lock_flags = xfs_ilock_data_map_shared(quotip);
663         if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
664                 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
665                 if (error)
666                         return error;
667         }
668
669         if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
670                 /* contiguous chunk, bump startoff for the id calculation */
671                 if (got.br_startoff < start)
672                         got.br_startoff = start;
673                 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
674         } else {
675                 error = -ENOENT;
676         }
677
678         xfs_iunlock(quotip, lock_flags);
679
680         return error;
681 }
682
683 /*
684  * Look up the dquot in the in-core cache.  If found, the dquot is returned
685  * locked and ready to go.
686  */
687 static struct xfs_dquot *
688 xfs_qm_dqget_cache_lookup(
689         struct xfs_mount        *mp,
690         struct xfs_quotainfo    *qi,
691         struct radix_tree_root  *tree,
692         xfs_dqid_t              id)
693 {
694         struct xfs_dquot        *dqp;
695
696 restart:
697         mutex_lock(&qi->qi_tree_lock);
698         dqp = radix_tree_lookup(tree, id);
699         if (!dqp) {
700                 mutex_unlock(&qi->qi_tree_lock);
701                 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
702                 return NULL;
703         }
704
705         xfs_dqlock(dqp);
706         if (dqp->dq_flags & XFS_DQ_FREEING) {
707                 xfs_dqunlock(dqp);
708                 mutex_unlock(&qi->qi_tree_lock);
709                 trace_xfs_dqget_freeing(dqp);
710                 delay(1);
711                 goto restart;
712         }
713
714         dqp->q_nrefs++;
715         mutex_unlock(&qi->qi_tree_lock);
716
717         trace_xfs_dqget_hit(dqp);
718         XFS_STATS_INC(mp, xs_qm_dqcachehits);
719         return dqp;
720 }
721
722 /*
723  * Try to insert a new dquot into the in-core cache.  If an error occurs the
724  * caller should throw away the dquot and start over.  Otherwise, the dquot
725  * is returned locked (and held by the cache) as if there had been a cache
726  * hit.
727  */
728 static int
729 xfs_qm_dqget_cache_insert(
730         struct xfs_mount        *mp,
731         struct xfs_quotainfo    *qi,
732         struct radix_tree_root  *tree,
733         xfs_dqid_t              id,
734         struct xfs_dquot        *dqp)
735 {
736         int                     error;
737
738         mutex_lock(&qi->qi_tree_lock);
739         error = radix_tree_insert(tree, id, dqp);
740         if (unlikely(error)) {
741                 /* Duplicate found!  Caller must try again. */
742                 WARN_ON(error != -EEXIST);
743                 mutex_unlock(&qi->qi_tree_lock);
744                 trace_xfs_dqget_dup(dqp);
745                 return error;
746         }
747
748         /* Return a locked dquot to the caller, with a reference taken. */
749         xfs_dqlock(dqp);
750         dqp->q_nrefs = 1;
751
752         qi->qi_dquots++;
753         mutex_unlock(&qi->qi_tree_lock);
754
755         return 0;
756 }
757
758 /* Check our input parameters. */
759 static int
760 xfs_qm_dqget_checks(
761         struct xfs_mount        *mp,
762         uint                    type)
763 {
764         if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
765                 return -ESRCH;
766
767         switch (type) {
768         case XFS_DQ_USER:
769                 if (!XFS_IS_UQUOTA_ON(mp))
770                         return -ESRCH;
771                 return 0;
772         case XFS_DQ_GROUP:
773                 if (!XFS_IS_GQUOTA_ON(mp))
774                         return -ESRCH;
775                 return 0;
776         case XFS_DQ_PROJ:
777                 if (!XFS_IS_PQUOTA_ON(mp))
778                         return -ESRCH;
779                 return 0;
780         default:
781                 WARN_ON_ONCE(0);
782                 return -EINVAL;
783         }
784 }
785
786 /*
787  * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
788  * dquot, doing an allocation (if requested) as needed.
789  */
790 int
791 xfs_qm_dqget(
792         struct xfs_mount        *mp,
793         xfs_dqid_t              id,
794         uint                    type,
795         bool                    can_alloc,
796         struct xfs_dquot        **O_dqpp)
797 {
798         struct xfs_quotainfo    *qi = mp->m_quotainfo;
799         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
800         struct xfs_dquot        *dqp;
801         int                     error;
802
803         error = xfs_qm_dqget_checks(mp, type);
804         if (error)
805                 return error;
806
807 restart:
808         dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
809         if (dqp) {
810                 *O_dqpp = dqp;
811                 return 0;
812         }
813
814         error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
815         if (error)
816                 return error;
817
818         error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
819         if (error) {
820                 /*
821                  * Duplicate found. Just throw away the new dquot and start
822                  * over.
823                  */
824                 xfs_qm_dqdestroy(dqp);
825                 XFS_STATS_INC(mp, xs_qm_dquot_dups);
826                 goto restart;
827         }
828
829         trace_xfs_dqget_miss(dqp);
830         *O_dqpp = dqp;
831         return 0;
832 }
833
834 /*
835  * Given a dquot id and type, read and initialize a dquot from the on-disk
836  * metadata.  This function is only for use during quota initialization so
837  * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
838  * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
839  */
840 int
841 xfs_qm_dqget_uncached(
842         struct xfs_mount        *mp,
843         xfs_dqid_t              id,
844         uint                    type,
845         struct xfs_dquot        **dqpp)
846 {
847         int                     error;
848
849         error = xfs_qm_dqget_checks(mp, type);
850         if (error)
851                 return error;
852
853         return xfs_qm_dqread(mp, id, type, 0, dqpp);
854 }
855
856 /* Return the quota id for a given inode and type. */
857 xfs_dqid_t
858 xfs_qm_id_for_quotatype(
859         struct xfs_inode        *ip,
860         uint                    type)
861 {
862         switch (type) {
863         case XFS_DQ_USER:
864                 return ip->i_d.di_uid;
865         case XFS_DQ_GROUP:
866                 return ip->i_d.di_gid;
867         case XFS_DQ_PROJ:
868                 return xfs_get_projid(ip);
869         }
870         ASSERT(0);
871         return 0;
872 }
873
874 /*
875  * Return the dquot for a given inode and type.  If @can_alloc is true, then
876  * allocate blocks if needed.  The inode's ILOCK must be held and it must not
877  * have already had an inode attached.
878  */
879 int
880 xfs_qm_dqget_inode(
881         struct xfs_inode        *ip,
882         uint                    type,
883         bool                    can_alloc,
884         struct xfs_dquot        **O_dqpp)
885 {
886         struct xfs_mount        *mp = ip->i_mount;
887         struct xfs_quotainfo    *qi = mp->m_quotainfo;
888         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
889         struct xfs_dquot        *dqp;
890         xfs_dqid_t              id;
891         int                     error;
892
893         error = xfs_qm_dqget_checks(mp, type);
894         if (error)
895                 return error;
896
897         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
898         ASSERT(xfs_inode_dquot(ip, type) == NULL);
899
900         id = xfs_qm_id_for_quotatype(ip, type);
901
902 restart:
903         dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
904         if (dqp) {
905                 *O_dqpp = dqp;
906                 return 0;
907         }
908
909         /*
910          * Dquot cache miss. We don't want to keep the inode lock across
911          * a (potential) disk read. Also we don't want to deal with the lock
912          * ordering between quotainode and this inode. OTOH, dropping the inode
913          * lock here means dealing with a chown that can happen before
914          * we re-acquire the lock.
915          */
916         xfs_iunlock(ip, XFS_ILOCK_EXCL);
917         error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
918         xfs_ilock(ip, XFS_ILOCK_EXCL);
919         if (error)
920                 return error;
921
922         /*
923          * A dquot could be attached to this inode by now, since we had
924          * dropped the ilock.
925          */
926         if (xfs_this_quota_on(mp, type)) {
927                 struct xfs_dquot        *dqp1;
928
929                 dqp1 = xfs_inode_dquot(ip, type);
930                 if (dqp1) {
931                         xfs_qm_dqdestroy(dqp);
932                         dqp = dqp1;
933                         xfs_dqlock(dqp);
934                         goto dqret;
935                 }
936         } else {
937                 /* inode stays locked on return */
938                 xfs_qm_dqdestroy(dqp);
939                 return -ESRCH;
940         }
941
942         error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
943         if (error) {
944                 /*
945                  * Duplicate found. Just throw away the new dquot and start
946                  * over.
947                  */
948                 xfs_qm_dqdestroy(dqp);
949                 XFS_STATS_INC(mp, xs_qm_dquot_dups);
950                 goto restart;
951         }
952
953 dqret:
954         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
955         trace_xfs_dqget_miss(dqp);
956         *O_dqpp = dqp;
957         return 0;
958 }
959
960 /*
961  * Starting at @id and progressing upwards, look for an initialized incore
962  * dquot, lock it, and return it.
963  */
964 int
965 xfs_qm_dqget_next(
966         struct xfs_mount        *mp,
967         xfs_dqid_t              id,
968         uint                    type,
969         struct xfs_dquot        **dqpp)
970 {
971         struct xfs_dquot        *dqp;
972         int                     error = 0;
973
974         *dqpp = NULL;
975         for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
976                 error = xfs_qm_dqget(mp, id, type, false, &dqp);
977                 if (error == -ENOENT)
978                         continue;
979                 else if (error != 0)
980                         break;
981
982                 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
983                         *dqpp = dqp;
984                         return 0;
985                 }
986
987                 xfs_qm_dqput(dqp);
988         }
989
990         return error;
991 }
992
993 /*
994  * Release a reference to the dquot (decrement ref-count) and unlock it.
995  *
996  * If there is a group quota attached to this dquot, carefully release that
997  * too without tripping over deadlocks'n'stuff.
998  */
999 void
1000 xfs_qm_dqput(
1001         struct xfs_dquot        *dqp)
1002 {
1003         ASSERT(dqp->q_nrefs > 0);
1004         ASSERT(XFS_DQ_IS_LOCKED(dqp));
1005
1006         trace_xfs_dqput(dqp);
1007
1008         if (--dqp->q_nrefs == 0) {
1009                 struct xfs_quotainfo    *qi = dqp->q_mount->m_quotainfo;
1010                 trace_xfs_dqput_free(dqp);
1011
1012                 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
1013                         XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
1014         }
1015         xfs_dqunlock(dqp);
1016 }
1017
1018 /*
1019  * Release a dquot. Flush it if dirty, then dqput() it.
1020  * dquot must not be locked.
1021  */
1022 void
1023 xfs_qm_dqrele(
1024         xfs_dquot_t     *dqp)
1025 {
1026         if (!dqp)
1027                 return;
1028
1029         trace_xfs_dqrele(dqp);
1030
1031         xfs_dqlock(dqp);
1032         /*
1033          * We don't care to flush it if the dquot is dirty here.
1034          * That will create stutters that we want to avoid.
1035          * Instead we do a delayed write when we try to reclaim
1036          * a dirty dquot. Also xfs_sync will take part of the burden...
1037          */
1038         xfs_qm_dqput(dqp);
1039 }
1040
1041 /*
1042  * This is the dquot flushing I/O completion routine.  It is called
1043  * from interrupt level when the buffer containing the dquot is
1044  * flushed to disk.  It is responsible for removing the dquot logitem
1045  * from the AIL if it has not been re-logged, and unlocking the dquot's
1046  * flush lock. This behavior is very similar to that of inodes..
1047  */
1048 STATIC void
1049 xfs_qm_dqflush_done(
1050         struct xfs_buf          *bp,
1051         struct xfs_log_item     *lip)
1052 {
1053         xfs_dq_logitem_t        *qip = (struct xfs_dq_logitem *)lip;
1054         xfs_dquot_t             *dqp = qip->qli_dquot;
1055         struct xfs_ail          *ailp = lip->li_ailp;
1056
1057         /*
1058          * We only want to pull the item from the AIL if its
1059          * location in the log has not changed since we started the flush.
1060          * Thus, we only bother if the dquot's lsn has
1061          * not changed. First we check the lsn outside the lock
1062          * since it's cheaper, and then we recheck while
1063          * holding the lock before removing the dquot from the AIL.
1064          */
1065         if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
1066             ((lip->li_lsn == qip->qli_flush_lsn) ||
1067              test_bit(XFS_LI_FAILED, &lip->li_flags))) {
1068
1069                 /* xfs_trans_ail_delete() drops the AIL lock. */
1070                 spin_lock(&ailp->ail_lock);
1071                 if (lip->li_lsn == qip->qli_flush_lsn) {
1072                         xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
1073                 } else {
1074                         /*
1075                          * Clear the failed state since we are about to drop the
1076                          * flush lock
1077                          */
1078                         xfs_clear_li_failed(lip);
1079                         spin_unlock(&ailp->ail_lock);
1080                 }
1081         }
1082
1083         /*
1084          * Release the dq's flush lock since we're done with it.
1085          */
1086         xfs_dqfunlock(dqp);
1087 }
1088
1089 /*
1090  * Write a modified dquot to disk.
1091  * The dquot must be locked and the flush lock too taken by caller.
1092  * The flush lock will not be unlocked until the dquot reaches the disk,
1093  * but the dquot is free to be unlocked and modified by the caller
1094  * in the interim. Dquot is still locked on return. This behavior is
1095  * identical to that of inodes.
1096  */
1097 int
1098 xfs_qm_dqflush(
1099         struct xfs_dquot        *dqp,
1100         struct xfs_buf          **bpp)
1101 {
1102         struct xfs_mount        *mp = dqp->q_mount;
1103         struct xfs_buf          *bp;
1104         struct xfs_dqblk        *dqb;
1105         struct xfs_disk_dquot   *ddqp;
1106         xfs_failaddr_t          fa;
1107         int                     error;
1108
1109         ASSERT(XFS_DQ_IS_LOCKED(dqp));
1110         ASSERT(!completion_done(&dqp->q_flush));
1111
1112         trace_xfs_dqflush(dqp);
1113
1114         *bpp = NULL;
1115
1116         xfs_qm_dqunpin_wait(dqp);
1117
1118         /*
1119          * This may have been unpinned because the filesystem is shutting
1120          * down forcibly. If that's the case we must not write this dquot
1121          * to disk, because the log record didn't make it to disk.
1122          *
1123          * We also have to remove the log item from the AIL in this case,
1124          * as we wait for an emptry AIL as part of the unmount process.
1125          */
1126         if (XFS_FORCED_SHUTDOWN(mp)) {
1127                 struct xfs_log_item     *lip = &dqp->q_logitem.qli_item;
1128                 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1129
1130                 xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);
1131
1132                 error = -EIO;
1133                 goto out_unlock;
1134         }
1135
1136         /*
1137          * Get the buffer containing the on-disk dquot
1138          */
1139         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1140                                    mp->m_quotainfo->qi_dqchunklen, 0, &bp,
1141                                    &xfs_dquot_buf_ops);
1142         if (error)
1143                 goto out_unlock;
1144
1145         /*
1146          * Calculate the location of the dquot inside the buffer.
1147          */
1148         dqb = bp->b_addr + dqp->q_bufoffset;
1149         ddqp = &dqb->dd_diskdq;
1150
1151         /*
1152          * A simple sanity check in case we got a corrupted dquot.
1153          */
1154         fa = xfs_dqblk_verify(mp, dqb, be32_to_cpu(ddqp->d_id), 0);
1155         if (fa) {
1156                 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1157                                 be32_to_cpu(ddqp->d_id), fa);
1158                 xfs_buf_relse(bp);
1159                 xfs_dqfunlock(dqp);
1160                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1161                 return -EIO;
1162         }
1163
1164         /* This is the only portion of data that needs to persist */
1165         memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
1166
1167         /*
1168          * Clear the dirty field and remember the flush lsn for later use.
1169          */
1170         dqp->dq_flags &= ~XFS_DQ_DIRTY;
1171
1172         xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1173                                         &dqp->q_logitem.qli_item.li_lsn);
1174
1175         /*
1176          * copy the lsn into the on-disk dquot now while we have the in memory
1177          * dquot here. This can't be done later in the write verifier as we
1178          * can't get access to the log item at that point in time.
1179          *
1180          * We also calculate the CRC here so that the on-disk dquot in the
1181          * buffer always has a valid CRC. This ensures there is no possibility
1182          * of a dquot without an up-to-date CRC getting to disk.
1183          */
1184         if (xfs_sb_version_hascrc(&mp->m_sb)) {
1185                 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
1186                 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1187                                  XFS_DQUOT_CRC_OFF);
1188         }
1189
1190         /*
1191          * Attach an iodone routine so that we can remove this dquot from the
1192          * AIL and release the flush lock once the dquot is synced to disk.
1193          */
1194         xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1195                                   &dqp->q_logitem.qli_item);
1196
1197         /*
1198          * If the buffer is pinned then push on the log so we won't
1199          * get stuck waiting in the write for too long.
1200          */
1201         if (xfs_buf_ispinned(bp)) {
1202                 trace_xfs_dqflush_force(dqp);
1203                 xfs_log_force(mp, 0);
1204         }
1205
1206         trace_xfs_dqflush_done(dqp);
1207         *bpp = bp;
1208         return 0;
1209
1210 out_unlock:
1211         xfs_dqfunlock(dqp);
1212         return -EIO;
1213 }
1214
1215 /*
1216  * Lock two xfs_dquot structures.
1217  *
1218  * To avoid deadlocks we always lock the quota structure with
1219  * the lowerd id first.
1220  */
1221 void
1222 xfs_dqlock2(
1223         xfs_dquot_t     *d1,
1224         xfs_dquot_t     *d2)
1225 {
1226         if (d1 && d2) {
1227                 ASSERT(d1 != d2);
1228                 if (be32_to_cpu(d1->q_core.d_id) >
1229                     be32_to_cpu(d2->q_core.d_id)) {
1230                         mutex_lock(&d2->q_qlock);
1231                         mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1232                 } else {
1233                         mutex_lock(&d1->q_qlock);
1234                         mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1235                 }
1236         } else if (d1) {
1237                 mutex_lock(&d1->q_qlock);
1238         } else if (d2) {
1239                 mutex_lock(&d2->q_qlock);
1240         }
1241 }
1242
1243 int __init
1244 xfs_qm_init(void)
1245 {
1246         xfs_qm_dqzone =
1247                 kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
1248         if (!xfs_qm_dqzone)
1249                 goto out;
1250
1251         xfs_qm_dqtrxzone =
1252                 kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
1253         if (!xfs_qm_dqtrxzone)
1254                 goto out_free_dqzone;
1255
1256         return 0;
1257
1258 out_free_dqzone:
1259         kmem_zone_destroy(xfs_qm_dqzone);
1260 out:
1261         return -ENOMEM;
1262 }
1263
1264 void
1265 xfs_qm_exit(void)
1266 {
1267         kmem_zone_destroy(xfs_qm_dqtrxzone);
1268         kmem_zone_destroy(xfs_qm_dqzone);
1269 }
1270
1271 /*
1272  * Iterate every dquot of a particular type.  The caller must ensure that the
1273  * particular quota type is active.  iter_fn can return negative error codes,
1274  * or XFS_BTREE_QUERY_RANGE_ABORT to indicate that it wants to stop iterating.
1275  */
1276 int
1277 xfs_qm_dqiterate(
1278         struct xfs_mount        *mp,
1279         uint                    dqtype,
1280         xfs_qm_dqiterate_fn     iter_fn,
1281         void                    *priv)
1282 {
1283         struct xfs_dquot        *dq;
1284         xfs_dqid_t              id = 0;
1285         int                     error;
1286
1287         do {
1288                 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1289                 if (error == -ENOENT)
1290                         return 0;
1291                 if (error)
1292                         return error;
1293
1294                 error = iter_fn(dq, dqtype, priv);
1295                 id = be32_to_cpu(dq->q_core.d_id);
1296                 xfs_qm_dqput(dq);
1297                 id++;
1298         } while (error == 0 && id != 0);
1299
1300         return error;
1301 }