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 = sdp->sd_qc_inode->u.generic_ip;
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_vnode, 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 = ip->i_sbd;
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 = ip->i_sbd;
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 = sdp->sd_qc_inode->u.generic_ip;
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_vnode;
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 = sdp->sd_quota_inode->u.generic_ip;
651 unsigned int data_blocks, ind_blocks;
652 struct file_ra_state ra_state;
653 struct gfs2_holder *ghs, i_gh;
655 struct gfs2_quota_data *qd;
657 unsigned int nalloc = 0;
658 struct gfs2_alloc *al = NULL;
661 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
662 &data_blocks, &ind_blocks);
664 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
668 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
669 for (qx = 0; qx < num_qd; qx++) {
670 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
672 GL_NOCACHE, &ghs[qx]);
677 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
681 for (x = 0; x < num_qd; x++) {
684 offset = qd2offset(qda[x]);
685 error = gfs2_write_alloc_required(ip, offset,
686 sizeof(struct gfs2_quota),
695 al = gfs2_alloc_get(ip);
697 al->al_requested = nalloc * (data_blocks + ind_blocks);
699 error = gfs2_inplace_reserve(ip);
703 error = gfs2_trans_begin(sdp,
704 al->al_rgd->rd_ri.ri_length +
705 num_qd * data_blocks +
706 nalloc * ind_blocks +
707 RES_DINODE + num_qd +
712 error = gfs2_trans_begin(sdp,
713 num_qd * data_blocks +
714 RES_DINODE + num_qd, 0);
719 file_ra_state_init(&ra_state, ip->i_vnode->i_mapping);
720 for (x = 0; x < num_qd; x++) {
722 offset = qd2offset(qd);
723 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
724 (struct gfs2_quota_data *)
729 do_qc(qd, -qd->qd_change_sync);
739 gfs2_inplace_release(ip);
746 gfs2_glock_dq_uninit(&i_gh);
750 gfs2_glock_dq_uninit(&ghs[qx]);
752 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
757 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
758 struct gfs2_holder *q_gh)
760 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
761 struct gfs2_inode *ip = sdp->sd_quota_inode->u.generic_ip;
762 struct gfs2_holder i_gh;
764 char buf[sizeof(struct gfs2_quota)];
765 struct file_ra_state ra_state;
768 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
770 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
774 gfs2_quota_lvb_in(&qd->qd_qb, qd->qd_gl->gl_lvb);
776 if (force_refresh || qd->qd_qb.qb_magic != GFS2_MAGIC) {
778 gfs2_glock_dq_uninit(q_gh);
779 error = gfs2_glock_nq_init(qd->qd_gl,
780 LM_ST_EXCLUSIVE, GL_NOCACHE,
785 error = gfs2_glock_nq_init(ip->i_gl,
791 memset(buf, 0, sizeof(struct gfs2_quota));
793 error = gfs2_internal_read(ip,
796 sizeof(struct gfs2_quota));
800 gfs2_glock_dq_uninit(&i_gh);
802 gfs2_quota_in(&q, buf);
804 memset(&qd->qd_qb, 0, sizeof(struct gfs2_quota_lvb));
805 qd->qd_qb.qb_magic = GFS2_MAGIC;
806 qd->qd_qb.qb_limit = q.qu_limit;
807 qd->qd_qb.qb_warn = q.qu_warn;
808 qd->qd_qb.qb_value = q.qu_value;
810 gfs2_quota_lvb_out(&qd->qd_qb, qd->qd_gl->gl_lvb);
812 if (gfs2_glock_is_blocking(qd->qd_gl)) {
813 gfs2_glock_dq_uninit(q_gh);
822 gfs2_glock_dq_uninit(&i_gh);
825 gfs2_glock_dq_uninit(q_gh);
830 int gfs2_quota_lock(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
832 struct gfs2_sbd *sdp = ip->i_sbd;
833 struct gfs2_alloc *al = &ip->i_alloc;
837 gfs2_quota_hold(ip, uid, gid);
839 if (capable(CAP_SYS_RESOURCE) ||
840 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
843 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
846 for (x = 0; x < al->al_qd_num; x++) {
847 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
853 set_bit(GIF_QD_LOCKED, &ip->i_flags);
856 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
857 gfs2_quota_unhold(ip);
863 static int need_sync(struct gfs2_quota_data *qd)
865 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
866 struct gfs2_tune *gt = &sdp->sd_tune;
868 unsigned int num, den;
871 if (!qd->qd_qb.qb_limit)
874 spin_lock(&sdp->sd_quota_spin);
875 value = qd->qd_change;
876 spin_unlock(&sdp->sd_quota_spin);
878 spin_lock(>->gt_spin);
879 num = gt->gt_quota_scale_num;
880 den = gt->gt_quota_scale_den;
881 spin_unlock(>->gt_spin);
885 else if (qd->qd_qb.qb_value >= (int64_t)qd->qd_qb.qb_limit)
888 value *= gfs2_jindex_size(sdp) * num;
890 value += qd->qd_qb.qb_value;
891 if (value < (int64_t)qd->qd_qb.qb_limit)
898 void gfs2_quota_unlock(struct gfs2_inode *ip)
900 struct gfs2_alloc *al = &ip->i_alloc;
901 struct gfs2_quota_data *qda[4];
902 unsigned int count = 0;
905 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
908 for (x = 0; x < al->al_qd_num; x++) {
909 struct gfs2_quota_data *qd;
913 sync = need_sync(qd);
915 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
917 if (sync && qd_trylock(qd))
923 for (x = 0; x < count; x++)
928 gfs2_quota_unhold(ip);
933 static int print_message(struct gfs2_quota_data *qd, char *type)
935 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
939 line = kmalloc(MAX_LINE, GFP_KERNEL);
943 len = snprintf(line, MAX_LINE-1,
944 "GFS2: fsid=%s: quota %s for %s %u\r\n",
945 sdp->sd_fsname, type,
946 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
948 line[MAX_LINE-1] = 0;
950 if (current->signal) { /* Is this test still required? */
951 tty_write_message(current->signal->tty, line);
959 int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
961 struct gfs2_sbd *sdp = ip->i_sbd;
962 struct gfs2_alloc *al = &ip->i_alloc;
963 struct gfs2_quota_data *qd;
968 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
971 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
974 for (x = 0; x < al->al_qd_num; x++) {
977 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
978 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
981 value = qd->qd_qb.qb_value;
982 spin_lock(&sdp->sd_quota_spin);
983 value += qd->qd_change;
984 spin_unlock(&sdp->sd_quota_spin);
986 if (qd->qd_qb.qb_limit && (int64_t)qd->qd_qb.qb_limit < value) {
987 print_message(qd, "exceeded");
990 } else if (qd->qd_qb.qb_warn &&
991 (int64_t)qd->qd_qb.qb_warn < value &&
992 time_after_eq(jiffies, qd->qd_last_warn +
994 gt_quota_warn_period) * HZ)) {
995 error = print_message(qd, "warning");
996 qd->qd_last_warn = jiffies;
1003 void gfs2_quota_change(struct gfs2_inode *ip, int64_t change,
1004 uint32_t uid, uint32_t gid)
1006 struct gfs2_alloc *al = &ip->i_alloc;
1007 struct gfs2_quota_data *qd;
1009 unsigned int found = 0;
1011 if (gfs2_assert_warn(ip->i_sbd, change))
1013 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
1016 for (x = 0; x < al->al_qd_num; x++) {
1019 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1020 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1027 int gfs2_quota_sync(struct gfs2_sbd *sdp)
1029 struct gfs2_quota_data **qda;
1030 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1031 unsigned int num_qd;
1035 sdp->sd_quota_sync_gen++;
1037 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1045 error = qd_fish(sdp, qda + num_qd);
1046 if (error || !qda[num_qd])
1048 if (++num_qd == max_qd)
1054 error = do_sync(num_qd, qda);
1056 for (x = 0; x < num_qd; x++)
1057 qda[x]->qd_sync_gen =
1058 sdp->sd_quota_sync_gen;
1060 for (x = 0; x < num_qd; x++)
1063 } while (!error && num_qd == max_qd);
1070 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
1072 struct gfs2_quota_data *qd;
1073 struct gfs2_holder q_gh;
1076 error = qd_get(sdp, user, id, CREATE, &qd);
1080 error = do_glock(qd, FORCE, &q_gh);
1082 gfs2_glock_dq_uninit(&q_gh);
1090 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1091 struct gfs2_quota *q)
1093 struct gfs2_quota_data *qd;
1094 struct gfs2_holder q_gh;
1097 if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1098 !capable(CAP_SYS_ADMIN))
1101 error = qd_get(sdp, user, id, CREATE, &qd);
1105 error = do_glock(qd, NO_FORCE, &q_gh);
1109 memset(q, 0, sizeof(struct gfs2_quota));
1110 q->qu_limit = qd->qd_qb.qb_limit;
1111 q->qu_warn = qd->qd_qb.qb_warn;
1112 q->qu_value = qd->qd_qb.qb_value;
1114 spin_lock(&sdp->sd_quota_spin);
1115 q->qu_value += qd->qd_change;
1116 spin_unlock(&sdp->sd_quota_spin);
1118 gfs2_glock_dq_uninit(&q_gh);
1127 int gfs2_quota_init(struct gfs2_sbd *sdp)
1129 struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
1130 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1131 unsigned int x, slot = 0;
1132 unsigned int found = 0;
1134 uint32_t extlen = 0;
1137 if (!ip->i_di.di_size ||
1138 ip->i_di.di_size > (64 << 20) ||
1139 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1140 gfs2_consist_inode(ip);
1143 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1144 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1148 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1149 sizeof(unsigned char *), GFP_KERNEL);
1150 if (!sdp->sd_quota_bitmap)
1153 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1154 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1155 if (!sdp->sd_quota_bitmap[x])
1159 for (x = 0; x < blocks; x++) {
1160 struct buffer_head *bh;
1165 error = gfs2_extent_map(ip->i_vnode, x, &new, &dblock, &extlen);
1169 gfs2_meta_ra(ip->i_gl, dblock, extlen);
1170 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1175 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1181 y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1183 struct gfs2_quota_change qc;
1184 struct gfs2_quota_data *qd;
1186 gfs2_quota_change_in(&qc, bh->b_data +
1187 sizeof(struct gfs2_meta_header) +
1188 y * sizeof(struct gfs2_quota_change));
1192 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1199 set_bit(QDF_CHANGE, &qd->qd_flags);
1200 qd->qd_change = qc.qc_change;
1202 qd->qd_slot_count = 1;
1203 qd->qd_last_touched = jiffies;
1205 spin_lock(&sdp->sd_quota_spin);
1206 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1207 list_add(&qd->qd_list, &sdp->sd_quota_list);
1208 atomic_inc(&sdp->sd_quota_count);
1209 spin_unlock(&sdp->sd_quota_spin);
1220 fs_info(sdp, "found %u quota changes\n", found);
1225 gfs2_quota_cleanup(sdp);
1229 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1231 struct gfs2_quota_data *qd, *safe;
1234 spin_lock(&sdp->sd_quota_spin);
1235 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1236 if (!qd->qd_count &&
1237 time_after_eq(jiffies, qd->qd_last_touched +
1238 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1239 list_move(&qd->qd_list, &dead);
1240 gfs2_assert_warn(sdp,
1241 atomic_read(&sdp->sd_quota_count) > 0);
1242 atomic_dec(&sdp->sd_quota_count);
1245 spin_unlock(&sdp->sd_quota_spin);
1247 while (!list_empty(&dead)) {
1248 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1249 list_del(&qd->qd_list);
1251 gfs2_assert_warn(sdp, !qd->qd_change);
1252 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1253 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1255 gfs2_lvb_unhold(qd->qd_gl);
1260 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1262 struct list_head *head = &sdp->sd_quota_list;
1263 struct gfs2_quota_data *qd;
1266 spin_lock(&sdp->sd_quota_spin);
1267 while (!list_empty(head)) {
1268 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1270 if (qd->qd_count > 1 ||
1271 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1272 list_move(&qd->qd_list, head);
1273 spin_unlock(&sdp->sd_quota_spin);
1275 spin_lock(&sdp->sd_quota_spin);
1279 list_del(&qd->qd_list);
1280 atomic_dec(&sdp->sd_quota_count);
1281 spin_unlock(&sdp->sd_quota_spin);
1283 if (!qd->qd_count) {
1284 gfs2_assert_warn(sdp, !qd->qd_change);
1285 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1287 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1288 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1290 gfs2_lvb_unhold(qd->qd_gl);
1293 spin_lock(&sdp->sd_quota_spin);
1295 spin_unlock(&sdp->sd_quota_spin);
1297 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1299 if (sdp->sd_quota_bitmap) {
1300 for (x = 0; x < sdp->sd_quota_chunks; x++)
1301 kfree(sdp->sd_quota_bitmap[x]);
1302 kfree(sdp->sd_quota_bitmap);