]> Pileus Git - ~andy/linux/blob - fs/gfs2/quota.c
Linux 3.14
[~andy/linux] / 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 for 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/mm.h>
42 #include <linux/spinlock.h>
43 #include <linux/completion.h>
44 #include <linux/buffer_head.h>
45 #include <linux/sort.h>
46 #include <linux/fs.h>
47 #include <linux/bio.h>
48 #include <linux/gfs2_ondisk.h>
49 #include <linux/kthread.h>
50 #include <linux/freezer.h>
51 #include <linux/quota.h>
52 #include <linux/dqblk_xfs.h>
53 #include <linux/lockref.h>
54 #include <linux/list_lru.h>
55 #include <linux/rcupdate.h>
56 #include <linux/rculist_bl.h>
57 #include <linux/bit_spinlock.h>
58 #include <linux/jhash.h>
59 #include <linux/vmalloc.h>
60
61 #include "gfs2.h"
62 #include "incore.h"
63 #include "bmap.h"
64 #include "glock.h"
65 #include "glops.h"
66 #include "log.h"
67 #include "meta_io.h"
68 #include "quota.h"
69 #include "rgrp.h"
70 #include "super.h"
71 #include "trans.h"
72 #include "inode.h"
73 #include "util.h"
74
75 #define GFS2_QD_HASH_SHIFT      12
76 #define GFS2_QD_HASH_SIZE       (1 << GFS2_QD_HASH_SHIFT)
77 #define GFS2_QD_HASH_MASK       (GFS2_QD_HASH_SIZE - 1)
78
79 /* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */
80 /*                     -> sd_bitmap_lock                              */
81 static DEFINE_SPINLOCK(qd_lock);
82 struct list_lru gfs2_qd_lru;
83
84 static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
85
86 static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
87                                  const struct kqid qid)
88 {
89         unsigned int h;
90
91         h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
92         h = jhash(&qid, sizeof(struct kqid), h);
93
94         return h & GFS2_QD_HASH_MASK;
95 }
96
97 static inline void spin_lock_bucket(unsigned int hash)
98 {
99         hlist_bl_lock(&qd_hash_table[hash]);
100 }
101
102 static inline void spin_unlock_bucket(unsigned int hash)
103 {
104         hlist_bl_unlock(&qd_hash_table[hash]);
105 }
106
107 static void gfs2_qd_dealloc(struct rcu_head *rcu)
108 {
109         struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
110         kmem_cache_free(gfs2_quotad_cachep, qd);
111 }
112
113 static void gfs2_qd_dispose(struct list_head *list)
114 {
115         struct gfs2_quota_data *qd;
116         struct gfs2_sbd *sdp;
117
118         while (!list_empty(list)) {
119                 qd = list_entry(list->next, struct gfs2_quota_data, qd_lru);
120                 sdp = qd->qd_gl->gl_sbd;
121
122                 list_del(&qd->qd_lru);
123
124                 /* Free from the filesystem-specific list */
125                 spin_lock(&qd_lock);
126                 list_del(&qd->qd_list);
127                 spin_unlock(&qd_lock);
128
129                 spin_lock_bucket(qd->qd_hash);
130                 hlist_bl_del_rcu(&qd->qd_hlist);
131                 spin_unlock_bucket(qd->qd_hash);
132
133                 gfs2_assert_warn(sdp, !qd->qd_change);
134                 gfs2_assert_warn(sdp, !qd->qd_slot_count);
135                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
136
137                 gfs2_glock_put(qd->qd_gl);
138                 atomic_dec(&sdp->sd_quota_count);
139
140                 /* Delete it from the common reclaim list */
141                 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
142         }
143 }
144
145
146 static enum lru_status gfs2_qd_isolate(struct list_head *item, spinlock_t *lock, void *arg)
147 {
148         struct list_head *dispose = arg;
149         struct gfs2_quota_data *qd = list_entry(item, struct gfs2_quota_data, qd_lru);
150
151         if (!spin_trylock(&qd->qd_lockref.lock))
152                 return LRU_SKIP;
153
154         if (qd->qd_lockref.count == 0) {
155                 lockref_mark_dead(&qd->qd_lockref);
156                 list_move(&qd->qd_lru, dispose);
157         }
158
159         spin_unlock(&qd->qd_lockref.lock);
160         return LRU_REMOVED;
161 }
162
163 static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
164                                          struct shrink_control *sc)
165 {
166         LIST_HEAD(dispose);
167         unsigned long freed;
168
169         if (!(sc->gfp_mask & __GFP_FS))
170                 return SHRINK_STOP;
171
172         freed = list_lru_walk_node(&gfs2_qd_lru, sc->nid, gfs2_qd_isolate,
173                                    &dispose, &sc->nr_to_scan);
174
175         gfs2_qd_dispose(&dispose);
176
177         return freed;
178 }
179
180 static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
181                                           struct shrink_control *sc)
182 {
183         return vfs_pressure_ratio(list_lru_count_node(&gfs2_qd_lru, sc->nid));
184 }
185
186 struct shrinker gfs2_qd_shrinker = {
187         .count_objects = gfs2_qd_shrink_count,
188         .scan_objects = gfs2_qd_shrink_scan,
189         .seeks = DEFAULT_SEEKS,
190         .flags = SHRINKER_NUMA_AWARE,
191 };
192
193
194 static u64 qd2index(struct gfs2_quota_data *qd)
195 {
196         struct kqid qid = qd->qd_id;
197         return (2 * (u64)from_kqid(&init_user_ns, qid)) +
198                 ((qid.type == USRQUOTA) ? 0 : 1);
199 }
200
201 static u64 qd2offset(struct gfs2_quota_data *qd)
202 {
203         u64 offset;
204
205         offset = qd2index(qd);
206         offset *= sizeof(struct gfs2_quota);
207
208         return offset;
209 }
210
211 static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
212 {
213         struct gfs2_quota_data *qd;
214         int error;
215
216         qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
217         if (!qd)
218                 return NULL;
219
220         qd->qd_sbd = sdp;
221         qd->qd_lockref.count = 1;
222         spin_lock_init(&qd->qd_lockref.lock);
223         qd->qd_id = qid;
224         qd->qd_slot = -1;
225         INIT_LIST_HEAD(&qd->qd_lru);
226         qd->qd_hash = hash;
227
228         error = gfs2_glock_get(sdp, qd2index(qd),
229                               &gfs2_quota_glops, CREATE, &qd->qd_gl);
230         if (error)
231                 goto fail;
232
233         return qd;
234
235 fail:
236         kmem_cache_free(gfs2_quotad_cachep, qd);
237         return NULL;
238 }
239
240 static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
241                                                      const struct gfs2_sbd *sdp,
242                                                      struct kqid qid)
243 {
244         struct gfs2_quota_data *qd;
245         struct hlist_bl_node *h;
246
247         hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
248                 if (!qid_eq(qd->qd_id, qid))
249                         continue;
250                 if (qd->qd_sbd != sdp)
251                         continue;
252                 if (lockref_get_not_dead(&qd->qd_lockref)) {
253                         list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
254                         return qd;
255                 }
256         }
257
258         return NULL;
259 }
260
261
262 static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
263                   struct gfs2_quota_data **qdp)
264 {
265         struct gfs2_quota_data *qd, *new_qd;
266         unsigned int hash = gfs2_qd_hash(sdp, qid);
267
268         rcu_read_lock();
269         *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
270         rcu_read_unlock();
271
272         if (qd)
273                 return 0;
274
275         new_qd = qd_alloc(hash, sdp, qid);
276         if (!new_qd)
277                 return -ENOMEM;
278
279         spin_lock(&qd_lock);
280         spin_lock_bucket(hash);
281         *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
282         if (qd == NULL) {
283                 *qdp = new_qd;
284                 list_add(&new_qd->qd_list, &sdp->sd_quota_list);
285                 hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
286                 atomic_inc(&sdp->sd_quota_count);
287         }
288         spin_unlock_bucket(hash);
289         spin_unlock(&qd_lock);
290
291         if (qd) {
292                 gfs2_glock_put(new_qd->qd_gl);
293                 kmem_cache_free(gfs2_quotad_cachep, new_qd);
294         }
295
296         return 0;
297 }
298
299
300 static void qd_hold(struct gfs2_quota_data *qd)
301 {
302         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
303         gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
304         lockref_get(&qd->qd_lockref);
305 }
306
307 static void qd_put(struct gfs2_quota_data *qd)
308 {
309         if (lockref_put_or_lock(&qd->qd_lockref))
310                 return;
311
312         qd->qd_lockref.count = 0;
313         list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
314         spin_unlock(&qd->qd_lockref.lock);
315
316 }
317
318 static int slot_get(struct gfs2_quota_data *qd)
319 {
320         struct gfs2_sbd *sdp = qd->qd_sbd;
321         unsigned int bit;
322         int error = 0;
323
324         spin_lock(&sdp->sd_bitmap_lock);
325         if (qd->qd_slot_count != 0)
326                 goto out;
327
328         error = -ENOSPC;
329         bit = find_first_zero_bit(sdp->sd_quota_bitmap, sdp->sd_quota_slots);
330         if (bit < sdp->sd_quota_slots) {
331                 set_bit(bit, sdp->sd_quota_bitmap);
332                 qd->qd_slot = bit;
333 out:
334                 qd->qd_slot_count++;
335         }
336         spin_unlock(&sdp->sd_bitmap_lock);
337
338         return error;
339 }
340
341 static void slot_hold(struct gfs2_quota_data *qd)
342 {
343         struct gfs2_sbd *sdp = qd->qd_sbd;
344
345         spin_lock(&sdp->sd_bitmap_lock);
346         gfs2_assert(sdp, qd->qd_slot_count);
347         qd->qd_slot_count++;
348         spin_unlock(&sdp->sd_bitmap_lock);
349 }
350
351 static void slot_put(struct gfs2_quota_data *qd)
352 {
353         struct gfs2_sbd *sdp = qd->qd_sbd;
354
355         spin_lock(&sdp->sd_bitmap_lock);
356         gfs2_assert(sdp, qd->qd_slot_count);
357         if (!--qd->qd_slot_count) {
358                 BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap));
359                 qd->qd_slot = -1;
360         }
361         spin_unlock(&sdp->sd_bitmap_lock);
362 }
363
364 static int bh_get(struct gfs2_quota_data *qd)
365 {
366         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
367         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
368         unsigned int block, offset;
369         struct buffer_head *bh;
370         int error;
371         struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
372
373         mutex_lock(&sdp->sd_quota_mutex);
374
375         if (qd->qd_bh_count++) {
376                 mutex_unlock(&sdp->sd_quota_mutex);
377                 return 0;
378         }
379
380         block = qd->qd_slot / sdp->sd_qc_per_block;
381         offset = qd->qd_slot % sdp->sd_qc_per_block;
382
383         bh_map.b_size = 1 << ip->i_inode.i_blkbits;
384         error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
385         if (error)
386                 goto fail;
387         error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
388         if (error)
389                 goto fail;
390         error = -EIO;
391         if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
392                 goto fail_brelse;
393
394         qd->qd_bh = bh;
395         qd->qd_bh_qc = (struct gfs2_quota_change *)
396                 (bh->b_data + sizeof(struct gfs2_meta_header) +
397                  offset * sizeof(struct gfs2_quota_change));
398
399         mutex_unlock(&sdp->sd_quota_mutex);
400
401         return 0;
402
403 fail_brelse:
404         brelse(bh);
405 fail:
406         qd->qd_bh_count--;
407         mutex_unlock(&sdp->sd_quota_mutex);
408         return error;
409 }
410
411 static void bh_put(struct gfs2_quota_data *qd)
412 {
413         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
414
415         mutex_lock(&sdp->sd_quota_mutex);
416         gfs2_assert(sdp, qd->qd_bh_count);
417         if (!--qd->qd_bh_count) {
418                 brelse(qd->qd_bh);
419                 qd->qd_bh = NULL;
420                 qd->qd_bh_qc = NULL;
421         }
422         mutex_unlock(&sdp->sd_quota_mutex);
423 }
424
425 static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
426                          u64 *sync_gen)
427 {
428         if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
429             !test_bit(QDF_CHANGE, &qd->qd_flags) ||
430             (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
431                 return 0;
432
433         if (!lockref_get_not_dead(&qd->qd_lockref))
434                 return 0;
435
436         list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
437         set_bit(QDF_LOCKED, &qd->qd_flags);
438         qd->qd_change_sync = qd->qd_change;
439         slot_hold(qd);
440         return 1;
441 }
442
443 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
444 {
445         struct gfs2_quota_data *qd = NULL;
446         int error;
447         int found = 0;
448
449         *qdp = NULL;
450
451         if (sdp->sd_vfs->s_flags & MS_RDONLY)
452                 return 0;
453
454         spin_lock(&qd_lock);
455
456         list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
457                 found = qd_check_sync(sdp, qd, &sdp->sd_quota_sync_gen);
458                 if (found)
459                         break;
460         }
461
462         if (!found)
463                 qd = NULL;
464
465         spin_unlock(&qd_lock);
466
467         if (qd) {
468                 gfs2_assert_warn(sdp, qd->qd_change_sync);
469                 error = bh_get(qd);
470                 if (error) {
471                         clear_bit(QDF_LOCKED, &qd->qd_flags);
472                         slot_put(qd);
473                         qd_put(qd);
474                         return error;
475                 }
476         }
477
478         *qdp = qd;
479
480         return 0;
481 }
482
483 static void qd_unlock(struct gfs2_quota_data *qd)
484 {
485         gfs2_assert_warn(qd->qd_gl->gl_sbd,
486                          test_bit(QDF_LOCKED, &qd->qd_flags));
487         clear_bit(QDF_LOCKED, &qd->qd_flags);
488         bh_put(qd);
489         slot_put(qd);
490         qd_put(qd);
491 }
492
493 static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
494                     struct gfs2_quota_data **qdp)
495 {
496         int error;
497
498         error = qd_get(sdp, qid, qdp);
499         if (error)
500                 return error;
501
502         error = slot_get(*qdp);
503         if (error)
504                 goto fail;
505
506         error = bh_get(*qdp);
507         if (error)
508                 goto fail_slot;
509
510         return 0;
511
512 fail_slot:
513         slot_put(*qdp);
514 fail:
515         qd_put(*qdp);
516         return error;
517 }
518
519 static void qdsb_put(struct gfs2_quota_data *qd)
520 {
521         bh_put(qd);
522         slot_put(qd);
523         qd_put(qd);
524 }
525
526 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
527 {
528         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
529         struct gfs2_quota_data **qd;
530         int error;
531
532         if (ip->i_res == NULL) {
533                 error = gfs2_rs_alloc(ip);
534                 if (error)
535                         return error;
536         }
537
538         qd = ip->i_res->rs_qa_qd;
539
540         if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
541             gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
542                 return -EIO;
543
544         if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
545                 return 0;
546
547         error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
548         if (error)
549                 goto out;
550         ip->i_res->rs_qa_qd_num++;
551         qd++;
552
553         error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
554         if (error)
555                 goto out;
556         ip->i_res->rs_qa_qd_num++;
557         qd++;
558
559         if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
560             !uid_eq(uid, ip->i_inode.i_uid)) {
561                 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
562                 if (error)
563                         goto out;
564                 ip->i_res->rs_qa_qd_num++;
565                 qd++;
566         }
567
568         if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
569             !gid_eq(gid, ip->i_inode.i_gid)) {
570                 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
571                 if (error)
572                         goto out;
573                 ip->i_res->rs_qa_qd_num++;
574                 qd++;
575         }
576
577 out:
578         if (error)
579                 gfs2_quota_unhold(ip);
580         return error;
581 }
582
583 void gfs2_quota_unhold(struct gfs2_inode *ip)
584 {
585         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
586         unsigned int x;
587
588         if (ip->i_res == NULL)
589                 return;
590         gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
591
592         for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
593                 qdsb_put(ip->i_res->rs_qa_qd[x]);
594                 ip->i_res->rs_qa_qd[x] = NULL;
595         }
596         ip->i_res->rs_qa_qd_num = 0;
597 }
598
599 static int sort_qd(const void *a, const void *b)
600 {
601         const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
602         const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
603
604         if (qid_lt(qd_a->qd_id, qd_b->qd_id))
605                 return -1;
606         if (qid_lt(qd_b->qd_id, qd_a->qd_id))
607                 return 1;
608         return 0;
609 }
610
611 static void do_qc(struct gfs2_quota_data *qd, s64 change)
612 {
613         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
614         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
615         struct gfs2_quota_change *qc = qd->qd_bh_qc;
616         s64 x;
617
618         mutex_lock(&sdp->sd_quota_mutex);
619         gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
620
621         if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
622                 qc->qc_change = 0;
623                 qc->qc_flags = 0;
624                 if (qd->qd_id.type == USRQUOTA)
625                         qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
626                 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
627         }
628
629         x = be64_to_cpu(qc->qc_change) + change;
630         qc->qc_change = cpu_to_be64(x);
631
632         spin_lock(&qd_lock);
633         qd->qd_change = x;
634         spin_unlock(&qd_lock);
635
636         if (!x) {
637                 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
638                 clear_bit(QDF_CHANGE, &qd->qd_flags);
639                 qc->qc_flags = 0;
640                 qc->qc_id = 0;
641                 slot_put(qd);
642                 qd_put(qd);
643         } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
644                 qd_hold(qd);
645                 slot_hold(qd);
646         }
647
648         mutex_unlock(&sdp->sd_quota_mutex);
649 }
650
651 /**
652  * gfs2_adjust_quota - adjust record of current block usage
653  * @ip: The quota inode
654  * @loc: Offset of the entry in the quota file
655  * @change: The amount of usage change to record
656  * @qd: The quota data
657  * @fdq: The updated limits to record
658  *
659  * This function was mostly borrowed from gfs2_block_truncate_page which was
660  * in turn mostly borrowed from ext3
661  *
662  * Returns: 0 or -ve on error
663  */
664
665 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
666                              s64 change, struct gfs2_quota_data *qd,
667                              struct fs_disk_quota *fdq)
668 {
669         struct inode *inode = &ip->i_inode;
670         struct gfs2_sbd *sdp = GFS2_SB(inode);
671         struct address_space *mapping = inode->i_mapping;
672         unsigned long index = loc >> PAGE_CACHE_SHIFT;
673         unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
674         unsigned blocksize, iblock, pos;
675         struct buffer_head *bh;
676         struct page *page;
677         void *kaddr, *ptr;
678         struct gfs2_quota q;
679         int err, nbytes;
680         u64 size;
681
682         if (gfs2_is_stuffed(ip)) {
683                 err = gfs2_unstuff_dinode(ip, NULL);
684                 if (err)
685                         return err;
686         }
687
688         memset(&q, 0, sizeof(struct gfs2_quota));
689         err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
690         if (err < 0)
691                 return err;
692
693         err = -EIO;
694         be64_add_cpu(&q.qu_value, change);
695         qd->qd_qb.qb_value = q.qu_value;
696         if (fdq) {
697                 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
698                         q.qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
699                         qd->qd_qb.qb_warn = q.qu_warn;
700                 }
701                 if (fdq->d_fieldmask & FS_DQ_BHARD) {
702                         q.qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
703                         qd->qd_qb.qb_limit = q.qu_limit;
704                 }
705                 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
706                         q.qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
707                         qd->qd_qb.qb_value = q.qu_value;
708                 }
709         }
710
711         /* Write the quota into the quota file on disk */
712         ptr = &q;
713         nbytes = sizeof(struct gfs2_quota);
714 get_a_page:
715         page = find_or_create_page(mapping, index, GFP_NOFS);
716         if (!page)
717                 return -ENOMEM;
718
719         blocksize = inode->i_sb->s_blocksize;
720         iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
721
722         if (!page_has_buffers(page))
723                 create_empty_buffers(page, blocksize, 0);
724
725         bh = page_buffers(page);
726         pos = blocksize;
727         while (offset >= pos) {
728                 bh = bh->b_this_page;
729                 iblock++;
730                 pos += blocksize;
731         }
732
733         if (!buffer_mapped(bh)) {
734                 gfs2_block_map(inode, iblock, bh, 1);
735                 if (!buffer_mapped(bh))
736                         goto unlock_out;
737                 /* If it's a newly allocated disk block for quota, zero it */
738                 if (buffer_new(bh))
739                         zero_user(page, pos - blocksize, bh->b_size);
740         }
741
742         if (PageUptodate(page))
743                 set_buffer_uptodate(bh);
744
745         if (!buffer_uptodate(bh)) {
746                 ll_rw_block(READ | REQ_META, 1, &bh);
747                 wait_on_buffer(bh);
748                 if (!buffer_uptodate(bh))
749                         goto unlock_out;
750         }
751
752         gfs2_trans_add_data(ip->i_gl, bh);
753
754         kaddr = kmap_atomic(page);
755         if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
756                 nbytes = PAGE_CACHE_SIZE - offset;
757         memcpy(kaddr + offset, ptr, nbytes);
758         flush_dcache_page(page);
759         kunmap_atomic(kaddr);
760         unlock_page(page);
761         page_cache_release(page);
762
763         /* If quota straddles page boundary, we need to update the rest of the
764          * quota at the beginning of the next page */
765         if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
766                 ptr = ptr + nbytes;
767                 nbytes = sizeof(struct gfs2_quota) - nbytes;
768                 offset = 0;
769                 index++;
770                 goto get_a_page;
771         }
772
773         size = loc + sizeof(struct gfs2_quota);
774         if (size > inode->i_size)
775                 i_size_write(inode, size);
776         inode->i_mtime = inode->i_atime = CURRENT_TIME;
777         mark_inode_dirty(inode);
778         return 0;
779
780 unlock_out:
781         unlock_page(page);
782         page_cache_release(page);
783         return err;
784 }
785
786 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
787 {
788         struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
789         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
790         struct gfs2_alloc_parms ap = { .aflags = 0, };
791         unsigned int data_blocks, ind_blocks;
792         struct gfs2_holder *ghs, i_gh;
793         unsigned int qx, x;
794         struct gfs2_quota_data *qd;
795         unsigned reserved;
796         loff_t offset;
797         unsigned int nalloc = 0, blocks;
798         int error;
799
800         error = gfs2_rs_alloc(ip);
801         if (error)
802                 return error;
803
804         gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
805                               &data_blocks, &ind_blocks);
806
807         ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
808         if (!ghs)
809                 return -ENOMEM;
810
811         sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
812         mutex_lock(&ip->i_inode.i_mutex);
813         for (qx = 0; qx < num_qd; qx++) {
814                 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
815                                            GL_NOCACHE, &ghs[qx]);
816                 if (error)
817                         goto out;
818         }
819
820         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
821         if (error)
822                 goto out;
823
824         for (x = 0; x < num_qd; x++) {
825                 offset = qd2offset(qda[x]);
826                 if (gfs2_write_alloc_required(ip, offset,
827                                               sizeof(struct gfs2_quota)))
828                         nalloc++;
829         }
830
831         /* 
832          * 1 blk for unstuffing inode if stuffed. We add this extra
833          * block to the reservation unconditionally. If the inode
834          * doesn't need unstuffing, the block will be released to the 
835          * rgrp since it won't be allocated during the transaction
836          */
837         /* +3 in the end for unstuffing block, inode size update block
838          * and another block in case quota straddles page boundary and 
839          * two blocks need to be updated instead of 1 */
840         blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
841
842         reserved = 1 + (nalloc * (data_blocks + ind_blocks));
843         ap.target = reserved;
844         error = gfs2_inplace_reserve(ip, &ap);
845         if (error)
846                 goto out_alloc;
847
848         if (nalloc)
849                 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
850
851         error = gfs2_trans_begin(sdp, blocks, 0);
852         if (error)
853                 goto out_ipres;
854
855         for (x = 0; x < num_qd; x++) {
856                 qd = qda[x];
857                 offset = qd2offset(qd);
858                 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
859                 if (error)
860                         goto out_end_trans;
861
862                 do_qc(qd, -qd->qd_change_sync);
863                 set_bit(QDF_REFRESH, &qd->qd_flags);
864         }
865
866         error = 0;
867
868 out_end_trans:
869         gfs2_trans_end(sdp);
870 out_ipres:
871         gfs2_inplace_release(ip);
872 out_alloc:
873         gfs2_glock_dq_uninit(&i_gh);
874 out:
875         while (qx--)
876                 gfs2_glock_dq_uninit(&ghs[qx]);
877         mutex_unlock(&ip->i_inode.i_mutex);
878         kfree(ghs);
879         gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
880         return error;
881 }
882
883 static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
884 {
885         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
886         struct gfs2_quota q;
887         struct gfs2_quota_lvb *qlvb;
888         loff_t pos;
889         int error;
890
891         memset(&q, 0, sizeof(struct gfs2_quota));
892         pos = qd2offset(qd);
893         error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
894         if (error < 0)
895                 return error;
896
897         qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
898         qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
899         qlvb->__pad = 0;
900         qlvb->qb_limit = q.qu_limit;
901         qlvb->qb_warn = q.qu_warn;
902         qlvb->qb_value = q.qu_value;
903         qd->qd_qb = *qlvb;
904
905         return 0;
906 }
907
908 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
909                     struct gfs2_holder *q_gh)
910 {
911         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
912         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
913         struct gfs2_holder i_gh;
914         int error;
915
916 restart:
917         error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
918         if (error)
919                 return error;
920
921         qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
922
923         if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
924                 gfs2_glock_dq_uninit(q_gh);
925                 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
926                                            GL_NOCACHE, q_gh);
927                 if (error)
928                         return error;
929
930                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
931                 if (error)
932                         goto fail;
933
934                 error = update_qd(sdp, qd);
935                 if (error)
936                         goto fail_gunlock;
937
938                 gfs2_glock_dq_uninit(&i_gh);
939                 gfs2_glock_dq_uninit(q_gh);
940                 force_refresh = 0;
941                 goto restart;
942         }
943
944         return 0;
945
946 fail_gunlock:
947         gfs2_glock_dq_uninit(&i_gh);
948 fail:
949         gfs2_glock_dq_uninit(q_gh);
950         return error;
951 }
952
953 int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
954 {
955         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
956         struct gfs2_quota_data *qd;
957         unsigned int x;
958         int error = 0;
959
960         error = gfs2_quota_hold(ip, uid, gid);
961         if (error)
962                 return error;
963
964         if (capable(CAP_SYS_RESOURCE) ||
965             sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
966                 return 0;
967
968         sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
969              sizeof(struct gfs2_quota_data *), sort_qd, NULL);
970
971         for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
972                 int force = NO_FORCE;
973                 qd = ip->i_res->rs_qa_qd[x];
974                 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
975                         force = FORCE;
976                 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
977                 if (error)
978                         break;
979         }
980
981         if (!error)
982                 set_bit(GIF_QD_LOCKED, &ip->i_flags);
983         else {
984                 while (x--)
985                         gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
986                 gfs2_quota_unhold(ip);
987         }
988
989         return error;
990 }
991
992 static int need_sync(struct gfs2_quota_data *qd)
993 {
994         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
995         struct gfs2_tune *gt = &sdp->sd_tune;
996         s64 value;
997         unsigned int num, den;
998         int do_sync = 1;
999
1000         if (!qd->qd_qb.qb_limit)
1001                 return 0;
1002
1003         spin_lock(&qd_lock);
1004         value = qd->qd_change;
1005         spin_unlock(&qd_lock);
1006
1007         spin_lock(&gt->gt_spin);
1008         num = gt->gt_quota_scale_num;
1009         den = gt->gt_quota_scale_den;
1010         spin_unlock(&gt->gt_spin);
1011
1012         if (value < 0)
1013                 do_sync = 0;
1014         else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1015                  (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1016                 do_sync = 0;
1017         else {
1018                 value *= gfs2_jindex_size(sdp) * num;
1019                 value = div_s64(value, den);
1020                 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
1021                 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1022                         do_sync = 0;
1023         }
1024
1025         return do_sync;
1026 }
1027
1028 void gfs2_quota_unlock(struct gfs2_inode *ip)
1029 {
1030         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1031         struct gfs2_quota_data *qda[4];
1032         unsigned int count = 0;
1033         unsigned int x;
1034         int found;
1035
1036         if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1037                 goto out;
1038
1039         for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1040                 struct gfs2_quota_data *qd;
1041                 int sync;
1042
1043                 qd = ip->i_res->rs_qa_qd[x];
1044                 sync = need_sync(qd);
1045
1046                 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
1047                 if (!sync)
1048                         continue;
1049
1050                 spin_lock(&qd_lock);
1051                 found = qd_check_sync(sdp, qd, NULL);
1052                 spin_unlock(&qd_lock);
1053
1054                 if (!found)
1055                         continue;
1056
1057                 gfs2_assert_warn(sdp, qd->qd_change_sync);
1058                 if (bh_get(qd)) {
1059                         clear_bit(QDF_LOCKED, &qd->qd_flags);
1060                         slot_put(qd);
1061                         qd_put(qd);
1062                         continue;
1063                 }
1064
1065                 qda[count++] = qd;
1066         }
1067
1068         if (count) {
1069                 do_sync(count, qda);
1070                 for (x = 0; x < count; x++)
1071                         qd_unlock(qda[x]);
1072         }
1073
1074 out:
1075         gfs2_quota_unhold(ip);
1076 }
1077
1078 #define MAX_LINE 256
1079
1080 static int print_message(struct gfs2_quota_data *qd, char *type)
1081 {
1082         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
1083
1084         printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
1085                sdp->sd_fsname, type,
1086                (qd->qd_id.type == USRQUOTA) ? "user" : "group",
1087                from_kqid(&init_user_ns, qd->qd_id));
1088
1089         return 0;
1090 }
1091
1092 int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
1093 {
1094         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1095         struct gfs2_quota_data *qd;
1096         s64 value;
1097         unsigned int x;
1098         int error = 0;
1099
1100         if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1101                 return 0;
1102
1103         if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1104                 return 0;
1105
1106         for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1107                 qd = ip->i_res->rs_qa_qd[x];
1108
1109                 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1110                       qid_eq(qd->qd_id, make_kqid_gid(gid))))
1111                         continue;
1112
1113                 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
1114                 spin_lock(&qd_lock);
1115                 value += qd->qd_change;
1116                 spin_unlock(&qd_lock);
1117
1118                 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
1119                         print_message(qd, "exceeded");
1120                         quota_send_warning(qd->qd_id,
1121                                            sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1122
1123                         error = -EDQUOT;
1124                         break;
1125                 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
1126                            (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
1127                            time_after_eq(jiffies, qd->qd_last_warn +
1128                                          gfs2_tune_get(sdp,
1129                                                 gt_quota_warn_period) * HZ)) {
1130                         quota_send_warning(qd->qd_id,
1131                                            sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
1132                         error = print_message(qd, "warning");
1133                         qd->qd_last_warn = jiffies;
1134                 }
1135         }
1136
1137         return error;
1138 }
1139
1140 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1141                        kuid_t uid, kgid_t gid)
1142 {
1143         struct gfs2_quota_data *qd;
1144         unsigned int x;
1145
1146         if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
1147                 return;
1148         if (ip->i_diskflags & GFS2_DIF_SYSTEM)
1149                 return;
1150
1151         for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1152                 qd = ip->i_res->rs_qa_qd[x];
1153
1154                 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1155                     qid_eq(qd->qd_id, make_kqid_gid(gid))) {
1156                         do_qc(qd, change);
1157                 }
1158         }
1159 }
1160
1161 int gfs2_quota_sync(struct super_block *sb, int type)
1162 {
1163         struct gfs2_sbd *sdp = sb->s_fs_info;
1164         struct gfs2_quota_data **qda;
1165         unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
1166         unsigned int num_qd;
1167         unsigned int x;
1168         int error = 0;
1169
1170         qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1171         if (!qda)
1172                 return -ENOMEM;
1173
1174         mutex_lock(&sdp->sd_quota_sync_mutex);
1175         sdp->sd_quota_sync_gen++;
1176
1177         do {
1178                 num_qd = 0;
1179
1180                 for (;;) {
1181                         error = qd_fish(sdp, qda + num_qd);
1182                         if (error || !qda[num_qd])
1183                                 break;
1184                         if (++num_qd == max_qd)
1185                                 break;
1186                 }
1187
1188                 if (num_qd) {
1189                         if (!error)
1190                                 error = do_sync(num_qd, qda);
1191                         if (!error)
1192                                 for (x = 0; x < num_qd; x++)
1193                                         qda[x]->qd_sync_gen =
1194                                                 sdp->sd_quota_sync_gen;
1195
1196                         for (x = 0; x < num_qd; x++)
1197                                 qd_unlock(qda[x]);
1198                 }
1199         } while (!error && num_qd == max_qd);
1200
1201         mutex_unlock(&sdp->sd_quota_sync_mutex);
1202         kfree(qda);
1203
1204         return error;
1205 }
1206
1207 int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
1208 {
1209         struct gfs2_quota_data *qd;
1210         struct gfs2_holder q_gh;
1211         int error;
1212
1213         error = qd_get(sdp, qid, &qd);
1214         if (error)
1215                 return error;
1216
1217         error = do_glock(qd, FORCE, &q_gh);
1218         if (!error)
1219                 gfs2_glock_dq_uninit(&q_gh);
1220
1221         qd_put(qd);
1222         return error;
1223 }
1224
1225 int gfs2_quota_init(struct gfs2_sbd *sdp)
1226 {
1227         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1228         u64 size = i_size_read(sdp->sd_qc_inode);
1229         unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
1230         unsigned int x, slot = 0;
1231         unsigned int found = 0;
1232         unsigned int hash;
1233         unsigned int bm_size;
1234         u64 dblock;
1235         u32 extlen = 0;
1236         int error;
1237
1238         if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
1239                 return -EIO;
1240
1241         sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1242         bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
1243         bm_size *= sizeof(unsigned long);
1244         error = -ENOMEM;
1245         sdp->sd_quota_bitmap = kmalloc(bm_size, GFP_NOFS|__GFP_NOWARN);
1246         if (sdp->sd_quota_bitmap == NULL)
1247                 sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS, PAGE_KERNEL);
1248         if (!sdp->sd_quota_bitmap)
1249                 return error;
1250
1251         memset(sdp->sd_quota_bitmap, 0, bm_size);
1252
1253         for (x = 0; x < blocks; x++) {
1254                 struct buffer_head *bh;
1255                 const struct gfs2_quota_change *qc;
1256                 unsigned int y;
1257
1258                 if (!extlen) {
1259                         int new = 0;
1260                         error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1261                         if (error)
1262                                 goto fail;
1263                 }
1264                 error = -EIO;
1265                 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1266                 if (!bh)
1267                         goto fail;
1268                 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1269                         brelse(bh);
1270                         goto fail;
1271                 }
1272
1273                 qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
1274                 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1275                      y++, slot++) {
1276                         struct gfs2_quota_data *qd;
1277                         s64 qc_change = be64_to_cpu(qc->qc_change);
1278                         u32 qc_flags = be32_to_cpu(qc->qc_flags);
1279                         enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1280                                                 USRQUOTA : GRPQUOTA;
1281                         struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1282                                                       be32_to_cpu(qc->qc_id));
1283                         qc++;
1284                         if (!qc_change)
1285                                 continue;
1286
1287                         hash = gfs2_qd_hash(sdp, qc_id);
1288                         qd = qd_alloc(hash, sdp, qc_id);
1289                         if (qd == NULL) {
1290                                 brelse(bh);
1291                                 goto fail;
1292                         }
1293
1294                         set_bit(QDF_CHANGE, &qd->qd_flags);
1295                         qd->qd_change = qc_change;
1296                         qd->qd_slot = slot;
1297                         qd->qd_slot_count = 1;
1298
1299                         spin_lock(&qd_lock);
1300                         BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
1301                         list_add(&qd->qd_list, &sdp->sd_quota_list);
1302                         atomic_inc(&sdp->sd_quota_count);
1303                         spin_unlock(&qd_lock);
1304
1305                         spin_lock_bucket(hash);
1306                         hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
1307                         spin_unlock_bucket(hash);
1308
1309                         found++;
1310                 }
1311
1312                 brelse(bh);
1313                 dblock++;
1314                 extlen--;
1315         }
1316
1317         if (found)
1318                 fs_info(sdp, "found %u quota changes\n", found);
1319
1320         return 0;
1321
1322 fail:
1323         gfs2_quota_cleanup(sdp);
1324         return error;
1325 }
1326
1327 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1328 {
1329         struct list_head *head = &sdp->sd_quota_list;
1330         struct gfs2_quota_data *qd;
1331
1332         spin_lock(&qd_lock);
1333         while (!list_empty(head)) {
1334                 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1335
1336                 list_del(&qd->qd_list);
1337
1338                 /* Also remove if this qd exists in the reclaim list */
1339                 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
1340                 atomic_dec(&sdp->sd_quota_count);
1341                 spin_unlock(&qd_lock);
1342
1343                 spin_lock_bucket(qd->qd_hash);
1344                 hlist_bl_del_rcu(&qd->qd_hlist);
1345                 spin_unlock_bucket(qd->qd_hash);
1346
1347                 gfs2_assert_warn(sdp, !qd->qd_change);
1348                 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1349                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1350
1351                 gfs2_glock_put(qd->qd_gl);
1352                 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
1353
1354                 spin_lock(&qd_lock);
1355         }
1356         spin_unlock(&qd_lock);
1357
1358         gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1359
1360         if (sdp->sd_quota_bitmap) {
1361                 if (is_vmalloc_addr(sdp->sd_quota_bitmap))
1362                         vfree(sdp->sd_quota_bitmap);
1363                 else
1364                         kfree(sdp->sd_quota_bitmap);
1365                 sdp->sd_quota_bitmap = NULL;
1366         }
1367 }
1368
1369 static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1370 {
1371         if (error == 0 || error == -EROFS)
1372                 return;
1373         if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1374                 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1375 }
1376
1377 static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
1378                                int (*fxn)(struct super_block *sb, int type),
1379                                unsigned long t, unsigned long *timeo,
1380                                unsigned int *new_timeo)
1381 {
1382         if (t >= *timeo) {
1383                 int error = fxn(sdp->sd_vfs, 0);
1384                 quotad_error(sdp, msg, error);
1385                 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1386         } else {
1387                 *timeo -= t;
1388         }
1389 }
1390
1391 static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1392 {
1393         struct gfs2_inode *ip;
1394
1395         while(1) {
1396                 ip = NULL;
1397                 spin_lock(&sdp->sd_trunc_lock);
1398                 if (!list_empty(&sdp->sd_trunc_list)) {
1399                         ip = list_entry(sdp->sd_trunc_list.next,
1400                                         struct gfs2_inode, i_trunc_list);
1401                         list_del_init(&ip->i_trunc_list);
1402                 }
1403                 spin_unlock(&sdp->sd_trunc_lock);
1404                 if (ip == NULL)
1405                         return;
1406                 gfs2_glock_finish_truncate(ip);
1407         }
1408 }
1409
1410 void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1411         if (!sdp->sd_statfs_force_sync) {
1412                 sdp->sd_statfs_force_sync = 1;
1413                 wake_up(&sdp->sd_quota_wait);
1414         }
1415 }
1416
1417
1418 /**
1419  * gfs2_quotad - Write cached quota changes into the quota file
1420  * @sdp: Pointer to GFS2 superblock
1421  *
1422  */
1423
1424 int gfs2_quotad(void *data)
1425 {
1426         struct gfs2_sbd *sdp = data;
1427         struct gfs2_tune *tune = &sdp->sd_tune;
1428         unsigned long statfs_timeo = 0;
1429         unsigned long quotad_timeo = 0;
1430         unsigned long t = 0;
1431         DEFINE_WAIT(wait);
1432         int empty;
1433
1434         while (!kthread_should_stop()) {
1435
1436                 /* Update the master statfs file */
1437                 if (sdp->sd_statfs_force_sync) {
1438                         int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1439                         quotad_error(sdp, "statfs", error);
1440                         statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1441                 }
1442                 else
1443                         quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1444                                            &statfs_timeo,
1445                                            &tune->gt_statfs_quantum);
1446
1447                 /* Update quota file */
1448                 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1449                                    &quotad_timeo, &tune->gt_quota_quantum);
1450
1451                 /* Check for & recover partially truncated inodes */
1452                 quotad_check_trunc_list(sdp);
1453
1454                 try_to_freeze();
1455
1456                 t = min(quotad_timeo, statfs_timeo);
1457
1458                 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
1459                 spin_lock(&sdp->sd_trunc_lock);
1460                 empty = list_empty(&sdp->sd_trunc_list);
1461                 spin_unlock(&sdp->sd_trunc_lock);
1462                 if (empty && !sdp->sd_statfs_force_sync)
1463                         t -= schedule_timeout(t);
1464                 else
1465                         t = 0;
1466                 finish_wait(&sdp->sd_quota_wait, &wait);
1467         }
1468
1469         return 0;
1470 }
1471
1472 static int gfs2_quota_get_xstate(struct super_block *sb,
1473                                  struct fs_quota_stat *fqs)
1474 {
1475         struct gfs2_sbd *sdp = sb->s_fs_info;
1476
1477         memset(fqs, 0, sizeof(struct fs_quota_stat));
1478         fqs->qs_version = FS_QSTAT_VERSION;
1479
1480         switch (sdp->sd_args.ar_quota) {
1481         case GFS2_QUOTA_ON:
1482                 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
1483                 /*FALLTHRU*/
1484         case GFS2_QUOTA_ACCOUNT:
1485                 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
1486                 break;
1487         case GFS2_QUOTA_OFF:
1488                 break;
1489         }
1490
1491         if (sdp->sd_quota_inode) {
1492                 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1493                 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1494         }
1495         fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1496         fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
1497         fqs->qs_incoredqs = list_lru_count(&gfs2_qd_lru);
1498         return 0;
1499 }
1500
1501 static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
1502                           struct fs_disk_quota *fdq)
1503 {
1504         struct gfs2_sbd *sdp = sb->s_fs_info;
1505         struct gfs2_quota_lvb *qlvb;
1506         struct gfs2_quota_data *qd;
1507         struct gfs2_holder q_gh;
1508         int error;
1509
1510         memset(fdq, 0, sizeof(struct fs_disk_quota));
1511
1512         if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1513                 return -ESRCH; /* Crazy XFS error code */
1514
1515         if ((qid.type != USRQUOTA) &&
1516             (qid.type != GRPQUOTA))
1517                 return -EINVAL;
1518
1519         error = qd_get(sdp, qid, &qd);
1520         if (error)
1521                 return error;
1522         error = do_glock(qd, FORCE, &q_gh);
1523         if (error)
1524                 goto out;
1525
1526         qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1527         fdq->d_version = FS_DQUOT_VERSION;
1528         fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
1529         fdq->d_id = from_kqid_munged(current_user_ns(), qid);
1530         fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1531         fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1532         fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
1533
1534         gfs2_glock_dq_uninit(&q_gh);
1535 out:
1536         qd_put(qd);
1537         return error;
1538 }
1539
1540 /* GFS2 only supports a subset of the XFS fields */
1541 #define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
1542
1543 static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
1544                           struct fs_disk_quota *fdq)
1545 {
1546         struct gfs2_sbd *sdp = sb->s_fs_info;
1547         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1548         struct gfs2_quota_data *qd;
1549         struct gfs2_holder q_gh, i_gh;
1550         unsigned int data_blocks, ind_blocks;
1551         unsigned int blocks = 0;
1552         int alloc_required;
1553         loff_t offset;
1554         int error;
1555
1556         if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1557                 return -ESRCH; /* Crazy XFS error code */
1558
1559         if ((qid.type != USRQUOTA) &&
1560             (qid.type != GRPQUOTA))
1561                 return -EINVAL;
1562
1563         if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1564                 return -EINVAL;
1565
1566         error = qd_get(sdp, qid, &qd);
1567         if (error)
1568                 return error;
1569
1570         error = gfs2_rs_alloc(ip);
1571         if (error)
1572                 goto out_put;
1573
1574         mutex_lock(&ip->i_inode.i_mutex);
1575         error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1576         if (error)
1577                 goto out_unlockput;
1578         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1579         if (error)
1580                 goto out_q;
1581
1582         /* Check for existing entry, if none then alloc new blocks */
1583         error = update_qd(sdp, qd);
1584         if (error)
1585                 goto out_i;
1586
1587         /* If nothing has changed, this is a no-op */
1588         if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
1589             ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
1590                 fdq->d_fieldmask ^= FS_DQ_BSOFT;
1591
1592         if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
1593             ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
1594                 fdq->d_fieldmask ^= FS_DQ_BHARD;
1595
1596         if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1597             ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1598                 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1599
1600         if (fdq->d_fieldmask == 0)
1601                 goto out_i;
1602
1603         offset = qd2offset(qd);
1604         alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
1605         if (gfs2_is_stuffed(ip))
1606                 alloc_required = 1;
1607         if (alloc_required) {
1608                 struct gfs2_alloc_parms ap = { .aflags = 0, };
1609                 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1610                                        &data_blocks, &ind_blocks);
1611                 blocks = 1 + data_blocks + ind_blocks;
1612                 ap.target = blocks;
1613                 error = gfs2_inplace_reserve(ip, &ap);
1614                 if (error)
1615                         goto out_i;
1616                 blocks += gfs2_rg_blocks(ip, blocks);
1617         }
1618
1619         /* Some quotas span block boundaries and can update two blocks,
1620            adding an extra block to the transaction to handle such quotas */
1621         error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
1622         if (error)
1623                 goto out_release;
1624
1625         /* Apply changes */
1626         error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1627
1628         gfs2_trans_end(sdp);
1629 out_release:
1630         if (alloc_required)
1631                 gfs2_inplace_release(ip);
1632 out_i:
1633         gfs2_glock_dq_uninit(&i_gh);
1634 out_q:
1635         gfs2_glock_dq_uninit(&q_gh);
1636 out_unlockput:
1637         mutex_unlock(&ip->i_inode.i_mutex);
1638 out_put:
1639         qd_put(qd);
1640         return error;
1641 }
1642
1643 const struct quotactl_ops gfs2_quotactl_ops = {
1644         .quota_sync     = gfs2_quota_sync,
1645         .get_xstate     = gfs2_quota_get_xstate,
1646         .get_dqblk      = gfs2_get_dqblk,
1647         .set_dqblk      = gfs2_set_dqblk,
1648 };
1649
1650 void __init gfs2_quota_hash_init(void)
1651 {
1652         unsigned i;
1653
1654         for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
1655                 INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
1656 }