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-2005 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" |
| 40 | #include "xfs_itable.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_rtalloc.h" |
| 42 | #include "xfs_error.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 43 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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_utils.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 | * The global quota manager. There is only one of these for the entire |
| 54 | * system, _not_ one per file system. XQM keeps track of the overall |
| 55 | * quota functionality, including maintaining the freelist and hash |
| 56 | * tables of dquots. |
| 57 | */ |
Christoph Hellwig | a0b0b8a | 2009-03-29 09:51:00 +0200 | [diff] [blame] | 58 | struct mutex xfs_Gqm_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct xfs_qm *xfs_Gqm; |
Nathan Scott | 6b3f6b5 | 2005-11-02 15:08:25 +1100 | [diff] [blame] | 60 | uint ndquot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | kmem_zone_t *qm_dqzone; |
| 63 | kmem_zone_t *qm_dqtrxzone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 65 | static cred_t xfs_zerocr; |
Nathan Scott | dae81d4 | 2006-02-28 12:30:13 +1100 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int); |
| 68 | STATIC void xfs_qm_list_destroy(xfs_dqlist_t *); |
| 69 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 70 | STATIC void xfs_qm_freelist_init(xfs_frlist_t *); |
| 71 | STATIC void xfs_qm_freelist_destroy(xfs_frlist_t *); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | STATIC int xfs_qm_init_quotainos(xfs_mount_t *); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 74 | STATIC int xfs_qm_init_quotainfo(xfs_mount_t *); |
Al Viro | 51bfb75 | 2005-12-15 09:17:24 +0000 | [diff] [blame] | 75 | STATIC int xfs_qm_shake(int, gfp_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 77 | static struct shrinker xfs_qm_shaker = { |
| 78 | .shrink = xfs_qm_shake, |
| 79 | .seeks = DEFAULT_SEEKS, |
| 80 | }; |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #ifdef DEBUG |
Christoph Hellwig | a0b0b8a | 2009-03-29 09:51:00 +0200 | [diff] [blame] | 83 | extern struct mutex qcheck_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | #ifdef QUOTADEBUG |
| 87 | #define XQM_LIST_PRINT(l, NXT, title) \ |
| 88 | { \ |
| 89 | xfs_dquot_t *dqp; int i = 0; \ |
| 90 | cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \ |
| 91 | for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \ |
| 92 | cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \ |
| 93 | "bcnt = %d, icnt = %d, refs = %d", \ |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 94 | ++i, (int) be32_to_cpu(dqp->q_core.d_id), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | DQFLAGTO_TYPESTR(dqp), \ |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 96 | (int) be64_to_cpu(dqp->q_core.d_bcount), \ |
| 97 | (int) be64_to_cpu(dqp->q_core.d_icount), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | (int) dqp->q_nrefs); } \ |
| 99 | } |
| 100 | #else |
| 101 | #define XQM_LIST_PRINT(l, NXT, title) do { } while (0) |
| 102 | #endif |
| 103 | |
| 104 | /* |
| 105 | * Initialize the XQM structure. |
| 106 | * Note that there is not one quota manager per file system. |
| 107 | */ |
| 108 | STATIC struct xfs_qm * |
| 109 | xfs_Gqm_init(void) |
| 110 | { |
Nathan Scott | 6b3f6b5 | 2005-11-02 15:08:25 +1100 | [diff] [blame] | 111 | xfs_dqhash_t *udqhash, *gdqhash; |
| 112 | xfs_qm_t *xqm; |
Nathan Scott | 215101c | 2006-09-28 11:04:43 +1000 | [diff] [blame] | 113 | size_t hsize; |
| 114 | uint i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Initialize the dquot hash tables. |
| 118 | */ |
Nathan Scott | 77e4635 | 2006-09-28 11:03:27 +1000 | [diff] [blame] | 119 | udqhash = kmem_zalloc_greedy(&hsize, |
Eric Sandeen | 5995cb7 | 2007-08-16 16:49:11 +1000 | [diff] [blame] | 120 | XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t), |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 121 | XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t)); |
| 122 | if (!udqhash) |
| 123 | goto out; |
| 124 | |
| 125 | gdqhash = kmem_zalloc_large(hsize); |
| 126 | if (!udqhash) |
| 127 | goto out_free_udqhash; |
| 128 | |
Nathan Scott | 77e4635 | 2006-09-28 11:03:27 +1000 | [diff] [blame] | 129 | hsize /= sizeof(xfs_dqhash_t); |
Nathan Scott | 6b3f6b5 | 2005-11-02 15:08:25 +1100 | [diff] [blame] | 130 | ndquot = hsize << 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Nathan Scott | 6b3f6b5 | 2005-11-02 15:08:25 +1100 | [diff] [blame] | 132 | xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP); |
| 133 | xqm->qm_dqhashmask = hsize - 1; |
| 134 | xqm->qm_usr_dqhtable = udqhash; |
| 135 | xqm->qm_grp_dqhtable = gdqhash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | ASSERT(xqm->qm_usr_dqhtable != NULL); |
| 137 | ASSERT(xqm->qm_grp_dqhtable != NULL); |
| 138 | |
| 139 | for (i = 0; i < hsize; i++) { |
| 140 | xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i); |
| 141 | xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Freelist of all dquots of all file systems |
| 146 | */ |
| 147 | xfs_qm_freelist_init(&(xqm->qm_dqfreelist)); |
| 148 | |
| 149 | /* |
| 150 | * dquot zone. we register our own low-memory callback. |
| 151 | */ |
| 152 | if (!qm_dqzone) { |
| 153 | xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t), |
| 154 | "xfs_dquots"); |
| 155 | qm_dqzone = xqm->qm_dqzone; |
| 156 | } else |
| 157 | xqm->qm_dqzone = qm_dqzone; |
| 158 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 159 | register_shrinker(&xfs_qm_shaker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * The t_dqinfo portion of transactions. |
| 163 | */ |
| 164 | if (!qm_dqtrxzone) { |
| 165 | xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t), |
| 166 | "xfs_dqtrx"); |
| 167 | qm_dqtrxzone = xqm->qm_dqtrxzone; |
| 168 | } else |
| 169 | xqm->qm_dqtrxzone = qm_dqtrxzone; |
| 170 | |
| 171 | atomic_set(&xqm->qm_totaldquots, 0); |
| 172 | xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO; |
| 173 | xqm->qm_nrefs = 0; |
| 174 | #ifdef DEBUG |
Nathan Scott | c2e8143 | 2006-01-16 16:21:34 +1100 | [diff] [blame] | 175 | mutex_init(&qcheck_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | #endif |
| 177 | return xqm; |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 178 | |
| 179 | out_free_udqhash: |
| 180 | kmem_free_large(udqhash); |
| 181 | out: |
| 182 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Destroy the global quota manager when its reference count goes to zero. |
| 187 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 188 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | xfs_qm_destroy( |
| 190 | struct xfs_qm *xqm) |
| 191 | { |
| 192 | int hsize, i; |
| 193 | |
| 194 | ASSERT(xqm != NULL); |
| 195 | ASSERT(xqm->qm_nrefs == 0); |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 196 | unregister_shrinker(&xfs_qm_shaker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | hsize = xqm->qm_dqhashmask + 1; |
| 198 | for (i = 0; i < hsize; i++) { |
| 199 | xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i])); |
| 200 | xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i])); |
| 201 | } |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 202 | kmem_free_large(xqm->qm_usr_dqhtable); |
| 203 | kmem_free_large(xqm->qm_grp_dqhtable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | xqm->qm_usr_dqhtable = NULL; |
| 205 | xqm->qm_grp_dqhtable = NULL; |
| 206 | xqm->qm_dqhashmask = 0; |
| 207 | xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist)); |
| 208 | #ifdef DEBUG |
| 209 | mutex_destroy(&qcheck_lock); |
| 210 | #endif |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 211 | kmem_free(xqm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Called at mount time to let XQM know that another file system is |
| 216 | * starting quotas. This isn't crucial information as the individual mount |
| 217 | * structures are pretty independent, but it helps the XQM keep a |
| 218 | * global view of what's going on. |
| 219 | */ |
| 220 | /* ARGSUSED */ |
| 221 | STATIC int |
| 222 | xfs_qm_hold_quotafs_ref( |
| 223 | struct xfs_mount *mp) |
| 224 | { |
| 225 | /* |
| 226 | * Need to lock the xfs_Gqm structure for things like this. For example, |
| 227 | * the structure could disappear between the entry to this routine and |
| 228 | * a HOLD operation if not locked. |
| 229 | */ |
Christoph Hellwig | e249458 | 2009-02-09 08:38:39 +0100 | [diff] [blame] | 230 | mutex_lock(&xfs_Gqm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 232 | if (!xfs_Gqm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | xfs_Gqm = xfs_Gqm_init(); |
Christoph Hellwig | bdfb043 | 2010-01-20 21:55:30 +0000 | [diff] [blame] | 234 | if (!xfs_Gqm) |
| 235 | return ENOMEM; |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* |
| 239 | * We can keep a list of all filesystems with quotas mounted for |
| 240 | * debugging and statistical purposes, but ... |
| 241 | * Just take a reference and get out. |
| 242 | */ |
Christoph Hellwig | e249458 | 2009-02-09 08:38:39 +0100 | [diff] [blame] | 243 | xfs_Gqm->qm_nrefs++; |
| 244 | mutex_unlock(&xfs_Gqm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /* |
| 251 | * Release the reference that a filesystem took at mount time, |
| 252 | * so that we know when we need to destroy the entire quota manager. |
| 253 | */ |
| 254 | /* ARGSUSED */ |
| 255 | STATIC void |
| 256 | xfs_qm_rele_quotafs_ref( |
| 257 | struct xfs_mount *mp) |
| 258 | { |
| 259 | xfs_dquot_t *dqp, *nextdqp; |
| 260 | |
| 261 | ASSERT(xfs_Gqm); |
| 262 | ASSERT(xfs_Gqm->qm_nrefs > 0); |
| 263 | |
| 264 | /* |
| 265 | * Go thru the freelist and destroy all inactive dquots. |
| 266 | */ |
| 267 | xfs_qm_freelist_lock(xfs_Gqm); |
| 268 | |
| 269 | for (dqp = xfs_Gqm->qm_dqfreelist.qh_next; |
| 270 | dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) { |
| 271 | xfs_dqlock(dqp); |
| 272 | nextdqp = dqp->dq_flnext; |
| 273 | if (dqp->dq_flags & XFS_DQ_INACTIVE) { |
| 274 | ASSERT(dqp->q_mount == NULL); |
| 275 | ASSERT(! XFS_DQ_IS_DIRTY(dqp)); |
| 276 | ASSERT(dqp->HL_PREVP == NULL); |
| 277 | ASSERT(dqp->MPL_PREVP == NULL); |
| 278 | XQM_FREELIST_REMOVE(dqp); |
| 279 | xfs_dqunlock(dqp); |
| 280 | xfs_qm_dqdestroy(dqp); |
| 281 | } else { |
| 282 | xfs_dqunlock(dqp); |
| 283 | } |
| 284 | dqp = nextdqp; |
| 285 | } |
| 286 | xfs_qm_freelist_unlock(xfs_Gqm); |
| 287 | |
| 288 | /* |
| 289 | * Destroy the entire XQM. If somebody mounts with quotaon, this'll |
| 290 | * be restarted. |
| 291 | */ |
Christoph Hellwig | e249458 | 2009-02-09 08:38:39 +0100 | [diff] [blame] | 292 | mutex_lock(&xfs_Gqm_lock); |
| 293 | if (--xfs_Gqm->qm_nrefs == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | xfs_qm_destroy(xfs_Gqm); |
| 295 | xfs_Gqm = NULL; |
| 296 | } |
Christoph Hellwig | e249458 | 2009-02-09 08:38:39 +0100 | [diff] [blame] | 297 | mutex_unlock(&xfs_Gqm_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | * Just destroy the quotainfo structure. |
| 302 | */ |
| 303 | void |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 304 | xfs_qm_unmount( |
| 305 | struct xfs_mount *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 307 | if (mp->m_quotainfo) { |
| 308 | xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | xfs_qm_destroy_quotainfo(mp); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
| 314 | /* |
| 315 | * This is called from xfs_mountfs to start quotas and initialize all |
| 316 | * necessary data structures like quotainfo. This is also responsible for |
| 317 | * running a quotacheck as necessary. We are guaranteed that the superblock |
| 318 | * is consistently read in at this point. |
David Chinner | 53aa791 | 2008-04-10 12:20:31 +1000 | [diff] [blame] | 319 | * |
| 320 | * If we fail here, the mount will continue with quota turned off. We don't |
| 321 | * need to inidicate success or failure at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | */ |
David Chinner | 53aa791 | 2008-04-10 12:20:31 +1000 | [diff] [blame] | 323 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | xfs_qm_mount_quotas( |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 325 | xfs_mount_t *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | int error = 0; |
| 328 | uint sbf; |
| 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | /* |
| 331 | * If quotas on realtime volumes is not supported, we disable |
| 332 | * quotas immediately. |
| 333 | */ |
| 334 | if (mp->m_sb.sb_rextents) { |
| 335 | cmn_err(CE_NOTE, |
| 336 | "Cannot turn on quotas for realtime filesystem %s", |
| 337 | mp->m_fsname); |
| 338 | mp->m_qflags = 0; |
| 339 | goto write_changes; |
| 340 | } |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
Nathan Scott | 155ffd0 | 2005-09-02 16:43:48 +1000 | [diff] [blame] | 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | /* |
| 345 | * Allocate the quotainfo structure inside the mount struct, and |
| 346 | * create quotainode(s), and change/rev superblock if necessary. |
| 347 | */ |
David Chinner | 53aa791 | 2008-04-10 12:20:31 +1000 | [diff] [blame] | 348 | error = xfs_qm_init_quotainfo(mp); |
| 349 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | /* |
| 351 | * We must turn off quotas. |
| 352 | */ |
| 353 | ASSERT(mp->m_quotainfo == NULL); |
| 354 | mp->m_qflags = 0; |
| 355 | goto write_changes; |
| 356 | } |
| 357 | /* |
| 358 | * If any of the quotas are not consistent, do a quotacheck. |
| 359 | */ |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 360 | if (XFS_QM_NEED_QUOTACHECK(mp)) { |
David Chinner | 53aa791 | 2008-04-10 12:20:31 +1000 | [diff] [blame] | 361 | error = xfs_qm_quotacheck(mp); |
| 362 | if (error) { |
| 363 | /* Quotacheck failed and disabled quotas. */ |
| 364 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
Donald Douwsma | 646d5bd | 2007-05-08 13:49:09 +1000 | [diff] [blame] | 367 | /* |
| 368 | * If one type of quotas is off, then it will lose its |
| 369 | * quotachecked status, since we won't be doing accounting for |
| 370 | * that type anymore. |
| 371 | */ |
David Chinner | 53aa791 | 2008-04-10 12:20:31 +1000 | [diff] [blame] | 372 | if (!XFS_IS_UQUOTA_ON(mp)) |
Donald Douwsma | 646d5bd | 2007-05-08 13:49:09 +1000 | [diff] [blame] | 373 | mp->m_qflags &= ~XFS_UQUOTA_CHKD; |
David Chinner | 53aa791 | 2008-04-10 12:20:31 +1000 | [diff] [blame] | 374 | if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp))) |
Donald Douwsma | 646d5bd | 2007-05-08 13:49:09 +1000 | [diff] [blame] | 375 | mp->m_qflags &= ~XFS_OQUOTA_CHKD; |
Nathan Scott | 155ffd0 | 2005-09-02 16:43:48 +1000 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | write_changes: |
| 378 | /* |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 379 | * We actually don't have to acquire the m_sb_lock at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * This can only be called from mount, and that's single threaded. XXX |
| 381 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 382 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | sbf = mp->m_sb.sb_qflags; |
| 384 | mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 385 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) { |
| 388 | if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) { |
| 389 | /* |
| 390 | * We could only have been turning quotas off. |
| 391 | * We aren't in very good shape actually because |
| 392 | * the incore structures are convinced that quotas are |
| 393 | * off, but the on disk superblock doesn't know that ! |
| 394 | */ |
| 395 | ASSERT(!(XFS_IS_QUOTA_RUNNING(mp))); |
| 396 | xfs_fs_cmn_err(CE_ALERT, mp, |
| 397 | "XFS mount_quotas: Superblock update failed!"); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (error) { |
| 402 | xfs_fs_cmn_err(CE_WARN, mp, |
| 403 | "Failed to initialize disk quotas."); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 404 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 406 | |
| 407 | #ifdef QUOTADEBUG |
| 408 | if (XFS_IS_QUOTA_ON(mp)) |
| 409 | xfs_qm_internalqcheck(mp); |
| 410 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Called from the vfsops layer. |
| 415 | */ |
Christoph Hellwig | e57481d | 2008-12-03 12:20:36 +0100 | [diff] [blame] | 416 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | xfs_qm_unmount_quotas( |
| 418 | xfs_mount_t *mp) |
| 419 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* |
| 421 | * Release the dquots that root inode, et al might be holding, |
| 422 | * before we flush quotas and blow away the quotainfo structure. |
| 423 | */ |
| 424 | ASSERT(mp->m_rootip); |
| 425 | xfs_qm_dqdetach(mp->m_rootip); |
| 426 | if (mp->m_rbmip) |
| 427 | xfs_qm_dqdetach(mp->m_rbmip); |
| 428 | if (mp->m_rsumip) |
| 429 | xfs_qm_dqdetach(mp->m_rsumip); |
| 430 | |
| 431 | /* |
Christoph Hellwig | e57481d | 2008-12-03 12:20:36 +0100 | [diff] [blame] | 432 | * Release the quota inodes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (mp->m_quotainfo) { |
Christoph Hellwig | e57481d | 2008-12-03 12:20:36 +0100 | [diff] [blame] | 435 | if (mp->m_quotainfo->qi_uquotaip) { |
| 436 | IRELE(mp->m_quotainfo->qi_uquotaip); |
| 437 | mp->m_quotainfo->qi_uquotaip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
Christoph Hellwig | e57481d | 2008-12-03 12:20:36 +0100 | [diff] [blame] | 439 | if (mp->m_quotainfo->qi_gquotaip) { |
| 440 | IRELE(mp->m_quotainfo->qi_gquotaip); |
| 441 | mp->m_quotainfo->qi_gquotaip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Flush all dquots of the given file system to disk. The dquots are |
| 448 | * _not_ purged from memory here, just their data written to disk. |
| 449 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 450 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | xfs_qm_dqflush_all( |
| 452 | xfs_mount_t *mp, |
| 453 | int flags) |
| 454 | { |
| 455 | int recl; |
| 456 | xfs_dquot_t *dqp; |
| 457 | int niters; |
| 458 | int error; |
| 459 | |
| 460 | if (mp->m_quotainfo == NULL) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 461 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | niters = 0; |
| 463 | again: |
| 464 | xfs_qm_mplist_lock(mp); |
| 465 | FOREACH_DQUOT_IN_MP(dqp, mp) { |
| 466 | xfs_dqlock(dqp); |
| 467 | if (! XFS_DQ_IS_DIRTY(dqp)) { |
| 468 | xfs_dqunlock(dqp); |
| 469 | continue; |
| 470 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* XXX a sentinel would be better */ |
| 473 | recl = XFS_QI_MPLRECLAIMS(mp); |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 474 | if (!xfs_dqflock_nowait(dqp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | /* |
| 476 | * If we can't grab the flush lock then check |
| 477 | * to see if the dquot has been flushed delayed |
| 478 | * write. If so, grab its buffer and send it |
| 479 | * out immediately. We'll be able to acquire |
| 480 | * the flush lock when the I/O completes. |
| 481 | */ |
| 482 | xfs_qm_dqflock_pushbuf_wait(dqp); |
| 483 | } |
| 484 | /* |
| 485 | * Let go of the mplist lock. We don't want to hold it |
| 486 | * across a disk write. |
| 487 | */ |
| 488 | xfs_qm_mplist_unlock(mp); |
| 489 | error = xfs_qm_dqflush(dqp, flags); |
| 490 | xfs_dqunlock(dqp); |
| 491 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 492 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | xfs_qm_mplist_lock(mp); |
| 495 | if (recl != XFS_QI_MPLRECLAIMS(mp)) { |
| 496 | xfs_qm_mplist_unlock(mp); |
| 497 | /* XXX restart limit */ |
| 498 | goto again; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | xfs_qm_mplist_unlock(mp); |
| 503 | /* return ! busy */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 504 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | /* |
| 507 | * Release the group dquot pointers the user dquots may be |
| 508 | * carrying around as a hint. mplist is locked on entry and exit. |
| 509 | */ |
| 510 | STATIC void |
| 511 | xfs_qm_detach_gdquots( |
| 512 | xfs_mount_t *mp) |
| 513 | { |
| 514 | xfs_dquot_t *dqp, *gdqp; |
| 515 | int nrecl; |
| 516 | |
| 517 | again: |
| 518 | ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp)); |
| 519 | dqp = XFS_QI_MPLNEXT(mp); |
| 520 | while (dqp) { |
| 521 | xfs_dqlock(dqp); |
| 522 | if ((gdqp = dqp->q_gdquot)) { |
| 523 | xfs_dqlock(gdqp); |
| 524 | dqp->q_gdquot = NULL; |
| 525 | } |
| 526 | xfs_dqunlock(dqp); |
| 527 | |
| 528 | if (gdqp) { |
| 529 | /* |
| 530 | * Can't hold the mplist lock across a dqput. |
| 531 | * XXXmust convert to marker based iterations here. |
| 532 | */ |
| 533 | nrecl = XFS_QI_MPLRECLAIMS(mp); |
| 534 | xfs_qm_mplist_unlock(mp); |
| 535 | xfs_qm_dqput(gdqp); |
| 536 | |
| 537 | xfs_qm_mplist_lock(mp); |
| 538 | if (nrecl != XFS_QI_MPLRECLAIMS(mp)) |
| 539 | goto again; |
| 540 | } |
| 541 | dqp = dqp->MPL_NEXT; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Go through all the incore dquots of this file system and take them |
| 547 | * off the mplist and hashlist, if the dquot type matches the dqtype |
| 548 | * parameter. This is used when turning off quota accounting for |
| 549 | * users and/or groups, as well as when the filesystem is unmounting. |
| 550 | */ |
| 551 | STATIC int |
| 552 | xfs_qm_dqpurge_int( |
| 553 | xfs_mount_t *mp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 554 | uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
| 556 | xfs_dquot_t *dqp; |
| 557 | uint dqtype; |
| 558 | int nrecl; |
| 559 | xfs_dquot_t *nextdqp; |
| 560 | int nmisses; |
| 561 | |
| 562 | if (mp->m_quotainfo == NULL) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 563 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 566 | dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0; |
| 568 | |
| 569 | xfs_qm_mplist_lock(mp); |
| 570 | |
| 571 | /* |
| 572 | * In the first pass through all incore dquots of this filesystem, |
| 573 | * we release the group dquot pointers the user dquots may be |
| 574 | * carrying around as a hint. We need to do this irrespective of |
| 575 | * what's being turned off. |
| 576 | */ |
| 577 | xfs_qm_detach_gdquots(mp); |
| 578 | |
| 579 | again: |
| 580 | nmisses = 0; |
| 581 | ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp)); |
| 582 | /* |
| 583 | * Try to get rid of all of the unwanted dquots. The idea is to |
| 584 | * get them off mplist and hashlist, but leave them on freelist. |
| 585 | */ |
| 586 | dqp = XFS_QI_MPLNEXT(mp); |
| 587 | while (dqp) { |
| 588 | /* |
| 589 | * It's OK to look at the type without taking dqlock here. |
| 590 | * We're holding the mplist lock here, and that's needed for |
| 591 | * a dqreclaim. |
| 592 | */ |
| 593 | if ((dqp->dq_flags & dqtype) == 0) { |
| 594 | dqp = dqp->MPL_NEXT; |
| 595 | continue; |
| 596 | } |
| 597 | |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 598 | if (!mutex_trylock(&dqp->q_hash->qh_lock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | nrecl = XFS_QI_MPLRECLAIMS(mp); |
| 600 | xfs_qm_mplist_unlock(mp); |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 601 | mutex_lock(&dqp->q_hash->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | xfs_qm_mplist_lock(mp); |
| 603 | |
| 604 | /* |
| 605 | * XXXTheoretically, we can get into a very long |
| 606 | * ping pong game here. |
| 607 | * No one can be adding dquots to the mplist at |
| 608 | * this point, but somebody might be taking things off. |
| 609 | */ |
| 610 | if (nrecl != XFS_QI_MPLRECLAIMS(mp)) { |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 611 | mutex_unlock(&dqp->q_hash->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | goto again; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Take the dquot off the mplist and hashlist. It may remain on |
| 618 | * freelist in INACTIVE state. |
| 619 | */ |
| 620 | nextdqp = dqp->MPL_NEXT; |
Denys Vlasenko | 4f0e8a9 | 2008-05-19 16:34:04 +1000 | [diff] [blame] | 621 | nmisses += xfs_qm_dqpurge(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | dqp = nextdqp; |
| 623 | } |
| 624 | xfs_qm_mplist_unlock(mp); |
| 625 | return nmisses; |
| 626 | } |
| 627 | |
| 628 | int |
| 629 | xfs_qm_dqpurge_all( |
| 630 | xfs_mount_t *mp, |
| 631 | uint flags) |
| 632 | { |
| 633 | int ndquots; |
| 634 | |
| 635 | /* |
| 636 | * Purge the dquot cache. |
| 637 | * None of the dquots should really be busy at this point. |
| 638 | */ |
| 639 | if (mp->m_quotainfo) { |
| 640 | while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) { |
| 641 | delay(ndquots * 10); |
| 642 | } |
| 643 | } |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | STATIC int |
| 648 | xfs_qm_dqattach_one( |
| 649 | xfs_inode_t *ip, |
| 650 | xfs_dqid_t id, |
| 651 | uint type, |
| 652 | uint doalloc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | xfs_dquot_t *udqhint, /* hint */ |
| 654 | xfs_dquot_t **IO_idqpp) |
| 655 | { |
| 656 | xfs_dquot_t *dqp; |
| 657 | int error; |
| 658 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 659 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | error = 0; |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | /* |
| 663 | * See if we already have it in the inode itself. IO_idqpp is |
| 664 | * &i_udquot or &i_gdquot. This made the code look weird, but |
| 665 | * made the logic a lot simpler. |
| 666 | */ |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 667 | dqp = *IO_idqpp; |
| 668 | if (dqp) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 669 | trace_xfs_dqattach_found(dqp); |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 670 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* |
| 674 | * udqhint is the i_udquot field in inode, and is non-NULL only |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 675 | * when the type arg is group/project. Its purpose is to save a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside |
| 677 | * the user dquot. |
| 678 | */ |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 679 | if (udqhint) { |
| 680 | ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | xfs_dqlock(udqhint); |
| 682 | |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 683 | /* |
| 684 | * No need to take dqlock to look at the id. |
| 685 | * |
| 686 | * The ID can't change until it gets reclaimed, and it won't |
| 687 | * be reclaimed as long as we have a ref from inode and we |
| 688 | * hold the ilock. |
| 689 | */ |
| 690 | dqp = udqhint->q_gdquot; |
| 691 | if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) { |
| 692 | xfs_dqlock(dqp); |
| 693 | XFS_DQHOLD(dqp); |
| 694 | ASSERT(*IO_idqpp == NULL); |
| 695 | *IO_idqpp = dqp; |
| 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | xfs_dqunlock(dqp); |
| 698 | xfs_dqunlock(udqhint); |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 699 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 701 | |
| 702 | /* |
| 703 | * We can't hold a dquot lock when we call the dqget code. |
| 704 | * We'll deadlock in no time, because of (not conforming to) |
| 705 | * lock ordering - the inodelock comes before any dquot lock, |
| 706 | * and we may drop and reacquire the ilock in xfs_qm_dqget(). |
| 707 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | xfs_dqunlock(udqhint); |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* |
| 712 | * Find the dquot from somewhere. This bumps the |
| 713 | * reference count of dquot and returns it locked. |
| 714 | * This can return ENOENT if dquot didn't exist on |
| 715 | * disk and we didn't ask it to allocate; |
| 716 | * ESRCH if quotas got turned off suddenly. |
| 717 | */ |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 718 | error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp); |
| 719 | if (error) |
| 720 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 722 | trace_xfs_dqattach_get(dqp); |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | /* |
| 725 | * dqget may have dropped and re-acquired the ilock, but it guarantees |
| 726 | * that the dquot returned is the one that should go in the inode. |
| 727 | */ |
| 728 | *IO_idqpp = dqp; |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 729 | xfs_dqunlock(dqp); |
| 730 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
| 734 | /* |
| 735 | * Given a udquot and gdquot, attach a ptr to the group dquot in the |
| 736 | * udquot as a hint for future lookups. The idea sounds simple, but the |
| 737 | * execution isn't, because the udquot might have a group dquot attached |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 738 | * already and getting rid of that gets us into lock ordering constraints. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | * The process is complicated more by the fact that the dquots may or may not |
| 740 | * be locked on entry. |
| 741 | */ |
| 742 | STATIC void |
| 743 | xfs_qm_dqattach_grouphint( |
| 744 | xfs_dquot_t *udq, |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 745 | xfs_dquot_t *gdq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
| 747 | xfs_dquot_t *tmp; |
| 748 | |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 749 | xfs_dqlock(udq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
| 751 | if ((tmp = udq->q_gdquot)) { |
| 752 | if (tmp == gdq) { |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 753 | xfs_dqunlock(udq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | |
| 757 | udq->q_gdquot = NULL; |
| 758 | /* |
| 759 | * We can't keep any dqlocks when calling dqrele, |
| 760 | * because the freelist lock comes before dqlocks. |
| 761 | */ |
| 762 | xfs_dqunlock(udq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
| 764 | * we took a hard reference once upon a time in dqget, |
| 765 | * so give it back when the udquot no longer points at it |
| 766 | * dqput() does the unlocking of the dquot. |
| 767 | */ |
| 768 | xfs_qm_dqrele(tmp); |
| 769 | |
| 770 | xfs_dqlock(udq); |
| 771 | xfs_dqlock(gdq); |
| 772 | |
| 773 | } else { |
| 774 | ASSERT(XFS_DQ_IS_LOCKED(udq)); |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 775 | xfs_dqlock(gdq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | ASSERT(XFS_DQ_IS_LOCKED(udq)); |
| 779 | ASSERT(XFS_DQ_IS_LOCKED(gdq)); |
| 780 | /* |
| 781 | * Somebody could have attached a gdquot here, |
| 782 | * when we dropped the uqlock. If so, just do nothing. |
| 783 | */ |
| 784 | if (udq->q_gdquot == NULL) { |
| 785 | XFS_DQHOLD(gdq); |
| 786 | udq->q_gdquot = gdq; |
| 787 | } |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 788 | |
| 789 | xfs_dqunlock(gdq); |
| 790 | xfs_dqunlock(udq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
| 794 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 795 | * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON |
| 796 | * into account. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | * Inode may get unlocked and relocked in here, and the caller must deal with |
| 799 | * the consequences. |
| 800 | */ |
| 801 | int |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 802 | xfs_qm_dqattach_locked( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | xfs_inode_t *ip, |
| 804 | uint flags) |
| 805 | { |
| 806 | xfs_mount_t *mp = ip->i_mount; |
| 807 | uint nquotas = 0; |
| 808 | int error = 0; |
| 809 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 810 | if (!XFS_IS_QUOTA_RUNNING(mp) || |
| 811 | !XFS_IS_QUOTA_ON(mp) || |
| 812 | !XFS_NOT_DQATTACHED(mp, ip) || |
| 813 | ip->i_ino == mp->m_sb.sb_uquotino || |
| 814 | ip->i_ino == mp->m_sb.sb_gquotino) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 815 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 817 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
| 819 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 820 | error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER, |
| 821 | flags & XFS_QMOPT_DQALLOC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | NULL, &ip->i_udquot); |
| 823 | if (error) |
| 824 | goto done; |
| 825 | nquotas++; |
| 826 | } |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 827 | |
| 828 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 829 | if (XFS_IS_OQUOTA_ON(mp)) { |
| 830 | error = XFS_IS_GQUOTA_ON(mp) ? |
| 831 | xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP, |
| 832 | flags & XFS_QMOPT_DQALLOC, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 833 | ip->i_udquot, &ip->i_gdquot) : |
| 834 | xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | flags & XFS_QMOPT_DQALLOC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | ip->i_udquot, &ip->i_gdquot); |
| 837 | /* |
| 838 | * Don't worry about the udquot that we may have |
| 839 | * attached above. It'll get detached, if not already. |
| 840 | */ |
| 841 | if (error) |
| 842 | goto done; |
| 843 | nquotas++; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Attach this group quota to the user quota as a hint. |
| 848 | * This WON'T, in general, result in a thrash. |
| 849 | */ |
| 850 | if (nquotas == 2) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 851 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | ASSERT(ip->i_udquot); |
| 853 | ASSERT(ip->i_gdquot); |
| 854 | |
| 855 | /* |
| 856 | * We may or may not have the i_udquot locked at this point, |
| 857 | * but this check is OK since we don't depend on the i_gdquot to |
| 858 | * be accurate 100% all the time. It is just a hint, and this |
| 859 | * will succeed in general. |
| 860 | */ |
| 861 | if (ip->i_udquot->q_gdquot == ip->i_gdquot) |
| 862 | goto done; |
| 863 | /* |
| 864 | * Attach i_gdquot to the gdquot hint inside the i_udquot. |
| 865 | */ |
Christoph Hellwig | 8e9b6e7 | 2009-02-08 21:51:42 +0100 | [diff] [blame] | 866 | xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 869 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | #ifdef QUOTADEBUG |
| 871 | if (! error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | if (XFS_IS_UQUOTA_ON(mp)) |
| 873 | ASSERT(ip->i_udquot); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 874 | if (XFS_IS_OQUOTA_ON(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | ASSERT(ip->i_gdquot); |
| 876 | } |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 877 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | #endif |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 879 | return error; |
| 880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 882 | int |
| 883 | xfs_qm_dqattach( |
| 884 | struct xfs_inode *ip, |
| 885 | uint flags) |
| 886 | { |
| 887 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 889 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 890 | error = xfs_qm_dqattach_locked(ip, flags); |
| 891 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 892 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 893 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Release dquots (and their references) if any. |
| 898 | * The inode should be locked EXCL except when this's called by |
| 899 | * xfs_ireclaim. |
| 900 | */ |
| 901 | void |
| 902 | xfs_qm_dqdetach( |
| 903 | xfs_inode_t *ip) |
| 904 | { |
| 905 | if (!(ip->i_udquot || ip->i_gdquot)) |
| 906 | return; |
| 907 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 908 | trace_xfs_dquot_dqdetach(ip); |
| 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino); |
| 911 | ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | if (ip->i_udquot) { |
| 913 | xfs_qm_dqrele(ip->i_udquot); |
| 914 | ip->i_udquot = NULL; |
| 915 | } |
| 916 | if (ip->i_gdquot) { |
| 917 | xfs_qm_dqrele(ip->i_gdquot); |
| 918 | ip->i_gdquot = NULL; |
| 919 | } |
| 920 | } |
| 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | int |
| 923 | xfs_qm_sync( |
| 924 | xfs_mount_t *mp, |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 925 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
| 927 | int recl, restarts; |
| 928 | xfs_dquot_t *dqp; |
| 929 | uint flush_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | int error; |
| 931 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 932 | if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp)) |
Donald Douwsma | d757762 | 2007-11-23 16:27:42 +1100 | [diff] [blame] | 933 | return 0; |
| 934 | |
Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 935 | flush_flags = (flags & SYNC_WAIT) ? XFS_QMOPT_SYNC : XFS_QMOPT_DELWRI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | restarts = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | again: |
| 939 | xfs_qm_mplist_lock(mp); |
| 940 | /* |
| 941 | * dqpurge_all() also takes the mplist lock and iterate thru all dquots |
| 942 | * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared |
| 943 | * when we have the mplist lock, we know that dquots will be consistent |
| 944 | * as long as we have it locked. |
| 945 | */ |
| 946 | if (! XFS_IS_QUOTA_ON(mp)) { |
| 947 | xfs_qm_mplist_unlock(mp); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 948 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
| 950 | FOREACH_DQUOT_IN_MP(dqp, mp) { |
| 951 | /* |
| 952 | * If this is vfs_sync calling, then skip the dquots that |
| 953 | * don't 'seem' to be dirty. ie. don't acquire dqlock. |
| 954 | * This is very similar to what xfs_sync does with inodes. |
| 955 | */ |
Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 956 | if (flags & SYNC_TRYLOCK) { |
| 957 | if (!XFS_DQ_IS_DIRTY(dqp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | continue; |
Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 959 | if (!xfs_qm_dqlock_nowait(dqp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | continue; |
| 961 | } else { |
| 962 | xfs_dqlock(dqp); |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Now, find out for sure if this dquot is dirty or not. |
| 967 | */ |
| 968 | if (! XFS_DQ_IS_DIRTY(dqp)) { |
| 969 | xfs_dqunlock(dqp); |
| 970 | continue; |
| 971 | } |
| 972 | |
| 973 | /* XXX a sentinel would be better */ |
| 974 | recl = XFS_QI_MPLRECLAIMS(mp); |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 975 | if (!xfs_dqflock_nowait(dqp)) { |
Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 976 | if (flags & SYNC_TRYLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | xfs_dqunlock(dqp); |
| 978 | continue; |
| 979 | } |
| 980 | /* |
| 981 | * If we can't grab the flush lock then if the caller |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 982 | * really wanted us to give this our best shot, so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | * see if we can give a push to the buffer before we wait |
| 984 | * on the flush lock. At this point, we know that |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 985 | * even though the dquot is being flushed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | * it has (new) dirty data. |
| 987 | */ |
| 988 | xfs_qm_dqflock_pushbuf_wait(dqp); |
| 989 | } |
| 990 | /* |
| 991 | * Let go of the mplist lock. We don't want to hold it |
| 992 | * across a disk write |
| 993 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | xfs_qm_mplist_unlock(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | error = xfs_qm_dqflush(dqp, flush_flags); |
| 996 | xfs_dqunlock(dqp); |
| 997 | if (error && XFS_FORCED_SHUTDOWN(mp)) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 998 | return 0; /* Need to prevent umount failure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | else if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1000 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | xfs_qm_mplist_lock(mp); |
| 1003 | if (recl != XFS_QI_MPLRECLAIMS(mp)) { |
| 1004 | if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS) |
| 1005 | break; |
| 1006 | |
| 1007 | xfs_qm_mplist_unlock(mp); |
| 1008 | goto again; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | xfs_qm_mplist_unlock(mp); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1013 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Christoph Hellwig | a4edd1d | 2009-01-19 02:03:11 +0100 | [diff] [blame] | 1016 | /* |
| 1017 | * The hash chains and the mplist use the same xfs_dqhash structure as |
| 1018 | * their list head, but we can take the mplist qh_lock and one of the |
| 1019 | * hash qh_locks at the same time without any problem as they aren't |
| 1020 | * related. |
| 1021 | */ |
| 1022 | static struct lock_class_key xfs_quota_mplist_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | /* |
| 1025 | * This initializes all the quota information that's kept in the |
| 1026 | * mount structure |
| 1027 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1028 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | xfs_qm_init_quotainfo( |
| 1030 | xfs_mount_t *mp) |
| 1031 | { |
| 1032 | xfs_quotainfo_t *qinf; |
| 1033 | int error; |
| 1034 | xfs_dquot_t *dqp; |
| 1035 | |
| 1036 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
| 1037 | |
| 1038 | /* |
| 1039 | * Tell XQM that we exist as soon as possible. |
| 1040 | */ |
| 1041 | if ((error = xfs_qm_hold_quotafs_ref(mp))) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1042 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP); |
| 1046 | |
| 1047 | /* |
| 1048 | * See if quotainodes are setup, and if not, allocate them, |
| 1049 | * and change the superblock accordingly. |
| 1050 | */ |
| 1051 | if ((error = xfs_qm_init_quotainos(mp))) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1052 | kmem_free(qinf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | mp->m_quotainfo = NULL; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1054 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0); |
Christoph Hellwig | a4edd1d | 2009-01-19 02:03:11 +0100 | [diff] [blame] | 1058 | lockdep_set_class(&qinf->qi_dqlist.qh_lock, &xfs_quota_mplist_class); |
| 1059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | qinf->qi_dqreclaims = 0; |
| 1061 | |
| 1062 | /* mutex used to serialize quotaoffs */ |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 1063 | mutex_init(&qinf->qi_quotaofflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | /* Precalc some constants */ |
| 1066 | qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB); |
| 1067 | ASSERT(qinf->qi_dqchunklen); |
| 1068 | qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen); |
| 1069 | do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t)); |
| 1070 | |
| 1071 | mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD); |
| 1072 | |
| 1073 | /* |
| 1074 | * We try to get the limits from the superuser's limits fields. |
| 1075 | * This is quite hacky, but it is standard quota practice. |
| 1076 | * We look at the USR dquot with id == 0 first, but if user quotas |
| 1077 | * are not enabled we goto the GRP dquot with id == 0. |
| 1078 | * We don't really care to keep separate default limits for user |
| 1079 | * and group quotas, at least not at this point. |
| 1080 | */ |
| 1081 | error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1082 | XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER : |
| 1083 | (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP : |
| 1084 | XFS_DQ_PROJ), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN, |
| 1086 | &dqp); |
| 1087 | if (! error) { |
| 1088 | xfs_disk_dquot_t *ddqp = &dqp->q_core; |
| 1089 | |
| 1090 | /* |
| 1091 | * The warnings and timers set the grace period given to |
| 1092 | * a user or group before he or she can not perform any |
| 1093 | * more writing. If it is zero, a default is used. |
| 1094 | */ |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1095 | qinf->qi_btimelimit = ddqp->d_btimer ? |
| 1096 | be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT; |
| 1097 | qinf->qi_itimelimit = ddqp->d_itimer ? |
| 1098 | be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT; |
| 1099 | qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ? |
| 1100 | be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT; |
| 1101 | qinf->qi_bwarnlimit = ddqp->d_bwarns ? |
| 1102 | be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT; |
| 1103 | qinf->qi_iwarnlimit = ddqp->d_iwarns ? |
| 1104 | be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT; |
| 1105 | qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ? |
| 1106 | be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT; |
| 1107 | qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit); |
| 1108 | qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit); |
| 1109 | qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit); |
| 1110 | qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit); |
| 1111 | qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit); |
| 1112 | qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
| 1114 | /* |
| 1115 | * We sent the XFS_QMOPT_DQSUSER flag to dqget because |
| 1116 | * we don't want this dquot cached. We haven't done a |
| 1117 | * quotacheck yet, and quotacheck doesn't like incore dquots. |
| 1118 | */ |
| 1119 | xfs_qm_dqdestroy(dqp); |
| 1120 | } else { |
| 1121 | qinf->qi_btimelimit = XFS_QM_BTIMELIMIT; |
| 1122 | qinf->qi_itimelimit = XFS_QM_ITIMELIMIT; |
| 1123 | qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT; |
| 1124 | qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT; |
| 1125 | qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 1126 | qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | /* |
| 1134 | * Gets called when unmounting a filesystem or when all quotas get |
| 1135 | * turned off. |
| 1136 | * This purges the quota inodes, destroys locks and frees itself. |
| 1137 | */ |
| 1138 | void |
| 1139 | xfs_qm_destroy_quotainfo( |
| 1140 | xfs_mount_t *mp) |
| 1141 | { |
| 1142 | xfs_quotainfo_t *qi; |
| 1143 | |
| 1144 | qi = mp->m_quotainfo; |
| 1145 | ASSERT(qi != NULL); |
| 1146 | ASSERT(xfs_Gqm != NULL); |
| 1147 | |
| 1148 | /* |
| 1149 | * Release the reference that XQM kept, so that we know |
| 1150 | * when the XQM structure should be freed. We cannot assume |
| 1151 | * that xfs_Gqm is non-null after this point. |
| 1152 | */ |
| 1153 | xfs_qm_rele_quotafs_ref(mp); |
| 1154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | xfs_qm_list_destroy(&qi->qi_dqlist); |
| 1156 | |
| 1157 | if (qi->qi_uquotaip) { |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 1158 | IRELE(qi->qi_uquotaip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | qi->qi_uquotaip = NULL; /* paranoia */ |
| 1160 | } |
| 1161 | if (qi->qi_gquotaip) { |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 1162 | IRELE(qi->qi_gquotaip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | qi->qi_gquotaip = NULL; |
| 1164 | } |
| 1165 | mutex_destroy(&qi->qi_quotaofflock); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1166 | kmem_free(qi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | mp->m_quotainfo = NULL; |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | /* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */ |
| 1173 | |
| 1174 | /* ARGSUSED */ |
| 1175 | STATIC void |
| 1176 | xfs_qm_list_init( |
| 1177 | xfs_dqlist_t *list, |
| 1178 | char *str, |
| 1179 | int n) |
| 1180 | { |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 1181 | mutex_init(&list->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | list->qh_next = NULL; |
| 1183 | list->qh_version = 0; |
| 1184 | list->qh_nelems = 0; |
| 1185 | } |
| 1186 | |
| 1187 | STATIC void |
| 1188 | xfs_qm_list_destroy( |
| 1189 | xfs_dqlist_t *list) |
| 1190 | { |
| 1191 | mutex_destroy(&(list->qh_lock)); |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | /* |
| 1196 | * Stripped down version of dqattach. This doesn't attach, or even look at the |
| 1197 | * dquots attached to the inode. The rationale is that there won't be any |
| 1198 | * attached at the time this is called from quotacheck. |
| 1199 | */ |
| 1200 | STATIC int |
| 1201 | xfs_qm_dqget_noattach( |
| 1202 | xfs_inode_t *ip, |
| 1203 | xfs_dquot_t **O_udqpp, |
| 1204 | xfs_dquot_t **O_gdqpp) |
| 1205 | { |
| 1206 | int error; |
| 1207 | xfs_mount_t *mp; |
| 1208 | xfs_dquot_t *udqp, *gdqp; |
| 1209 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1210 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | mp = ip->i_mount; |
| 1212 | udqp = NULL; |
| 1213 | gdqp = NULL; |
| 1214 | |
| 1215 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 1216 | ASSERT(ip->i_udquot == NULL); |
| 1217 | /* |
| 1218 | * We want the dquot allocated if it doesn't exist. |
| 1219 | */ |
| 1220 | if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER, |
| 1221 | XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, |
| 1222 | &udqp))) { |
| 1223 | /* |
| 1224 | * Shouldn't be able to turn off quotas here. |
| 1225 | */ |
| 1226 | ASSERT(error != ESRCH); |
| 1227 | ASSERT(error != ENOENT); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1228 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } |
| 1230 | ASSERT(udqp); |
| 1231 | } |
| 1232 | |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1233 | if (XFS_IS_OQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | ASSERT(ip->i_gdquot == NULL); |
| 1235 | if (udqp) |
| 1236 | xfs_dqunlock(udqp); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1237 | error = XFS_IS_GQUOTA_ON(mp) ? |
| 1238 | xfs_qm_dqget(mp, ip, |
| 1239 | ip->i_d.di_gid, XFS_DQ_GROUP, |
| 1240 | XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN, |
| 1241 | &gdqp) : |
| 1242 | xfs_qm_dqget(mp, ip, |
| 1243 | ip->i_d.di_projid, XFS_DQ_PROJ, |
| 1244 | XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN, |
| 1245 | &gdqp); |
| 1246 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | if (udqp) |
| 1248 | xfs_qm_dqrele(udqp); |
| 1249 | ASSERT(error != ESRCH); |
| 1250 | ASSERT(error != ENOENT); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1251 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
| 1253 | ASSERT(gdqp); |
| 1254 | |
| 1255 | /* Reacquire the locks in the right order */ |
| 1256 | if (udqp) { |
| 1257 | if (! xfs_qm_dqlock_nowait(udqp)) { |
| 1258 | xfs_dqunlock(gdqp); |
| 1259 | xfs_dqlock(udqp); |
| 1260 | xfs_dqlock(gdqp); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | *O_udqpp = udqp; |
| 1266 | *O_gdqpp = gdqp; |
| 1267 | |
| 1268 | #ifdef QUOTADEBUG |
| 1269 | if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp)); |
| 1270 | if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp)); |
| 1271 | #endif |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1272 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * Create an inode and return with a reference already taken, but unlocked |
| 1277 | * This is how we create quota inodes |
| 1278 | */ |
| 1279 | STATIC int |
| 1280 | xfs_qm_qino_alloc( |
| 1281 | xfs_mount_t *mp, |
| 1282 | xfs_inode_t **ip, |
| 1283 | __int64_t sbfields, |
| 1284 | uint flags) |
| 1285 | { |
| 1286 | xfs_trans_t *tp; |
| 1287 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | int committed; |
| 1289 | |
Nathan Scott | 061f720 | 2006-01-11 15:27:50 +1100 | [diff] [blame] | 1290 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | if ((error = xfs_trans_reserve(tp, |
| 1292 | XFS_QM_QINOCREATE_SPACE_RES(mp), |
| 1293 | XFS_CREATE_LOG_RES(mp), 0, |
| 1294 | XFS_TRANS_PERM_LOG_RES, |
| 1295 | XFS_CREATE_LOG_COUNT))) { |
| 1296 | xfs_trans_cancel(tp, 0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1297 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 1300 | if ((error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, |
Nathan Scott | dae81d4 | 2006-02-28 12:30:13 +1100 | [diff] [blame] | 1301 | &xfs_zerocr, 0, 1, ip, &committed))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | |
| 1303 | XFS_TRANS_ABORT); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1304 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Keep an extra reference to this quota inode. This inode is |
| 1309 | * locked exclusively and joined to the transaction already. |
| 1310 | */ |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1311 | ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 1312 | IHOLD(*ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
| 1314 | /* |
| 1315 | * Make the changes in the superblock, and log those too. |
| 1316 | * sbfields arg may contain fields other than *QUOTINO; |
| 1317 | * VERSIONNUM for example. |
| 1318 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1319 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | if (flags & XFS_QMOPT_SBVERSION) { |
| 1321 | #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY) |
| 1322 | unsigned oldv = mp->m_sb.sb_versionnum; |
| 1323 | #endif |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1324 | ASSERT(!xfs_sb_version_hasquota(&mp->m_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO | |
| 1326 | XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) == |
| 1327 | (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO | |
| 1328 | XFS_SB_GQUOTINO | XFS_SB_QFLAGS)); |
| 1329 | |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1330 | xfs_sb_version_addquota(&mp->m_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | mp->m_sb.sb_uquotino = NULLFSINO; |
| 1332 | mp->m_sb.sb_gquotino = NULLFSINO; |
| 1333 | |
| 1334 | /* qflags will get updated _after_ quotacheck */ |
| 1335 | mp->m_sb.sb_qflags = 0; |
| 1336 | #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY) |
| 1337 | cmn_err(CE_NOTE, |
| 1338 | "Old superblock version %x, converting to %x.", |
| 1339 | oldv, mp->m_sb.sb_versionnum); |
| 1340 | #endif |
| 1341 | } |
| 1342 | if (flags & XFS_QMOPT_UQUOTA) |
| 1343 | mp->m_sb.sb_uquotino = (*ip)->i_ino; |
| 1344 | else |
| 1345 | mp->m_sb.sb_gquotino = (*ip)->i_ino; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1346 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | xfs_mod_sb(tp, sbfields); |
| 1348 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1349 | if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!"); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1351 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1353 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | |
David Chinner | 5b13973 | 2008-04-10 12:20:10 +1000 | [diff] [blame] | 1357 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | xfs_qm_reset_dqcounts( |
| 1359 | xfs_mount_t *mp, |
| 1360 | xfs_buf_t *bp, |
| 1361 | xfs_dqid_t id, |
| 1362 | uint type) |
| 1363 | { |
| 1364 | xfs_disk_dquot_t *ddq; |
| 1365 | int j; |
| 1366 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1367 | trace_xfs_reset_dqcounts(bp, _RET_IP_); |
| 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | /* |
| 1370 | * Reset all counters and timers. They'll be |
| 1371 | * started afresh by xfs_qm_quotacheck. |
| 1372 | */ |
| 1373 | #ifdef DEBUG |
| 1374 | j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB); |
| 1375 | do_div(j, sizeof(xfs_dqblk_t)); |
| 1376 | ASSERT(XFS_QM_DQPERBLK(mp) == j); |
| 1377 | #endif |
| 1378 | ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp); |
| 1379 | for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) { |
| 1380 | /* |
| 1381 | * Do a sanity check, and if needed, repair the dqblk. Don't |
| 1382 | * output any warnings because it's perfectly possible to |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1383 | * find uninitialised dquot blks. See comment in xfs_qm_dqcheck. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | */ |
| 1385 | (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR, |
| 1386 | "xfs_quotacheck"); |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1387 | ddq->d_bcount = 0; |
| 1388 | ddq->d_icount = 0; |
| 1389 | ddq->d_rtbcount = 0; |
| 1390 | ddq->d_btimer = 0; |
| 1391 | ddq->d_itimer = 0; |
| 1392 | ddq->d_rtbtimer = 0; |
| 1393 | ddq->d_bwarns = 0; |
| 1394 | ddq->d_iwarns = 0; |
| 1395 | ddq->d_rtbwarns = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1); |
| 1397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | STATIC int |
| 1401 | xfs_qm_dqiter_bufs( |
| 1402 | xfs_mount_t *mp, |
| 1403 | xfs_dqid_t firstid, |
| 1404 | xfs_fsblock_t bno, |
| 1405 | xfs_filblks_t blkcnt, |
| 1406 | uint flags) |
| 1407 | { |
| 1408 | xfs_buf_t *bp; |
| 1409 | int error; |
| 1410 | int notcommitted; |
| 1411 | int incr; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1412 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
| 1414 | ASSERT(blkcnt > 0); |
| 1415 | notcommitted = 0; |
| 1416 | incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ? |
| 1417 | XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1418 | type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER : |
| 1419 | (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | error = 0; |
| 1421 | |
| 1422 | /* |
| 1423 | * Blkcnt arg can be a very big number, and might even be |
| 1424 | * larger than the log itself. So, we have to break it up into |
| 1425 | * manageable-sized transactions. |
| 1426 | * Note that we don't start a permanent transaction here; we might |
| 1427 | * not be able to get a log reservation for the whole thing up front, |
| 1428 | * and we don't really care to either, because we just discard |
| 1429 | * everything if we were to crash in the middle of this loop. |
| 1430 | */ |
| 1431 | while (blkcnt--) { |
| 1432 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, |
| 1433 | XFS_FSB_TO_DADDR(mp, bno), |
| 1434 | (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp); |
| 1435 | if (error) |
| 1436 | break; |
| 1437 | |
David Chinner | 5b13973 | 2008-04-10 12:20:10 +1000 | [diff] [blame] | 1438 | xfs_qm_reset_dqcounts(mp, bp, firstid, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | xfs_bdwrite(mp, bp); |
| 1440 | /* |
| 1441 | * goto the next block. |
| 1442 | */ |
| 1443 | bno++; |
| 1444 | firstid += XFS_QM_DQPERBLK(mp); |
| 1445 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1446 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1450 | * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | * caller supplied function for every chunk of dquots that we find. |
| 1452 | */ |
| 1453 | STATIC int |
| 1454 | xfs_qm_dqiterate( |
| 1455 | xfs_mount_t *mp, |
| 1456 | xfs_inode_t *qip, |
| 1457 | uint flags) |
| 1458 | { |
| 1459 | xfs_bmbt_irec_t *map; |
| 1460 | int i, nmaps; /* number of map entries */ |
| 1461 | int error; /* return value */ |
| 1462 | xfs_fileoff_t lblkno; |
| 1463 | xfs_filblks_t maxlblkcnt; |
| 1464 | xfs_dqid_t firstid; |
| 1465 | xfs_fsblock_t rablkno; |
| 1466 | xfs_filblks_t rablkcnt; |
| 1467 | |
| 1468 | error = 0; |
| 1469 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1470 | * This looks racy, but we can't keep an inode lock across a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | * trans_reserve. But, this gets called during quotacheck, and that |
| 1472 | * happens only at mount time which is single threaded. |
| 1473 | */ |
| 1474 | if (qip->i_d.di_nblocks == 0) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1475 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
| 1477 | map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP); |
| 1478 | |
| 1479 | lblkno = 0; |
| 1480 | maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); |
| 1481 | do { |
| 1482 | nmaps = XFS_DQITER_MAP_SIZE; |
| 1483 | /* |
| 1484 | * We aren't changing the inode itself. Just changing |
| 1485 | * some of its data. No new blocks are added here, and |
| 1486 | * the inode is never added to the transaction. |
| 1487 | */ |
| 1488 | xfs_ilock(qip, XFS_ILOCK_SHARED); |
| 1489 | error = xfs_bmapi(NULL, qip, lblkno, |
| 1490 | maxlblkcnt - lblkno, |
| 1491 | XFS_BMAPI_METADATA, |
| 1492 | NULL, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1493 | 0, map, &nmaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | xfs_iunlock(qip, XFS_ILOCK_SHARED); |
| 1495 | if (error) |
| 1496 | break; |
| 1497 | |
| 1498 | ASSERT(nmaps <= XFS_DQITER_MAP_SIZE); |
| 1499 | for (i = 0; i < nmaps; i++) { |
| 1500 | ASSERT(map[i].br_startblock != DELAYSTARTBLOCK); |
| 1501 | ASSERT(map[i].br_blockcount); |
| 1502 | |
| 1503 | |
| 1504 | lblkno += map[i].br_blockcount; |
| 1505 | |
| 1506 | if (map[i].br_startblock == HOLESTARTBLOCK) |
| 1507 | continue; |
| 1508 | |
| 1509 | firstid = (xfs_dqid_t) map[i].br_startoff * |
| 1510 | XFS_QM_DQPERBLK(mp); |
| 1511 | /* |
| 1512 | * Do a read-ahead on the next extent. |
| 1513 | */ |
| 1514 | if ((i+1 < nmaps) && |
| 1515 | (map[i+1].br_startblock != HOLESTARTBLOCK)) { |
| 1516 | rablkcnt = map[i+1].br_blockcount; |
| 1517 | rablkno = map[i+1].br_startblock; |
| 1518 | while (rablkcnt--) { |
| 1519 | xfs_baread(mp->m_ddev_targp, |
| 1520 | XFS_FSB_TO_DADDR(mp, rablkno), |
| 1521 | (int)XFS_QI_DQCHUNKLEN(mp)); |
| 1522 | rablkno++; |
| 1523 | } |
| 1524 | } |
| 1525 | /* |
| 1526 | * Iterate thru all the blks in the extent and |
| 1527 | * reset the counters of all the dquots inside them. |
| 1528 | */ |
| 1529 | if ((error = xfs_qm_dqiter_bufs(mp, |
| 1530 | firstid, |
| 1531 | map[i].br_startblock, |
| 1532 | map[i].br_blockcount, |
| 1533 | flags))) { |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | if (error) |
| 1539 | break; |
| 1540 | } while (nmaps > 0); |
| 1541 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1542 | kmem_free(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1544 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * Called by dqusage_adjust in doing a quotacheck. |
| 1549 | * Given the inode, and a dquot (either USR or GRP, doesn't matter), |
| 1550 | * this updates its incore copy as well as the buffer copy. This is |
| 1551 | * so that once the quotacheck is done, we can just log all the buffers, |
| 1552 | * as opposed to logging numerous updates to individual dquots. |
| 1553 | */ |
| 1554 | STATIC void |
| 1555 | xfs_qm_quotacheck_dqadjust( |
| 1556 | xfs_dquot_t *dqp, |
| 1557 | xfs_qcnt_t nblks, |
| 1558 | xfs_qcnt_t rtblks) |
| 1559 | { |
| 1560 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1561 | |
| 1562 | trace_xfs_dqadjust(dqp); |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | /* |
| 1565 | * Adjust the inode count and the block count to reflect this inode's |
| 1566 | * resource usage. |
| 1567 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1568 | be64_add_cpu(&dqp->q_core.d_icount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | dqp->q_res_icount++; |
| 1570 | if (nblks) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1571 | be64_add_cpu(&dqp->q_core.d_bcount, nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | dqp->q_res_bcount += nblks; |
| 1573 | } |
| 1574 | if (rtblks) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1575 | be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | dqp->q_res_rtbcount += rtblks; |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Set default limits, adjust timers (since we changed usages) |
| 1581 | */ |
| 1582 | if (! XFS_IS_SUSER_DQUOT(dqp)) { |
| 1583 | xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core); |
| 1584 | xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core); |
| 1585 | } |
| 1586 | |
| 1587 | dqp->dq_flags |= XFS_DQ_DIRTY; |
| 1588 | } |
| 1589 | |
| 1590 | STATIC int |
| 1591 | xfs_qm_get_rtblks( |
| 1592 | xfs_inode_t *ip, |
| 1593 | xfs_qcnt_t *O_rtblks) |
| 1594 | { |
| 1595 | xfs_filblks_t rtblks; /* total rt blks */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1596 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1598 | xfs_extnum_t nextents; /* number of extent entries */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | int error; |
| 1600 | |
| 1601 | ASSERT(XFS_IS_REALTIME_INODE(ip)); |
| 1602 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1603 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1604 | if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK))) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1605 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | rtblks = 0; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1608 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1609 | for (idx = 0; idx < nextents; idx++) |
| 1610 | rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | *O_rtblks = (xfs_qcnt_t)rtblks; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1612 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * callback routine supplied to bulkstat(). Given an inumber, find its |
| 1617 | * dquots and update them to account for resources taken by that inode. |
| 1618 | */ |
| 1619 | /* ARGSUSED */ |
| 1620 | STATIC int |
| 1621 | xfs_qm_dqusage_adjust( |
| 1622 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 1623 | xfs_ino_t ino, /* inode number to get data for */ |
| 1624 | void __user *buffer, /* not used */ |
| 1625 | int ubsize, /* not used */ |
| 1626 | void *private_data, /* not used */ |
| 1627 | xfs_daddr_t bno, /* starting block of inode cluster */ |
| 1628 | int *ubused, /* not used */ |
| 1629 | void *dip, /* on-disk inode pointer (not used) */ |
| 1630 | int *res) /* result code value */ |
| 1631 | { |
| 1632 | xfs_inode_t *ip; |
| 1633 | xfs_dquot_t *udqp, *gdqp; |
| 1634 | xfs_qcnt_t nblks, rtblks; |
| 1635 | int error; |
| 1636 | |
| 1637 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
| 1638 | |
| 1639 | /* |
| 1640 | * rootino must have its resources accounted for, not so with the quota |
| 1641 | * inodes. |
| 1642 | */ |
| 1643 | if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) { |
| 1644 | *res = BULKSTAT_RV_NOTHING; |
| 1645 | return XFS_ERROR(EINVAL); |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget |
| 1650 | * interface expects the inode to be exclusively locked because that's |
| 1651 | * the case in all other instances. It's OK that we do this because |
| 1652 | * quotacheck is done only at mount time. |
| 1653 | */ |
| 1654 | if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) { |
| 1655 | *res = BULKSTAT_RV_NOTHING; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1656 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | /* |
| 1660 | * Obtain the locked dquots. In case of an error (eg. allocation |
| 1661 | * fails for ENOSPC), we return the negative of the error number |
| 1662 | * to bulkstat, so that it can get propagated to quotacheck() and |
| 1663 | * making us disable quotas for the file system. |
| 1664 | */ |
| 1665 | if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) { |
| 1666 | xfs_iput(ip, XFS_ILOCK_EXCL); |
| 1667 | *res = BULKSTAT_RV_GIVEUP; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1668 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | rtblks = 0; |
| 1672 | if (! XFS_IS_REALTIME_INODE(ip)) { |
| 1673 | nblks = (xfs_qcnt_t)ip->i_d.di_nblocks; |
| 1674 | } else { |
| 1675 | /* |
| 1676 | * Walk thru the extent list and count the realtime blocks. |
| 1677 | */ |
| 1678 | if ((error = xfs_qm_get_rtblks(ip, &rtblks))) { |
| 1679 | xfs_iput(ip, XFS_ILOCK_EXCL); |
| 1680 | if (udqp) |
| 1681 | xfs_qm_dqput(udqp); |
| 1682 | if (gdqp) |
| 1683 | xfs_qm_dqput(gdqp); |
| 1684 | *res = BULKSTAT_RV_GIVEUP; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1685 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | } |
| 1687 | nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks; |
| 1688 | } |
| 1689 | ASSERT(ip->i_delayed_blks == 0); |
| 1690 | |
| 1691 | /* |
| 1692 | * We can't release the inode while holding its dquot locks. |
| 1693 | * The inode can go into inactive and might try to acquire the dquotlocks. |
| 1694 | * So, just unlock here and do a vn_rele at the end. |
| 1695 | */ |
| 1696 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1697 | |
| 1698 | /* |
| 1699 | * Add the (disk blocks and inode) resources occupied by this |
| 1700 | * inode to its dquots. We do this adjustment in the incore dquot, |
| 1701 | * and also copy the changes to its buffer. |
| 1702 | * We don't care about putting these changes in a transaction |
| 1703 | * envelope because if we crash in the middle of a 'quotacheck' |
| 1704 | * we have to start from the beginning anyway. |
| 1705 | * Once we're done, we'll log all the dquot bufs. |
| 1706 | * |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1707 | * The *QUOTA_ON checks below may look pretty racy, but quotachecks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | * and quotaoffs don't race. (Quotachecks happen at mount time only). |
| 1709 | */ |
| 1710 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 1711 | ASSERT(udqp); |
| 1712 | xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks); |
| 1713 | xfs_qm_dqput(udqp); |
| 1714 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1715 | if (XFS_IS_OQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | ASSERT(gdqp); |
| 1717 | xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks); |
| 1718 | xfs_qm_dqput(gdqp); |
| 1719 | } |
| 1720 | /* |
| 1721 | * Now release the inode. This will send it to 'inactive', and |
| 1722 | * possibly even free blocks. |
| 1723 | */ |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 1724 | IRELE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
| 1726 | /* |
| 1727 | * Goto next inode. |
| 1728 | */ |
| 1729 | *res = BULKSTAT_RV_DIDONE; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1730 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Walk thru all the filesystem inodes and construct a consistent view |
| 1735 | * of the disk quota world. If the quotacheck fails, disable quotas. |
| 1736 | */ |
| 1737 | int |
| 1738 | xfs_qm_quotacheck( |
| 1739 | xfs_mount_t *mp) |
| 1740 | { |
| 1741 | int done, count, error; |
| 1742 | xfs_ino_t lastino; |
| 1743 | size_t structsz; |
| 1744 | xfs_inode_t *uip, *gip; |
| 1745 | uint flags; |
| 1746 | |
| 1747 | count = INT_MAX; |
| 1748 | structsz = 1; |
| 1749 | lastino = 0; |
| 1750 | flags = 0; |
| 1751 | |
| 1752 | ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp)); |
| 1753 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
| 1754 | |
| 1755 | /* |
| 1756 | * There should be no cached dquots. The (simplistic) quotacheck |
| 1757 | * algorithm doesn't like that. |
| 1758 | */ |
| 1759 | ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0); |
| 1760 | |
| 1761 | cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname); |
| 1762 | |
| 1763 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1764 | * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | * their counters to zero. We need a clean slate. |
| 1766 | * We don't log our changes till later. |
| 1767 | */ |
| 1768 | if ((uip = XFS_QI_UQIP(mp))) { |
| 1769 | if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA))) |
| 1770 | goto error_return; |
| 1771 | flags |= XFS_UQUOTA_CHKD; |
| 1772 | } |
| 1773 | |
| 1774 | if ((gip = XFS_QI_GQIP(mp))) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1775 | if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ? |
| 1776 | XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | goto error_return; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1778 | flags |= XFS_OQUOTA_CHKD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | do { |
| 1782 | /* |
| 1783 | * Iterate thru all the inodes in the file system, |
| 1784 | * adjusting the corresponding dquot counters in core. |
| 1785 | */ |
| 1786 | if ((error = xfs_bulkstat(mp, &lastino, &count, |
| 1787 | xfs_qm_dqusage_adjust, NULL, |
Nathan Scott | 9c48876 | 2006-06-09 15:29:22 +1000 | [diff] [blame] | 1788 | structsz, NULL, BULKSTAT_FG_IGET, &done))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | break; |
| 1790 | |
| 1791 | } while (! done); |
| 1792 | |
| 1793 | /* |
David Chinner | 4b8879d | 2008-04-10 12:20:17 +1000 | [diff] [blame] | 1794 | * We've made all the changes that we need to make incore. |
| 1795 | * Flush them down to disk buffers if everything was updated |
| 1796 | * successfully. |
| 1797 | */ |
| 1798 | if (!error) |
| 1799 | error = xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI); |
| 1800 | |
| 1801 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | * We can get this error if we couldn't do a dquot allocation inside |
| 1803 | * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the |
| 1804 | * dirty dquots that might be cached, we just want to get rid of them |
| 1805 | * and turn quotaoff. The dquots won't be attached to any of the inodes |
| 1806 | * at this point (because we intentionally didn't in dqget_noattach). |
| 1807 | */ |
| 1808 | if (error) { |
Nathan Scott | ee2a4f7 | 2006-01-11 15:33:36 +1100 | [diff] [blame] | 1809 | xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | goto error_return; |
| 1811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
| 1813 | /* |
| 1814 | * We didn't log anything, because if we crashed, we'll have to |
| 1815 | * start the quotacheck from scratch anyway. However, we must make |
| 1816 | * sure that our dquot changes are secure before we put the |
| 1817 | * quotacheck'd stamp on the superblock. So, here we do a synchronous |
| 1818 | * flush. |
| 1819 | */ |
| 1820 | XFS_bflush(mp->m_ddev_targp); |
| 1821 | |
| 1822 | /* |
| 1823 | * If one type of quotas is off, then it will lose its |
| 1824 | * quotachecked status, since we won't be doing accounting for |
| 1825 | * that type anymore. |
| 1826 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1827 | mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | mp->m_qflags |= flags; |
| 1829 | |
| 1830 | XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++"); |
| 1831 | |
| 1832 | error_return: |
| 1833 | if (error) { |
| 1834 | cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): " |
| 1835 | "Disabling quotas.", |
| 1836 | mp->m_fsname, error); |
| 1837 | /* |
| 1838 | * We must turn off quotas. |
| 1839 | */ |
| 1840 | ASSERT(mp->m_quotainfo != NULL); |
| 1841 | ASSERT(xfs_Gqm != NULL); |
| 1842 | xfs_qm_destroy_quotainfo(mp); |
David Chinner | 31d5577 | 2008-04-10 12:20:38 +1000 | [diff] [blame] | 1843 | if (xfs_mount_reset_sbqflags(mp)) { |
| 1844 | cmn_err(CE_WARN, "XFS quotacheck %s: " |
| 1845 | "Failed to reset quota flags.", mp->m_fsname); |
| 1846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | } else { |
| 1848 | cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname); |
| 1849 | } |
| 1850 | return (error); |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * This is called after the superblock has been read in and we're ready to |
| 1855 | * iget the quota inodes. |
| 1856 | */ |
| 1857 | STATIC int |
| 1858 | xfs_qm_init_quotainos( |
| 1859 | xfs_mount_t *mp) |
| 1860 | { |
| 1861 | xfs_inode_t *uip, *gip; |
| 1862 | int error; |
| 1863 | __int64_t sbflags; |
| 1864 | uint flags; |
| 1865 | |
| 1866 | ASSERT(mp->m_quotainfo); |
| 1867 | uip = gip = NULL; |
| 1868 | sbflags = 0; |
| 1869 | flags = 0; |
| 1870 | |
| 1871 | /* |
| 1872 | * Get the uquota and gquota inodes |
| 1873 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1874 | if (xfs_sb_version_hasquota(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | if (XFS_IS_UQUOTA_ON(mp) && |
| 1876 | mp->m_sb.sb_uquotino != NULLFSINO) { |
| 1877 | ASSERT(mp->m_sb.sb_uquotino > 0); |
| 1878 | if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, |
| 1879 | 0, 0, &uip, 0))) |
| 1880 | return XFS_ERROR(error); |
| 1881 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1882 | if (XFS_IS_OQUOTA_ON(mp) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | mp->m_sb.sb_gquotino != NULLFSINO) { |
| 1884 | ASSERT(mp->m_sb.sb_gquotino > 0); |
| 1885 | if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, |
| 1886 | 0, 0, &gip, 0))) { |
| 1887 | if (uip) |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 1888 | IRELE(uip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | return XFS_ERROR(error); |
| 1890 | } |
| 1891 | } |
| 1892 | } else { |
| 1893 | flags |= XFS_QMOPT_SBVERSION; |
| 1894 | sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO | |
| 1895 | XFS_SB_GQUOTINO | XFS_SB_QFLAGS); |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * Create the two inodes, if they don't exist already. The changes |
| 1900 | * made above will get added to a transaction and logged in one of |
| 1901 | * the qino_alloc calls below. If the device is readonly, |
| 1902 | * temporarily switch to read-write to do this. |
| 1903 | */ |
| 1904 | if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) { |
| 1905 | if ((error = xfs_qm_qino_alloc(mp, &uip, |
| 1906 | sbflags | XFS_SB_UQUOTINO, |
| 1907 | flags | XFS_QMOPT_UQUOTA))) |
| 1908 | return XFS_ERROR(error); |
| 1909 | |
| 1910 | flags &= ~XFS_QMOPT_SBVERSION; |
| 1911 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1912 | if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) { |
| 1913 | flags |= (XFS_IS_GQUOTA_ON(mp) ? |
| 1914 | XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA); |
| 1915 | error = xfs_qm_qino_alloc(mp, &gip, |
| 1916 | sbflags | XFS_SB_GQUOTINO, flags); |
| 1917 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | if (uip) |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 1919 | IRELE(uip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
| 1921 | return XFS_ERROR(error); |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | XFS_QI_UQIP(mp) = uip; |
| 1926 | XFS_QI_GQIP(mp) = gip; |
| 1927 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1928 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | |
| 1932 | /* |
| 1933 | * Traverse the freelist of dquots and attempt to reclaim a maximum of |
| 1934 | * 'howmany' dquots. This operation races with dqlookup(), and attempts to |
| 1935 | * favor the lookup function ... |
| 1936 | * XXXsup merge this with qm_reclaim_one(). |
| 1937 | */ |
| 1938 | STATIC int |
| 1939 | xfs_qm_shake_freelist( |
| 1940 | int howmany) |
| 1941 | { |
| 1942 | int nreclaimed; |
| 1943 | xfs_dqhash_t *hash; |
| 1944 | xfs_dquot_t *dqp, *nextdqp; |
| 1945 | int restarts; |
| 1946 | int nflushes; |
| 1947 | |
| 1948 | if (howmany <= 0) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1949 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | |
| 1951 | nreclaimed = 0; |
| 1952 | restarts = 0; |
| 1953 | nflushes = 0; |
| 1954 | |
| 1955 | #ifdef QUOTADEBUG |
| 1956 | cmn_err(CE_DEBUG, "Shake free 0x%x", howmany); |
| 1957 | #endif |
| 1958 | /* lock order is : hashchainlock, freelistlock, mplistlock */ |
| 1959 | tryagain: |
| 1960 | xfs_qm_freelist_lock(xfs_Gqm); |
| 1961 | |
| 1962 | for (dqp = xfs_Gqm->qm_dqfreelist.qh_next; |
| 1963 | ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) && |
| 1964 | nreclaimed < howmany); ) { |
| 1965 | xfs_dqlock(dqp); |
| 1966 | |
| 1967 | /* |
| 1968 | * We are racing with dqlookup here. Naturally we don't |
| 1969 | * want to reclaim a dquot that lookup wants. |
| 1970 | */ |
| 1971 | if (dqp->dq_flags & XFS_DQ_WANT) { |
| 1972 | xfs_dqunlock(dqp); |
| 1973 | xfs_qm_freelist_unlock(xfs_Gqm); |
| 1974 | if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1975 | return nreclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | XQM_STATS_INC(xqmstats.xs_qm_dqwants); |
| 1977 | goto tryagain; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * If the dquot is inactive, we are assured that it is |
| 1982 | * not on the mplist or the hashlist, and that makes our |
| 1983 | * life easier. |
| 1984 | */ |
| 1985 | if (dqp->dq_flags & XFS_DQ_INACTIVE) { |
| 1986 | ASSERT(dqp->q_mount == NULL); |
| 1987 | ASSERT(! XFS_DQ_IS_DIRTY(dqp)); |
| 1988 | ASSERT(dqp->HL_PREVP == NULL); |
| 1989 | ASSERT(dqp->MPL_PREVP == NULL); |
| 1990 | XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims); |
| 1991 | nextdqp = dqp->dq_flnext; |
| 1992 | goto off_freelist; |
| 1993 | } |
| 1994 | |
| 1995 | ASSERT(dqp->MPL_PREVP); |
| 1996 | /* |
| 1997 | * Try to grab the flush lock. If this dquot is in the process of |
| 1998 | * getting flushed to disk, we don't want to reclaim it. |
| 1999 | */ |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 2000 | if (!xfs_dqflock_nowait(dqp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | xfs_dqunlock(dqp); |
| 2002 | dqp = dqp->dq_flnext; |
| 2003 | continue; |
| 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * We have the flush lock so we know that this is not in the |
| 2008 | * process of being flushed. So, if this is dirty, flush it |
| 2009 | * DELWRI so that we don't get a freelist infested with |
| 2010 | * dirty dquots. |
| 2011 | */ |
| 2012 | if (XFS_DQ_IS_DIRTY(dqp)) { |
David Chinner | 3c56836 | 2008-04-10 12:20:24 +1000 | [diff] [blame] | 2013 | int error; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2014 | |
| 2015 | trace_xfs_dqshake_dirty(dqp); |
| 2016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | /* |
| 2018 | * We flush it delayed write, so don't bother |
| 2019 | * releasing the mplock. |
| 2020 | */ |
David Chinner | 3c56836 | 2008-04-10 12:20:24 +1000 | [diff] [blame] | 2021 | error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI); |
| 2022 | if (error) { |
| 2023 | xfs_fs_cmn_err(CE_WARN, dqp->q_mount, |
| 2024 | "xfs_qm_dqflush_all: dquot %p flush failed", dqp); |
| 2025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | xfs_dqunlock(dqp); /* dqflush unlocks dqflock */ |
| 2027 | dqp = dqp->dq_flnext; |
| 2028 | continue; |
| 2029 | } |
| 2030 | /* |
| 2031 | * We're trying to get the hashlock out of order. This races |
| 2032 | * with dqlookup; so, we giveup and goto the next dquot if |
| 2033 | * we couldn't get the hashlock. This way, we won't starve |
| 2034 | * a dqlookup process that holds the hashlock that is |
| 2035 | * waiting for the freelist lock. |
| 2036 | */ |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 2037 | if (!mutex_trylock(&dqp->q_hash->qh_lock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | xfs_dqfunlock(dqp); |
| 2039 | xfs_dqunlock(dqp); |
| 2040 | dqp = dqp->dq_flnext; |
| 2041 | continue; |
| 2042 | } |
| 2043 | /* |
| 2044 | * This races with dquot allocation code as well as dqflush_all |
| 2045 | * and reclaim code. So, if we failed to grab the mplist lock, |
| 2046 | * giveup everything and start over. |
| 2047 | */ |
| 2048 | hash = dqp->q_hash; |
| 2049 | ASSERT(hash); |
| 2050 | if (! xfs_qm_mplist_nowait(dqp->q_mount)) { |
| 2051 | /* XXX put a sentinel so that we can come back here */ |
| 2052 | xfs_dqfunlock(dqp); |
| 2053 | xfs_dqunlock(dqp); |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 2054 | mutex_unlock(&hash->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | xfs_qm_freelist_unlock(xfs_Gqm); |
| 2056 | if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2057 | return nreclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | goto tryagain; |
| 2059 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2060 | |
| 2061 | trace_xfs_dqshake_unlink(dqp); |
| 2062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | #ifdef QUOTADEBUG |
| 2064 | cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 2065 | dqp, be32_to_cpu(dqp->q_core.d_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | #endif |
| 2067 | ASSERT(dqp->q_nrefs == 0); |
| 2068 | nextdqp = dqp->dq_flnext; |
| 2069 | XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp); |
| 2070 | XQM_HASHLIST_REMOVE(hash, dqp); |
| 2071 | xfs_dqfunlock(dqp); |
| 2072 | xfs_qm_mplist_unlock(dqp->q_mount); |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 2073 | mutex_unlock(&hash->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
| 2075 | off_freelist: |
| 2076 | XQM_FREELIST_REMOVE(dqp); |
| 2077 | xfs_dqunlock(dqp); |
| 2078 | nreclaimed++; |
| 2079 | XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims); |
| 2080 | xfs_qm_dqdestroy(dqp); |
| 2081 | dqp = nextdqp; |
| 2082 | } |
| 2083 | xfs_qm_freelist_unlock(xfs_Gqm); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2084 | return nreclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | /* |
| 2089 | * The kmem_shake interface is invoked when memory is running low. |
| 2090 | */ |
| 2091 | /* ARGSUSED */ |
| 2092 | STATIC int |
Al Viro | 51bfb75 | 2005-12-15 09:17:24 +0000 | [diff] [blame] | 2093 | xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | { |
| 2095 | int ndqused, nfree, n; |
| 2096 | |
| 2097 | if (!kmem_shake_allow(gfp_mask)) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2098 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | if (!xfs_Gqm) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2100 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
| 2102 | nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */ |
| 2103 | /* incore dquots in all f/s's */ |
| 2104 | ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree; |
| 2105 | |
| 2106 | ASSERT(ndqused >= 0); |
| 2107 | |
| 2108 | if (nfree <= ndqused && nfree < ndquot) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2109 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | |
| 2111 | ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */ |
| 2112 | n = nfree - ndqused - ndquot; /* # over target */ |
| 2113 | |
| 2114 | return xfs_qm_shake_freelist(MAX(nfree, n)); |
| 2115 | } |
| 2116 | |
| 2117 | |
| 2118 | /* |
| 2119 | * Just pop the least recently used dquot off the freelist and |
| 2120 | * recycle it. The returned dquot is locked. |
| 2121 | */ |
| 2122 | STATIC xfs_dquot_t * |
| 2123 | xfs_qm_dqreclaim_one(void) |
| 2124 | { |
| 2125 | xfs_dquot_t *dqpout; |
| 2126 | xfs_dquot_t *dqp; |
| 2127 | int restarts; |
| 2128 | int nflushes; |
| 2129 | |
| 2130 | restarts = 0; |
| 2131 | dqpout = NULL; |
| 2132 | nflushes = 0; |
| 2133 | |
| 2134 | /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */ |
| 2135 | startagain: |
| 2136 | xfs_qm_freelist_lock(xfs_Gqm); |
| 2137 | |
| 2138 | FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) { |
| 2139 | xfs_dqlock(dqp); |
| 2140 | |
| 2141 | /* |
| 2142 | * We are racing with dqlookup here. Naturally we don't |
| 2143 | * want to reclaim a dquot that lookup wants. We release the |
| 2144 | * freelist lock and start over, so that lookup will grab |
| 2145 | * both the dquot and the freelistlock. |
| 2146 | */ |
| 2147 | if (dqp->dq_flags & XFS_DQ_WANT) { |
| 2148 | ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2149 | |
| 2150 | trace_xfs_dqreclaim_want(dqp); |
| 2151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | xfs_dqunlock(dqp); |
| 2153 | xfs_qm_freelist_unlock(xfs_Gqm); |
| 2154 | if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2155 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | XQM_STATS_INC(xqmstats.xs_qm_dqwants); |
| 2157 | goto startagain; |
| 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * If the dquot is inactive, we are assured that it is |
| 2162 | * not on the mplist or the hashlist, and that makes our |
| 2163 | * life easier. |
| 2164 | */ |
| 2165 | if (dqp->dq_flags & XFS_DQ_INACTIVE) { |
| 2166 | ASSERT(dqp->q_mount == NULL); |
| 2167 | ASSERT(! XFS_DQ_IS_DIRTY(dqp)); |
| 2168 | ASSERT(dqp->HL_PREVP == NULL); |
| 2169 | ASSERT(dqp->MPL_PREVP == NULL); |
| 2170 | XQM_FREELIST_REMOVE(dqp); |
| 2171 | xfs_dqunlock(dqp); |
| 2172 | dqpout = dqp; |
| 2173 | XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims); |
| 2174 | break; |
| 2175 | } |
| 2176 | |
| 2177 | ASSERT(dqp->q_hash); |
| 2178 | ASSERT(dqp->MPL_PREVP); |
| 2179 | |
| 2180 | /* |
| 2181 | * Try to grab the flush lock. If this dquot is in the process of |
| 2182 | * getting flushed to disk, we don't want to reclaim it. |
| 2183 | */ |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 2184 | if (!xfs_dqflock_nowait(dqp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | xfs_dqunlock(dqp); |
| 2186 | continue; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | * We have the flush lock so we know that this is not in the |
| 2191 | * process of being flushed. So, if this is dirty, flush it |
| 2192 | * DELWRI so that we don't get a freelist infested with |
| 2193 | * dirty dquots. |
| 2194 | */ |
| 2195 | if (XFS_DQ_IS_DIRTY(dqp)) { |
David Chinner | 3c56836 | 2008-04-10 12:20:24 +1000 | [diff] [blame] | 2196 | int error; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2197 | |
| 2198 | trace_xfs_dqreclaim_dirty(dqp); |
| 2199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | /* |
| 2201 | * We flush it delayed write, so don't bother |
| 2202 | * releasing the freelist lock. |
| 2203 | */ |
David Chinner | 3c56836 | 2008-04-10 12:20:24 +1000 | [diff] [blame] | 2204 | error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI); |
| 2205 | if (error) { |
| 2206 | xfs_fs_cmn_err(CE_WARN, dqp->q_mount, |
| 2207 | "xfs_qm_dqreclaim: dquot %p flush failed", dqp); |
| 2208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | xfs_dqunlock(dqp); /* dqflush unlocks dqflock */ |
| 2210 | continue; |
| 2211 | } |
| 2212 | |
| 2213 | if (! xfs_qm_mplist_nowait(dqp->q_mount)) { |
| 2214 | xfs_dqfunlock(dqp); |
| 2215 | xfs_dqunlock(dqp); |
| 2216 | continue; |
| 2217 | } |
| 2218 | |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 2219 | if (!mutex_trylock(&dqp->q_hash->qh_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | goto mplistunlock; |
| 2221 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2222 | trace_xfs_dqreclaim_unlink(dqp); |
| 2223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | ASSERT(dqp->q_nrefs == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp); |
| 2226 | XQM_HASHLIST_REMOVE(dqp->q_hash, dqp); |
| 2227 | XQM_FREELIST_REMOVE(dqp); |
| 2228 | dqpout = dqp; |
Christoph Hellwig | c9a192d | 2009-02-09 08:47:22 +0100 | [diff] [blame] | 2229 | mutex_unlock(&dqp->q_hash->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | mplistunlock: |
| 2231 | xfs_qm_mplist_unlock(dqp->q_mount); |
| 2232 | xfs_dqfunlock(dqp); |
| 2233 | xfs_dqunlock(dqp); |
| 2234 | if (dqpout) |
| 2235 | break; |
| 2236 | } |
| 2237 | |
| 2238 | xfs_qm_freelist_unlock(xfs_Gqm); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2239 | return dqpout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | |
| 2243 | /*------------------------------------------------------------------*/ |
| 2244 | |
| 2245 | /* |
| 2246 | * Return a new incore dquot. Depending on the number of |
| 2247 | * dquots in the system, we either allocate a new one on the kernel heap, |
| 2248 | * or reclaim a free one. |
| 2249 | * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed |
| 2250 | * to reclaim an existing one from the freelist. |
| 2251 | */ |
| 2252 | boolean_t |
| 2253 | xfs_qm_dqalloc_incore( |
| 2254 | xfs_dquot_t **O_dqpp) |
| 2255 | { |
| 2256 | xfs_dquot_t *dqp; |
| 2257 | |
| 2258 | /* |
| 2259 | * Check against high water mark to see if we want to pop |
| 2260 | * a nincompoop dquot off the freelist. |
| 2261 | */ |
| 2262 | if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) { |
| 2263 | /* |
| 2264 | * Try to recycle a dquot from the freelist. |
| 2265 | */ |
| 2266 | if ((dqp = xfs_qm_dqreclaim_one())) { |
| 2267 | XQM_STATS_INC(xqmstats.xs_qm_dqreclaims); |
| 2268 | /* |
| 2269 | * Just zero the core here. The rest will get |
| 2270 | * reinitialized by caller. XXX we shouldn't even |
| 2271 | * do this zero ... |
| 2272 | */ |
| 2273 | memset(&dqp->q_core, 0, sizeof(dqp->q_core)); |
| 2274 | *O_dqpp = dqp; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2275 | return B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | } |
| 2277 | XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses); |
| 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | * Allocate a brand new dquot on the kernel heap and return it |
| 2282 | * to the caller to initialize. |
| 2283 | */ |
| 2284 | ASSERT(xfs_Gqm->qm_dqzone != NULL); |
| 2285 | *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP); |
| 2286 | atomic_inc(&xfs_Gqm->qm_totaldquots); |
| 2287 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2288 | return B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | |
| 2292 | /* |
| 2293 | * Start a transaction and write the incore superblock changes to |
| 2294 | * disk. flags parameter indicates which fields have changed. |
| 2295 | */ |
| 2296 | int |
| 2297 | xfs_qm_write_sb_changes( |
| 2298 | xfs_mount_t *mp, |
| 2299 | __int64_t flags) |
| 2300 | { |
| 2301 | xfs_trans_t *tp; |
| 2302 | int error; |
| 2303 | |
| 2304 | #ifdef QUOTADEBUG |
| 2305 | cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname); |
| 2306 | #endif |
| 2307 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE); |
| 2308 | if ((error = xfs_trans_reserve(tp, 0, |
| 2309 | mp->m_sb.sb_sectsize + 128, 0, |
| 2310 | 0, |
| 2311 | XFS_DEFAULT_LOG_COUNT))) { |
| 2312 | xfs_trans_cancel(tp, 0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2313 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | xfs_mod_sb(tp, flags); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 2317 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 2319 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | /* --------------- utility functions for vnodeops ---------------- */ |
| 2324 | |
| 2325 | |
| 2326 | /* |
| 2327 | * Given an inode, a uid and gid (from cred_t) make sure that we have |
| 2328 | * allocated relevant dquot(s) on disk, and that we won't exceed inode |
| 2329 | * quotas by creating this file. |
| 2330 | * This also attaches dquot(s) to the given inode after locking it, |
| 2331 | * and returns the dquots corresponding to the uid and/or gid. |
| 2332 | * |
| 2333 | * in : inode (unlocked) |
| 2334 | * out : udquot, gdquot with references taken and unlocked |
| 2335 | */ |
| 2336 | int |
| 2337 | xfs_qm_vop_dqalloc( |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2338 | struct xfs_inode *ip, |
| 2339 | uid_t uid, |
| 2340 | gid_t gid, |
| 2341 | prid_t prid, |
| 2342 | uint flags, |
| 2343 | struct xfs_dquot **O_udqpp, |
| 2344 | struct xfs_dquot **O_gdqpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2346 | struct xfs_mount *mp = ip->i_mount; |
| 2347 | struct xfs_dquot *uq, *gq; |
| 2348 | int error; |
| 2349 | uint lockflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2351 | if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | return 0; |
| 2353 | |
| 2354 | lockflags = XFS_ILOCK_EXCL; |
| 2355 | xfs_ilock(ip, lockflags); |
| 2356 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 2357 | if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | gid = ip->i_d.di_gid; |
| 2359 | |
| 2360 | /* |
| 2361 | * Attach the dquot(s) to this inode, doing a dquot allocation |
| 2362 | * if necessary. The dquot(s) will not be locked. |
| 2363 | */ |
| 2364 | if (XFS_NOT_DQATTACHED(mp, ip)) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2365 | error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC); |
| 2366 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | xfs_iunlock(ip, lockflags); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2368 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | uq = gq = NULL; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2373 | if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | if (ip->i_d.di_uid != uid) { |
| 2375 | /* |
| 2376 | * What we need is the dquot that has this uid, and |
| 2377 | * if we send the inode to dqget, the uid of the inode |
| 2378 | * takes priority over what's sent in the uid argument. |
| 2379 | * We must unlock inode here before calling dqget if |
| 2380 | * we're not sending the inode, because otherwise |
| 2381 | * we'll deadlock by doing trans_reserve while |
| 2382 | * holding ilock. |
| 2383 | */ |
| 2384 | xfs_iunlock(ip, lockflags); |
| 2385 | if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid, |
| 2386 | XFS_DQ_USER, |
| 2387 | XFS_QMOPT_DQALLOC | |
| 2388 | XFS_QMOPT_DOWARN, |
| 2389 | &uq))) { |
| 2390 | ASSERT(error != ENOENT); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2391 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | } |
| 2393 | /* |
| 2394 | * Get the ilock in the right order. |
| 2395 | */ |
| 2396 | xfs_dqunlock(uq); |
| 2397 | lockflags = XFS_ILOCK_SHARED; |
| 2398 | xfs_ilock(ip, lockflags); |
| 2399 | } else { |
| 2400 | /* |
| 2401 | * Take an extra reference, because we'll return |
| 2402 | * this to caller |
| 2403 | */ |
| 2404 | ASSERT(ip->i_udquot); |
| 2405 | uq = ip->i_udquot; |
| 2406 | xfs_dqlock(uq); |
| 2407 | XFS_DQHOLD(uq); |
| 2408 | xfs_dqunlock(uq); |
| 2409 | } |
| 2410 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2411 | if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | if (ip->i_d.di_gid != gid) { |
| 2413 | xfs_iunlock(ip, lockflags); |
| 2414 | if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid, |
| 2415 | XFS_DQ_GROUP, |
| 2416 | XFS_QMOPT_DQALLOC | |
| 2417 | XFS_QMOPT_DOWARN, |
| 2418 | &gq))) { |
| 2419 | if (uq) |
| 2420 | xfs_qm_dqrele(uq); |
| 2421 | ASSERT(error != ENOENT); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2422 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | } |
| 2424 | xfs_dqunlock(gq); |
| 2425 | lockflags = XFS_ILOCK_SHARED; |
| 2426 | xfs_ilock(ip, lockflags); |
| 2427 | } else { |
| 2428 | ASSERT(ip->i_gdquot); |
| 2429 | gq = ip->i_gdquot; |
| 2430 | xfs_dqlock(gq); |
| 2431 | XFS_DQHOLD(gq); |
| 2432 | xfs_dqunlock(gq); |
| 2433 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2434 | } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) { |
| 2435 | if (ip->i_d.di_projid != prid) { |
| 2436 | xfs_iunlock(ip, lockflags); |
| 2437 | if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid, |
| 2438 | XFS_DQ_PROJ, |
| 2439 | XFS_QMOPT_DQALLOC | |
| 2440 | XFS_QMOPT_DOWARN, |
| 2441 | &gq))) { |
| 2442 | if (uq) |
| 2443 | xfs_qm_dqrele(uq); |
| 2444 | ASSERT(error != ENOENT); |
| 2445 | return (error); |
| 2446 | } |
| 2447 | xfs_dqunlock(gq); |
| 2448 | lockflags = XFS_ILOCK_SHARED; |
| 2449 | xfs_ilock(ip, lockflags); |
| 2450 | } else { |
| 2451 | ASSERT(ip->i_gdquot); |
| 2452 | gq = ip->i_gdquot; |
| 2453 | xfs_dqlock(gq); |
| 2454 | XFS_DQHOLD(gq); |
| 2455 | xfs_dqunlock(gq); |
| 2456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | } |
| 2458 | if (uq) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2459 | trace_xfs_dquot_dqalloc(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
| 2461 | xfs_iunlock(ip, lockflags); |
| 2462 | if (O_udqpp) |
| 2463 | *O_udqpp = uq; |
| 2464 | else if (uq) |
| 2465 | xfs_qm_dqrele(uq); |
| 2466 | if (O_gdqpp) |
| 2467 | *O_gdqpp = gq; |
| 2468 | else if (gq) |
| 2469 | xfs_qm_dqrele(gq); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2470 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * Actually transfer ownership, and do dquot modifications. |
| 2475 | * These were already reserved. |
| 2476 | */ |
| 2477 | xfs_dquot_t * |
| 2478 | xfs_qm_vop_chown( |
| 2479 | xfs_trans_t *tp, |
| 2480 | xfs_inode_t *ip, |
| 2481 | xfs_dquot_t **IO_olddq, |
| 2482 | xfs_dquot_t *newdq) |
| 2483 | { |
| 2484 | xfs_dquot_t *prevdq; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 2485 | uint bfield = XFS_IS_REALTIME_INODE(ip) ? |
| 2486 | XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT; |
| 2487 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2488 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2489 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount)); |
| 2491 | |
| 2492 | /* old dquot */ |
| 2493 | prevdq = *IO_olddq; |
| 2494 | ASSERT(prevdq); |
| 2495 | ASSERT(prevdq != newdq); |
| 2496 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 2497 | xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks)); |
| 2498 | xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | |
| 2500 | /* the sparkling new dquot */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 2501 | xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks); |
| 2502 | xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | |
| 2504 | /* |
| 2505 | * Take an extra reference, because the inode |
| 2506 | * is going to keep this dquot pointer even |
| 2507 | * after the trans_commit. |
| 2508 | */ |
| 2509 | xfs_dqlock(newdq); |
| 2510 | XFS_DQHOLD(newdq); |
| 2511 | xfs_dqunlock(newdq); |
| 2512 | *IO_olddq = newdq; |
| 2513 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2514 | return prevdq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2518 | * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | */ |
| 2520 | int |
| 2521 | xfs_qm_vop_chown_reserve( |
| 2522 | xfs_trans_t *tp, |
| 2523 | xfs_inode_t *ip, |
| 2524 | xfs_dquot_t *udqp, |
| 2525 | xfs_dquot_t *gdqp, |
| 2526 | uint flags) |
| 2527 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2528 | xfs_mount_t *mp = ip->i_mount; |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 2529 | uint delblks, blkflags, prjflags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2531 | int error; |
| 2532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2534 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
| 2536 | |
| 2537 | delblks = ip->i_delayed_blks; |
| 2538 | delblksudq = delblksgdq = unresudq = unresgdq = NULL; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 2539 | blkflags = XFS_IS_REALTIME_INODE(ip) ? |
| 2540 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | |
| 2542 | if (XFS_IS_UQUOTA_ON(mp) && udqp && |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 2543 | ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | delblksudq = udqp; |
| 2545 | /* |
| 2546 | * If there are delayed allocation blocks, then we have to |
| 2547 | * unreserve those from the old dquot, and add them to the |
| 2548 | * new dquot. |
| 2549 | */ |
| 2550 | if (delblks) { |
| 2551 | ASSERT(ip->i_udquot); |
| 2552 | unresudq = ip->i_udquot; |
| 2553 | } |
| 2554 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2555 | if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) { |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 2556 | if (XFS_IS_PQUOTA_ON(ip->i_mount) && |
| 2557 | ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id)) |
| 2558 | prjflags = XFS_QMOPT_ENOSPC; |
| 2559 | |
| 2560 | if (prjflags || |
| 2561 | (XFS_IS_GQUOTA_ON(ip->i_mount) && |
| 2562 | ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2563 | delblksgdq = gdqp; |
| 2564 | if (delblks) { |
| 2565 | ASSERT(ip->i_gdquot); |
| 2566 | unresgdq = ip->i_gdquot; |
| 2567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount, |
| 2572 | delblksudq, delblksgdq, ip->i_d.di_nblocks, 1, |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 2573 | flags | blkflags | prjflags))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | return (error); |
| 2575 | |
| 2576 | /* |
| 2577 | * Do the delayed blks reservations/unreservations now. Since, these |
| 2578 | * are done without the help of a transaction, if a reservation fails |
| 2579 | * its previous reservations won't be automatically undone by trans |
| 2580 | * code. So, we have to do it manually here. |
| 2581 | */ |
| 2582 | if (delblks) { |
| 2583 | /* |
| 2584 | * Do the reservations first. Unreservation can't fail. |
| 2585 | */ |
| 2586 | ASSERT(delblksudq || delblksgdq); |
| 2587 | ASSERT(unresudq || unresgdq); |
| 2588 | if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount, |
| 2589 | delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0, |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 2590 | flags | blkflags | prjflags))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | return (error); |
| 2592 | xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount, |
| 2593 | unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 2594 | blkflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | return (0); |
| 2598 | } |
| 2599 | |
| 2600 | int |
| 2601 | xfs_qm_vop_rename_dqattach( |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2602 | struct xfs_inode **i_tab) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2604 | struct xfs_mount *mp = i_tab[0]->i_mount; |
| 2605 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2607 | if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp)) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2608 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2610 | for (i = 0; (i < 4 && i_tab[i]); i++) { |
| 2611 | struct xfs_inode *ip = i_tab[i]; |
| 2612 | int error; |
| 2613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | /* |
| 2615 | * Watch out for duplicate entries in the table. |
| 2616 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2617 | if (i == 0 || ip != i_tab[i-1]) { |
| 2618 | if (XFS_NOT_DQATTACHED(mp, ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | error = xfs_qm_dqattach(ip, 0); |
| 2620 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2621 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2625 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | void |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2629 | xfs_qm_vop_create_dqattach( |
| 2630 | struct xfs_trans *tp, |
| 2631 | struct xfs_inode *ip, |
| 2632 | struct xfs_dquot *udqp, |
| 2633 | struct xfs_dquot *gdqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2635 | struct xfs_mount *mp = tp->t_mountp; |
| 2636 | |
| 2637 | if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | return; |
| 2639 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2640 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2641 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | |
| 2643 | if (udqp) { |
| 2644 | xfs_dqlock(udqp); |
| 2645 | XFS_DQHOLD(udqp); |
| 2646 | xfs_dqunlock(udqp); |
| 2647 | ASSERT(ip->i_udquot == NULL); |
| 2648 | ip->i_udquot = udqp; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2649 | ASSERT(XFS_IS_UQUOTA_ON(mp)); |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 2650 | ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1); |
| 2652 | } |
| 2653 | if (gdqp) { |
| 2654 | xfs_dqlock(gdqp); |
| 2655 | XFS_DQHOLD(gdqp); |
| 2656 | xfs_dqunlock(gdqp); |
| 2657 | ASSERT(ip->i_gdquot == NULL); |
| 2658 | ip->i_gdquot = gdqp; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2659 | ASSERT(XFS_IS_OQUOTA_ON(mp)); |
| 2660 | ASSERT((XFS_IS_GQUOTA_ON(mp) ? |
Nathan Scott | ee2a4f7 | 2006-01-11 15:33:36 +1100 | [diff] [blame] | 2661 | ip->i_d.di_gid : ip->i_d.di_projid) == |
| 2662 | be32_to_cpu(gdqp->q_core.d_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | /* ------------- list stuff -----------------*/ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2668 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | xfs_qm_freelist_init(xfs_frlist_t *ql) |
| 2670 | { |
| 2671 | ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql; |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 2672 | mutex_init(&ql->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | ql->qh_version = 0; |
| 2674 | ql->qh_nelems = 0; |
| 2675 | } |
| 2676 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2677 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | xfs_qm_freelist_destroy(xfs_frlist_t *ql) |
| 2679 | { |
| 2680 | xfs_dquot_t *dqp, *nextdqp; |
| 2681 | |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 2682 | mutex_lock(&ql->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | for (dqp = ql->qh_next; |
| 2684 | dqp != (xfs_dquot_t *)ql; ) { |
| 2685 | xfs_dqlock(dqp); |
| 2686 | nextdqp = dqp->dq_flnext; |
| 2687 | #ifdef QUOTADEBUG |
| 2688 | cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp); |
| 2689 | #endif |
| 2690 | XQM_FREELIST_REMOVE(dqp); |
| 2691 | xfs_dqunlock(dqp); |
| 2692 | xfs_qm_dqdestroy(dqp); |
| 2693 | dqp = nextdqp; |
| 2694 | } |
Nathan Scott | 20722a9 | 2006-03-14 13:19:08 +1100 | [diff] [blame] | 2695 | mutex_unlock(&ql->qh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | mutex_destroy(&ql->qh_lock); |
| 2697 | |
| 2698 | ASSERT(ql->qh_nelems == 0); |
| 2699 | } |
| 2700 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2701 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq) |
| 2703 | { |
| 2704 | dq->dq_flnext = ql->qh_next; |
| 2705 | dq->dq_flprev = (xfs_dquot_t *)ql; |
| 2706 | ql->qh_next = dq; |
| 2707 | dq->dq_flnext->dq_flprev = dq; |
| 2708 | xfs_Gqm->qm_dqfreelist.qh_nelems++; |
| 2709 | xfs_Gqm->qm_dqfreelist.qh_version++; |
| 2710 | } |
| 2711 | |
| 2712 | void |
| 2713 | xfs_qm_freelist_unlink(xfs_dquot_t *dq) |
| 2714 | { |
| 2715 | xfs_dquot_t *next = dq->dq_flnext; |
| 2716 | xfs_dquot_t *prev = dq->dq_flprev; |
| 2717 | |
| 2718 | next->dq_flprev = prev; |
| 2719 | prev->dq_flnext = next; |
| 2720 | dq->dq_flnext = dq->dq_flprev = dq; |
| 2721 | xfs_Gqm->qm_dqfreelist.qh_nelems--; |
| 2722 | xfs_Gqm->qm_dqfreelist.qh_version++; |
| 2723 | } |
| 2724 | |
| 2725 | void |
| 2726 | xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq) |
| 2727 | { |
| 2728 | xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq); |
| 2729 | } |