xref: /openbmc/linux/fs/gfs2/quota.c (revision d9612c66)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
5  */
6 
7 /*
8  * Quota change tags are associated with each transaction that allocates or
9  * deallocates space.  Those changes are accumulated locally to each node (in a
10  * per-node file) and then are periodically synced to the quota file.  This
11  * avoids the bottleneck of constantly touching the quota file, but introduces
12  * fuzziness in the current usage value of IDs that are being used on different
13  * nodes in the cluster simultaneously.  So, it is possible for a user on
14  * multiple nodes to overrun their quota, but that overrun is controlable.
15  * Since quota tags are part of transactions, there is no need for a quota check
16  * program to be run on node crashes or anything like that.
17  *
18  * There are couple of knobs that let the administrator manage the quota
19  * fuzziness.  "quota_quantum" sets the maximum time a quota change can be
20  * sitting on one node before being synced to the quota file.  (The default is
21  * 60 seconds.)  Another knob, "quota_scale" controls how quickly the frequency
22  * of quota file syncs increases as the user moves closer to their limit.  The
23  * more frequent the syncs, the more accurate the quota enforcement, but that
24  * means that there is more contention between the nodes for the quota file.
25  * The default value is one.  This sets the maximum theoretical quota overrun
26  * (with infinite node with infinite bandwidth) to twice the user's limit.  (In
27  * practice, the maximum overrun you see should be much less.)  A "quota_scale"
28  * number greater than one makes quota syncs more frequent and reduces the
29  * maximum overrun.  Numbers less than one (but greater than zero) make quota
30  * syncs less frequent.
31  *
32  * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of
33  * the quota file, so it is not being constantly read.
34  */
35 
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/mm.h>
41 #include <linux/spinlock.h>
42 #include <linux/completion.h>
43 #include <linux/buffer_head.h>
44 #include <linux/sort.h>
45 #include <linux/fs.h>
46 #include <linux/bio.h>
47 #include <linux/gfs2_ondisk.h>
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
50 #include <linux/quota.h>
51 #include <linux/dqblk_xfs.h>
52 #include <linux/lockref.h>
53 #include <linux/list_lru.h>
54 #include <linux/rcupdate.h>
55 #include <linux/rculist_bl.h>
56 #include <linux/bit_spinlock.h>
57 #include <linux/jhash.h>
58 #include <linux/vmalloc.h>
59 
60 #include "gfs2.h"
61 #include "incore.h"
62 #include "bmap.h"
63 #include "glock.h"
64 #include "glops.h"
65 #include "log.h"
66 #include "meta_io.h"
67 #include "quota.h"
68 #include "rgrp.h"
69 #include "super.h"
70 #include "trans.h"
71 #include "inode.h"
72 #include "util.h"
73 
74 #define GFS2_QD_HASH_SHIFT      12
75 #define GFS2_QD_HASH_SIZE       BIT(GFS2_QD_HASH_SHIFT)
76 #define GFS2_QD_HASH_MASK       (GFS2_QD_HASH_SIZE - 1)
77 
78 /* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */
79 /*                     -> sd_bitmap_lock                              */
80 static DEFINE_SPINLOCK(qd_lock);
81 struct list_lru gfs2_qd_lru;
82 
83 static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
84 
85 static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
86 				 const struct kqid qid)
87 {
88 	unsigned int h;
89 
90 	h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
91 	h = jhash(&qid, sizeof(struct kqid), h);
92 
93 	return h & GFS2_QD_HASH_MASK;
94 }
95 
96 static inline void spin_lock_bucket(unsigned int hash)
97 {
98         hlist_bl_lock(&qd_hash_table[hash]);
99 }
100 
101 static inline void spin_unlock_bucket(unsigned int hash)
102 {
103         hlist_bl_unlock(&qd_hash_table[hash]);
104 }
105 
106 static void gfs2_qd_dealloc(struct rcu_head *rcu)
107 {
108 	struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
109 	struct gfs2_sbd *sdp = qd->qd_sbd;
110 
111 	kmem_cache_free(gfs2_quotad_cachep, qd);
112 	if (atomic_dec_and_test(&sdp->sd_quota_count))
113 		wake_up(&sdp->sd_kill_wait);
114 }
115 
116 static void gfs2_qd_dispose(struct gfs2_quota_data *qd)
117 {
118 	struct gfs2_sbd *sdp = qd->qd_sbd;
119 
120 	spin_lock(&qd_lock);
121 	list_del(&qd->qd_list);
122 	spin_unlock(&qd_lock);
123 
124 	spin_lock_bucket(qd->qd_hash);
125 	hlist_bl_del_rcu(&qd->qd_hlist);
126 	spin_unlock_bucket(qd->qd_hash);
127 
128 	if (!gfs2_withdrawing_or_withdrawn(sdp)) {
129 		gfs2_assert_warn(sdp, !qd->qd_change);
130 		gfs2_assert_warn(sdp, !qd->qd_slot_ref);
131 		gfs2_assert_warn(sdp, !qd->qd_bh_count);
132 	}
133 
134 	gfs2_glock_put(qd->qd_gl);
135 	call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
136 }
137 
138 static void gfs2_qd_list_dispose(struct list_head *list)
139 {
140 	struct gfs2_quota_data *qd;
141 
142 	while (!list_empty(list)) {
143 		qd = list_first_entry(list, struct gfs2_quota_data, qd_lru);
144 		list_del(&qd->qd_lru);
145 
146 		gfs2_qd_dispose(qd);
147 	}
148 }
149 
150 
151 static enum lru_status gfs2_qd_isolate(struct list_head *item,
152 		struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
153 {
154 	struct list_head *dispose = arg;
155 	struct gfs2_quota_data *qd =
156 		list_entry(item, struct gfs2_quota_data, qd_lru);
157 	enum lru_status status;
158 
159 	if (!spin_trylock(&qd->qd_lockref.lock))
160 		return LRU_SKIP;
161 
162 	status = LRU_SKIP;
163 	if (qd->qd_lockref.count == 0) {
164 		lockref_mark_dead(&qd->qd_lockref);
165 		list_lru_isolate_move(lru, &qd->qd_lru, dispose);
166 		status = LRU_REMOVED;
167 	}
168 
169 	spin_unlock(&qd->qd_lockref.lock);
170 	return status;
171 }
172 
173 static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
174 					 struct shrink_control *sc)
175 {
176 	LIST_HEAD(dispose);
177 	unsigned long freed;
178 
179 	if (!(sc->gfp_mask & __GFP_FS))
180 		return SHRINK_STOP;
181 
182 	freed = list_lru_shrink_walk(&gfs2_qd_lru, sc,
183 				     gfs2_qd_isolate, &dispose);
184 
185 	gfs2_qd_list_dispose(&dispose);
186 
187 	return freed;
188 }
189 
190 static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
191 					  struct shrink_control *sc)
192 {
193 	return vfs_pressure_ratio(list_lru_shrink_count(&gfs2_qd_lru, sc));
194 }
195 
196 struct shrinker gfs2_qd_shrinker = {
197 	.count_objects = gfs2_qd_shrink_count,
198 	.scan_objects = gfs2_qd_shrink_scan,
199 	.seeks = DEFAULT_SEEKS,
200 	.flags = SHRINKER_NUMA_AWARE,
201 };
202 
203 
204 static u64 qd2index(struct gfs2_quota_data *qd)
205 {
206 	struct kqid qid = qd->qd_id;
207 	return (2 * (u64)from_kqid(&init_user_ns, qid)) +
208 		((qid.type == USRQUOTA) ? 0 : 1);
209 }
210 
211 static u64 qd2offset(struct gfs2_quota_data *qd)
212 {
213 	return qd2index(qd) * sizeof(struct gfs2_quota);
214 }
215 
216 static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
217 {
218 	struct gfs2_quota_data *qd;
219 	int error;
220 
221 	qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
222 	if (!qd)
223 		return NULL;
224 
225 	qd->qd_sbd = sdp;
226 	qd->qd_lockref.count = 0;
227 	spin_lock_init(&qd->qd_lockref.lock);
228 	qd->qd_id = qid;
229 	qd->qd_slot = -1;
230 	INIT_LIST_HEAD(&qd->qd_lru);
231 	qd->qd_hash = hash;
232 
233 	error = gfs2_glock_get(sdp, qd2index(qd),
234 			      &gfs2_quota_glops, CREATE, &qd->qd_gl);
235 	if (error)
236 		goto fail;
237 
238 	return qd;
239 
240 fail:
241 	kmem_cache_free(gfs2_quotad_cachep, qd);
242 	return NULL;
243 }
244 
245 static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
246 						     const struct gfs2_sbd *sdp,
247 						     struct kqid qid)
248 {
249 	struct gfs2_quota_data *qd;
250 	struct hlist_bl_node *h;
251 
252 	hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
253 		if (!qid_eq(qd->qd_id, qid))
254 			continue;
255 		if (qd->qd_sbd != sdp)
256 			continue;
257 		if (lockref_get_not_dead(&qd->qd_lockref)) {
258 			list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
259 			return qd;
260 		}
261 	}
262 
263 	return NULL;
264 }
265 
266 
267 static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
268 		  struct gfs2_quota_data **qdp)
269 {
270 	struct gfs2_quota_data *qd, *new_qd;
271 	unsigned int hash = gfs2_qd_hash(sdp, qid);
272 
273 	rcu_read_lock();
274 	*qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
275 	rcu_read_unlock();
276 
277 	if (qd)
278 		return 0;
279 
280 	new_qd = qd_alloc(hash, sdp, qid);
281 	if (!new_qd)
282 		return -ENOMEM;
283 
284 	spin_lock(&qd_lock);
285 	spin_lock_bucket(hash);
286 	*qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
287 	if (qd == NULL) {
288 		new_qd->qd_lockref.count++;
289 		*qdp = new_qd;
290 		list_add(&new_qd->qd_list, &sdp->sd_quota_list);
291 		hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
292 		atomic_inc(&sdp->sd_quota_count);
293 	}
294 	spin_unlock_bucket(hash);
295 	spin_unlock(&qd_lock);
296 
297 	if (qd) {
298 		gfs2_glock_put(new_qd->qd_gl);
299 		kmem_cache_free(gfs2_quotad_cachep, new_qd);
300 	}
301 
302 	return 0;
303 }
304 
305 
306 static void qd_hold(struct gfs2_quota_data *qd)
307 {
308 	struct gfs2_sbd *sdp = qd->qd_sbd;
309 	gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
310 	lockref_get(&qd->qd_lockref);
311 }
312 
313 static void qd_put(struct gfs2_quota_data *qd)
314 {
315 	struct gfs2_sbd *sdp;
316 
317 	if (lockref_put_or_lock(&qd->qd_lockref))
318 		return;
319 
320 	BUG_ON(__lockref_is_dead(&qd->qd_lockref));
321 	sdp = qd->qd_sbd;
322 	if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
323 		lockref_mark_dead(&qd->qd_lockref);
324 		spin_unlock(&qd->qd_lockref.lock);
325 
326 		gfs2_qd_dispose(qd);
327 		return;
328 	}
329 
330 	qd->qd_lockref.count = 0;
331 	list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
332 	spin_unlock(&qd->qd_lockref.lock);
333 }
334 
335 static int slot_get(struct gfs2_quota_data *qd)
336 {
337 	struct gfs2_sbd *sdp = qd->qd_sbd;
338 	unsigned int bit;
339 	int error = 0;
340 
341 	spin_lock(&sdp->sd_bitmap_lock);
342 	if (qd->qd_slot_ref == 0) {
343 		bit = find_first_zero_bit(sdp->sd_quota_bitmap,
344 					  sdp->sd_quota_slots);
345 		if (bit >= sdp->sd_quota_slots) {
346 			error = -ENOSPC;
347 			goto out;
348 		}
349 		set_bit(bit, sdp->sd_quota_bitmap);
350 		qd->qd_slot = bit;
351 	}
352 	qd->qd_slot_ref++;
353 out:
354 	spin_unlock(&sdp->sd_bitmap_lock);
355 	return error;
356 }
357 
358 static void slot_hold(struct gfs2_quota_data *qd)
359 {
360 	struct gfs2_sbd *sdp = qd->qd_sbd;
361 
362 	spin_lock(&sdp->sd_bitmap_lock);
363 	gfs2_assert(sdp, qd->qd_slot_ref);
364 	qd->qd_slot_ref++;
365 	spin_unlock(&sdp->sd_bitmap_lock);
366 }
367 
368 static void slot_put(struct gfs2_quota_data *qd)
369 {
370 	struct gfs2_sbd *sdp = qd->qd_sbd;
371 
372 	spin_lock(&sdp->sd_bitmap_lock);
373 	gfs2_assert(sdp, qd->qd_slot_ref);
374 	if (!--qd->qd_slot_ref) {
375 		BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap));
376 		qd->qd_slot = -1;
377 	}
378 	spin_unlock(&sdp->sd_bitmap_lock);
379 }
380 
381 static int bh_get(struct gfs2_quota_data *qd)
382 {
383 	struct gfs2_sbd *sdp = qd->qd_sbd;
384 	struct inode *inode = sdp->sd_qc_inode;
385 	struct gfs2_inode *ip = GFS2_I(inode);
386 	unsigned int block, offset;
387 	struct buffer_head *bh;
388 	struct iomap iomap = { };
389 	int error;
390 
391 	mutex_lock(&sdp->sd_quota_mutex);
392 
393 	if (qd->qd_bh_count++) {
394 		mutex_unlock(&sdp->sd_quota_mutex);
395 		return 0;
396 	}
397 
398 	block = qd->qd_slot / sdp->sd_qc_per_block;
399 	offset = qd->qd_slot % sdp->sd_qc_per_block;
400 
401 	error = gfs2_iomap_get(inode,
402 			       (loff_t)block << inode->i_blkbits,
403 			       i_blocksize(inode), &iomap);
404 	if (error)
405 		goto fail;
406 	error = -ENOENT;
407 	if (iomap.type != IOMAP_MAPPED)
408 		goto fail;
409 
410 	error = gfs2_meta_read(ip->i_gl, iomap.addr >> inode->i_blkbits,
411 			       DIO_WAIT, 0, &bh);
412 	if (error)
413 		goto fail;
414 	error = -EIO;
415 	if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
416 		goto fail_brelse;
417 
418 	qd->qd_bh = bh;
419 	qd->qd_bh_qc = (struct gfs2_quota_change *)
420 		(bh->b_data + sizeof(struct gfs2_meta_header) +
421 		 offset * sizeof(struct gfs2_quota_change));
422 
423 	mutex_unlock(&sdp->sd_quota_mutex);
424 
425 	return 0;
426 
427 fail_brelse:
428 	brelse(bh);
429 fail:
430 	qd->qd_bh_count--;
431 	mutex_unlock(&sdp->sd_quota_mutex);
432 	return error;
433 }
434 
435 static void bh_put(struct gfs2_quota_data *qd)
436 {
437 	struct gfs2_sbd *sdp = qd->qd_sbd;
438 
439 	mutex_lock(&sdp->sd_quota_mutex);
440 	gfs2_assert(sdp, qd->qd_bh_count);
441 	if (!--qd->qd_bh_count) {
442 		brelse(qd->qd_bh);
443 		qd->qd_bh = NULL;
444 		qd->qd_bh_qc = NULL;
445 	}
446 	mutex_unlock(&sdp->sd_quota_mutex);
447 }
448 
449 static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
450 			 u64 *sync_gen)
451 {
452 	if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
453 	    !test_bit(QDF_CHANGE, &qd->qd_flags) ||
454 	    (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
455 		return 0;
456 
457 	/*
458 	 * If qd_change is 0 it means a pending quota change was negated.
459 	 * We should not sync it, but we still have a qd reference and slot
460 	 * reference taken by gfs2_quota_change -> do_qc that need to be put.
461 	 */
462 	if (!qd->qd_change && test_and_clear_bit(QDF_CHANGE, &qd->qd_flags)) {
463 		slot_put(qd);
464 		qd_put(qd);
465 		return 0;
466 	}
467 
468 	if (!lockref_get_not_dead(&qd->qd_lockref))
469 		return 0;
470 
471 	list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
472 	set_bit(QDF_LOCKED, &qd->qd_flags);
473 	qd->qd_change_sync = qd->qd_change;
474 	slot_hold(qd);
475 	return 1;
476 }
477 
478 static int qd_bh_get_or_undo(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
479 {
480 	int error;
481 
482 	error = bh_get(qd);
483 	if (!error)
484 		return 0;
485 
486 	clear_bit(QDF_LOCKED, &qd->qd_flags);
487 	slot_put(qd);
488 	qd_put(qd);
489 	return error;
490 }
491 
492 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
493 {
494 	struct gfs2_quota_data *qd = NULL, *iter;
495 	int error;
496 
497 	*qdp = NULL;
498 
499 	if (sb_rdonly(sdp->sd_vfs))
500 		return 0;
501 
502 	spin_lock(&qd_lock);
503 
504 	list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
505 		if (qd_check_sync(sdp, iter, &sdp->sd_quota_sync_gen)) {
506 			qd = iter;
507 			break;
508 		}
509 	}
510 
511 	spin_unlock(&qd_lock);
512 
513 	if (qd) {
514 		error = qd_bh_get_or_undo(sdp, qd);
515 		if (error)
516 			return error;
517 		*qdp = qd;
518 	}
519 
520 	return 0;
521 }
522 
523 static void qdsb_put(struct gfs2_quota_data *qd)
524 {
525 	bh_put(qd);
526 	slot_put(qd);
527 	qd_put(qd);
528 }
529 
530 static void qd_unlock(struct gfs2_quota_data *qd)
531 {
532 	gfs2_assert_warn(qd->qd_sbd, test_bit(QDF_LOCKED, &qd->qd_flags));
533 	clear_bit(QDF_LOCKED, &qd->qd_flags);
534 	qdsb_put(qd);
535 }
536 
537 static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
538 		    struct gfs2_quota_data **qdp)
539 {
540 	int error;
541 
542 	error = qd_get(sdp, qid, qdp);
543 	if (error)
544 		return error;
545 
546 	error = slot_get(*qdp);
547 	if (error)
548 		goto fail;
549 
550 	error = bh_get(*qdp);
551 	if (error)
552 		goto fail_slot;
553 
554 	return 0;
555 
556 fail_slot:
557 	slot_put(*qdp);
558 fail:
559 	qd_put(*qdp);
560 	return error;
561 }
562 
563 /**
564  * gfs2_qa_get - make sure we have a quota allocations data structure,
565  *               if necessary
566  * @ip: the inode for this reservation
567  */
568 int gfs2_qa_get(struct gfs2_inode *ip)
569 {
570 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
571 	struct inode *inode = &ip->i_inode;
572 
573 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
574 		return 0;
575 
576 	spin_lock(&inode->i_lock);
577 	if (ip->i_qadata == NULL) {
578 		struct gfs2_qadata *tmp;
579 
580 		spin_unlock(&inode->i_lock);
581 		tmp = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
582 		if (!tmp)
583 			return -ENOMEM;
584 
585 		spin_lock(&inode->i_lock);
586 		if (ip->i_qadata == NULL)
587 			ip->i_qadata = tmp;
588 		else
589 			kmem_cache_free(gfs2_qadata_cachep, tmp);
590 	}
591 	ip->i_qadata->qa_ref++;
592 	spin_unlock(&inode->i_lock);
593 	return 0;
594 }
595 
596 void gfs2_qa_put(struct gfs2_inode *ip)
597 {
598 	struct inode *inode = &ip->i_inode;
599 
600 	spin_lock(&inode->i_lock);
601 	if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) {
602 		kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
603 		ip->i_qadata = NULL;
604 	}
605 	spin_unlock(&inode->i_lock);
606 }
607 
608 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
609 {
610 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
611 	struct gfs2_quota_data **qd;
612 	int error;
613 
614 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
615 		return 0;
616 
617 	error = gfs2_qa_get(ip);
618 	if (error)
619 		return error;
620 
621 	qd = ip->i_qadata->qa_qd;
622 
623 	if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) ||
624 	    gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) {
625 		error = -EIO;
626 		gfs2_qa_put(ip);
627 		goto out;
628 	}
629 
630 	error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
631 	if (error)
632 		goto out_unhold;
633 	ip->i_qadata->qa_qd_num++;
634 	qd++;
635 
636 	error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
637 	if (error)
638 		goto out_unhold;
639 	ip->i_qadata->qa_qd_num++;
640 	qd++;
641 
642 	if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
643 	    !uid_eq(uid, ip->i_inode.i_uid)) {
644 		error = qdsb_get(sdp, make_kqid_uid(uid), qd);
645 		if (error)
646 			goto out_unhold;
647 		ip->i_qadata->qa_qd_num++;
648 		qd++;
649 	}
650 
651 	if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
652 	    !gid_eq(gid, ip->i_inode.i_gid)) {
653 		error = qdsb_get(sdp, make_kqid_gid(gid), qd);
654 		if (error)
655 			goto out_unhold;
656 		ip->i_qadata->qa_qd_num++;
657 		qd++;
658 	}
659 
660 out_unhold:
661 	if (error)
662 		gfs2_quota_unhold(ip);
663 out:
664 	return error;
665 }
666 
667 void gfs2_quota_unhold(struct gfs2_inode *ip)
668 {
669 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
670 	u32 x;
671 
672 	if (ip->i_qadata == NULL)
673 		return;
674 
675 	gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
676 
677 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
678 		qdsb_put(ip->i_qadata->qa_qd[x]);
679 		ip->i_qadata->qa_qd[x] = NULL;
680 	}
681 	ip->i_qadata->qa_qd_num = 0;
682 	gfs2_qa_put(ip);
683 }
684 
685 static int sort_qd(const void *a, const void *b)
686 {
687 	const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
688 	const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
689 
690 	if (qid_lt(qd_a->qd_id, qd_b->qd_id))
691 		return -1;
692 	if (qid_lt(qd_b->qd_id, qd_a->qd_id))
693 		return 1;
694 	return 0;
695 }
696 
697 static void do_qc(struct gfs2_quota_data *qd, s64 change)
698 {
699 	struct gfs2_sbd *sdp = qd->qd_sbd;
700 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
701 	struct gfs2_quota_change *qc = qd->qd_bh_qc;
702 	s64 x;
703 
704 	mutex_lock(&sdp->sd_quota_mutex);
705 	gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
706 
707 	if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
708 		qc->qc_change = 0;
709 		qc->qc_flags = 0;
710 		if (qd->qd_id.type == USRQUOTA)
711 			qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
712 		qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
713 	}
714 
715 	x = be64_to_cpu(qc->qc_change) + change;
716 	qc->qc_change = cpu_to_be64(x);
717 
718 	spin_lock(&qd_lock);
719 	qd->qd_change = x;
720 	spin_unlock(&qd_lock);
721 
722 	if (!x) {
723 		gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
724 		clear_bit(QDF_CHANGE, &qd->qd_flags);
725 		qc->qc_flags = 0;
726 		qc->qc_id = 0;
727 		slot_put(qd);
728 		qd_put(qd);
729 	} else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
730 		qd_hold(qd);
731 		slot_hold(qd);
732 	}
733 
734 	if (change < 0) /* Reset quiet flag if we freed some blocks */
735 		clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
736 	mutex_unlock(&sdp->sd_quota_mutex);
737 }
738 
739 static int gfs2_write_buf_to_page(struct gfs2_sbd *sdp, unsigned long index,
740 				  unsigned off, void *buf, unsigned bytes)
741 {
742 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
743 	struct inode *inode = &ip->i_inode;
744 	struct address_space *mapping = inode->i_mapping;
745 	struct page *page;
746 	struct buffer_head *bh;
747 	u64 blk;
748 	unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0;
749 	unsigned to_write = bytes, pg_off = off;
750 
751 	blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift);
752 	boff = off % bsize;
753 
754 	page = grab_cache_page(mapping, index);
755 	if (!page)
756 		return -ENOMEM;
757 	if (!page_has_buffers(page))
758 		create_empty_buffers(page, bsize, 0);
759 
760 	bh = page_buffers(page);
761 	for(;;) {
762 		/* Find the beginning block within the page */
763 		if (pg_off >= ((bnum * bsize) + bsize)) {
764 			bh = bh->b_this_page;
765 			bnum++;
766 			blk++;
767 			continue;
768 		}
769 		if (!buffer_mapped(bh)) {
770 			gfs2_block_map(inode, blk, bh, 1);
771 			if (!buffer_mapped(bh))
772 				goto unlock_out;
773 			/* If it's a newly allocated disk block, zero it */
774 			if (buffer_new(bh))
775 				zero_user(page, bnum * bsize, bh->b_size);
776 		}
777 		if (PageUptodate(page))
778 			set_buffer_uptodate(bh);
779 		if (bh_read(bh, REQ_META | REQ_PRIO) < 0)
780 			goto unlock_out;
781 		gfs2_trans_add_data(ip->i_gl, bh);
782 
783 		/* If we need to write to the next block as well */
784 		if (to_write > (bsize - boff)) {
785 			pg_off += (bsize - boff);
786 			to_write -= (bsize - boff);
787 			boff = pg_off % bsize;
788 			continue;
789 		}
790 		break;
791 	}
792 
793 	/* Write to the page, now that we have setup the buffer(s) */
794 	memcpy_to_page(page, off, buf, bytes);
795 	flush_dcache_page(page);
796 	unlock_page(page);
797 	put_page(page);
798 
799 	return 0;
800 
801 unlock_out:
802 	unlock_page(page);
803 	put_page(page);
804 	return -EIO;
805 }
806 
807 static int gfs2_write_disk_quota(struct gfs2_sbd *sdp, struct gfs2_quota *qp,
808 				 loff_t loc)
809 {
810 	unsigned long pg_beg;
811 	unsigned pg_off, nbytes, overflow = 0;
812 	int error;
813 	void *ptr;
814 
815 	nbytes = sizeof(struct gfs2_quota);
816 
817 	pg_beg = loc >> PAGE_SHIFT;
818 	pg_off = offset_in_page(loc);
819 
820 	/* If the quota straddles a page boundary, split the write in two */
821 	if ((pg_off + nbytes) > PAGE_SIZE)
822 		overflow = (pg_off + nbytes) - PAGE_SIZE;
823 
824 	ptr = qp;
825 	error = gfs2_write_buf_to_page(sdp, pg_beg, pg_off, ptr,
826 				       nbytes - overflow);
827 	/* If there's an overflow, write the remaining bytes to the next page */
828 	if (!error && overflow)
829 		error = gfs2_write_buf_to_page(sdp, pg_beg + 1, 0,
830 					       ptr + nbytes - overflow,
831 					       overflow);
832 	return error;
833 }
834 
835 /**
836  * gfs2_adjust_quota - adjust record of current block usage
837  * @sdp: The superblock
838  * @loc: Offset of the entry in the quota file
839  * @change: The amount of usage change to record
840  * @qd: The quota data
841  * @fdq: The updated limits to record
842  *
843  * This function was mostly borrowed from gfs2_block_truncate_page which was
844  * in turn mostly borrowed from ext3
845  *
846  * Returns: 0 or -ve on error
847  */
848 
849 static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc,
850 			     s64 change, struct gfs2_quota_data *qd,
851 			     struct qc_dqblk *fdq)
852 {
853 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
854 	struct inode *inode = &ip->i_inode;
855 	struct gfs2_quota q;
856 	int err;
857 	u64 size;
858 
859 	if (gfs2_is_stuffed(ip)) {
860 		err = gfs2_unstuff_dinode(ip);
861 		if (err)
862 			return err;
863 	}
864 
865 	memset(&q, 0, sizeof(struct gfs2_quota));
866 	err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
867 	if (err < 0)
868 		return err;
869 
870 	loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */
871 	be64_add_cpu(&q.qu_value, change);
872 	if (((s64)be64_to_cpu(q.qu_value)) < 0)
873 		q.qu_value = 0; /* Never go negative on quota usage */
874 	qd->qd_qb.qb_value = q.qu_value;
875 	if (fdq) {
876 		if (fdq->d_fieldmask & QC_SPC_SOFT) {
877 			q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift);
878 			qd->qd_qb.qb_warn = q.qu_warn;
879 		}
880 		if (fdq->d_fieldmask & QC_SPC_HARD) {
881 			q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift);
882 			qd->qd_qb.qb_limit = q.qu_limit;
883 		}
884 		if (fdq->d_fieldmask & QC_SPACE) {
885 			q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift);
886 			qd->qd_qb.qb_value = q.qu_value;
887 		}
888 	}
889 
890 	err = gfs2_write_disk_quota(sdp, &q, loc);
891 	if (!err) {
892 		size = loc + sizeof(struct gfs2_quota);
893 		if (size > inode->i_size)
894 			i_size_write(inode, size);
895 		inode->i_mtime = inode_set_ctime_current(inode);
896 		mark_inode_dirty(inode);
897 		set_bit(QDF_REFRESH, &qd->qd_flags);
898 	}
899 
900 	return err;
901 }
902 
903 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
904 {
905 	struct gfs2_sbd *sdp = (*qda)->qd_sbd;
906 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
907 	struct gfs2_alloc_parms ap = { .aflags = 0, };
908 	unsigned int data_blocks, ind_blocks;
909 	struct gfs2_holder *ghs, i_gh;
910 	unsigned int qx, x;
911 	struct gfs2_quota_data *qd;
912 	unsigned reserved;
913 	loff_t offset;
914 	unsigned int nalloc = 0, blocks;
915 	int error;
916 
917 	gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
918 			      &data_blocks, &ind_blocks);
919 
920 	ghs = kmalloc_array(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
921 	if (!ghs)
922 		return -ENOMEM;
923 
924 	sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
925 	inode_lock(&ip->i_inode);
926 	for (qx = 0; qx < num_qd; qx++) {
927 		error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
928 					   GL_NOCACHE, &ghs[qx]);
929 		if (error)
930 			goto out_dq;
931 	}
932 
933 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
934 	if (error)
935 		goto out_dq;
936 
937 	for (x = 0; x < num_qd; x++) {
938 		offset = qd2offset(qda[x]);
939 		if (gfs2_write_alloc_required(ip, offset,
940 					      sizeof(struct gfs2_quota)))
941 			nalloc++;
942 	}
943 
944 	/*
945 	 * 1 blk for unstuffing inode if stuffed. We add this extra
946 	 * block to the reservation unconditionally. If the inode
947 	 * doesn't need unstuffing, the block will be released to the
948 	 * rgrp since it won't be allocated during the transaction
949 	 */
950 	/* +3 in the end for unstuffing block, inode size update block
951 	 * and another block in case quota straddles page boundary and
952 	 * two blocks need to be updated instead of 1 */
953 	blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
954 
955 	reserved = 1 + (nalloc * (data_blocks + ind_blocks));
956 	ap.target = reserved;
957 	error = gfs2_inplace_reserve(ip, &ap);
958 	if (error)
959 		goto out_alloc;
960 
961 	if (nalloc)
962 		blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
963 
964 	error = gfs2_trans_begin(sdp, blocks, 0);
965 	if (error)
966 		goto out_ipres;
967 
968 	for (x = 0; x < num_qd; x++) {
969 		qd = qda[x];
970 		offset = qd2offset(qd);
971 		error = gfs2_adjust_quota(sdp, offset, qd->qd_change_sync, qd,
972 							NULL);
973 		if (error)
974 			goto out_end_trans;
975 
976 		do_qc(qd, -qd->qd_change_sync);
977 		set_bit(QDF_REFRESH, &qd->qd_flags);
978 	}
979 
980 out_end_trans:
981 	gfs2_trans_end(sdp);
982 out_ipres:
983 	gfs2_inplace_release(ip);
984 out_alloc:
985 	gfs2_glock_dq_uninit(&i_gh);
986 out_dq:
987 	while (qx--)
988 		gfs2_glock_dq_uninit(&ghs[qx]);
989 	inode_unlock(&ip->i_inode);
990 	kfree(ghs);
991 	gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl,
992 		       GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
993 	if (!error) {
994 		for (x = 0; x < num_qd; x++)
995 			qda[x]->qd_sync_gen = sdp->sd_quota_sync_gen;
996 	}
997 	return error;
998 }
999 
1000 static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
1001 {
1002 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1003 	struct gfs2_quota q;
1004 	struct gfs2_quota_lvb *qlvb;
1005 	loff_t pos;
1006 	int error;
1007 
1008 	memset(&q, 0, sizeof(struct gfs2_quota));
1009 	pos = qd2offset(qd);
1010 	error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
1011 	if (error < 0)
1012 		return error;
1013 
1014 	qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1015 	qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
1016 	qlvb->__pad = 0;
1017 	qlvb->qb_limit = q.qu_limit;
1018 	qlvb->qb_warn = q.qu_warn;
1019 	qlvb->qb_value = q.qu_value;
1020 	qd->qd_qb = *qlvb;
1021 
1022 	return 0;
1023 }
1024 
1025 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
1026 		    struct gfs2_holder *q_gh)
1027 {
1028 	struct gfs2_sbd *sdp = qd->qd_sbd;
1029 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1030 	struct gfs2_holder i_gh;
1031 	int error;
1032 
1033 	gfs2_assert_warn(sdp, sdp == qd->qd_gl->gl_name.ln_sbd);
1034 restart:
1035 	error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
1036 	if (error)
1037 		return error;
1038 
1039 	if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
1040 		force_refresh = FORCE;
1041 
1042 	qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1043 
1044 	if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
1045 		gfs2_glock_dq_uninit(q_gh);
1046 		error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
1047 					   GL_NOCACHE, q_gh);
1048 		if (error)
1049 			return error;
1050 
1051 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1052 		if (error)
1053 			goto fail;
1054 
1055 		error = update_qd(sdp, qd);
1056 		if (error)
1057 			goto fail_gunlock;
1058 
1059 		gfs2_glock_dq_uninit(&i_gh);
1060 		gfs2_glock_dq_uninit(q_gh);
1061 		force_refresh = 0;
1062 		goto restart;
1063 	}
1064 
1065 	return 0;
1066 
1067 fail_gunlock:
1068 	gfs2_glock_dq_uninit(&i_gh);
1069 fail:
1070 	gfs2_glock_dq_uninit(q_gh);
1071 	return error;
1072 }
1073 
1074 int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
1075 {
1076 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1077 	struct gfs2_quota_data *qd;
1078 	u32 x;
1079 	int error;
1080 
1081 	if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON &&
1082 	    sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET)
1083 		return 0;
1084 
1085 	error = gfs2_quota_hold(ip, uid, gid);
1086 	if (error)
1087 		return error;
1088 
1089 	sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num,
1090 	     sizeof(struct gfs2_quota_data *), sort_qd, NULL);
1091 
1092 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1093 		qd = ip->i_qadata->qa_qd[x];
1094 		error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]);
1095 		if (error)
1096 			break;
1097 	}
1098 
1099 	if (!error)
1100 		set_bit(GIF_QD_LOCKED, &ip->i_flags);
1101 	else {
1102 		while (x--)
1103 			gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
1104 		gfs2_quota_unhold(ip);
1105 	}
1106 
1107 	return error;
1108 }
1109 
1110 static bool need_sync(struct gfs2_quota_data *qd)
1111 {
1112 	struct gfs2_sbd *sdp = qd->qd_sbd;
1113 	struct gfs2_tune *gt = &sdp->sd_tune;
1114 	s64 value;
1115 	unsigned int num, den;
1116 
1117 	if (!qd->qd_qb.qb_limit)
1118 		return false;
1119 
1120 	spin_lock(&qd_lock);
1121 	value = qd->qd_change;
1122 	spin_unlock(&qd_lock);
1123 
1124 	spin_lock(&gt->gt_spin);
1125 	num = gt->gt_quota_scale_num;
1126 	den = gt->gt_quota_scale_den;
1127 	spin_unlock(&gt->gt_spin);
1128 
1129 	if (value <= 0)
1130 		return false;
1131 	else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1132 		 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1133 		return false;
1134 	else {
1135 		value *= gfs2_jindex_size(sdp) * num;
1136 		value = div_s64(value, den);
1137 		value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
1138 		if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1139 			return false;
1140 	}
1141 
1142 	return true;
1143 }
1144 
1145 void gfs2_quota_unlock(struct gfs2_inode *ip)
1146 {
1147 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1148 	struct gfs2_quota_data *qda[2 * GFS2_MAXQUOTAS];
1149 	unsigned int count = 0;
1150 	u32 x;
1151 	int found;
1152 
1153 	if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1154 		return;
1155 
1156 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1157 		struct gfs2_quota_data *qd;
1158 		bool sync;
1159 
1160 		qd = ip->i_qadata->qa_qd[x];
1161 		sync = need_sync(qd);
1162 
1163 		gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
1164 		if (!sync)
1165 			continue;
1166 
1167 		spin_lock(&qd_lock);
1168 		found = qd_check_sync(sdp, qd, NULL);
1169 		spin_unlock(&qd_lock);
1170 
1171 		if (!found)
1172 			continue;
1173 
1174 		if (!qd_bh_get_or_undo(sdp, qd))
1175 			qda[count++] = qd;
1176 	}
1177 
1178 	if (count) {
1179 		do_sync(count, qda);
1180 		for (x = 0; x < count; x++)
1181 			qd_unlock(qda[x]);
1182 	}
1183 
1184 	gfs2_quota_unhold(ip);
1185 }
1186 
1187 #define MAX_LINE 256
1188 
1189 static int print_message(struct gfs2_quota_data *qd, char *type)
1190 {
1191 	struct gfs2_sbd *sdp = qd->qd_sbd;
1192 
1193 	if (sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET)
1194 		fs_info(sdp, "quota %s for %s %u\n",
1195 			type,
1196 			(qd->qd_id.type == USRQUOTA) ? "user" : "group",
1197 			from_kqid(&init_user_ns, qd->qd_id));
1198 
1199 	return 0;
1200 }
1201 
1202 /**
1203  * gfs2_quota_check - check if allocating new blocks will exceed quota
1204  * @ip:  The inode for which this check is being performed
1205  * @uid: The uid to check against
1206  * @gid: The gid to check against
1207  * @ap:  The allocation parameters. ap->target contains the requested
1208  *       blocks. ap->min_target, if set, contains the minimum blks
1209  *       requested.
1210  *
1211  * Returns: 0 on success.
1212  *                  min_req = ap->min_target ? ap->min_target : ap->target;
1213  *                  quota must allow at least min_req blks for success and
1214  *                  ap->allowed is set to the number of blocks allowed
1215  *
1216  *          -EDQUOT otherwise, quota violation. ap->allowed is set to number
1217  *                  of blocks available.
1218  */
1219 int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
1220 		     struct gfs2_alloc_parms *ap)
1221 {
1222 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1223 	struct gfs2_quota_data *qd;
1224 	s64 value, warn, limit;
1225 	u32 x;
1226 	int error = 0;
1227 
1228 	ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */
1229 	if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1230 		return 0;
1231 
1232 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1233 		qd = ip->i_qadata->qa_qd[x];
1234 
1235 		if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1236 		      qid_eq(qd->qd_id, make_kqid_gid(gid))))
1237 			continue;
1238 
1239 		warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
1240 		limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
1241 		value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
1242 		spin_lock(&qd_lock);
1243 		value += qd->qd_change;
1244 		spin_unlock(&qd_lock);
1245 
1246 		if (limit > 0 && (limit - value) < ap->allowed)
1247 			ap->allowed = limit - value;
1248 		/* If we can't meet the target */
1249 		if (limit && limit < (value + (s64)ap->target)) {
1250 			/* If no min_target specified or we don't meet
1251 			 * min_target, return -EDQUOT */
1252 			if (!ap->min_target || ap->min_target > ap->allowed) {
1253 				if (!test_and_set_bit(QDF_QMSG_QUIET,
1254 						      &qd->qd_flags)) {
1255 					print_message(qd, "exceeded");
1256 					quota_send_warning(qd->qd_id,
1257 							   sdp->sd_vfs->s_dev,
1258 							   QUOTA_NL_BHARDWARN);
1259 				}
1260 				error = -EDQUOT;
1261 				break;
1262 			}
1263 		} else if (warn && warn < value &&
1264 			   time_after_eq(jiffies, qd->qd_last_warn +
1265 					 gfs2_tune_get(sdp, gt_quota_warn_period)
1266 					 * HZ)) {
1267 			quota_send_warning(qd->qd_id,
1268 					   sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
1269 			error = print_message(qd, "warning");
1270 			qd->qd_last_warn = jiffies;
1271 		}
1272 	}
1273 	return error;
1274 }
1275 
1276 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1277 		       kuid_t uid, kgid_t gid)
1278 {
1279 	struct gfs2_quota_data *qd;
1280 	u32 x;
1281 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1282 
1283 	if ((sdp->sd_args.ar_quota != GFS2_QUOTA_ON &&
1284 	    sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET) ||
1285 	    gfs2_assert_warn(sdp, change))
1286 		return;
1287 	if (ip->i_diskflags & GFS2_DIF_SYSTEM)
1288 		return;
1289 
1290 	if (gfs2_assert_withdraw(sdp, ip->i_qadata &&
1291 				 ip->i_qadata->qa_ref > 0))
1292 		return;
1293 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1294 		qd = ip->i_qadata->qa_qd[x];
1295 
1296 		if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1297 		    qid_eq(qd->qd_id, make_kqid_gid(gid))) {
1298 			do_qc(qd, change);
1299 		}
1300 	}
1301 }
1302 
1303 static bool qd_changed(struct gfs2_sbd *sdp)
1304 {
1305 	struct gfs2_quota_data *qd;
1306 	bool changed = false;
1307 
1308 	spin_lock(&qd_lock);
1309 	list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
1310 		if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
1311 		    !test_bit(QDF_CHANGE, &qd->qd_flags))
1312 			continue;
1313 
1314 		changed = true;
1315 		break;
1316 	}
1317 	spin_unlock(&qd_lock);
1318 	return changed;
1319 }
1320 
1321 int gfs2_quota_sync(struct super_block *sb, int type)
1322 {
1323 	struct gfs2_sbd *sdp = sb->s_fs_info;
1324 	struct gfs2_quota_data **qda;
1325 	unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder);
1326 	unsigned int num_qd;
1327 	unsigned int x;
1328 	int error = 0;
1329 
1330 	if (!qd_changed(sdp))
1331 		return 0;
1332 
1333 	qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1334 	if (!qda)
1335 		return -ENOMEM;
1336 
1337 	mutex_lock(&sdp->sd_quota_sync_mutex);
1338 	sdp->sd_quota_sync_gen++;
1339 
1340 	do {
1341 		num_qd = 0;
1342 
1343 		for (;;) {
1344 			error = qd_fish(sdp, qda + num_qd);
1345 			if (error || !qda[num_qd])
1346 				break;
1347 			if (++num_qd == max_qd)
1348 				break;
1349 		}
1350 
1351 		if (num_qd) {
1352 			if (!error)
1353 				error = do_sync(num_qd, qda);
1354 
1355 			for (x = 0; x < num_qd; x++)
1356 				qd_unlock(qda[x]);
1357 		}
1358 	} while (!error && num_qd == max_qd);
1359 
1360 	mutex_unlock(&sdp->sd_quota_sync_mutex);
1361 	kfree(qda);
1362 
1363 	return error;
1364 }
1365 
1366 int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
1367 {
1368 	struct gfs2_quota_data *qd;
1369 	struct gfs2_holder q_gh;
1370 	int error;
1371 
1372 	error = qd_get(sdp, qid, &qd);
1373 	if (error)
1374 		return error;
1375 
1376 	error = do_glock(qd, FORCE, &q_gh);
1377 	if (!error)
1378 		gfs2_glock_dq_uninit(&q_gh);
1379 
1380 	qd_put(qd);
1381 	return error;
1382 }
1383 
1384 int gfs2_quota_init(struct gfs2_sbd *sdp)
1385 {
1386 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1387 	u64 size = i_size_read(sdp->sd_qc_inode);
1388 	unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
1389 	unsigned int x, slot = 0;
1390 	unsigned int found = 0;
1391 	unsigned int hash;
1392 	unsigned int bm_size;
1393 	u64 dblock;
1394 	u32 extlen = 0;
1395 	int error;
1396 
1397 	if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
1398 		return -EIO;
1399 
1400 	sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1401 	bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
1402 	bm_size *= sizeof(unsigned long);
1403 	error = -ENOMEM;
1404 	sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
1405 	if (sdp->sd_quota_bitmap == NULL)
1406 		sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
1407 						 __GFP_ZERO);
1408 	if (!sdp->sd_quota_bitmap)
1409 		return error;
1410 
1411 	for (x = 0; x < blocks; x++) {
1412 		struct buffer_head *bh;
1413 		const struct gfs2_quota_change *qc;
1414 		unsigned int y;
1415 
1416 		if (!extlen) {
1417 			extlen = 32;
1418 			error = gfs2_get_extent(&ip->i_inode, x, &dblock, &extlen);
1419 			if (error)
1420 				goto fail;
1421 		}
1422 		error = -EIO;
1423 		bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1424 		if (!bh)
1425 			goto fail;
1426 		if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1427 			brelse(bh);
1428 			goto fail;
1429 		}
1430 
1431 		qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
1432 		for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1433 		     y++, slot++) {
1434 			struct gfs2_quota_data *qd;
1435 			s64 qc_change = be64_to_cpu(qc->qc_change);
1436 			u32 qc_flags = be32_to_cpu(qc->qc_flags);
1437 			enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1438 						USRQUOTA : GRPQUOTA;
1439 			struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1440 						      be32_to_cpu(qc->qc_id));
1441 			qc++;
1442 			if (!qc_change)
1443 				continue;
1444 
1445 			hash = gfs2_qd_hash(sdp, qc_id);
1446 			qd = qd_alloc(hash, sdp, qc_id);
1447 			if (qd == NULL) {
1448 				brelse(bh);
1449 				goto fail;
1450 			}
1451 
1452 			set_bit(QDF_CHANGE, &qd->qd_flags);
1453 			qd->qd_change = qc_change;
1454 			qd->qd_slot = slot;
1455 			qd->qd_slot_ref = 1;
1456 
1457 			spin_lock(&qd_lock);
1458 			BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
1459 			list_add(&qd->qd_list, &sdp->sd_quota_list);
1460 			atomic_inc(&sdp->sd_quota_count);
1461 			spin_unlock(&qd_lock);
1462 
1463 			spin_lock_bucket(hash);
1464 			hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
1465 			spin_unlock_bucket(hash);
1466 
1467 			found++;
1468 		}
1469 
1470 		brelse(bh);
1471 		dblock++;
1472 		extlen--;
1473 	}
1474 
1475 	if (found)
1476 		fs_info(sdp, "found %u quota changes\n", found);
1477 
1478 	return 0;
1479 
1480 fail:
1481 	gfs2_quota_cleanup(sdp);
1482 	return error;
1483 }
1484 
1485 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1486 {
1487 	struct gfs2_quota_data *qd;
1488 	LIST_HEAD(dispose);
1489 	int count;
1490 
1491 	BUG_ON(!test_bit(SDF_NORECOVERY, &sdp->sd_flags) &&
1492 		test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
1493 
1494 	spin_lock(&qd_lock);
1495 	list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
1496 		spin_lock(&qd->qd_lockref.lock);
1497 		if (qd->qd_lockref.count != 0) {
1498 			spin_unlock(&qd->qd_lockref.lock);
1499 			continue;
1500 		}
1501 		lockref_mark_dead(&qd->qd_lockref);
1502 		spin_unlock(&qd->qd_lockref.lock);
1503 
1504 		list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
1505 		list_add(&qd->qd_lru, &dispose);
1506 	}
1507 	spin_unlock(&qd_lock);
1508 
1509 	gfs2_qd_list_dispose(&dispose);
1510 
1511 	wait_event_timeout(sdp->sd_kill_wait,
1512 		(count = atomic_read(&sdp->sd_quota_count)) == 0,
1513 		HZ * 60);
1514 
1515 	if (count != 0)
1516 		fs_err(sdp, "%d left-over quota data objects\n", count);
1517 
1518 	kvfree(sdp->sd_quota_bitmap);
1519 	sdp->sd_quota_bitmap = NULL;
1520 }
1521 
1522 static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1523 {
1524 	if (error == 0 || error == -EROFS)
1525 		return;
1526 	if (!gfs2_withdrawing_or_withdrawn(sdp)) {
1527 		if (!cmpxchg(&sdp->sd_log_error, 0, error))
1528 			fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1529 		wake_up(&sdp->sd_logd_waitq);
1530 	}
1531 }
1532 
1533 static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
1534 			       int (*fxn)(struct super_block *sb, int type),
1535 			       unsigned long t, unsigned long *timeo,
1536 			       unsigned int *new_timeo)
1537 {
1538 	if (t >= *timeo) {
1539 		int error = fxn(sdp->sd_vfs, 0);
1540 		quotad_error(sdp, msg, error);
1541 		*timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1542 	} else {
1543 		*timeo -= t;
1544 	}
1545 }
1546 
1547 void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1548 	if (!sdp->sd_statfs_force_sync) {
1549 		sdp->sd_statfs_force_sync = 1;
1550 		wake_up(&sdp->sd_quota_wait);
1551 	}
1552 }
1553 
1554 
1555 /**
1556  * gfs2_quotad - Write cached quota changes into the quota file
1557  * @data: Pointer to GFS2 superblock
1558  *
1559  */
1560 
1561 int gfs2_quotad(void *data)
1562 {
1563 	struct gfs2_sbd *sdp = data;
1564 	struct gfs2_tune *tune = &sdp->sd_tune;
1565 	unsigned long statfs_timeo = 0;
1566 	unsigned long quotad_timeo = 0;
1567 	unsigned long t = 0;
1568 
1569 	while (!kthread_should_stop()) {
1570 		if (gfs2_withdrawing_or_withdrawn(sdp))
1571 			break;
1572 
1573 		/* Update the master statfs file */
1574 		if (sdp->sd_statfs_force_sync) {
1575 			int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1576 			quotad_error(sdp, "statfs", error);
1577 			statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1578 		}
1579 		else
1580 			quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1581 				   	   &statfs_timeo,
1582 					   &tune->gt_statfs_quantum);
1583 
1584 		/* Update quota file */
1585 		quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1586 				   &quotad_timeo, &tune->gt_quota_quantum);
1587 
1588 		try_to_freeze();
1589 
1590 		t = min(quotad_timeo, statfs_timeo);
1591 
1592 		t = wait_event_interruptible_timeout(sdp->sd_quota_wait,
1593 				sdp->sd_statfs_force_sync ||
1594 				gfs2_withdrawing_or_withdrawn(sdp) ||
1595 				kthread_should_stop(),
1596 				t);
1597 
1598 		if (sdp->sd_statfs_force_sync)
1599 			t = 0;
1600 	}
1601 
1602 	return 0;
1603 }
1604 
1605 static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state)
1606 {
1607 	struct gfs2_sbd *sdp = sb->s_fs_info;
1608 
1609 	memset(state, 0, sizeof(*state));
1610 
1611 	switch (sdp->sd_args.ar_quota) {
1612 	case GFS2_QUOTA_QUIET:
1613 		fallthrough;
1614 	case GFS2_QUOTA_ON:
1615 		state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
1616 		state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
1617 		fallthrough;
1618 	case GFS2_QUOTA_ACCOUNT:
1619 		state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
1620 						  QCI_SYSFILE;
1621 		state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED |
1622 						  QCI_SYSFILE;
1623 		break;
1624 	case GFS2_QUOTA_OFF:
1625 		break;
1626 	}
1627 	if (sdp->sd_quota_inode) {
1628 		state->s_state[USRQUOTA].ino =
1629 					GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1630 		state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks;
1631 	}
1632 	state->s_state[USRQUOTA].nextents = 1;	/* unsupported */
1633 	state->s_state[GRPQUOTA] = state->s_state[USRQUOTA];
1634 	state->s_incoredqs = list_lru_count(&gfs2_qd_lru);
1635 	return 0;
1636 }
1637 
1638 static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
1639 			  struct qc_dqblk *fdq)
1640 {
1641 	struct gfs2_sbd *sdp = sb->s_fs_info;
1642 	struct gfs2_quota_lvb *qlvb;
1643 	struct gfs2_quota_data *qd;
1644 	struct gfs2_holder q_gh;
1645 	int error;
1646 
1647 	memset(fdq, 0, sizeof(*fdq));
1648 
1649 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1650 		return -ESRCH; /* Crazy XFS error code */
1651 
1652 	if ((qid.type != USRQUOTA) &&
1653 	    (qid.type != GRPQUOTA))
1654 		return -EINVAL;
1655 
1656 	error = qd_get(sdp, qid, &qd);
1657 	if (error)
1658 		return error;
1659 	error = do_glock(qd, FORCE, &q_gh);
1660 	if (error)
1661 		goto out;
1662 
1663 	qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1664 	fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift;
1665 	fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift;
1666 	fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift;
1667 
1668 	gfs2_glock_dq_uninit(&q_gh);
1669 out:
1670 	qd_put(qd);
1671 	return error;
1672 }
1673 
1674 /* GFS2 only supports a subset of the XFS fields */
1675 #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE)
1676 
1677 static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
1678 			  struct qc_dqblk *fdq)
1679 {
1680 	struct gfs2_sbd *sdp = sb->s_fs_info;
1681 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1682 	struct gfs2_quota_data *qd;
1683 	struct gfs2_holder q_gh, i_gh;
1684 	unsigned int data_blocks, ind_blocks;
1685 	unsigned int blocks = 0;
1686 	int alloc_required;
1687 	loff_t offset;
1688 	int error;
1689 
1690 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1691 		return -ESRCH; /* Crazy XFS error code */
1692 
1693 	if ((qid.type != USRQUOTA) &&
1694 	    (qid.type != GRPQUOTA))
1695 		return -EINVAL;
1696 
1697 	if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1698 		return -EINVAL;
1699 
1700 	error = qd_get(sdp, qid, &qd);
1701 	if (error)
1702 		return error;
1703 
1704 	error = gfs2_qa_get(ip);
1705 	if (error)
1706 		goto out_put;
1707 
1708 	inode_lock(&ip->i_inode);
1709 	error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1710 	if (error)
1711 		goto out_unlockput;
1712 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1713 	if (error)
1714 		goto out_q;
1715 
1716 	/* Check for existing entry, if none then alloc new blocks */
1717 	error = update_qd(sdp, qd);
1718 	if (error)
1719 		goto out_i;
1720 
1721 	/* If nothing has changed, this is a no-op */
1722 	if ((fdq->d_fieldmask & QC_SPC_SOFT) &&
1723 	    ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
1724 		fdq->d_fieldmask ^= QC_SPC_SOFT;
1725 
1726 	if ((fdq->d_fieldmask & QC_SPC_HARD) &&
1727 	    ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
1728 		fdq->d_fieldmask ^= QC_SPC_HARD;
1729 
1730 	if ((fdq->d_fieldmask & QC_SPACE) &&
1731 	    ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1732 		fdq->d_fieldmask ^= QC_SPACE;
1733 
1734 	if (fdq->d_fieldmask == 0)
1735 		goto out_i;
1736 
1737 	offset = qd2offset(qd);
1738 	alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
1739 	if (gfs2_is_stuffed(ip))
1740 		alloc_required = 1;
1741 	if (alloc_required) {
1742 		struct gfs2_alloc_parms ap = { .aflags = 0, };
1743 		gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1744 				       &data_blocks, &ind_blocks);
1745 		blocks = 1 + data_blocks + ind_blocks;
1746 		ap.target = blocks;
1747 		error = gfs2_inplace_reserve(ip, &ap);
1748 		if (error)
1749 			goto out_i;
1750 		blocks += gfs2_rg_blocks(ip, blocks);
1751 	}
1752 
1753 	/* Some quotas span block boundaries and can update two blocks,
1754 	   adding an extra block to the transaction to handle such quotas */
1755 	error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
1756 	if (error)
1757 		goto out_release;
1758 
1759 	/* Apply changes */
1760 	error = gfs2_adjust_quota(sdp, offset, 0, qd, fdq);
1761 	if (!error)
1762 		clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
1763 
1764 	gfs2_trans_end(sdp);
1765 out_release:
1766 	if (alloc_required)
1767 		gfs2_inplace_release(ip);
1768 out_i:
1769 	gfs2_glock_dq_uninit(&i_gh);
1770 out_q:
1771 	gfs2_glock_dq_uninit(&q_gh);
1772 out_unlockput:
1773 	gfs2_qa_put(ip);
1774 	inode_unlock(&ip->i_inode);
1775 out_put:
1776 	qd_put(qd);
1777 	return error;
1778 }
1779 
1780 const struct quotactl_ops gfs2_quotactl_ops = {
1781 	.quota_sync     = gfs2_quota_sync,
1782 	.get_state	= gfs2_quota_get_state,
1783 	.get_dqblk	= gfs2_get_dqblk,
1784 	.set_dqblk	= gfs2_set_dqblk,
1785 };
1786 
1787 void __init gfs2_quota_hash_init(void)
1788 {
1789 	unsigned i;
1790 
1791 	for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
1792 		INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
1793 }
1794