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/gfs2_ondisk.h>
49 #include "lm_interface.h"
62 #include "ops_address.h"
68 static uint64_t qd2offset(struct gfs2_quota_data
*qd
)
72 offset
= 2 * (uint64_t)qd
->qd_id
+ !test_bit(QDF_USER
, &qd
->qd_flags
);
73 offset
*= sizeof(struct gfs2_quota
);
78 static int qd_alloc(struct gfs2_sbd
*sdp
, int user
, uint32_t id
,
79 struct gfs2_quota_data
**qdp
)
81 struct gfs2_quota_data
*qd
;
84 qd
= kzalloc(sizeof(struct gfs2_quota_data
), GFP_KERNEL
);
91 set_bit(QDF_USER
, &qd
->qd_flags
);
94 error
= gfs2_glock_get(sdp
, 2 * (uint64_t)id
+ !user
,
95 &gfs2_quota_glops
, CREATE
, &qd
->qd_gl
);
99 error
= gfs2_lvb_hold(qd
->qd_gl
);
100 gfs2_glock_put(qd
->qd_gl
);
113 static int qd_get(struct gfs2_sbd
*sdp
, int user
, uint32_t id
, int create
,
114 struct gfs2_quota_data
**qdp
)
116 struct gfs2_quota_data
*qd
= NULL
, *new_qd
= NULL
;
123 spin_lock(&sdp
->sd_quota_spin
);
124 list_for_each_entry(qd
, &sdp
->sd_quota_list
, qd_list
) {
125 if (qd
->qd_id
== id
&&
126 !test_bit(QDF_USER
, &qd
->qd_flags
) == !user
) {
138 list_add(&qd
->qd_list
, &sdp
->sd_quota_list
);
139 atomic_inc(&sdp
->sd_quota_count
);
143 spin_unlock(&sdp
->sd_quota_spin
);
147 gfs2_lvb_unhold(new_qd
->qd_gl
);
154 error
= qd_alloc(sdp
, user
, id
, &new_qd
);
160 static void qd_hold(struct gfs2_quota_data
*qd
)
162 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
164 spin_lock(&sdp
->sd_quota_spin
);
165 gfs2_assert(sdp
, qd
->qd_count
);
167 spin_unlock(&sdp
->sd_quota_spin
);
170 static void qd_put(struct gfs2_quota_data
*qd
)
172 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
173 spin_lock(&sdp
->sd_quota_spin
);
174 gfs2_assert(sdp
, qd
->qd_count
);
176 qd
->qd_last_touched
= jiffies
;
177 spin_unlock(&sdp
->sd_quota_spin
);
180 static int slot_get(struct gfs2_quota_data
*qd
)
182 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
183 unsigned int c
, o
= 0, b
;
184 unsigned char byte
= 0;
186 spin_lock(&sdp
->sd_quota_spin
);
188 if (qd
->qd_slot_count
++) {
189 spin_unlock(&sdp
->sd_quota_spin
);
193 for (c
= 0; c
< sdp
->sd_quota_chunks
; c
++)
194 for (o
= 0; o
< PAGE_SIZE
; o
++) {
195 byte
= sdp
->sd_quota_bitmap
[c
][o
];
203 for (b
= 0; b
< 8; b
++)
204 if (!(byte
& (1 << b
)))
206 qd
->qd_slot
= c
* (8 * PAGE_SIZE
) + o
* 8 + b
;
208 if (qd
->qd_slot
>= sdp
->sd_quota_slots
)
211 sdp
->sd_quota_bitmap
[c
][o
] |= 1 << b
;
213 spin_unlock(&sdp
->sd_quota_spin
);
219 spin_unlock(&sdp
->sd_quota_spin
);
223 static void slot_hold(struct gfs2_quota_data
*qd
)
225 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
227 spin_lock(&sdp
->sd_quota_spin
);
228 gfs2_assert(sdp
, qd
->qd_slot_count
);
230 spin_unlock(&sdp
->sd_quota_spin
);
233 static void slot_put(struct gfs2_quota_data
*qd
)
235 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
237 spin_lock(&sdp
->sd_quota_spin
);
238 gfs2_assert(sdp
, qd
->qd_slot_count
);
239 if (!--qd
->qd_slot_count
) {
240 gfs2_icbit_munge(sdp
, sdp
->sd_quota_bitmap
, qd
->qd_slot
, 0);
243 spin_unlock(&sdp
->sd_quota_spin
);
246 static int bh_get(struct gfs2_quota_data
*qd
)
248 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
249 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
250 unsigned int block
, offset
;
253 struct buffer_head
*bh
;
257 mutex_lock(&sdp
->sd_quota_mutex
);
259 if (qd
->qd_bh_count
++) {
260 mutex_unlock(&sdp
->sd_quota_mutex
);
264 block
= qd
->qd_slot
/ sdp
->sd_qc_per_block
;
265 offset
= qd
->qd_slot
% sdp
->sd_qc_per_block
;;
267 error
= gfs2_block_map(&ip
->i_inode
, block
, &new, &dblock
, &boundary
);
270 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_START
| 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_lock(&sdp
->sd_quota_mutex
);
291 mutex_unlock(&sdp
->sd_quota_mutex
);
295 static void bh_put(struct gfs2_quota_data
*qd
)
297 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
299 mutex_lock(&sdp
->sd_quota_mutex
);
300 gfs2_assert(sdp
, qd
->qd_bh_count
);
301 if (!--qd
->qd_bh_count
) {
306 mutex_unlock(&sdp
->sd_quota_mutex
);
309 static int qd_fish(struct gfs2_sbd
*sdp
, struct gfs2_quota_data
**qdp
)
311 struct gfs2_quota_data
*qd
= NULL
;
317 if (sdp
->sd_vfs
->s_flags
& MS_RDONLY
)
320 spin_lock(&sdp
->sd_quota_spin
);
322 list_for_each_entry(qd
, &sdp
->sd_quota_list
, qd_list
) {
323 if (test_bit(QDF_LOCKED
, &qd
->qd_flags
) ||
324 !test_bit(QDF_CHANGE
, &qd
->qd_flags
) ||
325 qd
->qd_sync_gen
>= sdp
->sd_quota_sync_gen
)
328 list_move_tail(&qd
->qd_list
, &sdp
->sd_quota_list
);
330 set_bit(QDF_LOCKED
, &qd
->qd_flags
);
331 gfs2_assert_warn(sdp
, qd
->qd_count
);
333 qd
->qd_change_sync
= qd
->qd_change
;
334 gfs2_assert_warn(sdp
, qd
->qd_slot_count
);
344 spin_unlock(&sdp
->sd_quota_spin
);
347 gfs2_assert_warn(sdp
, qd
->qd_change_sync
);
350 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
362 static int qd_trylock(struct gfs2_quota_data
*qd
)
364 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
366 if (sdp
->sd_vfs
->s_flags
& MS_RDONLY
)
369 spin_lock(&sdp
->sd_quota_spin
);
371 if (test_bit(QDF_LOCKED
, &qd
->qd_flags
) ||
372 !test_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
373 spin_unlock(&sdp
->sd_quota_spin
);
377 list_move_tail(&qd
->qd_list
, &sdp
->sd_quota_list
);
379 set_bit(QDF_LOCKED
, &qd
->qd_flags
);
380 gfs2_assert_warn(sdp
, qd
->qd_count
);
382 qd
->qd_change_sync
= qd
->qd_change
;
383 gfs2_assert_warn(sdp
, qd
->qd_slot_count
);
386 spin_unlock(&sdp
->sd_quota_spin
);
388 gfs2_assert_warn(sdp
, qd
->qd_change_sync
);
390 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
399 static void qd_unlock(struct gfs2_quota_data
*qd
)
401 gfs2_assert_warn(qd
->qd_gl
->gl_sbd
,
402 test_bit(QDF_LOCKED
, &qd
->qd_flags
));
403 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
409 static int qdsb_get(struct gfs2_sbd
*sdp
, int user
, uint32_t id
, int create
,
410 struct gfs2_quota_data
**qdp
)
414 error
= qd_get(sdp
, user
, id
, create
, qdp
);
418 error
= slot_get(*qdp
);
422 error
= bh_get(*qdp
);
436 static void qdsb_put(struct gfs2_quota_data
*qd
)
443 int gfs2_quota_hold(struct gfs2_inode
*ip
, uint32_t uid
, uint32_t gid
)
445 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
446 struct gfs2_alloc
*al
= &ip
->i_alloc
;
447 struct gfs2_quota_data
**qd
= al
->al_qd
;
450 if (gfs2_assert_warn(sdp
, !al
->al_qd_num
) ||
451 gfs2_assert_warn(sdp
, !test_bit(GIF_QD_LOCKED
, &ip
->i_flags
)))
454 if (sdp
->sd_args
.ar_quota
== GFS2_QUOTA_OFF
)
457 error
= qdsb_get(sdp
, QUOTA_USER
, ip
->i_di
.di_uid
, CREATE
, qd
);
463 error
= qdsb_get(sdp
, QUOTA_GROUP
, ip
->i_di
.di_gid
, CREATE
, qd
);
469 if (uid
!= NO_QUOTA_CHANGE
&& uid
!= ip
->i_di
.di_uid
) {
470 error
= qdsb_get(sdp
, QUOTA_USER
, uid
, CREATE
, qd
);
477 if (gid
!= NO_QUOTA_CHANGE
&& gid
!= ip
->i_di
.di_gid
) {
478 error
= qdsb_get(sdp
, QUOTA_GROUP
, gid
, CREATE
, qd
);
487 gfs2_quota_unhold(ip
);
492 void gfs2_quota_unhold(struct gfs2_inode
*ip
)
494 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
495 struct gfs2_alloc
*al
= &ip
->i_alloc
;
498 gfs2_assert_warn(sdp
, !test_bit(GIF_QD_LOCKED
, &ip
->i_flags
));
500 for (x
= 0; x
< al
->al_qd_num
; x
++) {
501 qdsb_put(al
->al_qd
[x
]);
507 static int sort_qd(const void *a
, const void *b
)
509 struct gfs2_quota_data
*qd_a
= *(struct gfs2_quota_data
**)a
;
510 struct gfs2_quota_data
*qd_b
= *(struct gfs2_quota_data
**)b
;
513 if (!test_bit(QDF_USER
, &qd_a
->qd_flags
) !=
514 !test_bit(QDF_USER
, &qd_b
->qd_flags
)) {
515 if (test_bit(QDF_USER
, &qd_a
->qd_flags
))
520 if (qd_a
->qd_id
< qd_b
->qd_id
)
522 else if (qd_a
->qd_id
> qd_b
->qd_id
)
529 static void do_qc(struct gfs2_quota_data
*qd
, int64_t change
)
531 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
532 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
533 struct gfs2_quota_change
*qc
= qd
->qd_bh_qc
;
536 mutex_lock(&sdp
->sd_quota_mutex
);
537 gfs2_trans_add_bh(ip
->i_gl
, qd
->qd_bh
, 1);
539 if (!test_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
542 if (test_bit(QDF_USER
, &qd
->qd_flags
))
543 qc
->qc_flags
= cpu_to_be32(GFS2_QCF_USER
);
544 qc
->qc_id
= cpu_to_be32(qd
->qd_id
);
548 x
= be64_to_cpu(x
) + change
;
549 qc
->qc_change
= cpu_to_be64(x
);
551 spin_lock(&sdp
->sd_quota_spin
);
553 spin_unlock(&sdp
->sd_quota_spin
);
556 gfs2_assert_warn(sdp
, test_bit(QDF_CHANGE
, &qd
->qd_flags
));
557 clear_bit(QDF_CHANGE
, &qd
->qd_flags
);
562 } else if (!test_and_set_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
567 mutex_unlock(&sdp
->sd_quota_mutex
);
573 * This function was mostly borrowed from gfs2_block_truncate_page which was
574 * in turn mostly borrowed from ext3
576 static int gfs2_adjust_quota(struct gfs2_inode
*ip
, loff_t loc
,
577 int64_t change
, struct gfs2_quota_data
*qd
)
579 struct inode
*inode
= &ip
->i_inode
;
580 struct address_space
*mapping
= inode
->i_mapping
;
581 unsigned long index
= loc
>> PAGE_CACHE_SHIFT
;
582 unsigned offset
= loc
& (PAGE_CACHE_SHIFT
- 1);
583 unsigned blocksize
, iblock
, pos
;
584 struct buffer_head
*bh
;
591 page
= grab_cache_page(mapping
, index
);
595 blocksize
= inode
->i_sb
->s_blocksize
;
596 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
598 if (!page_has_buffers(page
))
599 create_empty_buffers(page
, blocksize
, 0);
601 bh
= page_buffers(page
);
603 while (offset
>= pos
) {
604 bh
= bh
->b_this_page
;
609 if (!buffer_mapped(bh
)) {
610 gfs2_get_block(inode
, iblock
, bh
, 1);
611 if (!buffer_mapped(bh
))
615 if (PageUptodate(page
))
616 set_buffer_uptodate(bh
);
618 if (!buffer_uptodate(bh
)) {
619 ll_rw_block(READ
, 1, &bh
);
621 if (!buffer_uptodate(bh
))
625 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
627 kaddr
= kmap_atomic(page
, KM_USER0
);
628 ptr
= (__be64
*)(kaddr
+ offset
);
629 value
= (s64
)be64_to_cpu(*ptr
) + change
;
630 *ptr
= cpu_to_be64(value
);
631 flush_dcache_page(page
);
632 kunmap_atomic(kaddr
, KM_USER0
);
634 qd
->qd_qb
.qb_magic
= cpu_to_be32(GFS2_MAGIC
);
636 qd
->qd_qb
.qb_limit
= cpu_to_be64(q
.qu_limit
);
637 qd
->qd_qb
.qb_warn
= cpu_to_be64(q
.qu_warn
);
639 qd
->qd_qb
.qb_value
= cpu_to_be64(value
);
642 page_cache_release(page
);
646 static int do_sync(unsigned int num_qd
, struct gfs2_quota_data
**qda
)
648 struct gfs2_sbd
*sdp
= (*qda
)->qd_gl
->gl_sbd
;
649 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_quota_inode
);
650 unsigned int data_blocks
, ind_blocks
;
651 struct gfs2_holder
*ghs
, i_gh
;
653 struct gfs2_quota_data
*qd
;
655 unsigned int nalloc
= 0;
656 struct gfs2_alloc
*al
= NULL
;
659 gfs2_write_calc_reserv(ip
, sizeof(struct gfs2_quota
),
660 &data_blocks
, &ind_blocks
);
662 ghs
= kcalloc(num_qd
, sizeof(struct gfs2_holder
), GFP_KERNEL
);
666 sort(qda
, num_qd
, sizeof(struct gfs2_quota_data
*), sort_qd
, NULL
);
667 for (qx
= 0; qx
< num_qd
; qx
++) {
668 error
= gfs2_glock_nq_init(qda
[qx
]->qd_gl
,
670 GL_NOCACHE
, &ghs
[qx
]);
675 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
679 for (x
= 0; x
< num_qd
; x
++) {
682 offset
= qd2offset(qda
[x
]);
683 error
= gfs2_write_alloc_required(ip
, offset
,
684 sizeof(struct gfs2_quota
),
693 al
= gfs2_alloc_get(ip
);
695 al
->al_requested
= nalloc
* (data_blocks
+ ind_blocks
);
697 error
= gfs2_inplace_reserve(ip
);
701 error
= gfs2_trans_begin(sdp
,
702 al
->al_rgd
->rd_ri
.ri_length
+
703 num_qd
* data_blocks
+
704 nalloc
* ind_blocks
+
705 RES_DINODE
+ num_qd
+
710 error
= gfs2_trans_begin(sdp
,
711 num_qd
* data_blocks
+
712 RES_DINODE
+ num_qd
, 0);
717 for (x
= 0; x
< num_qd
; x
++) {
719 offset
= qd2offset(qd
);
720 error
= gfs2_adjust_quota(ip
, offset
, qd
->qd_change_sync
,
721 (struct gfs2_quota_data
*)
726 do_qc(qd
, -qd
->qd_change_sync
);
736 gfs2_inplace_release(ip
);
743 gfs2_glock_dq_uninit(&i_gh
);
747 gfs2_glock_dq_uninit(&ghs
[qx
]);
749 gfs2_log_flush(ip
->i_gl
->gl_sbd
, ip
->i_gl
);
754 static int do_glock(struct gfs2_quota_data
*qd
, int force_refresh
,
755 struct gfs2_holder
*q_gh
)
757 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
758 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_quota_inode
);
759 struct gfs2_holder i_gh
;
761 char buf
[sizeof(struct gfs2_quota
)];
762 struct file_ra_state ra_state
;
764 struct gfs2_quota_lvb
*qlvb
;
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 qd
->qd_qb
= *(struct gfs2_quota_lvb
*)qd
->qd_gl
->gl_lvb
;
774 if (force_refresh
|| qd
->qd_qb
.qb_magic
!= cpu_to_be32(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
, LM_ST_SHARED
, 0, &i_gh
);
787 memset(buf
, 0, sizeof(struct gfs2_quota
));
789 error
= gfs2_internal_read(ip
, &ra_state
, buf
,
790 &pos
, sizeof(struct gfs2_quota
));
794 gfs2_glock_dq_uninit(&i_gh
);
797 gfs2_quota_in(&q
, buf
);
798 qlvb
= (struct gfs2_quota_lvb
*)qd
->qd_gl
->gl_lvb
;
799 qlvb
->qb_magic
= cpu_to_be32(GFS2_MAGIC
);
801 qlvb
->qb_limit
= cpu_to_be64(q
.qu_limit
);
802 qlvb
->qb_warn
= cpu_to_be64(q
.qu_warn
);
803 qlvb
->qb_value
= cpu_to_be64(q
.qu_value
);
806 if (gfs2_glock_is_blocking(qd
->qd_gl
)) {
807 gfs2_glock_dq_uninit(q_gh
);
816 gfs2_glock_dq_uninit(&i_gh
);
819 gfs2_glock_dq_uninit(q_gh
);
824 int gfs2_quota_lock(struct gfs2_inode
*ip
, uint32_t uid
, uint32_t gid
)
826 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
827 struct gfs2_alloc
*al
= &ip
->i_alloc
;
831 gfs2_quota_hold(ip
, uid
, gid
);
833 if (capable(CAP_SYS_RESOURCE
) ||
834 sdp
->sd_args
.ar_quota
!= GFS2_QUOTA_ON
)
837 sort(al
->al_qd
, al
->al_qd_num
, sizeof(struct gfs2_quota_data
*),
840 for (x
= 0; x
< al
->al_qd_num
; x
++) {
841 error
= do_glock(al
->al_qd
[x
], NO_FORCE
, &al
->al_qd_ghs
[x
]);
847 set_bit(GIF_QD_LOCKED
, &ip
->i_flags
);
850 gfs2_glock_dq_uninit(&al
->al_qd_ghs
[x
]);
851 gfs2_quota_unhold(ip
);
857 static int need_sync(struct gfs2_quota_data
*qd
)
859 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
860 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
862 unsigned int num
, den
;
865 if (!qd
->qd_qb
.qb_limit
)
868 spin_lock(&sdp
->sd_quota_spin
);
869 value
= qd
->qd_change
;
870 spin_unlock(&sdp
->sd_quota_spin
);
872 spin_lock(>
->gt_spin
);
873 num
= gt
->gt_quota_scale_num
;
874 den
= gt
->gt_quota_scale_den
;
875 spin_unlock(>
->gt_spin
);
879 else if ((s64
)be64_to_cpu(qd
->qd_qb
.qb_value
) >=
880 (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
))
883 value
*= gfs2_jindex_size(sdp
) * num
;
885 value
+= (s64
)be64_to_cpu(qd
->qd_qb
.qb_value
);
886 if (value
< (int64_t)be64_to_cpu(qd
->qd_qb
.qb_limit
))
893 void gfs2_quota_unlock(struct gfs2_inode
*ip
)
895 struct gfs2_alloc
*al
= &ip
->i_alloc
;
896 struct gfs2_quota_data
*qda
[4];
897 unsigned int count
= 0;
900 if (!test_and_clear_bit(GIF_QD_LOCKED
, &ip
->i_flags
))
903 for (x
= 0; x
< al
->al_qd_num
; x
++) {
904 struct gfs2_quota_data
*qd
;
908 sync
= need_sync(qd
);
910 gfs2_glock_dq_uninit(&al
->al_qd_ghs
[x
]);
912 if (sync
&& qd_trylock(qd
))
918 for (x
= 0; x
< count
; x
++)
923 gfs2_quota_unhold(ip
);
928 static int print_message(struct gfs2_quota_data
*qd
, char *type
)
930 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
932 printk(KERN_INFO
"GFS2: fsid=%s: quota %s for %s %u\r\n",
933 sdp
->sd_fsname
, type
,
934 (test_bit(QDF_USER
, &qd
->qd_flags
)) ? "user" : "group",
940 int gfs2_quota_check(struct gfs2_inode
*ip
, uint32_t uid
, uint32_t gid
)
942 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
943 struct gfs2_alloc
*al
= &ip
->i_alloc
;
944 struct gfs2_quota_data
*qd
;
949 if (!test_bit(GIF_QD_LOCKED
, &ip
->i_flags
))
952 if (sdp
->sd_args
.ar_quota
!= GFS2_QUOTA_ON
)
955 for (x
= 0; x
< al
->al_qd_num
; x
++) {
958 if (!((qd
->qd_id
== uid
&& test_bit(QDF_USER
, &qd
->qd_flags
)) ||
959 (qd
->qd_id
== gid
&& !test_bit(QDF_USER
, &qd
->qd_flags
))))
962 value
= (s64
)be64_to_cpu(qd
->qd_qb
.qb_value
);
963 spin_lock(&sdp
->sd_quota_spin
);
964 value
+= qd
->qd_change
;
965 spin_unlock(&sdp
->sd_quota_spin
);
967 if (be64_to_cpu(qd
->qd_qb
.qb_limit
) && (int64_t)be64_to_cpu(qd
->qd_qb
.qb_limit
) < value
) {
968 print_message(qd
, "exceeded");
971 } else if (be64_to_cpu(qd
->qd_qb
.qb_warn
) &&
972 (int64_t)be64_to_cpu(qd
->qd_qb
.qb_warn
) < value
&&
973 time_after_eq(jiffies
, qd
->qd_last_warn
+
975 gt_quota_warn_period
) * HZ
)) {
976 error
= print_message(qd
, "warning");
977 qd
->qd_last_warn
= jiffies
;
984 void gfs2_quota_change(struct gfs2_inode
*ip
, int64_t change
,
985 uint32_t uid
, uint32_t gid
)
987 struct gfs2_alloc
*al
= &ip
->i_alloc
;
988 struct gfs2_quota_data
*qd
;
990 unsigned int found
= 0;
992 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), change
))
994 if (ip
->i_di
.di_flags
& GFS2_DIF_SYSTEM
)
997 for (x
= 0; x
< al
->al_qd_num
; x
++) {
1000 if ((qd
->qd_id
== uid
&& test_bit(QDF_USER
, &qd
->qd_flags
)) ||
1001 (qd
->qd_id
== gid
&& !test_bit(QDF_USER
, &qd
->qd_flags
))) {
1008 int gfs2_quota_sync(struct gfs2_sbd
*sdp
)
1010 struct gfs2_quota_data
**qda
;
1011 unsigned int max_qd
= gfs2_tune_get(sdp
, gt_quota_simul_sync
);
1012 unsigned int num_qd
;
1016 sdp
->sd_quota_sync_gen
++;
1018 qda
= kcalloc(max_qd
, sizeof(struct gfs2_quota_data
*), GFP_KERNEL
);
1026 error
= qd_fish(sdp
, qda
+ num_qd
);
1027 if (error
|| !qda
[num_qd
])
1029 if (++num_qd
== max_qd
)
1035 error
= do_sync(num_qd
, qda
);
1037 for (x
= 0; x
< num_qd
; x
++)
1038 qda
[x
]->qd_sync_gen
=
1039 sdp
->sd_quota_sync_gen
;
1041 for (x
= 0; x
< num_qd
; x
++)
1044 } while (!error
&& num_qd
== max_qd
);
1051 int gfs2_quota_refresh(struct gfs2_sbd
*sdp
, int user
, uint32_t id
)
1053 struct gfs2_quota_data
*qd
;
1054 struct gfs2_holder q_gh
;
1057 error
= qd_get(sdp
, user
, id
, CREATE
, &qd
);
1061 error
= do_glock(qd
, FORCE
, &q_gh
);
1063 gfs2_glock_dq_uninit(&q_gh
);
1071 int gfs2_quota_read(struct gfs2_sbd
*sdp
, int user
, uint32_t id
,
1072 struct gfs2_quota
*q
)
1074 struct gfs2_quota_data
*qd
;
1075 struct gfs2_holder q_gh
;
1078 if (((user
) ? (id
!= current
->fsuid
) : (!in_group_p(id
))) &&
1079 !capable(CAP_SYS_ADMIN
))
1082 error
= qd_get(sdp
, user
, id
, CREATE
, &qd
);
1086 error
= do_glock(qd
, NO_FORCE
, &q_gh
);
1090 memset(q
, 0, sizeof(struct gfs2_quota
));
1091 q
->qu_limit
= be64_to_cpu(qd
->qd_qb
.qb_limit
);
1092 q
->qu_warn
= be64_to_cpu(qd
->qd_qb
.qb_warn
);
1093 q
->qu_value
= be64_to_cpu(qd
->qd_qb
.qb_value
);
1095 spin_lock(&sdp
->sd_quota_spin
);
1096 q
->qu_value
+= qd
->qd_change
;
1097 spin_unlock(&sdp
->sd_quota_spin
);
1099 gfs2_glock_dq_uninit(&q_gh
);
1108 int gfs2_quota_init(struct gfs2_sbd
*sdp
)
1110 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
1111 unsigned int blocks
= ip
->i_di
.di_size
>> sdp
->sd_sb
.sb_bsize_shift
;
1112 unsigned int x
, slot
= 0;
1113 unsigned int found
= 0;
1115 uint32_t extlen
= 0;
1118 if (!ip
->i_di
.di_size
||
1119 ip
->i_di
.di_size
> (64 << 20) ||
1120 ip
->i_di
.di_size
& (sdp
->sd_sb
.sb_bsize
- 1)) {
1121 gfs2_consist_inode(ip
);
1124 sdp
->sd_quota_slots
= blocks
* sdp
->sd_qc_per_block
;
1125 sdp
->sd_quota_chunks
= DIV_ROUND_UP(sdp
->sd_quota_slots
, 8 * PAGE_SIZE
);
1129 sdp
->sd_quota_bitmap
= kcalloc(sdp
->sd_quota_chunks
,
1130 sizeof(unsigned char *), GFP_KERNEL
);
1131 if (!sdp
->sd_quota_bitmap
)
1134 for (x
= 0; x
< sdp
->sd_quota_chunks
; x
++) {
1135 sdp
->sd_quota_bitmap
[x
] = kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1136 if (!sdp
->sd_quota_bitmap
[x
])
1140 for (x
= 0; x
< blocks
; x
++) {
1141 struct buffer_head
*bh
;
1146 error
= gfs2_extent_map(&ip
->i_inode
, x
, &new, &dblock
, &extlen
);
1150 gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
1151 error
= gfs2_meta_read(ip
->i_gl
, dblock
, DIO_START
| DIO_WAIT
,
1156 if (gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_QC
)) {
1162 y
< sdp
->sd_qc_per_block
&& slot
< sdp
->sd_quota_slots
;
1164 struct gfs2_quota_change qc
;
1165 struct gfs2_quota_data
*qd
;
1167 gfs2_quota_change_in(&qc
, bh
->b_data
+
1168 sizeof(struct gfs2_meta_header
) +
1169 y
* sizeof(struct gfs2_quota_change
));
1173 error
= qd_alloc(sdp
, (qc
.qc_flags
& GFS2_QCF_USER
),
1180 set_bit(QDF_CHANGE
, &qd
->qd_flags
);
1181 qd
->qd_change
= qc
.qc_change
;
1183 qd
->qd_slot_count
= 1;
1184 qd
->qd_last_touched
= jiffies
;
1186 spin_lock(&sdp
->sd_quota_spin
);
1187 gfs2_icbit_munge(sdp
, sdp
->sd_quota_bitmap
, slot
, 1);
1188 list_add(&qd
->qd_list
, &sdp
->sd_quota_list
);
1189 atomic_inc(&sdp
->sd_quota_count
);
1190 spin_unlock(&sdp
->sd_quota_spin
);
1201 fs_info(sdp
, "found %u quota changes\n", found
);
1206 gfs2_quota_cleanup(sdp
);
1210 void gfs2_quota_scan(struct gfs2_sbd
*sdp
)
1212 struct gfs2_quota_data
*qd
, *safe
;
1215 spin_lock(&sdp
->sd_quota_spin
);
1216 list_for_each_entry_safe(qd
, safe
, &sdp
->sd_quota_list
, qd_list
) {
1217 if (!qd
->qd_count
&&
1218 time_after_eq(jiffies
, qd
->qd_last_touched
+
1219 gfs2_tune_get(sdp
, gt_quota_cache_secs
) * HZ
)) {
1220 list_move(&qd
->qd_list
, &dead
);
1221 gfs2_assert_warn(sdp
,
1222 atomic_read(&sdp
->sd_quota_count
) > 0);
1223 atomic_dec(&sdp
->sd_quota_count
);
1226 spin_unlock(&sdp
->sd_quota_spin
);
1228 while (!list_empty(&dead
)) {
1229 qd
= list_entry(dead
.next
, struct gfs2_quota_data
, qd_list
);
1230 list_del(&qd
->qd_list
);
1232 gfs2_assert_warn(sdp
, !qd
->qd_change
);
1233 gfs2_assert_warn(sdp
, !qd
->qd_slot_count
);
1234 gfs2_assert_warn(sdp
, !qd
->qd_bh_count
);
1236 gfs2_lvb_unhold(qd
->qd_gl
);
1241 void gfs2_quota_cleanup(struct gfs2_sbd
*sdp
)
1243 struct list_head
*head
= &sdp
->sd_quota_list
;
1244 struct gfs2_quota_data
*qd
;
1247 spin_lock(&sdp
->sd_quota_spin
);
1248 while (!list_empty(head
)) {
1249 qd
= list_entry(head
->prev
, struct gfs2_quota_data
, qd_list
);
1251 if (qd
->qd_count
> 1 ||
1252 (qd
->qd_count
&& !test_bit(QDF_CHANGE
, &qd
->qd_flags
))) {
1253 list_move(&qd
->qd_list
, head
);
1254 spin_unlock(&sdp
->sd_quota_spin
);
1256 spin_lock(&sdp
->sd_quota_spin
);
1260 list_del(&qd
->qd_list
);
1261 atomic_dec(&sdp
->sd_quota_count
);
1262 spin_unlock(&sdp
->sd_quota_spin
);
1264 if (!qd
->qd_count
) {
1265 gfs2_assert_warn(sdp
, !qd
->qd_change
);
1266 gfs2_assert_warn(sdp
, !qd
->qd_slot_count
);
1268 gfs2_assert_warn(sdp
, qd
->qd_slot_count
== 1);
1269 gfs2_assert_warn(sdp
, !qd
->qd_bh_count
);
1271 gfs2_lvb_unhold(qd
->qd_gl
);
1274 spin_lock(&sdp
->sd_quota_spin
);
1276 spin_unlock(&sdp
->sd_quota_spin
);
1278 gfs2_assert_warn(sdp
, !atomic_read(&sdp
->sd_quota_count
));
1280 if (sdp
->sd_quota_bitmap
) {
1281 for (x
= 0; x
< sdp
->sd_quota_chunks
; x
++)
1282 kfree(sdp
->sd_quota_bitmap
[x
]);
1283 kfree(sdp
->sd_quota_bitmap
);
This page took 0.235372 seconds and 6 git commands to generate.