2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 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/bio.h>
47 #include <linux/gfs2_ondisk.h>
48 #include <linux/lm_interface.h>
49 #include <linux/kthread.h>
50 #include <linux/freezer.h>
64 #include "ops_address.h"
70 struct gfs2_quota_host {
77 struct gfs2_quota_change_host {
79 u32 qc_flags; /* GFS2_QCF_... */
83 static u64 qd2offset(struct gfs2_quota_data *qd)
87 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
88 offset *= sizeof(struct gfs2_quota);
93 static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
94 struct gfs2_quota_data **qdp)
96 struct gfs2_quota_data *qd;
99 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
106 set_bit(QDF_USER, &qd->qd_flags);
109 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
110 &gfs2_quota_glops, CREATE, &qd->qd_gl);
114 error = gfs2_lvb_hold(qd->qd_gl);
115 gfs2_glock_put(qd->qd_gl);
124 kmem_cache_free(gfs2_quotad_cachep, qd);
128 static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
129 struct gfs2_quota_data **qdp)
131 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
138 spin_lock(&sdp->sd_quota_spin);
139 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
140 if (qd->qd_id == id &&
141 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
153 list_add(&qd->qd_list, &sdp->sd_quota_list);
154 atomic_inc(&sdp->sd_quota_count);
158 spin_unlock(&sdp->sd_quota_spin);
162 gfs2_lvb_unhold(new_qd->qd_gl);
163 kmem_cache_free(gfs2_quotad_cachep, new_qd);
169 error = qd_alloc(sdp, user, id, &new_qd);
175 static void qd_hold(struct gfs2_quota_data *qd)
177 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
179 spin_lock(&sdp->sd_quota_spin);
180 gfs2_assert(sdp, qd->qd_count);
182 spin_unlock(&sdp->sd_quota_spin);
185 static void qd_put(struct gfs2_quota_data *qd)
187 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
188 spin_lock(&sdp->sd_quota_spin);
189 gfs2_assert(sdp, qd->qd_count);
191 qd->qd_last_touched = jiffies;
192 spin_unlock(&sdp->sd_quota_spin);
195 static int slot_get(struct gfs2_quota_data *qd)
197 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
198 unsigned int c, o = 0, b;
199 unsigned char byte = 0;
201 spin_lock(&sdp->sd_quota_spin);
203 if (qd->qd_slot_count++) {
204 spin_unlock(&sdp->sd_quota_spin);
208 for (c = 0; c < sdp->sd_quota_chunks; c++)
209 for (o = 0; o < PAGE_SIZE; o++) {
210 byte = sdp->sd_quota_bitmap[c][o];
218 for (b = 0; b < 8; b++)
219 if (!(byte & (1 << b)))
221 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
223 if (qd->qd_slot >= sdp->sd_quota_slots)
226 sdp->sd_quota_bitmap[c][o] |= 1 << b;
228 spin_unlock(&sdp->sd_quota_spin);
234 spin_unlock(&sdp->sd_quota_spin);
238 static void slot_hold(struct gfs2_quota_data *qd)
240 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
242 spin_lock(&sdp->sd_quota_spin);
243 gfs2_assert(sdp, qd->qd_slot_count);
245 spin_unlock(&sdp->sd_quota_spin);
248 static void slot_put(struct gfs2_quota_data *qd)
250 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
252 spin_lock(&sdp->sd_quota_spin);
253 gfs2_assert(sdp, qd->qd_slot_count);
254 if (!--qd->qd_slot_count) {
255 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
258 spin_unlock(&sdp->sd_quota_spin);
261 static int bh_get(struct gfs2_quota_data *qd)
263 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
264 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
265 unsigned int block, offset;
266 struct buffer_head *bh;
268 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
270 mutex_lock(&sdp->sd_quota_mutex);
272 if (qd->qd_bh_count++) {
273 mutex_unlock(&sdp->sd_quota_mutex);
277 block = qd->qd_slot / sdp->sd_qc_per_block;
278 offset = qd->qd_slot % sdp->sd_qc_per_block;
280 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
281 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
284 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
288 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
292 qd->qd_bh_qc = (struct gfs2_quota_change *)
293 (bh->b_data + sizeof(struct gfs2_meta_header) +
294 offset * sizeof(struct gfs2_quota_change));
296 mutex_unlock(&sdp->sd_quota_mutex);
304 mutex_unlock(&sdp->sd_quota_mutex);
308 static void bh_put(struct gfs2_quota_data *qd)
310 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
312 mutex_lock(&sdp->sd_quota_mutex);
313 gfs2_assert(sdp, qd->qd_bh_count);
314 if (!--qd->qd_bh_count) {
319 mutex_unlock(&sdp->sd_quota_mutex);
322 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
324 struct gfs2_quota_data *qd = NULL;
330 if (sdp->sd_vfs->s_flags & MS_RDONLY)
333 spin_lock(&sdp->sd_quota_spin);
335 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
336 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
337 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
338 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
341 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
343 set_bit(QDF_LOCKED, &qd->qd_flags);
344 gfs2_assert_warn(sdp, qd->qd_count);
346 qd->qd_change_sync = qd->qd_change;
347 gfs2_assert_warn(sdp, qd->qd_slot_count);
357 spin_unlock(&sdp->sd_quota_spin);
360 gfs2_assert_warn(sdp, qd->qd_change_sync);
363 clear_bit(QDF_LOCKED, &qd->qd_flags);
375 static int qd_trylock(struct gfs2_quota_data *qd)
377 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
379 if (sdp->sd_vfs->s_flags & MS_RDONLY)
382 spin_lock(&sdp->sd_quota_spin);
384 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
385 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
386 spin_unlock(&sdp->sd_quota_spin);
390 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
392 set_bit(QDF_LOCKED, &qd->qd_flags);
393 gfs2_assert_warn(sdp, qd->qd_count);
395 qd->qd_change_sync = qd->qd_change;
396 gfs2_assert_warn(sdp, qd->qd_slot_count);
399 spin_unlock(&sdp->sd_quota_spin);
401 gfs2_assert_warn(sdp, qd->qd_change_sync);
403 clear_bit(QDF_LOCKED, &qd->qd_flags);
412 static void qd_unlock(struct gfs2_quota_data *qd)
414 gfs2_assert_warn(qd->qd_gl->gl_sbd,
415 test_bit(QDF_LOCKED, &qd->qd_flags));
416 clear_bit(QDF_LOCKED, &qd->qd_flags);
422 static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
423 struct gfs2_quota_data **qdp)
427 error = qd_get(sdp, user, id, create, qdp);
431 error = slot_get(*qdp);
435 error = bh_get(*qdp);
448 static void qdsb_put(struct gfs2_quota_data *qd)
455 int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
457 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
458 struct gfs2_alloc *al = ip->i_alloc;
459 struct gfs2_quota_data **qd = al->al_qd;
462 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
463 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
466 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
469 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
475 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
481 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
482 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
489 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
490 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
499 gfs2_quota_unhold(ip);
503 void gfs2_quota_unhold(struct gfs2_inode *ip)
505 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
506 struct gfs2_alloc *al = ip->i_alloc;
509 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
511 for (x = 0; x < al->al_qd_num; x++) {
512 qdsb_put(al->al_qd[x]);
518 static int sort_qd(const void *a, const void *b)
520 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
521 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
523 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
524 !test_bit(QDF_USER, &qd_b->qd_flags)) {
525 if (test_bit(QDF_USER, &qd_a->qd_flags))
530 if (qd_a->qd_id < qd_b->qd_id)
532 if (qd_a->qd_id > qd_b->qd_id)
538 static void do_qc(struct gfs2_quota_data *qd, s64 change)
540 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
541 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
542 struct gfs2_quota_change *qc = qd->qd_bh_qc;
545 mutex_lock(&sdp->sd_quota_mutex);
546 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
548 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
551 if (test_bit(QDF_USER, &qd->qd_flags))
552 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
553 qc->qc_id = cpu_to_be32(qd->qd_id);
556 x = be64_to_cpu(qc->qc_change) + change;
557 qc->qc_change = cpu_to_be64(x);
559 spin_lock(&sdp->sd_quota_spin);
561 spin_unlock(&sdp->sd_quota_spin);
564 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
565 clear_bit(QDF_CHANGE, &qd->qd_flags);
570 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
575 mutex_unlock(&sdp->sd_quota_mutex);
578 static void gfs2_quota_in(struct gfs2_quota_host *qu, const void *buf)
580 const struct gfs2_quota *str = buf;
582 qu->qu_limit = be64_to_cpu(str->qu_limit);
583 qu->qu_warn = be64_to_cpu(str->qu_warn);
584 qu->qu_value = be64_to_cpu(str->qu_value);
585 qu->qu_ll_next = be32_to_cpu(str->qu_ll_next);
588 static void gfs2_quota_out(const struct gfs2_quota_host *qu, void *buf)
590 struct gfs2_quota *str = buf;
592 str->qu_limit = cpu_to_be64(qu->qu_limit);
593 str->qu_warn = cpu_to_be64(qu->qu_warn);
594 str->qu_value = cpu_to_be64(qu->qu_value);
595 str->qu_ll_next = cpu_to_be32(qu->qu_ll_next);
596 memset(&str->qu_reserved, 0, sizeof(str->qu_reserved));
602 * This function was mostly borrowed from gfs2_block_truncate_page which was
603 * in turn mostly borrowed from ext3
605 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
606 s64 change, struct gfs2_quota_data *qd)
608 struct inode *inode = &ip->i_inode;
609 struct address_space *mapping = inode->i_mapping;
610 unsigned long index = loc >> PAGE_CACHE_SHIFT;
611 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
612 unsigned blocksize, iblock, pos;
613 struct buffer_head *bh;
617 struct gfs2_quota_host qp;
621 if (gfs2_is_stuffed(ip))
622 gfs2_unstuff_dinode(ip, NULL);
624 page = grab_cache_page(mapping, index);
628 blocksize = inode->i_sb->s_blocksize;
629 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
631 if (!page_has_buffers(page))
632 create_empty_buffers(page, blocksize, 0);
634 bh = page_buffers(page);
636 while (offset >= pos) {
637 bh = bh->b_this_page;
642 if (!buffer_mapped(bh)) {
643 gfs2_block_map(inode, iblock, bh, 1);
644 if (!buffer_mapped(bh))
648 if (PageUptodate(page))
649 set_buffer_uptodate(bh);
651 if (!buffer_uptodate(bh)) {
652 ll_rw_block(READ_META, 1, &bh);
654 if (!buffer_uptodate(bh))
658 gfs2_trans_add_bh(ip->i_gl, bh, 0);
660 kaddr = kmap_atomic(page, KM_USER0);
661 ptr = kaddr + offset;
662 gfs2_quota_in(&qp, ptr);
663 qp.qu_value += change;
665 gfs2_quota_out(&qp, ptr);
666 flush_dcache_page(page);
667 kunmap_atomic(kaddr, KM_USER0);
669 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
670 qd->qd_qb.qb_value = cpu_to_be64(value);
671 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC);
672 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value);
675 page_cache_release(page);
679 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
681 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
682 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
683 unsigned int data_blocks, ind_blocks;
684 struct gfs2_holder *ghs, i_gh;
686 struct gfs2_quota_data *qd;
688 unsigned int nalloc = 0, blocks;
689 struct gfs2_alloc *al = NULL;
692 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
693 &data_blocks, &ind_blocks);
695 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
699 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
700 for (qx = 0; qx < num_qd; qx++) {
701 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
703 GL_NOCACHE, &ghs[qx]);
708 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
712 for (x = 0; x < num_qd; x++) {
715 offset = qd2offset(qda[x]);
716 error = gfs2_write_alloc_required(ip, offset,
717 sizeof(struct gfs2_quota),
725 al = gfs2_alloc_get(ip);
731 * 1 blk for unstuffing inode if stuffed. We add this extra
732 * block to the reservation unconditionally. If the inode
733 * doesn't need unstuffing, the block will be released to the
734 * rgrp since it won't be allocated during the transaction
736 al->al_requested = 1;
737 /* +1 in the end for block requested above for unstuffing */
738 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 1;
741 al->al_requested += nalloc * (data_blocks + ind_blocks);
742 error = gfs2_inplace_reserve(ip);
747 blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS;
749 error = gfs2_trans_begin(sdp, blocks, 0);
753 for (x = 0; x < num_qd; x++) {
755 offset = qd2offset(qd);
756 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
757 (struct gfs2_quota_data *)
762 do_qc(qd, -qd->qd_change_sync);
770 gfs2_inplace_release(ip);
774 gfs2_glock_dq_uninit(&i_gh);
777 gfs2_glock_dq_uninit(&ghs[qx]);
779 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
783 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
784 struct gfs2_holder *q_gh)
786 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
787 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
788 struct gfs2_holder i_gh;
789 struct gfs2_quota_host q;
790 char buf[sizeof(struct gfs2_quota)];
792 struct gfs2_quota_lvb *qlvb;
795 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
799 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
801 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
803 gfs2_glock_dq_uninit(q_gh);
804 error = gfs2_glock_nq_init(qd->qd_gl,
805 LM_ST_EXCLUSIVE, GL_NOCACHE,
810 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
814 memset(buf, 0, sizeof(struct gfs2_quota));
816 error = gfs2_internal_read(ip, NULL, buf, &pos,
817 sizeof(struct gfs2_quota));
821 gfs2_glock_dq_uninit(&i_gh);
824 gfs2_quota_in(&q, buf);
825 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
826 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
828 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
829 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
830 qlvb->qb_value = cpu_to_be64(q.qu_value);
833 if (gfs2_glock_is_blocking(qd->qd_gl)) {
834 gfs2_glock_dq_uninit(q_gh);
843 gfs2_glock_dq_uninit(&i_gh);
845 gfs2_glock_dq_uninit(q_gh);
849 int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
851 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
852 struct gfs2_alloc *al = ip->i_alloc;
856 gfs2_quota_hold(ip, uid, gid);
858 if (capable(CAP_SYS_RESOURCE) ||
859 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
862 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
865 for (x = 0; x < al->al_qd_num; x++) {
866 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
872 set_bit(GIF_QD_LOCKED, &ip->i_flags);
875 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
876 gfs2_quota_unhold(ip);
882 static int need_sync(struct gfs2_quota_data *qd)
884 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
885 struct gfs2_tune *gt = &sdp->sd_tune;
887 unsigned int num, den;
890 if (!qd->qd_qb.qb_limit)
893 spin_lock(&sdp->sd_quota_spin);
894 value = qd->qd_change;
895 spin_unlock(&sdp->sd_quota_spin);
897 spin_lock(>->gt_spin);
898 num = gt->gt_quota_scale_num;
899 den = gt->gt_quota_scale_den;
900 spin_unlock(>->gt_spin);
904 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
905 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
908 value *= gfs2_jindex_size(sdp) * num;
909 value = div_s64(value, den);
910 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
911 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
918 void gfs2_quota_unlock(struct gfs2_inode *ip)
920 struct gfs2_alloc *al = ip->i_alloc;
921 struct gfs2_quota_data *qda[4];
922 unsigned int count = 0;
925 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
928 for (x = 0; x < al->al_qd_num; x++) {
929 struct gfs2_quota_data *qd;
933 sync = need_sync(qd);
935 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
937 if (sync && qd_trylock(qd))
943 for (x = 0; x < count; x++)
948 gfs2_quota_unhold(ip);
953 static int print_message(struct gfs2_quota_data *qd, char *type)
955 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
957 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
958 sdp->sd_fsname, type,
959 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
965 int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
967 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
968 struct gfs2_alloc *al = ip->i_alloc;
969 struct gfs2_quota_data *qd;
974 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
977 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
980 for (x = 0; x < al->al_qd_num; x++) {
983 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
984 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
987 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
988 spin_lock(&sdp->sd_quota_spin);
989 value += qd->qd_change;
990 spin_unlock(&sdp->sd_quota_spin);
992 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
993 print_message(qd, "exceeded");
996 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
997 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
998 time_after_eq(jiffies, qd->qd_last_warn +
1000 gt_quota_warn_period) * HZ)) {
1001 error = print_message(qd, "warning");
1002 qd->qd_last_warn = jiffies;
1009 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1012 struct gfs2_alloc *al = ip->i_alloc;
1013 struct gfs2_quota_data *qd;
1016 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
1018 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
1021 for (x = 0; x < al->al_qd_num; x++) {
1024 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1025 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1031 int gfs2_quota_sync(struct gfs2_sbd *sdp)
1033 struct gfs2_quota_data **qda;
1034 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1035 unsigned int num_qd;
1039 sdp->sd_quota_sync_gen++;
1041 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1049 error = qd_fish(sdp, qda + num_qd);
1050 if (error || !qda[num_qd])
1052 if (++num_qd == max_qd)
1058 error = do_sync(num_qd, qda);
1060 for (x = 0; x < num_qd; x++)
1061 qda[x]->qd_sync_gen =
1062 sdp->sd_quota_sync_gen;
1064 for (x = 0; x < num_qd; x++)
1067 } while (!error && num_qd == max_qd);
1074 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
1076 struct gfs2_quota_data *qd;
1077 struct gfs2_holder q_gh;
1080 error = qd_get(sdp, user, id, CREATE, &qd);
1084 error = do_glock(qd, FORCE, &q_gh);
1086 gfs2_glock_dq_uninit(&q_gh);
1093 static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1095 const struct gfs2_quota_change *str = buf;
1097 qc->qc_change = be64_to_cpu(str->qc_change);
1098 qc->qc_flags = be32_to_cpu(str->qc_flags);
1099 qc->qc_id = be32_to_cpu(str->qc_id);
1102 int gfs2_quota_init(struct gfs2_sbd *sdp)
1104 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1105 unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
1106 unsigned int x, slot = 0;
1107 unsigned int found = 0;
1112 if (!ip->i_disksize || ip->i_disksize > (64 << 20) ||
1113 ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
1114 gfs2_consist_inode(ip);
1117 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1118 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1122 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1123 sizeof(unsigned char *), GFP_NOFS);
1124 if (!sdp->sd_quota_bitmap)
1127 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1128 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
1129 if (!sdp->sd_quota_bitmap[x])
1133 for (x = 0; x < blocks; x++) {
1134 struct buffer_head *bh;
1139 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1144 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1147 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1152 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1154 struct gfs2_quota_change_host qc;
1155 struct gfs2_quota_data *qd;
1157 gfs2_quota_change_in(&qc, bh->b_data +
1158 sizeof(struct gfs2_meta_header) +
1159 y * sizeof(struct gfs2_quota_change));
1163 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1170 set_bit(QDF_CHANGE, &qd->qd_flags);
1171 qd->qd_change = qc.qc_change;
1173 qd->qd_slot_count = 1;
1174 qd->qd_last_touched = jiffies;
1176 spin_lock(&sdp->sd_quota_spin);
1177 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1178 list_add(&qd->qd_list, &sdp->sd_quota_list);
1179 atomic_inc(&sdp->sd_quota_count);
1180 spin_unlock(&sdp->sd_quota_spin);
1191 fs_info(sdp, "found %u quota changes\n", found);
1196 gfs2_quota_cleanup(sdp);
1200 static void gfs2_quota_scan(struct gfs2_sbd *sdp)
1202 struct gfs2_quota_data *qd, *safe;
1205 spin_lock(&sdp->sd_quota_spin);
1206 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1207 if (!qd->qd_count &&
1208 time_after_eq(jiffies, qd->qd_last_touched +
1209 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1210 list_move(&qd->qd_list, &dead);
1211 gfs2_assert_warn(sdp,
1212 atomic_read(&sdp->sd_quota_count) > 0);
1213 atomic_dec(&sdp->sd_quota_count);
1216 spin_unlock(&sdp->sd_quota_spin);
1218 while (!list_empty(&dead)) {
1219 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1220 list_del(&qd->qd_list);
1222 gfs2_assert_warn(sdp, !qd->qd_change);
1223 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1224 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1226 gfs2_lvb_unhold(qd->qd_gl);
1227 kmem_cache_free(gfs2_quotad_cachep, qd);
1231 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1233 struct list_head *head = &sdp->sd_quota_list;
1234 struct gfs2_quota_data *qd;
1237 spin_lock(&sdp->sd_quota_spin);
1238 while (!list_empty(head)) {
1239 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1241 if (qd->qd_count > 1 ||
1242 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1243 list_move(&qd->qd_list, head);
1244 spin_unlock(&sdp->sd_quota_spin);
1246 spin_lock(&sdp->sd_quota_spin);
1250 list_del(&qd->qd_list);
1251 atomic_dec(&sdp->sd_quota_count);
1252 spin_unlock(&sdp->sd_quota_spin);
1254 if (!qd->qd_count) {
1255 gfs2_assert_warn(sdp, !qd->qd_change);
1256 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1258 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1259 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1261 gfs2_lvb_unhold(qd->qd_gl);
1262 kmem_cache_free(gfs2_quotad_cachep, qd);
1264 spin_lock(&sdp->sd_quota_spin);
1266 spin_unlock(&sdp->sd_quota_spin);
1268 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1270 if (sdp->sd_quota_bitmap) {
1271 for (x = 0; x < sdp->sd_quota_chunks; x++)
1272 kfree(sdp->sd_quota_bitmap[x]);
1273 kfree(sdp->sd_quota_bitmap);
1277 static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1279 if (error == 0 || error == -EROFS)
1281 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1282 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1285 static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
1286 int (*fxn)(struct gfs2_sbd *sdp),
1287 unsigned long t, unsigned long *timeo,
1288 unsigned int *new_timeo)
1291 int error = fxn(sdp);
1292 quotad_error(sdp, msg, error);
1293 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1300 * gfs2_quotad - Write cached quota changes into the quota file
1301 * @sdp: Pointer to GFS2 superblock
1305 int gfs2_quotad(void *data)
1307 struct gfs2_sbd *sdp = data;
1308 struct gfs2_tune *tune = &sdp->sd_tune;
1309 unsigned long statfs_timeo = 0;
1310 unsigned long quotad_timeo = 0;
1311 unsigned long t = 0;
1314 while (!kthread_should_stop()) {
1316 /* Update the master statfs file */
1317 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1318 &statfs_timeo, &tune->gt_statfs_quantum);
1320 /* Update quota file */
1321 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1322 "ad_timeo, &tune->gt_quota_quantum);
1324 /* FIXME: This should be turned into a shrinker */
1325 gfs2_quota_scan(sdp);
1327 if (freezing(current))
1329 t = min(quotad_timeo, statfs_timeo);
1331 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_UNINTERRUPTIBLE);
1332 t -= schedule_timeout(t);
1333 finish_wait(&sdp->sd_quota_wait, &wait);