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 v.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/tty.h>
45 #include <linux/sort.h>
47 #include <linux/gfs2_ondisk.h>
50 #include "lm_interface.h"
64 #include "ops_address.h"
70 static uint64_t qd2offset(struct gfs2_quota_data *qd)
74 offset = 2 * (uint64_t)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
75 offset *= sizeof(struct gfs2_quota);
80 static int qd_alloc(struct gfs2_sbd *sdp, int user, uint32_t id,
81 struct gfs2_quota_data **qdp)
83 struct gfs2_quota_data *qd;
86 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
93 set_bit(QDF_USER, &qd->qd_flags);
96 error = gfs2_glock_get(sdp, 2 * (uint64_t)id + !user,
97 &gfs2_quota_glops, CREATE, &qd->qd_gl);
101 error = gfs2_lvb_hold(qd->qd_gl);
102 gfs2_glock_put(qd->qd_gl);
115 static int qd_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
116 struct gfs2_quota_data **qdp)
118 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
125 spin_lock(&sdp->sd_quota_spin);
126 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
127 if (qd->qd_id == id &&
128 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
140 list_add(&qd->qd_list, &sdp->sd_quota_list);
141 atomic_inc(&sdp->sd_quota_count);
145 spin_unlock(&sdp->sd_quota_spin);
149 gfs2_lvb_unhold(new_qd->qd_gl);
156 error = qd_alloc(sdp, user, id, &new_qd);
162 static void qd_hold(struct gfs2_quota_data *qd)
164 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
166 spin_lock(&sdp->sd_quota_spin);
167 gfs2_assert(sdp, qd->qd_count);
169 spin_unlock(&sdp->sd_quota_spin);
172 static void qd_put(struct gfs2_quota_data *qd)
174 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
175 spin_lock(&sdp->sd_quota_spin);
176 gfs2_assert(sdp, qd->qd_count);
178 qd->qd_last_touched = jiffies;
179 spin_unlock(&sdp->sd_quota_spin);
182 static int slot_get(struct gfs2_quota_data *qd)
184 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
185 unsigned int c, o = 0, b;
186 unsigned char byte = 0;
188 spin_lock(&sdp->sd_quota_spin);
190 if (qd->qd_slot_count++) {
191 spin_unlock(&sdp->sd_quota_spin);
195 for (c = 0; c < sdp->sd_quota_chunks; c++)
196 for (o = 0; o < PAGE_SIZE; o++) {
197 byte = sdp->sd_quota_bitmap[c][o];
205 for (b = 0; b < 8; b++)
206 if (!(byte & (1 << b)))
208 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
210 if (qd->qd_slot >= sdp->sd_quota_slots)
213 sdp->sd_quota_bitmap[c][o] |= 1 << b;
215 spin_unlock(&sdp->sd_quota_spin);
221 spin_unlock(&sdp->sd_quota_spin);
225 static void slot_hold(struct gfs2_quota_data *qd)
227 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
229 spin_lock(&sdp->sd_quota_spin);
230 gfs2_assert(sdp, qd->qd_slot_count);
232 spin_unlock(&sdp->sd_quota_spin);
235 static void slot_put(struct gfs2_quota_data *qd)
237 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
239 spin_lock(&sdp->sd_quota_spin);
240 gfs2_assert(sdp, qd->qd_slot_count);
241 if (!--qd->qd_slot_count) {
242 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
245 spin_unlock(&sdp->sd_quota_spin);
248 static int bh_get(struct gfs2_quota_data *qd)
250 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
251 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
252 unsigned int block, offset;
255 struct buffer_head *bh;
259 mutex_lock(&sdp->sd_quota_mutex);
261 if (qd->qd_bh_count++) {
262 mutex_unlock(&sdp->sd_quota_mutex);
266 block = qd->qd_slot / sdp->sd_qc_per_block;
267 offset = qd->qd_slot % sdp->sd_qc_per_block;;
269 error = gfs2_block_map(&ip->i_inode, block, &new, &dblock, &boundary);
272 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT, &bh);
276 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
280 qd->qd_bh_qc = (struct gfs2_quota_change *)
281 (bh->b_data + sizeof(struct gfs2_meta_header) +
282 offset * sizeof(struct gfs2_quota_change));
284 mutex_lock(&sdp->sd_quota_mutex);
293 mutex_unlock(&sdp->sd_quota_mutex);
297 static void bh_put(struct gfs2_quota_data *qd)
299 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
301 mutex_lock(&sdp->sd_quota_mutex);
302 gfs2_assert(sdp, qd->qd_bh_count);
303 if (!--qd->qd_bh_count) {
308 mutex_unlock(&sdp->sd_quota_mutex);
311 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
313 struct gfs2_quota_data *qd = NULL;
319 if (sdp->sd_vfs->s_flags & MS_RDONLY)
322 spin_lock(&sdp->sd_quota_spin);
324 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
325 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
326 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
327 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
330 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
332 set_bit(QDF_LOCKED, &qd->qd_flags);
333 gfs2_assert_warn(sdp, qd->qd_count);
335 qd->qd_change_sync = qd->qd_change;
336 gfs2_assert_warn(sdp, qd->qd_slot_count);
346 spin_unlock(&sdp->sd_quota_spin);
349 gfs2_assert_warn(sdp, qd->qd_change_sync);
352 clear_bit(QDF_LOCKED, &qd->qd_flags);
364 static int qd_trylock(struct gfs2_quota_data *qd)
366 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
368 if (sdp->sd_vfs->s_flags & MS_RDONLY)
371 spin_lock(&sdp->sd_quota_spin);
373 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
374 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
375 spin_unlock(&sdp->sd_quota_spin);
379 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
381 set_bit(QDF_LOCKED, &qd->qd_flags);
382 gfs2_assert_warn(sdp, qd->qd_count);
384 qd->qd_change_sync = qd->qd_change;
385 gfs2_assert_warn(sdp, qd->qd_slot_count);
388 spin_unlock(&sdp->sd_quota_spin);
390 gfs2_assert_warn(sdp, qd->qd_change_sync);
392 clear_bit(QDF_LOCKED, &qd->qd_flags);
401 static void qd_unlock(struct gfs2_quota_data *qd)
403 gfs2_assert_warn(qd->qd_gl->gl_sbd,
404 test_bit(QDF_LOCKED, &qd->qd_flags));
405 clear_bit(QDF_LOCKED, &qd->qd_flags);
411 static int qdsb_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
412 struct gfs2_quota_data **qdp)
416 error = qd_get(sdp, user, id, create, qdp);
420 error = slot_get(*qdp);
424 error = bh_get(*qdp);
438 static void qdsb_put(struct gfs2_quota_data *qd)
445 int gfs2_quota_hold(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
447 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
448 struct gfs2_alloc *al = &ip->i_alloc;
449 struct gfs2_quota_data **qd = al->al_qd;
452 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
453 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
456 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
459 error = qdsb_get(sdp, QUOTA_USER, ip->i_di.di_uid, CREATE, qd);
465 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_di.di_gid, CREATE, qd);
471 if (uid != NO_QUOTA_CHANGE && uid != ip->i_di.di_uid) {
472 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
479 if (gid != NO_QUOTA_CHANGE && gid != ip->i_di.di_gid) {
480 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
489 gfs2_quota_unhold(ip);
494 void gfs2_quota_unhold(struct gfs2_inode *ip)
496 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
497 struct gfs2_alloc *al = &ip->i_alloc;
500 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
502 for (x = 0; x < al->al_qd_num; x++) {
503 qdsb_put(al->al_qd[x]);
509 static int sort_qd(const void *a, const void *b)
511 struct gfs2_quota_data *qd_a = *(struct gfs2_quota_data **)a;
512 struct gfs2_quota_data *qd_b = *(struct gfs2_quota_data **)b;
515 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
516 !test_bit(QDF_USER, &qd_b->qd_flags)) {
517 if (test_bit(QDF_USER, &qd_a->qd_flags))
522 if (qd_a->qd_id < qd_b->qd_id)
524 else if (qd_a->qd_id > qd_b->qd_id)
531 static void do_qc(struct gfs2_quota_data *qd, int64_t change)
533 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
534 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
535 struct gfs2_quota_change *qc = qd->qd_bh_qc;
538 mutex_lock(&sdp->sd_quota_mutex);
539 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
541 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
544 if (test_bit(QDF_USER, &qd->qd_flags))
545 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
546 qc->qc_id = cpu_to_be32(qd->qd_id);
550 x = be64_to_cpu(x) + change;
551 qc->qc_change = cpu_to_be64(x);
553 spin_lock(&sdp->sd_quota_spin);
555 spin_unlock(&sdp->sd_quota_spin);
558 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
559 clear_bit(QDF_CHANGE, &qd->qd_flags);
564 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
569 mutex_unlock(&sdp->sd_quota_mutex);
575 * This function was mostly borrowed from gfs2_block_truncate_page which was
576 * in turn mostly borrowed from ext3
578 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
579 int64_t change, struct gfs2_quota_data *qd)
581 struct inode *inode = &ip->i_inode;
582 struct address_space *mapping = inode->i_mapping;
583 unsigned long index = loc >> PAGE_CACHE_SHIFT;
584 unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
585 unsigned blocksize, iblock, pos;
586 struct buffer_head *bh;
593 page = grab_cache_page(mapping, index);
597 blocksize = inode->i_sb->s_blocksize;
598 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
600 if (!page_has_buffers(page))
601 create_empty_buffers(page, blocksize, 0);
603 bh = page_buffers(page);
605 while (offset >= pos) {
606 bh = bh->b_this_page;
611 if (!buffer_mapped(bh)) {
612 gfs2_get_block(inode, iblock, bh, 1);
613 if (!buffer_mapped(bh))
617 if (PageUptodate(page))
618 set_buffer_uptodate(bh);
620 if (!buffer_uptodate(bh)) {
621 ll_rw_block(READ, 1, &bh);
623 if (!buffer_uptodate(bh))
627 gfs2_trans_add_bh(ip->i_gl, bh, 0);
629 kaddr = kmap_atomic(page, KM_USER0);
630 ptr = (__be64 *)(kaddr + offset);
631 value = *ptr = cpu_to_be64(be64_to_cpu(*ptr) + change);
632 flush_dcache_page(page);
633 kunmap_atomic(kaddr, KM_USER0);
635 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
637 qd->qd_qb.qb_limit = cpu_to_be64(q.qu_limit);
638 qd->qd_qb.qb_warn = cpu_to_be64(q.qu_warn);
640 qd->qd_qb.qb_value = cpu_to_be64(value);
643 page_cache_release(page);
647 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
649 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
650 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
651 unsigned int data_blocks, ind_blocks;
652 struct gfs2_holder *ghs, i_gh;
654 struct gfs2_quota_data *qd;
656 unsigned int nalloc = 0;
657 struct gfs2_alloc *al = NULL;
660 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
661 &data_blocks, &ind_blocks);
663 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
667 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
668 for (qx = 0; qx < num_qd; qx++) {
669 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
671 GL_NOCACHE, &ghs[qx]);
676 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
680 for (x = 0; x < num_qd; x++) {
683 offset = qd2offset(qda[x]);
684 error = gfs2_write_alloc_required(ip, offset,
685 sizeof(struct gfs2_quota),
694 al = gfs2_alloc_get(ip);
696 al->al_requested = nalloc * (data_blocks + ind_blocks);
698 error = gfs2_inplace_reserve(ip);
702 error = gfs2_trans_begin(sdp,
703 al->al_rgd->rd_ri.ri_length +
704 num_qd * data_blocks +
705 nalloc * ind_blocks +
706 RES_DINODE + num_qd +
711 error = gfs2_trans_begin(sdp,
712 num_qd * data_blocks +
713 RES_DINODE + num_qd, 0);
718 for (x = 0; x < num_qd; x++) {
720 offset = qd2offset(qd);
721 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
722 (struct gfs2_quota_data *)
727 do_qc(qd, -qd->qd_change_sync);
737 gfs2_inplace_release(ip);
744 gfs2_glock_dq_uninit(&i_gh);
748 gfs2_glock_dq_uninit(&ghs[qx]);
750 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
755 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
756 struct gfs2_holder *q_gh)
758 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
759 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
760 struct gfs2_holder i_gh;
762 char buf[sizeof(struct gfs2_quota)];
763 struct file_ra_state ra_state;
766 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
768 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
772 gfs2_quota_lvb_in(&qd->qd_qb, qd->qd_gl->gl_lvb);
774 if (force_refresh || qd->qd_qb.qb_magic != GFS2_MAGIC) {
776 gfs2_glock_dq_uninit(q_gh);
777 error = gfs2_glock_nq_init(qd->qd_gl,
778 LM_ST_EXCLUSIVE, GL_NOCACHE,
783 error = gfs2_glock_nq_init(ip->i_gl,
789 memset(buf, 0, sizeof(struct gfs2_quota));
791 error = gfs2_internal_read(ip, &ra_state, buf,
792 &pos, sizeof(struct gfs2_quota));
796 gfs2_glock_dq_uninit(&i_gh);
798 gfs2_quota_in(&q, buf);
800 memset(&qd->qd_qb, 0, sizeof(struct gfs2_quota_lvb));
801 qd->qd_qb.qb_magic = GFS2_MAGIC;
802 qd->qd_qb.qb_limit = q.qu_limit;
803 qd->qd_qb.qb_warn = q.qu_warn;
804 qd->qd_qb.qb_value = q.qu_value;
806 gfs2_quota_lvb_out(&qd->qd_qb, qd->qd_gl->gl_lvb);
808 if (gfs2_glock_is_blocking(qd->qd_gl)) {
809 gfs2_glock_dq_uninit(q_gh);
818 gfs2_glock_dq_uninit(&i_gh);
821 gfs2_glock_dq_uninit(q_gh);
826 int gfs2_quota_lock(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
828 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
829 struct gfs2_alloc *al = &ip->i_alloc;
833 gfs2_quota_hold(ip, uid, gid);
835 if (capable(CAP_SYS_RESOURCE) ||
836 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
839 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
842 for (x = 0; x < al->al_qd_num; x++) {
843 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
849 set_bit(GIF_QD_LOCKED, &ip->i_flags);
852 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
853 gfs2_quota_unhold(ip);
859 static int need_sync(struct gfs2_quota_data *qd)
861 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
862 struct gfs2_tune *gt = &sdp->sd_tune;
864 unsigned int num, den;
867 if (!qd->qd_qb.qb_limit)
870 spin_lock(&sdp->sd_quota_spin);
871 value = qd->qd_change;
872 spin_unlock(&sdp->sd_quota_spin);
874 spin_lock(>->gt_spin);
875 num = gt->gt_quota_scale_num;
876 den = gt->gt_quota_scale_den;
877 spin_unlock(>->gt_spin);
881 else if (qd->qd_qb.qb_value >= (int64_t)qd->qd_qb.qb_limit)
884 value *= gfs2_jindex_size(sdp) * num;
886 value += qd->qd_qb.qb_value;
887 if (value < (int64_t)qd->qd_qb.qb_limit)
894 void gfs2_quota_unlock(struct gfs2_inode *ip)
896 struct gfs2_alloc *al = &ip->i_alloc;
897 struct gfs2_quota_data *qda[4];
898 unsigned int count = 0;
901 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
904 for (x = 0; x < al->al_qd_num; x++) {
905 struct gfs2_quota_data *qd;
909 sync = need_sync(qd);
911 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
913 if (sync && qd_trylock(qd))
919 for (x = 0; x < count; x++)
924 gfs2_quota_unhold(ip);
929 static int print_message(struct gfs2_quota_data *qd, char *type)
931 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
935 line = kmalloc(MAX_LINE, GFP_KERNEL);
939 len = snprintf(line, MAX_LINE-1,
940 "GFS2: fsid=%s: quota %s for %s %u\r\n",
941 sdp->sd_fsname, type,
942 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
944 line[MAX_LINE-1] = 0;
946 if (current->signal) { /* Is this test still required? */
947 tty_write_message(current->signal->tty, line);
955 int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
957 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
958 struct gfs2_alloc *al = &ip->i_alloc;
959 struct gfs2_quota_data *qd;
964 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
967 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
970 for (x = 0; x < al->al_qd_num; x++) {
973 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
974 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
977 value = qd->qd_qb.qb_value;
978 spin_lock(&sdp->sd_quota_spin);
979 value += qd->qd_change;
980 spin_unlock(&sdp->sd_quota_spin);
982 if (qd->qd_qb.qb_limit && (int64_t)qd->qd_qb.qb_limit < value) {
983 print_message(qd, "exceeded");
986 } else if (qd->qd_qb.qb_warn &&
987 (int64_t)qd->qd_qb.qb_warn < value &&
988 time_after_eq(jiffies, qd->qd_last_warn +
990 gt_quota_warn_period) * HZ)) {
991 error = print_message(qd, "warning");
992 qd->qd_last_warn = jiffies;
999 void gfs2_quota_change(struct gfs2_inode *ip, int64_t change,
1000 uint32_t uid, uint32_t gid)
1002 struct gfs2_alloc *al = &ip->i_alloc;
1003 struct gfs2_quota_data *qd;
1005 unsigned int found = 0;
1007 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
1009 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
1012 for (x = 0; x < al->al_qd_num; x++) {
1015 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1016 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1023 int gfs2_quota_sync(struct gfs2_sbd *sdp)
1025 struct gfs2_quota_data **qda;
1026 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1027 unsigned int num_qd;
1031 sdp->sd_quota_sync_gen++;
1033 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1041 error = qd_fish(sdp, qda + num_qd);
1042 if (error || !qda[num_qd])
1044 if (++num_qd == max_qd)
1050 error = do_sync(num_qd, qda);
1052 for (x = 0; x < num_qd; x++)
1053 qda[x]->qd_sync_gen =
1054 sdp->sd_quota_sync_gen;
1056 for (x = 0; x < num_qd; x++)
1059 } while (!error && num_qd == max_qd);
1066 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
1068 struct gfs2_quota_data *qd;
1069 struct gfs2_holder q_gh;
1072 error = qd_get(sdp, user, id, CREATE, &qd);
1076 error = do_glock(qd, FORCE, &q_gh);
1078 gfs2_glock_dq_uninit(&q_gh);
1086 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1087 struct gfs2_quota *q)
1089 struct gfs2_quota_data *qd;
1090 struct gfs2_holder q_gh;
1093 if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1094 !capable(CAP_SYS_ADMIN))
1097 error = qd_get(sdp, user, id, CREATE, &qd);
1101 error = do_glock(qd, NO_FORCE, &q_gh);
1105 memset(q, 0, sizeof(struct gfs2_quota));
1106 q->qu_limit = qd->qd_qb.qb_limit;
1107 q->qu_warn = qd->qd_qb.qb_warn;
1108 q->qu_value = qd->qd_qb.qb_value;
1110 spin_lock(&sdp->sd_quota_spin);
1111 q->qu_value += qd->qd_change;
1112 spin_unlock(&sdp->sd_quota_spin);
1114 gfs2_glock_dq_uninit(&q_gh);
1123 int gfs2_quota_init(struct gfs2_sbd *sdp)
1125 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1126 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1127 unsigned int x, slot = 0;
1128 unsigned int found = 0;
1130 uint32_t extlen = 0;
1133 if (!ip->i_di.di_size ||
1134 ip->i_di.di_size > (64 << 20) ||
1135 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1136 gfs2_consist_inode(ip);
1139 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1140 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1144 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1145 sizeof(unsigned char *), GFP_KERNEL);
1146 if (!sdp->sd_quota_bitmap)
1149 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1150 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1151 if (!sdp->sd_quota_bitmap[x])
1155 for (x = 0; x < blocks; x++) {
1156 struct buffer_head *bh;
1161 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1165 gfs2_meta_ra(ip->i_gl, dblock, extlen);
1166 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1171 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1177 y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1179 struct gfs2_quota_change qc;
1180 struct gfs2_quota_data *qd;
1182 gfs2_quota_change_in(&qc, bh->b_data +
1183 sizeof(struct gfs2_meta_header) +
1184 y * sizeof(struct gfs2_quota_change));
1188 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1195 set_bit(QDF_CHANGE, &qd->qd_flags);
1196 qd->qd_change = qc.qc_change;
1198 qd->qd_slot_count = 1;
1199 qd->qd_last_touched = jiffies;
1201 spin_lock(&sdp->sd_quota_spin);
1202 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1203 list_add(&qd->qd_list, &sdp->sd_quota_list);
1204 atomic_inc(&sdp->sd_quota_count);
1205 spin_unlock(&sdp->sd_quota_spin);
1216 fs_info(sdp, "found %u quota changes\n", found);
1221 gfs2_quota_cleanup(sdp);
1225 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1227 struct gfs2_quota_data *qd, *safe;
1230 spin_lock(&sdp->sd_quota_spin);
1231 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1232 if (!qd->qd_count &&
1233 time_after_eq(jiffies, qd->qd_last_touched +
1234 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1235 list_move(&qd->qd_list, &dead);
1236 gfs2_assert_warn(sdp,
1237 atomic_read(&sdp->sd_quota_count) > 0);
1238 atomic_dec(&sdp->sd_quota_count);
1241 spin_unlock(&sdp->sd_quota_spin);
1243 while (!list_empty(&dead)) {
1244 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1245 list_del(&qd->qd_list);
1247 gfs2_assert_warn(sdp, !qd->qd_change);
1248 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1249 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1251 gfs2_lvb_unhold(qd->qd_gl);
1256 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1258 struct list_head *head = &sdp->sd_quota_list;
1259 struct gfs2_quota_data *qd;
1262 spin_lock(&sdp->sd_quota_spin);
1263 while (!list_empty(head)) {
1264 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1266 if (qd->qd_count > 1 ||
1267 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1268 list_move(&qd->qd_list, head);
1269 spin_unlock(&sdp->sd_quota_spin);
1271 spin_lock(&sdp->sd_quota_spin);
1275 list_del(&qd->qd_list);
1276 atomic_dec(&sdp->sd_quota_count);
1277 spin_unlock(&sdp->sd_quota_spin);
1279 if (!qd->qd_count) {
1280 gfs2_assert_warn(sdp, !qd->qd_change);
1281 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1283 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1284 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1286 gfs2_lvb_unhold(qd->qd_gl);
1289 spin_lock(&sdp->sd_quota_spin);
1291 spin_unlock(&sdp->sd_quota_spin);
1293 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1295 if (sdp->sd_quota_bitmap) {
1296 for (x = 0; x < sdp->sd_quota_chunks; x++)
1297 kfree(sdp->sd_quota_bitmap[x]);
1298 kfree(sdp->sd_quota_bitmap);