2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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.
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.
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.
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.
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>
46 #include <linux/gfs2_ondisk.h>
47 #include <linux/lm_interface.h>
62 #include "ops_address.h"
68 static u64 qd2offset(struct gfs2_quota_data *qd)
72 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
73 offset *= sizeof(struct gfs2_quota);
78 static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
79 struct gfs2_quota_data **qdp)
81 struct gfs2_quota_data *qd;
84 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
91 set_bit(QDF_USER, &qd->qd_flags);
94 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
95 &gfs2_quota_glops, CREATE, &qd->qd_gl);
99 error = gfs2_lvb_hold(qd->qd_gl);
100 gfs2_glock_put(qd->qd_gl);
113 static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
114 struct gfs2_quota_data **qdp)
116 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
123 spin_lock(&sdp->sd_quota_spin);
124 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
125 if (qd->qd_id == id &&
126 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
138 list_add(&qd->qd_list, &sdp->sd_quota_list);
139 atomic_inc(&sdp->sd_quota_count);
143 spin_unlock(&sdp->sd_quota_spin);
147 gfs2_lvb_unhold(new_qd->qd_gl);
154 error = qd_alloc(sdp, user, id, &new_qd);
160 static void qd_hold(struct gfs2_quota_data *qd)
162 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
164 spin_lock(&sdp->sd_quota_spin);
165 gfs2_assert(sdp, qd->qd_count);
167 spin_unlock(&sdp->sd_quota_spin);
170 static void qd_put(struct gfs2_quota_data *qd)
172 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
173 spin_lock(&sdp->sd_quota_spin);
174 gfs2_assert(sdp, qd->qd_count);
176 qd->qd_last_touched = jiffies;
177 spin_unlock(&sdp->sd_quota_spin);
180 static int slot_get(struct gfs2_quota_data *qd)
182 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
183 unsigned int c, o = 0, b;
184 unsigned char byte = 0;
186 spin_lock(&sdp->sd_quota_spin);
188 if (qd->qd_slot_count++) {
189 spin_unlock(&sdp->sd_quota_spin);
193 for (c = 0; c < sdp->sd_quota_chunks; c++)
194 for (o = 0; o < PAGE_SIZE; o++) {
195 byte = sdp->sd_quota_bitmap[c][o];
203 for (b = 0; b < 8; b++)
204 if (!(byte & (1 << b)))
206 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
208 if (qd->qd_slot >= sdp->sd_quota_slots)
211 sdp->sd_quota_bitmap[c][o] |= 1 << b;
213 spin_unlock(&sdp->sd_quota_spin);
219 spin_unlock(&sdp->sd_quota_spin);
223 static void slot_hold(struct gfs2_quota_data *qd)
225 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
227 spin_lock(&sdp->sd_quota_spin);
228 gfs2_assert(sdp, qd->qd_slot_count);
230 spin_unlock(&sdp->sd_quota_spin);
233 static void slot_put(struct gfs2_quota_data *qd)
235 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
237 spin_lock(&sdp->sd_quota_spin);
238 gfs2_assert(sdp, qd->qd_slot_count);
239 if (!--qd->qd_slot_count) {
240 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
243 spin_unlock(&sdp->sd_quota_spin);
246 static int bh_get(struct gfs2_quota_data *qd)
248 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
249 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
250 unsigned int block, offset;
251 struct buffer_head *bh;
253 struct buffer_head bh_map;
255 mutex_lock(&sdp->sd_quota_mutex);
257 if (qd->qd_bh_count++) {
258 mutex_unlock(&sdp->sd_quota_mutex);
262 block = qd->qd_slot / sdp->sd_qc_per_block;
263 offset = qd->qd_slot % sdp->sd_qc_per_block;;
265 error = gfs2_block_map(&ip->i_inode, block, 0, &bh_map, 1);
268 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
272 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
276 qd->qd_bh_qc = (struct gfs2_quota_change *)
277 (bh->b_data + sizeof(struct gfs2_meta_header) +
278 offset * sizeof(struct gfs2_quota_change));
280 mutex_lock(&sdp->sd_quota_mutex);
288 mutex_unlock(&sdp->sd_quota_mutex);
292 static void bh_put(struct gfs2_quota_data *qd)
294 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
296 mutex_lock(&sdp->sd_quota_mutex);
297 gfs2_assert(sdp, qd->qd_bh_count);
298 if (!--qd->qd_bh_count) {
303 mutex_unlock(&sdp->sd_quota_mutex);
306 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
308 struct gfs2_quota_data *qd = NULL;
314 if (sdp->sd_vfs->s_flags & MS_RDONLY)
317 spin_lock(&sdp->sd_quota_spin);
319 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
320 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
321 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
322 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
325 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
327 set_bit(QDF_LOCKED, &qd->qd_flags);
328 gfs2_assert_warn(sdp, qd->qd_count);
330 qd->qd_change_sync = qd->qd_change;
331 gfs2_assert_warn(sdp, qd->qd_slot_count);
341 spin_unlock(&sdp->sd_quota_spin);
344 gfs2_assert_warn(sdp, qd->qd_change_sync);
347 clear_bit(QDF_LOCKED, &qd->qd_flags);
359 static int qd_trylock(struct gfs2_quota_data *qd)
361 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
363 if (sdp->sd_vfs->s_flags & MS_RDONLY)
366 spin_lock(&sdp->sd_quota_spin);
368 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
369 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
370 spin_unlock(&sdp->sd_quota_spin);
374 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
376 set_bit(QDF_LOCKED, &qd->qd_flags);
377 gfs2_assert_warn(sdp, qd->qd_count);
379 qd->qd_change_sync = qd->qd_change;
380 gfs2_assert_warn(sdp, qd->qd_slot_count);
383 spin_unlock(&sdp->sd_quota_spin);
385 gfs2_assert_warn(sdp, qd->qd_change_sync);
387 clear_bit(QDF_LOCKED, &qd->qd_flags);
396 static void qd_unlock(struct gfs2_quota_data *qd)
398 gfs2_assert_warn(qd->qd_gl->gl_sbd,
399 test_bit(QDF_LOCKED, &qd->qd_flags));
400 clear_bit(QDF_LOCKED, &qd->qd_flags);
406 static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
407 struct gfs2_quota_data **qdp)
411 error = qd_get(sdp, user, id, create, qdp);
415 error = slot_get(*qdp);
419 error = bh_get(*qdp);
432 static void qdsb_put(struct gfs2_quota_data *qd)
439 int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
442 struct gfs2_alloc *al = &ip->i_alloc;
443 struct gfs2_quota_data **qd = al->al_qd;
446 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
447 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
450 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
453 error = qdsb_get(sdp, QUOTA_USER, ip->i_di.di_uid, CREATE, qd);
459 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_di.di_gid, CREATE, qd);
465 if (uid != NO_QUOTA_CHANGE && uid != ip->i_di.di_uid) {
466 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
473 if (gid != NO_QUOTA_CHANGE && gid != ip->i_di.di_gid) {
474 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
483 gfs2_quota_unhold(ip);
487 void gfs2_quota_unhold(struct gfs2_inode *ip)
489 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
490 struct gfs2_alloc *al = &ip->i_alloc;
493 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
495 for (x = 0; x < al->al_qd_num; x++) {
496 qdsb_put(al->al_qd[x]);
502 static int sort_qd(const void *a, const void *b)
504 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
505 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
507 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
508 !test_bit(QDF_USER, &qd_b->qd_flags)) {
509 if (test_bit(QDF_USER, &qd_a->qd_flags))
514 if (qd_a->qd_id < qd_b->qd_id)
516 if (qd_a->qd_id > qd_b->qd_id)
522 static void do_qc(struct gfs2_quota_data *qd, s64 change)
524 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
525 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
526 struct gfs2_quota_change *qc = qd->qd_bh_qc;
529 mutex_lock(&sdp->sd_quota_mutex);
530 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
532 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
535 if (test_bit(QDF_USER, &qd->qd_flags))
536 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
537 qc->qc_id = cpu_to_be32(qd->qd_id);
541 x = be64_to_cpu(x) + change;
542 qc->qc_change = cpu_to_be64(x);
544 spin_lock(&sdp->sd_quota_spin);
546 spin_unlock(&sdp->sd_quota_spin);
549 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
550 clear_bit(QDF_CHANGE, &qd->qd_flags);
555 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
560 mutex_unlock(&sdp->sd_quota_mutex);
566 * This function was mostly borrowed from gfs2_block_truncate_page which was
567 * in turn mostly borrowed from ext3
569 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
570 s64 change, struct gfs2_quota_data *qd)
572 struct inode *inode = &ip->i_inode;
573 struct address_space *mapping = inode->i_mapping;
574 unsigned long index = loc >> PAGE_CACHE_SHIFT;
575 unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
576 unsigned blocksize, iblock, pos;
577 struct buffer_head *bh;
584 page = grab_cache_page(mapping, index);
588 blocksize = inode->i_sb->s_blocksize;
589 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
591 if (!page_has_buffers(page))
592 create_empty_buffers(page, blocksize, 0);
594 bh = page_buffers(page);
596 while (offset >= pos) {
597 bh = bh->b_this_page;
602 if (!buffer_mapped(bh)) {
603 gfs2_get_block(inode, iblock, bh, 1);
604 if (!buffer_mapped(bh))
608 if (PageUptodate(page))
609 set_buffer_uptodate(bh);
611 if (!buffer_uptodate(bh)) {
612 ll_rw_block(READ, 1, &bh);
614 if (!buffer_uptodate(bh))
618 gfs2_trans_add_bh(ip->i_gl, bh, 0);
620 kaddr = kmap_atomic(page, KM_USER0);
621 ptr = kaddr + offset;
622 value = (s64)be64_to_cpu(*ptr) + change;
623 *ptr = cpu_to_be64(value);
624 flush_dcache_page(page);
625 kunmap_atomic(kaddr, KM_USER0);
627 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
628 qd->qd_qb.qb_value = cpu_to_be64(value);
631 page_cache_release(page);
635 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
637 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
638 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
639 unsigned int data_blocks, ind_blocks;
640 struct gfs2_holder *ghs, i_gh;
642 struct gfs2_quota_data *qd;
644 unsigned int nalloc = 0;
645 struct gfs2_alloc *al = NULL;
648 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
649 &data_blocks, &ind_blocks);
651 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
655 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
656 for (qx = 0; qx < num_qd; qx++) {
657 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
659 GL_NOCACHE, &ghs[qx]);
664 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
668 for (x = 0; x < num_qd; x++) {
671 offset = qd2offset(qda[x]);
672 error = gfs2_write_alloc_required(ip, offset,
673 sizeof(struct gfs2_quota),
682 al = gfs2_alloc_get(ip);
684 al->al_requested = nalloc * (data_blocks + ind_blocks);
686 error = gfs2_inplace_reserve(ip);
690 error = gfs2_trans_begin(sdp,
691 al->al_rgd->rd_ri.ri_length +
692 num_qd * data_blocks +
693 nalloc * ind_blocks +
694 RES_DINODE + num_qd +
699 error = gfs2_trans_begin(sdp,
700 num_qd * data_blocks +
701 RES_DINODE + num_qd, 0);
706 for (x = 0; x < num_qd; x++) {
708 offset = qd2offset(qd);
709 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
710 (struct gfs2_quota_data *)
715 do_qc(qd, -qd->qd_change_sync);
724 gfs2_inplace_release(ip);
729 gfs2_glock_dq_uninit(&i_gh);
732 gfs2_glock_dq_uninit(&ghs[qx]);
734 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
738 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
739 struct gfs2_holder *q_gh)
741 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
742 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
743 struct gfs2_holder i_gh;
745 char buf[sizeof(struct gfs2_quota)];
746 struct file_ra_state ra_state;
748 struct gfs2_quota_lvb *qlvb;
750 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
752 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
756 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
758 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
760 gfs2_glock_dq_uninit(q_gh);
761 error = gfs2_glock_nq_init(qd->qd_gl,
762 LM_ST_EXCLUSIVE, GL_NOCACHE,
767 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
771 memset(buf, 0, sizeof(struct gfs2_quota));
773 error = gfs2_internal_read(ip, &ra_state, buf,
774 &pos, sizeof(struct gfs2_quota));
778 gfs2_glock_dq_uninit(&i_gh);
781 gfs2_quota_in(&q, buf);
782 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
783 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
785 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
786 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
787 qlvb->qb_value = cpu_to_be64(q.qu_value);
790 if (gfs2_glock_is_blocking(qd->qd_gl)) {
791 gfs2_glock_dq_uninit(q_gh);
800 gfs2_glock_dq_uninit(&i_gh);
802 gfs2_glock_dq_uninit(q_gh);
806 int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
808 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
809 struct gfs2_alloc *al = &ip->i_alloc;
813 gfs2_quota_hold(ip, uid, gid);
815 if (capable(CAP_SYS_RESOURCE) ||
816 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
819 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
822 for (x = 0; x < al->al_qd_num; x++) {
823 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
829 set_bit(GIF_QD_LOCKED, &ip->i_flags);
832 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
833 gfs2_quota_unhold(ip);
839 static int need_sync(struct gfs2_quota_data *qd)
841 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
842 struct gfs2_tune *gt = &sdp->sd_tune;
844 unsigned int num, den;
847 if (!qd->qd_qb.qb_limit)
850 spin_lock(&sdp->sd_quota_spin);
851 value = qd->qd_change;
852 spin_unlock(&sdp->sd_quota_spin);
854 spin_lock(>->gt_spin);
855 num = gt->gt_quota_scale_num;
856 den = gt->gt_quota_scale_den;
857 spin_unlock(>->gt_spin);
861 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
862 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
865 value *= gfs2_jindex_size(sdp) * num;
867 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
868 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
875 void gfs2_quota_unlock(struct gfs2_inode *ip)
877 struct gfs2_alloc *al = &ip->i_alloc;
878 struct gfs2_quota_data *qda[4];
879 unsigned int count = 0;
882 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
885 for (x = 0; x < al->al_qd_num; x++) {
886 struct gfs2_quota_data *qd;
890 sync = need_sync(qd);
892 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
894 if (sync && qd_trylock(qd))
900 for (x = 0; x < count; x++)
905 gfs2_quota_unhold(ip);
910 static int print_message(struct gfs2_quota_data *qd, char *type)
912 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
914 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
915 sdp->sd_fsname, type,
916 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
922 int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
924 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
925 struct gfs2_alloc *al = &ip->i_alloc;
926 struct gfs2_quota_data *qd;
931 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
934 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
937 for (x = 0; x < al->al_qd_num; x++) {
940 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
941 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
944 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
945 spin_lock(&sdp->sd_quota_spin);
946 value += qd->qd_change;
947 spin_unlock(&sdp->sd_quota_spin);
949 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
950 print_message(qd, "exceeded");
953 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
954 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
955 time_after_eq(jiffies, qd->qd_last_warn +
957 gt_quota_warn_period) * HZ)) {
958 error = print_message(qd, "warning");
959 qd->qd_last_warn = jiffies;
966 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
969 struct gfs2_alloc *al = &ip->i_alloc;
970 struct gfs2_quota_data *qd;
972 unsigned int found = 0;
974 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
976 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
979 for (x = 0; x < al->al_qd_num; x++) {
982 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
983 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
990 int gfs2_quota_sync(struct gfs2_sbd *sdp)
992 struct gfs2_quota_data **qda;
993 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
998 sdp->sd_quota_sync_gen++;
1000 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1008 error = qd_fish(sdp, qda + num_qd);
1009 if (error || !qda[num_qd])
1011 if (++num_qd == max_qd)
1017 error = do_sync(num_qd, qda);
1019 for (x = 0; x < num_qd; x++)
1020 qda[x]->qd_sync_gen =
1021 sdp->sd_quota_sync_gen;
1023 for (x = 0; x < num_qd; x++)
1026 } while (!error && num_qd == max_qd);
1033 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
1035 struct gfs2_quota_data *qd;
1036 struct gfs2_holder q_gh;
1039 error = qd_get(sdp, user, id, CREATE, &qd);
1043 error = do_glock(qd, FORCE, &q_gh);
1045 gfs2_glock_dq_uninit(&q_gh);
1052 int gfs2_quota_init(struct gfs2_sbd *sdp)
1054 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1055 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1056 unsigned int x, slot = 0;
1057 unsigned int found = 0;
1062 if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) ||
1063 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1064 gfs2_consist_inode(ip);
1067 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1068 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1072 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1073 sizeof(unsigned char *), GFP_KERNEL);
1074 if (!sdp->sd_quota_bitmap)
1077 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1078 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1079 if (!sdp->sd_quota_bitmap[x])
1083 for (x = 0; x < blocks; x++) {
1084 struct buffer_head *bh;
1089 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1094 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1097 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1102 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1104 struct gfs2_quota_change qc;
1105 struct gfs2_quota_data *qd;
1107 gfs2_quota_change_in(&qc, bh->b_data +
1108 sizeof(struct gfs2_meta_header) +
1109 y * sizeof(struct gfs2_quota_change));
1113 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1120 set_bit(QDF_CHANGE, &qd->qd_flags);
1121 qd->qd_change = qc.qc_change;
1123 qd->qd_slot_count = 1;
1124 qd->qd_last_touched = jiffies;
1126 spin_lock(&sdp->sd_quota_spin);
1127 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1128 list_add(&qd->qd_list, &sdp->sd_quota_list);
1129 atomic_inc(&sdp->sd_quota_count);
1130 spin_unlock(&sdp->sd_quota_spin);
1141 fs_info(sdp, "found %u quota changes\n", found);
1146 gfs2_quota_cleanup(sdp);
1150 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1152 struct gfs2_quota_data *qd, *safe;
1155 spin_lock(&sdp->sd_quota_spin);
1156 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1157 if (!qd->qd_count &&
1158 time_after_eq(jiffies, qd->qd_last_touched +
1159 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1160 list_move(&qd->qd_list, &dead);
1161 gfs2_assert_warn(sdp,
1162 atomic_read(&sdp->sd_quota_count) > 0);
1163 atomic_dec(&sdp->sd_quota_count);
1166 spin_unlock(&sdp->sd_quota_spin);
1168 while (!list_empty(&dead)) {
1169 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1170 list_del(&qd->qd_list);
1172 gfs2_assert_warn(sdp, !qd->qd_change);
1173 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1174 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1176 gfs2_lvb_unhold(qd->qd_gl);
1181 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1183 struct list_head *head = &sdp->sd_quota_list;
1184 struct gfs2_quota_data *qd;
1187 spin_lock(&sdp->sd_quota_spin);
1188 while (!list_empty(head)) {
1189 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1191 if (qd->qd_count > 1 ||
1192 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1193 list_move(&qd->qd_list, head);
1194 spin_unlock(&sdp->sd_quota_spin);
1196 spin_lock(&sdp->sd_quota_spin);
1200 list_del(&qd->qd_list);
1201 atomic_dec(&sdp->sd_quota_count);
1202 spin_unlock(&sdp->sd_quota_spin);
1204 if (!qd->qd_count) {
1205 gfs2_assert_warn(sdp, !qd->qd_change);
1206 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1208 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1209 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1211 gfs2_lvb_unhold(qd->qd_gl);
1214 spin_lock(&sdp->sd_quota_spin);
1216 spin_unlock(&sdp->sd_quota_spin);
1218 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1220 if (sdp->sd_quota_bitmap) {
1221 for (x = 0; x < sdp->sd_quota_chunks; x++)
1222 kfree(sdp->sd_quota_bitmap[x]);
1223 kfree(sdp->sd_quota_bitmap);