]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/gfs2/quota.c
[GFS2] Readpages support
[mv-sheeva.git] / fs / gfs2 / quota.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
4  *
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 v.2.
8  */
9
10 /*
11  * Quota change tags are associated with each transaction that allocates or
12  * deallocates space.  Those changes are accumulated locally to each node (in a
13  * per-node file) and then are periodically synced to the quota file.  This
14  * avoids the bottleneck of constantly touching the quota file, but introduces
15  * fuzziness in the current usage value of IDs that are being used on different
16  * nodes in the cluster simultaneously.  So, it is possible for a user on
17  * multiple nodes to overrun their quota, but that overrun is controlable.
18  * Since quota tags are part of transactions, there is no need to a quota check
19  * program to be run on node crashes or anything like that.
20  *
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.
34  *
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.
37  */
38
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/tty.h>
45 #include <linux/sort.h>
46 #include <linux/fs.h>
47 #include <linux/gfs2_ondisk.h>
48 #include <asm/semaphore.h>
49
50 #include "gfs2.h"
51 #include "lm_interface.h"
52 #include "incore.h"
53 #include "bmap.h"
54 #include "glock.h"
55 #include "glops.h"
56 #include "log.h"
57 #include "lvb.h"
58 #include "meta_io.h"
59 #include "quota.h"
60 #include "rgrp.h"
61 #include "super.h"
62 #include "trans.h"
63 #include "inode.h"
64 #include "ops_file.h"
65 #include "ops_address.h"
66 #include "util.h"
67
68 #define QUOTA_USER 1
69 #define QUOTA_GROUP 0
70
71 static uint64_t qd2offset(struct gfs2_quota_data *qd)
72 {
73         uint64_t offset;
74
75         offset = 2 * (uint64_t)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
76         offset *= sizeof(struct gfs2_quota);
77
78         return offset;
79 }
80
81 static int qd_alloc(struct gfs2_sbd *sdp, int user, uint32_t id,
82                     struct gfs2_quota_data **qdp)
83 {
84         struct gfs2_quota_data *qd;
85         int error;
86
87         qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
88         if (!qd)
89                 return -ENOMEM;
90
91         qd->qd_count = 1;
92         qd->qd_id = id;
93         if (user)
94                 set_bit(QDF_USER, &qd->qd_flags);
95         qd->qd_slot = -1;
96
97         error = gfs2_glock_get(sdp, 2 * (uint64_t)id + !user,
98                               &gfs2_quota_glops, CREATE, &qd->qd_gl);
99         if (error)
100                 goto fail;
101
102         error = gfs2_lvb_hold(qd->qd_gl);
103         gfs2_glock_put(qd->qd_gl);
104         if (error)
105                 goto fail;
106
107         *qdp = qd;
108
109         return 0;
110
111  fail:
112         kfree(qd);
113         return error;
114 }
115
116 static int qd_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
117                   struct gfs2_quota_data **qdp)
118 {
119         struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
120         int error, found;
121
122         *qdp = NULL;
123
124         for (;;) {
125                 found = 0;
126                 spin_lock(&sdp->sd_quota_spin);
127                 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
128                         if (qd->qd_id == id &&
129                             !test_bit(QDF_USER, &qd->qd_flags) == !user) {
130                                 qd->qd_count++;
131                                 found = 1;
132                                 break;
133                         }
134                 }
135
136                 if (!found)
137                         qd = NULL;
138
139                 if (!qd && new_qd) {
140                         qd = new_qd;
141                         list_add(&qd->qd_list, &sdp->sd_quota_list);
142                         atomic_inc(&sdp->sd_quota_count);
143                         new_qd = NULL;
144                 }
145
146                 spin_unlock(&sdp->sd_quota_spin);
147
148                 if (qd || !create) {
149                         if (new_qd) {
150                                 gfs2_lvb_unhold(new_qd->qd_gl);
151                                 kfree(new_qd);
152                         }
153                         *qdp = qd;
154                         return 0;
155                 }
156
157                 error = qd_alloc(sdp, user, id, &new_qd);
158                 if (error)
159                         return error;
160         }
161 }
162
163 static void qd_hold(struct gfs2_quota_data *qd)
164 {
165         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
166
167         spin_lock(&sdp->sd_quota_spin);
168         gfs2_assert(sdp, qd->qd_count);
169         qd->qd_count++;
170         spin_unlock(&sdp->sd_quota_spin);
171 }
172
173 static void qd_put(struct gfs2_quota_data *qd)
174 {
175         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
176         spin_lock(&sdp->sd_quota_spin);
177         gfs2_assert(sdp, qd->qd_count);
178         if (!--qd->qd_count)
179                 qd->qd_last_touched = jiffies;
180         spin_unlock(&sdp->sd_quota_spin);
181 }
182
183 static int slot_get(struct gfs2_quota_data *qd)
184 {
185         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
186         unsigned int c, o = 0, b;
187         unsigned char byte = 0;
188
189         spin_lock(&sdp->sd_quota_spin);
190
191         if (qd->qd_slot_count++) {
192                 spin_unlock(&sdp->sd_quota_spin);
193                 return 0;
194         }
195
196         for (c = 0; c < sdp->sd_quota_chunks; c++)
197                 for (o = 0; o < PAGE_SIZE; o++) {
198                         byte = sdp->sd_quota_bitmap[c][o];
199                         if (byte != 0xFF)
200                                 goto found;
201                 }
202
203         goto fail;
204
205  found:
206         for (b = 0; b < 8; b++)
207                 if (!(byte & (1 << b)))
208                         break;
209         qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
210
211         if (qd->qd_slot >= sdp->sd_quota_slots)
212                 goto fail;
213
214         sdp->sd_quota_bitmap[c][o] |= 1 << b;
215
216         spin_unlock(&sdp->sd_quota_spin);
217
218         return 0;
219
220  fail:
221         qd->qd_slot_count--;
222         spin_unlock(&sdp->sd_quota_spin);
223         return -ENOSPC;
224 }
225
226 static void slot_hold(struct gfs2_quota_data *qd)
227 {
228         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
229
230         spin_lock(&sdp->sd_quota_spin);
231         gfs2_assert(sdp, qd->qd_slot_count);
232         qd->qd_slot_count++;
233         spin_unlock(&sdp->sd_quota_spin);
234 }
235
236 static void slot_put(struct gfs2_quota_data *qd)
237 {
238         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
239
240         spin_lock(&sdp->sd_quota_spin);
241         gfs2_assert(sdp, qd->qd_slot_count);
242         if (!--qd->qd_slot_count) {
243                 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
244                 qd->qd_slot = -1;
245         }
246         spin_unlock(&sdp->sd_quota_spin);
247 }
248
249 static int bh_get(struct gfs2_quota_data *qd)
250 {
251         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
252         struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
253         unsigned int block, offset;
254         uint64_t dblock;
255         int new = 0;
256         struct buffer_head *bh;
257         int error;
258         int boundary;
259
260         mutex_lock(&sdp->sd_quota_mutex);
261
262         if (qd->qd_bh_count++) {
263                 mutex_unlock(&sdp->sd_quota_mutex);
264                 return 0;
265         }
266
267         block = qd->qd_slot / sdp->sd_qc_per_block;
268         offset = qd->qd_slot % sdp->sd_qc_per_block;;
269
270         error = gfs2_block_map(ip->i_vnode, block, &new, &dblock, &boundary);
271         if (error)
272                 goto fail;
273         error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT, &bh);
274         if (error)
275                 goto fail;
276         error = -EIO;
277         if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
278                 goto fail_brelse;
279
280         qd->qd_bh = bh;
281         qd->qd_bh_qc = (struct gfs2_quota_change *)
282                 (bh->b_data + sizeof(struct gfs2_meta_header) +
283                  offset * sizeof(struct gfs2_quota_change));
284
285         mutex_lock(&sdp->sd_quota_mutex);
286
287         return 0;
288
289  fail_brelse:
290         brelse(bh);
291
292  fail:
293         qd->qd_bh_count--;
294         mutex_unlock(&sdp->sd_quota_mutex);
295         return error;
296 }
297
298 static void bh_put(struct gfs2_quota_data *qd)
299 {
300         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
301
302         mutex_lock(&sdp->sd_quota_mutex);
303         gfs2_assert(sdp, qd->qd_bh_count);
304         if (!--qd->qd_bh_count) {
305                 brelse(qd->qd_bh);
306                 qd->qd_bh = NULL;
307                 qd->qd_bh_qc = NULL;
308         }
309         mutex_unlock(&sdp->sd_quota_mutex);
310 }
311
312 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
313 {
314         struct gfs2_quota_data *qd = NULL;
315         int error;
316         int found = 0;
317
318         *qdp = NULL;
319
320         if (sdp->sd_vfs->s_flags & MS_RDONLY)
321                 return 0;
322
323         spin_lock(&sdp->sd_quota_spin);
324
325         list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
326                 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
327                     !test_bit(QDF_CHANGE, &qd->qd_flags) ||
328                     qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
329                         continue;
330
331                 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
332
333                 set_bit(QDF_LOCKED, &qd->qd_flags);
334                 gfs2_assert_warn(sdp, qd->qd_count);
335                 qd->qd_count++;
336                 qd->qd_change_sync = qd->qd_change;
337                 gfs2_assert_warn(sdp, qd->qd_slot_count);
338                 qd->qd_slot_count++;
339                 found = 1;
340
341                 break;
342         }
343
344         if (!found)
345                 qd = NULL;
346
347         spin_unlock(&sdp->sd_quota_spin);
348
349         if (qd) {
350                 gfs2_assert_warn(sdp, qd->qd_change_sync);
351                 error = bh_get(qd);
352                 if (error) {
353                         clear_bit(QDF_LOCKED, &qd->qd_flags);
354                         slot_put(qd);
355                         qd_put(qd);
356                         return error;
357                 }
358         }
359
360         *qdp = qd;
361
362         return 0;
363 }
364
365 static int qd_trylock(struct gfs2_quota_data *qd)
366 {
367         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
368
369         if (sdp->sd_vfs->s_flags & MS_RDONLY)
370                 return 0;
371
372         spin_lock(&sdp->sd_quota_spin);
373
374         if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
375             !test_bit(QDF_CHANGE, &qd->qd_flags)) {
376                 spin_unlock(&sdp->sd_quota_spin);
377                 return 0;
378         }
379
380         list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
381
382         set_bit(QDF_LOCKED, &qd->qd_flags);
383         gfs2_assert_warn(sdp, qd->qd_count);
384         qd->qd_count++;
385         qd->qd_change_sync = qd->qd_change;
386         gfs2_assert_warn(sdp, qd->qd_slot_count);
387         qd->qd_slot_count++;
388
389         spin_unlock(&sdp->sd_quota_spin);
390
391         gfs2_assert_warn(sdp, qd->qd_change_sync);
392         if (bh_get(qd)) {
393                 clear_bit(QDF_LOCKED, &qd->qd_flags);
394                 slot_put(qd);
395                 qd_put(qd);
396                 return 0;
397         }
398
399         return 1;
400 }
401
402 static void qd_unlock(struct gfs2_quota_data *qd)
403 {
404         gfs2_assert_warn(qd->qd_gl->gl_sbd,
405                          test_bit(QDF_LOCKED, &qd->qd_flags));
406         clear_bit(QDF_LOCKED, &qd->qd_flags);
407         bh_put(qd);
408         slot_put(qd);
409         qd_put(qd);
410 }
411
412 static int qdsb_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
413                     struct gfs2_quota_data **qdp)
414 {
415         int error;
416
417         error = qd_get(sdp, user, id, create, qdp);
418         if (error)
419                 return error;
420
421         error = slot_get(*qdp);
422         if (error)
423                 goto fail;
424
425         error = bh_get(*qdp);
426         if (error)
427                 goto fail_slot;
428
429         return 0;
430
431  fail_slot:
432         slot_put(*qdp);
433
434  fail:
435         qd_put(*qdp);
436         return error;
437 }
438
439 static void qdsb_put(struct gfs2_quota_data *qd)
440 {
441         bh_put(qd);
442         slot_put(qd);
443         qd_put(qd);
444 }
445
446 int gfs2_quota_hold(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
447 {
448         struct gfs2_sbd *sdp = ip->i_sbd;
449         struct gfs2_alloc *al = &ip->i_alloc;
450         struct gfs2_quota_data **qd = al->al_qd;
451         int error;
452
453         if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
454             gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
455                 return -EIO;
456
457         if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
458                 return 0;
459
460         error = qdsb_get(sdp, QUOTA_USER, ip->i_di.di_uid, CREATE, qd);
461         if (error)
462                 goto out;
463         al->al_qd_num++;
464         qd++;
465
466         error = qdsb_get(sdp, QUOTA_GROUP, ip->i_di.di_gid, CREATE, qd);
467         if (error)
468                 goto out;
469         al->al_qd_num++;
470         qd++;
471
472         if (uid != NO_QUOTA_CHANGE && uid != ip->i_di.di_uid) {
473                 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
474                 if (error)
475                         goto out;
476                 al->al_qd_num++;
477                 qd++;
478         }
479
480         if (gid != NO_QUOTA_CHANGE && gid != ip->i_di.di_gid) {
481                 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
482                 if (error)
483                         goto out;
484                 al->al_qd_num++;
485                 qd++;
486         }
487
488  out:
489         if (error)
490                 gfs2_quota_unhold(ip);
491
492         return error;
493 }
494
495 void gfs2_quota_unhold(struct gfs2_inode *ip)
496 {
497         struct gfs2_sbd *sdp = ip->i_sbd;
498         struct gfs2_alloc *al = &ip->i_alloc;
499         unsigned int x;
500
501         gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
502
503         for (x = 0; x < al->al_qd_num; x++) {
504                 qdsb_put(al->al_qd[x]);
505                 al->al_qd[x] = NULL;
506         }
507         al->al_qd_num = 0;
508 }
509
510 static int sort_qd(const void *a, const void *b)
511 {
512         struct gfs2_quota_data *qd_a = *(struct gfs2_quota_data **)a;
513         struct gfs2_quota_data *qd_b = *(struct gfs2_quota_data **)b;
514         int ret = 0;
515
516         if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
517             !test_bit(QDF_USER, &qd_b->qd_flags)) {
518                 if (test_bit(QDF_USER, &qd_a->qd_flags))
519                         ret = -1;
520                 else
521                         ret = 1;
522         } else {
523                 if (qd_a->qd_id < qd_b->qd_id)
524                         ret = -1;
525                 else if (qd_a->qd_id > qd_b->qd_id)
526                         ret = 1;
527         }
528
529         return ret;
530 }
531
532 static void do_qc(struct gfs2_quota_data *qd, int64_t change)
533 {
534         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
535         struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
536         struct gfs2_quota_change *qc = qd->qd_bh_qc;
537         int64_t x;
538
539         mutex_lock(&sdp->sd_quota_mutex);
540         gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
541
542         if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
543                 qc->qc_change = 0;
544                 qc->qc_flags = 0;
545                 if (test_bit(QDF_USER, &qd->qd_flags))
546                         qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
547                 qc->qc_id = cpu_to_be32(qd->qd_id);
548         }
549
550         x = qc->qc_change;
551         x = be64_to_cpu(x) + change;
552         qc->qc_change = cpu_to_be64(x);
553
554         spin_lock(&sdp->sd_quota_spin);
555         qd->qd_change = x;
556         spin_unlock(&sdp->sd_quota_spin);
557
558         if (!x) {
559                 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
560                 clear_bit(QDF_CHANGE, &qd->qd_flags);
561                 qc->qc_flags = 0;
562                 qc->qc_id = 0;
563                 slot_put(qd);
564                 qd_put(qd);
565         } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
566                 qd_hold(qd);
567                 slot_hold(qd);
568         }
569                         
570         mutex_unlock(&sdp->sd_quota_mutex);
571 }
572
573 /**
574  * gfs2_adjust_quota
575  *
576  * This function was mostly borrowed from gfs2_block_truncate_page which was
577  * in turn mostly borrowed from ext3
578  */
579 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
580                              int64_t change, struct gfs2_quota_data *qd)
581 {
582         struct inode *inode = ip->i_vnode;
583         struct address_space *mapping = inode->i_mapping;
584         unsigned long index = loc >> PAGE_CACHE_SHIFT;
585         unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
586         unsigned blocksize, iblock, pos;
587         struct buffer_head *bh;
588         struct page *page;
589         void *kaddr;
590         __be64 *ptr;
591         u64 value;
592         int err = -EIO;
593
594         page = grab_cache_page(mapping, index);
595         if (!page)
596                 return -ENOMEM;
597
598         blocksize = inode->i_sb->s_blocksize;
599         iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
600
601         if (!page_has_buffers(page))
602                 create_empty_buffers(page, blocksize, 0);
603
604         bh = page_buffers(page);
605         pos = blocksize;
606         while (offset >= pos) {
607                 bh = bh->b_this_page;
608                 iblock++;
609                 pos += blocksize;
610         }
611
612         if (!buffer_mapped(bh)) {
613                 gfs2_get_block(inode, iblock, bh, 1);
614                 if (!buffer_mapped(bh))
615                         goto unlock;
616         }
617
618         if (PageUptodate(page))
619                 set_buffer_uptodate(bh);
620
621         if (!buffer_uptodate(bh)) {
622                 ll_rw_block(READ, 1, &bh);
623                 wait_on_buffer(bh);
624                 if (!buffer_uptodate(bh))
625                         goto unlock;
626         }
627
628         gfs2_trans_add_bh(ip->i_gl, bh, 0);
629
630         kaddr = kmap_atomic(page, KM_USER0);
631         ptr = (__be64 *)(kaddr + offset);
632         value = *ptr = cpu_to_be64(be64_to_cpu(*ptr) + change);
633         flush_dcache_page(page);
634         kunmap_atomic(kaddr, KM_USER0);
635         err = 0;
636         qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
637 #if 0
638         qd->qd_qb.qb_limit = cpu_to_be64(q.qu_limit);
639         qd->qd_qb.qb_warn = cpu_to_be64(q.qu_warn);
640 #endif
641         qd->qd_qb.qb_value = cpu_to_be64(value);
642 unlock:
643         unlock_page(page);
644         page_cache_release(page);
645         return err;
646 }
647
648 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
649 {
650         struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
651         struct gfs2_inode *ip = sdp->sd_quota_inode->u.generic_ip;
652         unsigned int data_blocks, ind_blocks;
653         struct file_ra_state ra_state;
654         struct gfs2_holder *ghs, i_gh;
655         unsigned int qx, x;
656         struct gfs2_quota_data *qd;
657         loff_t offset;
658         unsigned int nalloc = 0;
659         struct gfs2_alloc *al = NULL;
660         int error;
661
662         gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
663                               &data_blocks, &ind_blocks);
664
665         ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
666         if (!ghs)
667                 return -ENOMEM;
668
669         sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
670         for (qx = 0; qx < num_qd; qx++) {
671                 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
672                                            LM_ST_EXCLUSIVE,
673                                            GL_NOCACHE, &ghs[qx]);
674                 if (error)
675                         goto out;
676         }
677
678         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
679         if (error)
680                 goto out;
681
682         for (x = 0; x < num_qd; x++) {
683                 int alloc_required;
684
685                 offset = qd2offset(qda[x]);
686                 error = gfs2_write_alloc_required(ip, offset,
687                                                   sizeof(struct gfs2_quota),
688                                                   &alloc_required);
689                 if (error)
690                         goto out_gunlock;
691                 if (alloc_required)
692                         nalloc++;
693         }
694
695         if (nalloc) {
696                 al = gfs2_alloc_get(ip);
697
698                 al->al_requested = nalloc * (data_blocks + ind_blocks);
699
700                 error = gfs2_inplace_reserve(ip);
701                 if (error)
702                         goto out_alloc;
703
704                 error = gfs2_trans_begin(sdp,
705                                          al->al_rgd->rd_ri.ri_length +
706                                          num_qd * data_blocks +
707                                          nalloc * ind_blocks +
708                                          RES_DINODE + num_qd +
709                                          RES_STATFS, 0);
710                 if (error)
711                         goto out_ipres;
712         } else {
713                 error = gfs2_trans_begin(sdp,
714                                          num_qd * data_blocks +
715                                          RES_DINODE + num_qd, 0);
716                 if (error)
717                         goto out_gunlock;
718         }
719
720         file_ra_state_init(&ra_state, ip->i_vnode->i_mapping);
721         for (x = 0; x < num_qd; x++) {
722                 qd = qda[x];
723                 offset = qd2offset(qd);
724                 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
725                                           (struct gfs2_quota_data *)
726                                           qd->qd_gl->gl_lvb);
727                 if (error)
728                         goto out_end_trans;
729
730                 do_qc(qd, -qd->qd_change_sync);
731         }
732
733         error = 0;
734
735  out_end_trans:
736         gfs2_trans_end(sdp);
737
738  out_ipres:
739         if (nalloc)
740                 gfs2_inplace_release(ip);
741
742  out_alloc:
743         if (nalloc)
744                 gfs2_alloc_put(ip);
745
746  out_gunlock:
747         gfs2_glock_dq_uninit(&i_gh);
748
749  out:
750         while (qx--)
751                 gfs2_glock_dq_uninit(&ghs[qx]);
752         kfree(ghs);
753         gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
754
755         return error;
756 }
757
758 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
759                     struct gfs2_holder *q_gh)
760 {
761         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
762         struct gfs2_inode *ip = sdp->sd_quota_inode->u.generic_ip;
763         struct gfs2_holder i_gh;
764         struct gfs2_quota q;
765         char buf[sizeof(struct gfs2_quota)];
766         struct file_ra_state ra_state;
767         int error;
768
769         file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
770  restart:
771         error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
772         if (error)
773                 return error;
774
775         gfs2_quota_lvb_in(&qd->qd_qb, qd->qd_gl->gl_lvb);
776
777         if (force_refresh || qd->qd_qb.qb_magic != GFS2_MAGIC) {
778                 loff_t pos;
779                 gfs2_glock_dq_uninit(q_gh);
780                 error = gfs2_glock_nq_init(qd->qd_gl,
781                                           LM_ST_EXCLUSIVE, GL_NOCACHE,
782                                           q_gh);
783                 if (error)
784                         return error;
785
786                 error = gfs2_glock_nq_init(ip->i_gl,
787                                           LM_ST_SHARED, 0,
788                                           &i_gh);
789                 if (error)
790                         goto fail;
791
792                 memset(buf, 0, sizeof(struct gfs2_quota));
793                 pos = qd2offset(qd);
794                 error = gfs2_internal_read(ip,
795                                             &ra_state, buf,
796                                             &pos,
797                                             sizeof(struct gfs2_quota));
798                 if (error < 0)
799                         goto fail_gunlock;
800
801                 gfs2_glock_dq_uninit(&i_gh);
802
803                 gfs2_quota_in(&q, buf);
804
805                 memset(&qd->qd_qb, 0, sizeof(struct gfs2_quota_lvb));
806                 qd->qd_qb.qb_magic = GFS2_MAGIC;
807                 qd->qd_qb.qb_limit = q.qu_limit;
808                 qd->qd_qb.qb_warn = q.qu_warn;
809                 qd->qd_qb.qb_value = q.qu_value;
810
811                 gfs2_quota_lvb_out(&qd->qd_qb, qd->qd_gl->gl_lvb);
812
813                 if (gfs2_glock_is_blocking(qd->qd_gl)) {
814                         gfs2_glock_dq_uninit(q_gh);
815                         force_refresh = 0;
816                         goto restart;
817                 }
818         }
819
820         return 0;
821
822  fail_gunlock:
823         gfs2_glock_dq_uninit(&i_gh);
824
825  fail:
826         gfs2_glock_dq_uninit(q_gh);
827
828         return error;
829 }
830
831 int gfs2_quota_lock(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
832 {
833         struct gfs2_sbd *sdp = ip->i_sbd;
834         struct gfs2_alloc *al = &ip->i_alloc;
835         unsigned int x;
836         int error = 0;
837
838         gfs2_quota_hold(ip, uid, gid);
839
840         if (capable(CAP_SYS_RESOURCE) ||
841             sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
842                 return 0;
843
844         sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
845              sort_qd, NULL);
846
847         for (x = 0; x < al->al_qd_num; x++) {
848                 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
849                 if (error)
850                         break;
851         }
852
853         if (!error)
854                 set_bit(GIF_QD_LOCKED, &ip->i_flags);
855         else {
856                 while (x--)
857                         gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
858                 gfs2_quota_unhold(ip);
859         }
860
861         return error;
862 }
863
864 static int need_sync(struct gfs2_quota_data *qd)
865 {
866         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
867         struct gfs2_tune *gt = &sdp->sd_tune;
868         int64_t value;
869         unsigned int num, den;
870         int do_sync = 1;
871
872         if (!qd->qd_qb.qb_limit)
873                 return 0;
874
875         spin_lock(&sdp->sd_quota_spin);
876         value = qd->qd_change;
877         spin_unlock(&sdp->sd_quota_spin);
878
879         spin_lock(&gt->gt_spin);
880         num = gt->gt_quota_scale_num;
881         den = gt->gt_quota_scale_den;
882         spin_unlock(&gt->gt_spin);
883
884         if (value < 0)
885                 do_sync = 0;
886         else if (qd->qd_qb.qb_value >= (int64_t)qd->qd_qb.qb_limit)
887                 do_sync = 0;
888         else {
889                 value *= gfs2_jindex_size(sdp) * num;
890                 do_div(value, den);
891                 value += qd->qd_qb.qb_value;
892                 if (value < (int64_t)qd->qd_qb.qb_limit)
893                         do_sync = 0;
894         }
895
896         return do_sync;
897 }
898
899 void gfs2_quota_unlock(struct gfs2_inode *ip)
900 {
901         struct gfs2_alloc *al = &ip->i_alloc;
902         struct gfs2_quota_data *qda[4];
903         unsigned int count = 0;
904         unsigned int x;
905
906         if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
907                 goto out;
908
909         for (x = 0; x < al->al_qd_num; x++) {
910                 struct gfs2_quota_data *qd;
911                 int sync;
912
913                 qd = al->al_qd[x];
914                 sync = need_sync(qd);
915
916                 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
917
918                 if (sync && qd_trylock(qd))
919                         qda[count++] = qd;
920         }
921
922         if (count) {
923                 do_sync(count, qda);
924                 for (x = 0; x < count; x++)
925                         qd_unlock(qda[x]);
926         }
927
928  out:
929         gfs2_quota_unhold(ip);
930 }
931
932 #define MAX_LINE 256
933
934 static int print_message(struct gfs2_quota_data *qd, char *type)
935 {
936         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
937         char *line;
938         int len;
939
940         line = kmalloc(MAX_LINE, GFP_KERNEL);
941         if (!line)
942                 return -ENOMEM;
943
944         len = snprintf(line, MAX_LINE-1,
945                        "GFS2: fsid=%s: quota %s for %s %u\r\n",
946                        sdp->sd_fsname, type,
947                        (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
948                        qd->qd_id);
949         line[MAX_LINE-1] = 0;
950
951         if (current->signal) { /* Is this test still required? */
952                 tty_write_message(current->signal->tty, line);
953         }
954
955         kfree(line);
956
957         return 0;
958 }
959
960 int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
961 {
962         struct gfs2_sbd *sdp = ip->i_sbd;
963         struct gfs2_alloc *al = &ip->i_alloc;
964         struct gfs2_quota_data *qd;
965         int64_t value;
966         unsigned int x;
967         int error = 0;
968
969         if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
970                 return 0;
971
972         if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
973                 return 0;
974
975         for (x = 0; x < al->al_qd_num; x++) {
976                 qd = al->al_qd[x];
977
978                 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
979                       (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
980                         continue;
981
982                 value = qd->qd_qb.qb_value;
983                 spin_lock(&sdp->sd_quota_spin);
984                 value += qd->qd_change;
985                 spin_unlock(&sdp->sd_quota_spin);
986
987                 if (qd->qd_qb.qb_limit && (int64_t)qd->qd_qb.qb_limit < value) {
988                         print_message(qd, "exceeded");
989                         error = -EDQUOT;
990                         break;
991                 } else if (qd->qd_qb.qb_warn &&
992                            (int64_t)qd->qd_qb.qb_warn < value &&
993                            time_after_eq(jiffies, qd->qd_last_warn +
994                                          gfs2_tune_get(sdp,
995                                                 gt_quota_warn_period) * HZ)) {
996                         error = print_message(qd, "warning");
997                         qd->qd_last_warn = jiffies;
998                 }
999         }
1000
1001         return error;
1002 }
1003
1004 void gfs2_quota_change(struct gfs2_inode *ip, int64_t change,
1005                        uint32_t uid, uint32_t gid)
1006 {
1007         struct gfs2_alloc *al = &ip->i_alloc;
1008         struct gfs2_quota_data *qd;
1009         unsigned int x;
1010         unsigned int found = 0;
1011
1012         if (gfs2_assert_warn(ip->i_sbd, change))
1013                 return;
1014         if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
1015                 return;
1016
1017         for (x = 0; x < al->al_qd_num; x++) {
1018                 qd = al->al_qd[x];
1019
1020                 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1021                     (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1022                         do_qc(qd, change);
1023                         found++;
1024                 }
1025         }
1026 }
1027
1028 int gfs2_quota_sync(struct gfs2_sbd *sdp)
1029 {
1030         struct gfs2_quota_data **qda;
1031         unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1032         unsigned int num_qd;
1033         unsigned int x;
1034         int error = 0;
1035
1036         sdp->sd_quota_sync_gen++;
1037
1038         qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1039         if (!qda)
1040                 return -ENOMEM;
1041
1042         do {
1043                 num_qd = 0;
1044
1045                 for (;;) {
1046                         error = qd_fish(sdp, qda + num_qd);
1047                         if (error || !qda[num_qd])
1048                                 break;
1049                         if (++num_qd == max_qd)
1050                                 break;
1051                 }
1052
1053                 if (num_qd) {
1054                         if (!error)
1055                                 error = do_sync(num_qd, qda);
1056                         if (!error)
1057                                 for (x = 0; x < num_qd; x++)
1058                                         qda[x]->qd_sync_gen =
1059                                                 sdp->sd_quota_sync_gen;
1060
1061                         for (x = 0; x < num_qd; x++)
1062                                 qd_unlock(qda[x]);
1063                 }
1064         } while (!error && num_qd == max_qd);
1065
1066         kfree(qda);
1067
1068         return error;
1069 }
1070
1071 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
1072 {
1073         struct gfs2_quota_data *qd;
1074         struct gfs2_holder q_gh;
1075         int error;
1076
1077         error = qd_get(sdp, user, id, CREATE, &qd);
1078         if (error)
1079                 return error;
1080
1081         error = do_glock(qd, FORCE, &q_gh);
1082         if (!error)
1083                 gfs2_glock_dq_uninit(&q_gh);
1084
1085         qd_put(qd);
1086
1087         return error;
1088 }
1089
1090 #if 0
1091 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1092                     struct gfs2_quota *q)
1093 {
1094         struct gfs2_quota_data *qd;
1095         struct gfs2_holder q_gh;
1096         int error;
1097
1098         if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1099             !capable(CAP_SYS_ADMIN))
1100                 return -EACCES;
1101
1102         error = qd_get(sdp, user, id, CREATE, &qd);
1103         if (error)
1104                 return error;
1105
1106         error = do_glock(qd, NO_FORCE, &q_gh);
1107         if (error)
1108                 goto out;
1109
1110         memset(q, 0, sizeof(struct gfs2_quota));
1111         q->qu_limit = qd->qd_qb.qb_limit;
1112         q->qu_warn = qd->qd_qb.qb_warn;
1113         q->qu_value = qd->qd_qb.qb_value;
1114
1115         spin_lock(&sdp->sd_quota_spin);
1116         q->qu_value += qd->qd_change;
1117         spin_unlock(&sdp->sd_quota_spin);
1118
1119         gfs2_glock_dq_uninit(&q_gh);
1120
1121  out:
1122         qd_put(qd);
1123
1124         return error;
1125 }
1126 #endif  /*  0  */
1127
1128 int gfs2_quota_init(struct gfs2_sbd *sdp)
1129 {
1130         struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
1131         unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1132         unsigned int x, slot = 0;
1133         unsigned int found = 0;
1134         uint64_t dblock;
1135         uint32_t extlen = 0;
1136         int error;
1137
1138         if (!ip->i_di.di_size ||
1139             ip->i_di.di_size > (64 << 20) ||
1140             ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1141                 gfs2_consist_inode(ip);
1142                 return -EIO;            
1143         }
1144         sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1145         sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1146
1147         error = -ENOMEM;
1148
1149         sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1150                                        sizeof(unsigned char *), GFP_KERNEL);
1151         if (!sdp->sd_quota_bitmap)
1152                 return error;
1153
1154         for (x = 0; x < sdp->sd_quota_chunks; x++) {
1155                 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1156                 if (!sdp->sd_quota_bitmap[x])
1157                         goto fail;
1158         }
1159
1160         for (x = 0; x < blocks; x++) {
1161                 struct buffer_head *bh;
1162                 unsigned int y;
1163
1164                 if (!extlen) {
1165                         int new = 0;
1166                         error = gfs2_extent_map(ip->i_vnode, x, &new, &dblock, &extlen);
1167                         if (error)
1168                                 goto fail;
1169                 }
1170                 gfs2_meta_ra(ip->i_gl,  dblock, extlen);
1171                 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1172                                        &bh);
1173                 if (error)
1174                         goto fail;
1175                 error = -EIO;
1176                 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1177                         brelse(bh);
1178                         goto fail;
1179                 }
1180
1181                 for (y = 0;
1182                      y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1183                      y++, slot++) {
1184                         struct gfs2_quota_change qc;
1185                         struct gfs2_quota_data *qd;
1186
1187                         gfs2_quota_change_in(&qc, bh->b_data +
1188                                           sizeof(struct gfs2_meta_header) +
1189                                           y * sizeof(struct gfs2_quota_change));
1190                         if (!qc.qc_change)
1191                                 continue;
1192
1193                         error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1194                                          qc.qc_id, &qd);
1195                         if (error) {
1196                                 brelse(bh);
1197                                 goto fail;
1198                         }
1199
1200                         set_bit(QDF_CHANGE, &qd->qd_flags);
1201                         qd->qd_change = qc.qc_change;
1202                         qd->qd_slot = slot;
1203                         qd->qd_slot_count = 1;
1204                         qd->qd_last_touched = jiffies;
1205
1206                         spin_lock(&sdp->sd_quota_spin);
1207                         gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1208                         list_add(&qd->qd_list, &sdp->sd_quota_list);
1209                         atomic_inc(&sdp->sd_quota_count);
1210                         spin_unlock(&sdp->sd_quota_spin);
1211
1212                         found++;
1213                 }
1214
1215                 brelse(bh);
1216                 dblock++;
1217                 extlen--;
1218         }
1219
1220         if (found)
1221                 fs_info(sdp, "found %u quota changes\n", found);
1222
1223         return 0;
1224
1225  fail:
1226         gfs2_quota_cleanup(sdp);
1227         return error;
1228 }
1229
1230 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1231 {
1232         struct gfs2_quota_data *qd, *safe;
1233         LIST_HEAD(dead);
1234
1235         spin_lock(&sdp->sd_quota_spin);
1236         list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1237                 if (!qd->qd_count &&
1238                     time_after_eq(jiffies, qd->qd_last_touched +
1239                                 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1240                         list_move(&qd->qd_list, &dead);
1241                         gfs2_assert_warn(sdp,
1242                                          atomic_read(&sdp->sd_quota_count) > 0);
1243                         atomic_dec(&sdp->sd_quota_count);
1244                 }
1245         }
1246         spin_unlock(&sdp->sd_quota_spin);
1247
1248         while (!list_empty(&dead)) {
1249                 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1250                 list_del(&qd->qd_list);
1251
1252                 gfs2_assert_warn(sdp, !qd->qd_change);
1253                 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1254                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1255
1256                 gfs2_lvb_unhold(qd->qd_gl);
1257                 kfree(qd);
1258         }
1259 }
1260
1261 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1262 {
1263         struct list_head *head = &sdp->sd_quota_list;
1264         struct gfs2_quota_data *qd;
1265         unsigned int x;
1266
1267         spin_lock(&sdp->sd_quota_spin);
1268         while (!list_empty(head)) {
1269                 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1270
1271                 if (qd->qd_count > 1 ||
1272                     (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1273                         list_move(&qd->qd_list, head);
1274                         spin_unlock(&sdp->sd_quota_spin);
1275                         schedule();
1276                         spin_lock(&sdp->sd_quota_spin);
1277                         continue;
1278                 }
1279
1280                 list_del(&qd->qd_list);
1281                 atomic_dec(&sdp->sd_quota_count);
1282                 spin_unlock(&sdp->sd_quota_spin);
1283
1284                 if (!qd->qd_count) {
1285                         gfs2_assert_warn(sdp, !qd->qd_change);
1286                         gfs2_assert_warn(sdp, !qd->qd_slot_count);
1287                 } else
1288                         gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1289                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1290
1291                 gfs2_lvb_unhold(qd->qd_gl);
1292                 kfree(qd);
1293
1294                 spin_lock(&sdp->sd_quota_spin);
1295         }
1296         spin_unlock(&sdp->sd_quota_spin);
1297
1298         gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1299
1300         if (sdp->sd_quota_bitmap) {
1301                 for (x = 0; x < sdp->sd_quota_chunks; x++)
1302                         kfree(sdp->sd_quota_bitmap[x]);
1303                 kfree(sdp->sd_quota_bitmap);
1304         }
1305 }
1306