2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 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/kthread.h>
49 #include <linux/freezer.h>
63 #include "ops_address.h"
69 struct gfs2_quota_host
{
76 struct gfs2_quota_change_host
{
78 u32 qc_flags
; /* GFS2_QCF_... */
82 static LIST_HEAD(qd_lru_list
);
83 static atomic_t qd_lru_count
= ATOMIC_INIT(0);
84 static DEFINE_SPINLOCK(qd_lru_lock
);
86 int gfs2_shrink_qd_memory(int nr
, gfp_t gfp_mask
)
88 struct gfs2_quota_data
*qd
;
94 if (!(gfp_mask
& __GFP_FS
))
97 spin_lock(&qd_lru_lock
);
98 while (nr
&& !list_empty(&qd_lru_list
)) {
99 qd
= list_entry(qd_lru_list
.next
,
100 struct gfs2_quota_data
, qd_reclaim
);
101 sdp
= qd
->qd_gl
->gl_sbd
;
103 /* Free from the filesystem-specific list */
104 list_del(&qd
->qd_list
);
106 gfs2_assert_warn(sdp
, !qd
->qd_change
);
107 gfs2_assert_warn(sdp
, !qd
->qd_slot_count
);
108 gfs2_assert_warn(sdp
, !qd
->qd_bh_count
);
110 gfs2_glock_put(qd
->qd_gl
);
111 atomic_dec(&sdp
->sd_quota_count
);
113 /* Delete it from the common reclaim list */
114 list_del_init(&qd
->qd_reclaim
);
115 atomic_dec(&qd_lru_count
);
116 spin_unlock(&qd_lru_lock
);
117 kmem_cache_free(gfs2_quotad_cachep
, qd
);
118 spin_lock(&qd_lru_lock
);
121 spin_unlock(&qd_lru_lock
);
124 return (atomic_read(&qd_lru_count
) * sysctl_vfs_cache_pressure
) / 100;
127 static u64
qd2offset(struct gfs2_quota_data
*qd
)
131 offset
= 2 * (u64
)qd
->qd_id
+ !test_bit(QDF_USER
, &qd
->qd_flags
);
132 offset
*= sizeof(struct gfs2_quota
);
137 static int qd_alloc(struct gfs2_sbd
*sdp
, int user
, u32 id
,
138 struct gfs2_quota_data
**qdp
)
140 struct gfs2_quota_data
*qd
;
143 qd
= kmem_cache_zalloc(gfs2_quotad_cachep
, GFP_NOFS
);
147 atomic_set(&qd
->qd_count
, 1);
150 set_bit(QDF_USER
, &qd
->qd_flags
);
152 INIT_LIST_HEAD(&qd
->qd_reclaim
);
154 error
= gfs2_glock_get(sdp
, 2 * (u64
)id
+ !user
,
155 &gfs2_quota_glops
, CREATE
, &qd
->qd_gl
);
164 kmem_cache_free(gfs2_quotad_cachep
, qd
);
168 static int qd_get(struct gfs2_sbd
*sdp
, int user
, u32 id
, int create
,
169 struct gfs2_quota_data
**qdp
)
171 struct gfs2_quota_data
*qd
= NULL
, *new_qd
= NULL
;
178 spin_lock(&qd_lru_lock
);
179 list_for_each_entry(qd
, &sdp
->sd_quota_list
, qd_list
) {
180 if (qd
->qd_id
== id
&&
181 !test_bit(QDF_USER
, &qd
->qd_flags
) == !user
) {
182 if (!atomic_read(&qd
->qd_count
) &&
183 !list_empty(&qd
->qd_reclaim
)) {
184 /* Remove it from reclaim list */
185 list_del_init(&qd
->qd_reclaim
);
186 atomic_dec(&qd_lru_count
);
188 atomic_inc(&qd
->qd_count
);
199 list_add(&qd
->qd_list
, &sdp
->sd_quota_list
);
200 atomic_inc(&sdp
->sd_quota_count
);
204 spin_unlock(&qd_lru_lock
);
208 gfs2_glock_put(new_qd
->qd_gl
);
209 kmem_cache_free(gfs2_quotad_cachep
, new_qd
);
215 error
= qd_alloc(sdp
, user
, id
, &new_qd
);
221 static void qd_hold(struct gfs2_quota_data
*qd
)
223 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
224 gfs2_assert(sdp
, atomic_read(&qd
->qd_count
));
225 atomic_inc(&qd
->qd_count
);
228 static void qd_put(struct gfs2_quota_data
*qd
)
230 if (atomic_dec_and_lock(&qd
->qd_count
, &qd_lru_lock
)) {
231 /* Add to the reclaim list */
232 list_add_tail(&qd
->qd_reclaim
, &qd_lru_list
);
233 atomic_inc(&qd_lru_count
);
234 spin_unlock(&qd_lru_lock
);
238 static int slot_get(struct gfs2_quota_data
*qd
)
240 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
241 unsigned int c
, o
= 0, b
;
242 unsigned char byte
= 0;
244 spin_lock(&qd_lru_lock
);
246 if (qd
->qd_slot_count
++) {
247 spin_unlock(&qd_lru_lock
);
251 for (c
= 0; c
< sdp
->sd_quota_chunks
; c
++)
252 for (o
= 0; o
< PAGE_SIZE
; o
++) {
253 byte
= sdp
->sd_quota_bitmap
[c
][o
];
261 for (b
= 0; b
< 8; b
++)
262 if (!(byte
& (1 << b
)))
264 qd
->qd_slot
= c
* (8 * PAGE_SIZE
) + o
* 8 + b
;
266 if (qd
->qd_slot
>= sdp
->sd_quota_slots
)
269 sdp
->sd_quota_bitmap
[c
][o
] |= 1 << b
;
271 spin_unlock(&qd_lru_lock
);
277 spin_unlock(&qd_lru_lock
);
281 static void slot_hold(struct gfs2_quota_data
*qd
)
283 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
285 spin_lock(&qd_lru_lock
);
286 gfs2_assert(sdp
, qd
->qd_slot_count
);
288 spin_unlock(&qd_lru_lock
);
291 static void slot_put(struct gfs2_quota_data
*qd
)
293 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
295 spin_lock(&qd_lru_lock
);
296 gfs2_assert(sdp
, qd
->qd_slot_count
);
297 if (!--qd
->qd_slot_count
) {
298 gfs2_icbit_munge(sdp
, sdp
->sd_quota_bitmap
, qd
->qd_slot
, 0);
301 spin_unlock(&qd_lru_lock
);
304 static int bh_get(struct gfs2_quota_data
*qd
)
306 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
307 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
308 unsigned int block
, offset
;
309 struct buffer_head
*bh
;
311 struct buffer_head bh_map
= { .b_state
= 0, .b_blocknr
= 0 };
313 mutex_lock(&sdp
->sd_quota_mutex
);
315 if (qd
->qd_bh_count
++) {
316 mutex_unlock(&sdp
->sd_quota_mutex
);
320 block
= qd
->qd_slot
/ sdp
->sd_qc_per_block
;
321 offset
= qd
->qd_slot
% sdp
->sd_qc_per_block
;
323 bh_map
.b_size
= 1 << ip
->i_inode
.i_blkbits
;
324 error
= gfs2_block_map(&ip
->i_inode
, block
, &bh_map
, 0);
327 error
= gfs2_meta_read(ip
->i_gl
, bh_map
.b_blocknr
, DIO_WAIT
, &bh
);
331 if (gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_QC
))
335 qd
->qd_bh_qc
= (struct gfs2_quota_change
*)
336 (bh
->b_data
+ sizeof(struct gfs2_meta_header
) +
337 offset
* sizeof(struct gfs2_quota_change
));
339 mutex_unlock(&sdp
->sd_quota_mutex
);
347 mutex_unlock(&sdp
->sd_quota_mutex
);
351 static void bh_put(struct gfs2_quota_data
*qd
)
353 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
355 mutex_lock(&sdp
->sd_quota_mutex
);
356 gfs2_assert(sdp
, qd
->qd_bh_count
);
357 if (!--qd
->qd_bh_count
) {
362 mutex_unlock(&sdp
->sd_quota_mutex
);
365 static int qd_fish(struct gfs2_sbd
*sdp
, struct gfs2_quota_data
**qdp
)
367 struct gfs2_quota_data
*qd
= NULL
;
373 if (sdp
->sd_vfs
->s_flags
& MS_RDONLY
)
376 spin_lock(&qd_lru_lock
);
378 list_for_each_entry(qd
, &sdp
->sd_quota_list
, qd_list
) {
379 if (test_bit(QDF_LOCKED
, &qd
->qd_flags
) ||
380 !test_bit(QDF_CHANGE
, &qd
->qd_flags
) ||
381 qd
->qd_sync_gen
>= sdp
->sd_quota_sync_gen
)
384 list_move_tail(&qd
->qd_list
, &sdp
->sd_quota_list
);
386 set_bit(QDF_LOCKED
, &qd
->qd_flags
);
387 gfs2_assert_warn(sdp
, atomic_read(&qd
->qd_count
));
388 atomic_inc(&qd
->qd_count
);
389 qd
->qd_change_sync
= qd
->qd_change
;
390 gfs2_assert_warn(sdp
, qd
->qd_slot_count
);
400 spin_unlock(&qd_lru_lock
);
403 gfs2_assert_warn(sdp
, qd
->qd_change_sync
);
406 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
418 static int qd_trylock(struct gfs2_quota_data
*qd
)
420 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
422 if (sdp
->sd_vfs
->s_flags
& MS_RDONLY
)
425 spin_lock(&qd_lru_lock
);
427 if (test_bit(QDF_LOCKED
, &qd
->qd_flags
) ||
428 !test_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
429 spin_unlock(&qd_lru_lock
);
433 list_move_tail(&qd
->qd_list
, &sdp
->sd_quota_list
);
435 set_bit(QDF_LOCKED
, &qd
->qd_flags
);
436 gfs2_assert_warn(sdp
, atomic_read(&qd
->qd_count
));
437 atomic_inc(&qd
->qd_count
);
438 qd
->qd_change_sync
= qd
->qd_change
;
439 gfs2_assert_warn(sdp
, qd
->qd_slot_count
);
442 spin_unlock(&qd_lru_lock
);
444 gfs2_assert_warn(sdp
, qd
->qd_change_sync
);
446 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
455 static void qd_unlock(struct gfs2_quota_data
*qd
)
457 gfs2_assert_warn(qd
->qd_gl
->gl_sbd
,
458 test_bit(QDF_LOCKED
, &qd
->qd_flags
));
459 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
465 static int qdsb_get(struct gfs2_sbd
*sdp
, int user
, u32 id
, int create
,
466 struct gfs2_quota_data
**qdp
)
470 error
= qd_get(sdp
, user
, id
, create
, qdp
);
474 error
= slot_get(*qdp
);
478 error
= bh_get(*qdp
);
491 static void qdsb_put(struct gfs2_quota_data
*qd
)
498 int gfs2_quota_hold(struct gfs2_inode
*ip
, u32 uid
, u32 gid
)
500 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
501 struct gfs2_alloc
*al
= ip
->i_alloc
;
502 struct gfs2_quota_data
**qd
= al
->al_qd
;
505 if (gfs2_assert_warn(sdp
, !al
->al_qd_num
) ||
506 gfs2_assert_warn(sdp
, !test_bit(GIF_QD_LOCKED
, &ip
->i_flags
)))
509 if (sdp
->sd_args
.ar_quota
== GFS2_QUOTA_OFF
)
512 error
= qdsb_get(sdp
, QUOTA_USER
, ip
->i_inode
.i_uid
, CREATE
, qd
);
518 error
= qdsb_get(sdp
, QUOTA_GROUP
, ip
->i_inode
.i_gid
, CREATE
, qd
);
524 if (uid
!= NO_QUOTA_CHANGE
&& uid
!= ip
->i_inode
.i_uid
) {
525 error
= qdsb_get(sdp
, QUOTA_USER
, uid
, CREATE
, qd
);
532 if (gid
!= NO_QUOTA_CHANGE
&& gid
!= ip
->i_inode
.i_gid
) {
533 error
= qdsb_get(sdp
, QUOTA_GROUP
, gid
, CREATE
, qd
);
542 gfs2_quota_unhold(ip
);
546 void gfs2_quota_unhold(struct gfs2_inode
*ip
)
548 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
549 struct gfs2_alloc
*al
= ip
->i_alloc
;
552 gfs2_assert_warn(sdp
, !test_bit(GIF_QD_LOCKED
, &ip
->i_flags
));
554 for (x
= 0; x
< al
->al_qd_num
; x
++) {
555 qdsb_put(al
->al_qd
[x
]);
561 static int sort_qd(const void *a
, const void *b
)
563 const struct gfs2_quota_data
*qd_a
= *(const struct gfs2_quota_data
**)a
;
564 const struct gfs2_quota_data
*qd_b
= *(const struct gfs2_quota_data
**)b
;
566 if (!test_bit(QDF_USER
, &qd_a
->qd_flags
) !=
567 !test_bit(QDF_USER
, &qd_b
->qd_flags
)) {
568 if (test_bit(QDF_USER
, &qd_a
->qd_flags
))
573 if (qd_a
->qd_id
< qd_b
->qd_id
)
575 if (qd_a
->qd_id
> qd_b
->qd_id
)
581 static void do_qc(struct gfs2_quota_data
*qd
, s64 change
)
583 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
584 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
585 struct gfs2_quota_change
*qc
= qd
->qd_bh_qc
;
588 mutex_lock(&sdp
->sd_quota_mutex
);
589 gfs2_trans_add_bh(ip
->i_gl
, qd
->qd_bh
, 1);
591 if (!test_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
594 if (test_bit(QDF_USER
, &qd
->qd_flags
))
595 qc
->qc_flags
= cpu_to_be32(GFS2_QCF_USER
);
596 qc
->qc_id
= cpu_to_be32(qd
->qd_id
);
599 x
= be64_to_cpu(qc
->qc_change
) + change
;
600 qc
->qc_change
= cpu_to_be64(x
);
602 spin_lock(&qd_lru_lock
);
604 spin_unlock(&qd_lru_lock
);
607 gfs2_assert_warn(sdp
, test_bit(QDF_CHANGE
, &qd
->qd_flags
));
608 clear_bit(QDF_CHANGE
, &qd
->qd_flags
);
613 } else if (!test_and_set_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
618 mutex_unlock(&sdp
->sd_quota_mutex
);
621 static void gfs2_quota_in(struct gfs2_quota_host
*qu
, const void *buf
)
623 const struct gfs2_quota
*str
= buf
;
625 qu
->qu_limit
= be64_to_cpu(str
->qu_limit
);
626 qu
->qu_warn
= be64_to_cpu(str
->qu_warn
);
627 qu
->qu_value
= be64_to_cpu(str
->qu_value
);
628 qu
->qu_ll_next
= be32_to_cpu(str
->qu_ll_next
);
631 static void gfs2_quota_out(const struct gfs2_quota_host
*qu
, void *buf
)
633 struct gfs2_quota
*str
= buf
;
635 str
->qu_limit
= cpu_to_be64(qu
->qu_limit
);
636 str
->qu_warn
= cpu_to_be64(qu
->qu_warn
);
637 str
->qu_value
= cpu_to_be64(qu
->qu_value
);
638 str
->qu_ll_next
= cpu_to_be32(qu
->qu_ll_next
);
639 memset(&str
->qu_reserved
, 0, sizeof(str
->qu_reserved
));
645 * This function was mostly borrowed from gfs2_block_truncate_page which was
646 * in turn mostly borrowed from ext3
648 static int gfs2_adjust_quota(struct gfs2_inode
*ip
, loff_t loc
,
649 s64 change
, struct gfs2_quota_data
*qd
)
651 struct inode
*inode
= &ip
->i_inode
;
652 struct address_space
*mapping
= inode
->i_mapping
;
653 unsigned long index
= loc
>> PAGE_CACHE_SHIFT
;
654 unsigned offset
= loc
& (PAGE_CACHE_SIZE
- 1);
655 unsigned blocksize
, iblock
, pos
;
656 struct buffer_head
*bh
;
660 struct gfs2_quota_host qp
;
664 if (gfs2_is_stuffed(ip
))
665 gfs2_unstuff_dinode(ip
, NULL
);
667 page
= grab_cache_page(mapping
, index
);
671 blocksize
= inode
->i_sb
->s_blocksize
;
672 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
674 if (!page_has_buffers(page
))
675 create_empty_buffers(page
, blocksize
, 0);
677 bh
= page_buffers(page
);
679 while (offset
>= pos
) {
680 bh
= bh
->b_this_page
;
685 if (!buffer_mapped(bh
)) {
686 gfs2_block_map(inode
, iblock
, bh
, 1);
687 if (!buffer_mapped(bh
))
691 if (PageUptodate(page
))
692 set_buffer_uptodate(bh
);
694 if (!buffer_uptodate(bh
)) {
695 ll_rw_block(READ_META
, 1, &bh
);
697 if (!buffer_uptodate(bh
))
701 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
703 kaddr
= kmap_atomic(page
, KM_USER0
);
704 ptr
= kaddr
+ offset
;
705 gfs2_quota_in(&qp
, ptr
);
706 qp
.qu_value
+= change
;
708 gfs2_quota_out(&qp
, ptr
);
709 flush_dcache_page(page
);
710 kunmap_atomic(kaddr
, KM_USER0
);
712 qd
->qd_qb
.qb_magic
= cpu_to_be32(GFS2_MAGIC
);
713 qd
->qd_qb
.qb_value
= cpu_to_be64(value
);
714 ((struct gfs2_quota_lvb
*)(qd
->qd_gl
->gl_lvb
))->qb_magic
= cpu_to_be32(GFS2_MAGIC
);
715 ((struct gfs2_quota_lvb
*)(qd
->qd_gl
->gl_lvb
))->qb_value
= cpu_to_be64(value
);
718 page_cache_release(page
);
722 static int do_sync(unsigned int num_qd
, struct gfs2_quota_data
**qda
)
724 struct gfs2_sbd
*sdp
= (*qda
)->qd_gl
->gl_sbd
;
725 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_quota_inode
);
726 unsigned int data_blocks
, ind_blocks
;
727 struct gfs2_holder
*ghs
, i_gh
;
729 struct gfs2_quota_data
*qd
;
731 unsigned int nalloc
= 0, blocks
;
732 struct gfs2_alloc
*al
= NULL
;
735 gfs2_write_calc_reserv(ip
, sizeof(struct gfs2_quota
),
736 &data_blocks
, &ind_blocks
);
738 ghs
= kcalloc(num_qd
, sizeof(struct gfs2_holder
), GFP_NOFS
);
742 sort(qda
, num_qd
, sizeof(struct gfs2_quota_data
*), sort_qd
, NULL
);
743 for (qx
= 0; qx
< num_qd
; qx
++) {
744 error
= gfs2_glock_nq_init(qda
[qx
]->qd_gl
,
746 GL_NOCACHE
, &ghs
[qx
]);
751 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
755 for (x
= 0; x
< num_qd
; x
++) {
758 offset
= qd2offset(qda
[x
]);
759 error
= gfs2_write_alloc_required(ip
, offset
,
760 sizeof(struct gfs2_quota
),
768 al
= gfs2_alloc_get(ip
);
774 * 1 blk for unstuffing inode if stuffed. We add this extra
775 * block to the reservation unconditionally. If the inode
776 * doesn't need unstuffing, the block will be released to the
777 * rgrp since it won't be allocated during the transaction
779 al
->al_requested
= 1;
780 /* +1 in the end for block requested above for unstuffing */
781 blocks
= num_qd
* data_blocks
+ RES_DINODE
+ num_qd
+ 1;
784 al
->al_requested
+= nalloc
* (data_blocks
+ ind_blocks
);
785 error
= gfs2_inplace_reserve(ip
);
790 blocks
+= al
->al_rgd
->rd_length
+ nalloc
* ind_blocks
+ RES_STATFS
;
792 error
= gfs2_trans_begin(sdp
, blocks
, 0);
796 for (x
= 0; x
< num_qd
; x
++) {
798 offset
= qd2offset(qd
);
799 error
= gfs2_adjust_quota(ip
, offset
, qd
->qd_change_sync
,
800 (struct gfs2_quota_data
*)
805 do_qc(qd
, -qd
->qd_change_sync
);
813 gfs2_inplace_release(ip
);
817 gfs2_glock_dq_uninit(&i_gh
);
820 gfs2_glock_dq_uninit(&ghs
[qx
]);
822 gfs2_log_flush(ip
->i_gl
->gl_sbd
, ip
->i_gl
);
826 static int do_glock(struct gfs2_quota_data
*qd
, int force_refresh
,
827 struct gfs2_holder
*q_gh
)
829 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
830 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_quota_inode
);
831 struct gfs2_holder i_gh
;
832 struct gfs2_quota_host q
;
833 char buf
[sizeof(struct gfs2_quota
)];
835 struct gfs2_quota_lvb
*qlvb
;
838 error
= gfs2_glock_nq_init(qd
->qd_gl
, LM_ST_SHARED
, 0, q_gh
);
842 qd
->qd_qb
= *(struct gfs2_quota_lvb
*)qd
->qd_gl
->gl_lvb
;
844 if (force_refresh
|| qd
->qd_qb
.qb_magic
!= cpu_to_be32(GFS2_MAGIC
)) {
846 gfs2_glock_dq_uninit(q_gh
);
847 error
= gfs2_glock_nq_init(qd
->qd_gl
,
848 LM_ST_EXCLUSIVE
, GL_NOCACHE
,
853 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &i_gh
);
857 memset(buf
, 0, sizeof(struct gfs2_quota
));
859 error
= gfs2_internal_read(ip
, NULL
, buf
, &pos
,
860 sizeof(struct gfs2_quota
));
864 gfs2_glock_dq_uninit(&i_gh
);
866 gfs2_quota_in(&q
, buf
);
867 qlvb
= (struct gfs2_quota_lvb
*)qd
->qd_gl
->gl_lvb
;
868 qlvb
->qb_magic
= cpu_to_be32(GFS2_MAGIC
);
870 qlvb
->qb_limit
= cpu_to_be64(q
.qu_limit
);
871 qlvb
->qb_warn
= cpu_to_be64(q
.qu_warn
);
872 qlvb
->qb_value
= cpu_to_be64(q
.qu_value
);
875 if (gfs2_glock_is_blocking(qd
->qd_gl
)) {
876 gfs2_glock_dq_uninit(q_gh
);
885 gfs2_glock_dq_uninit(&i_gh
);
887 gfs2_glock_dq_uninit(q_gh
);
891 int gfs2_quota_lock(struct gfs2_inode
*ip
, u32 uid
, u32 gid
)
893 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
894 struct gfs2_alloc
*al
= ip
->i_alloc
;
898 gfs2_quota_hold(ip
, uid
, gid
);
900 if (capable(CAP_SYS_RESOURCE
) ||
901 sdp
->sd_args
.ar_quota
!= GFS2_QUOTA_ON
)
904 sort(al
->al_qd
, al
->al_qd_num
, sizeof(struct gfs2_quota_data
*),
907 for (x
= 0; x
< al
->al_qd_num
; x
++) {
908 error
= do_glock(al
->al_qd
[x
], NO_FORCE
, &al
->al_qd_ghs
[x
]);
914 set_bit(GIF_QD_LOCKED
, &ip
->i_flags
);
917 gfs2_glock_dq_uninit(&al
->al_qd_ghs
[x
]);
918 gfs2_quota_unhold(ip
);
924 static int need_sync(struct gfs2_quota_data
*qd
)
926 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
927 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
929 unsigned int num
, den
;
932 if (!qd
->qd_qb
.qb_limit
)
935 spin_lock(&qd_lru_lock
);
936 value
= qd
->qd_change
;
937 spin_unlock(&qd_lru_lock
);
939 spin_lock(>
->gt_spin
);
940 num
= gt
->gt_quota_scale_num
;
941 den
= gt
->gt_quota_scale_den
;
942 spin_unlock(>
->gt_spin
);
946 else if ((s64
)be64_to_cpu(qd
->qd_qb
.qb_value
) >=
947 (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
))
950 value
*= gfs2_jindex_size(sdp
) * num
;
951 value
= div_s64(value
, den
);
952 value
+= (s64
)be64_to_cpu(qd
->qd_qb
.qb_value
);
953 if (value
< (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
))
960 void gfs2_quota_unlock(struct gfs2_inode
*ip
)
962 struct gfs2_alloc
*al
= ip
->i_alloc
;
963 struct gfs2_quota_data
*qda
[4];
964 unsigned int count
= 0;
967 if (!test_and_clear_bit(GIF_QD_LOCKED
, &ip
->i_flags
))
970 for (x
= 0; x
< al
->al_qd_num
; x
++) {
971 struct gfs2_quota_data
*qd
;
975 sync
= need_sync(qd
);
977 gfs2_glock_dq_uninit(&al
->al_qd_ghs
[x
]);
979 if (sync
&& qd_trylock(qd
))
985 for (x
= 0; x
< count
; x
++)
990 gfs2_quota_unhold(ip
);
995 static int print_message(struct gfs2_quota_data
*qd
, char *type
)
997 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
999 printk(KERN_INFO
"GFS2: fsid=%s: quota %s for %s %u\r\n",
1000 sdp
->sd_fsname
, type
,
1001 (test_bit(QDF_USER
, &qd
->qd_flags
)) ? "user" : "group",
1007 int gfs2_quota_check(struct gfs2_inode
*ip
, u32 uid
, u32 gid
)
1009 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1010 struct gfs2_alloc
*al
= ip
->i_alloc
;
1011 struct gfs2_quota_data
*qd
;
1016 if (!test_bit(GIF_QD_LOCKED
, &ip
->i_flags
))
1019 if (sdp
->sd_args
.ar_quota
!= GFS2_QUOTA_ON
)
1022 for (x
= 0; x
< al
->al_qd_num
; x
++) {
1025 if (!((qd
->qd_id
== uid
&& test_bit(QDF_USER
, &qd
->qd_flags
)) ||
1026 (qd
->qd_id
== gid
&& !test_bit(QDF_USER
, &qd
->qd_flags
))))
1029 value
= (s64
)be64_to_cpu(qd
->qd_qb
.qb_value
);
1030 spin_lock(&qd_lru_lock
);
1031 value
+= qd
->qd_change
;
1032 spin_unlock(&qd_lru_lock
);
1034 if (be64_to_cpu(qd
->qd_qb
.qb_limit
) && (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
) < value
) {
1035 print_message(qd
, "exceeded");
1038 } else if (be64_to_cpu(qd
->qd_qb
.qb_warn
) &&
1039 (s64
)be64_to_cpu(qd
->qd_qb
.qb_warn
) < value
&&
1040 time_after_eq(jiffies
, qd
->qd_last_warn
+
1042 gt_quota_warn_period
) * HZ
)) {
1043 error
= print_message(qd
, "warning");
1044 qd
->qd_last_warn
= jiffies
;
1051 void gfs2_quota_change(struct gfs2_inode
*ip
, s64 change
,
1054 struct gfs2_alloc
*al
= ip
->i_alloc
;
1055 struct gfs2_quota_data
*qd
;
1058 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), change
))
1060 if (ip
->i_diskflags
& GFS2_DIF_SYSTEM
)
1063 for (x
= 0; x
< al
->al_qd_num
; x
++) {
1066 if ((qd
->qd_id
== uid
&& test_bit(QDF_USER
, &qd
->qd_flags
)) ||
1067 (qd
->qd_id
== gid
&& !test_bit(QDF_USER
, &qd
->qd_flags
))) {
1073 int gfs2_quota_sync(struct gfs2_sbd
*sdp
)
1075 struct gfs2_quota_data
**qda
;
1076 unsigned int max_qd
= gfs2_tune_get(sdp
, gt_quota_simul_sync
);
1077 unsigned int num_qd
;
1081 sdp
->sd_quota_sync_gen
++;
1083 qda
= kcalloc(max_qd
, sizeof(struct gfs2_quota_data
*), GFP_KERNEL
);
1091 error
= qd_fish(sdp
, qda
+ num_qd
);
1092 if (error
|| !qda
[num_qd
])
1094 if (++num_qd
== max_qd
)
1100 error
= do_sync(num_qd
, qda
);
1102 for (x
= 0; x
< num_qd
; x
++)
1103 qda
[x
]->qd_sync_gen
=
1104 sdp
->sd_quota_sync_gen
;
1106 for (x
= 0; x
< num_qd
; x
++)
1109 } while (!error
&& num_qd
== max_qd
);
1116 int gfs2_quota_refresh(struct gfs2_sbd
*sdp
, int user
, u32 id
)
1118 struct gfs2_quota_data
*qd
;
1119 struct gfs2_holder q_gh
;
1122 error
= qd_get(sdp
, user
, id
, CREATE
, &qd
);
1126 error
= do_glock(qd
, FORCE
, &q_gh
);
1128 gfs2_glock_dq_uninit(&q_gh
);
1135 static void gfs2_quota_change_in(struct gfs2_quota_change_host
*qc
, const void *buf
)
1137 const struct gfs2_quota_change
*str
= buf
;
1139 qc
->qc_change
= be64_to_cpu(str
->qc_change
);
1140 qc
->qc_flags
= be32_to_cpu(str
->qc_flags
);
1141 qc
->qc_id
= be32_to_cpu(str
->qc_id
);
1144 int gfs2_quota_init(struct gfs2_sbd
*sdp
)
1146 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
1147 unsigned int blocks
= ip
->i_disksize
>> sdp
->sd_sb
.sb_bsize_shift
;
1148 unsigned int x
, slot
= 0;
1149 unsigned int found
= 0;
1154 if (!ip
->i_disksize
|| ip
->i_disksize
> (64 << 20) ||
1155 ip
->i_disksize
& (sdp
->sd_sb
.sb_bsize
- 1)) {
1156 gfs2_consist_inode(ip
);
1159 sdp
->sd_quota_slots
= blocks
* sdp
->sd_qc_per_block
;
1160 sdp
->sd_quota_chunks
= DIV_ROUND_UP(sdp
->sd_quota_slots
, 8 * PAGE_SIZE
);
1164 sdp
->sd_quota_bitmap
= kcalloc(sdp
->sd_quota_chunks
,
1165 sizeof(unsigned char *), GFP_NOFS
);
1166 if (!sdp
->sd_quota_bitmap
)
1169 for (x
= 0; x
< sdp
->sd_quota_chunks
; x
++) {
1170 sdp
->sd_quota_bitmap
[x
] = kzalloc(PAGE_SIZE
, GFP_NOFS
);
1171 if (!sdp
->sd_quota_bitmap
[x
])
1175 for (x
= 0; x
< blocks
; x
++) {
1176 struct buffer_head
*bh
;
1181 error
= gfs2_extent_map(&ip
->i_inode
, x
, &new, &dblock
, &extlen
);
1186 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
1189 if (gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_QC
)) {
1194 for (y
= 0; y
< sdp
->sd_qc_per_block
&& slot
< sdp
->sd_quota_slots
;
1196 struct gfs2_quota_change_host qc
;
1197 struct gfs2_quota_data
*qd
;
1199 gfs2_quota_change_in(&qc
, bh
->b_data
+
1200 sizeof(struct gfs2_meta_header
) +
1201 y
* sizeof(struct gfs2_quota_change
));
1205 error
= qd_alloc(sdp
, (qc
.qc_flags
& GFS2_QCF_USER
),
1212 set_bit(QDF_CHANGE
, &qd
->qd_flags
);
1213 qd
->qd_change
= qc
.qc_change
;
1215 qd
->qd_slot_count
= 1;
1217 spin_lock(&qd_lru_lock
);
1218 gfs2_icbit_munge(sdp
, sdp
->sd_quota_bitmap
, slot
, 1);
1219 list_add(&qd
->qd_list
, &sdp
->sd_quota_list
);
1220 atomic_inc(&sdp
->sd_quota_count
);
1221 spin_unlock(&qd_lru_lock
);
1232 fs_info(sdp
, "found %u quota changes\n", found
);
1237 gfs2_quota_cleanup(sdp
);
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(&qd_lru_lock
);
1248 while (!list_empty(head
)) {
1249 qd
= list_entry(head
->prev
, struct gfs2_quota_data
, qd_list
);
1251 if (atomic_read(&qd
->qd_count
) > 1 ||
1252 (atomic_read(&qd
->qd_count
) &&
1253 !test_bit(QDF_CHANGE
, &qd
->qd_flags
))) {
1254 list_move(&qd
->qd_list
, head
);
1255 spin_unlock(&qd_lru_lock
);
1257 spin_lock(&qd_lru_lock
);
1261 list_del(&qd
->qd_list
);
1262 /* Also remove if this qd exists in the reclaim list */
1263 if (!list_empty(&qd
->qd_reclaim
)) {
1264 list_del_init(&qd
->qd_reclaim
);
1265 atomic_dec(&qd_lru_count
);
1267 atomic_dec(&sdp
->sd_quota_count
);
1268 spin_unlock(&qd_lru_lock
);
1270 if (!atomic_read(&qd
->qd_count
)) {
1271 gfs2_assert_warn(sdp
, !qd
->qd_change
);
1272 gfs2_assert_warn(sdp
, !qd
->qd_slot_count
);
1274 gfs2_assert_warn(sdp
, qd
->qd_slot_count
== 1);
1275 gfs2_assert_warn(sdp
, !qd
->qd_bh_count
);
1277 gfs2_glock_put(qd
->qd_gl
);
1278 kmem_cache_free(gfs2_quotad_cachep
, qd
);
1280 spin_lock(&qd_lru_lock
);
1282 spin_unlock(&qd_lru_lock
);
1284 gfs2_assert_warn(sdp
, !atomic_read(&sdp
->sd_quota_count
));
1286 if (sdp
->sd_quota_bitmap
) {
1287 for (x
= 0; x
< sdp
->sd_quota_chunks
; x
++)
1288 kfree(sdp
->sd_quota_bitmap
[x
]);
1289 kfree(sdp
->sd_quota_bitmap
);
1293 static void quotad_error(struct gfs2_sbd
*sdp
, const char *msg
, int error
)
1295 if (error
== 0 || error
== -EROFS
)
1297 if (!test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))
1298 fs_err(sdp
, "gfs2_quotad: %s error %d\n", msg
, error
);
1301 static void quotad_check_timeo(struct gfs2_sbd
*sdp
, const char *msg
,
1302 int (*fxn
)(struct gfs2_sbd
*sdp
),
1303 unsigned long t
, unsigned long *timeo
,
1304 unsigned int *new_timeo
)
1307 int error
= fxn(sdp
);
1308 quotad_error(sdp
, msg
, error
);
1309 *timeo
= gfs2_tune_get_i(&sdp
->sd_tune
, new_timeo
) * HZ
;
1315 static void quotad_check_trunc_list(struct gfs2_sbd
*sdp
)
1317 struct gfs2_inode
*ip
;
1321 spin_lock(&sdp
->sd_trunc_lock
);
1322 if (!list_empty(&sdp
->sd_trunc_list
)) {
1323 ip
= list_entry(sdp
->sd_trunc_list
.next
,
1324 struct gfs2_inode
, i_trunc_list
);
1325 list_del_init(&ip
->i_trunc_list
);
1327 spin_unlock(&sdp
->sd_trunc_lock
);
1330 gfs2_glock_finish_truncate(ip
);
1335 * gfs2_quotad - Write cached quota changes into the quota file
1336 * @sdp: Pointer to GFS2 superblock
1340 int gfs2_quotad(void *data
)
1342 struct gfs2_sbd
*sdp
= data
;
1343 struct gfs2_tune
*tune
= &sdp
->sd_tune
;
1344 unsigned long statfs_timeo
= 0;
1345 unsigned long quotad_timeo
= 0;
1346 unsigned long t
= 0;
1350 while (!kthread_should_stop()) {
1352 /* Update the master statfs file */
1353 quotad_check_timeo(sdp
, "statfs", gfs2_statfs_sync
, t
,
1354 &statfs_timeo
, &tune
->gt_statfs_quantum
);
1356 /* Update quota file */
1357 quotad_check_timeo(sdp
, "sync", gfs2_quota_sync
, t
,
1358 "ad_timeo
, &tune
->gt_quota_quantum
);
1360 /* Check for & recover partially truncated inodes */
1361 quotad_check_trunc_list(sdp
);
1363 if (freezing(current
))
1365 t
= min(quotad_timeo
, statfs_timeo
);
1367 prepare_to_wait(&sdp
->sd_quota_wait
, &wait
, TASK_INTERRUPTIBLE
);
1368 spin_lock(&sdp
->sd_trunc_lock
);
1369 empty
= list_empty(&sdp
->sd_trunc_list
);
1370 spin_unlock(&sdp
->sd_trunc_lock
);
1372 t
-= schedule_timeout(t
);
1375 finish_wait(&sdp
->sd_quota_wait
, &wait
);