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/bio.h>
47 #include <linux/gfs2_ondisk.h>
48 #include <linux/lm_interface.h>
63 #include "ops_address.h"
69 static u64 qd2offset(struct gfs2_quota_data *qd)
73 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
74 offset *= sizeof(struct gfs2_quota);
79 static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
80 struct gfs2_quota_data **qdp)
82 struct gfs2_quota_data *qd;
85 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
92 set_bit(QDF_USER, &qd->qd_flags);
95 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
96 &gfs2_quota_glops, CREATE, &qd->qd_gl);
100 error = gfs2_lvb_hold(qd->qd_gl);
101 gfs2_glock_put(qd->qd_gl);
114 static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
115 struct gfs2_quota_data **qdp)
117 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
124 spin_lock(&sdp->sd_quota_spin);
125 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
126 if (qd->qd_id == id &&
127 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
139 list_add(&qd->qd_list, &sdp->sd_quota_list);
140 atomic_inc(&sdp->sd_quota_count);
144 spin_unlock(&sdp->sd_quota_spin);
148 gfs2_lvb_unhold(new_qd->qd_gl);
155 error = qd_alloc(sdp, user, id, &new_qd);
161 static void qd_hold(struct gfs2_quota_data *qd)
163 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
165 spin_lock(&sdp->sd_quota_spin);
166 gfs2_assert(sdp, qd->qd_count);
168 spin_unlock(&sdp->sd_quota_spin);
171 static void qd_put(struct gfs2_quota_data *qd)
173 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
174 spin_lock(&sdp->sd_quota_spin);
175 gfs2_assert(sdp, qd->qd_count);
177 qd->qd_last_touched = jiffies;
178 spin_unlock(&sdp->sd_quota_spin);
181 static int slot_get(struct gfs2_quota_data *qd)
183 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
184 unsigned int c, o = 0, b;
185 unsigned char byte = 0;
187 spin_lock(&sdp->sd_quota_spin);
189 if (qd->qd_slot_count++) {
190 spin_unlock(&sdp->sd_quota_spin);
194 for (c = 0; c < sdp->sd_quota_chunks; c++)
195 for (o = 0; o < PAGE_SIZE; o++) {
196 byte = sdp->sd_quota_bitmap[c][o];
204 for (b = 0; b < 8; b++)
205 if (!(byte & (1 << b)))
207 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
209 if (qd->qd_slot >= sdp->sd_quota_slots)
212 sdp->sd_quota_bitmap[c][o] |= 1 << b;
214 spin_unlock(&sdp->sd_quota_spin);
220 spin_unlock(&sdp->sd_quota_spin);
224 static void slot_hold(struct gfs2_quota_data *qd)
226 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
228 spin_lock(&sdp->sd_quota_spin);
229 gfs2_assert(sdp, qd->qd_slot_count);
231 spin_unlock(&sdp->sd_quota_spin);
234 static void slot_put(struct gfs2_quota_data *qd)
236 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
238 spin_lock(&sdp->sd_quota_spin);
239 gfs2_assert(sdp, qd->qd_slot_count);
240 if (!--qd->qd_slot_count) {
241 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
244 spin_unlock(&sdp->sd_quota_spin);
247 static int bh_get(struct gfs2_quota_data *qd)
249 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
250 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
251 unsigned int block, offset;
252 struct buffer_head *bh;
254 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
256 mutex_lock(&sdp->sd_quota_mutex);
258 if (qd->qd_bh_count++) {
259 mutex_unlock(&sdp->sd_quota_mutex);
263 block = qd->qd_slot / sdp->sd_qc_per_block;
264 offset = qd->qd_slot % sdp->sd_qc_per_block;;
266 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
267 error = gfs2_block_map(&ip->i_inode, block, 0, &bh_map);
270 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
274 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
278 qd->qd_bh_qc = (struct gfs2_quota_change *)
279 (bh->b_data + sizeof(struct gfs2_meta_header) +
280 offset * sizeof(struct gfs2_quota_change));
282 mutex_unlock(&sdp->sd_quota_mutex);
290 mutex_unlock(&sdp->sd_quota_mutex);
294 static void bh_put(struct gfs2_quota_data *qd)
296 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
298 mutex_lock(&sdp->sd_quota_mutex);
299 gfs2_assert(sdp, qd->qd_bh_count);
300 if (!--qd->qd_bh_count) {
305 mutex_unlock(&sdp->sd_quota_mutex);
308 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
310 struct gfs2_quota_data *qd = NULL;
316 if (sdp->sd_vfs->s_flags & MS_RDONLY)
319 spin_lock(&sdp->sd_quota_spin);
321 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
322 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
323 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
324 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
327 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
329 set_bit(QDF_LOCKED, &qd->qd_flags);
330 gfs2_assert_warn(sdp, qd->qd_count);
332 qd->qd_change_sync = qd->qd_change;
333 gfs2_assert_warn(sdp, qd->qd_slot_count);
343 spin_unlock(&sdp->sd_quota_spin);
346 gfs2_assert_warn(sdp, qd->qd_change_sync);
349 clear_bit(QDF_LOCKED, &qd->qd_flags);
361 static int qd_trylock(struct gfs2_quota_data *qd)
363 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
365 if (sdp->sd_vfs->s_flags & MS_RDONLY)
368 spin_lock(&sdp->sd_quota_spin);
370 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
371 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
372 spin_unlock(&sdp->sd_quota_spin);
376 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
378 set_bit(QDF_LOCKED, &qd->qd_flags);
379 gfs2_assert_warn(sdp, qd->qd_count);
381 qd->qd_change_sync = qd->qd_change;
382 gfs2_assert_warn(sdp, qd->qd_slot_count);
385 spin_unlock(&sdp->sd_quota_spin);
387 gfs2_assert_warn(sdp, qd->qd_change_sync);
389 clear_bit(QDF_LOCKED, &qd->qd_flags);
398 static void qd_unlock(struct gfs2_quota_data *qd)
400 gfs2_assert_warn(qd->qd_gl->gl_sbd,
401 test_bit(QDF_LOCKED, &qd->qd_flags));
402 clear_bit(QDF_LOCKED, &qd->qd_flags);
408 static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
409 struct gfs2_quota_data **qdp)
413 error = qd_get(sdp, user, id, create, qdp);
417 error = slot_get(*qdp);
421 error = bh_get(*qdp);
434 static void qdsb_put(struct gfs2_quota_data *qd)
441 int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
443 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
444 struct gfs2_alloc *al = &ip->i_alloc;
445 struct gfs2_quota_data **qd = al->al_qd;
448 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
449 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
452 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
455 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
461 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
467 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
468 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
475 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
476 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
485 gfs2_quota_unhold(ip);
489 void gfs2_quota_unhold(struct gfs2_inode *ip)
491 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
492 struct gfs2_alloc *al = &ip->i_alloc;
495 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
497 for (x = 0; x < al->al_qd_num; x++) {
498 qdsb_put(al->al_qd[x]);
504 static int sort_qd(const void *a, const void *b)
506 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
507 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
509 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
510 !test_bit(QDF_USER, &qd_b->qd_flags)) {
511 if (test_bit(QDF_USER, &qd_a->qd_flags))
516 if (qd_a->qd_id < qd_b->qd_id)
518 if (qd_a->qd_id > qd_b->qd_id)
524 static void do_qc(struct gfs2_quota_data *qd, s64 change)
526 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
527 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
528 struct gfs2_quota_change *qc = qd->qd_bh_qc;
531 mutex_lock(&sdp->sd_quota_mutex);
532 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
534 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
537 if (test_bit(QDF_USER, &qd->qd_flags))
538 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
539 qc->qc_id = cpu_to_be32(qd->qd_id);
542 x = be64_to_cpu(qc->qc_change) + change;
543 qc->qc_change = cpu_to_be64(x);
545 spin_lock(&sdp->sd_quota_spin);
547 spin_unlock(&sdp->sd_quota_spin);
550 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
551 clear_bit(QDF_CHANGE, &qd->qd_flags);
556 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
561 mutex_unlock(&sdp->sd_quota_mutex);
567 * This function was mostly borrowed from gfs2_block_truncate_page which was
568 * in turn mostly borrowed from ext3
570 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
571 s64 change, struct gfs2_quota_data *qd)
573 struct inode *inode = &ip->i_inode;
574 struct address_space *mapping = inode->i_mapping;
575 unsigned long index = loc >> PAGE_CACHE_SHIFT;
576 unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
577 unsigned blocksize, iblock, pos;
578 struct buffer_head *bh;
585 page = grab_cache_page(mapping, index);
589 blocksize = inode->i_sb->s_blocksize;
590 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
592 if (!page_has_buffers(page))
593 create_empty_buffers(page, blocksize, 0);
595 bh = page_buffers(page);
597 while (offset >= pos) {
598 bh = bh->b_this_page;
603 if (!buffer_mapped(bh)) {
604 gfs2_get_block(inode, iblock, bh, 1);
605 if (!buffer_mapped(bh))
609 if (PageUptodate(page))
610 set_buffer_uptodate(bh);
612 if (!buffer_uptodate(bh)) {
613 ll_rw_block(READ_META, 1, &bh);
615 if (!buffer_uptodate(bh))
619 gfs2_trans_add_bh(ip->i_gl, bh, 0);
621 kaddr = kmap_atomic(page, KM_USER0);
622 ptr = kaddr + offset;
623 value = (s64)be64_to_cpu(*ptr) + change;
624 *ptr = cpu_to_be64(value);
625 flush_dcache_page(page);
626 kunmap_atomic(kaddr, KM_USER0);
628 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
629 qd->qd_qb.qb_value = cpu_to_be64(value);
630 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC);
631 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value);
634 page_cache_release(page);
638 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
640 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
641 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
642 unsigned int data_blocks, ind_blocks;
643 struct gfs2_holder *ghs, i_gh;
645 struct gfs2_quota_data *qd;
647 unsigned int nalloc = 0;
648 struct gfs2_alloc *al = NULL;
651 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
652 &data_blocks, &ind_blocks);
654 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
658 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
659 for (qx = 0; qx < num_qd; qx++) {
660 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
662 GL_NOCACHE, &ghs[qx]);
667 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
671 for (x = 0; x < num_qd; x++) {
674 offset = qd2offset(qda[x]);
675 error = gfs2_write_alloc_required(ip, offset,
676 sizeof(struct gfs2_quota),
685 al = gfs2_alloc_get(ip);
687 al->al_requested = nalloc * (data_blocks + ind_blocks);
689 error = gfs2_inplace_reserve(ip);
693 error = gfs2_trans_begin(sdp,
694 al->al_rgd->rd_ri.ri_length +
695 num_qd * data_blocks +
696 nalloc * ind_blocks +
697 RES_DINODE + num_qd +
702 error = gfs2_trans_begin(sdp,
703 num_qd * data_blocks +
704 RES_DINODE + num_qd, 0);
709 for (x = 0; x < num_qd; x++) {
711 offset = qd2offset(qd);
712 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
713 (struct gfs2_quota_data *)
718 do_qc(qd, -qd->qd_change_sync);
727 gfs2_inplace_release(ip);
732 gfs2_glock_dq_uninit(&i_gh);
735 gfs2_glock_dq_uninit(&ghs[qx]);
737 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
741 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
742 struct gfs2_holder *q_gh)
744 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
745 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
746 struct gfs2_holder i_gh;
747 struct gfs2_quota_host q;
748 char buf[sizeof(struct gfs2_quota)];
749 struct file_ra_state ra_state;
751 struct gfs2_quota_lvb *qlvb;
753 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
755 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
759 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
761 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
763 gfs2_glock_dq_uninit(q_gh);
764 error = gfs2_glock_nq_init(qd->qd_gl,
765 LM_ST_EXCLUSIVE, GL_NOCACHE,
770 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
774 memset(buf, 0, sizeof(struct gfs2_quota));
776 error = gfs2_internal_read(ip, &ra_state, buf,
777 &pos, sizeof(struct gfs2_quota));
781 gfs2_glock_dq_uninit(&i_gh);
784 gfs2_quota_in(&q, buf);
785 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
786 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
788 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
789 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
790 qlvb->qb_value = cpu_to_be64(q.qu_value);
793 if (gfs2_glock_is_blocking(qd->qd_gl)) {
794 gfs2_glock_dq_uninit(q_gh);
803 gfs2_glock_dq_uninit(&i_gh);
805 gfs2_glock_dq_uninit(q_gh);
809 int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
811 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
812 struct gfs2_alloc *al = &ip->i_alloc;
816 gfs2_quota_hold(ip, uid, gid);
818 if (capable(CAP_SYS_RESOURCE) ||
819 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
822 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
825 for (x = 0; x < al->al_qd_num; x++) {
826 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
832 set_bit(GIF_QD_LOCKED, &ip->i_flags);
835 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
836 gfs2_quota_unhold(ip);
842 static int need_sync(struct gfs2_quota_data *qd)
844 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
845 struct gfs2_tune *gt = &sdp->sd_tune;
847 unsigned int num, den;
850 if (!qd->qd_qb.qb_limit)
853 spin_lock(&sdp->sd_quota_spin);
854 value = qd->qd_change;
855 spin_unlock(&sdp->sd_quota_spin);
857 spin_lock(>->gt_spin);
858 num = gt->gt_quota_scale_num;
859 den = gt->gt_quota_scale_den;
860 spin_unlock(>->gt_spin);
864 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
865 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
868 value *= gfs2_jindex_size(sdp) * num;
870 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
871 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
878 void gfs2_quota_unlock(struct gfs2_inode *ip)
880 struct gfs2_alloc *al = &ip->i_alloc;
881 struct gfs2_quota_data *qda[4];
882 unsigned int count = 0;
885 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
888 for (x = 0; x < al->al_qd_num; x++) {
889 struct gfs2_quota_data *qd;
893 sync = need_sync(qd);
895 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
897 if (sync && qd_trylock(qd))
903 for (x = 0; x < count; x++)
908 gfs2_quota_unhold(ip);
913 static int print_message(struct gfs2_quota_data *qd, char *type)
915 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
917 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
918 sdp->sd_fsname, type,
919 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
925 int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
927 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
928 struct gfs2_alloc *al = &ip->i_alloc;
929 struct gfs2_quota_data *qd;
934 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
937 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
940 for (x = 0; x < al->al_qd_num; x++) {
943 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
944 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
947 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
948 spin_lock(&sdp->sd_quota_spin);
949 value += qd->qd_change;
950 spin_unlock(&sdp->sd_quota_spin);
952 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
953 print_message(qd, "exceeded");
956 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
957 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
958 time_after_eq(jiffies, qd->qd_last_warn +
960 gt_quota_warn_period) * HZ)) {
961 error = print_message(qd, "warning");
962 qd->qd_last_warn = jiffies;
969 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
972 struct gfs2_alloc *al = &ip->i_alloc;
973 struct gfs2_quota_data *qd;
975 unsigned int found = 0;
977 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
979 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
982 for (x = 0; x < al->al_qd_num; x++) {
985 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
986 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
993 int gfs2_quota_sync(struct gfs2_sbd *sdp)
995 struct gfs2_quota_data **qda;
996 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1001 sdp->sd_quota_sync_gen++;
1003 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1011 error = qd_fish(sdp, qda + num_qd);
1012 if (error || !qda[num_qd])
1014 if (++num_qd == max_qd)
1020 error = do_sync(num_qd, qda);
1022 for (x = 0; x < num_qd; x++)
1023 qda[x]->qd_sync_gen =
1024 sdp->sd_quota_sync_gen;
1026 for (x = 0; x < num_qd; x++)
1029 } while (!error && num_qd == max_qd);
1036 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
1038 struct gfs2_quota_data *qd;
1039 struct gfs2_holder q_gh;
1042 error = qd_get(sdp, user, id, CREATE, &qd);
1046 error = do_glock(qd, FORCE, &q_gh);
1048 gfs2_glock_dq_uninit(&q_gh);
1055 int gfs2_quota_init(struct gfs2_sbd *sdp)
1057 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1058 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1059 unsigned int x, slot = 0;
1060 unsigned int found = 0;
1065 if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) ||
1066 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1067 gfs2_consist_inode(ip);
1070 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1071 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1075 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1076 sizeof(unsigned char *), GFP_KERNEL);
1077 if (!sdp->sd_quota_bitmap)
1080 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1081 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1082 if (!sdp->sd_quota_bitmap[x])
1086 for (x = 0; x < blocks; x++) {
1087 struct buffer_head *bh;
1092 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1097 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1100 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1105 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1107 struct gfs2_quota_change_host qc;
1108 struct gfs2_quota_data *qd;
1110 gfs2_quota_change_in(&qc, bh->b_data +
1111 sizeof(struct gfs2_meta_header) +
1112 y * sizeof(struct gfs2_quota_change));
1116 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1123 set_bit(QDF_CHANGE, &qd->qd_flags);
1124 qd->qd_change = qc.qc_change;
1126 qd->qd_slot_count = 1;
1127 qd->qd_last_touched = jiffies;
1129 spin_lock(&sdp->sd_quota_spin);
1130 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1131 list_add(&qd->qd_list, &sdp->sd_quota_list);
1132 atomic_inc(&sdp->sd_quota_count);
1133 spin_unlock(&sdp->sd_quota_spin);
1144 fs_info(sdp, "found %u quota changes\n", found);
1149 gfs2_quota_cleanup(sdp);
1153 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1155 struct gfs2_quota_data *qd, *safe;
1158 spin_lock(&sdp->sd_quota_spin);
1159 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1160 if (!qd->qd_count &&
1161 time_after_eq(jiffies, qd->qd_last_touched +
1162 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1163 list_move(&qd->qd_list, &dead);
1164 gfs2_assert_warn(sdp,
1165 atomic_read(&sdp->sd_quota_count) > 0);
1166 atomic_dec(&sdp->sd_quota_count);
1169 spin_unlock(&sdp->sd_quota_spin);
1171 while (!list_empty(&dead)) {
1172 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1173 list_del(&qd->qd_list);
1175 gfs2_assert_warn(sdp, !qd->qd_change);
1176 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1177 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1179 gfs2_lvb_unhold(qd->qd_gl);
1184 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1186 struct list_head *head = &sdp->sd_quota_list;
1187 struct gfs2_quota_data *qd;
1190 spin_lock(&sdp->sd_quota_spin);
1191 while (!list_empty(head)) {
1192 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1194 if (qd->qd_count > 1 ||
1195 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1196 list_move(&qd->qd_list, head);
1197 spin_unlock(&sdp->sd_quota_spin);
1199 spin_lock(&sdp->sd_quota_spin);
1203 list_del(&qd->qd_list);
1204 atomic_dec(&sdp->sd_quota_count);
1205 spin_unlock(&sdp->sd_quota_spin);
1207 if (!qd->qd_count) {
1208 gfs2_assert_warn(sdp, !qd->qd_change);
1209 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1211 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1212 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1214 gfs2_lvb_unhold(qd->qd_gl);
1217 spin_lock(&sdp->sd_quota_spin);
1219 spin_unlock(&sdp->sd_quota_spin);
1221 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1223 if (sdp->sd_quota_bitmap) {
1224 for (x = 0; x < sdp->sd_quota_chunks; x++)
1225 kfree(sdp->sd_quota_bitmap[x]);
1226 kfree(sdp->sd_quota_bitmap);