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