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