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