Merge branch 'master'
[linux-2.6] / fs / gfs2 / quota.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 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
49 #include "gfs2.h"
50 #include "lm_interface.h"
51 #include "incore.h"
52 #include "bmap.h"
53 #include "glock.h"
54 #include "glops.h"
55 #include "log.h"
56 #include "lvb.h"
57 #include "meta_io.h"
58 #include "quota.h"
59 #include "rgrp.h"
60 #include "super.h"
61 #include "trans.h"
62 #include "inode.h"
63 #include "ops_file.h"
64 #include "ops_address.h"
65 #include "util.h"
66
67 #define QUOTA_USER 1
68 #define QUOTA_GROUP 0
69
70 static uint64_t qd2offset(struct gfs2_quota_data *qd)
71 {
72         uint64_t offset;
73
74         offset = 2 * (uint64_t)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
75         offset *= sizeof(struct gfs2_quota);
76
77         return offset;
78 }
79
80 static int qd_alloc(struct gfs2_sbd *sdp, int user, uint32_t id,
81                     struct gfs2_quota_data **qdp)
82 {
83         struct gfs2_quota_data *qd;
84         int error;
85
86         qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
87         if (!qd)
88                 return -ENOMEM;
89
90         qd->qd_count = 1;
91         qd->qd_id = id;
92         if (user)
93                 set_bit(QDF_USER, &qd->qd_flags);
94         qd->qd_slot = -1;
95
96         error = gfs2_glock_get(sdp, 2 * (uint64_t)id + !user,
97                               &gfs2_quota_glops, CREATE, &qd->qd_gl);
98         if (error)
99                 goto fail;
100
101         error = gfs2_lvb_hold(qd->qd_gl);
102         gfs2_glock_put(qd->qd_gl);
103         if (error)
104                 goto fail;
105
106         *qdp = qd;
107
108         return 0;
109
110  fail:
111         kfree(qd);
112         return error;
113 }
114
115 static int qd_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
116                   struct gfs2_quota_data **qdp)
117 {
118         struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
119         int error, found;
120
121         *qdp = NULL;
122
123         for (;;) {
124                 found = 0;
125                 spin_lock(&sdp->sd_quota_spin);
126                 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
127                         if (qd->qd_id == id &&
128                             !test_bit(QDF_USER, &qd->qd_flags) == !user) {
129                                 qd->qd_count++;
130                                 found = 1;
131                                 break;
132                         }
133                 }
134
135                 if (!found)
136                         qd = NULL;
137
138                 if (!qd && new_qd) {
139                         qd = new_qd;
140                         list_add(&qd->qd_list, &sdp->sd_quota_list);
141                         atomic_inc(&sdp->sd_quota_count);
142                         new_qd = NULL;
143                 }
144
145                 spin_unlock(&sdp->sd_quota_spin);
146
147                 if (qd || !create) {
148                         if (new_qd) {
149                                 gfs2_lvb_unhold(new_qd->qd_gl);
150                                 kfree(new_qd);
151                         }
152                         *qdp = qd;
153                         return 0;
154                 }
155
156                 error = qd_alloc(sdp, user, id, &new_qd);
157                 if (error)
158                         return error;
159         }
160 }
161
162 static void qd_hold(struct gfs2_quota_data *qd)
163 {
164         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
165
166         spin_lock(&sdp->sd_quota_spin);
167         gfs2_assert(sdp, qd->qd_count);
168         qd->qd_count++;
169         spin_unlock(&sdp->sd_quota_spin);
170 }
171
172 static void qd_put(struct gfs2_quota_data *qd)
173 {
174         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
175         spin_lock(&sdp->sd_quota_spin);
176         gfs2_assert(sdp, qd->qd_count);
177         if (!--qd->qd_count)
178                 qd->qd_last_touched = jiffies;
179         spin_unlock(&sdp->sd_quota_spin);
180 }
181
182 static int slot_get(struct gfs2_quota_data *qd)
183 {
184         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
185         unsigned int c, o = 0, b;
186         unsigned char byte = 0;
187
188         spin_lock(&sdp->sd_quota_spin);
189
190         if (qd->qd_slot_count++) {
191                 spin_unlock(&sdp->sd_quota_spin);
192                 return 0;
193         }
194
195         for (c = 0; c < sdp->sd_quota_chunks; c++)
196                 for (o = 0; o < PAGE_SIZE; o++) {
197                         byte = sdp->sd_quota_bitmap[c][o];
198                         if (byte != 0xFF)
199                                 goto found;
200                 }
201
202         goto fail;
203
204  found:
205         for (b = 0; b < 8; b++)
206                 if (!(byte & (1 << b)))
207                         break;
208         qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
209
210         if (qd->qd_slot >= sdp->sd_quota_slots)
211                 goto fail;
212
213         sdp->sd_quota_bitmap[c][o] |= 1 << b;
214
215         spin_unlock(&sdp->sd_quota_spin);
216
217         return 0;
218
219  fail:
220         qd->qd_slot_count--;
221         spin_unlock(&sdp->sd_quota_spin);
222         return -ENOSPC;
223 }
224
225 static void slot_hold(struct gfs2_quota_data *qd)
226 {
227         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
228
229         spin_lock(&sdp->sd_quota_spin);
230         gfs2_assert(sdp, qd->qd_slot_count);
231         qd->qd_slot_count++;
232         spin_unlock(&sdp->sd_quota_spin);
233 }
234
235 static void slot_put(struct gfs2_quota_data *qd)
236 {
237         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
238
239         spin_lock(&sdp->sd_quota_spin);
240         gfs2_assert(sdp, qd->qd_slot_count);
241         if (!--qd->qd_slot_count) {
242                 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
243                 qd->qd_slot = -1;
244         }
245         spin_unlock(&sdp->sd_quota_spin);
246 }
247
248 static int bh_get(struct gfs2_quota_data *qd)
249 {
250         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
251         struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
252         unsigned int block, offset;
253         uint64_t dblock;
254         int new = 0;
255         struct buffer_head *bh;
256         int error;
257         int boundary;
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->i_vnode, block, &new, &dblock, &boundary);
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(ip->i_gl->gl_sbd, 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 #if 0
1090 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1091                     struct gfs2_quota *q)
1092 {
1093         struct gfs2_quota_data *qd;
1094         struct gfs2_holder q_gh;
1095         int error;
1096
1097         if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1098             !capable(CAP_SYS_ADMIN))
1099                 return -EACCES;
1100
1101         error = qd_get(sdp, user, id, CREATE, &qd);
1102         if (error)
1103                 return error;
1104
1105         error = do_glock(qd, NO_FORCE, &q_gh);
1106         if (error)
1107                 goto out;
1108
1109         memset(q, 0, sizeof(struct gfs2_quota));
1110         q->qu_limit = qd->qd_qb.qb_limit;
1111         q->qu_warn = qd->qd_qb.qb_warn;
1112         q->qu_value = qd->qd_qb.qb_value;
1113
1114         spin_lock(&sdp->sd_quota_spin);
1115         q->qu_value += qd->qd_change;
1116         spin_unlock(&sdp->sd_quota_spin);
1117
1118         gfs2_glock_dq_uninit(&q_gh);
1119
1120  out:
1121         qd_put(qd);
1122
1123         return error;
1124 }
1125 #endif  /*  0  */
1126
1127 int gfs2_quota_init(struct gfs2_sbd *sdp)
1128 {
1129         struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
1130         unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1131         unsigned int x, slot = 0;
1132         unsigned int found = 0;
1133         uint64_t dblock;
1134         uint32_t extlen = 0;
1135         int error;
1136
1137         if (!ip->i_di.di_size ||
1138             ip->i_di.di_size > (64 << 20) ||
1139             ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1140                 gfs2_consist_inode(ip);
1141                 return -EIO;            
1142         }
1143         sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1144         sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1145
1146         error = -ENOMEM;
1147
1148         sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1149                                        sizeof(unsigned char *), GFP_KERNEL);
1150         if (!sdp->sd_quota_bitmap)
1151                 return error;
1152
1153         for (x = 0; x < sdp->sd_quota_chunks; x++) {
1154                 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1155                 if (!sdp->sd_quota_bitmap[x])
1156                         goto fail;
1157         }
1158
1159         for (x = 0; x < blocks; x++) {
1160                 struct buffer_head *bh;
1161                 unsigned int y;
1162
1163                 if (!extlen) {
1164                         int new = 0;
1165                         error = gfs2_extent_map(ip->i_vnode, x, &new, &dblock, &extlen);
1166                         if (error)
1167                                 goto fail;
1168                 }
1169                 gfs2_meta_ra(ip->i_gl,  dblock, extlen);
1170                 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1171                                        &bh);
1172                 if (error)
1173                         goto fail;
1174                 error = -EIO;
1175                 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1176                         brelse(bh);
1177                         goto fail;
1178                 }
1179
1180                 for (y = 0;
1181                      y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1182                      y++, slot++) {
1183                         struct gfs2_quota_change qc;
1184                         struct gfs2_quota_data *qd;
1185
1186                         gfs2_quota_change_in(&qc, bh->b_data +
1187                                           sizeof(struct gfs2_meta_header) +
1188                                           y * sizeof(struct gfs2_quota_change));
1189                         if (!qc.qc_change)
1190                                 continue;
1191
1192                         error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1193                                          qc.qc_id, &qd);
1194                         if (error) {
1195                                 brelse(bh);
1196                                 goto fail;
1197                         }
1198
1199                         set_bit(QDF_CHANGE, &qd->qd_flags);
1200                         qd->qd_change = qc.qc_change;
1201                         qd->qd_slot = slot;
1202                         qd->qd_slot_count = 1;
1203                         qd->qd_last_touched = jiffies;
1204
1205                         spin_lock(&sdp->sd_quota_spin);
1206                         gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1207                         list_add(&qd->qd_list, &sdp->sd_quota_list);
1208                         atomic_inc(&sdp->sd_quota_count);
1209                         spin_unlock(&sdp->sd_quota_spin);
1210
1211                         found++;
1212                 }
1213
1214                 brelse(bh);
1215                 dblock++;
1216                 extlen--;
1217         }
1218
1219         if (found)
1220                 fs_info(sdp, "found %u quota changes\n", found);
1221
1222         return 0;
1223
1224  fail:
1225         gfs2_quota_cleanup(sdp);
1226         return error;
1227 }
1228
1229 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1230 {
1231         struct gfs2_quota_data *qd, *safe;
1232         LIST_HEAD(dead);
1233
1234         spin_lock(&sdp->sd_quota_spin);
1235         list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1236                 if (!qd->qd_count &&
1237                     time_after_eq(jiffies, qd->qd_last_touched +
1238                                 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1239                         list_move(&qd->qd_list, &dead);
1240                         gfs2_assert_warn(sdp,
1241                                          atomic_read(&sdp->sd_quota_count) > 0);
1242                         atomic_dec(&sdp->sd_quota_count);
1243                 }
1244         }
1245         spin_unlock(&sdp->sd_quota_spin);
1246
1247         while (!list_empty(&dead)) {
1248                 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1249                 list_del(&qd->qd_list);
1250
1251                 gfs2_assert_warn(sdp, !qd->qd_change);
1252                 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1253                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1254
1255                 gfs2_lvb_unhold(qd->qd_gl);
1256                 kfree(qd);
1257         }
1258 }
1259
1260 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1261 {
1262         struct list_head *head = &sdp->sd_quota_list;
1263         struct gfs2_quota_data *qd;
1264         unsigned int x;
1265
1266         spin_lock(&sdp->sd_quota_spin);
1267         while (!list_empty(head)) {
1268                 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1269
1270                 if (qd->qd_count > 1 ||
1271                     (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1272                         list_move(&qd->qd_list, head);
1273                         spin_unlock(&sdp->sd_quota_spin);
1274                         schedule();
1275                         spin_lock(&sdp->sd_quota_spin);
1276                         continue;
1277                 }
1278
1279                 list_del(&qd->qd_list);
1280                 atomic_dec(&sdp->sd_quota_count);
1281                 spin_unlock(&sdp->sd_quota_spin);
1282
1283                 if (!qd->qd_count) {
1284                         gfs2_assert_warn(sdp, !qd->qd_change);
1285                         gfs2_assert_warn(sdp, !qd->qd_slot_count);
1286                 } else
1287                         gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1288                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1289
1290                 gfs2_lvb_unhold(qd->qd_gl);
1291                 kfree(qd);
1292
1293                 spin_lock(&sdp->sd_quota_spin);
1294         }
1295         spin_unlock(&sdp->sd_quota_spin);
1296
1297         gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1298
1299         if (sdp->sd_quota_bitmap) {
1300                 for (x = 0; x < sdp->sd_quota_chunks; x++)
1301                         kfree(sdp->sd_quota_bitmap[x]);
1302                 kfree(sdp->sd_quota_bitmap);
1303         }
1304 }
1305