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