David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Bob Peterson | 0d0868b | 2007-12-11 18:51:25 -0600 | [diff] [blame] | 3 | * Copyright (C) 2004-2007 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> |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 48 | #include <linux/kthread.h> |
| 49 | #include <linux/freezer.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 50 | |
| 51 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 52 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 53 | #include "bmap.h" |
| 54 | #include "glock.h" |
| 55 | #include "glops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 56 | #include "log.h" |
| 57 | #include "meta_io.h" |
| 58 | #include "quota.h" |
| 59 | #include "rgrp.h" |
| 60 | #include "super.h" |
| 61 | #include "trans.h" |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 62 | #include "inode.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 | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 69 | struct gfs2_quota_host { |
| 70 | u64 qu_limit; |
| 71 | u64 qu_warn; |
| 72 | s64 qu_value; |
Abhijith Das | 2d9a4bb | 2007-08-15 11:25:05 -0500 | [diff] [blame] | 73 | u32 qu_ll_next; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | struct gfs2_quota_change_host { |
| 77 | u64 qc_change; |
| 78 | u32 qc_flags; /* GFS2_QCF_... */ |
| 79 | u32 qc_id; |
| 80 | }; |
| 81 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 82 | static LIST_HEAD(qd_lru_list); |
| 83 | static atomic_t qd_lru_count = ATOMIC_INIT(0); |
| 84 | static spinlock_t qd_lru_lock = SPIN_LOCK_UNLOCKED; |
| 85 | |
| 86 | int gfs2_shrink_qd_memory(int nr, gfp_t gfp_mask) |
| 87 | { |
| 88 | struct gfs2_quota_data *qd; |
| 89 | struct gfs2_sbd *sdp; |
| 90 | |
| 91 | if (nr == 0) |
| 92 | goto out; |
| 93 | |
| 94 | if (!(gfp_mask & __GFP_FS)) |
| 95 | return -1; |
| 96 | |
| 97 | spin_lock(&qd_lru_lock); |
| 98 | while (nr && !list_empty(&qd_lru_list)) { |
| 99 | qd = list_entry(qd_lru_list.next, |
| 100 | struct gfs2_quota_data, qd_reclaim); |
| 101 | sdp = qd->qd_gl->gl_sbd; |
| 102 | |
| 103 | /* Free from the filesystem-specific list */ |
| 104 | list_del(&qd->qd_list); |
| 105 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 106 | gfs2_assert_warn(sdp, !qd->qd_change); |
| 107 | gfs2_assert_warn(sdp, !qd->qd_slot_count); |
| 108 | gfs2_assert_warn(sdp, !qd->qd_bh_count); |
| 109 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame^] | 110 | gfs2_glock_put(qd->qd_gl); |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 111 | atomic_dec(&sdp->sd_quota_count); |
| 112 | |
| 113 | /* Delete it from the common reclaim list */ |
| 114 | list_del_init(&qd->qd_reclaim); |
| 115 | atomic_dec(&qd_lru_count); |
| 116 | spin_unlock(&qd_lru_lock); |
| 117 | kmem_cache_free(gfs2_quotad_cachep, qd); |
| 118 | spin_lock(&qd_lru_lock); |
| 119 | nr--; |
| 120 | } |
| 121 | spin_unlock(&qd_lru_lock); |
| 122 | |
| 123 | out: |
| 124 | return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100; |
| 125 | } |
| 126 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 127 | static u64 qd2offset(struct gfs2_quota_data *qd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 128 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 129 | u64 offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 130 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 131 | 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] | 132 | offset *= sizeof(struct gfs2_quota); |
| 133 | |
| 134 | return offset; |
| 135 | } |
| 136 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 137 | static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 138 | struct gfs2_quota_data **qdp) |
| 139 | { |
| 140 | struct gfs2_quota_data *qd; |
| 141 | int error; |
| 142 | |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 143 | qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 144 | if (!qd) |
| 145 | return -ENOMEM; |
| 146 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 147 | atomic_set(&qd->qd_count, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 148 | qd->qd_id = id; |
| 149 | if (user) |
| 150 | set_bit(QDF_USER, &qd->qd_flags); |
| 151 | qd->qd_slot = -1; |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 152 | INIT_LIST_HEAD(&qd->qd_reclaim); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 153 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 154 | error = gfs2_glock_get(sdp, 2 * (u64)id + !user, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 155 | &gfs2_quota_glops, CREATE, &qd->qd_gl); |
| 156 | if (error) |
| 157 | goto fail; |
| 158 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 159 | *qdp = qd; |
| 160 | |
| 161 | return 0; |
| 162 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 163 | fail: |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 164 | kmem_cache_free(gfs2_quotad_cachep, qd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 165 | return error; |
| 166 | } |
| 167 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 168 | 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] | 169 | struct gfs2_quota_data **qdp) |
| 170 | { |
| 171 | struct gfs2_quota_data *qd = NULL, *new_qd = NULL; |
| 172 | int error, found; |
| 173 | |
| 174 | *qdp = NULL; |
| 175 | |
| 176 | for (;;) { |
| 177 | found = 0; |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 178 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 179 | list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { |
| 180 | if (qd->qd_id == id && |
| 181 | !test_bit(QDF_USER, &qd->qd_flags) == !user) { |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 182 | if (!atomic_read(&qd->qd_count) && |
| 183 | !list_empty(&qd->qd_reclaim)) { |
| 184 | /* Remove it from reclaim list */ |
| 185 | list_del_init(&qd->qd_reclaim); |
| 186 | atomic_dec(&qd_lru_count); |
| 187 | } |
| 188 | atomic_inc(&qd->qd_count); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 189 | found = 1; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (!found) |
| 195 | qd = NULL; |
| 196 | |
| 197 | if (!qd && new_qd) { |
| 198 | qd = new_qd; |
| 199 | list_add(&qd->qd_list, &sdp->sd_quota_list); |
| 200 | atomic_inc(&sdp->sd_quota_count); |
| 201 | new_qd = NULL; |
| 202 | } |
| 203 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 204 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 205 | |
| 206 | if (qd || !create) { |
| 207 | if (new_qd) { |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame^] | 208 | gfs2_glock_put(new_qd->qd_gl); |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 209 | kmem_cache_free(gfs2_quotad_cachep, new_qd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 210 | } |
| 211 | *qdp = qd; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | error = qd_alloc(sdp, user, id, &new_qd); |
| 216 | if (error) |
| 217 | return error; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void qd_hold(struct gfs2_quota_data *qd) |
| 222 | { |
| 223 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 224 | gfs2_assert(sdp, atomic_read(&qd->qd_count)); |
| 225 | atomic_inc(&qd->qd_count); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void qd_put(struct gfs2_quota_data *qd) |
| 229 | { |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 230 | if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) { |
| 231 | /* Add to the reclaim list */ |
| 232 | list_add_tail(&qd->qd_reclaim, &qd_lru_list); |
| 233 | atomic_inc(&qd_lru_count); |
| 234 | spin_unlock(&qd_lru_lock); |
| 235 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static int slot_get(struct gfs2_quota_data *qd) |
| 239 | { |
| 240 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 241 | unsigned int c, o = 0, b; |
| 242 | unsigned char byte = 0; |
| 243 | |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 244 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 245 | |
| 246 | if (qd->qd_slot_count++) { |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 247 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | for (c = 0; c < sdp->sd_quota_chunks; c++) |
| 252 | for (o = 0; o < PAGE_SIZE; o++) { |
| 253 | byte = sdp->sd_quota_bitmap[c][o]; |
| 254 | if (byte != 0xFF) |
| 255 | goto found; |
| 256 | } |
| 257 | |
| 258 | goto fail; |
| 259 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 260 | found: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 261 | for (b = 0; b < 8; b++) |
| 262 | if (!(byte & (1 << b))) |
| 263 | break; |
| 264 | qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b; |
| 265 | |
| 266 | if (qd->qd_slot >= sdp->sd_quota_slots) |
| 267 | goto fail; |
| 268 | |
| 269 | sdp->sd_quota_bitmap[c][o] |= 1 << b; |
| 270 | |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 271 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 275 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 276 | qd->qd_slot_count--; |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 277 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 278 | return -ENOSPC; |
| 279 | } |
| 280 | |
| 281 | static void slot_hold(struct gfs2_quota_data *qd) |
| 282 | { |
| 283 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 284 | |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 285 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 286 | gfs2_assert(sdp, qd->qd_slot_count); |
| 287 | qd->qd_slot_count++; |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 288 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static void slot_put(struct gfs2_quota_data *qd) |
| 292 | { |
| 293 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 294 | |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 295 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 296 | gfs2_assert(sdp, qd->qd_slot_count); |
| 297 | if (!--qd->qd_slot_count) { |
| 298 | gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0); |
| 299 | qd->qd_slot = -1; |
| 300 | } |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 301 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int bh_get(struct gfs2_quota_data *qd) |
| 305 | { |
| 306 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 307 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 308 | unsigned int block, offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 309 | struct buffer_head *bh; |
| 310 | int error; |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 311 | struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 312 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 313 | mutex_lock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 314 | |
| 315 | if (qd->qd_bh_count++) { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 316 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | block = qd->qd_slot / sdp->sd_qc_per_block; |
Bob Peterson | 0d0868b | 2007-12-11 18:51:25 -0600 | [diff] [blame] | 321 | offset = qd->qd_slot % sdp->sd_qc_per_block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 322 | |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 323 | bh_map.b_size = 1 << ip->i_inode.i_blkbits; |
Bob Peterson | e9e1ef2 | 2007-12-10 14:13:27 -0600 | [diff] [blame] | 324 | error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 325 | if (error) |
| 326 | goto fail; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 327 | 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] | 328 | if (error) |
| 329 | goto fail; |
| 330 | error = -EIO; |
| 331 | if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) |
| 332 | goto fail_brelse; |
| 333 | |
| 334 | qd->qd_bh = bh; |
| 335 | qd->qd_bh_qc = (struct gfs2_quota_change *) |
| 336 | (bh->b_data + sizeof(struct gfs2_meta_header) + |
| 337 | offset * sizeof(struct gfs2_quota_change)); |
| 338 | |
Josef Whiter | 2e95b66 | 2007-02-20 00:03:29 -0500 | [diff] [blame] | 339 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 340 | |
| 341 | return 0; |
| 342 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 343 | fail_brelse: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 344 | brelse(bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 345 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 346 | qd->qd_bh_count--; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 347 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 348 | return error; |
| 349 | } |
| 350 | |
| 351 | static void bh_put(struct gfs2_quota_data *qd) |
| 352 | { |
| 353 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 354 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 355 | mutex_lock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 356 | gfs2_assert(sdp, qd->qd_bh_count); |
| 357 | if (!--qd->qd_bh_count) { |
| 358 | brelse(qd->qd_bh); |
| 359 | qd->qd_bh = NULL; |
| 360 | qd->qd_bh_qc = NULL; |
| 361 | } |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 362 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp) |
| 366 | { |
| 367 | struct gfs2_quota_data *qd = NULL; |
| 368 | int error; |
| 369 | int found = 0; |
| 370 | |
| 371 | *qdp = NULL; |
| 372 | |
| 373 | if (sdp->sd_vfs->s_flags & MS_RDONLY) |
| 374 | return 0; |
| 375 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 376 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 377 | |
| 378 | list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { |
| 379 | if (test_bit(QDF_LOCKED, &qd->qd_flags) || |
| 380 | !test_bit(QDF_CHANGE, &qd->qd_flags) || |
| 381 | qd->qd_sync_gen >= sdp->sd_quota_sync_gen) |
| 382 | continue; |
| 383 | |
| 384 | list_move_tail(&qd->qd_list, &sdp->sd_quota_list); |
| 385 | |
| 386 | set_bit(QDF_LOCKED, &qd->qd_flags); |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 387 | gfs2_assert_warn(sdp, atomic_read(&qd->qd_count)); |
| 388 | atomic_inc(&qd->qd_count); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 389 | qd->qd_change_sync = qd->qd_change; |
| 390 | gfs2_assert_warn(sdp, qd->qd_slot_count); |
| 391 | qd->qd_slot_count++; |
| 392 | found = 1; |
| 393 | |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | if (!found) |
| 398 | qd = NULL; |
| 399 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 400 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 401 | |
| 402 | if (qd) { |
| 403 | gfs2_assert_warn(sdp, qd->qd_change_sync); |
| 404 | error = bh_get(qd); |
| 405 | if (error) { |
| 406 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
| 407 | slot_put(qd); |
| 408 | qd_put(qd); |
| 409 | return error; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | *qdp = qd; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int qd_trylock(struct gfs2_quota_data *qd) |
| 419 | { |
| 420 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 421 | |
| 422 | if (sdp->sd_vfs->s_flags & MS_RDONLY) |
| 423 | return 0; |
| 424 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 425 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 426 | |
| 427 | if (test_bit(QDF_LOCKED, &qd->qd_flags) || |
| 428 | !test_bit(QDF_CHANGE, &qd->qd_flags)) { |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 429 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | list_move_tail(&qd->qd_list, &sdp->sd_quota_list); |
| 434 | |
| 435 | set_bit(QDF_LOCKED, &qd->qd_flags); |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 436 | gfs2_assert_warn(sdp, atomic_read(&qd->qd_count)); |
| 437 | atomic_inc(&qd->qd_count); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 438 | qd->qd_change_sync = qd->qd_change; |
| 439 | gfs2_assert_warn(sdp, qd->qd_slot_count); |
| 440 | qd->qd_slot_count++; |
| 441 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 442 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 443 | |
| 444 | gfs2_assert_warn(sdp, qd->qd_change_sync); |
| 445 | if (bh_get(qd)) { |
| 446 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
| 447 | slot_put(qd); |
| 448 | qd_put(qd); |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | static void qd_unlock(struct gfs2_quota_data *qd) |
| 456 | { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 457 | gfs2_assert_warn(qd->qd_gl->gl_sbd, |
| 458 | test_bit(QDF_LOCKED, &qd->qd_flags)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 459 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
| 460 | bh_put(qd); |
| 461 | slot_put(qd); |
| 462 | qd_put(qd); |
| 463 | } |
| 464 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 465 | 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] | 466 | struct gfs2_quota_data **qdp) |
| 467 | { |
| 468 | int error; |
| 469 | |
| 470 | error = qd_get(sdp, user, id, create, qdp); |
| 471 | if (error) |
| 472 | return error; |
| 473 | |
| 474 | error = slot_get(*qdp); |
| 475 | if (error) |
| 476 | goto fail; |
| 477 | |
| 478 | error = bh_get(*qdp); |
| 479 | if (error) |
| 480 | goto fail_slot; |
| 481 | |
| 482 | return 0; |
| 483 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 484 | fail_slot: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 485 | slot_put(*qdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 486 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 487 | qd_put(*qdp); |
| 488 | return error; |
| 489 | } |
| 490 | |
| 491 | static void qdsb_put(struct gfs2_quota_data *qd) |
| 492 | { |
| 493 | bh_put(qd); |
| 494 | slot_put(qd); |
| 495 | qd_put(qd); |
| 496 | } |
| 497 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 498 | int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 499 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 500 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 501 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 502 | struct gfs2_quota_data **qd = al->al_qd; |
| 503 | int error; |
| 504 | |
| 505 | if (gfs2_assert_warn(sdp, !al->al_qd_num) || |
| 506 | gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) |
| 507 | return -EIO; |
| 508 | |
| 509 | if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) |
| 510 | return 0; |
| 511 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 512 | 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] | 513 | if (error) |
| 514 | goto out; |
| 515 | al->al_qd_num++; |
| 516 | qd++; |
| 517 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 518 | 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] | 519 | if (error) |
| 520 | goto out; |
| 521 | al->al_qd_num++; |
| 522 | qd++; |
| 523 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 524 | if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 525 | error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd); |
| 526 | if (error) |
| 527 | goto out; |
| 528 | al->al_qd_num++; |
| 529 | qd++; |
| 530 | } |
| 531 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 532 | if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 533 | error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd); |
| 534 | if (error) |
| 535 | goto out; |
| 536 | al->al_qd_num++; |
| 537 | qd++; |
| 538 | } |
| 539 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 540 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 541 | if (error) |
| 542 | gfs2_quota_unhold(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 543 | return error; |
| 544 | } |
| 545 | |
| 546 | void gfs2_quota_unhold(struct gfs2_inode *ip) |
| 547 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 548 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 549 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 550 | unsigned int x; |
| 551 | |
| 552 | gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)); |
| 553 | |
| 554 | for (x = 0; x < al->al_qd_num; x++) { |
| 555 | qdsb_put(al->al_qd[x]); |
| 556 | al->al_qd[x] = NULL; |
| 557 | } |
| 558 | al->al_qd_num = 0; |
| 559 | } |
| 560 | |
| 561 | static int sort_qd(const void *a, const void *b) |
| 562 | { |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 563 | const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a; |
| 564 | 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] | 565 | |
| 566 | if (!test_bit(QDF_USER, &qd_a->qd_flags) != |
| 567 | !test_bit(QDF_USER, &qd_b->qd_flags)) { |
| 568 | if (test_bit(QDF_USER, &qd_a->qd_flags)) |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 569 | return -1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 570 | else |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 571 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 572 | } |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 573 | if (qd_a->qd_id < qd_b->qd_id) |
| 574 | return -1; |
| 575 | if (qd_a->qd_id > qd_b->qd_id) |
| 576 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 577 | |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 578 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 581 | static void do_qc(struct gfs2_quota_data *qd, s64 change) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 582 | { |
| 583 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 584 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 585 | struct gfs2_quota_change *qc = qd->qd_bh_qc; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 586 | s64 x; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 587 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 588 | mutex_lock(&sdp->sd_quota_mutex); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 589 | gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 590 | |
| 591 | if (!test_bit(QDF_CHANGE, &qd->qd_flags)) { |
| 592 | qc->qc_change = 0; |
| 593 | qc->qc_flags = 0; |
| 594 | if (test_bit(QDF_USER, &qd->qd_flags)) |
| 595 | qc->qc_flags = cpu_to_be32(GFS2_QCF_USER); |
| 596 | qc->qc_id = cpu_to_be32(qd->qd_id); |
| 597 | } |
| 598 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 599 | x = be64_to_cpu(qc->qc_change) + change; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 600 | qc->qc_change = cpu_to_be64(x); |
| 601 | |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 602 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 603 | qd->qd_change = x; |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 604 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 605 | |
| 606 | if (!x) { |
| 607 | gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags)); |
| 608 | clear_bit(QDF_CHANGE, &qd->qd_flags); |
| 609 | qc->qc_flags = 0; |
| 610 | qc->qc_id = 0; |
| 611 | slot_put(qd); |
| 612 | qd_put(qd); |
| 613 | } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) { |
| 614 | qd_hold(qd); |
| 615 | slot_hold(qd); |
| 616 | } |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 617 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 618 | mutex_unlock(&sdp->sd_quota_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 621 | static void gfs2_quota_in(struct gfs2_quota_host *qu, const void *buf) |
| 622 | { |
| 623 | const struct gfs2_quota *str = buf; |
| 624 | |
| 625 | qu->qu_limit = be64_to_cpu(str->qu_limit); |
| 626 | qu->qu_warn = be64_to_cpu(str->qu_warn); |
| 627 | qu->qu_value = be64_to_cpu(str->qu_value); |
Abhijith Das | 2d9a4bb | 2007-08-15 11:25:05 -0500 | [diff] [blame] | 628 | qu->qu_ll_next = be32_to_cpu(str->qu_ll_next); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static void gfs2_quota_out(const struct gfs2_quota_host *qu, void *buf) |
| 632 | { |
| 633 | struct gfs2_quota *str = buf; |
| 634 | |
| 635 | str->qu_limit = cpu_to_be64(qu->qu_limit); |
| 636 | str->qu_warn = cpu_to_be64(qu->qu_warn); |
| 637 | str->qu_value = cpu_to_be64(qu->qu_value); |
Abhijith Das | 2d9a4bb | 2007-08-15 11:25:05 -0500 | [diff] [blame] | 638 | str->qu_ll_next = cpu_to_be32(qu->qu_ll_next); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 639 | memset(&str->qu_reserved, 0, sizeof(str->qu_reserved)); |
| 640 | } |
| 641 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 642 | /** |
| 643 | * gfs2_adjust_quota |
| 644 | * |
| 645 | * This function was mostly borrowed from gfs2_block_truncate_page which was |
| 646 | * in turn mostly borrowed from ext3 |
| 647 | */ |
| 648 | static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 649 | s64 change, struct gfs2_quota_data *qd) |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 650 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 651 | struct inode *inode = &ip->i_inode; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 652 | struct address_space *mapping = inode->i_mapping; |
| 653 | unsigned long index = loc >> PAGE_CACHE_SHIFT; |
Abhijith Das | 1990e91 | 2007-05-31 17:52:02 -0500 | [diff] [blame] | 654 | unsigned offset = loc & (PAGE_CACHE_SIZE - 1); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 655 | unsigned blocksize, iblock, pos; |
| 656 | struct buffer_head *bh; |
| 657 | struct page *page; |
| 658 | void *kaddr; |
Abhijith Das | 1990e91 | 2007-05-31 17:52:02 -0500 | [diff] [blame] | 659 | char *ptr; |
| 660 | struct gfs2_quota_host qp; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 661 | s64 value; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 662 | int err = -EIO; |
| 663 | |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 664 | if (gfs2_is_stuffed(ip)) |
Abhijith Das | 0fd5355 | 2007-08-14 15:34:58 -0500 | [diff] [blame] | 665 | gfs2_unstuff_dinode(ip, NULL); |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 666 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 667 | page = grab_cache_page(mapping, index); |
| 668 | if (!page) |
| 669 | return -ENOMEM; |
| 670 | |
| 671 | blocksize = inode->i_sb->s_blocksize; |
| 672 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); |
| 673 | |
| 674 | if (!page_has_buffers(page)) |
| 675 | create_empty_buffers(page, blocksize, 0); |
| 676 | |
| 677 | bh = page_buffers(page); |
| 678 | pos = blocksize; |
| 679 | while (offset >= pos) { |
| 680 | bh = bh->b_this_page; |
| 681 | iblock++; |
| 682 | pos += blocksize; |
| 683 | } |
| 684 | |
| 685 | if (!buffer_mapped(bh)) { |
Bob Peterson | e9e1ef2 | 2007-12-10 14:13:27 -0600 | [diff] [blame] | 686 | gfs2_block_map(inode, iblock, bh, 1); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 687 | if (!buffer_mapped(bh)) |
| 688 | goto unlock; |
| 689 | } |
| 690 | |
| 691 | if (PageUptodate(page)) |
| 692 | set_buffer_uptodate(bh); |
| 693 | |
| 694 | if (!buffer_uptodate(bh)) { |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 695 | ll_rw_block(READ_META, 1, &bh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 696 | wait_on_buffer(bh); |
| 697 | if (!buffer_uptodate(bh)) |
| 698 | goto unlock; |
| 699 | } |
| 700 | |
| 701 | gfs2_trans_add_bh(ip->i_gl, bh, 0); |
| 702 | |
| 703 | kaddr = kmap_atomic(page, KM_USER0); |
Steven Whitehouse | 48fac17 | 2006-09-05 15:17:12 -0400 | [diff] [blame] | 704 | ptr = kaddr + offset; |
Abhijith Das | 1990e91 | 2007-05-31 17:52:02 -0500 | [diff] [blame] | 705 | gfs2_quota_in(&qp, ptr); |
| 706 | qp.qu_value += change; |
| 707 | value = qp.qu_value; |
| 708 | gfs2_quota_out(&qp, ptr); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 709 | flush_dcache_page(page); |
| 710 | kunmap_atomic(kaddr, KM_USER0); |
| 711 | err = 0; |
| 712 | qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 713 | qd->qd_qb.qb_value = cpu_to_be64(value); |
Abhijith Das | 2a87ab0 | 2007-05-16 17:02:19 -0500 | [diff] [blame] | 714 | ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC); |
| 715 | ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 716 | unlock: |
| 717 | unlock_page(page); |
| 718 | page_cache_release(page); |
| 719 | return err; |
| 720 | } |
| 721 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda) |
| 723 | { |
| 724 | struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 725 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 726 | unsigned int data_blocks, ind_blocks; |
| 727 | struct gfs2_holder *ghs, i_gh; |
| 728 | unsigned int qx, x; |
| 729 | struct gfs2_quota_data *qd; |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 730 | loff_t offset; |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 731 | unsigned int nalloc = 0, blocks; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 732 | struct gfs2_alloc *al = NULL; |
| 733 | int error; |
| 734 | |
| 735 | gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), |
| 736 | &data_blocks, &ind_blocks); |
| 737 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 738 | ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 739 | if (!ghs) |
| 740 | return -ENOMEM; |
| 741 | |
| 742 | sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL); |
| 743 | for (qx = 0; qx < num_qd; qx++) { |
| 744 | error = gfs2_glock_nq_init(qda[qx]->qd_gl, |
| 745 | LM_ST_EXCLUSIVE, |
| 746 | GL_NOCACHE, &ghs[qx]); |
| 747 | if (error) |
| 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 752 | if (error) |
| 753 | goto out; |
| 754 | |
| 755 | for (x = 0; x < num_qd; x++) { |
| 756 | int alloc_required; |
| 757 | |
| 758 | offset = qd2offset(qda[x]); |
| 759 | error = gfs2_write_alloc_required(ip, offset, |
| 760 | sizeof(struct gfs2_quota), |
| 761 | &alloc_required); |
| 762 | if (error) |
| 763 | goto out_gunlock; |
| 764 | if (alloc_required) |
| 765 | nalloc++; |
| 766 | } |
| 767 | |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 768 | al = gfs2_alloc_get(ip); |
| 769 | if (!al) { |
| 770 | error = -ENOMEM; |
| 771 | goto out_gunlock; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 772 | } |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 773 | /* |
| 774 | * 1 blk for unstuffing inode if stuffed. We add this extra |
| 775 | * block to the reservation unconditionally. If the inode |
| 776 | * doesn't need unstuffing, the block will be released to the |
| 777 | * rgrp since it won't be allocated during the transaction |
| 778 | */ |
| 779 | al->al_requested = 1; |
| 780 | /* +1 in the end for block requested above for unstuffing */ |
| 781 | blocks = num_qd * data_blocks + RES_DINODE + num_qd + 1; |
| 782 | |
| 783 | if (nalloc) |
| 784 | al->al_requested += nalloc * (data_blocks + ind_blocks); |
| 785 | error = gfs2_inplace_reserve(ip); |
| 786 | if (error) |
| 787 | goto out_alloc; |
| 788 | |
| 789 | if (nalloc) |
| 790 | blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS; |
| 791 | |
| 792 | error = gfs2_trans_begin(sdp, blocks, 0); |
| 793 | if (error) |
| 794 | goto out_ipres; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 795 | |
| 796 | for (x = 0; x < num_qd; x++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 797 | qd = qda[x]; |
| 798 | offset = qd2offset(qd); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 799 | error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 800 | (struct gfs2_quota_data *) |
Abhijith Das | 2a87ab0 | 2007-05-16 17:02:19 -0500 | [diff] [blame] | 801 | qd); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 802 | if (error) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 803 | goto out_end_trans; |
| 804 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 805 | do_qc(qd, -qd->qd_change_sync); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | error = 0; |
| 809 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 810 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 811 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 812 | out_ipres: |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 813 | gfs2_inplace_release(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 814 | out_alloc: |
Abhijith Das | 20b95bf | 2008-03-06 17:43:52 -0600 | [diff] [blame] | 815 | gfs2_alloc_put(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 816 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 817 | gfs2_glock_dq_uninit(&i_gh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 818 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 819 | while (qx--) |
| 820 | gfs2_glock_dq_uninit(&ghs[qx]); |
| 821 | kfree(ghs); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 822 | gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 823 | return error; |
| 824 | } |
| 825 | |
| 826 | static int do_glock(struct gfs2_quota_data *qd, int force_refresh, |
| 827 | struct gfs2_holder *q_gh) |
| 828 | { |
| 829 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 830 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 831 | struct gfs2_holder i_gh; |
Al Viro | b5bc9e8 | 2006-10-13 23:31:55 -0400 | [diff] [blame] | 832 | struct gfs2_quota_host q; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 833 | char buf[sizeof(struct gfs2_quota)]; |
| 834 | int error; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 835 | struct gfs2_quota_lvb *qlvb; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 836 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 837 | restart: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 838 | error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh); |
| 839 | if (error) |
| 840 | return error; |
| 841 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 842 | qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 843 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 844 | 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] | 845 | loff_t pos; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 846 | gfs2_glock_dq_uninit(q_gh); |
| 847 | error = gfs2_glock_nq_init(qd->qd_gl, |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 848 | LM_ST_EXCLUSIVE, GL_NOCACHE, |
| 849 | q_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 850 | if (error) |
| 851 | return error; |
| 852 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 853 | 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] | 854 | if (error) |
| 855 | goto fail; |
| 856 | |
| 857 | memset(buf, 0, sizeof(struct gfs2_quota)); |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 858 | pos = qd2offset(qd); |
Steven Whitehouse | 51ff87b | 2007-10-15 14:42:35 +0100 | [diff] [blame] | 859 | error = gfs2_internal_read(ip, NULL, buf, &pos, |
| 860 | sizeof(struct gfs2_quota)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 861 | if (error < 0) |
| 862 | goto fail_gunlock; |
| 863 | |
| 864 | gfs2_glock_dq_uninit(&i_gh); |
| 865 | |
| 866 | gfs2_quota_in(&q, buf); |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 867 | qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb; |
| 868 | qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC); |
| 869 | qlvb->__pad = 0; |
| 870 | qlvb->qb_limit = cpu_to_be64(q.qu_limit); |
| 871 | qlvb->qb_warn = cpu_to_be64(q.qu_warn); |
| 872 | qlvb->qb_value = cpu_to_be64(q.qu_value); |
| 873 | qd->qd_qb = *qlvb; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 874 | |
| 875 | if (gfs2_glock_is_blocking(qd->qd_gl)) { |
| 876 | gfs2_glock_dq_uninit(q_gh); |
| 877 | force_refresh = 0; |
| 878 | goto restart; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return 0; |
| 883 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 884 | fail_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 885 | gfs2_glock_dq_uninit(&i_gh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 886 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 887 | gfs2_glock_dq_uninit(q_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 888 | return error; |
| 889 | } |
| 890 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 891 | int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 892 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 893 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 894 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 895 | unsigned int x; |
| 896 | int error = 0; |
| 897 | |
| 898 | gfs2_quota_hold(ip, uid, gid); |
| 899 | |
| 900 | if (capable(CAP_SYS_RESOURCE) || |
| 901 | sdp->sd_args.ar_quota != GFS2_QUOTA_ON) |
| 902 | return 0; |
| 903 | |
| 904 | sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *), |
| 905 | sort_qd, NULL); |
| 906 | |
| 907 | for (x = 0; x < al->al_qd_num; x++) { |
| 908 | error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]); |
| 909 | if (error) |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | if (!error) |
| 914 | set_bit(GIF_QD_LOCKED, &ip->i_flags); |
| 915 | else { |
| 916 | while (x--) |
| 917 | gfs2_glock_dq_uninit(&al->al_qd_ghs[x]); |
| 918 | gfs2_quota_unhold(ip); |
| 919 | } |
| 920 | |
| 921 | return error; |
| 922 | } |
| 923 | |
| 924 | static int need_sync(struct gfs2_quota_data *qd) |
| 925 | { |
| 926 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
| 927 | struct gfs2_tune *gt = &sdp->sd_tune; |
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 num, den; |
| 930 | int do_sync = 1; |
| 931 | |
| 932 | if (!qd->qd_qb.qb_limit) |
| 933 | return 0; |
| 934 | |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 935 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 936 | value = qd->qd_change; |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 937 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 938 | |
| 939 | spin_lock(>->gt_spin); |
| 940 | num = gt->gt_quota_scale_num; |
| 941 | den = gt->gt_quota_scale_den; |
| 942 | spin_unlock(>->gt_spin); |
| 943 | |
| 944 | if (value < 0) |
| 945 | do_sync = 0; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 946 | else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >= |
| 947 | (s64)be64_to_cpu(qd->qd_qb.qb_limit)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 948 | do_sync = 0; |
| 949 | else { |
| 950 | value *= gfs2_jindex_size(sdp) * num; |
David Howells | 4abaca17 | 2008-07-11 14:39:56 +0100 | [diff] [blame] | 951 | value = div_s64(value, den); |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 952 | value += (s64)be64_to_cpu(qd->qd_qb.qb_value); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 953 | if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 954 | do_sync = 0; |
| 955 | } |
| 956 | |
| 957 | return do_sync; |
| 958 | } |
| 959 | |
| 960 | void gfs2_quota_unlock(struct gfs2_inode *ip) |
| 961 | { |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 962 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 963 | struct gfs2_quota_data *qda[4]; |
| 964 | unsigned int count = 0; |
| 965 | unsigned int x; |
| 966 | |
| 967 | if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags)) |
| 968 | goto out; |
| 969 | |
| 970 | for (x = 0; x < al->al_qd_num; x++) { |
| 971 | struct gfs2_quota_data *qd; |
| 972 | int sync; |
| 973 | |
| 974 | qd = al->al_qd[x]; |
| 975 | sync = need_sync(qd); |
| 976 | |
| 977 | gfs2_glock_dq_uninit(&al->al_qd_ghs[x]); |
| 978 | |
| 979 | if (sync && qd_trylock(qd)) |
| 980 | qda[count++] = qd; |
| 981 | } |
| 982 | |
| 983 | if (count) { |
| 984 | do_sync(count, qda); |
| 985 | for (x = 0; x < count; x++) |
| 986 | qd_unlock(qda[x]); |
| 987 | } |
| 988 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 989 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 990 | gfs2_quota_unhold(ip); |
| 991 | } |
| 992 | |
| 993 | #define MAX_LINE 256 |
| 994 | |
| 995 | static int print_message(struct gfs2_quota_data *qd, char *type) |
| 996 | { |
| 997 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 998 | |
Steven Whitehouse | 02630a1 | 2006-07-03 11:20:06 -0400 | [diff] [blame] | 999 | printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n", |
| 1000 | sdp->sd_fsname, type, |
| 1001 | (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group", |
| 1002 | qd->qd_id); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1007 | int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1008 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1009 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1010 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1011 | struct gfs2_quota_data *qd; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1012 | s64 value; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1013 | unsigned int x; |
| 1014 | int error = 0; |
| 1015 | |
| 1016 | if (!test_bit(GIF_QD_LOCKED, &ip->i_flags)) |
| 1017 | return 0; |
| 1018 | |
| 1019 | if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON) |
| 1020 | return 0; |
| 1021 | |
| 1022 | for (x = 0; x < al->al_qd_num; x++) { |
| 1023 | qd = al->al_qd[x]; |
| 1024 | |
| 1025 | if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) || |
| 1026 | (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags)))) |
| 1027 | continue; |
| 1028 | |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 1029 | value = (s64)be64_to_cpu(qd->qd_qb.qb_value); |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 1030 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1031 | value += qd->qd_change; |
Steven Whitehouse | 22077f5 | 2009-01-08 14:28:42 +0000 | [diff] [blame] | 1032 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1033 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1034 | 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] | 1035 | print_message(qd, "exceeded"); |
| 1036 | error = -EDQUOT; |
| 1037 | break; |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 1038 | } else if (be64_to_cpu(qd->qd_qb.qb_warn) && |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1039 | (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value && |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1040 | time_after_eq(jiffies, qd->qd_last_warn + |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 1041 | gfs2_tune_get(sdp, |
| 1042 | gt_quota_warn_period) * HZ)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1043 | error = print_message(qd, "warning"); |
| 1044 | qd->qd_last_warn = jiffies; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | return error; |
| 1049 | } |
| 1050 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1051 | void gfs2_quota_change(struct gfs2_inode *ip, s64 change, |
| 1052 | u32 uid, u32 gid) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1053 | { |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1054 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1055 | struct gfs2_quota_data *qd; |
| 1056 | unsigned int x; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1057 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1058 | if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1059 | return; |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1060 | if (ip->i_diskflags & GFS2_DIF_SYSTEM) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | |
| 1063 | for (x = 0; x < al->al_qd_num; x++) { |
| 1064 | qd = al->al_qd[x]; |
| 1065 | |
| 1066 | if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) || |
| 1067 | (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) { |
| 1068 | do_qc(qd, change); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | int gfs2_quota_sync(struct gfs2_sbd *sdp) |
| 1074 | { |
| 1075 | struct gfs2_quota_data **qda; |
| 1076 | unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync); |
| 1077 | unsigned int num_qd; |
| 1078 | unsigned int x; |
| 1079 | int error = 0; |
| 1080 | |
| 1081 | sdp->sd_quota_sync_gen++; |
| 1082 | |
| 1083 | qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL); |
| 1084 | if (!qda) |
| 1085 | return -ENOMEM; |
| 1086 | |
| 1087 | do { |
| 1088 | num_qd = 0; |
| 1089 | |
| 1090 | for (;;) { |
| 1091 | error = qd_fish(sdp, qda + num_qd); |
| 1092 | if (error || !qda[num_qd]) |
| 1093 | break; |
| 1094 | if (++num_qd == max_qd) |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | if (num_qd) { |
| 1099 | if (!error) |
| 1100 | error = do_sync(num_qd, qda); |
| 1101 | if (!error) |
| 1102 | for (x = 0; x < num_qd; x++) |
| 1103 | qda[x]->qd_sync_gen = |
| 1104 | sdp->sd_quota_sync_gen; |
| 1105 | |
| 1106 | for (x = 0; x < num_qd; x++) |
| 1107 | qd_unlock(qda[x]); |
| 1108 | } |
| 1109 | } while (!error && num_qd == max_qd); |
| 1110 | |
| 1111 | kfree(qda); |
| 1112 | |
| 1113 | return error; |
| 1114 | } |
| 1115 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1116 | int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1117 | { |
| 1118 | struct gfs2_quota_data *qd; |
| 1119 | struct gfs2_holder q_gh; |
| 1120 | int error; |
| 1121 | |
| 1122 | error = qd_get(sdp, user, id, CREATE, &qd); |
| 1123 | if (error) |
| 1124 | return error; |
| 1125 | |
| 1126 | error = do_glock(qd, FORCE, &q_gh); |
| 1127 | if (!error) |
| 1128 | gfs2_glock_dq_uninit(&q_gh); |
| 1129 | |
| 1130 | qd_put(qd); |
| 1131 | |
| 1132 | return error; |
| 1133 | } |
| 1134 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1135 | static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf) |
| 1136 | { |
| 1137 | const struct gfs2_quota_change *str = buf; |
| 1138 | |
| 1139 | qc->qc_change = be64_to_cpu(str->qc_change); |
| 1140 | qc->qc_flags = be32_to_cpu(str->qc_flags); |
| 1141 | qc->qc_id = be32_to_cpu(str->qc_id); |
| 1142 | } |
| 1143 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1144 | int gfs2_quota_init(struct gfs2_sbd *sdp) |
| 1145 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1146 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
Steven Whitehouse | c9e9888 | 2008-11-04 09:47:33 +0000 | [diff] [blame] | 1147 | unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1148 | unsigned int x, slot = 0; |
| 1149 | unsigned int found = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1150 | u64 dblock; |
| 1151 | u32 extlen = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1152 | int error; |
| 1153 | |
Steven Whitehouse | c9e9888 | 2008-11-04 09:47:33 +0000 | [diff] [blame] | 1154 | if (!ip->i_disksize || ip->i_disksize > (64 << 20) || |
| 1155 | ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1156 | gfs2_consist_inode(ip); |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1157 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1158 | } |
| 1159 | sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1160 | 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] | 1161 | |
| 1162 | error = -ENOMEM; |
| 1163 | |
| 1164 | sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks, |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 1165 | sizeof(unsigned char *), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1166 | if (!sdp->sd_quota_bitmap) |
| 1167 | return error; |
| 1168 | |
| 1169 | for (x = 0; x < sdp->sd_quota_chunks; x++) { |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 1170 | sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1171 | if (!sdp->sd_quota_bitmap[x]) |
| 1172 | goto fail; |
| 1173 | } |
| 1174 | |
| 1175 | for (x = 0; x < blocks; x++) { |
| 1176 | struct buffer_head *bh; |
| 1177 | unsigned int y; |
| 1178 | |
| 1179 | if (!extlen) { |
| 1180 | int new = 0; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1181 | error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1182 | if (error) |
| 1183 | goto fail; |
| 1184 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1185 | error = -EIO; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1186 | bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); |
| 1187 | if (!bh) |
| 1188 | goto fail; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1189 | if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) { |
| 1190 | brelse(bh); |
| 1191 | goto fail; |
| 1192 | } |
| 1193 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1194 | 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] | 1195 | y++, slot++) { |
Al Viro | b62f963 | 2006-10-13 23:46:46 -0400 | [diff] [blame] | 1196 | struct gfs2_quota_change_host qc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1197 | struct gfs2_quota_data *qd; |
| 1198 | |
| 1199 | gfs2_quota_change_in(&qc, bh->b_data + |
| 1200 | sizeof(struct gfs2_meta_header) + |
| 1201 | y * sizeof(struct gfs2_quota_change)); |
| 1202 | if (!qc.qc_change) |
| 1203 | continue; |
| 1204 | |
| 1205 | error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER), |
| 1206 | qc.qc_id, &qd); |
| 1207 | if (error) { |
| 1208 | brelse(bh); |
| 1209 | goto fail; |
| 1210 | } |
| 1211 | |
| 1212 | set_bit(QDF_CHANGE, &qd->qd_flags); |
| 1213 | qd->qd_change = qc.qc_change; |
| 1214 | qd->qd_slot = slot; |
| 1215 | qd->qd_slot_count = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1216 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1217 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1218 | gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1); |
| 1219 | list_add(&qd->qd_list, &sdp->sd_quota_list); |
| 1220 | atomic_inc(&sdp->sd_quota_count); |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1221 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1222 | |
| 1223 | found++; |
| 1224 | } |
| 1225 | |
| 1226 | brelse(bh); |
| 1227 | dblock++; |
| 1228 | extlen--; |
| 1229 | } |
| 1230 | |
| 1231 | if (found) |
| 1232 | fs_info(sdp, "found %u quota changes\n", found); |
| 1233 | |
| 1234 | return 0; |
| 1235 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1236 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1237 | gfs2_quota_cleanup(sdp); |
| 1238 | return error; |
| 1239 | } |
| 1240 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1241 | void gfs2_quota_cleanup(struct gfs2_sbd *sdp) |
| 1242 | { |
| 1243 | struct list_head *head = &sdp->sd_quota_list; |
| 1244 | struct gfs2_quota_data *qd; |
| 1245 | unsigned int x; |
| 1246 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1247 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1248 | while (!list_empty(head)) { |
| 1249 | qd = list_entry(head->prev, struct gfs2_quota_data, qd_list); |
| 1250 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1251 | if (atomic_read(&qd->qd_count) > 1 || |
| 1252 | (atomic_read(&qd->qd_count) && |
| 1253 | !test_bit(QDF_CHANGE, &qd->qd_flags))) { |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1254 | list_move(&qd->qd_list, head); |
| 1255 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1256 | schedule(); |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1257 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1258 | continue; |
| 1259 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1260 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1261 | list_del(&qd->qd_list); |
| 1262 | /* Also remove if this qd exists in the reclaim list */ |
| 1263 | if (!list_empty(&qd->qd_reclaim)) { |
| 1264 | list_del_init(&qd->qd_reclaim); |
| 1265 | atomic_dec(&qd_lru_count); |
| 1266 | } |
| 1267 | atomic_dec(&sdp->sd_quota_count); |
| 1268 | spin_unlock(&qd_lru_lock); |
| 1269 | |
| 1270 | if (!atomic_read(&qd->qd_count)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1271 | gfs2_assert_warn(sdp, !qd->qd_change); |
| 1272 | gfs2_assert_warn(sdp, !qd->qd_slot_count); |
| 1273 | } else |
| 1274 | gfs2_assert_warn(sdp, qd->qd_slot_count == 1); |
| 1275 | gfs2_assert_warn(sdp, !qd->qd_bh_count); |
| 1276 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame^] | 1277 | gfs2_glock_put(qd->qd_gl); |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 1278 | kmem_cache_free(gfs2_quotad_cachep, qd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1279 | |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1280 | spin_lock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1281 | } |
Abhijith Das | 0a7ab79 | 2009-01-07 16:03:37 -0600 | [diff] [blame] | 1282 | spin_unlock(&qd_lru_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1283 | |
| 1284 | gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count)); |
| 1285 | |
| 1286 | if (sdp->sd_quota_bitmap) { |
| 1287 | for (x = 0; x < sdp->sd_quota_chunks; x++) |
| 1288 | kfree(sdp->sd_quota_bitmap[x]); |
| 1289 | kfree(sdp->sd_quota_bitmap); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 1293 | static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error) |
| 1294 | { |
| 1295 | if (error == 0 || error == -EROFS) |
| 1296 | return; |
| 1297 | if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) |
| 1298 | fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error); |
| 1299 | } |
| 1300 | |
| 1301 | static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg, |
| 1302 | int (*fxn)(struct gfs2_sbd *sdp), |
| 1303 | unsigned long t, unsigned long *timeo, |
| 1304 | unsigned int *new_timeo) |
| 1305 | { |
| 1306 | if (t >= *timeo) { |
| 1307 | int error = fxn(sdp); |
| 1308 | quotad_error(sdp, msg, error); |
| 1309 | *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ; |
| 1310 | } else { |
| 1311 | *timeo -= t; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Steven Whitehouse | 813e0c4 | 2008-11-18 13:38:48 +0000 | [diff] [blame] | 1315 | static void quotad_check_trunc_list(struct gfs2_sbd *sdp) |
| 1316 | { |
| 1317 | struct gfs2_inode *ip; |
| 1318 | |
| 1319 | while(1) { |
| 1320 | ip = NULL; |
| 1321 | spin_lock(&sdp->sd_trunc_lock); |
| 1322 | if (!list_empty(&sdp->sd_trunc_list)) { |
| 1323 | ip = list_entry(sdp->sd_trunc_list.next, |
| 1324 | struct gfs2_inode, i_trunc_list); |
| 1325 | list_del_init(&ip->i_trunc_list); |
| 1326 | } |
| 1327 | spin_unlock(&sdp->sd_trunc_lock); |
| 1328 | if (ip == NULL) |
| 1329 | return; |
| 1330 | gfs2_glock_finish_truncate(ip); |
| 1331 | } |
| 1332 | } |
| 1333 | |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 1334 | /** |
| 1335 | * gfs2_quotad - Write cached quota changes into the quota file |
| 1336 | * @sdp: Pointer to GFS2 superblock |
| 1337 | * |
| 1338 | */ |
| 1339 | |
| 1340 | int gfs2_quotad(void *data) |
| 1341 | { |
| 1342 | struct gfs2_sbd *sdp = data; |
| 1343 | struct gfs2_tune *tune = &sdp->sd_tune; |
| 1344 | unsigned long statfs_timeo = 0; |
| 1345 | unsigned long quotad_timeo = 0; |
| 1346 | unsigned long t = 0; |
| 1347 | DEFINE_WAIT(wait); |
Steven Whitehouse | 813e0c4 | 2008-11-18 13:38:48 +0000 | [diff] [blame] | 1348 | int empty; |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 1349 | |
| 1350 | while (!kthread_should_stop()) { |
| 1351 | |
| 1352 | /* Update the master statfs file */ |
| 1353 | quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t, |
| 1354 | &statfs_timeo, &tune->gt_statfs_quantum); |
| 1355 | |
| 1356 | /* Update quota file */ |
| 1357 | quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t, |
| 1358 | "ad_timeo, &tune->gt_quota_quantum); |
| 1359 | |
Steven Whitehouse | 813e0c4 | 2008-11-18 13:38:48 +0000 | [diff] [blame] | 1360 | /* Check for & recover partially truncated inodes */ |
| 1361 | quotad_check_trunc_list(sdp); |
| 1362 | |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 1363 | if (freezing(current)) |
| 1364 | refrigerator(); |
| 1365 | t = min(quotad_timeo, statfs_timeo); |
| 1366 | |
| 1367 | prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_UNINTERRUPTIBLE); |
Steven Whitehouse | 813e0c4 | 2008-11-18 13:38:48 +0000 | [diff] [blame] | 1368 | spin_lock(&sdp->sd_trunc_lock); |
| 1369 | empty = list_empty(&sdp->sd_trunc_list); |
| 1370 | spin_unlock(&sdp->sd_trunc_lock); |
| 1371 | if (empty) |
| 1372 | t -= schedule_timeout(t); |
| 1373 | else |
| 1374 | t = 0; |
Steven Whitehouse | 37b2c83 | 2008-11-17 14:25:37 +0000 | [diff] [blame] | 1375 | finish_wait(&sdp->sd_quota_wait, &wait); |
| 1376 | } |
| 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |