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