1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
41#include <linux/sched.h>
42#include <linux/slab.h>
43#include <linux/mm.h>
44#include <linux/spinlock.h>
45#include <linux/completion.h>
46#include <linux/buffer_head.h>
47#include <linux/sort.h>
48#include <linux/fs.h>
49#include <linux/bio.h>
50#include <linux/gfs2_ondisk.h>
51#include <linux/kthread.h>
52#include <linux/freezer.h>
53#include <linux/quota.h>
54#include <linux/dqblk_xfs.h>
55#include <linux/lockref.h>
56#include <linux/list_lru.h>
57#include <linux/rcupdate.h>
58#include <linux/rculist_bl.h>
59#include <linux/bit_spinlock.h>
60#include <linux/jhash.h>
61#include <linux/vmalloc.h>
62
63#include "gfs2.h"
64#include "incore.h"
65#include "bmap.h"
66#include "glock.h"
67#include "glops.h"
68#include "log.h"
69#include "meta_io.h"
70#include "quota.h"
71#include "rgrp.h"
72#include "super.h"
73#include "trans.h"
74#include "inode.h"
75#include "util.h"
76
77#define GFS2_QD_HASH_SHIFT 12
78#define GFS2_QD_HASH_SIZE BIT(GFS2_QD_HASH_SHIFT)
79#define GFS2_QD_HASH_MASK (GFS2_QD_HASH_SIZE - 1)
80
81
82
83static DEFINE_SPINLOCK(qd_lock);
84struct list_lru gfs2_qd_lru;
85
86static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
87
88static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
89 const struct kqid qid)
90{
91 unsigned int h;
92
93 h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
94 h = jhash(&qid, sizeof(struct kqid), h);
95
96 return h & GFS2_QD_HASH_MASK;
97}
98
99static inline void spin_lock_bucket(unsigned int hash)
100{
101 hlist_bl_lock(&qd_hash_table[hash]);
102}
103
104static inline void spin_unlock_bucket(unsigned int hash)
105{
106 hlist_bl_unlock(&qd_hash_table[hash]);
107}
108
109static void gfs2_qd_dealloc(struct rcu_head *rcu)
110{
111 struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
112 kmem_cache_free(gfs2_quotad_cachep, qd);
113}
114
115static void gfs2_qd_dispose(struct list_head *list)
116{
117 struct gfs2_quota_data *qd;
118 struct gfs2_sbd *sdp;
119
120 while (!list_empty(list)) {
121 qd = list_first_entry(list, struct gfs2_quota_data, qd_lru);
122 sdp = qd->qd_gl->gl_name.ln_sbd;
123
124 list_del(&qd->qd_lru);
125
126
127 spin_lock(&qd_lock);
128 list_del(&qd->qd_list);
129 spin_unlock(&qd_lock);
130
131 spin_lock_bucket(qd->qd_hash);
132 hlist_bl_del_rcu(&qd->qd_hlist);
133 spin_unlock_bucket(qd->qd_hash);
134
135 gfs2_assert_warn(sdp, !qd->qd_change);
136 gfs2_assert_warn(sdp, !qd->qd_slot_count);
137 gfs2_assert_warn(sdp, !qd->qd_bh_count);
138
139 gfs2_glock_put(qd->qd_gl);
140 atomic_dec(&sdp->sd_quota_count);
141
142
143 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
144 }
145}
146
147
148static enum lru_status gfs2_qd_isolate(struct list_head *item,
149 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
150{
151 struct list_head *dispose = arg;
152 struct gfs2_quota_data *qd = list_entry(item, struct gfs2_quota_data, qd_lru);
153
154 if (!spin_trylock(&qd->qd_lockref.lock))
155 return LRU_SKIP;
156
157 if (qd->qd_lockref.count == 0) {
158 lockref_mark_dead(&qd->qd_lockref);
159 list_lru_isolate_move(lru, &qd->qd_lru, dispose);
160 }
161
162 spin_unlock(&qd->qd_lockref.lock);
163 return LRU_REMOVED;
164}
165
166static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
167 struct shrink_control *sc)
168{
169 LIST_HEAD(dispose);
170 unsigned long freed;
171
172 if (!(sc->gfp_mask & __GFP_FS))
173 return SHRINK_STOP;
174
175 freed = list_lru_shrink_walk(&gfs2_qd_lru, sc,
176 gfs2_qd_isolate, &dispose);
177
178 gfs2_qd_dispose(&dispose);
179
180 return freed;
181}
182
183static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
184 struct shrink_control *sc)
185{
186 return vfs_pressure_ratio(list_lru_shrink_count(&gfs2_qd_lru, sc));
187}
188
189struct shrinker gfs2_qd_shrinker = {
190 .count_objects = gfs2_qd_shrink_count,
191 .scan_objects = gfs2_qd_shrink_scan,
192 .seeks = DEFAULT_SEEKS,
193 .flags = SHRINKER_NUMA_AWARE,
194};
195
196
197static u64 qd2index(struct gfs2_quota_data *qd)
198{
199 struct kqid qid = qd->qd_id;
200 return (2 * (u64)from_kqid(&init_user_ns, qid)) +
201 ((qid.type == USRQUOTA) ? 0 : 1);
202}
203
204static u64 qd2offset(struct gfs2_quota_data *qd)
205{
206 u64 offset;
207
208 offset = qd2index(qd);
209 offset *= sizeof(struct gfs2_quota);
210
211 return offset;
212}
213
214static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
215{
216 struct gfs2_quota_data *qd;
217 int error;
218
219 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
220 if (!qd)
221 return NULL;
222
223 qd->qd_sbd = sdp;
224 qd->qd_lockref.count = 1;
225 spin_lock_init(&qd->qd_lockref.lock);
226 qd->qd_id = qid;
227 qd->qd_slot = -1;
228 INIT_LIST_HEAD(&qd->qd_lru);
229 qd->qd_hash = hash;
230
231 error = gfs2_glock_get(sdp, qd2index(qd),
232 &gfs2_quota_glops, CREATE, &qd->qd_gl);
233 if (error)
234 goto fail;
235
236 return qd;
237
238fail:
239 kmem_cache_free(gfs2_quotad_cachep, qd);
240 return NULL;
241}
242
243static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
244 const struct gfs2_sbd *sdp,
245 struct kqid qid)
246{
247 struct gfs2_quota_data *qd;
248 struct hlist_bl_node *h;
249
250 hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
251 if (!qid_eq(qd->qd_id, qid))
252 continue;
253 if (qd->qd_sbd != sdp)
254 continue;
255 if (lockref_get_not_dead(&qd->qd_lockref)) {
256 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
257 return qd;
258 }
259 }
260
261 return NULL;
262}
263
264
265static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
266 struct gfs2_quota_data **qdp)
267{
268 struct gfs2_quota_data *qd, *new_qd;
269 unsigned int hash = gfs2_qd_hash(sdp, qid);
270
271 rcu_read_lock();
272 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
273 rcu_read_unlock();
274
275 if (qd)
276 return 0;
277
278 new_qd = qd_alloc(hash, sdp, qid);
279 if (!new_qd)
280 return -ENOMEM;
281
282 spin_lock(&qd_lock);
283 spin_lock_bucket(hash);
284 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
285 if (qd == NULL) {
286 *qdp = new_qd;
287 list_add(&new_qd->qd_list, &sdp->sd_quota_list);
288 hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
289 atomic_inc(&sdp->sd_quota_count);
290 }
291 spin_unlock_bucket(hash);
292 spin_unlock(&qd_lock);
293
294 if (qd) {
295 gfs2_glock_put(new_qd->qd_gl);
296 kmem_cache_free(gfs2_quotad_cachep, new_qd);
297 }
298
299 return 0;
300}
301
302
303static void qd_hold(struct gfs2_quota_data *qd)
304{
305 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
306 gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
307 lockref_get(&qd->qd_lockref);
308}
309
310static void qd_put(struct gfs2_quota_data *qd)
311{
312 if (lockref_put_or_lock(&qd->qd_lockref))
313 return;
314
315 qd->qd_lockref.count = 0;
316 list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
317 spin_unlock(&qd->qd_lockref.lock);
318
319}
320
321static int slot_get(struct gfs2_quota_data *qd)
322{
323 struct gfs2_sbd *sdp = qd->qd_sbd;
324 unsigned int bit;
325 int error = 0;
326
327 spin_lock(&sdp->sd_bitmap_lock);
328 if (qd->qd_slot_count != 0)
329 goto out;
330
331 error = -ENOSPC;
332 bit = find_first_zero_bit(sdp->sd_quota_bitmap, sdp->sd_quota_slots);
333 if (bit < sdp->sd_quota_slots) {
334 set_bit(bit, sdp->sd_quota_bitmap);
335 qd->qd_slot = bit;
336 error = 0;
337out:
338 qd->qd_slot_count++;
339 }
340 spin_unlock(&sdp->sd_bitmap_lock);
341
342 return error;
343}
344
345static void slot_hold(struct gfs2_quota_data *qd)
346{
347 struct gfs2_sbd *sdp = qd->qd_sbd;
348
349 spin_lock(&sdp->sd_bitmap_lock);
350 gfs2_assert(sdp, qd->qd_slot_count);
351 qd->qd_slot_count++;
352 spin_unlock(&sdp->sd_bitmap_lock);
353}
354
355static void slot_put(struct gfs2_quota_data *qd)
356{
357 struct gfs2_sbd *sdp = qd->qd_sbd;
358
359 spin_lock(&sdp->sd_bitmap_lock);
360 gfs2_assert(sdp, qd->qd_slot_count);
361 if (!--qd->qd_slot_count) {
362 BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap));
363 qd->qd_slot = -1;
364 }
365 spin_unlock(&sdp->sd_bitmap_lock);
366}
367
368static int bh_get(struct gfs2_quota_data *qd)
369{
370 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
371 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
372 unsigned int block, offset;
373 struct buffer_head *bh;
374 int error;
375 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
376
377 mutex_lock(&sdp->sd_quota_mutex);
378
379 if (qd->qd_bh_count++) {
380 mutex_unlock(&sdp->sd_quota_mutex);
381 return 0;
382 }
383
384 block = qd->qd_slot / sdp->sd_qc_per_block;
385 offset = qd->qd_slot % sdp->sd_qc_per_block;
386
387 bh_map.b_size = BIT(ip->i_inode.i_blkbits);
388 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
389 if (error)
390 goto fail;
391 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, 0, &bh);
392 if (error)
393 goto fail;
394 error = -EIO;
395 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
396 goto fail_brelse;
397
398 qd->qd_bh = bh;
399 qd->qd_bh_qc = (struct gfs2_quota_change *)
400 (bh->b_data + sizeof(struct gfs2_meta_header) +
401 offset * sizeof(struct gfs2_quota_change));
402
403 mutex_unlock(&sdp->sd_quota_mutex);
404
405 return 0;
406
407fail_brelse:
408 brelse(bh);
409fail:
410 qd->qd_bh_count--;
411 mutex_unlock(&sdp->sd_quota_mutex);
412 return error;
413}
414
415static void bh_put(struct gfs2_quota_data *qd)
416{
417 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
418
419 mutex_lock(&sdp->sd_quota_mutex);
420 gfs2_assert(sdp, qd->qd_bh_count);
421 if (!--qd->qd_bh_count) {
422 brelse(qd->qd_bh);
423 qd->qd_bh = NULL;
424 qd->qd_bh_qc = NULL;
425 }
426 mutex_unlock(&sdp->sd_quota_mutex);
427}
428
429static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
430 u64 *sync_gen)
431{
432 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
433 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
434 (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
435 return 0;
436
437 if (!lockref_get_not_dead(&qd->qd_lockref))
438 return 0;
439
440 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
441 set_bit(QDF_LOCKED, &qd->qd_flags);
442 qd->qd_change_sync = qd->qd_change;
443 slot_hold(qd);
444 return 1;
445}
446
447static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
448{
449 struct gfs2_quota_data *qd = NULL;
450 int error;
451 int found = 0;
452
453 *qdp = NULL;
454
455 if (sb_rdonly(sdp->sd_vfs))
456 return 0;
457
458 spin_lock(&qd_lock);
459
460 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
461 found = qd_check_sync(sdp, qd, &sdp->sd_quota_sync_gen);
462 if (found)
463 break;
464 }
465
466 if (!found)
467 qd = NULL;
468
469 spin_unlock(&qd_lock);
470
471 if (qd) {
472 gfs2_assert_warn(sdp, qd->qd_change_sync);
473 error = bh_get(qd);
474 if (error) {
475 clear_bit(QDF_LOCKED, &qd->qd_flags);
476 slot_put(qd);
477 qd_put(qd);
478 return error;
479 }
480 }
481
482 *qdp = qd;
483
484 return 0;
485}
486
487static void qd_unlock(struct gfs2_quota_data *qd)
488{
489 gfs2_assert_warn(qd->qd_gl->gl_name.ln_sbd,
490 test_bit(QDF_LOCKED, &qd->qd_flags));
491 clear_bit(QDF_LOCKED, &qd->qd_flags);
492 bh_put(qd);
493 slot_put(qd);
494 qd_put(qd);
495}
496
497static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
498 struct gfs2_quota_data **qdp)
499{
500 int error;
501
502 error = qd_get(sdp, qid, qdp);
503 if (error)
504 return error;
505
506 error = slot_get(*qdp);
507 if (error)
508 goto fail;
509
510 error = bh_get(*qdp);
511 if (error)
512 goto fail_slot;
513
514 return 0;
515
516fail_slot:
517 slot_put(*qdp);
518fail:
519 qd_put(*qdp);
520 return error;
521}
522
523static void qdsb_put(struct gfs2_quota_data *qd)
524{
525 bh_put(qd);
526 slot_put(qd);
527 qd_put(qd);
528}
529
530
531
532
533
534
535int gfs2_qa_get(struct gfs2_inode *ip)
536{
537 int error = 0;
538 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
539
540 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
541 return 0;
542
543 down_write(&ip->i_rw_mutex);
544 if (ip->i_qadata == NULL) {
545 ip->i_qadata = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
546 if (!ip->i_qadata) {
547 error = -ENOMEM;
548 goto out;
549 }
550 }
551 ip->i_qadata->qa_ref++;
552out:
553 up_write(&ip->i_rw_mutex);
554 return error;
555}
556
557void gfs2_qa_put(struct gfs2_inode *ip)
558{
559 down_write(&ip->i_rw_mutex);
560 if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) {
561 kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
562 ip->i_qadata = NULL;
563 }
564 up_write(&ip->i_rw_mutex);
565}
566
567int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
568{
569 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
570 struct gfs2_quota_data **qd;
571 int error;
572
573 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
574 return 0;
575
576 error = gfs2_qa_get(ip);
577 if (error)
578 return error;
579
580 qd = ip->i_qadata->qa_qd;
581
582 if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) ||
583 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) {
584 error = -EIO;
585 goto out;
586 }
587
588 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
589 if (error)
590 goto out_unhold;
591 ip->i_qadata->qa_qd_num++;
592 qd++;
593
594 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
595 if (error)
596 goto out_unhold;
597 ip->i_qadata->qa_qd_num++;
598 qd++;
599
600 if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
601 !uid_eq(uid, ip->i_inode.i_uid)) {
602 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
603 if (error)
604 goto out_unhold;
605 ip->i_qadata->qa_qd_num++;
606 qd++;
607 }
608
609 if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
610 !gid_eq(gid, ip->i_inode.i_gid)) {
611 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
612 if (error)
613 goto out_unhold;
614 ip->i_qadata->qa_qd_num++;
615 qd++;
616 }
617
618out_unhold:
619 if (error)
620 gfs2_quota_unhold(ip);
621out:
622 return error;
623}
624
625void gfs2_quota_unhold(struct gfs2_inode *ip)
626{
627 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
628 u32 x;
629
630 if (ip->i_qadata == NULL)
631 return;
632
633 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
634
635 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
636 qdsb_put(ip->i_qadata->qa_qd[x]);
637 ip->i_qadata->qa_qd[x] = NULL;
638 }
639 ip->i_qadata->qa_qd_num = 0;
640 gfs2_qa_put(ip);
641}
642
643static int sort_qd(const void *a, const void *b)
644{
645 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
646 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
647
648 if (qid_lt(qd_a->qd_id, qd_b->qd_id))
649 return -1;
650 if (qid_lt(qd_b->qd_id, qd_a->qd_id))
651 return 1;
652 return 0;
653}
654
655static void do_qc(struct gfs2_quota_data *qd, s64 change)
656{
657 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
658 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
659 struct gfs2_quota_change *qc = qd->qd_bh_qc;
660 s64 x;
661
662 mutex_lock(&sdp->sd_quota_mutex);
663 gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
664
665 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
666 qc->qc_change = 0;
667 qc->qc_flags = 0;
668 if (qd->qd_id.type == USRQUOTA)
669 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
670 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
671 }
672
673 x = be64_to_cpu(qc->qc_change) + change;
674 qc->qc_change = cpu_to_be64(x);
675
676 spin_lock(&qd_lock);
677 qd->qd_change = x;
678 spin_unlock(&qd_lock);
679
680 if (!x) {
681 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
682 clear_bit(QDF_CHANGE, &qd->qd_flags);
683 qc->qc_flags = 0;
684 qc->qc_id = 0;
685 slot_put(qd);
686 qd_put(qd);
687 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
688 qd_hold(qd);
689 slot_hold(qd);
690 }
691
692 if (change < 0)
693 clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
694 mutex_unlock(&sdp->sd_quota_mutex);
695}
696
697static int gfs2_write_buf_to_page(struct gfs2_inode *ip, unsigned long index,
698 unsigned off, void *buf, unsigned bytes)
699{
700 struct inode *inode = &ip->i_inode;
701 struct gfs2_sbd *sdp = GFS2_SB(inode);
702 struct address_space *mapping = inode->i_mapping;
703 struct page *page;
704 struct buffer_head *bh;
705 void *kaddr;
706 u64 blk;
707 unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0;
708 unsigned to_write = bytes, pg_off = off;
709 int done = 0;
710
711 blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift);
712 boff = off % bsize;
713
714 page = find_or_create_page(mapping, index, GFP_NOFS);
715 if (!page)
716 return -ENOMEM;
717 if (!page_has_buffers(page))
718 create_empty_buffers(page, bsize, 0);
719
720 bh = page_buffers(page);
721 while (!done) {
722
723 if (pg_off >= ((bnum * bsize) + bsize)) {
724 bh = bh->b_this_page;
725 bnum++;
726 blk++;
727 continue;
728 }
729 if (!buffer_mapped(bh)) {
730 gfs2_block_map(inode, blk, bh, 1);
731 if (!buffer_mapped(bh))
732 goto unlock_out;
733
734 if (buffer_new(bh))
735 zero_user(page, bnum * bsize, bh->b_size);
736 }
737 if (PageUptodate(page))
738 set_buffer_uptodate(bh);
739 if (!buffer_uptodate(bh)) {
740 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
741 wait_on_buffer(bh);
742 if (!buffer_uptodate(bh))
743 goto unlock_out;
744 }
745 if (gfs2_is_jdata(ip))
746 gfs2_trans_add_data(ip->i_gl, bh);
747 else
748 gfs2_ordered_add_inode(ip);
749
750
751 if (to_write > (bsize - boff)) {
752 pg_off += (bsize - boff);
753 to_write -= (bsize - boff);
754 boff = pg_off % bsize;
755 continue;
756 }
757 done = 1;
758 }
759
760
761 kaddr = kmap_atomic(page);
762 memcpy(kaddr + off, buf, bytes);
763 flush_dcache_page(page);
764 kunmap_atomic(kaddr);
765 unlock_page(page);
766 put_page(page);
767
768 return 0;
769
770unlock_out:
771 unlock_page(page);
772 put_page(page);
773 return -EIO;
774}
775
776static int gfs2_write_disk_quota(struct gfs2_inode *ip, struct gfs2_quota *qp,
777 loff_t loc)
778{
779 unsigned long pg_beg;
780 unsigned pg_off, nbytes, overflow = 0;
781 int pg_oflow = 0, error;
782 void *ptr;
783
784 nbytes = sizeof(struct gfs2_quota);
785
786 pg_beg = loc >> PAGE_SHIFT;
787 pg_off = loc % PAGE_SIZE;
788
789
790 if ((pg_off + nbytes) > PAGE_SIZE) {
791 pg_oflow = 1;
792 overflow = (pg_off + nbytes) - PAGE_SIZE;
793 }
794
795 ptr = qp;
796 error = gfs2_write_buf_to_page(ip, pg_beg, pg_off, ptr,
797 nbytes - overflow);
798
799 if (!error && pg_oflow)
800 error = gfs2_write_buf_to_page(ip, pg_beg + 1, 0,
801 ptr + nbytes - overflow,
802 overflow);
803 return error;
804}
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
821 s64 change, struct gfs2_quota_data *qd,
822 struct qc_dqblk *fdq)
823{
824 struct inode *inode = &ip->i_inode;
825 struct gfs2_sbd *sdp = GFS2_SB(inode);
826 struct gfs2_quota q;
827 int err;
828 u64 size;
829
830 if (gfs2_is_stuffed(ip)) {
831 err = gfs2_unstuff_dinode(ip);
832 if (err)
833 return err;
834 }
835
836 memset(&q, 0, sizeof(struct gfs2_quota));
837 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
838 if (err < 0)
839 return err;
840
841 loc -= sizeof(q);
842 err = -EIO;
843 be64_add_cpu(&q.qu_value, change);
844 if (((s64)be64_to_cpu(q.qu_value)) < 0)
845 q.qu_value = 0;
846 qd->qd_qb.qb_value = q.qu_value;
847 if (fdq) {
848 if (fdq->d_fieldmask & QC_SPC_SOFT) {
849 q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift);
850 qd->qd_qb.qb_warn = q.qu_warn;
851 }
852 if (fdq->d_fieldmask & QC_SPC_HARD) {
853 q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift);
854 qd->qd_qb.qb_limit = q.qu_limit;
855 }
856 if (fdq->d_fieldmask & QC_SPACE) {
857 q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift);
858 qd->qd_qb.qb_value = q.qu_value;
859 }
860 }
861
862 err = gfs2_write_disk_quota(ip, &q, loc);
863 if (!err) {
864 size = loc + sizeof(struct gfs2_quota);
865 if (size > inode->i_size)
866 i_size_write(inode, size);
867 inode->i_mtime = inode->i_atime = current_time(inode);
868 mark_inode_dirty(inode);
869 set_bit(QDF_REFRESH, &qd->qd_flags);
870 }
871
872 return err;
873}
874
875static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
876{
877 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_name.ln_sbd;
878 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
879 struct gfs2_alloc_parms ap = { .aflags = 0, };
880 unsigned int data_blocks, ind_blocks;
881 struct gfs2_holder *ghs, i_gh;
882 unsigned int qx, x;
883 struct gfs2_quota_data *qd;
884 unsigned reserved;
885 loff_t offset;
886 unsigned int nalloc = 0, blocks;
887 int error;
888
889 error = gfs2_qa_get(ip);
890 if (error)
891 return error;
892
893 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
894 &data_blocks, &ind_blocks);
895
896 ghs = kmalloc_array(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
897 if (!ghs) {
898 error = -ENOMEM;
899 goto out;
900 }
901
902 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
903 inode_lock(&ip->i_inode);
904 for (qx = 0; qx < num_qd; qx++) {
905 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
906 GL_NOCACHE, &ghs[qx]);
907 if (error)
908 goto out_dq;
909 }
910
911 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
912 if (error)
913 goto out_dq;
914
915 for (x = 0; x < num_qd; x++) {
916 offset = qd2offset(qda[x]);
917 if (gfs2_write_alloc_required(ip, offset,
918 sizeof(struct gfs2_quota)))
919 nalloc++;
920 }
921
922
923
924
925
926
927
928
929
930
931 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
932
933 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
934 ap.target = reserved;
935 error = gfs2_inplace_reserve(ip, &ap);
936 if (error)
937 goto out_alloc;
938
939 if (nalloc)
940 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
941
942 error = gfs2_trans_begin(sdp, blocks, 0);
943 if (error)
944 goto out_ipres;
945
946 for (x = 0; x < num_qd; x++) {
947 qd = qda[x];
948 offset = qd2offset(qd);
949 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
950 if (error)
951 goto out_end_trans;
952
953 do_qc(qd, -qd->qd_change_sync);
954 set_bit(QDF_REFRESH, &qd->qd_flags);
955 }
956
957 error = 0;
958
959out_end_trans:
960 gfs2_trans_end(sdp);
961out_ipres:
962 gfs2_inplace_release(ip);
963out_alloc:
964 gfs2_glock_dq_uninit(&i_gh);
965out_dq:
966 while (qx--)
967 gfs2_glock_dq_uninit(&ghs[qx]);
968 inode_unlock(&ip->i_inode);
969 kfree(ghs);
970 gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl,
971 GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
972out:
973 gfs2_qa_put(ip);
974 return error;
975}
976
977static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
978{
979 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
980 struct gfs2_quota q;
981 struct gfs2_quota_lvb *qlvb;
982 loff_t pos;
983 int error;
984
985 memset(&q, 0, sizeof(struct gfs2_quota));
986 pos = qd2offset(qd);
987 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
988 if (error < 0)
989 return error;
990
991 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
992 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
993 qlvb->__pad = 0;
994 qlvb->qb_limit = q.qu_limit;
995 qlvb->qb_warn = q.qu_warn;
996 qlvb->qb_value = q.qu_value;
997 qd->qd_qb = *qlvb;
998
999 return 0;
1000}
1001
1002static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
1003 struct gfs2_holder *q_gh)
1004{
1005 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
1006 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1007 struct gfs2_holder i_gh;
1008 int error;
1009
1010restart:
1011 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
1012 if (error)
1013 return error;
1014
1015 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
1016 force_refresh = FORCE;
1017
1018 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1019
1020 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
1021 gfs2_glock_dq_uninit(q_gh);
1022 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
1023 GL_NOCACHE, q_gh);
1024 if (error)
1025 return error;
1026
1027 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1028 if (error)
1029 goto fail;
1030
1031 error = update_qd(sdp, qd);
1032 if (error)
1033 goto fail_gunlock;
1034
1035 gfs2_glock_dq_uninit(&i_gh);
1036 gfs2_glock_dq_uninit(q_gh);
1037 force_refresh = 0;
1038 goto restart;
1039 }
1040
1041 return 0;
1042
1043fail_gunlock:
1044 gfs2_glock_dq_uninit(&i_gh);
1045fail:
1046 gfs2_glock_dq_uninit(q_gh);
1047 return error;
1048}
1049
1050int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
1051{
1052 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1053 struct gfs2_quota_data *qd;
1054 u32 x;
1055 int error = 0;
1056
1057 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1058 return 0;
1059
1060 error = gfs2_quota_hold(ip, uid, gid);
1061 if (error)
1062 return error;
1063
1064 sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num,
1065 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
1066
1067 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1068 qd = ip->i_qadata->qa_qd[x];
1069 error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]);
1070 if (error)
1071 break;
1072 }
1073
1074 if (!error)
1075 set_bit(GIF_QD_LOCKED, &ip->i_flags);
1076 else {
1077 while (x--)
1078 gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
1079 gfs2_quota_unhold(ip);
1080 }
1081
1082 return error;
1083}
1084
1085static int need_sync(struct gfs2_quota_data *qd)
1086{
1087 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
1088 struct gfs2_tune *gt = &sdp->sd_tune;
1089 s64 value;
1090 unsigned int num, den;
1091 int do_sync = 1;
1092
1093 if (!qd->qd_qb.qb_limit)
1094 return 0;
1095
1096 spin_lock(&qd_lock);
1097 value = qd->qd_change;
1098 spin_unlock(&qd_lock);
1099
1100 spin_lock(>->gt_spin);
1101 num = gt->gt_quota_scale_num;
1102 den = gt->gt_quota_scale_den;
1103 spin_unlock(>->gt_spin);
1104
1105 if (value < 0)
1106 do_sync = 0;
1107 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1108 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1109 do_sync = 0;
1110 else {
1111 value *= gfs2_jindex_size(sdp) * num;
1112 value = div_s64(value, den);
1113 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
1114 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1115 do_sync = 0;
1116 }
1117
1118 return do_sync;
1119}
1120
1121void gfs2_quota_unlock(struct gfs2_inode *ip)
1122{
1123 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1124 struct gfs2_quota_data *qda[4];
1125 unsigned int count = 0;
1126 u32 x;
1127 int found;
1128
1129 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1130 return;
1131
1132 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1133 struct gfs2_quota_data *qd;
1134 int sync;
1135
1136 qd = ip->i_qadata->qa_qd[x];
1137 sync = need_sync(qd);
1138
1139 gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
1140 if (!sync)
1141 continue;
1142
1143 spin_lock(&qd_lock);
1144 found = qd_check_sync(sdp, qd, NULL);
1145 spin_unlock(&qd_lock);
1146
1147 if (!found)
1148 continue;
1149
1150 gfs2_assert_warn(sdp, qd->qd_change_sync);
1151 if (bh_get(qd)) {
1152 clear_bit(QDF_LOCKED, &qd->qd_flags);
1153 slot_put(qd);
1154 qd_put(qd);
1155 continue;
1156 }
1157
1158 qda[count++] = qd;
1159 }
1160
1161 if (count) {
1162 do_sync(count, qda);
1163 for (x = 0; x < count; x++)
1164 qd_unlock(qda[x]);
1165 }
1166
1167 gfs2_quota_unhold(ip);
1168}
1169
1170#define MAX_LINE 256
1171
1172static int print_message(struct gfs2_quota_data *qd, char *type)
1173{
1174 struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
1175
1176 fs_info(sdp, "quota %s for %s %u\n",
1177 type,
1178 (qd->qd_id.type == USRQUOTA) ? "user" : "group",
1179 from_kqid(&init_user_ns, qd->qd_id));
1180
1181 return 0;
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
1202 struct gfs2_alloc_parms *ap)
1203{
1204 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1205 struct gfs2_quota_data *qd;
1206 s64 value, warn, limit;
1207 u32 x;
1208 int error = 0;
1209
1210 ap->allowed = UINT_MAX;
1211 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1212 return 0;
1213
1214 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1215 qd = ip->i_qadata->qa_qd[x];
1216
1217 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1218 qid_eq(qd->qd_id, make_kqid_gid(gid))))
1219 continue;
1220
1221 warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
1222 limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
1223 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
1224 spin_lock(&qd_lock);
1225 value += qd->qd_change;
1226 spin_unlock(&qd_lock);
1227
1228 if (limit > 0 && (limit - value) < ap->allowed)
1229 ap->allowed = limit - value;
1230
1231 if (limit && limit < (value + (s64)ap->target)) {
1232
1233
1234 if (!ap->min_target || ap->min_target > ap->allowed) {
1235 if (!test_and_set_bit(QDF_QMSG_QUIET,
1236 &qd->qd_flags)) {
1237 print_message(qd, "exceeded");
1238 quota_send_warning(qd->qd_id,
1239 sdp->sd_vfs->s_dev,
1240 QUOTA_NL_BHARDWARN);
1241 }
1242 error = -EDQUOT;
1243 break;
1244 }
1245 } else if (warn && warn < value &&
1246 time_after_eq(jiffies, qd->qd_last_warn +
1247 gfs2_tune_get(sdp, gt_quota_warn_period)
1248 * HZ)) {
1249 quota_send_warning(qd->qd_id,
1250 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
1251 error = print_message(qd, "warning");
1252 qd->qd_last_warn = jiffies;
1253 }
1254 }
1255 return error;
1256}
1257
1258void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1259 kuid_t uid, kgid_t gid)
1260{
1261 struct gfs2_quota_data *qd;
1262 u32 x;
1263 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1264
1265 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON ||
1266 gfs2_assert_warn(sdp, change))
1267 return;
1268 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
1269 return;
1270
1271 if (gfs2_assert_withdraw(sdp, ip->i_qadata &&
1272 ip->i_qadata->qa_ref > 0))
1273 return;
1274 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1275 qd = ip->i_qadata->qa_qd[x];
1276
1277 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1278 qid_eq(qd->qd_id, make_kqid_gid(gid))) {
1279 do_qc(qd, change);
1280 }
1281 }
1282}
1283
1284int gfs2_quota_sync(struct super_block *sb, int type)
1285{
1286 struct gfs2_sbd *sdp = sb->s_fs_info;
1287 struct gfs2_quota_data **qda;
1288 unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
1289 unsigned int num_qd;
1290 unsigned int x;
1291 int error = 0;
1292
1293 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1294 if (!qda)
1295 return -ENOMEM;
1296
1297 mutex_lock(&sdp->sd_quota_sync_mutex);
1298 sdp->sd_quota_sync_gen++;
1299
1300 do {
1301 num_qd = 0;
1302
1303 for (;;) {
1304 error = qd_fish(sdp, qda + num_qd);
1305 if (error || !qda[num_qd])
1306 break;
1307 if (++num_qd == max_qd)
1308 break;
1309 }
1310
1311 if (num_qd) {
1312 if (!error)
1313 error = do_sync(num_qd, qda);
1314 if (!error)
1315 for (x = 0; x < num_qd; x++)
1316 qda[x]->qd_sync_gen =
1317 sdp->sd_quota_sync_gen;
1318
1319 for (x = 0; x < num_qd; x++)
1320 qd_unlock(qda[x]);
1321 }
1322 } while (!error && num_qd == max_qd);
1323
1324 mutex_unlock(&sdp->sd_quota_sync_mutex);
1325 kfree(qda);
1326
1327 return error;
1328}
1329
1330int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
1331{
1332 struct gfs2_quota_data *qd;
1333 struct gfs2_holder q_gh;
1334 int error;
1335
1336 error = qd_get(sdp, qid, &qd);
1337 if (error)
1338 return error;
1339
1340 error = do_glock(qd, FORCE, &q_gh);
1341 if (!error)
1342 gfs2_glock_dq_uninit(&q_gh);
1343
1344 qd_put(qd);
1345 return error;
1346}
1347
1348int gfs2_quota_init(struct gfs2_sbd *sdp)
1349{
1350 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1351 u64 size = i_size_read(sdp->sd_qc_inode);
1352 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
1353 unsigned int x, slot = 0;
1354 unsigned int found = 0;
1355 unsigned int hash;
1356 unsigned int bm_size;
1357 u64 dblock;
1358 u32 extlen = 0;
1359 int error;
1360
1361 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
1362 return -EIO;
1363
1364 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1365 bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
1366 bm_size *= sizeof(unsigned long);
1367 error = -ENOMEM;
1368 sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
1369 if (sdp->sd_quota_bitmap == NULL)
1370 sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
1371 __GFP_ZERO, PAGE_KERNEL);
1372 if (!sdp->sd_quota_bitmap)
1373 return error;
1374
1375 for (x = 0; x < blocks; x++) {
1376 struct buffer_head *bh;
1377 const struct gfs2_quota_change *qc;
1378 unsigned int y;
1379
1380 if (!extlen) {
1381 extlen = 32;
1382 error = gfs2_get_extent(&ip->i_inode, x, &dblock, &extlen);
1383 if (error)
1384 goto fail;
1385 }
1386 error = -EIO;
1387 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1388 if (!bh)
1389 goto fail;
1390 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1391 brelse(bh);
1392 goto fail;
1393 }
1394
1395 qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
1396 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1397 y++, slot++) {
1398 struct gfs2_quota_data *qd;
1399 s64 qc_change = be64_to_cpu(qc->qc_change);
1400 u32 qc_flags = be32_to_cpu(qc->qc_flags);
1401 enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1402 USRQUOTA : GRPQUOTA;
1403 struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1404 be32_to_cpu(qc->qc_id));
1405 qc++;
1406 if (!qc_change)
1407 continue;
1408
1409 hash = gfs2_qd_hash(sdp, qc_id);
1410 qd = qd_alloc(hash, sdp, qc_id);
1411 if (qd == NULL) {
1412 brelse(bh);
1413 goto fail;
1414 }
1415
1416 set_bit(QDF_CHANGE, &qd->qd_flags);
1417 qd->qd_change = qc_change;
1418 qd->qd_slot = slot;
1419 qd->qd_slot_count = 1;
1420
1421 spin_lock(&qd_lock);
1422 BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
1423 list_add(&qd->qd_list, &sdp->sd_quota_list);
1424 atomic_inc(&sdp->sd_quota_count);
1425 spin_unlock(&qd_lock);
1426
1427 spin_lock_bucket(hash);
1428 hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
1429 spin_unlock_bucket(hash);
1430
1431 found++;
1432 }
1433
1434 brelse(bh);
1435 dblock++;
1436 extlen--;
1437 }
1438
1439 if (found)
1440 fs_info(sdp, "found %u quota changes\n", found);
1441
1442 return 0;
1443
1444fail:
1445 gfs2_quota_cleanup(sdp);
1446 return error;
1447}
1448
1449void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1450{
1451 struct list_head *head = &sdp->sd_quota_list;
1452 struct gfs2_quota_data *qd;
1453
1454 spin_lock(&qd_lock);
1455 while (!list_empty(head)) {
1456 qd = list_last_entry(head, struct gfs2_quota_data, qd_list);
1457
1458 list_del(&qd->qd_list);
1459
1460
1461 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
1462 atomic_dec(&sdp->sd_quota_count);
1463 spin_unlock(&qd_lock);
1464
1465 spin_lock_bucket(qd->qd_hash);
1466 hlist_bl_del_rcu(&qd->qd_hlist);
1467 spin_unlock_bucket(qd->qd_hash);
1468
1469 gfs2_assert_warn(sdp, !qd->qd_change);
1470 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1471 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1472
1473 gfs2_glock_put(qd->qd_gl);
1474 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
1475
1476 spin_lock(&qd_lock);
1477 }
1478 spin_unlock(&qd_lock);
1479
1480 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1481
1482 kvfree(sdp->sd_quota_bitmap);
1483 sdp->sd_quota_bitmap = NULL;
1484}
1485
1486static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1487{
1488 if (error == 0 || error == -EROFS)
1489 return;
1490 if (!gfs2_withdrawn(sdp)) {
1491 if (!cmpxchg(&sdp->sd_log_error, 0, error))
1492 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1493 wake_up(&sdp->sd_logd_waitq);
1494 }
1495}
1496
1497static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
1498 int (*fxn)(struct super_block *sb, int type),
1499 unsigned long t, unsigned long *timeo,
1500 unsigned int *new_timeo)
1501{
1502 if (t >= *timeo) {
1503 int error = fxn(sdp->sd_vfs, 0);
1504 quotad_error(sdp, msg, error);
1505 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1506 } else {
1507 *timeo -= t;
1508 }
1509}
1510
1511static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1512{
1513 struct gfs2_inode *ip;
1514
1515 while(1) {
1516 ip = NULL;
1517 spin_lock(&sdp->sd_trunc_lock);
1518 if (!list_empty(&sdp->sd_trunc_list)) {
1519 ip = list_first_entry(&sdp->sd_trunc_list,
1520 struct gfs2_inode, i_trunc_list);
1521 list_del_init(&ip->i_trunc_list);
1522 }
1523 spin_unlock(&sdp->sd_trunc_lock);
1524 if (ip == NULL)
1525 return;
1526 gfs2_glock_finish_truncate(ip);
1527 }
1528}
1529
1530void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1531 if (!sdp->sd_statfs_force_sync) {
1532 sdp->sd_statfs_force_sync = 1;
1533 wake_up(&sdp->sd_quota_wait);
1534 }
1535}
1536
1537
1538
1539
1540
1541
1542
1543
1544int gfs2_quotad(void *data)
1545{
1546 struct gfs2_sbd *sdp = data;
1547 struct gfs2_tune *tune = &sdp->sd_tune;
1548 unsigned long statfs_timeo = 0;
1549 unsigned long quotad_timeo = 0;
1550 unsigned long t = 0;
1551 DEFINE_WAIT(wait);
1552 int empty;
1553
1554 while (!kthread_should_stop()) {
1555
1556 if (gfs2_withdrawn(sdp))
1557 goto bypass;
1558
1559 if (sdp->sd_statfs_force_sync) {
1560 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1561 quotad_error(sdp, "statfs", error);
1562 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1563 }
1564 else
1565 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1566 &statfs_timeo,
1567 &tune->gt_statfs_quantum);
1568
1569
1570 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1571 "ad_timeo, &tune->gt_quota_quantum);
1572
1573
1574 quotad_check_trunc_list(sdp);
1575
1576 try_to_freeze();
1577
1578bypass:
1579 t = min(quotad_timeo, statfs_timeo);
1580
1581 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
1582 spin_lock(&sdp->sd_trunc_lock);
1583 empty = list_empty(&sdp->sd_trunc_list);
1584 spin_unlock(&sdp->sd_trunc_lock);
1585 if (empty && !sdp->sd_statfs_force_sync)
1586 t -= schedule_timeout(t);
1587 else
1588 t = 0;
1589 finish_wait(&sdp->sd_quota_wait, &wait);
1590 }
1591
1592 return 0;
1593}
1594
1595static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state)
1596{
1597 struct gfs2_sbd *sdp = sb->s_fs_info;
1598
1599 memset(state, 0, sizeof(*state));
1600
1601 switch (sdp->sd_args.ar_quota) {
1602 case GFS2_QUOTA_ON:
1603 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
1604 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
1605
1606 case GFS2_QUOTA_ACCOUNT:
1607 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
1608 QCI_SYSFILE;
1609 state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED |
1610 QCI_SYSFILE;
1611 break;
1612 case GFS2_QUOTA_OFF:
1613 break;
1614 }
1615 if (sdp->sd_quota_inode) {
1616 state->s_state[USRQUOTA].ino =
1617 GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1618 state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks;
1619 }
1620 state->s_state[USRQUOTA].nextents = 1;
1621 state->s_state[GRPQUOTA] = state->s_state[USRQUOTA];
1622 state->s_incoredqs = list_lru_count(&gfs2_qd_lru);
1623 return 0;
1624}
1625
1626static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
1627 struct qc_dqblk *fdq)
1628{
1629 struct gfs2_sbd *sdp = sb->s_fs_info;
1630 struct gfs2_quota_lvb *qlvb;
1631 struct gfs2_quota_data *qd;
1632 struct gfs2_holder q_gh;
1633 int error;
1634
1635 memset(fdq, 0, sizeof(*fdq));
1636
1637 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1638 return -ESRCH;
1639
1640 if ((qid.type != USRQUOTA) &&
1641 (qid.type != GRPQUOTA))
1642 return -EINVAL;
1643
1644 error = qd_get(sdp, qid, &qd);
1645 if (error)
1646 return error;
1647 error = do_glock(qd, FORCE, &q_gh);
1648 if (error)
1649 goto out;
1650
1651 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1652 fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift;
1653 fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift;
1654 fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift;
1655
1656 gfs2_glock_dq_uninit(&q_gh);
1657out:
1658 qd_put(qd);
1659 return error;
1660}
1661
1662
1663#define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE)
1664
1665static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
1666 struct qc_dqblk *fdq)
1667{
1668 struct gfs2_sbd *sdp = sb->s_fs_info;
1669 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1670 struct gfs2_quota_data *qd;
1671 struct gfs2_holder q_gh, i_gh;
1672 unsigned int data_blocks, ind_blocks;
1673 unsigned int blocks = 0;
1674 int alloc_required;
1675 loff_t offset;
1676 int error;
1677
1678 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1679 return -ESRCH;
1680
1681 if ((qid.type != USRQUOTA) &&
1682 (qid.type != GRPQUOTA))
1683 return -EINVAL;
1684
1685 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1686 return -EINVAL;
1687
1688 error = qd_get(sdp, qid, &qd);
1689 if (error)
1690 return error;
1691
1692 error = gfs2_qa_get(ip);
1693 if (error)
1694 goto out_put;
1695
1696 inode_lock(&ip->i_inode);
1697 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1698 if (error)
1699 goto out_unlockput;
1700 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1701 if (error)
1702 goto out_q;
1703
1704
1705 error = update_qd(sdp, qd);
1706 if (error)
1707 goto out_i;
1708
1709
1710 if ((fdq->d_fieldmask & QC_SPC_SOFT) &&
1711 ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
1712 fdq->d_fieldmask ^= QC_SPC_SOFT;
1713
1714 if ((fdq->d_fieldmask & QC_SPC_HARD) &&
1715 ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
1716 fdq->d_fieldmask ^= QC_SPC_HARD;
1717
1718 if ((fdq->d_fieldmask & QC_SPACE) &&
1719 ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1720 fdq->d_fieldmask ^= QC_SPACE;
1721
1722 if (fdq->d_fieldmask == 0)
1723 goto out_i;
1724
1725 offset = qd2offset(qd);
1726 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
1727 if (gfs2_is_stuffed(ip))
1728 alloc_required = 1;
1729 if (alloc_required) {
1730 struct gfs2_alloc_parms ap = { .aflags = 0, };
1731 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1732 &data_blocks, &ind_blocks);
1733 blocks = 1 + data_blocks + ind_blocks;
1734 ap.target = blocks;
1735 error = gfs2_inplace_reserve(ip, &ap);
1736 if (error)
1737 goto out_i;
1738 blocks += gfs2_rg_blocks(ip, blocks);
1739 }
1740
1741
1742
1743 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
1744 if (error)
1745 goto out_release;
1746
1747
1748 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1749 if (!error)
1750 clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
1751
1752 gfs2_trans_end(sdp);
1753out_release:
1754 if (alloc_required)
1755 gfs2_inplace_release(ip);
1756out_i:
1757 gfs2_glock_dq_uninit(&i_gh);
1758out_q:
1759 gfs2_glock_dq_uninit(&q_gh);
1760out_unlockput:
1761 gfs2_qa_put(ip);
1762 inode_unlock(&ip->i_inode);
1763out_put:
1764 qd_put(qd);
1765 return error;
1766}
1767
1768const struct quotactl_ops gfs2_quotactl_ops = {
1769 .quota_sync = gfs2_quota_sync,
1770 .get_state = gfs2_quota_get_state,
1771 .get_dqblk = gfs2_get_dqblk,
1772 .set_dqblk = gfs2_set_dqblk,
1773};
1774
1775void __init gfs2_quota_hash_init(void)
1776{
1777 unsigned i;
1778
1779 for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
1780 INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
1781}
1782