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