David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Quota change tags are associated with each transaction that allocates or |
| 12 | * deallocates space. Those changes are accumulated locally to each node (in a |
| 13 | * per-node file) and then are periodically synced to the quota file. This |
| 14 | * avoids the bottleneck of constantly touching the quota file, but introduces |
| 15 | * fuzziness in the current usage value of IDs that are being used on different |
| 16 | * nodes in the cluster simultaneously. So, it is possible for a user on |
| 17 | * multiple nodes to overrun their quota, but that overrun is controlable. |
| 18 | * Since quota tags are part of transactions, there is no need to a quota check |
| 19 | * program to be run on node crashes or anything like that. |
| 20 | * |
| 21 | * There are couple of knobs that let the administrator manage the quota |
| 22 | * fuzziness. "quota_quantum" sets the maximum time a quota change can be |
| 23 | * sitting on one node before being synced to the quota file. (The default is |
| 24 | * 60 seconds.) Another knob, "quota_scale" controls how quickly the frequency |
| 25 | * of quota file syncs increases as the user moves closer to their limit. The |
| 26 | * more frequent the syncs, the more accurate the quota enforcement, but that |
| 27 | * means that there is more contention between the nodes for the quota file. |
| 28 | * The default value is one. This sets the maximum theoretical quota overrun |
| 29 | * (with infinite node with infinite bandwidth) to twice the user's limit. (In |
| 30 | * practice, the maximum overrun you see should be much less.) A "quota_scale" |
| 31 | * number greater than one makes quota syncs more frequent and reduces the |
| 32 | * maximum overrun. Numbers less than one (but greater than zero) make quota |
| 33 | * syncs less frequent. |
| 34 | * |
| 35 | * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of |
| 36 | * the quota file, so it is not being constantly read. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/sched.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/completion.h> |
| 43 | #include <linux/buffer_head.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 44 | #include <linux/sort.h> |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 45 | #include <linux/fs.h> |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 46 | #include <linux/bio.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 47 | #include <linux/gfs2_ondisk.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 48 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 49 | |
| 50 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 51 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 52 | #include "bmap.h" |
| 53 | #include "glock.h" |
| 54 | #include "glops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | #include "log.h" |
| 56 | #include "meta_io.h" |
| 57 | #include "quota.h" |
| 58 | #include "rgrp.h" |
| 59 | #include "super.h" |
| 60 | #include "trans.h" |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 61 | #include "inode.h" |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 62 | #include "ops_file.h" |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 63 | #include "ops_address.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 64 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 65 | |
| 66 | #define QUOTA_USER 1 |
| 67 | #define QUOTA_GROUP 0 |
| 68 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 69 | static u64 qd2offset(struct gfs2_quota_data *qd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 70 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 71 | u64 offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 72 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 73 | offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 74 | offset *= sizeof(struct gfs2_quota); |
| 75 | |
| 76 | return offset; |
| 77 | } |
| 78 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 79 | static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 80 | struct gfs2_quota_data **qdp) |
| 81 | { |
| 82 | struct gfs2_quota_data *qd; |
| 83 | int error; |
| 84 | |
| 85 | qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL); |
| 86 | if (!qd) |
| 87 | return -ENOMEM; |
| 88 | |
| 89 | qd->qd_count = 1; |
| 90 | qd->qd_id = id; |
| 91 | if (user) |
| 92 | set_bit(QDF_USER, &qd->qd_flags); |
| 93 | qd->qd_slot = -1; |
| 94 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 95 | error = gfs2_glock_get(sdp, 2 * (u64)id + !user, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 96 | &gfs2_quota_glops, CREATE, &qd->qd_gl); |
| 97 | if (error) |
| 98 | goto fail; |
| 99 | |
| 100 | error = gfs2_lvb_hold(qd->qd_gl); |
| 101 | gfs2_glock_put(qd->qd_gl); |
| 102 | if (error) |
| 103 | goto fail; |
| 104 | |
| 105 | *qdp = qd; |
| 106 | |
| 107 | return 0; |
| 108 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 109 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 110 | kfree(qd); |
| 111 | return error; |
| 112 | } |
| 113 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 114 | static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 115 | struct gfs2_quota_data **qdp) |
| 116 | { |
| 117 | struct gfs2_quota_data *qd = NULL, *new_qd = NULL; |
| 118 | int error, found; |
| 119 | |
| 120 | *qdp = NULL; |
| 121 | |
| 122 | for (;;) { |
| 123 | found = 0; |
| 124 | spin_lock(&sdp->sd_quota_spin); |
| 125 | list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { |
| 126 | if (qd->qd_id == id && |
| 127 | !test_bit(QDF_USER, &qd->qd_flags) == !user) { |
| 128 | qd->qd_count++; |
| 129 | found = 1; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (!found) |
| 135 | qd = NULL; |
| 136 | |
| 137 | if (!qd && new_qd) { |
| 138 | qd = new_qd; |
| 139 | list_add(&qd->qd_list, &sdp->sd_quota_list); |
| 140 | atomic_inc(&sdp->sd_quota_count); |
| 141 | new_qd = NULL; |
| 142 | } |
| 143 | |
| 144 | spin_unlock(&sdp->sd_quota_spin); |
| 145 | |
| 146 | if (qd || !create) { |
| 147 | if (new_qd) { |
| 148 | gfs2_lvb_unhold(new_qd->qd_gl); |
| 149 | kfree(new_qd); |
| 150 | } |
| 151 | *qdp = qd; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | error = qd_alloc(sdp, user, id, &new_qd); |
| 156 | if (error) |
| 157 | return error; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static void qd_hold(struct gfs2_quota_data *qd) |
| 162 | { |
| 163 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 164 | |
| 165 | spin_lock(&sdp->sd_quota_spin); |
| 166 | gfs2_assert(sdp, qd->qd_count); |
| 167 | qd->qd_count++; |
| 168 | spin_unlock(&sdp->sd_quota_spin); |
| 169 | } |
| 170 | |
| 171 | static void qd_put(struct gfs2_quota_data *qd) |
| 172 | { |
| 173 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 174 | spin_lock(&sdp->sd_quota_spin); |
| 175 | gfs2_assert(sdp, qd->qd_count); |
| 176 | if (!--qd->qd_count) |
| 177 | qd->qd_last_touched = jiffies; |
| 178 | spin_unlock(&sdp->sd_quota_spin); |
| 179 | } |
| 180 | |
| 181 | static int slot_get(struct gfs2_quota_data *qd) |
| 182 | { |
| 183 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 184 | unsigned int c, o = 0, b; |
| 185 | unsigned char byte = 0; |
| 186 | |
| 187 | spin_lock(&sdp->sd_quota_spin); |
| 188 | |
| 189 | if (qd->qd_slot_count++) { |
| 190 | spin_unlock(&sdp->sd_quota_spin); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | for (c = 0; c < sdp->sd_quota_chunks; c++) |
| 195 | for (o = 0; o < PAGE_SIZE; o++) { |
| 196 | byte = sdp->sd_quota_bitmap[c][o]; |
| 197 | if (byte != 0xFF) |
| 198 | goto found; |
| 199 | } |
| 200 | |
| 201 | goto fail; |
| 202 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 203 | found: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 204 | for (b = 0; b < 8; b++) |
| 205 | if (!(byte & (1 << b))) |
| 206 | break; |
| 207 | qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b; |
| 208 | |
| 209 | if (qd->qd_slot >= sdp->sd_quota_slots) |
| 210 | goto fail; |
| 211 | |
| 212 | sdp->sd_quota_bitmap[c][o] |= 1 << b; |
| 213 | |
| 214 | spin_unlock(&sdp->sd_quota_spin); |
| 215 | |
| 216 | return 0; |
| 217 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 218 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 219 | qd->qd_slot_count--; |
| 220 | spin_unlock(&sdp->sd_quota_spin); |
| 221 | return -ENOSPC; |
| 222 | } |
| 223 | |
| 224 | static void slot_hold(struct gfs2_quota_data *qd) |
| 225 | { |
| 226 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 227 | |
| 228 | spin_lock(&sdp->sd_quota_spin); |
| 229 | gfs2_assert(sdp, qd->qd_slot_count); |
| 230 | qd->qd_slot_count++; |
| 231 | spin_unlock(&sdp->sd_quota_spin); |
| 232 | } |
| 233 | |
| 234 | static void slot_put(struct gfs2_quota_data *qd) |
| 235 | { |
| 236 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 237 | |
| 238 | spin_lock(&sdp->sd_quota_spin); |
| 239 | gfs2_assert(sdp, qd->qd_slot_count); |
| 240 | if (!--qd->qd_slot_count) { |
| 241 | gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0); |
| 242 | qd->qd_slot = -1; |
| 243 | } |
| 244 | spin_unlock(&sdp->sd_quota_spin); |
| 245 | } |
| 246 | |
| 247 | static int bh_get(struct gfs2_quota_data *qd) |
| 248 | { |
| 249 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 250 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 251 | unsigned int block, offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 252 | struct buffer_head *bh; |
| 253 | int error; |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 254 | struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 255 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 256 | mutex_lock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 257 | |
| 258 | if (qd->qd_bh_count++) { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 259 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | block = qd->qd_slot / sdp->sd_qc_per_block; |
| 264 | offset = qd->qd_slot % sdp->sd_qc_per_block;; |
| 265 | |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 266 | bh_map.b_size = 1 << ip->i_inode.i_blkbits; |
| 267 | error = gfs2_block_map(&ip->i_inode, block, 0, &bh_map); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 268 | if (error) |
| 269 | goto fail; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 270 | error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 271 | if (error) |
| 272 | goto fail; |
| 273 | error = -EIO; |
| 274 | if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) |
| 275 | goto fail_brelse; |
| 276 | |
| 277 | qd->qd_bh = bh; |
| 278 | qd->qd_bh_qc = (struct gfs2_quota_change *) |
| 279 | (bh->b_data + sizeof(struct gfs2_meta_header) + |
| 280 | offset * sizeof(struct gfs2_quota_change)); |
| 281 | |
Josef Whiter | 2e95b66 | 2007-02-20 00:03:29 -0500 | [diff] [blame^] | 282 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 283 | |
| 284 | return 0; |
| 285 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 286 | fail_brelse: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 287 | brelse(bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 288 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 289 | qd->qd_bh_count--; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 290 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 291 | return error; |
| 292 | } |
| 293 | |
| 294 | static void bh_put(struct gfs2_quota_data *qd) |
| 295 | { |
| 296 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 297 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 298 | mutex_lock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 299 | gfs2_assert(sdp, qd->qd_bh_count); |
| 300 | if (!--qd->qd_bh_count) { |
| 301 | brelse(qd->qd_bh); |
| 302 | qd->qd_bh = NULL; |
| 303 | qd->qd_bh_qc = NULL; |
| 304 | } |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 305 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp) |
| 309 | { |
| 310 | struct gfs2_quota_data *qd = NULL; |
| 311 | int error; |
| 312 | int found = 0; |
| 313 | |
| 314 | *qdp = NULL; |
| 315 | |
| 316 | if (sdp->sd_vfs->s_flags & MS_RDONLY) |
| 317 | return 0; |
| 318 | |
| 319 | spin_lock(&sdp->sd_quota_spin); |
| 320 | |
| 321 | list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { |
| 322 | if (test_bit(QDF_LOCKED, &qd->qd_flags) || |
| 323 | !test_bit(QDF_CHANGE, &qd->qd_flags) || |
| 324 | qd->qd_sync_gen >= sdp->sd_quota_sync_gen) |
| 325 | continue; |
| 326 | |
| 327 | list_move_tail(&qd->qd_list, &sdp->sd_quota_list); |
| 328 | |
| 329 | set_bit(QDF_LOCKED, &qd->qd_flags); |
| 330 | gfs2_assert_warn(sdp, qd->qd_count); |
| 331 | qd->qd_count++; |
| 332 | qd->qd_change_sync = qd->qd_change; |
| 333 | gfs2_assert_warn(sdp, qd->qd_slot_count); |
| 334 | qd->qd_slot_count++; |
| 335 | found = 1; |
| 336 | |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | if (!found) |
| 341 | qd = NULL; |
| 342 | |
| 343 | spin_unlock(&sdp->sd_quota_spin); |
| 344 | |
| 345 | if (qd) { |
| 346 | gfs2_assert_warn(sdp, qd->qd_change_sync); |
| 347 | error = bh_get(qd); |
| 348 | if (error) { |
| 349 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
| 350 | slot_put(qd); |
| 351 | qd_put(qd); |
| 352 | return error; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | *qdp = qd; |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int qd_trylock(struct gfs2_quota_data *qd) |
| 362 | { |
| 363 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 364 | |
| 365 | if (sdp->sd_vfs->s_flags & MS_RDONLY) |
| 366 | return 0; |
| 367 | |
| 368 | spin_lock(&sdp->sd_quota_spin); |
| 369 | |
| 370 | if (test_bit(QDF_LOCKED, &qd->qd_flags) || |
| 371 | !test_bit(QDF_CHANGE, &qd->qd_flags)) { |
| 372 | spin_unlock(&sdp->sd_quota_spin); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | list_move_tail(&qd->qd_list, &sdp->sd_quota_list); |
| 377 | |
| 378 | set_bit(QDF_LOCKED, &qd->qd_flags); |
| 379 | gfs2_assert_warn(sdp, qd->qd_count); |
| 380 | qd->qd_count++; |
| 381 | qd->qd_change_sync = qd->qd_change; |
| 382 | gfs2_assert_warn(sdp, qd->qd_slot_count); |
| 383 | qd->qd_slot_count++; |
| 384 | |
| 385 | spin_unlock(&sdp->sd_quota_spin); |
| 386 | |
| 387 | gfs2_assert_warn(sdp, qd->qd_change_sync); |
| 388 | if (bh_get(qd)) { |
| 389 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
| 390 | slot_put(qd); |
| 391 | qd_put(qd); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | static void qd_unlock(struct gfs2_quota_data *qd) |
| 399 | { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 400 | gfs2_assert_warn(qd->qd_gl->gl_sbd, |
| 401 | test_bit(QDF_LOCKED, &qd->qd_flags)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
| 403 | bh_put(qd); |
| 404 | slot_put(qd); |
| 405 | qd_put(qd); |
| 406 | } |
| 407 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 408 | static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 409 | struct gfs2_quota_data **qdp) |
| 410 | { |
| 411 | int error; |
| 412 | |
| 413 | error = qd_get(sdp, user, id, create, qdp); |
| 414 | if (error) |
| 415 | return error; |
| 416 | |
| 417 | error = slot_get(*qdp); |
| 418 | if (error) |
| 419 | goto fail; |
| 420 | |
| 421 | error = bh_get(*qdp); |
| 422 | if (error) |
| 423 | goto fail_slot; |
| 424 | |
| 425 | return 0; |
| 426 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 427 | fail_slot: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 428 | slot_put(*qdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 429 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | qd_put(*qdp); |
| 431 | return error; |
| 432 | } |
| 433 | |
| 434 | static void qdsb_put(struct gfs2_quota_data *qd) |
| 435 | { |
| 436 | bh_put(qd); |
| 437 | slot_put(qd); |
| 438 | qd_put(qd); |
| 439 | } |
| 440 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 441 | int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 442 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 443 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 444 | struct gfs2_alloc *al = &ip->i_alloc; |
| 445 | struct gfs2_quota_data **qd = al->al_qd; |
| 446 | int error; |
| 447 | |
| 448 | if (gfs2_assert_warn(sdp, !al->al_qd_num) || |
| 449 | gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) |
| 450 | return -EIO; |
| 451 | |
| 452 | if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) |
| 453 | return 0; |
| 454 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 455 | error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 456 | if (error) |
| 457 | goto out; |
| 458 | al->al_qd_num++; |
| 459 | qd++; |
| 460 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 461 | error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 462 | if (error) |
| 463 | goto out; |
| 464 | al->al_qd_num++; |
| 465 | qd++; |
| 466 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 467 | if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 468 | error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd); |
| 469 | if (error) |
| 470 | goto out; |
| 471 | al->al_qd_num++; |
| 472 | qd++; |
| 473 | } |
| 474 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 475 | if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 476 | error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd); |
| 477 | if (error) |
| 478 | goto out; |
| 479 | al->al_qd_num++; |
| 480 | qd++; |
| 481 | } |
| 482 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 483 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 484 | if (error) |
| 485 | gfs2_quota_unhold(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 486 | return error; |
| 487 | } |
| 488 | |
| 489 | void gfs2_quota_unhold(struct gfs2_inode *ip) |
| 490 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 491 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 492 | struct gfs2_alloc *al = &ip->i_alloc; |
| 493 | unsigned int x; |
| 494 | |
| 495 | gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)); |
| 496 | |
| 497 | for (x = 0; x < al->al_qd_num; x++) { |
| 498 | qdsb_put(al->al_qd[x]); |
| 499 | al->al_qd[x] = NULL; |
| 500 | } |
| 501 | al->al_qd_num = 0; |
| 502 | } |
| 503 | |
| 504 | static int sort_qd(const void *a, const void *b) |
| 505 | { |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 506 | const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a; |
| 507 | const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 508 | |
| 509 | if (!test_bit(QDF_USER, &qd_a->qd_flags) != |
| 510 | !test_bit(QDF_USER, &qd_b->qd_flags)) { |
| 511 | if (test_bit(QDF_USER, &qd_a->qd_flags)) |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 512 | return -1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 513 | else |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 514 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 515 | } |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 516 | if (qd_a->qd_id < qd_b->qd_id) |
| 517 | return -1; |
| 518 | if (qd_a->qd_id > qd_b->qd_id) |
| 519 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 520 | |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 521 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 524 | static void do_qc(struct gfs2_quota_data *qd, s64 change) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 525 | { |
| 526 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 527 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 528 | struct gfs2_quota_change *qc = qd->qd_bh_qc; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 529 | s64 x; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 530 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 531 | mutex_lock(&sdp->sd_quota_mutex); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 532 | gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 533 | |
| 534 | if (!test_bit(QDF_CHANGE, &qd->qd_flags)) { |
| 535 | qc->qc_change = 0; |
| 536 | qc->qc_flags = 0; |
| 537 | if (test_bit(QDF_USER, &qd->qd_flags)) |
| 538 | qc->qc_flags = cpu_to_be32(GFS2_QCF_USER); |
| 539 | qc->qc_id = cpu_to_be32(qd->qd_id); |
| 540 | } |
| 541 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 542 | x = be64_to_cpu(qc->qc_change) + change; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 543 | qc->qc_change = cpu_to_be64(x); |
| 544 | |
| 545 | spin_lock(&sdp->sd_quota_spin); |
| 546 | qd->qd_change = x; |
| 547 | spin_unlock(&sdp->sd_quota_spin); |
| 548 | |
| 549 | if (!x) { |
| 550 | gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags)); |
| 551 | clear_bit(QDF_CHANGE, &qd->qd_flags); |
| 552 | qc->qc_flags = 0; |
| 553 | qc->qc_id = 0; |
| 554 | slot_put(qd); |
| 555 | qd_put(qd); |
| 556 | } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) { |
| 557 | qd_hold(qd); |
| 558 | slot_hold(qd); |
| 559 | } |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 560 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 561 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 564 | /** |
| 565 | * gfs2_adjust_quota |
| 566 | * |
| 567 | * This function was mostly borrowed from gfs2_block_truncate_page which was |
| 568 | * in turn mostly borrowed from ext3 |
| 569 | */ |
| 570 | static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 571 | s64 change, struct gfs2_quota_data *qd) |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 572 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 573 | struct inode *inode = &ip->i_inode; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 574 | struct address_space *mapping = inode->i_mapping; |
| 575 | unsigned long index = loc >> PAGE_CACHE_SHIFT; |
| 576 | unsigned offset = loc & (PAGE_CACHE_SHIFT - 1); |
| 577 | unsigned blocksize, iblock, pos; |
| 578 | struct buffer_head *bh; |
| 579 | struct page *page; |
| 580 | void *kaddr; |
| 581 | __be64 *ptr; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 582 | s64 value; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 583 | int err = -EIO; |
| 584 | |
| 585 | page = grab_cache_page(mapping, index); |
| 586 | if (!page) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | blocksize = inode->i_sb->s_blocksize; |
| 590 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); |
| 591 | |
| 592 | if (!page_has_buffers(page)) |
| 593 | create_empty_buffers(page, blocksize, 0); |
| 594 | |
| 595 | bh = page_buffers(page); |
| 596 | pos = blocksize; |
| 597 | while (offset >= pos) { |
| 598 | bh = bh->b_this_page; |
| 599 | iblock++; |
| 600 | pos += blocksize; |
| 601 | } |
| 602 | |
| 603 | if (!buffer_mapped(bh)) { |
| 604 | gfs2_get_block(inode, iblock, bh, 1); |
| 605 | if (!buffer_mapped(bh)) |
| 606 | goto unlock; |
| 607 | } |
| 608 | |
| 609 | if (PageUptodate(page)) |
| 610 | set_buffer_uptodate(bh); |
| 611 | |
| 612 | if (!buffer_uptodate(bh)) { |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 613 | ll_rw_block(READ_META, 1, &bh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 614 | wait_on_buffer(bh); |
| 615 | if (!buffer_uptodate(bh)) |
| 616 | goto unlock; |
| 617 | } |
| 618 | |
| 619 | gfs2_trans_add_bh(ip->i_gl, bh, 0); |
| 620 | |
| 621 | kaddr = kmap_atomic(page, KM_USER0); |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 622 | ptr = kaddr + offset; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 623 | value = (s64)be64_to_cpu(*ptr) + change; |
| 624 | *ptr = cpu_to_be64(value); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 625 | flush_dcache_page(page); |
| 626 | kunmap_atomic(kaddr, KM_USER0); |
| 627 | err = 0; |
| 628 | qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 629 | qd->qd_qb.qb_value = cpu_to_be64(value); |
| 630 | unlock: |
| 631 | unlock_page(page); |
| 632 | page_cache_release(page); |
| 633 | return err; |
| 634 | } |
| 635 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 636 | static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda) |
| 637 | { |
| 638 | struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 639 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 640 | unsigned int data_blocks, ind_blocks; |
| 641 | struct gfs2_holder *ghs, i_gh; |
| 642 | unsigned int qx, x; |
| 643 | struct gfs2_quota_data *qd; |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 644 | loff_t offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 645 | unsigned int nalloc = 0; |
| 646 | struct gfs2_alloc *al = NULL; |
| 647 | int error; |
| 648 | |
| 649 | gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), |
| 650 | &data_blocks, &ind_blocks); |
| 651 | |
| 652 | ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL); |
| 653 | if (!ghs) |
| 654 | return -ENOMEM; |
| 655 | |
| 656 | sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL); |
| 657 | for (qx = 0; qx < num_qd; qx++) { |
| 658 | error = gfs2_glock_nq_init(qda[qx]->qd_gl, |
| 659 | LM_ST_EXCLUSIVE, |
| 660 | GL_NOCACHE, &ghs[qx]); |
| 661 | if (error) |
| 662 | goto out; |
| 663 | } |
| 664 | |
| 665 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 666 | if (error) |
| 667 | goto out; |
| 668 | |
| 669 | for (x = 0; x < num_qd; x++) { |
| 670 | int alloc_required; |
| 671 | |
| 672 | offset = qd2offset(qda[x]); |
| 673 | error = gfs2_write_alloc_required(ip, offset, |
| 674 | sizeof(struct gfs2_quota), |
| 675 | &alloc_required); |
| 676 | if (error) |
| 677 | goto out_gunlock; |
| 678 | if (alloc_required) |
| 679 | nalloc++; |
| 680 | } |
| 681 | |
| 682 | if (nalloc) { |
| 683 | al = gfs2_alloc_get(ip); |
| 684 | |
| 685 | al->al_requested = nalloc * (data_blocks + ind_blocks); |
| 686 | |
| 687 | error = gfs2_inplace_reserve(ip); |
| 688 | if (error) |
| 689 | goto out_alloc; |
| 690 | |
| 691 | error = gfs2_trans_begin(sdp, |
| 692 | al->al_rgd->rd_ri.ri_length + |
| 693 | num_qd * data_blocks + |
| 694 | nalloc * ind_blocks + |
| 695 | RES_DINODE + num_qd + |
| 696 | RES_STATFS, 0); |
| 697 | if (error) |
| 698 | goto out_ipres; |
| 699 | } else { |
| 700 | error = gfs2_trans_begin(sdp, |
| 701 | num_qd * data_blocks + |
| 702 | RES_DINODE + num_qd, 0); |
| 703 | if (error) |
| 704 | goto out_gunlock; |
| 705 | } |
| 706 | |
| 707 | for (x = 0; x < num_qd; x++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 708 | qd = qda[x]; |
| 709 | offset = qd2offset(qd); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 710 | error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 711 | (struct gfs2_quota_data *) |
| 712 | qd->qd_gl->gl_lvb); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 713 | if (error) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 714 | goto out_end_trans; |
| 715 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 716 | do_qc(qd, -qd->qd_change_sync); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | error = 0; |
| 720 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 721 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 723 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 724 | if (nalloc) |
| 725 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 726 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 727 | if (nalloc) |
| 728 | gfs2_alloc_put(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 729 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 730 | gfs2_glock_dq_uninit(&i_gh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 731 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 732 | while (qx--) |
| 733 | gfs2_glock_dq_uninit(&ghs[qx]); |
| 734 | kfree(ghs); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 735 | gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 736 | return error; |
| 737 | } |
| 738 | |
| 739 | static int do_glock(struct gfs2_quota_data *qd, int force_refresh, |
| 740 | struct gfs2_holder *q_gh) |
| 741 | { |
| 742 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 743 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 744 | struct gfs2_holder i_gh; |
Al Viro | b5bc9e8 | 2006-10-13 23:31:55 -0400 | [diff] [blame] | 745 | struct gfs2_quota_host q; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 746 | char buf[sizeof(struct gfs2_quota)]; |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 747 | struct file_ra_state ra_state; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 748 | int error; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 749 | struct gfs2_quota_lvb *qlvb; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 750 | |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 751 | file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 752 | restart: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 753 | error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh); |
| 754 | if (error) |
| 755 | return error; |
| 756 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 757 | qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 758 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 759 | if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) { |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 760 | loff_t pos; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 761 | gfs2_glock_dq_uninit(q_gh); |
| 762 | error = gfs2_glock_nq_init(qd->qd_gl, |
| 763 | LM_ST_EXCLUSIVE, GL_NOCACHE, |
| 764 | q_gh); |
| 765 | if (error) |
| 766 | return error; |
| 767 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 768 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 769 | if (error) |
| 770 | goto fail; |
| 771 | |
| 772 | memset(buf, 0, sizeof(struct gfs2_quota)); |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 773 | pos = qd2offset(qd); |
Steven Whitehouse | 0d42e54 | 2006-06-20 16:13:49 -0400 | [diff] [blame] | 774 | error = gfs2_internal_read(ip, &ra_state, buf, |
| 775 | &pos, sizeof(struct gfs2_quota)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 776 | if (error < 0) |
| 777 | goto fail_gunlock; |
| 778 | |
| 779 | gfs2_glock_dq_uninit(&i_gh); |
| 780 | |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 781 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 782 | gfs2_quota_in(&q, buf); |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 783 | qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb; |
| 784 | qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC); |
| 785 | qlvb->__pad = 0; |
| 786 | qlvb->qb_limit = cpu_to_be64(q.qu_limit); |
| 787 | qlvb->qb_warn = cpu_to_be64(q.qu_warn); |
| 788 | qlvb->qb_value = cpu_to_be64(q.qu_value); |
| 789 | qd->qd_qb = *qlvb; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 790 | |
| 791 | if (gfs2_glock_is_blocking(qd->qd_gl)) { |
| 792 | gfs2_glock_dq_uninit(q_gh); |
| 793 | force_refresh = 0; |
| 794 | goto restart; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return 0; |
| 799 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 800 | fail_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 801 | gfs2_glock_dq_uninit(&i_gh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 802 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 803 | gfs2_glock_dq_uninit(q_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 804 | return error; |
| 805 | } |
| 806 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 807 | int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 808 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 809 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 810 | struct gfs2_alloc *al = &ip->i_alloc; |
| 811 | unsigned int x; |
| 812 | int error = 0; |
| 813 | |
| 814 | gfs2_quota_hold(ip, uid, gid); |
| 815 | |
| 816 | if (capable(CAP_SYS_RESOURCE) || |
| 817 | sdp->sd_args.ar_quota != GFS2_QUOTA_ON) |
| 818 | return 0; |
| 819 | |
| 820 | sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *), |
| 821 | sort_qd, NULL); |
| 822 | |
| 823 | for (x = 0; x < al->al_qd_num; x++) { |
| 824 | error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]); |
| 825 | if (error) |
| 826 | break; |
| 827 | } |
| 828 | |
| 829 | if (!error) |
| 830 | set_bit(GIF_QD_LOCKED, &ip->i_flags); |
| 831 | else { |
| 832 | while (x--) |
| 833 | gfs2_glock_dq_uninit(&al->al_qd_ghs[x]); |
| 834 | gfs2_quota_unhold(ip); |
| 835 | } |
| 836 | |
| 837 | return error; |
| 838 | } |
| 839 | |
| 840 | static int need_sync(struct gfs2_quota_data *qd) |
| 841 | { |
| 842 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 843 | struct gfs2_tune *gt = &sdp->sd_tune; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 844 | s64 value; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 845 | unsigned int num, den; |
| 846 | int do_sync = 1; |
| 847 | |
| 848 | if (!qd->qd_qb.qb_limit) |
| 849 | return 0; |
| 850 | |
| 851 | spin_lock(&sdp->sd_quota_spin); |
| 852 | value = qd->qd_change; |
| 853 | spin_unlock(&sdp->sd_quota_spin); |
| 854 | |
| 855 | spin_lock(>->gt_spin); |
| 856 | num = gt->gt_quota_scale_num; |
| 857 | den = gt->gt_quota_scale_den; |
| 858 | spin_unlock(>->gt_spin); |
| 859 | |
| 860 | if (value < 0) |
| 861 | do_sync = 0; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 862 | else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >= |
| 863 | (s64)be64_to_cpu(qd->qd_qb.qb_limit)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 864 | do_sync = 0; |
| 865 | else { |
| 866 | value *= gfs2_jindex_size(sdp) * num; |
| 867 | do_div(value, den); |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 868 | value += (s64)be64_to_cpu(qd->qd_qb.qb_value); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 869 | if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 870 | do_sync = 0; |
| 871 | } |
| 872 | |
| 873 | return do_sync; |
| 874 | } |
| 875 | |
| 876 | void gfs2_quota_unlock(struct gfs2_inode *ip) |
| 877 | { |
| 878 | struct gfs2_alloc *al = &ip->i_alloc; |
| 879 | struct gfs2_quota_data *qda[4]; |
| 880 | unsigned int count = 0; |
| 881 | unsigned int x; |
| 882 | |
| 883 | if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags)) |
| 884 | goto out; |
| 885 | |
| 886 | for (x = 0; x < al->al_qd_num; x++) { |
| 887 | struct gfs2_quota_data *qd; |
| 888 | int sync; |
| 889 | |
| 890 | qd = al->al_qd[x]; |
| 891 | sync = need_sync(qd); |
| 892 | |
| 893 | gfs2_glock_dq_uninit(&al->al_qd_ghs[x]); |
| 894 | |
| 895 | if (sync && qd_trylock(qd)) |
| 896 | qda[count++] = qd; |
| 897 | } |
| 898 | |
| 899 | if (count) { |
| 900 | do_sync(count, qda); |
| 901 | for (x = 0; x < count; x++) |
| 902 | qd_unlock(qda[x]); |
| 903 | } |
| 904 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 905 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 906 | gfs2_quota_unhold(ip); |
| 907 | } |
| 908 | |
| 909 | #define MAX_LINE 256 |
| 910 | |
| 911 | static int print_message(struct gfs2_quota_data *qd, char *type) |
| 912 | { |
| 913 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 914 | |
Steven Whitehouse | 02630a1 | 2006-07-03 11:20:06 -0400 | [diff] [blame] | 915 | printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n", |
| 916 | sdp->sd_fsname, type, |
| 917 | (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group", |
| 918 | qd->qd_id); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 923 | int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 924 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 925 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 926 | struct gfs2_alloc *al = &ip->i_alloc; |
| 927 | struct gfs2_quota_data *qd; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 928 | s64 value; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 929 | unsigned int x; |
| 930 | int error = 0; |
| 931 | |
| 932 | if (!test_bit(GIF_QD_LOCKED, &ip->i_flags)) |
| 933 | return 0; |
| 934 | |
| 935 | if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON) |
| 936 | return 0; |
| 937 | |
| 938 | for (x = 0; x < al->al_qd_num; x++) { |
| 939 | qd = al->al_qd[x]; |
| 940 | |
| 941 | if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) || |
| 942 | (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags)))) |
| 943 | continue; |
| 944 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 945 | value = (s64)be64_to_cpu(qd->qd_qb.qb_value); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 946 | spin_lock(&sdp->sd_quota_spin); |
| 947 | value += qd->qd_change; |
| 948 | spin_unlock(&sdp->sd_quota_spin); |
| 949 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 950 | if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 951 | print_message(qd, "exceeded"); |
| 952 | error = -EDQUOT; |
| 953 | break; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 954 | } else if (be64_to_cpu(qd->qd_qb.qb_warn) && |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 955 | (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value && |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 956 | time_after_eq(jiffies, qd->qd_last_warn + |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 957 | gfs2_tune_get(sdp, |
| 958 | gt_quota_warn_period) * HZ)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 959 | error = print_message(qd, "warning"); |
| 960 | qd->qd_last_warn = jiffies; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | return error; |
| 965 | } |
| 966 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 967 | void gfs2_quota_change(struct gfs2_inode *ip, s64 change, |
| 968 | u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 969 | { |
| 970 | struct gfs2_alloc *al = &ip->i_alloc; |
| 971 | struct gfs2_quota_data *qd; |
| 972 | unsigned int x; |
| 973 | unsigned int found = 0; |
| 974 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 975 | if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 976 | return; |
| 977 | if (ip->i_di.di_flags & GFS2_DIF_SYSTEM) |
| 978 | return; |
| 979 | |
| 980 | for (x = 0; x < al->al_qd_num; x++) { |
| 981 | qd = al->al_qd[x]; |
| 982 | |
| 983 | if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) || |
| 984 | (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) { |
| 985 | do_qc(qd, change); |
| 986 | found++; |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | int gfs2_quota_sync(struct gfs2_sbd *sdp) |
| 992 | { |
| 993 | struct gfs2_quota_data **qda; |
| 994 | unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync); |
| 995 | unsigned int num_qd; |
| 996 | unsigned int x; |
| 997 | int error = 0; |
| 998 | |
| 999 | sdp->sd_quota_sync_gen++; |
| 1000 | |
| 1001 | qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL); |
| 1002 | if (!qda) |
| 1003 | return -ENOMEM; |
| 1004 | |
| 1005 | do { |
| 1006 | num_qd = 0; |
| 1007 | |
| 1008 | for (;;) { |
| 1009 | error = qd_fish(sdp, qda + num_qd); |
| 1010 | if (error || !qda[num_qd]) |
| 1011 | break; |
| 1012 | if (++num_qd == max_qd) |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | if (num_qd) { |
| 1017 | if (!error) |
| 1018 | error = do_sync(num_qd, qda); |
| 1019 | if (!error) |
| 1020 | for (x = 0; x < num_qd; x++) |
| 1021 | qda[x]->qd_sync_gen = |
| 1022 | sdp->sd_quota_sync_gen; |
| 1023 | |
| 1024 | for (x = 0; x < num_qd; x++) |
| 1025 | qd_unlock(qda[x]); |
| 1026 | } |
| 1027 | } while (!error && num_qd == max_qd); |
| 1028 | |
| 1029 | kfree(qda); |
| 1030 | |
| 1031 | return error; |
| 1032 | } |
| 1033 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1034 | int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1035 | { |
| 1036 | struct gfs2_quota_data *qd; |
| 1037 | struct gfs2_holder q_gh; |
| 1038 | int error; |
| 1039 | |
| 1040 | error = qd_get(sdp, user, id, CREATE, &qd); |
| 1041 | if (error) |
| 1042 | return error; |
| 1043 | |
| 1044 | error = do_glock(qd, FORCE, &q_gh); |
| 1045 | if (!error) |
| 1046 | gfs2_glock_dq_uninit(&q_gh); |
| 1047 | |
| 1048 | qd_put(qd); |
| 1049 | |
| 1050 | return error; |
| 1051 | } |
| 1052 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1053 | int gfs2_quota_init(struct gfs2_sbd *sdp) |
| 1054 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1055 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1056 | unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift; |
| 1057 | unsigned int x, slot = 0; |
| 1058 | unsigned int found = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1059 | u64 dblock; |
| 1060 | u32 extlen = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1061 | int error; |
| 1062 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1063 | if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) || |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1064 | ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) { |
| 1065 | gfs2_consist_inode(ip); |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1066 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1067 | } |
| 1068 | sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1069 | sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1070 | |
| 1071 | error = -ENOMEM; |
| 1072 | |
| 1073 | sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks, |
| 1074 | sizeof(unsigned char *), GFP_KERNEL); |
| 1075 | if (!sdp->sd_quota_bitmap) |
| 1076 | return error; |
| 1077 | |
| 1078 | for (x = 0; x < sdp->sd_quota_chunks; x++) { |
| 1079 | sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1080 | if (!sdp->sd_quota_bitmap[x]) |
| 1081 | goto fail; |
| 1082 | } |
| 1083 | |
| 1084 | for (x = 0; x < blocks; x++) { |
| 1085 | struct buffer_head *bh; |
| 1086 | unsigned int y; |
| 1087 | |
| 1088 | if (!extlen) { |
| 1089 | int new = 0; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1090 | error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1091 | if (error) |
| 1092 | goto fail; |
| 1093 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1094 | error = -EIO; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1095 | bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); |
| 1096 | if (!bh) |
| 1097 | goto fail; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1098 | if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) { |
| 1099 | brelse(bh); |
| 1100 | goto fail; |
| 1101 | } |
| 1102 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1103 | for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1104 | y++, slot++) { |
Al Viro | b62f963 | 2006-10-13 23:46:46 -0400 | [diff] [blame] | 1105 | struct gfs2_quota_change_host qc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1106 | struct gfs2_quota_data *qd; |
| 1107 | |
| 1108 | gfs2_quota_change_in(&qc, bh->b_data + |
| 1109 | sizeof(struct gfs2_meta_header) + |
| 1110 | y * sizeof(struct gfs2_quota_change)); |
| 1111 | if (!qc.qc_change) |
| 1112 | continue; |
| 1113 | |
| 1114 | error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER), |
| 1115 | qc.qc_id, &qd); |
| 1116 | if (error) { |
| 1117 | brelse(bh); |
| 1118 | goto fail; |
| 1119 | } |
| 1120 | |
| 1121 | set_bit(QDF_CHANGE, &qd->qd_flags); |
| 1122 | qd->qd_change = qc.qc_change; |
| 1123 | qd->qd_slot = slot; |
| 1124 | qd->qd_slot_count = 1; |
| 1125 | qd->qd_last_touched = jiffies; |
| 1126 | |
| 1127 | spin_lock(&sdp->sd_quota_spin); |
| 1128 | gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1); |
| 1129 | list_add(&qd->qd_list, &sdp->sd_quota_list); |
| 1130 | atomic_inc(&sdp->sd_quota_count); |
| 1131 | spin_unlock(&sdp->sd_quota_spin); |
| 1132 | |
| 1133 | found++; |
| 1134 | } |
| 1135 | |
| 1136 | brelse(bh); |
| 1137 | dblock++; |
| 1138 | extlen--; |
| 1139 | } |
| 1140 | |
| 1141 | if (found) |
| 1142 | fs_info(sdp, "found %u quota changes\n", found); |
| 1143 | |
| 1144 | return 0; |
| 1145 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1146 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1147 | gfs2_quota_cleanup(sdp); |
| 1148 | return error; |
| 1149 | } |
| 1150 | |
| 1151 | void gfs2_quota_scan(struct gfs2_sbd *sdp) |
| 1152 | { |
| 1153 | struct gfs2_quota_data *qd, *safe; |
| 1154 | LIST_HEAD(dead); |
| 1155 | |
| 1156 | spin_lock(&sdp->sd_quota_spin); |
| 1157 | list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) { |
| 1158 | if (!qd->qd_count && |
| 1159 | time_after_eq(jiffies, qd->qd_last_touched + |
| 1160 | gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) { |
| 1161 | list_move(&qd->qd_list, &dead); |
| 1162 | gfs2_assert_warn(sdp, |
| 1163 | atomic_read(&sdp->sd_quota_count) > 0); |
| 1164 | atomic_dec(&sdp->sd_quota_count); |
| 1165 | } |
| 1166 | } |
| 1167 | spin_unlock(&sdp->sd_quota_spin); |
| 1168 | |
| 1169 | while (!list_empty(&dead)) { |
| 1170 | qd = list_entry(dead.next, struct gfs2_quota_data, qd_list); |
| 1171 | list_del(&qd->qd_list); |
| 1172 | |
| 1173 | gfs2_assert_warn(sdp, !qd->qd_change); |
| 1174 | gfs2_assert_warn(sdp, !qd->qd_slot_count); |
| 1175 | gfs2_assert_warn(sdp, !qd->qd_bh_count); |
| 1176 | |
| 1177 | gfs2_lvb_unhold(qd->qd_gl); |
| 1178 | kfree(qd); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | void gfs2_quota_cleanup(struct gfs2_sbd *sdp) |
| 1183 | { |
| 1184 | struct list_head *head = &sdp->sd_quota_list; |
| 1185 | struct gfs2_quota_data *qd; |
| 1186 | unsigned int x; |
| 1187 | |
| 1188 | spin_lock(&sdp->sd_quota_spin); |
| 1189 | while (!list_empty(head)) { |
| 1190 | qd = list_entry(head->prev, struct gfs2_quota_data, qd_list); |
| 1191 | |
| 1192 | if (qd->qd_count > 1 || |
| 1193 | (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) { |
| 1194 | list_move(&qd->qd_list, head); |
| 1195 | spin_unlock(&sdp->sd_quota_spin); |
| 1196 | schedule(); |
| 1197 | spin_lock(&sdp->sd_quota_spin); |
| 1198 | continue; |
| 1199 | } |
| 1200 | |
| 1201 | list_del(&qd->qd_list); |
| 1202 | atomic_dec(&sdp->sd_quota_count); |
| 1203 | spin_unlock(&sdp->sd_quota_spin); |
| 1204 | |
| 1205 | if (!qd->qd_count) { |
| 1206 | gfs2_assert_warn(sdp, !qd->qd_change); |
| 1207 | gfs2_assert_warn(sdp, !qd->qd_slot_count); |
| 1208 | } else |
| 1209 | gfs2_assert_warn(sdp, qd->qd_slot_count == 1); |
| 1210 | gfs2_assert_warn(sdp, !qd->qd_bh_count); |
| 1211 | |
| 1212 | gfs2_lvb_unhold(qd->qd_gl); |
| 1213 | kfree(qd); |
| 1214 | |
| 1215 | spin_lock(&sdp->sd_quota_spin); |
| 1216 | } |
| 1217 | spin_unlock(&sdp->sd_quota_spin); |
| 1218 | |
| 1219 | gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count)); |
| 1220 | |
| 1221 | if (sdp->sd_quota_bitmap) { |
| 1222 | for (x = 0; x < sdp->sd_quota_chunks; x++) |
| 1223 | kfree(sdp->sd_quota_bitmap[x]); |
| 1224 | kfree(sdp->sd_quota_bitmap); |
| 1225 | } |
| 1226 | } |
| 1227 | |