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