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