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