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