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 for 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>
68 struct gfs2_quota_change_host
{
70 u32 qc_flags
; /* GFS2_QCF_... */
74 static LIST_HEAD(qd_lru_list
);
75 static atomic_t qd_lru_count
= ATOMIC_INIT(0);
76 static DEFINE_SPINLOCK(qd_lru_lock
);
78 int gfs2_shrink_qd_memory(int nr
, gfp_t gfp_mask
)
80 struct gfs2_quota_data
*qd
;
86 if (!(gfp_mask
& __GFP_FS
))
89 spin_lock(&qd_lru_lock
);
90 while (nr
&& !list_empty(&qd_lru_list
)) {
91 qd
= list_entry(qd_lru_list
.next
,
92 struct gfs2_quota_data
, qd_reclaim
);
93 sdp
= qd
->qd_gl
->gl_sbd
;
95 /* Free from the filesystem-specific list */
96 list_del(&qd
->qd_list
);
98 gfs2_assert_warn(sdp
, !qd
->qd_change
);
99 gfs2_assert_warn(sdp
, !qd
->qd_slot_count
);
100 gfs2_assert_warn(sdp
, !qd
->qd_bh_count
);
102 gfs2_glock_put(qd
->qd_gl
);
103 atomic_dec(&sdp
->sd_quota_count
);
105 /* Delete it from the common reclaim list */
106 list_del_init(&qd
->qd_reclaim
);
107 atomic_dec(&qd_lru_count
);
108 spin_unlock(&qd_lru_lock
);
109 kmem_cache_free(gfs2_quotad_cachep
, qd
);
110 spin_lock(&qd_lru_lock
);
113 spin_unlock(&qd_lru_lock
);
116 return (atomic_read(&qd_lru_count
) * sysctl_vfs_cache_pressure
) / 100;
119 static u64
qd2offset(struct gfs2_quota_data
*qd
)
123 offset
= 2 * (u64
)qd
->qd_id
+ !test_bit(QDF_USER
, &qd
->qd_flags
);
124 offset
*= sizeof(struct gfs2_quota
);
129 static int qd_alloc(struct gfs2_sbd
*sdp
, int user
, u32 id
,
130 struct gfs2_quota_data
**qdp
)
132 struct gfs2_quota_data
*qd
;
135 qd
= kmem_cache_zalloc(gfs2_quotad_cachep
, GFP_NOFS
);
139 atomic_set(&qd
->qd_count
, 1);
142 set_bit(QDF_USER
, &qd
->qd_flags
);
144 INIT_LIST_HEAD(&qd
->qd_reclaim
);
146 error
= gfs2_glock_get(sdp
, 2 * (u64
)id
+ !user
,
147 &gfs2_quota_glops
, CREATE
, &qd
->qd_gl
);
156 kmem_cache_free(gfs2_quotad_cachep
, qd
);
160 static int qd_get(struct gfs2_sbd
*sdp
, int user
, u32 id
, int create
,
161 struct gfs2_quota_data
**qdp
)
163 struct gfs2_quota_data
*qd
= NULL
, *new_qd
= NULL
;
170 spin_lock(&qd_lru_lock
);
171 list_for_each_entry(qd
, &sdp
->sd_quota_list
, qd_list
) {
172 if (qd
->qd_id
== id
&&
173 !test_bit(QDF_USER
, &qd
->qd_flags
) == !user
) {
174 if (!atomic_read(&qd
->qd_count
) &&
175 !list_empty(&qd
->qd_reclaim
)) {
176 /* Remove it from reclaim list */
177 list_del_init(&qd
->qd_reclaim
);
178 atomic_dec(&qd_lru_count
);
180 atomic_inc(&qd
->qd_count
);
191 list_add(&qd
->qd_list
, &sdp
->sd_quota_list
);
192 atomic_inc(&sdp
->sd_quota_count
);
196 spin_unlock(&qd_lru_lock
);
200 gfs2_glock_put(new_qd
->qd_gl
);
201 kmem_cache_free(gfs2_quotad_cachep
, new_qd
);
207 error
= qd_alloc(sdp
, user
, id
, &new_qd
);
213 static void qd_hold(struct gfs2_quota_data
*qd
)
215 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
216 gfs2_assert(sdp
, atomic_read(&qd
->qd_count
));
217 atomic_inc(&qd
->qd_count
);
220 static void qd_put(struct gfs2_quota_data
*qd
)
222 if (atomic_dec_and_lock(&qd
->qd_count
, &qd_lru_lock
)) {
223 /* Add to the reclaim list */
224 list_add_tail(&qd
->qd_reclaim
, &qd_lru_list
);
225 atomic_inc(&qd_lru_count
);
226 spin_unlock(&qd_lru_lock
);
230 static int slot_get(struct gfs2_quota_data
*qd
)
232 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
233 unsigned int c
, o
= 0, b
;
234 unsigned char byte
= 0;
236 spin_lock(&qd_lru_lock
);
238 if (qd
->qd_slot_count
++) {
239 spin_unlock(&qd_lru_lock
);
243 for (c
= 0; c
< sdp
->sd_quota_chunks
; c
++)
244 for (o
= 0; o
< PAGE_SIZE
; o
++) {
245 byte
= sdp
->sd_quota_bitmap
[c
][o
];
253 for (b
= 0; b
< 8; b
++)
254 if (!(byte
& (1 << b
)))
256 qd
->qd_slot
= c
* (8 * PAGE_SIZE
) + o
* 8 + b
;
258 if (qd
->qd_slot
>= sdp
->sd_quota_slots
)
261 sdp
->sd_quota_bitmap
[c
][o
] |= 1 << b
;
263 spin_unlock(&qd_lru_lock
);
269 spin_unlock(&qd_lru_lock
);
273 static void slot_hold(struct gfs2_quota_data
*qd
)
275 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
277 spin_lock(&qd_lru_lock
);
278 gfs2_assert(sdp
, qd
->qd_slot_count
);
280 spin_unlock(&qd_lru_lock
);
283 static void slot_put(struct gfs2_quota_data
*qd
)
285 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
287 spin_lock(&qd_lru_lock
);
288 gfs2_assert(sdp
, qd
->qd_slot_count
);
289 if (!--qd
->qd_slot_count
) {
290 gfs2_icbit_munge(sdp
, sdp
->sd_quota_bitmap
, qd
->qd_slot
, 0);
293 spin_unlock(&qd_lru_lock
);
296 static int bh_get(struct gfs2_quota_data
*qd
)
298 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
299 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
300 unsigned int block
, offset
;
301 struct buffer_head
*bh
;
303 struct buffer_head bh_map
= { .b_state
= 0, .b_blocknr
= 0 };
305 mutex_lock(&sdp
->sd_quota_mutex
);
307 if (qd
->qd_bh_count
++) {
308 mutex_unlock(&sdp
->sd_quota_mutex
);
312 block
= qd
->qd_slot
/ sdp
->sd_qc_per_block
;
313 offset
= qd
->qd_slot
% sdp
->sd_qc_per_block
;
315 bh_map
.b_size
= 1 << ip
->i_inode
.i_blkbits
;
316 error
= gfs2_block_map(&ip
->i_inode
, block
, &bh_map
, 0);
319 error
= gfs2_meta_read(ip
->i_gl
, bh_map
.b_blocknr
, DIO_WAIT
, &bh
);
323 if (gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_QC
))
327 qd
->qd_bh_qc
= (struct gfs2_quota_change
*)
328 (bh
->b_data
+ sizeof(struct gfs2_meta_header
) +
329 offset
* sizeof(struct gfs2_quota_change
));
331 mutex_unlock(&sdp
->sd_quota_mutex
);
339 mutex_unlock(&sdp
->sd_quota_mutex
);
343 static void bh_put(struct gfs2_quota_data
*qd
)
345 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
347 mutex_lock(&sdp
->sd_quota_mutex
);
348 gfs2_assert(sdp
, qd
->qd_bh_count
);
349 if (!--qd
->qd_bh_count
) {
354 mutex_unlock(&sdp
->sd_quota_mutex
);
357 static int qd_fish(struct gfs2_sbd
*sdp
, struct gfs2_quota_data
**qdp
)
359 struct gfs2_quota_data
*qd
= NULL
;
365 if (sdp
->sd_vfs
->s_flags
& MS_RDONLY
)
368 spin_lock(&qd_lru_lock
);
370 list_for_each_entry(qd
, &sdp
->sd_quota_list
, qd_list
) {
371 if (test_bit(QDF_LOCKED
, &qd
->qd_flags
) ||
372 !test_bit(QDF_CHANGE
, &qd
->qd_flags
) ||
373 qd
->qd_sync_gen
>= sdp
->sd_quota_sync_gen
)
376 list_move_tail(&qd
->qd_list
, &sdp
->sd_quota_list
);
378 set_bit(QDF_LOCKED
, &qd
->qd_flags
);
379 gfs2_assert_warn(sdp
, atomic_read(&qd
->qd_count
));
380 atomic_inc(&qd
->qd_count
);
381 qd
->qd_change_sync
= qd
->qd_change
;
382 gfs2_assert_warn(sdp
, qd
->qd_slot_count
);
392 spin_unlock(&qd_lru_lock
);
395 gfs2_assert_warn(sdp
, qd
->qd_change_sync
);
398 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
410 static int qd_trylock(struct gfs2_quota_data
*qd
)
412 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
414 if (sdp
->sd_vfs
->s_flags
& MS_RDONLY
)
417 spin_lock(&qd_lru_lock
);
419 if (test_bit(QDF_LOCKED
, &qd
->qd_flags
) ||
420 !test_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
421 spin_unlock(&qd_lru_lock
);
425 list_move_tail(&qd
->qd_list
, &sdp
->sd_quota_list
);
427 set_bit(QDF_LOCKED
, &qd
->qd_flags
);
428 gfs2_assert_warn(sdp
, atomic_read(&qd
->qd_count
));
429 atomic_inc(&qd
->qd_count
);
430 qd
->qd_change_sync
= qd
->qd_change
;
431 gfs2_assert_warn(sdp
, qd
->qd_slot_count
);
434 spin_unlock(&qd_lru_lock
);
436 gfs2_assert_warn(sdp
, qd
->qd_change_sync
);
438 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
447 static void qd_unlock(struct gfs2_quota_data
*qd
)
449 gfs2_assert_warn(qd
->qd_gl
->gl_sbd
,
450 test_bit(QDF_LOCKED
, &qd
->qd_flags
));
451 clear_bit(QDF_LOCKED
, &qd
->qd_flags
);
457 static int qdsb_get(struct gfs2_sbd
*sdp
, int user
, u32 id
, int create
,
458 struct gfs2_quota_data
**qdp
)
462 error
= qd_get(sdp
, user
, id
, create
, qdp
);
466 error
= slot_get(*qdp
);
470 error
= bh_get(*qdp
);
483 static void qdsb_put(struct gfs2_quota_data
*qd
)
490 int gfs2_quota_hold(struct gfs2_inode
*ip
, u32 uid
, u32 gid
)
492 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
493 struct gfs2_alloc
*al
= ip
->i_alloc
;
494 struct gfs2_quota_data
**qd
= al
->al_qd
;
497 if (gfs2_assert_warn(sdp
, !al
->al_qd_num
) ||
498 gfs2_assert_warn(sdp
, !test_bit(GIF_QD_LOCKED
, &ip
->i_flags
)))
501 if (sdp
->sd_args
.ar_quota
== GFS2_QUOTA_OFF
)
504 error
= qdsb_get(sdp
, QUOTA_USER
, ip
->i_inode
.i_uid
, CREATE
, qd
);
510 error
= qdsb_get(sdp
, QUOTA_GROUP
, ip
->i_inode
.i_gid
, CREATE
, qd
);
516 if (uid
!= NO_QUOTA_CHANGE
&& uid
!= ip
->i_inode
.i_uid
) {
517 error
= qdsb_get(sdp
, QUOTA_USER
, uid
, CREATE
, qd
);
524 if (gid
!= NO_QUOTA_CHANGE
&& gid
!= ip
->i_inode
.i_gid
) {
525 error
= qdsb_get(sdp
, QUOTA_GROUP
, gid
, CREATE
, qd
);
534 gfs2_quota_unhold(ip
);
538 void gfs2_quota_unhold(struct gfs2_inode
*ip
)
540 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
541 struct gfs2_alloc
*al
= ip
->i_alloc
;
544 gfs2_assert_warn(sdp
, !test_bit(GIF_QD_LOCKED
, &ip
->i_flags
));
546 for (x
= 0; x
< al
->al_qd_num
; x
++) {
547 qdsb_put(al
->al_qd
[x
]);
553 static int sort_qd(const void *a
, const void *b
)
555 const struct gfs2_quota_data
*qd_a
= *(const struct gfs2_quota_data
**)a
;
556 const struct gfs2_quota_data
*qd_b
= *(const struct gfs2_quota_data
**)b
;
558 if (!test_bit(QDF_USER
, &qd_a
->qd_flags
) !=
559 !test_bit(QDF_USER
, &qd_b
->qd_flags
)) {
560 if (test_bit(QDF_USER
, &qd_a
->qd_flags
))
565 if (qd_a
->qd_id
< qd_b
->qd_id
)
567 if (qd_a
->qd_id
> qd_b
->qd_id
)
573 static void do_qc(struct gfs2_quota_data
*qd
, s64 change
)
575 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
576 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
577 struct gfs2_quota_change
*qc
= qd
->qd_bh_qc
;
580 mutex_lock(&sdp
->sd_quota_mutex
);
581 gfs2_trans_add_bh(ip
->i_gl
, qd
->qd_bh
, 1);
583 if (!test_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
586 if (test_bit(QDF_USER
, &qd
->qd_flags
))
587 qc
->qc_flags
= cpu_to_be32(GFS2_QCF_USER
);
588 qc
->qc_id
= cpu_to_be32(qd
->qd_id
);
591 x
= be64_to_cpu(qc
->qc_change
) + change
;
592 qc
->qc_change
= cpu_to_be64(x
);
594 spin_lock(&qd_lru_lock
);
596 spin_unlock(&qd_lru_lock
);
599 gfs2_assert_warn(sdp
, test_bit(QDF_CHANGE
, &qd
->qd_flags
));
600 clear_bit(QDF_CHANGE
, &qd
->qd_flags
);
605 } else if (!test_and_set_bit(QDF_CHANGE
, &qd
->qd_flags
)) {
610 mutex_unlock(&sdp
->sd_quota_mutex
);
614 * gfs2_adjust_quota - adjust record of current block usage
615 * @ip: The quota inode
616 * @loc: Offset of the entry in the quota file
617 * @change: The amount of change to record
618 * @qd: The quota data
620 * This function was mostly borrowed from gfs2_block_truncate_page which was
621 * in turn mostly borrowed from ext3
623 * Returns: 0 or -ve on error
626 static int gfs2_adjust_quota(struct gfs2_inode
*ip
, loff_t loc
,
627 s64 change
, struct gfs2_quota_data
*qd
)
629 struct inode
*inode
= &ip
->i_inode
;
630 struct address_space
*mapping
= inode
->i_mapping
;
631 unsigned long index
= loc
>> PAGE_CACHE_SHIFT
;
632 unsigned offset
= loc
& (PAGE_CACHE_SIZE
- 1);
633 unsigned blocksize
, iblock
, pos
;
634 struct buffer_head
*bh
;
637 struct gfs2_quota q
, *qp
;
640 if (gfs2_is_stuffed(ip
))
641 gfs2_unstuff_dinode(ip
, NULL
);
643 memset(&q
, 0, sizeof(struct gfs2_quota
));
644 err
= gfs2_internal_read(ip
, NULL
, (char *)&q
, &loc
, sizeof(q
));
650 qp
->qu_value
= be64_to_cpu(qp
->qu_value
);
651 qp
->qu_value
+= change
;
652 qp
->qu_value
= cpu_to_be64(qp
->qu_value
);
653 qd
->qd_qb
.qb_value
= qp
->qu_value
;
655 /* Write the quota into the quota file on disk */
657 nbytes
= sizeof(struct gfs2_quota
);
659 page
= grab_cache_page(mapping
, index
);
663 blocksize
= inode
->i_sb
->s_blocksize
;
664 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
666 if (!page_has_buffers(page
))
667 create_empty_buffers(page
, blocksize
, 0);
669 bh
= page_buffers(page
);
671 while (offset
>= pos
) {
672 bh
= bh
->b_this_page
;
677 if (!buffer_mapped(bh
)) {
678 gfs2_block_map(inode
, iblock
, bh
, 1);
679 if (!buffer_mapped(bh
))
681 /* If it's a newly allocated disk block for quota, zero it */
683 zero_user(page
, pos
- blocksize
, bh
->b_size
);
686 if (PageUptodate(page
))
687 set_buffer_uptodate(bh
);
689 if (!buffer_uptodate(bh
)) {
690 ll_rw_block(READ_META
, 1, &bh
);
692 if (!buffer_uptodate(bh
))
696 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
698 kaddr
= kmap_atomic(page
, KM_USER0
);
699 if (offset
+ sizeof(struct gfs2_quota
) > PAGE_CACHE_SIZE
)
700 nbytes
= PAGE_CACHE_SIZE
- offset
;
701 memcpy(kaddr
+ offset
, ptr
, nbytes
);
702 flush_dcache_page(page
);
703 kunmap_atomic(kaddr
, KM_USER0
);
705 page_cache_release(page
);
707 /* If quota straddles page boundary, we need to update the rest of the
708 * quota at the beginning of the next page */
709 if ((offset
+ sizeof(struct gfs2_quota
)) > PAGE_CACHE_SIZE
) {
711 nbytes
= sizeof(struct gfs2_quota
) - nbytes
;
720 page_cache_release(page
);
724 static int do_sync(unsigned int num_qd
, struct gfs2_quota_data
**qda
)
726 struct gfs2_sbd
*sdp
= (*qda
)->qd_gl
->gl_sbd
;
727 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_quota_inode
);
728 unsigned int data_blocks
, ind_blocks
;
729 struct gfs2_holder
*ghs
, i_gh
;
731 struct gfs2_quota_data
*qd
;
733 unsigned int nalloc
= 0, blocks
;
734 struct gfs2_alloc
*al
= NULL
;
737 gfs2_write_calc_reserv(ip
, sizeof(struct gfs2_quota
),
738 &data_blocks
, &ind_blocks
);
740 ghs
= kcalloc(num_qd
, sizeof(struct gfs2_holder
), GFP_NOFS
);
744 sort(qda
, num_qd
, sizeof(struct gfs2_quota_data
*), sort_qd
, NULL
);
745 for (qx
= 0; qx
< num_qd
; qx
++) {
746 error
= gfs2_glock_nq_init(qda
[qx
]->qd_gl
, LM_ST_EXCLUSIVE
,
747 GL_NOCACHE
, &ghs
[qx
]);
752 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
756 for (x
= 0; x
< num_qd
; x
++) {
759 offset
= qd2offset(qda
[x
]);
760 error
= gfs2_write_alloc_required(ip
, offset
,
761 sizeof(struct gfs2_quota
),
769 al
= gfs2_alloc_get(ip
);
775 * 1 blk for unstuffing inode if stuffed. We add this extra
776 * block to the reservation unconditionally. If the inode
777 * doesn't need unstuffing, the block will be released to the
778 * rgrp since it won't be allocated during the transaction
780 al
->al_requested
= 1;
781 /* +3 in the end for unstuffing block, inode size update block
782 * and another block in case quota straddles page boundary and
783 * two blocks need to be updated instead of 1 */
784 blocks
= num_qd
* data_blocks
+ RES_DINODE
+ num_qd
+ 3;
787 al
->al_requested
+= nalloc
* (data_blocks
+ ind_blocks
);
788 error
= gfs2_inplace_reserve(ip
);
793 blocks
+= al
->al_rgd
->rd_length
+ nalloc
* ind_blocks
+ RES_STATFS
;
795 error
= gfs2_trans_begin(sdp
, blocks
, 0);
799 for (x
= 0; x
< num_qd
; x
++) {
801 offset
= qd2offset(qd
);
802 error
= gfs2_adjust_quota(ip
, offset
, qd
->qd_change_sync
,
803 (struct gfs2_quota_data
*)qd
);
807 do_qc(qd
, -qd
->qd_change_sync
);
815 gfs2_inplace_release(ip
);
819 gfs2_glock_dq_uninit(&i_gh
);
822 gfs2_glock_dq_uninit(&ghs
[qx
]);
824 gfs2_log_flush(ip
->i_gl
->gl_sbd
, ip
->i_gl
);
828 static int do_glock(struct gfs2_quota_data
*qd
, int force_refresh
,
829 struct gfs2_holder
*q_gh
)
831 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
832 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_quota_inode
);
833 struct gfs2_holder i_gh
;
836 struct gfs2_quota_lvb
*qlvb
;
839 error
= gfs2_glock_nq_init(qd
->qd_gl
, LM_ST_SHARED
, 0, q_gh
);
843 qd
->qd_qb
= *(struct gfs2_quota_lvb
*)qd
->qd_gl
->gl_lvb
;
845 if (force_refresh
|| qd
->qd_qb
.qb_magic
!= cpu_to_be32(GFS2_MAGIC
)) {
847 gfs2_glock_dq_uninit(q_gh
);
848 error
= gfs2_glock_nq_init(qd
->qd_gl
,
849 LM_ST_EXCLUSIVE
, GL_NOCACHE
,
854 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &i_gh
);
858 memset(&q
, 0, sizeof(struct gfs2_quota
));
860 error
= gfs2_internal_read(ip
, NULL
, (char *)&q
, &pos
, sizeof(q
));
863 if ((error
< sizeof(q
)) && force_refresh
) {
867 gfs2_glock_dq_uninit(&i_gh
);
869 qlvb
= (struct gfs2_quota_lvb
*)qd
->qd_gl
->gl_lvb
;
870 qlvb
->qb_magic
= cpu_to_be32(GFS2_MAGIC
);
872 qlvb
->qb_limit
= q
.qu_limit
;
873 qlvb
->qb_warn
= q
.qu_warn
;
874 qlvb
->qb_value
= q
.qu_value
;
877 if (gfs2_glock_is_blocking(qd
->qd_gl
)) {
878 gfs2_glock_dq_uninit(q_gh
);
887 gfs2_glock_dq_uninit(&i_gh
);
889 gfs2_glock_dq_uninit(q_gh
);
893 int gfs2_quota_lock(struct gfs2_inode
*ip
, u32 uid
, u32 gid
)
895 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
896 struct gfs2_alloc
*al
= ip
->i_alloc
;
900 gfs2_quota_hold(ip
, uid
, gid
);
902 if (capable(CAP_SYS_RESOURCE
) ||
903 sdp
->sd_args
.ar_quota
!= GFS2_QUOTA_ON
)
906 sort(al
->al_qd
, al
->al_qd_num
, sizeof(struct gfs2_quota_data
*),
909 for (x
= 0; x
< al
->al_qd_num
; x
++) {
910 error
= do_glock(al
->al_qd
[x
], NO_FORCE
, &al
->al_qd_ghs
[x
]);
916 set_bit(GIF_QD_LOCKED
, &ip
->i_flags
);
919 gfs2_glock_dq_uninit(&al
->al_qd_ghs
[x
]);
920 gfs2_quota_unhold(ip
);
926 static int need_sync(struct gfs2_quota_data
*qd
)
928 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
929 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
931 unsigned int num
, den
;
934 if (!qd
->qd_qb
.qb_limit
)
937 spin_lock(&qd_lru_lock
);
938 value
= qd
->qd_change
;
939 spin_unlock(&qd_lru_lock
);
941 spin_lock(>
->gt_spin
);
942 num
= gt
->gt_quota_scale_num
;
943 den
= gt
->gt_quota_scale_den
;
944 spin_unlock(>
->gt_spin
);
948 else if ((s64
)be64_to_cpu(qd
->qd_qb
.qb_value
) >=
949 (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
))
952 value
*= gfs2_jindex_size(sdp
) * num
;
953 value
= div_s64(value
, den
);
954 value
+= (s64
)be64_to_cpu(qd
->qd_qb
.qb_value
);
955 if (value
< (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
))
962 void gfs2_quota_unlock(struct gfs2_inode
*ip
)
964 struct gfs2_alloc
*al
= ip
->i_alloc
;
965 struct gfs2_quota_data
*qda
[4];
966 unsigned int count
= 0;
969 if (!test_and_clear_bit(GIF_QD_LOCKED
, &ip
->i_flags
))
972 for (x
= 0; x
< al
->al_qd_num
; x
++) {
973 struct gfs2_quota_data
*qd
;
977 sync
= need_sync(qd
);
979 gfs2_glock_dq_uninit(&al
->al_qd_ghs
[x
]);
981 if (sync
&& qd_trylock(qd
))
987 for (x
= 0; x
< count
; x
++)
992 gfs2_quota_unhold(ip
);
997 static int print_message(struct gfs2_quota_data
*qd
, char *type
)
999 struct gfs2_sbd
*sdp
= qd
->qd_gl
->gl_sbd
;
1001 printk(KERN_INFO
"GFS2: fsid=%s: quota %s for %s %u\r\n",
1002 sdp
->sd_fsname
, type
,
1003 (test_bit(QDF_USER
, &qd
->qd_flags
)) ? "user" : "group",
1009 int gfs2_quota_check(struct gfs2_inode
*ip
, u32 uid
, u32 gid
)
1011 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1012 struct gfs2_alloc
*al
= ip
->i_alloc
;
1013 struct gfs2_quota_data
*qd
;
1018 if (!test_bit(GIF_QD_LOCKED
, &ip
->i_flags
))
1021 if (sdp
->sd_args
.ar_quota
!= GFS2_QUOTA_ON
)
1024 for (x
= 0; x
< al
->al_qd_num
; x
++) {
1027 if (!((qd
->qd_id
== uid
&& test_bit(QDF_USER
, &qd
->qd_flags
)) ||
1028 (qd
->qd_id
== gid
&& !test_bit(QDF_USER
, &qd
->qd_flags
))))
1031 value
= (s64
)be64_to_cpu(qd
->qd_qb
.qb_value
);
1032 spin_lock(&qd_lru_lock
);
1033 value
+= qd
->qd_change
;
1034 spin_unlock(&qd_lru_lock
);
1036 if (be64_to_cpu(qd
->qd_qb
.qb_limit
) && (s64
)be64_to_cpu(qd
->qd_qb
.qb_limit
) < value
) {
1037 print_message(qd
, "exceeded");
1040 } else if (be64_to_cpu(qd
->qd_qb
.qb_warn
) &&
1041 (s64
)be64_to_cpu(qd
->qd_qb
.qb_warn
) < value
&&
1042 time_after_eq(jiffies
, qd
->qd_last_warn
+
1044 gt_quota_warn_period
) * HZ
)) {
1045 error
= print_message(qd
, "warning");
1046 qd
->qd_last_warn
= jiffies
;
1053 void gfs2_quota_change(struct gfs2_inode
*ip
, s64 change
,
1056 struct gfs2_alloc
*al
= ip
->i_alloc
;
1057 struct gfs2_quota_data
*qd
;
1060 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), change
))
1062 if (ip
->i_diskflags
& GFS2_DIF_SYSTEM
)
1065 for (x
= 0; x
< al
->al_qd_num
; x
++) {
1068 if ((qd
->qd_id
== uid
&& test_bit(QDF_USER
, &qd
->qd_flags
)) ||
1069 (qd
->qd_id
== gid
&& !test_bit(QDF_USER
, &qd
->qd_flags
))) {
1075 int gfs2_quota_sync(struct gfs2_sbd
*sdp
)
1077 struct gfs2_quota_data
**qda
;
1078 unsigned int max_qd
= gfs2_tune_get(sdp
, gt_quota_simul_sync
);
1079 unsigned int num_qd
;
1083 sdp
->sd_quota_sync_gen
++;
1085 qda
= kcalloc(max_qd
, sizeof(struct gfs2_quota_data
*), GFP_KERNEL
);
1093 error
= qd_fish(sdp
, qda
+ num_qd
);
1094 if (error
|| !qda
[num_qd
])
1096 if (++num_qd
== max_qd
)
1102 error
= do_sync(num_qd
, qda
);
1104 for (x
= 0; x
< num_qd
; x
++)
1105 qda
[x
]->qd_sync_gen
=
1106 sdp
->sd_quota_sync_gen
;
1108 for (x
= 0; x
< num_qd
; x
++)
1111 } while (!error
&& num_qd
== max_qd
);
1118 int gfs2_quota_refresh(struct gfs2_sbd
*sdp
, int user
, u32 id
)
1120 struct gfs2_quota_data
*qd
;
1121 struct gfs2_holder q_gh
;
1124 error
= qd_get(sdp
, user
, id
, CREATE
, &qd
);
1128 error
= do_glock(qd
, FORCE
, &q_gh
);
1130 gfs2_glock_dq_uninit(&q_gh
);
1136 static void gfs2_quota_change_in(struct gfs2_quota_change_host
*qc
, const void *buf
)
1138 const struct gfs2_quota_change
*str
= buf
;
1140 qc
->qc_change
= be64_to_cpu(str
->qc_change
);
1141 qc
->qc_flags
= be32_to_cpu(str
->qc_flags
);
1142 qc
->qc_id
= be32_to_cpu(str
->qc_id
);
1145 int gfs2_quota_init(struct gfs2_sbd
*sdp
)
1147 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_qc_inode
);
1148 unsigned int blocks
= ip
->i_disksize
>> sdp
->sd_sb
.sb_bsize_shift
;
1149 unsigned int x
, slot
= 0;
1150 unsigned int found
= 0;
1155 if (!ip
->i_disksize
|| ip
->i_disksize
> (64 << 20) ||
1156 ip
->i_disksize
& (sdp
->sd_sb
.sb_bsize
- 1)) {
1157 gfs2_consist_inode(ip
);
1160 sdp
->sd_quota_slots
= blocks
* sdp
->sd_qc_per_block
;
1161 sdp
->sd_quota_chunks
= DIV_ROUND_UP(sdp
->sd_quota_slots
, 8 * PAGE_SIZE
);
1165 sdp
->sd_quota_bitmap
= kcalloc(sdp
->sd_quota_chunks
,
1166 sizeof(unsigned char *), GFP_NOFS
);
1167 if (!sdp
->sd_quota_bitmap
)
1170 for (x
= 0; x
< sdp
->sd_quota_chunks
; x
++) {
1171 sdp
->sd_quota_bitmap
[x
] = kzalloc(PAGE_SIZE
, GFP_NOFS
);
1172 if (!sdp
->sd_quota_bitmap
[x
])
1176 for (x
= 0; x
< blocks
; x
++) {
1177 struct buffer_head
*bh
;
1182 error
= gfs2_extent_map(&ip
->i_inode
, x
, &new, &dblock
, &extlen
);
1187 bh
= gfs2_meta_ra(ip
->i_gl
, dblock
, extlen
);
1190 if (gfs2_metatype_check(sdp
, bh
, GFS2_METATYPE_QC
)) {
1195 for (y
= 0; y
< sdp
->sd_qc_per_block
&& slot
< sdp
->sd_quota_slots
;
1197 struct gfs2_quota_change_host qc
;
1198 struct gfs2_quota_data
*qd
;
1200 gfs2_quota_change_in(&qc
, bh
->b_data
+
1201 sizeof(struct gfs2_meta_header
) +
1202 y
* sizeof(struct gfs2_quota_change
));
1206 error
= qd_alloc(sdp
, (qc
.qc_flags
& GFS2_QCF_USER
),
1213 set_bit(QDF_CHANGE
, &qd
->qd_flags
);
1214 qd
->qd_change
= qc
.qc_change
;
1216 qd
->qd_slot_count
= 1;
1218 spin_lock(&qd_lru_lock
);
1219 gfs2_icbit_munge(sdp
, sdp
->sd_quota_bitmap
, slot
, 1);
1220 list_add(&qd
->qd_list
, &sdp
->sd_quota_list
);
1221 atomic_inc(&sdp
->sd_quota_count
);
1222 spin_unlock(&qd_lru_lock
);
1233 fs_info(sdp
, "found %u quota changes\n", found
);
1238 gfs2_quota_cleanup(sdp
);
1242 void gfs2_quota_cleanup(struct gfs2_sbd
*sdp
)
1244 struct list_head
*head
= &sdp
->sd_quota_list
;
1245 struct gfs2_quota_data
*qd
;
1248 spin_lock(&qd_lru_lock
);
1249 while (!list_empty(head
)) {
1250 qd
= list_entry(head
->prev
, struct gfs2_quota_data
, qd_list
);
1252 if (atomic_read(&qd
->qd_count
) > 1 ||
1253 (atomic_read(&qd
->qd_count
) &&
1254 !test_bit(QDF_CHANGE
, &qd
->qd_flags
))) {
1255 list_move(&qd
->qd_list
, head
);
1256 spin_unlock(&qd_lru_lock
);
1258 spin_lock(&qd_lru_lock
);
1262 list_del(&qd
->qd_list
);
1263 /* Also remove if this qd exists in the reclaim list */
1264 if (!list_empty(&qd
->qd_reclaim
)) {
1265 list_del_init(&qd
->qd_reclaim
);
1266 atomic_dec(&qd_lru_count
);
1268 atomic_dec(&sdp
->sd_quota_count
);
1269 spin_unlock(&qd_lru_lock
);
1271 if (!atomic_read(&qd
->qd_count
)) {
1272 gfs2_assert_warn(sdp
, !qd
->qd_change
);
1273 gfs2_assert_warn(sdp
, !qd
->qd_slot_count
);
1275 gfs2_assert_warn(sdp
, qd
->qd_slot_count
== 1);
1276 gfs2_assert_warn(sdp
, !qd
->qd_bh_count
);
1278 gfs2_glock_put(qd
->qd_gl
);
1279 kmem_cache_free(gfs2_quotad_cachep
, qd
);
1281 spin_lock(&qd_lru_lock
);
1283 spin_unlock(&qd_lru_lock
);
1285 gfs2_assert_warn(sdp
, !atomic_read(&sdp
->sd_quota_count
));
1287 if (sdp
->sd_quota_bitmap
) {
1288 for (x
= 0; x
< sdp
->sd_quota_chunks
; x
++)
1289 kfree(sdp
->sd_quota_bitmap
[x
]);
1290 kfree(sdp
->sd_quota_bitmap
);
1294 static void quotad_error(struct gfs2_sbd
*sdp
, const char *msg
, int error
)
1296 if (error
== 0 || error
== -EROFS
)
1298 if (!test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
))
1299 fs_err(sdp
, "gfs2_quotad: %s error %d\n", msg
, error
);
1302 static void quotad_check_timeo(struct gfs2_sbd
*sdp
, const char *msg
,
1303 int (*fxn
)(struct gfs2_sbd
*sdp
),
1304 unsigned long t
, unsigned long *timeo
,
1305 unsigned int *new_timeo
)
1308 int error
= fxn(sdp
);
1309 quotad_error(sdp
, msg
, error
);
1310 *timeo
= gfs2_tune_get_i(&sdp
->sd_tune
, new_timeo
) * HZ
;
1316 static void quotad_check_trunc_list(struct gfs2_sbd
*sdp
)
1318 struct gfs2_inode
*ip
;
1322 spin_lock(&sdp
->sd_trunc_lock
);
1323 if (!list_empty(&sdp
->sd_trunc_list
)) {
1324 ip
= list_entry(sdp
->sd_trunc_list
.next
,
1325 struct gfs2_inode
, i_trunc_list
);
1326 list_del_init(&ip
->i_trunc_list
);
1328 spin_unlock(&sdp
->sd_trunc_lock
);
1331 gfs2_glock_finish_truncate(ip
);
1336 * gfs2_quotad - Write cached quota changes into the quota file
1337 * @sdp: Pointer to GFS2 superblock
1341 int gfs2_quotad(void *data
)
1343 struct gfs2_sbd
*sdp
= data
;
1344 struct gfs2_tune
*tune
= &sdp
->sd_tune
;
1345 unsigned long statfs_timeo
= 0;
1346 unsigned long quotad_timeo
= 0;
1347 unsigned long t
= 0;
1351 while (!kthread_should_stop()) {
1353 /* Update the master statfs file */
1354 quotad_check_timeo(sdp
, "statfs", gfs2_statfs_sync
, t
,
1355 &statfs_timeo
, &tune
->gt_statfs_quantum
);
1357 /* Update quota file */
1358 quotad_check_timeo(sdp
, "sync", gfs2_quota_sync
, t
,
1359 "ad_timeo
, &tune
->gt_quota_quantum
);
1361 /* Check for & recover partially truncated inodes */
1362 quotad_check_trunc_list(sdp
);
1364 if (freezing(current
))
1366 t
= min(quotad_timeo
, statfs_timeo
);
1368 prepare_to_wait(&sdp
->sd_quota_wait
, &wait
, TASK_INTERRUPTIBLE
);
1369 spin_lock(&sdp
->sd_trunc_lock
);
1370 empty
= list_empty(&sdp
->sd_trunc_list
);
1371 spin_unlock(&sdp
->sd_trunc_lock
);
1373 t
-= schedule_timeout(t
);
1376 finish_wait(&sdp
->sd_quota_wait
, &wait
);