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