2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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>
46 #include <asm/semaphore.h>
64 static uint64_t qd2offset(struct gfs2_quota_data *qd)
68 offset = 2 * (uint64_t)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
69 offset *= sizeof(struct gfs2_quota);
74 static int qd_alloc(struct gfs2_sbd *sdp, int user, uint32_t id,
75 struct gfs2_quota_data **qdp)
77 struct gfs2_quota_data *qd;
80 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
87 set_bit(QDF_USER, &qd->qd_flags);
90 error = gfs2_glock_get(sdp, 2 * (uint64_t)id + !user,
91 &gfs2_quota_glops, CREATE, &qd->qd_gl);
95 error = gfs2_lvb_hold(qd->qd_gl);
96 gfs2_glock_put(qd->qd_gl);
109 static int qd_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
110 struct gfs2_quota_data **qdp)
112 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
119 spin_lock(&sdp->sd_quota_spin);
120 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
121 if (qd->qd_id == id &&
122 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
134 list_add(&qd->qd_list, &sdp->sd_quota_list);
135 atomic_inc(&sdp->sd_quota_count);
139 spin_unlock(&sdp->sd_quota_spin);
143 gfs2_lvb_unhold(new_qd->qd_gl);
150 error = qd_alloc(sdp, user, id, &new_qd);
156 static void qd_hold(struct gfs2_quota_data *qd)
158 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
160 spin_lock(&sdp->sd_quota_spin);
161 gfs2_assert(sdp, qd->qd_count);
163 spin_unlock(&sdp->sd_quota_spin);
166 static void qd_put(struct gfs2_quota_data *qd)
168 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
169 spin_lock(&sdp->sd_quota_spin);
170 gfs2_assert(sdp, qd->qd_count);
172 qd->qd_last_touched = jiffies;
173 spin_unlock(&sdp->sd_quota_spin);
176 static int slot_get(struct gfs2_quota_data *qd)
178 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
179 unsigned int c, o = 0, b;
180 unsigned char byte = 0;
182 spin_lock(&sdp->sd_quota_spin);
184 if (qd->qd_slot_count++) {
185 spin_unlock(&sdp->sd_quota_spin);
189 for (c = 0; c < sdp->sd_quota_chunks; c++)
190 for (o = 0; o < PAGE_SIZE; o++) {
191 byte = sdp->sd_quota_bitmap[c][o];
199 for (b = 0; b < 8; b++)
200 if (!(byte & (1 << b)))
202 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
204 if (qd->qd_slot >= sdp->sd_quota_slots)
207 sdp->sd_quota_bitmap[c][o] |= 1 << b;
209 spin_unlock(&sdp->sd_quota_spin);
215 spin_unlock(&sdp->sd_quota_spin);
219 static void slot_hold(struct gfs2_quota_data *qd)
221 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
223 spin_lock(&sdp->sd_quota_spin);
224 gfs2_assert(sdp, qd->qd_slot_count);
226 spin_unlock(&sdp->sd_quota_spin);
229 static void slot_put(struct gfs2_quota_data *qd)
231 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
233 spin_lock(&sdp->sd_quota_spin);
234 gfs2_assert(sdp, qd->qd_slot_count);
235 if (!--qd->qd_slot_count) {
236 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
239 spin_unlock(&sdp->sd_quota_spin);
242 static int bh_get(struct gfs2_quota_data *qd)
244 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
245 struct gfs2_inode *ip = get_v2ip(sdp->sd_qc_inode);
246 unsigned int block, offset;
249 struct buffer_head *bh;
252 down(&sdp->sd_quota_mutex);
254 if (qd->qd_bh_count++) {
255 up(&sdp->sd_quota_mutex);
259 block = qd->qd_slot / sdp->sd_qc_per_block;
260 offset = qd->qd_slot % sdp->sd_qc_per_block;;
262 error = gfs2_block_map(ip, block, &new, &dblock, NULL);
265 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT, &bh);
269 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
273 qd->qd_bh_qc = (struct gfs2_quota_change *)
274 (bh->b_data + sizeof(struct gfs2_meta_header) +
275 offset * sizeof(struct gfs2_quota_change));
277 up(&sdp->sd_quota_mutex);
286 up(&sdp->sd_quota_mutex);
290 static void bh_put(struct gfs2_quota_data *qd)
292 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
294 down(&sdp->sd_quota_mutex);
295 gfs2_assert(sdp, qd->qd_bh_count);
296 if (!--qd->qd_bh_count) {
301 up(&sdp->sd_quota_mutex);
304 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
306 struct gfs2_quota_data *qd = NULL;
312 if (sdp->sd_vfs->s_flags & MS_RDONLY)
315 spin_lock(&sdp->sd_quota_spin);
317 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
318 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
319 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
320 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
323 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
325 set_bit(QDF_LOCKED, &qd->qd_flags);
326 gfs2_assert_warn(sdp, qd->qd_count);
328 qd->qd_change_sync = qd->qd_change;
329 gfs2_assert_warn(sdp, qd->qd_slot_count);
339 spin_unlock(&sdp->sd_quota_spin);
342 gfs2_assert_warn(sdp, qd->qd_change_sync);
345 clear_bit(QDF_LOCKED, &qd->qd_flags);
357 static int qd_trylock(struct gfs2_quota_data *qd)
359 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
361 if (sdp->sd_vfs->s_flags & MS_RDONLY)
364 spin_lock(&sdp->sd_quota_spin);
366 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
367 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
368 spin_unlock(&sdp->sd_quota_spin);
372 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
374 set_bit(QDF_LOCKED, &qd->qd_flags);
375 gfs2_assert_warn(sdp, qd->qd_count);
377 qd->qd_change_sync = qd->qd_change;
378 gfs2_assert_warn(sdp, qd->qd_slot_count);
381 spin_unlock(&sdp->sd_quota_spin);
383 gfs2_assert_warn(sdp, qd->qd_change_sync);
385 clear_bit(QDF_LOCKED, &qd->qd_flags);
394 static void qd_unlock(struct gfs2_quota_data *qd)
396 gfs2_assert_warn(qd->qd_gl->gl_sbd, test_bit(QDF_LOCKED, &qd->qd_flags));
397 clear_bit(QDF_LOCKED, &qd->qd_flags);
403 static int qdsb_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
404 struct gfs2_quota_data **qdp)
408 error = qd_get(sdp, user, id, create, qdp);
412 error = slot_get(*qdp);
416 error = bh_get(*qdp);
430 static void qdsb_put(struct gfs2_quota_data *qd)
437 int gfs2_quota_hold(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
439 struct gfs2_sbd *sdp = ip->i_sbd;
440 struct gfs2_alloc *al = &ip->i_alloc;
441 struct gfs2_quota_data **qd = al->al_qd;
444 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
445 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
448 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
451 error = qdsb_get(sdp, QUOTA_USER, ip->i_di.di_uid, CREATE, qd);
457 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_di.di_gid, CREATE, qd);
463 if (uid != NO_QUOTA_CHANGE && uid != ip->i_di.di_uid) {
464 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
471 if (gid != NO_QUOTA_CHANGE && gid != ip->i_di.di_gid) {
472 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
481 gfs2_quota_unhold(ip);
486 void gfs2_quota_unhold(struct gfs2_inode *ip)
488 struct gfs2_sbd *sdp = ip->i_sbd;
489 struct gfs2_alloc *al = &ip->i_alloc;
492 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
494 for (x = 0; x < al->al_qd_num; x++) {
495 qdsb_put(al->al_qd[x]);
501 static int sort_qd(const void *a, const void *b)
503 struct gfs2_quota_data *qd_a = *(struct gfs2_quota_data **)a;
504 struct gfs2_quota_data *qd_b = *(struct gfs2_quota_data **)b;
507 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
508 !test_bit(QDF_USER, &qd_b->qd_flags)) {
509 if (test_bit(QDF_USER, &qd_a->qd_flags))
514 if (qd_a->qd_id < qd_b->qd_id)
516 else if (qd_a->qd_id > qd_b->qd_id)
523 static void do_qc(struct gfs2_quota_data *qd, int64_t change)
525 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
526 struct gfs2_inode *ip = get_v2ip(sdp->sd_qc_inode);
527 struct gfs2_quota_change *qc = qd->qd_bh_qc;
530 down(&sdp->sd_quota_mutex);
531 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
533 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
536 if (test_bit(QDF_USER, &qd->qd_flags))
537 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
538 qc->qc_id = cpu_to_be32(qd->qd_id);
542 x = be64_to_cpu(x) + 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 up(&sdp->sd_quota_mutex);
564 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
566 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
567 struct gfs2_inode *ip = get_v2ip(sdp->sd_quota_inode);
568 unsigned int data_blocks, ind_blocks;
569 struct file_ra_state ra_state;
570 struct gfs2_holder *ghs, i_gh;
572 struct gfs2_quota_data *qd;
574 unsigned int nalloc = 0;
575 struct gfs2_alloc *al = NULL;
578 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
579 &data_blocks, &ind_blocks);
581 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
585 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
586 for (qx = 0; qx < num_qd; qx++) {
587 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
589 GL_NOCACHE, &ghs[qx]);
594 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
598 for (x = 0; x < num_qd; x++) {
601 offset = qd2offset(qda[x]);
602 error = gfs2_write_alloc_required(ip, offset,
603 sizeof(struct gfs2_quota),
612 al = gfs2_alloc_get(ip);
614 al->al_requested = nalloc * (data_blocks + ind_blocks);
616 error = gfs2_inplace_reserve(ip);
620 error = gfs2_trans_begin(sdp,
621 al->al_rgd->rd_ri.ri_length +
622 num_qd * data_blocks +
623 nalloc * ind_blocks +
624 RES_DINODE + num_qd +
629 error = gfs2_trans_begin(sdp,
630 num_qd * data_blocks +
631 RES_DINODE + num_qd, 0);
636 file_ra_state_init(&ra_state, ip->i_vnode->i_mapping);
637 for (x = 0; x < num_qd; x++) {
638 char buf[sizeof(struct gfs2_quota)];
642 offset = qd2offset(qd);
644 /* The quota file may not be a multiple of
645 sizeof(struct gfs2_quota) bytes. */
646 memset(buf, 0, sizeof(struct gfs2_quota));
648 error = gfs2_internal_read(ip, &ra_state, buf, &offset,
649 sizeof(struct gfs2_quota));
653 gfs2_quota_in(&q, buf);
654 q.qu_value += qda[x]->qd_change_sync;
655 gfs2_quota_out(&q, buf);
657 error = gfs2_jdata_write_mem(ip, buf, offset,
658 sizeof(struct gfs2_quota));
661 else if (error != sizeof(struct gfs2_quota)) {
666 do_qc(qd, -qd->qd_change_sync);
668 memset(&qd->qd_qb, 0, sizeof(struct gfs2_quota_lvb));
669 qd->qd_qb.qb_magic = GFS2_MAGIC;
670 qd->qd_qb.qb_limit = q.qu_limit;
671 qd->qd_qb.qb_warn = q.qu_warn;
672 qd->qd_qb.qb_value = q.qu_value;
674 gfs2_quota_lvb_out(&qd->qd_qb, qd->qd_gl->gl_lvb);
684 gfs2_inplace_release(ip);
691 gfs2_glock_dq_uninit(&i_gh);
695 gfs2_glock_dq_uninit(&ghs[qx]);
697 gfs2_log_flush_glock(ip->i_gl);
702 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
703 struct gfs2_holder *q_gh)
705 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
706 struct gfs2_holder i_gh;
708 char buf[sizeof(struct gfs2_quota)];
709 struct file_ra_state ra_state;
712 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
714 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
718 gfs2_quota_lvb_in(&qd->qd_qb, qd->qd_gl->gl_lvb);
720 if (force_refresh || qd->qd_qb.qb_magic != GFS2_MAGIC) {
722 gfs2_glock_dq_uninit(q_gh);
723 error = gfs2_glock_nq_init(qd->qd_gl,
724 LM_ST_EXCLUSIVE, GL_NOCACHE,
729 error = gfs2_glock_nq_init(get_v2ip(sdp->sd_quota_inode)->i_gl,
735 memset(buf, 0, sizeof(struct gfs2_quota));
737 error = gfs2_internal_read(get_v2ip(sdp->sd_quota_inode),
740 sizeof(struct gfs2_quota));
744 gfs2_glock_dq_uninit(&i_gh);
746 gfs2_quota_in(&q, buf);
748 memset(&qd->qd_qb, 0, sizeof(struct gfs2_quota_lvb));
749 qd->qd_qb.qb_magic = GFS2_MAGIC;
750 qd->qd_qb.qb_limit = q.qu_limit;
751 qd->qd_qb.qb_warn = q.qu_warn;
752 qd->qd_qb.qb_value = q.qu_value;
754 gfs2_quota_lvb_out(&qd->qd_qb, qd->qd_gl->gl_lvb);
756 if (gfs2_glock_is_blocking(qd->qd_gl)) {
757 gfs2_glock_dq_uninit(q_gh);
766 gfs2_glock_dq_uninit(&i_gh);
769 gfs2_glock_dq_uninit(q_gh);
774 int gfs2_quota_lock(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
776 struct gfs2_sbd *sdp = ip->i_sbd;
777 struct gfs2_alloc *al = &ip->i_alloc;
781 gfs2_quota_hold(ip, uid, gid);
783 if (capable(CAP_SYS_RESOURCE) ||
784 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
787 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
790 for (x = 0; x < al->al_qd_num; x++) {
791 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
797 set_bit(GIF_QD_LOCKED, &ip->i_flags);
800 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
801 gfs2_quota_unhold(ip);
807 static int need_sync(struct gfs2_quota_data *qd)
809 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
810 struct gfs2_tune *gt = &sdp->sd_tune;
812 unsigned int num, den;
815 if (!qd->qd_qb.qb_limit)
818 spin_lock(&sdp->sd_quota_spin);
819 value = qd->qd_change;
820 spin_unlock(&sdp->sd_quota_spin);
822 spin_lock(>->gt_spin);
823 num = gt->gt_quota_scale_num;
824 den = gt->gt_quota_scale_den;
825 spin_unlock(>->gt_spin);
829 else if (qd->qd_qb.qb_value >= (int64_t)qd->qd_qb.qb_limit)
832 value *= gfs2_jindex_size(sdp) * num;
834 value += qd->qd_qb.qb_value;
835 if (value < (int64_t)qd->qd_qb.qb_limit)
842 void gfs2_quota_unlock(struct gfs2_inode *ip)
844 struct gfs2_alloc *al = &ip->i_alloc;
845 struct gfs2_quota_data *qda[4];
846 unsigned int count = 0;
849 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
852 for (x = 0; x < al->al_qd_num; x++) {
853 struct gfs2_quota_data *qd;
857 sync = need_sync(qd);
859 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
861 if (sync && qd_trylock(qd))
867 for (x = 0; x < count; x++)
872 gfs2_quota_unhold(ip);
877 static int print_message(struct gfs2_quota_data *qd, char *type)
879 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
883 line = kmalloc(MAX_LINE, GFP_KERNEL);
887 len = snprintf(line, MAX_LINE-1, "GFS2: fsid=%s: quota %s for %s %u\r\n",
888 sdp->sd_fsname, type,
889 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
891 line[MAX_LINE-1] = 0;
893 if (current->signal) { /* Is this test still required? */
894 tty_write_message(current->signal->tty, line);
902 int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
904 struct gfs2_sbd *sdp = ip->i_sbd;
905 struct gfs2_alloc *al = &ip->i_alloc;
906 struct gfs2_quota_data *qd;
911 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
914 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
917 for (x = 0; x < al->al_qd_num; x++) {
920 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
921 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
924 value = qd->qd_qb.qb_value;
925 spin_lock(&sdp->sd_quota_spin);
926 value += qd->qd_change;
927 spin_unlock(&sdp->sd_quota_spin);
929 if (qd->qd_qb.qb_limit && (int64_t)qd->qd_qb.qb_limit < value) {
930 print_message(qd, "exceeded");
933 } else if (qd->qd_qb.qb_warn &&
934 (int64_t)qd->qd_qb.qb_warn < value &&
935 time_after_eq(jiffies, qd->qd_last_warn +
936 gfs2_tune_get(sdp, gt_quota_warn_period) * HZ)) {
937 error = print_message(qd, "warning");
938 qd->qd_last_warn = jiffies;
945 void gfs2_quota_change(struct gfs2_inode *ip, int64_t change,
946 uint32_t uid, uint32_t gid)
948 struct gfs2_alloc *al = &ip->i_alloc;
949 struct gfs2_quota_data *qd;
951 unsigned int found = 0;
953 if (gfs2_assert_warn(ip->i_sbd, change))
955 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
958 for (x = 0; x < al->al_qd_num; x++) {
961 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
962 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
969 int gfs2_quota_sync(struct gfs2_sbd *sdp)
971 struct gfs2_quota_data **qda;
972 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
977 sdp->sd_quota_sync_gen++;
979 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
987 error = qd_fish(sdp, qda + num_qd);
988 if (error || !qda[num_qd])
990 if (++num_qd == max_qd)
996 error = do_sync(num_qd, qda);
998 for (x = 0; x < num_qd; x++)
999 qda[x]->qd_sync_gen =
1000 sdp->sd_quota_sync_gen;
1002 for (x = 0; x < num_qd; x++)
1005 } while (!error && num_qd == max_qd);
1012 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
1014 struct gfs2_quota_data *qd;
1015 struct gfs2_holder q_gh;
1018 error = qd_get(sdp, user, id, CREATE, &qd);
1022 error = do_glock(qd, FORCE, &q_gh);
1024 gfs2_glock_dq_uninit(&q_gh);
1031 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1032 struct gfs2_quota *q)
1034 struct gfs2_quota_data *qd;
1035 struct gfs2_holder q_gh;
1038 if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1039 !capable(CAP_SYS_ADMIN))
1042 error = qd_get(sdp, user, id, CREATE, &qd);
1046 error = do_glock(qd, NO_FORCE, &q_gh);
1050 memset(q, 0, sizeof(struct gfs2_quota));
1051 q->qu_limit = qd->qd_qb.qb_limit;
1052 q->qu_warn = qd->qd_qb.qb_warn;
1053 q->qu_value = qd->qd_qb.qb_value;
1055 spin_lock(&sdp->sd_quota_spin);
1056 q->qu_value += qd->qd_change;
1057 spin_unlock(&sdp->sd_quota_spin);
1059 gfs2_glock_dq_uninit(&q_gh);
1067 int gfs2_quota_init(struct gfs2_sbd *sdp)
1069 struct gfs2_inode *ip = get_v2ip(sdp->sd_qc_inode);
1070 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1071 unsigned int x, slot = 0;
1072 unsigned int found = 0;
1074 uint32_t extlen = 0;
1077 if (!ip->i_di.di_size ||
1078 ip->i_di.di_size > (64 << 20) ||
1079 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1080 gfs2_consist_inode(ip);
1083 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1084 sdp->sd_quota_chunks = DIV_RU(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1088 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1089 sizeof(unsigned char *), GFP_KERNEL);
1090 if (!sdp->sd_quota_bitmap)
1093 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1094 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1095 if (!sdp->sd_quota_bitmap[x])
1099 for (x = 0; x < blocks; x++) {
1100 struct buffer_head *bh;
1105 error = gfs2_block_map(ip, x, &new, &dblock, &extlen);
1109 gfs2_meta_ra(ip->i_gl, dblock, extlen);
1110 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1115 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1121 y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1123 struct gfs2_quota_change qc;
1124 struct gfs2_quota_data *qd;
1126 gfs2_quota_change_in(&qc, bh->b_data +
1127 sizeof(struct gfs2_meta_header) +
1128 y * sizeof(struct gfs2_quota_change));
1132 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1139 set_bit(QDF_CHANGE, &qd->qd_flags);
1140 qd->qd_change = qc.qc_change;
1142 qd->qd_slot_count = 1;
1143 qd->qd_last_touched = jiffies;
1145 spin_lock(&sdp->sd_quota_spin);
1146 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1147 list_add(&qd->qd_list, &sdp->sd_quota_list);
1148 atomic_inc(&sdp->sd_quota_count);
1149 spin_unlock(&sdp->sd_quota_spin);
1160 fs_info(sdp, "found %u quota changes\n", found);
1165 gfs2_quota_cleanup(sdp);
1169 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1171 struct gfs2_quota_data *qd, *safe;
1174 spin_lock(&sdp->sd_quota_spin);
1175 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1176 if (!qd->qd_count &&
1177 time_after_eq(jiffies, qd->qd_last_touched +
1178 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1179 list_move(&qd->qd_list, &dead);
1180 gfs2_assert_warn(sdp,
1181 atomic_read(&sdp->sd_quota_count) > 0);
1182 atomic_dec(&sdp->sd_quota_count);
1185 spin_unlock(&sdp->sd_quota_spin);
1187 while (!list_empty(&dead)) {
1188 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1189 list_del(&qd->qd_list);
1191 gfs2_assert_warn(sdp, !qd->qd_change);
1192 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1193 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1195 gfs2_lvb_unhold(qd->qd_gl);
1200 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1202 struct list_head *head = &sdp->sd_quota_list;
1203 struct gfs2_quota_data *qd;
1206 spin_lock(&sdp->sd_quota_spin);
1207 while (!list_empty(head)) {
1208 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1210 if (qd->qd_count > 1 ||
1211 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1212 list_move(&qd->qd_list, head);
1213 spin_unlock(&sdp->sd_quota_spin);
1215 spin_lock(&sdp->sd_quota_spin);
1219 list_del(&qd->qd_list);
1220 atomic_dec(&sdp->sd_quota_count);
1221 spin_unlock(&sdp->sd_quota_spin);
1223 if (!qd->qd_count) {
1224 gfs2_assert_warn(sdp, !qd->qd_change);
1225 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1227 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1228 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1230 gfs2_lvb_unhold(qd->qd_gl);
1233 spin_lock(&sdp->sd_quota_spin);
1235 spin_unlock(&sdp->sd_quota_spin);
1237 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1239 if (sdp->sd_quota_bitmap) {
1240 for (x = 0; x < sdp->sd_quota_chunks; x++)
1241 kfree(sdp->sd_quota_bitmap[x]);
1242 kfree(sdp->sd_quota_bitmap);