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