initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / fs / gfs2 / quota.c
blob37af9a980e4d6610476ff1417f094ff75ad1ed5f
1 /*
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.
8 */
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>
45 #include <linux/fs.h>
46 #include <linux/bio.h>
47 #include <linux/gfs2_ondisk.h>
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
51 #include "gfs2.h"
52 #include "incore.h"
53 #include "bmap.h"
54 #include "glock.h"
55 #include "glops.h"
56 #include "log.h"
57 #include "meta_io.h"
58 #include "quota.h"
59 #include "rgrp.h"
60 #include "super.h"
61 #include "trans.h"
62 #include "inode.h"
63 #include "util.h"
65 #define QUOTA_USER 1
66 #define QUOTA_GROUP 0
68 struct gfs2_quota_change_host {
69 u64 qc_change;
70 u32 qc_flags; /* GFS2_QCF_... */
71 u32 qc_id;
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;
81 struct gfs2_sbd *sdp;
83 if (nr == 0)
84 goto out;
86 if (!(gfp_mask & __GFP_FS))
87 return -1;
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);
111 nr--;
113 spin_unlock(&qd_lru_lock);
115 out:
116 return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
119 static u64 qd2offset(struct gfs2_quota_data *qd)
121 u64 offset;
123 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
124 offset *= sizeof(struct gfs2_quota);
126 return offset;
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;
133 int error;
135 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
136 if (!qd)
137 return -ENOMEM;
139 atomic_set(&qd->qd_count, 1);
140 qd->qd_id = id;
141 if (user)
142 set_bit(QDF_USER, &qd->qd_flags);
143 qd->qd_slot = -1;
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);
148 if (error)
149 goto fail;
151 *qdp = qd;
153 return 0;
155 fail:
156 kmem_cache_free(gfs2_quotad_cachep, qd);
157 return error;
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;
164 int error, found;
166 *qdp = NULL;
168 for (;;) {
169 found = 0;
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);
181 found = 1;
182 break;
186 if (!found)
187 qd = NULL;
189 if (!qd && new_qd) {
190 qd = new_qd;
191 list_add(&qd->qd_list, &sdp->sd_quota_list);
192 atomic_inc(&sdp->sd_quota_count);
193 new_qd = NULL;
196 spin_unlock(&qd_lru_lock);
198 if (qd || !create) {
199 if (new_qd) {
200 gfs2_glock_put(new_qd->qd_gl);
201 kmem_cache_free(gfs2_quotad_cachep, new_qd);
203 *qdp = qd;
204 return 0;
207 error = qd_alloc(sdp, user, id, &new_qd);
208 if (error)
209 return error;
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);
240 return 0;
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];
246 if (byte != 0xFF)
247 goto found;
250 goto fail;
252 found:
253 for (b = 0; b < 8; b++)
254 if (!(byte & (1 << b)))
255 break;
256 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
258 if (qd->qd_slot >= sdp->sd_quota_slots)
259 goto fail;
261 sdp->sd_quota_bitmap[c][o] |= 1 << b;
263 spin_unlock(&qd_lru_lock);
265 return 0;
267 fail:
268 qd->qd_slot_count--;
269 spin_unlock(&qd_lru_lock);
270 return -ENOSPC;
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);
279 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);
291 qd->qd_slot = -1;
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;
302 int error;
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);
309 return 0;
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);
317 if (error)
318 goto fail;
319 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
320 if (error)
321 goto fail;
322 error = -EIO;
323 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
324 goto fail_brelse;
326 qd->qd_bh = bh;
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);
333 return 0;
335 fail_brelse:
336 brelse(bh);
337 fail:
338 qd->qd_bh_count--;
339 mutex_unlock(&sdp->sd_quota_mutex);
340 return error;
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) {
350 brelse(qd->qd_bh);
351 qd->qd_bh = NULL;
352 qd->qd_bh_qc = NULL;
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;
360 int error;
361 int found = 0;
363 *qdp = NULL;
365 if (sdp->sd_vfs->s_flags & MS_RDONLY)
366 return 0;
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)
374 continue;
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);
383 qd->qd_slot_count++;
384 found = 1;
386 break;
389 if (!found)
390 qd = NULL;
392 spin_unlock(&qd_lru_lock);
394 if (qd) {
395 gfs2_assert_warn(sdp, qd->qd_change_sync);
396 error = bh_get(qd);
397 if (error) {
398 clear_bit(QDF_LOCKED, &qd->qd_flags);
399 slot_put(qd);
400 qd_put(qd);
401 return error;
405 *qdp = qd;
407 return 0;
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)
415 return 0;
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);
422 return 0;
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);
432 qd->qd_slot_count++;
434 spin_unlock(&qd_lru_lock);
436 gfs2_assert_warn(sdp, qd->qd_change_sync);
437 if (bh_get(qd)) {
438 clear_bit(QDF_LOCKED, &qd->qd_flags);
439 slot_put(qd);
440 qd_put(qd);
441 return 0;
444 return 1;
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);
452 bh_put(qd);
453 slot_put(qd);
454 qd_put(qd);
457 static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
458 struct gfs2_quota_data **qdp)
460 int error;
462 error = qd_get(sdp, user, id, create, qdp);
463 if (error)
464 return error;
466 error = slot_get(*qdp);
467 if (error)
468 goto fail;
470 error = bh_get(*qdp);
471 if (error)
472 goto fail_slot;
474 return 0;
476 fail_slot:
477 slot_put(*qdp);
478 fail:
479 qd_put(*qdp);
480 return error;
483 static void qdsb_put(struct gfs2_quota_data *qd)
485 bh_put(qd);
486 slot_put(qd);
487 qd_put(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;
495 int error;
497 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
498 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
499 return -EIO;
501 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
502 return 0;
504 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
505 if (error)
506 goto out;
507 al->al_qd_num++;
508 qd++;
510 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
511 if (error)
512 goto out;
513 al->al_qd_num++;
514 qd++;
516 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
517 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
518 if (error)
519 goto out;
520 al->al_qd_num++;
521 qd++;
524 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
525 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
526 if (error)
527 goto out;
528 al->al_qd_num++;
529 qd++;
532 out:
533 if (error)
534 gfs2_quota_unhold(ip);
535 return error;
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;
542 unsigned int x;
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]);
548 al->al_qd[x] = NULL;
550 al->al_qd_num = 0;
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))
561 return -1;
562 else
563 return 1;
565 if (qd_a->qd_id < qd_b->qd_id)
566 return -1;
567 if (qd_a->qd_id > qd_b->qd_id)
568 return 1;
570 return 0;
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;
578 s64 x;
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)) {
584 qc->qc_change = 0;
585 qc->qc_flags = 0;
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);
595 qd->qd_change = x;
596 spin_unlock(&qd_lru_lock);
598 if (!x) {
599 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
600 clear_bit(QDF_CHANGE, &qd->qd_flags);
601 qc->qc_flags = 0;
602 qc->qc_id = 0;
603 slot_put(qd);
604 qd_put(qd);
605 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
606 qd_hold(qd);
607 slot_hold(qd);
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;
635 struct page *page;
636 void *kaddr, *ptr;
637 struct gfs2_quota q, *qp;
638 int err, nbytes;
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));
645 if (err < 0)
646 return err;
648 err = -EIO;
649 qp = &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 */
656 ptr = qp;
657 nbytes = sizeof(struct gfs2_quota);
658 get_a_page:
659 page = grab_cache_page(mapping, index);
660 if (!page)
661 return -ENOMEM;
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);
670 pos = blocksize;
671 while (offset >= pos) {
672 bh = bh->b_this_page;
673 iblock++;
674 pos += blocksize;
677 if (!buffer_mapped(bh)) {
678 gfs2_block_map(inode, iblock, bh, 1);
679 if (!buffer_mapped(bh))
680 goto unlock_out;
681 /* If it's a newly allocated disk block for quota, zero it */
682 if (buffer_new(bh))
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);
691 wait_on_buffer(bh);
692 if (!buffer_uptodate(bh))
693 goto unlock_out;
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);
704 unlock_page(page);
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) {
710 ptr = ptr + nbytes;
711 nbytes = sizeof(struct gfs2_quota) - nbytes;
712 offset = 0;
713 index++;
714 goto get_a_page;
716 err = 0;
717 return err;
718 unlock_out:
719 unlock_page(page);
720 page_cache_release(page);
721 return err;
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;
730 unsigned int qx, x;
731 struct gfs2_quota_data *qd;
732 loff_t offset;
733 unsigned int nalloc = 0, blocks;
734 struct gfs2_alloc *al = NULL;
735 int error;
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);
741 if (!ghs)
742 return -ENOMEM;
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]);
748 if (error)
749 goto out;
752 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
753 if (error)
754 goto out;
756 for (x = 0; x < num_qd; x++) {
757 int alloc_required;
759 offset = qd2offset(qda[x]);
760 error = gfs2_write_alloc_required(ip, offset,
761 sizeof(struct gfs2_quota),
762 &alloc_required);
763 if (error)
764 goto out_gunlock;
765 if (alloc_required)
766 nalloc++;
769 al = gfs2_alloc_get(ip);
770 if (!al) {
771 error = -ENOMEM;
772 goto out_gunlock;
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;
786 if (nalloc)
787 al->al_requested += nalloc * (data_blocks + ind_blocks);
788 error = gfs2_inplace_reserve(ip);
789 if (error)
790 goto out_alloc;
792 if (nalloc)
793 blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS;
795 error = gfs2_trans_begin(sdp, blocks, 0);
796 if (error)
797 goto out_ipres;
799 for (x = 0; x < num_qd; x++) {
800 qd = qda[x];
801 offset = qd2offset(qd);
802 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
803 (struct gfs2_quota_data *)qd);
804 if (error)
805 goto out_end_trans;
807 do_qc(qd, -qd->qd_change_sync);
810 error = 0;
812 out_end_trans:
813 gfs2_trans_end(sdp);
814 out_ipres:
815 gfs2_inplace_release(ip);
816 out_alloc:
817 gfs2_alloc_put(ip);
818 out_gunlock:
819 gfs2_glock_dq_uninit(&i_gh);
820 out:
821 while (qx--)
822 gfs2_glock_dq_uninit(&ghs[qx]);
823 kfree(ghs);
824 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
825 return error;
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;
834 struct gfs2_quota q;
835 int error;
836 struct gfs2_quota_lvb *qlvb;
838 restart:
839 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
840 if (error)
841 return error;
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)) {
846 loff_t pos;
847 gfs2_glock_dq_uninit(q_gh);
848 error = gfs2_glock_nq_init(qd->qd_gl,
849 LM_ST_EXCLUSIVE, GL_NOCACHE,
850 q_gh);
851 if (error)
852 return error;
854 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
855 if (error)
856 goto fail;
858 memset(&q, 0, sizeof(struct gfs2_quota));
859 pos = qd2offset(qd);
860 error = gfs2_internal_read(ip, NULL, (char *)&q, &pos, sizeof(q));
861 if (error < 0)
862 goto fail_gunlock;
863 if ((error < sizeof(q)) && force_refresh) {
864 error = -ENOENT;
865 goto fail_gunlock;
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);
871 qlvb->__pad = 0;
872 qlvb->qb_limit = q.qu_limit;
873 qlvb->qb_warn = q.qu_warn;
874 qlvb->qb_value = q.qu_value;
875 qd->qd_qb = *qlvb;
877 if (gfs2_glock_is_blocking(qd->qd_gl)) {
878 gfs2_glock_dq_uninit(q_gh);
879 force_refresh = 0;
880 goto restart;
884 return 0;
886 fail_gunlock:
887 gfs2_glock_dq_uninit(&i_gh);
888 fail:
889 gfs2_glock_dq_uninit(q_gh);
890 return error;
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;
897 unsigned int x;
898 int error = 0;
900 gfs2_quota_hold(ip, uid, gid);
902 if (capable(CAP_SYS_RESOURCE) ||
903 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
904 return 0;
906 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
907 sort_qd, NULL);
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]);
911 if (error)
912 break;
915 if (!error)
916 set_bit(GIF_QD_LOCKED, &ip->i_flags);
917 else {
918 while (x--)
919 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
920 gfs2_quota_unhold(ip);
923 return error;
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;
930 s64 value;
931 unsigned int num, den;
932 int do_sync = 1;
934 if (!qd->qd_qb.qb_limit)
935 return 0;
937 spin_lock(&qd_lru_lock);
938 value = qd->qd_change;
939 spin_unlock(&qd_lru_lock);
941 spin_lock(&gt->gt_spin);
942 num = gt->gt_quota_scale_num;
943 den = gt->gt_quota_scale_den;
944 spin_unlock(&gt->gt_spin);
946 if (value < 0)
947 do_sync = 0;
948 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
949 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
950 do_sync = 0;
951 else {
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))
956 do_sync = 0;
959 return do_sync;
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;
967 unsigned int x;
969 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
970 goto out;
972 for (x = 0; x < al->al_qd_num; x++) {
973 struct gfs2_quota_data *qd;
974 int sync;
976 qd = al->al_qd[x];
977 sync = need_sync(qd);
979 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
981 if (sync && qd_trylock(qd))
982 qda[count++] = qd;
985 if (count) {
986 do_sync(count, qda);
987 for (x = 0; x < count; x++)
988 qd_unlock(qda[x]);
991 out:
992 gfs2_quota_unhold(ip);
995 #define MAX_LINE 256
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",
1004 qd->qd_id);
1006 return 0;
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;
1014 s64 value;
1015 unsigned int x;
1016 int error = 0;
1018 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1019 return 0;
1021 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1022 return 0;
1024 for (x = 0; x < al->al_qd_num; x++) {
1025 qd = al->al_qd[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))))
1029 continue;
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");
1038 error = -EDQUOT;
1039 break;
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 +
1043 gfs2_tune_get(sdp,
1044 gt_quota_warn_period) * HZ)) {
1045 error = print_message(qd, "warning");
1046 qd->qd_last_warn = jiffies;
1050 return error;
1053 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1054 u32 uid, u32 gid)
1056 struct gfs2_alloc *al = ip->i_alloc;
1057 struct gfs2_quota_data *qd;
1058 unsigned int x;
1060 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
1061 return;
1062 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
1063 return;
1065 for (x = 0; x < al->al_qd_num; x++) {
1066 qd = al->al_qd[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))) {
1070 do_qc(qd, change);
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;
1080 unsigned int x;
1081 int error = 0;
1083 sdp->sd_quota_sync_gen++;
1085 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1086 if (!qda)
1087 return -ENOMEM;
1089 do {
1090 num_qd = 0;
1092 for (;;) {
1093 error = qd_fish(sdp, qda + num_qd);
1094 if (error || !qda[num_qd])
1095 break;
1096 if (++num_qd == max_qd)
1097 break;
1100 if (num_qd) {
1101 if (!error)
1102 error = do_sync(num_qd, qda);
1103 if (!error)
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++)
1109 qd_unlock(qda[x]);
1111 } while (!error && num_qd == max_qd);
1113 kfree(qda);
1115 return error;
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;
1122 int error;
1124 error = qd_get(sdp, user, id, CREATE, &qd);
1125 if (error)
1126 return error;
1128 error = do_glock(qd, FORCE, &q_gh);
1129 if (!error)
1130 gfs2_glock_dq_uninit(&q_gh);
1132 qd_put(qd);
1133 return error;
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;
1151 u64 dblock;
1152 u32 extlen = 0;
1153 int error;
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);
1158 return -EIO;
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);
1163 error = -ENOMEM;
1165 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1166 sizeof(unsigned char *), GFP_NOFS);
1167 if (!sdp->sd_quota_bitmap)
1168 return error;
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])
1173 goto fail;
1176 for (x = 0; x < blocks; x++) {
1177 struct buffer_head *bh;
1178 unsigned int y;
1180 if (!extlen) {
1181 int new = 0;
1182 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1183 if (error)
1184 goto fail;
1186 error = -EIO;
1187 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1188 if (!bh)
1189 goto fail;
1190 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1191 brelse(bh);
1192 goto fail;
1195 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1196 y++, slot++) {
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));
1203 if (!qc.qc_change)
1204 continue;
1206 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1207 qc.qc_id, &qd);
1208 if (error) {
1209 brelse(bh);
1210 goto fail;
1213 set_bit(QDF_CHANGE, &qd->qd_flags);
1214 qd->qd_change = qc.qc_change;
1215 qd->qd_slot = slot;
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);
1224 found++;
1227 brelse(bh);
1228 dblock++;
1229 extlen--;
1232 if (found)
1233 fs_info(sdp, "found %u quota changes\n", found);
1235 return 0;
1237 fail:
1238 gfs2_quota_cleanup(sdp);
1239 return error;
1242 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1244 struct list_head *head = &sdp->sd_quota_list;
1245 struct gfs2_quota_data *qd;
1246 unsigned int x;
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);
1257 schedule();
1258 spin_lock(&qd_lru_lock);
1259 continue;
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);
1274 } else
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)
1297 return;
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)
1307 if (t >= *timeo) {
1308 int error = fxn(sdp);
1309 quotad_error(sdp, msg, error);
1310 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1311 } else {
1312 *timeo -= t;
1316 static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1318 struct gfs2_inode *ip;
1320 while(1) {
1321 ip = NULL;
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);
1329 if (ip == NULL)
1330 return;
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;
1348 DEFINE_WAIT(wait);
1349 int empty;
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 &quotad_timeo, &tune->gt_quota_quantum);
1361 /* Check for & recover partially truncated inodes */
1362 quotad_check_trunc_list(sdp);
1364 if (freezing(current))
1365 refrigerator();
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);
1372 if (empty)
1373 t -= schedule_timeout(t);
1374 else
1375 t = 0;
1376 finish_wait(&sdp->sd_quota_wait, &wait);
1379 return 0;