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