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