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