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