gfs2: Modify struct gfs2_quota_change_host to use struct kqid
[linux-block.git] / fs / gfs2 / quota.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
0d0868bd 3 * Copyright (C) 2004-2007 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.
1e72c0f7 18 * Since quota tags are part of transactions, there is no need for a quota check
b3b94faa
DT
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>
1495f230 41#include <linux/mm.h>
b3b94faa
DT
42#include <linux/spinlock.h>
43#include <linux/completion.h>
44#include <linux/buffer_head.h>
b3b94faa 45#include <linux/sort.h>
18ec7d5c 46#include <linux/fs.h>
2e565bb6 47#include <linux/bio.h>
5c676f6d 48#include <linux/gfs2_ondisk.h>
37b2c837
SW
49#include <linux/kthread.h>
50#include <linux/freezer.h>
2ec46505 51#include <linux/quota.h>
1d371b5e 52#include <linux/dqblk_xfs.h>
b3b94faa
DT
53
54#include "gfs2.h"
5c676f6d 55#include "incore.h"
b3b94faa
DT
56#include "bmap.h"
57#include "glock.h"
58#include "glops.h"
b3b94faa
DT
59#include "log.h"
60#include "meta_io.h"
61#include "quota.h"
62#include "rgrp.h"
63#include "super.h"
64#include "trans.h"
18ec7d5c 65#include "inode.h"
5c676f6d 66#include "util.h"
b3b94faa
DT
67
68#define QUOTA_USER 1
69#define QUOTA_GROUP 0
70
bb8d8a6f
SW
71struct gfs2_quota_change_host {
72 u64 qc_change;
73 u32 qc_flags; /* GFS2_QCF_... */
e08d8d7f 74 struct kqid qc_id;
bb8d8a6f
SW
75};
76
0a7ab79c
AD
77static LIST_HEAD(qd_lru_list);
78static atomic_t qd_lru_count = ATOMIC_INIT(0);
1328df72 79static DEFINE_SPINLOCK(qd_lru_lock);
0a7ab79c 80
1495f230 81int gfs2_shrink_qd_memory(struct shrinker *shrink, struct shrink_control *sc)
0a7ab79c
AD
82{
83 struct gfs2_quota_data *qd;
84 struct gfs2_sbd *sdp;
1495f230 85 int nr_to_scan = sc->nr_to_scan;
0a7ab79c 86
1495f230 87 if (nr_to_scan == 0)
0a7ab79c
AD
88 goto out;
89
1495f230 90 if (!(sc->gfp_mask & __GFP_FS))
0a7ab79c
AD
91 return -1;
92
93 spin_lock(&qd_lru_lock);
1495f230 94 while (nr_to_scan && !list_empty(&qd_lru_list)) {
0a7ab79c
AD
95 qd = list_entry(qd_lru_list.next,
96 struct gfs2_quota_data, qd_reclaim);
97 sdp = qd->qd_gl->gl_sbd;
98
99 /* Free from the filesystem-specific list */
100 list_del(&qd->qd_list);
101
0a7ab79c
AD
102 gfs2_assert_warn(sdp, !qd->qd_change);
103 gfs2_assert_warn(sdp, !qd->qd_slot_count);
104 gfs2_assert_warn(sdp, !qd->qd_bh_count);
105
f057f6cd 106 gfs2_glock_put(qd->qd_gl);
0a7ab79c
AD
107 atomic_dec(&sdp->sd_quota_count);
108
109 /* Delete it from the common reclaim list */
110 list_del_init(&qd->qd_reclaim);
111 atomic_dec(&qd_lru_count);
112 spin_unlock(&qd_lru_lock);
113 kmem_cache_free(gfs2_quotad_cachep, qd);
114 spin_lock(&qd_lru_lock);
1495f230 115 nr_to_scan--;
0a7ab79c
AD
116 }
117 spin_unlock(&qd_lru_lock);
118
119out:
120 return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
121}
122
2f6c9896
EB
123static u64 qd2index(struct gfs2_quota_data *qd)
124{
125 return (2 * (u64)qd->qd_id) +
126 test_bit(QDF_USER, &qd->qd_flags) ? 0 : 1;
127}
128
cd915493 129static u64 qd2offset(struct gfs2_quota_data *qd)
b3b94faa 130{
cd915493 131 u64 offset;
b3b94faa 132
2f6c9896 133 offset = qd2index(qd);
b3b94faa
DT
134 offset *= sizeof(struct gfs2_quota);
135
136 return offset;
137}
138
cd915493 139static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
b3b94faa
DT
140 struct gfs2_quota_data **qdp)
141{
142 struct gfs2_quota_data *qd;
143 int error;
144
37b2c837 145 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
b3b94faa
DT
146 if (!qd)
147 return -ENOMEM;
148
0a7ab79c 149 atomic_set(&qd->qd_count, 1);
b3b94faa
DT
150 qd->qd_id = id;
151 if (user)
152 set_bit(QDF_USER, &qd->qd_flags);
153 qd->qd_slot = -1;
0a7ab79c 154 INIT_LIST_HEAD(&qd->qd_reclaim);
b3b94faa 155
2f6c9896 156 error = gfs2_glock_get(sdp, qd2index(qd),
b3b94faa
DT
157 &gfs2_quota_glops, CREATE, &qd->qd_gl);
158 if (error)
159 goto fail;
160
b3b94faa
DT
161 *qdp = qd;
162
163 return 0;
164
a91ea69f 165fail:
37b2c837 166 kmem_cache_free(gfs2_quotad_cachep, qd);
b3b94faa
DT
167 return error;
168}
169
6a6ada81 170static int qd_get(struct gfs2_sbd *sdp, int user, u32 id,
b3b94faa
DT
171 struct gfs2_quota_data **qdp)
172{
173 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
174 int error, found;
175
176 *qdp = NULL;
177
178 for (;;) {
179 found = 0;
0a7ab79c 180 spin_lock(&qd_lru_lock);
b3b94faa
DT
181 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
182 if (qd->qd_id == id &&
183 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
0a7ab79c
AD
184 if (!atomic_read(&qd->qd_count) &&
185 !list_empty(&qd->qd_reclaim)) {
186 /* Remove it from reclaim list */
187 list_del_init(&qd->qd_reclaim);
188 atomic_dec(&qd_lru_count);
189 }
190 atomic_inc(&qd->qd_count);
b3b94faa
DT
191 found = 1;
192 break;
193 }
194 }
195
196 if (!found)
197 qd = NULL;
198
199 if (!qd && new_qd) {
200 qd = new_qd;
201 list_add(&qd->qd_list, &sdp->sd_quota_list);
202 atomic_inc(&sdp->sd_quota_count);
203 new_qd = NULL;
204 }
205
0a7ab79c 206 spin_unlock(&qd_lru_lock);
b3b94faa 207
6a6ada81 208 if (qd) {
b3b94faa 209 if (new_qd) {
f057f6cd 210 gfs2_glock_put(new_qd->qd_gl);
37b2c837 211 kmem_cache_free(gfs2_quotad_cachep, new_qd);
b3b94faa
DT
212 }
213 *qdp = qd;
214 return 0;
215 }
216
217 error = qd_alloc(sdp, user, id, &new_qd);
218 if (error)
219 return error;
220 }
221}
222
223static void qd_hold(struct gfs2_quota_data *qd)
224{
225 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
0a7ab79c
AD
226 gfs2_assert(sdp, atomic_read(&qd->qd_count));
227 atomic_inc(&qd->qd_count);
b3b94faa
DT
228}
229
230static void qd_put(struct gfs2_quota_data *qd)
231{
0a7ab79c
AD
232 if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
233 /* Add to the reclaim list */
234 list_add_tail(&qd->qd_reclaim, &qd_lru_list);
235 atomic_inc(&qd_lru_count);
236 spin_unlock(&qd_lru_lock);
237 }
b3b94faa
DT
238}
239
240static int slot_get(struct gfs2_quota_data *qd)
241{
242 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
243 unsigned int c, o = 0, b;
244 unsigned char byte = 0;
245
22077f57 246 spin_lock(&qd_lru_lock);
b3b94faa
DT
247
248 if (qd->qd_slot_count++) {
22077f57 249 spin_unlock(&qd_lru_lock);
b3b94faa
DT
250 return 0;
251 }
252
253 for (c = 0; c < sdp->sd_quota_chunks; c++)
254 for (o = 0; o < PAGE_SIZE; o++) {
255 byte = sdp->sd_quota_bitmap[c][o];
256 if (byte != 0xFF)
257 goto found;
258 }
259
260 goto fail;
261
a91ea69f 262found:
b3b94faa
DT
263 for (b = 0; b < 8; b++)
264 if (!(byte & (1 << b)))
265 break;
266 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
267
268 if (qd->qd_slot >= sdp->sd_quota_slots)
269 goto fail;
270
271 sdp->sd_quota_bitmap[c][o] |= 1 << b;
272
22077f57 273 spin_unlock(&qd_lru_lock);
b3b94faa
DT
274
275 return 0;
276
a91ea69f 277fail:
b3b94faa 278 qd->qd_slot_count--;
22077f57 279 spin_unlock(&qd_lru_lock);
b3b94faa
DT
280 return -ENOSPC;
281}
282
283static void slot_hold(struct gfs2_quota_data *qd)
284{
285 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
286
22077f57 287 spin_lock(&qd_lru_lock);
b3b94faa
DT
288 gfs2_assert(sdp, qd->qd_slot_count);
289 qd->qd_slot_count++;
22077f57 290 spin_unlock(&qd_lru_lock);
b3b94faa
DT
291}
292
293static void slot_put(struct gfs2_quota_data *qd)
294{
295 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
296
22077f57 297 spin_lock(&qd_lru_lock);
b3b94faa
DT
298 gfs2_assert(sdp, qd->qd_slot_count);
299 if (!--qd->qd_slot_count) {
300 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
301 qd->qd_slot = -1;
302 }
22077f57 303 spin_unlock(&qd_lru_lock);
b3b94faa
DT
304}
305
306static int bh_get(struct gfs2_quota_data *qd)
307{
308 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
feaa7bba 309 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
b3b94faa 310 unsigned int block, offset;
b3b94faa
DT
311 struct buffer_head *bh;
312 int error;
23591256 313 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
b3b94faa 314
f55ab26a 315 mutex_lock(&sdp->sd_quota_mutex);
b3b94faa
DT
316
317 if (qd->qd_bh_count++) {
f55ab26a 318 mutex_unlock(&sdp->sd_quota_mutex);
b3b94faa
DT
319 return 0;
320 }
321
322 block = qd->qd_slot / sdp->sd_qc_per_block;
0d0868bd 323 offset = qd->qd_slot % sdp->sd_qc_per_block;
b3b94faa 324
23591256 325 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
e9e1ef2b 326 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
b3b94faa
DT
327 if (error)
328 goto fail;
7276b3b0 329 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
b3b94faa
DT
330 if (error)
331 goto fail;
332 error = -EIO;
333 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
334 goto fail_brelse;
335
336 qd->qd_bh = bh;
337 qd->qd_bh_qc = (struct gfs2_quota_change *)
338 (bh->b_data + sizeof(struct gfs2_meta_header) +
339 offset * sizeof(struct gfs2_quota_change));
340
2e95b665 341 mutex_unlock(&sdp->sd_quota_mutex);
b3b94faa
DT
342
343 return 0;
344
a91ea69f 345fail_brelse:
b3b94faa 346 brelse(bh);
a91ea69f 347fail:
b3b94faa 348 qd->qd_bh_count--;
f55ab26a 349 mutex_unlock(&sdp->sd_quota_mutex);
b3b94faa
DT
350 return error;
351}
352
353static void bh_put(struct gfs2_quota_data *qd)
354{
355 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
356
f55ab26a 357 mutex_lock(&sdp->sd_quota_mutex);
b3b94faa
DT
358 gfs2_assert(sdp, qd->qd_bh_count);
359 if (!--qd->qd_bh_count) {
360 brelse(qd->qd_bh);
361 qd->qd_bh = NULL;
362 qd->qd_bh_qc = NULL;
363 }
f55ab26a 364 mutex_unlock(&sdp->sd_quota_mutex);
b3b94faa
DT
365}
366
367static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
368{
369 struct gfs2_quota_data *qd = NULL;
370 int error;
371 int found = 0;
372
373 *qdp = NULL;
374
375 if (sdp->sd_vfs->s_flags & MS_RDONLY)
376 return 0;
377
0a7ab79c 378 spin_lock(&qd_lru_lock);
b3b94faa
DT
379
380 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
381 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
382 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
383 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
384 continue;
385
386 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
387
388 set_bit(QDF_LOCKED, &qd->qd_flags);
0a7ab79c
AD
389 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
390 atomic_inc(&qd->qd_count);
b3b94faa
DT
391 qd->qd_change_sync = qd->qd_change;
392 gfs2_assert_warn(sdp, qd->qd_slot_count);
393 qd->qd_slot_count++;
394 found = 1;
395
396 break;
397 }
398
399 if (!found)
400 qd = NULL;
401
0a7ab79c 402 spin_unlock(&qd_lru_lock);
b3b94faa
DT
403
404 if (qd) {
405 gfs2_assert_warn(sdp, qd->qd_change_sync);
406 error = bh_get(qd);
407 if (error) {
408 clear_bit(QDF_LOCKED, &qd->qd_flags);
409 slot_put(qd);
410 qd_put(qd);
411 return error;
412 }
413 }
414
415 *qdp = qd;
416
417 return 0;
418}
419
420static int qd_trylock(struct gfs2_quota_data *qd)
421{
422 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
423
424 if (sdp->sd_vfs->s_flags & MS_RDONLY)
425 return 0;
426
0a7ab79c 427 spin_lock(&qd_lru_lock);
b3b94faa
DT
428
429 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
430 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
0a7ab79c 431 spin_unlock(&qd_lru_lock);
b3b94faa
DT
432 return 0;
433 }
434
435 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
436
437 set_bit(QDF_LOCKED, &qd->qd_flags);
0a7ab79c
AD
438 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
439 atomic_inc(&qd->qd_count);
b3b94faa
DT
440 qd->qd_change_sync = qd->qd_change;
441 gfs2_assert_warn(sdp, qd->qd_slot_count);
442 qd->qd_slot_count++;
443
0a7ab79c 444 spin_unlock(&qd_lru_lock);
b3b94faa
DT
445
446 gfs2_assert_warn(sdp, qd->qd_change_sync);
447 if (bh_get(qd)) {
448 clear_bit(QDF_LOCKED, &qd->qd_flags);
449 slot_put(qd);
450 qd_put(qd);
451 return 0;
452 }
453
454 return 1;
455}
456
457static void qd_unlock(struct gfs2_quota_data *qd)
458{
568f4c96
SW
459 gfs2_assert_warn(qd->qd_gl->gl_sbd,
460 test_bit(QDF_LOCKED, &qd->qd_flags));
b3b94faa
DT
461 clear_bit(QDF_LOCKED, &qd->qd_flags);
462 bh_put(qd);
463 slot_put(qd);
464 qd_put(qd);
465}
466
33a82529 467static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id,
b3b94faa
DT
468 struct gfs2_quota_data **qdp)
469{
470 int error;
471
6a6ada81 472 error = qd_get(sdp, user, id, qdp);
b3b94faa
DT
473 if (error)
474 return error;
475
476 error = slot_get(*qdp);
477 if (error)
478 goto fail;
479
480 error = bh_get(*qdp);
481 if (error)
482 goto fail_slot;
483
484 return 0;
485
a91ea69f 486fail_slot:
b3b94faa 487 slot_put(*qdp);
a91ea69f 488fail:
b3b94faa
DT
489 qd_put(*qdp);
490 return error;
491}
492
493static void qdsb_put(struct gfs2_quota_data *qd)
494{
495 bh_put(qd);
496 slot_put(qd);
497 qd_put(qd);
498}
499
cd915493 500int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
b3b94faa 501{
feaa7bba 502 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
5407e242 503 struct gfs2_quota_data **qd;
b3b94faa
DT
504 int error;
505
aaaf68c5
AP
506 if (ip->i_res == NULL) {
507 error = gfs2_rs_alloc(ip);
508 if (error)
509 return error;
510 }
5407e242
BP
511
512 qd = ip->i_res->rs_qa_qd;
513
514 if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
b3b94faa
DT
515 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
516 return -EIO;
517
518 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
519 return 0;
520
33a82529 521 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, qd);
b3b94faa
DT
522 if (error)
523 goto out;
5407e242 524 ip->i_res->rs_qa_qd_num++;
b3b94faa
DT
525 qd++;
526
33a82529 527 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, qd);
b3b94faa
DT
528 if (error)
529 goto out;
5407e242 530 ip->i_res->rs_qa_qd_num++;
b3b94faa
DT
531 qd++;
532
f4108a60 533 if (uid != NO_UID_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
33a82529 534 error = qdsb_get(sdp, QUOTA_USER, uid, qd);
b3b94faa
DT
535 if (error)
536 goto out;
5407e242 537 ip->i_res->rs_qa_qd_num++;
b3b94faa
DT
538 qd++;
539 }
540
f4108a60 541 if (gid != NO_GID_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
33a82529 542 error = qdsb_get(sdp, QUOTA_GROUP, gid, qd);
b3b94faa
DT
543 if (error)
544 goto out;
5407e242 545 ip->i_res->rs_qa_qd_num++;
b3b94faa
DT
546 qd++;
547 }
548
a91ea69f 549out:
b3b94faa
DT
550 if (error)
551 gfs2_quota_unhold(ip);
b3b94faa
DT
552 return error;
553}
554
555void gfs2_quota_unhold(struct gfs2_inode *ip)
556{
feaa7bba 557 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
558 unsigned int x;
559
5407e242
BP
560 if (ip->i_res == NULL)
561 return;
b3b94faa
DT
562 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
563
5407e242
BP
564 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
565 qdsb_put(ip->i_res->rs_qa_qd[x]);
566 ip->i_res->rs_qa_qd[x] = NULL;
b3b94faa 567 }
5407e242 568 ip->i_res->rs_qa_qd_num = 0;
b3b94faa
DT
569}
570
571static int sort_qd(const void *a, const void *b)
572{
48fac179
SW
573 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
574 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
b3b94faa
DT
575
576 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
577 !test_bit(QDF_USER, &qd_b->qd_flags)) {
578 if (test_bit(QDF_USER, &qd_a->qd_flags))
48fac179 579 return -1;
b3b94faa 580 else
48fac179 581 return 1;
b3b94faa 582 }
48fac179
SW
583 if (qd_a->qd_id < qd_b->qd_id)
584 return -1;
585 if (qd_a->qd_id > qd_b->qd_id)
586 return 1;
b3b94faa 587
48fac179 588 return 0;
b3b94faa
DT
589}
590
cd915493 591static void do_qc(struct gfs2_quota_data *qd, s64 change)
b3b94faa
DT
592{
593 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
feaa7bba 594 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
b3b94faa 595 struct gfs2_quota_change *qc = qd->qd_bh_qc;
cd915493 596 s64 x;
b3b94faa 597
f55ab26a 598 mutex_lock(&sdp->sd_quota_mutex);
d4e9c4c3 599 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
b3b94faa
DT
600
601 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
602 qc->qc_change = 0;
603 qc->qc_flags = 0;
604 if (test_bit(QDF_USER, &qd->qd_flags))
605 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
606 qc->qc_id = cpu_to_be32(qd->qd_id);
607 }
608
b44b84d7 609 x = be64_to_cpu(qc->qc_change) + change;
b3b94faa
DT
610 qc->qc_change = cpu_to_be64(x);
611
22077f57 612 spin_lock(&qd_lru_lock);
b3b94faa 613 qd->qd_change = x;
22077f57 614 spin_unlock(&qd_lru_lock);
b3b94faa
DT
615
616 if (!x) {
617 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
618 clear_bit(QDF_CHANGE, &qd->qd_flags);
619 qc->qc_flags = 0;
620 qc->qc_id = 0;
621 slot_put(qd);
622 qd_put(qd);
623 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
624 qd_hold(qd);
625 slot_hold(qd);
626 }
907b9bce 627
f55ab26a 628 mutex_unlock(&sdp->sd_quota_mutex);
b3b94faa
DT
629}
630
18ec7d5c 631/**
1e72c0f7
SW
632 * gfs2_adjust_quota - adjust record of current block usage
633 * @ip: The quota inode
634 * @loc: Offset of the entry in the quota file
e285c100 635 * @change: The amount of usage change to record
1e72c0f7 636 * @qd: The quota data
e285c100 637 * @fdq: The updated limits to record
18ec7d5c
SW
638 *
639 * This function was mostly borrowed from gfs2_block_truncate_page which was
640 * in turn mostly borrowed from ext3
1e72c0f7
SW
641 *
642 * Returns: 0 or -ve on error
18ec7d5c 643 */
1e72c0f7 644
18ec7d5c 645static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
e285c100
SW
646 s64 change, struct gfs2_quota_data *qd,
647 struct fs_disk_quota *fdq)
18ec7d5c 648{
feaa7bba 649 struct inode *inode = &ip->i_inode;
14870b45 650 struct gfs2_sbd *sdp = GFS2_SB(inode);
18ec7d5c
SW
651 struct address_space *mapping = inode->i_mapping;
652 unsigned long index = loc >> PAGE_CACHE_SHIFT;
1990e917 653 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
18ec7d5c 654 unsigned blocksize, iblock, pos;
ab9bbda0 655 struct buffer_head *bh;
18ec7d5c 656 struct page *page;
7e619bc3
AD
657 void *kaddr, *ptr;
658 struct gfs2_quota q, *qp;
659 int err, nbytes;
e285c100 660 u64 size;
18ec7d5c 661
891a8e93
SW
662 if (gfs2_is_stuffed(ip)) {
663 err = gfs2_unstuff_dinode(ip, NULL);
664 if (err)
665 return err;
666 }
7e619bc3
AD
667
668 memset(&q, 0, sizeof(struct gfs2_quota));
4306629e 669 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
7e619bc3
AD
670 if (err < 0)
671 return err;
672
673 err = -EIO;
674 qp = &q;
675 qp->qu_value = be64_to_cpu(qp->qu_value);
676 qp->qu_value += change;
677 qp->qu_value = cpu_to_be64(qp->qu_value);
678 qd->qd_qb.qb_value = qp->qu_value;
679 if (fdq) {
680 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
14870b45 681 qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
7e619bc3
AD
682 qd->qd_qb.qb_warn = qp->qu_warn;
683 }
684 if (fdq->d_fieldmask & FS_DQ_BHARD) {
14870b45 685 qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
7e619bc3
AD
686 qd->qd_qb.qb_limit = qp->qu_limit;
687 }
802ec9b6
AD
688 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
689 qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
690 qd->qd_qb.qb_value = qp->qu_value;
691 }
7e619bc3
AD
692 }
693
694 /* Write the quota into the quota file on disk */
695 ptr = qp;
696 nbytes = sizeof(struct gfs2_quota);
697get_a_page:
220cca2a 698 page = find_or_create_page(mapping, index, GFP_NOFS);
18ec7d5c
SW
699 if (!page)
700 return -ENOMEM;
701
702 blocksize = inode->i_sb->s_blocksize;
703 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
704
705 if (!page_has_buffers(page))
706 create_empty_buffers(page, blocksize, 0);
707
708 bh = page_buffers(page);
709 pos = blocksize;
710 while (offset >= pos) {
711 bh = bh->b_this_page;
712 iblock++;
713 pos += blocksize;
714 }
715
716 if (!buffer_mapped(bh)) {
e9e1ef2b 717 gfs2_block_map(inode, iblock, bh, 1);
18ec7d5c 718 if (!buffer_mapped(bh))
7e619bc3
AD
719 goto unlock_out;
720 /* If it's a newly allocated disk block for quota, zero it */
8b421601
AD
721 if (buffer_new(bh))
722 zero_user(page, pos - blocksize, bh->b_size);
18ec7d5c
SW
723 }
724
725 if (PageUptodate(page))
726 set_buffer_uptodate(bh);
727
728 if (!buffer_uptodate(bh)) {
20ed0535 729 ll_rw_block(READ | REQ_META, 1, &bh);
18ec7d5c
SW
730 wait_on_buffer(bh);
731 if (!buffer_uptodate(bh))
7e619bc3 732 goto unlock_out;
18ec7d5c
SW
733 }
734
735 gfs2_trans_add_bh(ip->i_gl, bh, 0);
736
d9349285 737 kaddr = kmap_atomic(page);
7e619bc3
AD
738 if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
739 nbytes = PAGE_CACHE_SIZE - offset;
740 memcpy(kaddr + offset, ptr, nbytes);
18ec7d5c 741 flush_dcache_page(page);
d9349285 742 kunmap_atomic(kaddr);
7e619bc3
AD
743 unlock_page(page);
744 page_cache_release(page);
745
746 /* If quota straddles page boundary, we need to update the rest of the
747 * quota at the beginning of the next page */
8b421601 748 if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
7e619bc3
AD
749 ptr = ptr + nbytes;
750 nbytes = sizeof(struct gfs2_quota) - nbytes;
751 offset = 0;
752 index++;
753 goto get_a_page;
754 }
e285c100 755
e285c100 756 size = loc + sizeof(struct gfs2_quota);
a2e0f799 757 if (size > inode->i_size)
e285c100 758 i_size_write(inode, size);
e285c100 759 inode->i_mtime = inode->i_atime = CURRENT_TIME;
e285c100 760 mark_inode_dirty(inode);
500242ac 761 return 0;
ab9bbda0 762
7e619bc3 763unlock_out:
18ec7d5c
SW
764 unlock_page(page);
765 page_cache_release(page);
766 return err;
767}
768
b3b94faa
DT
769static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
770{
771 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
feaa7bba 772 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
b3b94faa
DT
773 unsigned int data_blocks, ind_blocks;
774 struct gfs2_holder *ghs, i_gh;
775 unsigned int qx, x;
776 struct gfs2_quota_data *qd;
71f890f7 777 unsigned reserved;
f42faf4f 778 loff_t offset;
20b95bf2 779 unsigned int nalloc = 0, blocks;
b3b94faa
DT
780 int error;
781
0a305e49
BP
782 error = gfs2_rs_alloc(ip);
783 if (error)
784 return error;
785
b3b94faa
DT
786 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
787 &data_blocks, &ind_blocks);
788
16c5f06f 789 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
b3b94faa
DT
790 if (!ghs)
791 return -ENOMEM;
792
793 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
56aa72d0 794 mutex_lock(&ip->i_inode.i_mutex);
b3b94faa 795 for (qx = 0; qx < num_qd; qx++) {
1e72c0f7 796 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
b3b94faa
DT
797 GL_NOCACHE, &ghs[qx]);
798 if (error)
799 goto out;
800 }
801
802 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
803 if (error)
804 goto out;
805
806 for (x = 0; x < num_qd; x++) {
b3b94faa 807 offset = qd2offset(qda[x]);
461cb419
BP
808 if (gfs2_write_alloc_required(ip, offset,
809 sizeof(struct gfs2_quota)))
b3b94faa
DT
810 nalloc++;
811 }
812
20b95bf2
AD
813 /*
814 * 1 blk for unstuffing inode if stuffed. We add this extra
815 * block to the reservation unconditionally. If the inode
816 * doesn't need unstuffing, the block will be released to the
817 * rgrp since it won't be allocated during the transaction
818 */
7e619bc3
AD
819 /* +3 in the end for unstuffing block, inode size update block
820 * and another block in case quota straddles page boundary and
821 * two blocks need to be updated instead of 1 */
822 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
b3b94faa 823
71f890f7 824 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
9dbe9610 825 error = gfs2_inplace_reserve(ip, reserved, 0);
20b95bf2
AD
826 if (error)
827 goto out_alloc;
b3b94faa 828
20b95bf2 829 if (nalloc)
71f890f7 830 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
20b95bf2
AD
831
832 error = gfs2_trans_begin(sdp, blocks, 0);
833 if (error)
834 goto out_ipres;
b3b94faa
DT
835
836 for (x = 0; x < num_qd; x++) {
b3b94faa
DT
837 qd = qda[x];
838 offset = qd2offset(qd);
e285c100 839 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
18ec7d5c 840 if (error)
b3b94faa 841 goto out_end_trans;
b3b94faa
DT
842
843 do_qc(qd, -qd->qd_change_sync);
662e3a55 844 set_bit(QDF_REFRESH, &qd->qd_flags);
b3b94faa
DT
845 }
846
847 error = 0;
848
a91ea69f 849out_end_trans:
b3b94faa 850 gfs2_trans_end(sdp);
a91ea69f 851out_ipres:
20b95bf2 852 gfs2_inplace_release(ip);
a91ea69f 853out_alloc:
b3b94faa 854 gfs2_glock_dq_uninit(&i_gh);
a91ea69f 855out:
b3b94faa
DT
856 while (qx--)
857 gfs2_glock_dq_uninit(&ghs[qx]);
e285c100 858 mutex_unlock(&ip->i_inode.i_mutex);
b3b94faa 859 kfree(ghs);
b09e593d 860 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
b3b94faa
DT
861 return error;
862}
863
e285c100
SW
864static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
865{
866 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
867 struct gfs2_quota q;
868 struct gfs2_quota_lvb *qlvb;
869 loff_t pos;
870 int error;
871
872 memset(&q, 0, sizeof(struct gfs2_quota));
873 pos = qd2offset(qd);
4306629e 874 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
e285c100
SW
875 if (error < 0)
876 return error;
877
4e2f8849 878 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
e285c100
SW
879 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
880 qlvb->__pad = 0;
881 qlvb->qb_limit = q.qu_limit;
882 qlvb->qb_warn = q.qu_warn;
883 qlvb->qb_value = q.qu_value;
884 qd->qd_qb = *qlvb;
885
886 return 0;
887}
888
b3b94faa
DT
889static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
890 struct gfs2_holder *q_gh)
891{
892 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
feaa7bba 893 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
b3b94faa 894 struct gfs2_holder i_gh;
b3b94faa
DT
895 int error;
896
a91ea69f 897restart:
b3b94faa
DT
898 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
899 if (error)
900 return error;
901
4e2f8849 902 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
b3b94faa 903
e9fc2aa0 904 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
b3b94faa 905 gfs2_glock_dq_uninit(q_gh);
91094d0f
SW
906 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
907 GL_NOCACHE, q_gh);
b3b94faa
DT
908 if (error)
909 return error;
910
e9fc2aa0 911 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
b3b94faa
DT
912 if (error)
913 goto fail;
914
e285c100
SW
915 error = update_qd(sdp, qd);
916 if (error)
1e72c0f7 917 goto fail_gunlock;
b3b94faa 918
e285c100 919 gfs2_glock_dq_uninit(&i_gh);
91094d0f
SW
920 gfs2_glock_dq_uninit(q_gh);
921 force_refresh = 0;
922 goto restart;
b3b94faa
DT
923 }
924
925 return 0;
926
a91ea69f 927fail_gunlock:
b3b94faa 928 gfs2_glock_dq_uninit(&i_gh);
a91ea69f 929fail:
b3b94faa 930 gfs2_glock_dq_uninit(q_gh);
b3b94faa
DT
931 return error;
932}
933
cd915493 934int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
b3b94faa 935{
feaa7bba 936 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
662e3a55 937 struct gfs2_quota_data *qd;
b3b94faa
DT
938 unsigned int x;
939 int error = 0;
940
891a8e93
SW
941 error = gfs2_quota_hold(ip, uid, gid);
942 if (error)
943 return error;
b3b94faa
DT
944
945 if (capable(CAP_SYS_RESOURCE) ||
946 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
947 return 0;
948
5407e242
BP
949 sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
950 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
b3b94faa 951
5407e242 952 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
662e3a55 953 int force = NO_FORCE;
5407e242 954 qd = ip->i_res->rs_qa_qd[x];
662e3a55
AD
955 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
956 force = FORCE;
5407e242 957 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
b3b94faa
DT
958 if (error)
959 break;
960 }
961
962 if (!error)
963 set_bit(GIF_QD_LOCKED, &ip->i_flags);
964 else {
965 while (x--)
5407e242 966 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
b3b94faa
DT
967 gfs2_quota_unhold(ip);
968 }
969
970 return error;
971}
972
973static int need_sync(struct gfs2_quota_data *qd)
974{
975 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
976 struct gfs2_tune *gt = &sdp->sd_tune;
cd915493 977 s64 value;
b3b94faa
DT
978 unsigned int num, den;
979 int do_sync = 1;
980
981 if (!qd->qd_qb.qb_limit)
982 return 0;
983
22077f57 984 spin_lock(&qd_lru_lock);
b3b94faa 985 value = qd->qd_change;
22077f57 986 spin_unlock(&qd_lru_lock);
b3b94faa
DT
987
988 spin_lock(&gt->gt_spin);
989 num = gt->gt_quota_scale_num;
990 den = gt->gt_quota_scale_den;
991 spin_unlock(&gt->gt_spin);
992
993 if (value < 0)
994 do_sync = 0;
e9fc2aa0
SW
995 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
996 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
b3b94faa
DT
997 do_sync = 0;
998 else {
999 value *= gfs2_jindex_size(sdp) * num;
4abaca17 1000 value = div_s64(value, den);
e9fc2aa0 1001 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
cd915493 1002 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
b3b94faa
DT
1003 do_sync = 0;
1004 }
1005
1006 return do_sync;
1007}
1008
1009void gfs2_quota_unlock(struct gfs2_inode *ip)
1010{
b3b94faa
DT
1011 struct gfs2_quota_data *qda[4];
1012 unsigned int count = 0;
1013 unsigned int x;
1014
1015 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1016 goto out;
1017
5407e242 1018 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
b3b94faa
DT
1019 struct gfs2_quota_data *qd;
1020 int sync;
1021
5407e242 1022 qd = ip->i_res->rs_qa_qd[x];
b3b94faa
DT
1023 sync = need_sync(qd);
1024
5407e242 1025 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
b3b94faa
DT
1026
1027 if (sync && qd_trylock(qd))
1028 qda[count++] = qd;
1029 }
1030
1031 if (count) {
1032 do_sync(count, qda);
1033 for (x = 0; x < count; x++)
1034 qd_unlock(qda[x]);
1035 }
1036
a91ea69f 1037out:
b3b94faa
DT
1038 gfs2_quota_unhold(ip);
1039}
1040
1041#define MAX_LINE 256
1042
1043static int print_message(struct gfs2_quota_data *qd, char *type)
1044{
1045 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
b3b94faa 1046
2ec46505 1047 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
02630a12
SW
1048 sdp->sd_fsname, type,
1049 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
1050 qd->qd_id);
b3b94faa
DT
1051
1052 return 0;
1053}
1054
cd915493 1055int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
b3b94faa 1056{
feaa7bba 1057 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa 1058 struct gfs2_quota_data *qd;
cd915493 1059 s64 value;
b3b94faa
DT
1060 unsigned int x;
1061 int error = 0;
1062
1063 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1064 return 0;
1065
1066 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1067 return 0;
1068
5407e242
BP
1069 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1070 qd = ip->i_res->rs_qa_qd[x];
b3b94faa
DT
1071
1072 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1073 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
1074 continue;
1075
e9fc2aa0 1076 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
22077f57 1077 spin_lock(&qd_lru_lock);
b3b94faa 1078 value += qd->qd_change;
22077f57 1079 spin_unlock(&qd_lru_lock);
b3b94faa 1080
cd915493 1081 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
b3b94faa 1082 print_message(qd, "exceeded");
431f1974
EB
1083 quota_send_warning(make_kqid(&init_user_ns,
1084 test_bit(QDF_USER, &qd->qd_flags) ?
1085 USRQUOTA : GRPQUOTA,
1086 qd->qd_id),
2ec46505
SW
1087 sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1088
b3b94faa
DT
1089 error = -EDQUOT;
1090 break;
e9fc2aa0 1091 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
cd915493 1092 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
b3b94faa 1093 time_after_eq(jiffies, qd->qd_last_warn +
568f4c96
SW
1094 gfs2_tune_get(sdp,
1095 gt_quota_warn_period) * HZ)) {
431f1974
EB
1096 quota_send_warning(make_kqid(&init_user_ns,
1097 test_bit(QDF_USER, &qd->qd_flags) ?
1098 USRQUOTA : GRPQUOTA,
1099 qd->qd_id),
2ec46505 1100 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
b3b94faa
DT
1101 error = print_message(qd, "warning");
1102 qd->qd_last_warn = jiffies;
1103 }
1104 }
1105
1106 return error;
1107}
1108
cd915493
SW
1109void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1110 u32 uid, u32 gid)
b3b94faa 1111{
b3b94faa
DT
1112 struct gfs2_quota_data *qd;
1113 unsigned int x;
b3b94faa 1114
feaa7bba 1115 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
b3b94faa 1116 return;
383f01fb 1117 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
b3b94faa
DT
1118 return;
1119
5407e242
BP
1120 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1121 qd = ip->i_res->rs_qa_qd[x];
b3b94faa
DT
1122
1123 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1124 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1125 do_qc(qd, change);
b3b94faa
DT
1126 }
1127 }
1128}
1129
ceed1723 1130int gfs2_quota_sync(struct super_block *sb, int type)
b3b94faa 1131{
8c42d637 1132 struct gfs2_sbd *sdp = sb->s_fs_info;
b3b94faa
DT
1133 struct gfs2_quota_data **qda;
1134 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1135 unsigned int num_qd;
1136 unsigned int x;
1137 int error = 0;
1138
1139 sdp->sd_quota_sync_gen++;
1140
1141 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1142 if (!qda)
1143 return -ENOMEM;
1144
1145 do {
1146 num_qd = 0;
1147
1148 for (;;) {
1149 error = qd_fish(sdp, qda + num_qd);
1150 if (error || !qda[num_qd])
1151 break;
1152 if (++num_qd == max_qd)
1153 break;
1154 }
1155
1156 if (num_qd) {
1157 if (!error)
1158 error = do_sync(num_qd, qda);
1159 if (!error)
1160 for (x = 0; x < num_qd; x++)
1161 qda[x]->qd_sync_gen =
1162 sdp->sd_quota_sync_gen;
1163
1164 for (x = 0; x < num_qd; x++)
1165 qd_unlock(qda[x]);
1166 }
1167 } while (!error && num_qd == max_qd);
1168
1169 kfree(qda);
1170
1171 return error;
1172}
1173
5fb324ad
CH
1174static int gfs2_quota_sync_timeo(struct super_block *sb, int type)
1175{
ceed1723 1176 return gfs2_quota_sync(sb, type);
5fb324ad
CH
1177}
1178
cd915493 1179int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
b3b94faa
DT
1180{
1181 struct gfs2_quota_data *qd;
1182 struct gfs2_holder q_gh;
1183 int error;
1184
6a6ada81 1185 error = qd_get(sdp, user, id, &qd);
b3b94faa
DT
1186 if (error)
1187 return error;
1188
1189 error = do_glock(qd, FORCE, &q_gh);
1190 if (!error)
1191 gfs2_glock_dq_uninit(&q_gh);
1192
1193 qd_put(qd);
b3b94faa
DT
1194 return error;
1195}
1196
bb8d8a6f
SW
1197static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1198{
1199 const struct gfs2_quota_change *str = buf;
1200
1201 qc->qc_change = be64_to_cpu(str->qc_change);
1202 qc->qc_flags = be32_to_cpu(str->qc_flags);
e08d8d7f
EB
1203 qc->qc_id = make_kqid(&init_user_ns,
1204 (qc->qc_flags & GFS2_QCF_USER)?USRQUOTA:GRPQUOTA,
1205 be32_to_cpu(str->qc_id));
bb8d8a6f
SW
1206}
1207
b3b94faa
DT
1208int gfs2_quota_init(struct gfs2_sbd *sdp)
1209{
feaa7bba 1210 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
a2e0f799
SW
1211 u64 size = i_size_read(sdp->sd_qc_inode);
1212 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
b3b94faa
DT
1213 unsigned int x, slot = 0;
1214 unsigned int found = 0;
cd915493
SW
1215 u64 dblock;
1216 u32 extlen = 0;
b3b94faa
DT
1217 int error;
1218
a2e0f799 1219 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
907b9bce 1220 return -EIO;
a2e0f799 1221
b3b94faa 1222 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
5c676f6d 1223 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
b3b94faa
DT
1224
1225 error = -ENOMEM;
1226
1227 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
16c5f06f 1228 sizeof(unsigned char *), GFP_NOFS);
b3b94faa
DT
1229 if (!sdp->sd_quota_bitmap)
1230 return error;
1231
1232 for (x = 0; x < sdp->sd_quota_chunks; x++) {
16c5f06f 1233 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
b3b94faa
DT
1234 if (!sdp->sd_quota_bitmap[x])
1235 goto fail;
1236 }
1237
1238 for (x = 0; x < blocks; x++) {
1239 struct buffer_head *bh;
1240 unsigned int y;
1241
1242 if (!extlen) {
1243 int new = 0;
feaa7bba 1244 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
b3b94faa
DT
1245 if (error)
1246 goto fail;
1247 }
b3b94faa 1248 error = -EIO;
7276b3b0
SW
1249 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1250 if (!bh)
1251 goto fail;
b3b94faa
DT
1252 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1253 brelse(bh);
1254 goto fail;
1255 }
1256
7276b3b0 1257 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
b3b94faa 1258 y++, slot++) {
b62f963e 1259 struct gfs2_quota_change_host qc;
b3b94faa
DT
1260 struct gfs2_quota_data *qd;
1261
1262 gfs2_quota_change_in(&qc, bh->b_data +
1263 sizeof(struct gfs2_meta_header) +
1264 y * sizeof(struct gfs2_quota_change));
1265 if (!qc.qc_change)
1266 continue;
1267
1268 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
e08d8d7f 1269 from_kqid(&init_user_ns, qc.qc_id), &qd);
b3b94faa
DT
1270 if (error) {
1271 brelse(bh);
1272 goto fail;
1273 }
1274
1275 set_bit(QDF_CHANGE, &qd->qd_flags);
1276 qd->qd_change = qc.qc_change;
1277 qd->qd_slot = slot;
1278 qd->qd_slot_count = 1;
b3b94faa 1279
0a7ab79c 1280 spin_lock(&qd_lru_lock);
b3b94faa
DT
1281 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1282 list_add(&qd->qd_list, &sdp->sd_quota_list);
1283 atomic_inc(&sdp->sd_quota_count);
0a7ab79c 1284 spin_unlock(&qd_lru_lock);
b3b94faa
DT
1285
1286 found++;
1287 }
1288
1289 brelse(bh);
1290 dblock++;
1291 extlen--;
1292 }
1293
1294 if (found)
1295 fs_info(sdp, "found %u quota changes\n", found);
1296
1297 return 0;
1298
a91ea69f 1299fail:
b3b94faa
DT
1300 gfs2_quota_cleanup(sdp);
1301 return error;
1302}
1303
b3b94faa
DT
1304void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1305{
1306 struct list_head *head = &sdp->sd_quota_list;
1307 struct gfs2_quota_data *qd;
1308 unsigned int x;
1309
0a7ab79c 1310 spin_lock(&qd_lru_lock);
b3b94faa
DT
1311 while (!list_empty(head)) {
1312 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1313
0a7ab79c
AD
1314 if (atomic_read(&qd->qd_count) > 1 ||
1315 (atomic_read(&qd->qd_count) &&
1316 !test_bit(QDF_CHANGE, &qd->qd_flags))) {
0a7ab79c
AD
1317 list_move(&qd->qd_list, head);
1318 spin_unlock(&qd_lru_lock);
b3b94faa 1319 schedule();
0a7ab79c 1320 spin_lock(&qd_lru_lock);
b3b94faa
DT
1321 continue;
1322 }
1323
1324 list_del(&qd->qd_list);
0a7ab79c
AD
1325 /* Also remove if this qd exists in the reclaim list */
1326 if (!list_empty(&qd->qd_reclaim)) {
1327 list_del_init(&qd->qd_reclaim);
1328 atomic_dec(&qd_lru_count);
1329 }
b3b94faa 1330 atomic_dec(&sdp->sd_quota_count);
0a7ab79c 1331 spin_unlock(&qd_lru_lock);
b3b94faa 1332
0a7ab79c 1333 if (!atomic_read(&qd->qd_count)) {
b3b94faa
DT
1334 gfs2_assert_warn(sdp, !qd->qd_change);
1335 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1336 } else
1337 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1338 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1339
f057f6cd 1340 gfs2_glock_put(qd->qd_gl);
37b2c837 1341 kmem_cache_free(gfs2_quotad_cachep, qd);
b3b94faa 1342
0a7ab79c 1343 spin_lock(&qd_lru_lock);
b3b94faa 1344 }
0a7ab79c 1345 spin_unlock(&qd_lru_lock);
b3b94faa
DT
1346
1347 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1348
1349 if (sdp->sd_quota_bitmap) {
1350 for (x = 0; x < sdp->sd_quota_chunks; x++)
1351 kfree(sdp->sd_quota_bitmap[x]);
1352 kfree(sdp->sd_quota_bitmap);
1353 }
1354}
1355
37b2c837
SW
1356static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1357{
1358 if (error == 0 || error == -EROFS)
1359 return;
1360 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1361 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1362}
1363
1364static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
8c42d637 1365 int (*fxn)(struct super_block *sb, int type),
37b2c837
SW
1366 unsigned long t, unsigned long *timeo,
1367 unsigned int *new_timeo)
1368{
1369 if (t >= *timeo) {
8c42d637 1370 int error = fxn(sdp->sd_vfs, 0);
37b2c837
SW
1371 quotad_error(sdp, msg, error);
1372 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1373 } else {
1374 *timeo -= t;
1375 }
1376}
1377
813e0c46
SW
1378static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1379{
1380 struct gfs2_inode *ip;
1381
1382 while(1) {
1383 ip = NULL;
1384 spin_lock(&sdp->sd_trunc_lock);
1385 if (!list_empty(&sdp->sd_trunc_list)) {
1386 ip = list_entry(sdp->sd_trunc_list.next,
1387 struct gfs2_inode, i_trunc_list);
1388 list_del_init(&ip->i_trunc_list);
1389 }
1390 spin_unlock(&sdp->sd_trunc_lock);
1391 if (ip == NULL)
1392 return;
1393 gfs2_glock_finish_truncate(ip);
1394 }
1395}
1396
3d3c10f2
BM
1397void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1398 if (!sdp->sd_statfs_force_sync) {
1399 sdp->sd_statfs_force_sync = 1;
1400 wake_up(&sdp->sd_quota_wait);
1401 }
1402}
1403
1404
37b2c837
SW
1405/**
1406 * gfs2_quotad - Write cached quota changes into the quota file
1407 * @sdp: Pointer to GFS2 superblock
1408 *
1409 */
1410
1411int gfs2_quotad(void *data)
1412{
1413 struct gfs2_sbd *sdp = data;
1414 struct gfs2_tune *tune = &sdp->sd_tune;
1415 unsigned long statfs_timeo = 0;
1416 unsigned long quotad_timeo = 0;
1417 unsigned long t = 0;
1418 DEFINE_WAIT(wait);
813e0c46 1419 int empty;
37b2c837
SW
1420
1421 while (!kthread_should_stop()) {
1422
1423 /* Update the master statfs file */
3d3c10f2
BM
1424 if (sdp->sd_statfs_force_sync) {
1425 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1426 quotad_error(sdp, "statfs", error);
1427 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1428 }
1429 else
1430 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1431 &statfs_timeo,
1432 &tune->gt_statfs_quantum);
37b2c837
SW
1433
1434 /* Update quota file */
5fb324ad 1435 quotad_check_timeo(sdp, "sync", gfs2_quota_sync_timeo, t,
37b2c837
SW
1436 &quotad_timeo, &tune->gt_quota_quantum);
1437
813e0c46
SW
1438 /* Check for & recover partially truncated inodes */
1439 quotad_check_trunc_list(sdp);
1440
a0acae0e
TH
1441 try_to_freeze();
1442
37b2c837
SW
1443 t = min(quotad_timeo, statfs_timeo);
1444
7fa5d20d 1445 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
813e0c46
SW
1446 spin_lock(&sdp->sd_trunc_lock);
1447 empty = list_empty(&sdp->sd_trunc_list);
1448 spin_unlock(&sdp->sd_trunc_lock);
3d3c10f2 1449 if (empty && !sdp->sd_statfs_force_sync)
813e0c46
SW
1450 t -= schedule_timeout(t);
1451 else
1452 t = 0;
37b2c837
SW
1453 finish_wait(&sdp->sd_quota_wait, &wait);
1454 }
1455
1456 return 0;
1457}
1458
1d371b5e
SW
1459static int gfs2_quota_get_xstate(struct super_block *sb,
1460 struct fs_quota_stat *fqs)
1461{
1462 struct gfs2_sbd *sdp = sb->s_fs_info;
1463
1464 memset(fqs, 0, sizeof(struct fs_quota_stat));
1465 fqs->qs_version = FS_QSTAT_VERSION;
ad6bb90f
CH
1466
1467 switch (sdp->sd_args.ar_quota) {
1468 case GFS2_QUOTA_ON:
ade7ce31 1469 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
ad6bb90f
CH
1470 /*FALLTHRU*/
1471 case GFS2_QUOTA_ACCOUNT:
ade7ce31 1472 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
ad6bb90f
CH
1473 break;
1474 case GFS2_QUOTA_OFF:
1475 break;
1476 }
1477
1d371b5e
SW
1478 if (sdp->sd_quota_inode) {
1479 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1480 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1481 }
1482 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1483 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
1484 fqs->qs_incoredqs = atomic_read(&qd_lru_count);
1485 return 0;
1486}
1487
74a8a103 1488static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
b9b2dd36 1489 struct fs_disk_quota *fdq)
113d6b3c
SW
1490{
1491 struct gfs2_sbd *sdp = sb->s_fs_info;
1492 struct gfs2_quota_lvb *qlvb;
1493 struct gfs2_quota_data *qd;
1494 struct gfs2_holder q_gh;
1495 int error;
74a8a103 1496 int type;
113d6b3c
SW
1497
1498 memset(fdq, 0, sizeof(struct fs_disk_quota));
1499
1500 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1501 return -ESRCH; /* Crazy XFS error code */
1502
74a8a103 1503 if (qid.type == USRQUOTA)
113d6b3c 1504 type = QUOTA_USER;
74a8a103 1505 else if (qid.type == GRPQUOTA)
113d6b3c
SW
1506 type = QUOTA_GROUP;
1507 else
1508 return -EINVAL;
1509
74a8a103 1510 error = qd_get(sdp, type, from_kqid(&init_user_ns, qid), &qd);
113d6b3c
SW
1511 if (error)
1512 return error;
1513 error = do_glock(qd, FORCE, &q_gh);
1514 if (error)
1515 goto out;
1516
4e2f8849 1517 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
113d6b3c 1518 fdq->d_version = FS_DQUOT_VERSION;
ade7ce31 1519 fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
558e8528 1520 fdq->d_id = from_kqid_munged(current_user_ns(), qid);
14870b45
AD
1521 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1522 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1523 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
113d6b3c
SW
1524
1525 gfs2_glock_dq_uninit(&q_gh);
1526out:
1527 qd_put(qd);
1528 return error;
1529}
1530
e285c100 1531/* GFS2 only supports a subset of the XFS fields */
802ec9b6 1532#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
e285c100 1533
74a8a103 1534static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
c472b432 1535 struct fs_disk_quota *fdq)
e285c100
SW
1536{
1537 struct gfs2_sbd *sdp = sb->s_fs_info;
1538 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1539 struct gfs2_quota_data *qd;
1540 struct gfs2_holder q_gh, i_gh;
1541 unsigned int data_blocks, ind_blocks;
1542 unsigned int blocks = 0;
1543 int alloc_required;
e285c100
SW
1544 loff_t offset;
1545 int error;
74a8a103 1546 int type;
e285c100
SW
1547
1548 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1549 return -ESRCH; /* Crazy XFS error code */
1550
74a8a103 1551 switch(qid.type) {
e285c100
SW
1552 case USRQUOTA:
1553 type = QUOTA_USER;
e285c100
SW
1554 break;
1555 case GRPQUOTA:
1556 type = QUOTA_GROUP;
e285c100
SW
1557 break;
1558 default:
1559 return -EINVAL;
1560 }
1561
1562 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1563 return -EINVAL;
e285c100 1564
74a8a103 1565 error = qd_get(sdp, type, from_kqid(&init_user_ns, qid), &qd);
e285c100
SW
1566 if (error)
1567 return error;
1568
0a305e49
BP
1569 error = gfs2_rs_alloc(ip);
1570 if (error)
1571 goto out_put;
1572
e285c100
SW
1573 mutex_lock(&ip->i_inode.i_mutex);
1574 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1575 if (error)
0a305e49 1576 goto out_unlockput;
e285c100
SW
1577 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1578 if (error)
1579 goto out_q;
1580
1581 /* Check for existing entry, if none then alloc new blocks */
1582 error = update_qd(sdp, qd);
1583 if (error)
1584 goto out_i;
1585
1586 /* If nothing has changed, this is a no-op */
1587 if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
14870b45 1588 ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
e285c100 1589 fdq->d_fieldmask ^= FS_DQ_BSOFT;
802ec9b6 1590
e285c100 1591 if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
14870b45 1592 ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
e285c100 1593 fdq->d_fieldmask ^= FS_DQ_BHARD;
802ec9b6
AD
1594
1595 if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1596 ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1597 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1598
e285c100
SW
1599 if (fdq->d_fieldmask == 0)
1600 goto out_i;
1601
1602 offset = qd2offset(qd);
461cb419 1603 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
e79a46a0
AD
1604 if (gfs2_is_stuffed(ip))
1605 alloc_required = 1;
e285c100 1606 if (alloc_required) {
e285c100
SW
1607 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1608 &data_blocks, &ind_blocks);
564e12b1 1609 blocks = 1 + data_blocks + ind_blocks;
9dbe9610 1610 error = gfs2_inplace_reserve(ip, blocks, 0);
e285c100 1611 if (error)
564e12b1 1612 goto out_i;
71f890f7 1613 blocks += gfs2_rg_blocks(ip, blocks);
e285c100
SW
1614 }
1615
e79a46a0
AD
1616 /* Some quotas span block boundaries and can update two blocks,
1617 adding an extra block to the transaction to handle such quotas */
1618 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
e285c100
SW
1619 if (error)
1620 goto out_release;
1621
1622 /* Apply changes */
1623 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1624
1625 gfs2_trans_end(sdp);
1626out_release:
564e12b1 1627 if (alloc_required)
e285c100 1628 gfs2_inplace_release(ip);
e285c100
SW
1629out_i:
1630 gfs2_glock_dq_uninit(&i_gh);
1631out_q:
1632 gfs2_glock_dq_uninit(&q_gh);
0a305e49 1633out_unlockput:
e285c100 1634 mutex_unlock(&ip->i_inode.i_mutex);
0a305e49 1635out_put:
e285c100
SW
1636 qd_put(qd);
1637 return error;
1638}
1639
cc632e7f
SW
1640const struct quotactl_ops gfs2_quotactl_ops = {
1641 .quota_sync = gfs2_quota_sync,
1d371b5e 1642 .get_xstate = gfs2_quota_get_xstate,
b9b2dd36 1643 .get_dqblk = gfs2_get_dqblk,
c472b432 1644 .set_dqblk = gfs2_set_dqblk,
cc632e7f 1645};