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