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