GFS2: change gfs2_quota_scan into a shrinker
[linux-2.6.git] / fs / gfs2 / quota.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
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
7  * of the GNU General Public License version 2.
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>
44 #include <linux/sort.h>
45 #include <linux/fs.h>
46 #include <linux/bio.h>
47 #include <linux/gfs2_ondisk.h>
48 #include <linux/lm_interface.h>
49 #include <linux/kthread.h>
50 #include <linux/freezer.h>
51
52 #include "gfs2.h"
53 #include "incore.h"
54 #include "bmap.h"
55 #include "glock.h"
56 #include "glops.h"
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"
63 #include "inode.h"
64 #include "ops_address.h"
65 #include "util.h"
66
67 #define QUOTA_USER 1
68 #define QUOTA_GROUP 0
69
70 struct gfs2_quota_host {
71         u64 qu_limit;
72         u64 qu_warn;
73         s64 qu_value;
74         u32 qu_ll_next;
75 };
76
77 struct gfs2_quota_change_host {
78         u64 qc_change;
79         u32 qc_flags; /* GFS2_QCF_... */
80         u32 qc_id;
81 };
82
83 static LIST_HEAD(qd_lru_list);
84 static atomic_t qd_lru_count = ATOMIC_INIT(0);
85 static spinlock_t qd_lru_lock = SPIN_LOCK_UNLOCKED;
86
87 int 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
126 out:
127         return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
128 }
129
130 static u64 qd2offset(struct gfs2_quota_data *qd)
131 {
132         u64 offset;
133
134         offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
135         offset *= sizeof(struct gfs2_quota);
136
137         return offset;
138 }
139
140 static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
141                     struct gfs2_quota_data **qdp)
142 {
143         struct gfs2_quota_data *qd;
144         int error;
145
146         qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
147         if (!qd)
148                 return -ENOMEM;
149
150         atomic_set(&qd->qd_count, 1);
151         qd->qd_id = id;
152         if (user)
153                 set_bit(QDF_USER, &qd->qd_flags);
154         qd->qd_slot = -1;
155         INIT_LIST_HEAD(&qd->qd_reclaim);
156
157         error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
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
171 fail:
172         kmem_cache_free(gfs2_quotad_cachep, qd);
173         return error;
174 }
175
176 static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
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;
186                 spin_lock(&qd_lru_lock);
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) {
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);
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
212                 spin_unlock(&qd_lru_lock);
213
214                 if (qd || !create) {
215                         if (new_qd) {
216                                 gfs2_lvb_unhold(new_qd->qd_gl);
217                                 kmem_cache_free(gfs2_quotad_cachep, new_qd);
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
229 static void qd_hold(struct gfs2_quota_data *qd)
230 {
231         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
232         gfs2_assert(sdp, atomic_read(&qd->qd_count));
233         atomic_inc(&qd->qd_count);
234 }
235
236 static void qd_put(struct gfs2_quota_data *qd)
237 {
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         }
244 }
245
246 static 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
268 found:
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
283 fail:
284         qd->qd_slot_count--;
285         spin_unlock(&sdp->sd_quota_spin);
286         return -ENOSPC;
287 }
288
289 static 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
299 static 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
312 static int bh_get(struct gfs2_quota_data *qd)
313 {
314         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
315         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
316         unsigned int block, offset;
317         struct buffer_head *bh;
318         int error;
319         struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
320
321         mutex_lock(&sdp->sd_quota_mutex);
322
323         if (qd->qd_bh_count++) {
324                 mutex_unlock(&sdp->sd_quota_mutex);
325                 return 0;
326         }
327
328         block = qd->qd_slot / sdp->sd_qc_per_block;
329         offset = qd->qd_slot % sdp->sd_qc_per_block;
330
331         bh_map.b_size = 1 << ip->i_inode.i_blkbits;
332         error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
333         if (error)
334                 goto fail;
335         error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
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
347         mutex_unlock(&sdp->sd_quota_mutex);
348
349         return 0;
350
351 fail_brelse:
352         brelse(bh);
353 fail:
354         qd->qd_bh_count--;
355         mutex_unlock(&sdp->sd_quota_mutex);
356         return error;
357 }
358
359 static void bh_put(struct gfs2_quota_data *qd)
360 {
361         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
362
363         mutex_lock(&sdp->sd_quota_mutex);
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         }
370         mutex_unlock(&sdp->sd_quota_mutex);
371 }
372
373 static 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
384         spin_lock(&qd_lru_lock);
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);
396                 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
397                 atomic_inc(&qd->qd_count);
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);
410         spin_unlock(&qd_lru_lock);
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
428 static 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
435         spin_lock(&qd_lru_lock);
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);
441                 spin_unlock(&qd_lru_lock);
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);
448         gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
449         atomic_inc(&qd->qd_count);
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);
455         spin_unlock(&qd_lru_lock);
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
468 static void qd_unlock(struct gfs2_quota_data *qd)
469 {
470         gfs2_assert_warn(qd->qd_gl->gl_sbd,
471                          test_bit(QDF_LOCKED, &qd->qd_flags));
472         clear_bit(QDF_LOCKED, &qd->qd_flags);
473         bh_put(qd);
474         slot_put(qd);
475         qd_put(qd);
476 }
477
478 static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
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
497 fail_slot:
498         slot_put(*qdp);
499 fail:
500         qd_put(*qdp);
501         return error;
502 }
503
504 static void qdsb_put(struct gfs2_quota_data *qd)
505 {
506         bh_put(qd);
507         slot_put(qd);
508         qd_put(qd);
509 }
510
511 int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
512 {
513         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
514         struct gfs2_alloc *al = ip->i_alloc;
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
525         error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
526         if (error)
527                 goto out;
528         al->al_qd_num++;
529         qd++;
530
531         error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
532         if (error)
533                 goto out;
534         al->al_qd_num++;
535         qd++;
536
537         if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
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
545         if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
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
553 out:
554         if (error)
555                 gfs2_quota_unhold(ip);
556         return error;
557 }
558
559 void gfs2_quota_unhold(struct gfs2_inode *ip)
560 {
561         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
562         struct gfs2_alloc *al = ip->i_alloc;
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
574 static int sort_qd(const void *a, const void *b)
575 {
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;
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))
582                         return -1;
583                 else
584                         return 1;
585         }
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;
590
591         return 0;
592 }
593
594 static void do_qc(struct gfs2_quota_data *qd, s64 change)
595 {
596         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
597         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
598         struct gfs2_quota_change *qc = qd->qd_bh_qc;
599         s64 x;
600
601         mutex_lock(&sdp->sd_quota_mutex);
602         gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
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
612         x = be64_to_cpu(qc->qc_change) + change;
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         }
630
631         mutex_unlock(&sdp->sd_quota_mutex);
632 }
633
634 static 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);
641         qu->qu_ll_next = be32_to_cpu(str->qu_ll_next);
642 }
643
644 static 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);
651         str->qu_ll_next = cpu_to_be32(qu->qu_ll_next);
652         memset(&str->qu_reserved, 0, sizeof(str->qu_reserved));
653 }
654
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  */
661 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
662                              s64 change, struct gfs2_quota_data *qd)
663 {
664         struct inode *inode = &ip->i_inode;
665         struct address_space *mapping = inode->i_mapping;
666         unsigned long index = loc >> PAGE_CACHE_SHIFT;
667         unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
668         unsigned blocksize, iblock, pos;
669         struct buffer_head *bh;
670         struct page *page;
671         void *kaddr;
672         char *ptr;
673         struct gfs2_quota_host qp;
674         s64 value;
675         int err = -EIO;
676
677         if (gfs2_is_stuffed(ip))
678                 gfs2_unstuff_dinode(ip, NULL);
679         
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)) {
699                 gfs2_block_map(inode, iblock, bh, 1);
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)) {
708                 ll_rw_block(READ_META, 1, &bh);
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);
717         ptr = kaddr + offset;
718         gfs2_quota_in(&qp, ptr);
719         qp.qu_value += change;
720         value = qp.qu_value;
721         gfs2_quota_out(&qp, ptr);
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);
726         qd->qd_qb.qb_value = cpu_to_be64(value);
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);
729 unlock:
730         unlock_page(page);
731         page_cache_release(page);
732         return err;
733 }
734
735 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
736 {
737         struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
738         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
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;
743         loff_t offset;
744         unsigned int nalloc = 0, blocks;
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
751         ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
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
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;
795
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;
801
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;
808
809         for (x = 0; x < num_qd; x++) {
810                 qd = qda[x];
811                 offset = qd2offset(qd);
812                 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
813                                           (struct gfs2_quota_data *)
814                                           qd);
815                 if (error)
816                         goto out_end_trans;
817
818                 do_qc(qd, -qd->qd_change_sync);
819         }
820
821         error = 0;
822
823 out_end_trans:
824         gfs2_trans_end(sdp);
825 out_ipres:
826         gfs2_inplace_release(ip);
827 out_alloc:
828         gfs2_alloc_put(ip);
829 out_gunlock:
830         gfs2_glock_dq_uninit(&i_gh);
831 out:
832         while (qx--)
833                 gfs2_glock_dq_uninit(&ghs[qx]);
834         kfree(ghs);
835         gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
836         return error;
837 }
838
839 static 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;
843         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
844         struct gfs2_holder i_gh;
845         struct gfs2_quota_host q;
846         char buf[sizeof(struct gfs2_quota)];
847         int error;
848         struct gfs2_quota_lvb *qlvb;
849
850 restart:
851         error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
852         if (error)
853                 return error;
854
855         qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
856
857         if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
858                 loff_t pos;
859                 gfs2_glock_dq_uninit(q_gh);
860                 error = gfs2_glock_nq_init(qd->qd_gl,
861                                            LM_ST_EXCLUSIVE, GL_NOCACHE,
862                                            q_gh);
863                 if (error)
864                         return error;
865
866                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
867                 if (error)
868                         goto fail;
869
870                 memset(buf, 0, sizeof(struct gfs2_quota));
871                 pos = qd2offset(qd);
872                 error = gfs2_internal_read(ip, NULL, buf, &pos,
873                                            sizeof(struct gfs2_quota));
874                 if (error < 0)
875                         goto fail_gunlock;
876
877                 gfs2_glock_dq_uninit(&i_gh);
878
879                 gfs2_quota_in(&q, buf);
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;
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
897 fail_gunlock:
898         gfs2_glock_dq_uninit(&i_gh);
899 fail:
900         gfs2_glock_dq_uninit(q_gh);
901         return error;
902 }
903
904 int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
905 {
906         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
907         struct gfs2_alloc *al = ip->i_alloc;
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
937 static 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;
941         s64 value;
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;
959         else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
960                  (s64)be64_to_cpu(qd->qd_qb.qb_limit))
961                 do_sync = 0;
962         else {
963                 value *= gfs2_jindex_size(sdp) * num;
964                 value = div_s64(value, den);
965                 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
966                 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
967                         do_sync = 0;
968         }
969
970         return do_sync;
971 }
972
973 void gfs2_quota_unlock(struct gfs2_inode *ip)
974 {
975         struct gfs2_alloc *al = ip->i_alloc;
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
1002 out:
1003         gfs2_quota_unhold(ip);
1004 }
1005
1006 #define MAX_LINE 256
1007
1008 static int print_message(struct gfs2_quota_data *qd, char *type)
1009 {
1010         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
1011
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);
1016
1017         return 0;
1018 }
1019
1020 int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
1021 {
1022         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1023         struct gfs2_alloc *al = ip->i_alloc;
1024         struct gfs2_quota_data *qd;
1025         s64 value;
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
1042                 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
1043                 spin_lock(&sdp->sd_quota_spin);
1044                 value += qd->qd_change;
1045                 spin_unlock(&sdp->sd_quota_spin);
1046
1047                 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
1048                         print_message(qd, "exceeded");
1049                         error = -EDQUOT;
1050                         break;
1051                 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
1052                            (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
1053                            time_after_eq(jiffies, qd->qd_last_warn +
1054                                          gfs2_tune_get(sdp,
1055                                                 gt_quota_warn_period) * HZ)) {
1056                         error = print_message(qd, "warning");
1057                         qd->qd_last_warn = jiffies;
1058                 }
1059         }
1060
1061         return error;
1062 }
1063
1064 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1065                        u32 uid, u32 gid)
1066 {
1067         struct gfs2_alloc *al = ip->i_alloc;
1068         struct gfs2_quota_data *qd;
1069         unsigned int x;
1070
1071         if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
1072                 return;
1073         if (ip->i_diskflags & GFS2_DIF_SYSTEM)
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);
1082                 }
1083         }
1084 }
1085
1086 int 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
1129 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
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
1148 static 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
1157 int gfs2_quota_init(struct gfs2_sbd *sdp)
1158 {
1159         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1160         unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
1161         unsigned int x, slot = 0;
1162         unsigned int found = 0;
1163         u64 dblock;
1164         u32 extlen = 0;
1165         int error;
1166
1167         if (!ip->i_disksize || ip->i_disksize > (64 << 20) ||
1168             ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
1169                 gfs2_consist_inode(ip);
1170                 return -EIO;
1171         }
1172         sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1173         sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1174
1175         error = -ENOMEM;
1176
1177         sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1178                                        sizeof(unsigned char *), GFP_NOFS);
1179         if (!sdp->sd_quota_bitmap)
1180                 return error;
1181
1182         for (x = 0; x < sdp->sd_quota_chunks; x++) {
1183                 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
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;
1194                         error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1195                         if (error)
1196                                 goto fail;
1197                 }
1198                 error = -EIO;
1199                 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1200                 if (!bh)
1201                         goto fail;
1202                 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1203                         brelse(bh);
1204                         goto fail;
1205                 }
1206
1207                 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1208                      y++, slot++) {
1209                         struct gfs2_quota_change_host qc;
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;
1229
1230                         spin_lock(&qd_lru_lock);
1231                         spin_lock(&sdp->sd_quota_spin);
1232                         gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1233                         spin_unlock(&sdp->sd_quota_spin);
1234                         list_add(&qd->qd_list, &sdp->sd_quota_list);
1235                         atomic_inc(&sdp->sd_quota_count);
1236                         spin_unlock(&qd_lru_lock);
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
1251 fail:
1252         gfs2_quota_cleanup(sdp);
1253         return error;
1254 }
1255
1256 void 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
1262         spin_lock(&qd_lru_lock);
1263         while (!list_empty(head)) {
1264                 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1265
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))) {
1270                         spin_unlock(&sdp->sd_quota_spin);
1271                         list_move(&qd->qd_list, head);
1272                         spin_unlock(&qd_lru_lock);
1273                         schedule();
1274                         spin_lock(&qd_lru_lock);
1275                         continue;
1276                 }
1277                 spin_unlock(&sdp->sd_quota_spin);
1278
1279                 list_del(&qd->qd_list);
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                 }
1285                 atomic_dec(&sdp->sd_quota_count);
1286                 spin_unlock(&qd_lru_lock);
1287
1288                 if (!atomic_read(&qd->qd_count)) {
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);
1296                 kmem_cache_free(gfs2_quotad_cachep, qd);
1297
1298                 spin_lock(&qd_lru_lock);
1299         }
1300         spin_unlock(&qd_lru_lock);
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
1311 static 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
1319 static 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
1333 static 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
1352 /**
1353  * gfs2_quotad - Write cached quota changes into the quota file
1354  * @sdp: Pointer to GFS2 superblock
1355  *
1356  */
1357
1358 int 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);
1366         int empty;
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
1378                 /* Check for & recover partially truncated inodes */
1379                 quotad_check_trunc_list(sdp);
1380
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);
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;
1393                 finish_wait(&sdp->sd_quota_wait, &wait);
1394         }
1395
1396         return 0;
1397 }
1398