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