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