[GFS2] Align all labels against LH side
[linux-2.6.git] / 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 uint64_t qd2offset(struct gfs2_quota_data *qd)
69 {
70         uint64_t offset;
71
72         offset = 2 * (uint64_t)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, uint32_t 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 * (uint64_t)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, uint32_t 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         uint64_t 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, uint32_t 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, uint32_t uid, uint32_t 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         struct gfs2_quota_data *qd_a = *(struct gfs2_quota_data **)a;
507         struct gfs2_quota_data *qd_b = *(struct gfs2_quota_data **)b;
508         int ret = 0;
509
510         if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
511             !test_bit(QDF_USER, &qd_b->qd_flags)) {
512                 if (test_bit(QDF_USER, &qd_a->qd_flags))
513                         ret = -1;
514                 else
515                         ret = 1;
516         } else {
517                 if (qd_a->qd_id < qd_b->qd_id)
518                         ret = -1;
519                 else if (qd_a->qd_id > qd_b->qd_id)
520                         ret = 1;
521         }
522
523         return ret;
524 }
525
526 static void do_qc(struct gfs2_quota_data *qd, int64_t change)
527 {
528         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
529         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
530         struct gfs2_quota_change *qc = qd->qd_bh_qc;
531         int64_t x;
532
533         mutex_lock(&sdp->sd_quota_mutex);
534         gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
535
536         if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
537                 qc->qc_change = 0;
538                 qc->qc_flags = 0;
539                 if (test_bit(QDF_USER, &qd->qd_flags))
540                         qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
541                 qc->qc_id = cpu_to_be32(qd->qd_id);
542         }
543
544         x = qc->qc_change;
545         x = be64_to_cpu(x) + change;
546         qc->qc_change = cpu_to_be64(x);
547
548         spin_lock(&sdp->sd_quota_spin);
549         qd->qd_change = x;
550         spin_unlock(&sdp->sd_quota_spin);
551
552         if (!x) {
553                 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
554                 clear_bit(QDF_CHANGE, &qd->qd_flags);
555                 qc->qc_flags = 0;
556                 qc->qc_id = 0;
557                 slot_put(qd);
558                 qd_put(qd);
559         } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
560                 qd_hold(qd);
561                 slot_hold(qd);
562         }
563                         
564         mutex_unlock(&sdp->sd_quota_mutex);
565 }
566
567 /**
568  * gfs2_adjust_quota
569  *
570  * This function was mostly borrowed from gfs2_block_truncate_page which was
571  * in turn mostly borrowed from ext3
572  */
573 static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
574                              int64_t change, struct gfs2_quota_data *qd)
575 {
576         struct inode *inode = &ip->i_inode;
577         struct address_space *mapping = inode->i_mapping;
578         unsigned long index = loc >> PAGE_CACHE_SHIFT;
579         unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
580         unsigned blocksize, iblock, pos;
581         struct buffer_head *bh;
582         struct page *page;
583         void *kaddr;
584         __be64 *ptr;
585         s64 value;
586         int err = -EIO;
587
588         page = grab_cache_page(mapping, index);
589         if (!page)
590                 return -ENOMEM;
591
592         blocksize = inode->i_sb->s_blocksize;
593         iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
594
595         if (!page_has_buffers(page))
596                 create_empty_buffers(page, blocksize, 0);
597
598         bh = page_buffers(page);
599         pos = blocksize;
600         while (offset >= pos) {
601                 bh = bh->b_this_page;
602                 iblock++;
603                 pos += blocksize;
604         }
605
606         if (!buffer_mapped(bh)) {
607                 gfs2_get_block(inode, iblock, bh, 1);
608                 if (!buffer_mapped(bh))
609                         goto unlock;
610         }
611
612         if (PageUptodate(page))
613                 set_buffer_uptodate(bh);
614
615         if (!buffer_uptodate(bh)) {
616                 ll_rw_block(READ, 1, &bh);
617                 wait_on_buffer(bh);
618                 if (!buffer_uptodate(bh))
619                         goto unlock;
620         }
621
622         gfs2_trans_add_bh(ip->i_gl, bh, 0);
623
624         kaddr = kmap_atomic(page, KM_USER0);
625         ptr = (__be64 *)(kaddr + offset);
626         value = (s64)be64_to_cpu(*ptr) + change;
627         *ptr = cpu_to_be64(value);
628         flush_dcache_page(page);
629         kunmap_atomic(kaddr, KM_USER0);
630         err = 0;
631         qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
632 #if 0
633         qd->qd_qb.qb_limit = cpu_to_be64(q.qu_limit);
634         qd->qd_qb.qb_warn = cpu_to_be64(q.qu_warn);
635 #endif
636         qd->qd_qb.qb_value = cpu_to_be64(value);
637 unlock:
638         unlock_page(page);
639         page_cache_release(page);
640         return err;
641 }
642
643 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
644 {
645         struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
646         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
647         unsigned int data_blocks, ind_blocks;
648         struct gfs2_holder *ghs, i_gh;
649         unsigned int qx, x;
650         struct gfs2_quota_data *qd;
651         loff_t offset;
652         unsigned int nalloc = 0;
653         struct gfs2_alloc *al = NULL;
654         int error;
655
656         gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
657                               &data_blocks, &ind_blocks);
658
659         ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
660         if (!ghs)
661                 return -ENOMEM;
662
663         sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
664         for (qx = 0; qx < num_qd; qx++) {
665                 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
666                                            LM_ST_EXCLUSIVE,
667                                            GL_NOCACHE, &ghs[qx]);
668                 if (error)
669                         goto out;
670         }
671
672         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
673         if (error)
674                 goto out;
675
676         for (x = 0; x < num_qd; x++) {
677                 int alloc_required;
678
679                 offset = qd2offset(qda[x]);
680                 error = gfs2_write_alloc_required(ip, offset,
681                                                   sizeof(struct gfs2_quota),
682                                                   &alloc_required);
683                 if (error)
684                         goto out_gunlock;
685                 if (alloc_required)
686                         nalloc++;
687         }
688
689         if (nalloc) {
690                 al = gfs2_alloc_get(ip);
691
692                 al->al_requested = nalloc * (data_blocks + ind_blocks);
693
694                 error = gfs2_inplace_reserve(ip);
695                 if (error)
696                         goto out_alloc;
697
698                 error = gfs2_trans_begin(sdp,
699                                          al->al_rgd->rd_ri.ri_length +
700                                          num_qd * data_blocks +
701                                          nalloc * ind_blocks +
702                                          RES_DINODE + num_qd +
703                                          RES_STATFS, 0);
704                 if (error)
705                         goto out_ipres;
706         } else {
707                 error = gfs2_trans_begin(sdp,
708                                          num_qd * data_blocks +
709                                          RES_DINODE + num_qd, 0);
710                 if (error)
711                         goto out_gunlock;
712         }
713
714         for (x = 0; x < num_qd; x++) {
715                 qd = qda[x];
716                 offset = qd2offset(qd);
717                 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
718                                           (struct gfs2_quota_data *)
719                                           qd->qd_gl->gl_lvb);
720                 if (error)
721                         goto out_end_trans;
722
723                 do_qc(qd, -qd->qd_change_sync);
724         }
725
726         error = 0;
727
728 out_end_trans:
729         gfs2_trans_end(sdp);
730 out_ipres:
731         if (nalloc)
732                 gfs2_inplace_release(ip);
733 out_alloc:
734         if (nalloc)
735                 gfs2_alloc_put(ip);
736 out_gunlock:
737         gfs2_glock_dq_uninit(&i_gh);
738 out:
739         while (qx--)
740                 gfs2_glock_dq_uninit(&ghs[qx]);
741         kfree(ghs);
742         gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
743         return error;
744 }
745
746 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
747                     struct gfs2_holder *q_gh)
748 {
749         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
750         struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
751         struct gfs2_holder i_gh;
752         struct gfs2_quota q;
753         char buf[sizeof(struct gfs2_quota)];
754         struct file_ra_state ra_state;
755         int error;
756         struct gfs2_quota_lvb *qlvb;
757
758         file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
759 restart:
760         error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
761         if (error)
762                 return error;
763
764         qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
765
766         if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
767                 loff_t pos;
768                 gfs2_glock_dq_uninit(q_gh);
769                 error = gfs2_glock_nq_init(qd->qd_gl,
770                                           LM_ST_EXCLUSIVE, GL_NOCACHE,
771                                           q_gh);
772                 if (error)
773                         return error;
774
775                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
776                 if (error)
777                         goto fail;
778
779                 memset(buf, 0, sizeof(struct gfs2_quota));
780                 pos = qd2offset(qd);
781                 error = gfs2_internal_read(ip, &ra_state, buf,
782                                            &pos, sizeof(struct gfs2_quota));
783                 if (error < 0)
784                         goto fail_gunlock;
785
786                 gfs2_glock_dq_uninit(&i_gh);
787
788                 
789                 gfs2_quota_in(&q, buf);
790                 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
791                 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
792                 qlvb->__pad = 0;
793                 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
794                 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
795                 qlvb->qb_value = cpu_to_be64(q.qu_value);
796                 qd->qd_qb = *qlvb;
797
798                 if (gfs2_glock_is_blocking(qd->qd_gl)) {
799                         gfs2_glock_dq_uninit(q_gh);
800                         force_refresh = 0;
801                         goto restart;
802                 }
803         }
804
805         return 0;
806
807 fail_gunlock:
808         gfs2_glock_dq_uninit(&i_gh);
809 fail:
810         gfs2_glock_dq_uninit(q_gh);
811         return error;
812 }
813
814 int gfs2_quota_lock(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
815 {
816         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
817         struct gfs2_alloc *al = &ip->i_alloc;
818         unsigned int x;
819         int error = 0;
820
821         gfs2_quota_hold(ip, uid, gid);
822
823         if (capable(CAP_SYS_RESOURCE) ||
824             sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
825                 return 0;
826
827         sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
828              sort_qd, NULL);
829
830         for (x = 0; x < al->al_qd_num; x++) {
831                 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
832                 if (error)
833                         break;
834         }
835
836         if (!error)
837                 set_bit(GIF_QD_LOCKED, &ip->i_flags);
838         else {
839                 while (x--)
840                         gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
841                 gfs2_quota_unhold(ip);
842         }
843
844         return error;
845 }
846
847 static int need_sync(struct gfs2_quota_data *qd)
848 {
849         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
850         struct gfs2_tune *gt = &sdp->sd_tune;
851         int64_t value;
852         unsigned int num, den;
853         int do_sync = 1;
854
855         if (!qd->qd_qb.qb_limit)
856                 return 0;
857
858         spin_lock(&sdp->sd_quota_spin);
859         value = qd->qd_change;
860         spin_unlock(&sdp->sd_quota_spin);
861
862         spin_lock(&gt->gt_spin);
863         num = gt->gt_quota_scale_num;
864         den = gt->gt_quota_scale_den;
865         spin_unlock(&gt->gt_spin);
866
867         if (value < 0)
868                 do_sync = 0;
869         else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
870                  (s64)be64_to_cpu(qd->qd_qb.qb_limit))
871                 do_sync = 0;
872         else {
873                 value *= gfs2_jindex_size(sdp) * num;
874                 do_div(value, den);
875                 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
876                 if (value < (int64_t)be64_to_cpu(qd->qd_qb.qb_limit))
877                         do_sync = 0;
878         }
879
880         return do_sync;
881 }
882
883 void gfs2_quota_unlock(struct gfs2_inode *ip)
884 {
885         struct gfs2_alloc *al = &ip->i_alloc;
886         struct gfs2_quota_data *qda[4];
887         unsigned int count = 0;
888         unsigned int x;
889
890         if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
891                 goto out;
892
893         for (x = 0; x < al->al_qd_num; x++) {
894                 struct gfs2_quota_data *qd;
895                 int sync;
896
897                 qd = al->al_qd[x];
898                 sync = need_sync(qd);
899
900                 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
901
902                 if (sync && qd_trylock(qd))
903                         qda[count++] = qd;
904         }
905
906         if (count) {
907                 do_sync(count, qda);
908                 for (x = 0; x < count; x++)
909                         qd_unlock(qda[x]);
910         }
911
912 out:
913         gfs2_quota_unhold(ip);
914 }
915
916 #define MAX_LINE 256
917
918 static int print_message(struct gfs2_quota_data *qd, char *type)
919 {
920         struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
921
922         printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
923                sdp->sd_fsname, type,
924                (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
925                qd->qd_id);
926
927         return 0;
928 }
929
930 int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
931 {
932         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
933         struct gfs2_alloc *al = &ip->i_alloc;
934         struct gfs2_quota_data *qd;
935         int64_t value;
936         unsigned int x;
937         int error = 0;
938
939         if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
940                 return 0;
941
942         if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
943                 return 0;
944
945         for (x = 0; x < al->al_qd_num; x++) {
946                 qd = al->al_qd[x];
947
948                 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
949                       (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
950                         continue;
951
952                 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
953                 spin_lock(&sdp->sd_quota_spin);
954                 value += qd->qd_change;
955                 spin_unlock(&sdp->sd_quota_spin);
956
957                 if (be64_to_cpu(qd->qd_qb.qb_limit) && (int64_t)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
958                         print_message(qd, "exceeded");
959                         error = -EDQUOT;
960                         break;
961                 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
962                            (int64_t)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
963                            time_after_eq(jiffies, qd->qd_last_warn +
964                                          gfs2_tune_get(sdp,
965                                                 gt_quota_warn_period) * HZ)) {
966                         error = print_message(qd, "warning");
967                         qd->qd_last_warn = jiffies;
968                 }
969         }
970
971         return error;
972 }
973
974 void gfs2_quota_change(struct gfs2_inode *ip, int64_t change,
975                        uint32_t uid, uint32_t gid)
976 {
977         struct gfs2_alloc *al = &ip->i_alloc;
978         struct gfs2_quota_data *qd;
979         unsigned int x;
980         unsigned int found = 0;
981
982         if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
983                 return;
984         if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
985                 return;
986
987         for (x = 0; x < al->al_qd_num; x++) {
988                 qd = al->al_qd[x];
989
990                 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
991                     (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
992                         do_qc(qd, change);
993                         found++;
994                 }
995         }
996 }
997
998 int gfs2_quota_sync(struct gfs2_sbd *sdp)
999 {
1000         struct gfs2_quota_data **qda;
1001         unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1002         unsigned int num_qd;
1003         unsigned int x;
1004         int error = 0;
1005
1006         sdp->sd_quota_sync_gen++;
1007
1008         qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1009         if (!qda)
1010                 return -ENOMEM;
1011
1012         do {
1013                 num_qd = 0;
1014
1015                 for (;;) {
1016                         error = qd_fish(sdp, qda + num_qd);
1017                         if (error || !qda[num_qd])
1018                                 break;
1019                         if (++num_qd == max_qd)
1020                                 break;
1021                 }
1022
1023                 if (num_qd) {
1024                         if (!error)
1025                                 error = do_sync(num_qd, qda);
1026                         if (!error)
1027                                 for (x = 0; x < num_qd; x++)
1028                                         qda[x]->qd_sync_gen =
1029                                                 sdp->sd_quota_sync_gen;
1030
1031                         for (x = 0; x < num_qd; x++)
1032                                 qd_unlock(qda[x]);
1033                 }
1034         } while (!error && num_qd == max_qd);
1035
1036         kfree(qda);
1037
1038         return error;
1039 }
1040
1041 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
1042 {
1043         struct gfs2_quota_data *qd;
1044         struct gfs2_holder q_gh;
1045         int error;
1046
1047         error = qd_get(sdp, user, id, CREATE, &qd);
1048         if (error)
1049                 return error;
1050
1051         error = do_glock(qd, FORCE, &q_gh);
1052         if (!error)
1053                 gfs2_glock_dq_uninit(&q_gh);
1054
1055         qd_put(qd);
1056
1057         return error;
1058 }
1059
1060 #if 0
1061 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1062                     struct gfs2_quota *q)
1063 {
1064         struct gfs2_quota_data *qd;
1065         struct gfs2_holder q_gh;
1066         int error;
1067
1068         if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1069             !capable(CAP_SYS_ADMIN))
1070                 return -EACCES;
1071
1072         error = qd_get(sdp, user, id, CREATE, &qd);
1073         if (error)
1074                 return error;
1075
1076         error = do_glock(qd, NO_FORCE, &q_gh);
1077         if (error)
1078                 goto out;
1079
1080         memset(q, 0, sizeof(struct gfs2_quota));
1081         q->qu_limit = be64_to_cpu(qd->qd_qb.qb_limit);
1082         q->qu_warn = be64_to_cpu(qd->qd_qb.qb_warn);
1083         q->qu_value = be64_to_cpu(qd->qd_qb.qb_value);
1084
1085         spin_lock(&sdp->sd_quota_spin);
1086         q->qu_value += qd->qd_change;
1087         spin_unlock(&sdp->sd_quota_spin);
1088
1089         gfs2_glock_dq_uninit(&q_gh);
1090
1091 out:
1092         qd_put(qd);
1093         return error;
1094 }
1095 #endif  /*  0  */
1096
1097 int gfs2_quota_init(struct gfs2_sbd *sdp)
1098 {
1099         struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1100         unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1101         unsigned int x, slot = 0;
1102         unsigned int found = 0;
1103         uint64_t dblock;
1104         uint32_t extlen = 0;
1105         int error;
1106
1107         if (!ip->i_di.di_size ||
1108             ip->i_di.di_size > (64 << 20) ||
1109             ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1110                 gfs2_consist_inode(ip);
1111                 return -EIO;            
1112         }
1113         sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1114         sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
1115
1116         error = -ENOMEM;
1117
1118         sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1119                                        sizeof(unsigned char *), GFP_KERNEL);
1120         if (!sdp->sd_quota_bitmap)
1121                 return error;
1122
1123         for (x = 0; x < sdp->sd_quota_chunks; x++) {
1124                 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1125                 if (!sdp->sd_quota_bitmap[x])
1126                         goto fail;
1127         }
1128
1129         for (x = 0; x < blocks; x++) {
1130                 struct buffer_head *bh;
1131                 unsigned int y;
1132
1133                 if (!extlen) {
1134                         int new = 0;
1135                         error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
1136                         if (error)
1137                                 goto fail;
1138                 }
1139                 gfs2_meta_ra(ip->i_gl,  dblock, extlen);
1140                 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1141                                        &bh);
1142                 if (error)
1143                         goto fail;
1144                 error = -EIO;
1145                 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1146                         brelse(bh);
1147                         goto fail;
1148                 }
1149
1150                 for (y = 0;
1151                      y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1152                      y++, slot++) {
1153                         struct gfs2_quota_change qc;
1154                         struct gfs2_quota_data *qd;
1155
1156                         gfs2_quota_change_in(&qc, bh->b_data +
1157                                           sizeof(struct gfs2_meta_header) +
1158                                           y * sizeof(struct gfs2_quota_change));
1159                         if (!qc.qc_change)
1160                                 continue;
1161
1162                         error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1163                                          qc.qc_id, &qd);
1164                         if (error) {
1165                                 brelse(bh);
1166                                 goto fail;
1167                         }
1168
1169                         set_bit(QDF_CHANGE, &qd->qd_flags);
1170                         qd->qd_change = qc.qc_change;
1171                         qd->qd_slot = slot;
1172                         qd->qd_slot_count = 1;
1173                         qd->qd_last_touched = jiffies;
1174
1175                         spin_lock(&sdp->sd_quota_spin);
1176                         gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1177                         list_add(&qd->qd_list, &sdp->sd_quota_list);
1178                         atomic_inc(&sdp->sd_quota_count);
1179                         spin_unlock(&sdp->sd_quota_spin);
1180
1181                         found++;
1182                 }
1183
1184                 brelse(bh);
1185                 dblock++;
1186                 extlen--;
1187         }
1188
1189         if (found)
1190                 fs_info(sdp, "found %u quota changes\n", found);
1191
1192         return 0;
1193
1194 fail:
1195         gfs2_quota_cleanup(sdp);
1196         return error;
1197 }
1198
1199 void gfs2_quota_scan(struct gfs2_sbd *sdp)
1200 {
1201         struct gfs2_quota_data *qd, *safe;
1202         LIST_HEAD(dead);
1203
1204         spin_lock(&sdp->sd_quota_spin);
1205         list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1206                 if (!qd->qd_count &&
1207                     time_after_eq(jiffies, qd->qd_last_touched +
1208                                 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1209                         list_move(&qd->qd_list, &dead);
1210                         gfs2_assert_warn(sdp,
1211                                          atomic_read(&sdp->sd_quota_count) > 0);
1212                         atomic_dec(&sdp->sd_quota_count);
1213                 }
1214         }
1215         spin_unlock(&sdp->sd_quota_spin);
1216
1217         while (!list_empty(&dead)) {
1218                 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1219                 list_del(&qd->qd_list);
1220
1221                 gfs2_assert_warn(sdp, !qd->qd_change);
1222                 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1223                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1224
1225                 gfs2_lvb_unhold(qd->qd_gl);
1226                 kfree(qd);
1227         }
1228 }
1229
1230 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1231 {
1232         struct list_head *head = &sdp->sd_quota_list;
1233         struct gfs2_quota_data *qd;
1234         unsigned int x;
1235
1236         spin_lock(&sdp->sd_quota_spin);
1237         while (!list_empty(head)) {
1238                 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1239
1240                 if (qd->qd_count > 1 ||
1241                     (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1242                         list_move(&qd->qd_list, head);
1243                         spin_unlock(&sdp->sd_quota_spin);
1244                         schedule();
1245                         spin_lock(&sdp->sd_quota_spin);
1246                         continue;
1247                 }
1248
1249                 list_del(&qd->qd_list);
1250                 atomic_dec(&sdp->sd_quota_count);
1251                 spin_unlock(&sdp->sd_quota_spin);
1252
1253                 if (!qd->qd_count) {
1254                         gfs2_assert_warn(sdp, !qd->qd_change);
1255                         gfs2_assert_warn(sdp, !qd->qd_slot_count);
1256                 } else
1257                         gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1258                 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1259
1260                 gfs2_lvb_unhold(qd->qd_gl);
1261                 kfree(qd);
1262
1263                 spin_lock(&sdp->sd_quota_spin);
1264         }
1265         spin_unlock(&sdp->sd_quota_spin);
1266
1267         gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1268
1269         if (sdp->sd_quota_bitmap) {
1270                 for (x = 0; x < sdp->sd_quota_chunks; x++)
1271                         kfree(sdp->sd_quota_bitmap[x]);
1272                 kfree(sdp->sd_quota_bitmap);
1273         }
1274 }
1275