xref: /openbmc/linux/fs/gfs2/quota.c (revision 50e33567)
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 
gfs2_qd_hash(const struct gfs2_sbd * sdp,const struct kqid qid)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 
spin_lock_bucket(unsigned int hash)96 static inline void spin_lock_bucket(unsigned int hash)
97 {
98         hlist_bl_lock(&qd_hash_table[hash]);
99 }
100 
spin_unlock_bucket(unsigned int hash)101 static inline void spin_unlock_bucket(unsigned int hash)
102 {
103         hlist_bl_unlock(&qd_hash_table[hash]);
104 }
105 
gfs2_qd_dealloc(struct rcu_head * rcu)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 
gfs2_qd_dispose(struct gfs2_quota_data * qd)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 
gfs2_qd_list_dispose(struct list_head * list)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 
gfs2_qd_isolate(struct list_head * item,struct list_lru_one * lru,spinlock_t * lru_lock,void * arg)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 
gfs2_qd_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)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 
gfs2_qd_shrink_count(struct shrinker * shrink,struct shrink_control * sc)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 
qd2index(struct gfs2_quota_data * qd)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 
qd2offset(struct gfs2_quota_data * qd)211 static u64 qd2offset(struct gfs2_quota_data *qd)
212 {
213 	return qd2index(qd) * sizeof(struct gfs2_quota);
214 }
215 
qd_alloc(unsigned hash,struct gfs2_sbd * sdp,struct kqid qid)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 
gfs2_qd_search_bucket(unsigned int hash,const struct gfs2_sbd * sdp,struct kqid qid)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 
qd_get(struct gfs2_sbd * sdp,struct kqid qid,struct gfs2_quota_data ** qdp)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 
qd_hold(struct gfs2_quota_data * qd)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 
qd_put(struct gfs2_quota_data * qd)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 
slot_get(struct gfs2_quota_data * qd)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 
slot_hold(struct gfs2_quota_data * qd)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 
slot_put(struct gfs2_quota_data * qd)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 
bh_get(struct gfs2_quota_data * qd)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 
bh_put(struct gfs2_quota_data * qd)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 
qd_grab_sync(struct gfs2_sbd * sdp,struct gfs2_quota_data * qd,u64 sync_gen)449 static bool qd_grab_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 	    qd->qd_sync_gen >= sync_gen)
455 		return false;
456 
457 	if (!lockref_get_not_dead(&qd->qd_lockref))
458 		return false;
459 
460 	list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
461 	set_bit(QDF_LOCKED, &qd->qd_flags);
462 	qd->qd_change_sync = qd->qd_change;
463 	slot_hold(qd);
464 	return true;
465 }
466 
qd_ungrab_sync(struct gfs2_quota_data * qd)467 static void qd_ungrab_sync(struct gfs2_quota_data *qd)
468 {
469 	clear_bit(QDF_LOCKED, &qd->qd_flags);
470 	slot_put(qd);
471 	qd_put(qd);
472 }
473 
qd_fish(struct gfs2_sbd * sdp,struct gfs2_quota_data ** qdp)474 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
475 {
476 	struct gfs2_quota_data *qd = NULL, *iter;
477 	int error;
478 
479 	*qdp = NULL;
480 
481 	if (sb_rdonly(sdp->sd_vfs))
482 		return 0;
483 
484 	spin_lock(&qd_lock);
485 
486 	list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
487 		if (qd_grab_sync(sdp, iter, sdp->sd_quota_sync_gen)) {
488 			qd = iter;
489 			break;
490 		}
491 	}
492 
493 	spin_unlock(&qd_lock);
494 
495 	if (qd) {
496 		error = bh_get(qd);
497 		if (error) {
498 			qd_ungrab_sync(qd);
499 			return error;
500 		}
501 	}
502 
503 	*qdp = qd;
504 
505 	return 0;
506 }
507 
qdsb_put(struct gfs2_quota_data * qd)508 static void qdsb_put(struct gfs2_quota_data *qd)
509 {
510 	bh_put(qd);
511 	slot_put(qd);
512 	qd_put(qd);
513 }
514 
qd_unlock(struct gfs2_quota_data * qd)515 static void qd_unlock(struct gfs2_quota_data *qd)
516 {
517 	gfs2_assert_warn(qd->qd_sbd, test_bit(QDF_LOCKED, &qd->qd_flags));
518 	clear_bit(QDF_LOCKED, &qd->qd_flags);
519 	qdsb_put(qd);
520 }
521 
qdsb_get(struct gfs2_sbd * sdp,struct kqid qid,struct gfs2_quota_data ** qdp)522 static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
523 		    struct gfs2_quota_data **qdp)
524 {
525 	int error;
526 
527 	error = qd_get(sdp, qid, qdp);
528 	if (error)
529 		return error;
530 
531 	error = slot_get(*qdp);
532 	if (error)
533 		goto fail;
534 
535 	error = bh_get(*qdp);
536 	if (error)
537 		goto fail_slot;
538 
539 	return 0;
540 
541 fail_slot:
542 	slot_put(*qdp);
543 fail:
544 	qd_put(*qdp);
545 	return error;
546 }
547 
548 /**
549  * gfs2_qa_get - make sure we have a quota allocations data structure,
550  *               if necessary
551  * @ip: the inode for this reservation
552  */
gfs2_qa_get(struct gfs2_inode * ip)553 int gfs2_qa_get(struct gfs2_inode *ip)
554 {
555 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
556 	struct inode *inode = &ip->i_inode;
557 
558 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
559 		return 0;
560 
561 	spin_lock(&inode->i_lock);
562 	if (ip->i_qadata == NULL) {
563 		struct gfs2_qadata *tmp;
564 
565 		spin_unlock(&inode->i_lock);
566 		tmp = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
567 		if (!tmp)
568 			return -ENOMEM;
569 
570 		spin_lock(&inode->i_lock);
571 		if (ip->i_qadata == NULL)
572 			ip->i_qadata = tmp;
573 		else
574 			kmem_cache_free(gfs2_qadata_cachep, tmp);
575 	}
576 	ip->i_qadata->qa_ref++;
577 	spin_unlock(&inode->i_lock);
578 	return 0;
579 }
580 
gfs2_qa_put(struct gfs2_inode * ip)581 void gfs2_qa_put(struct gfs2_inode *ip)
582 {
583 	struct inode *inode = &ip->i_inode;
584 
585 	spin_lock(&inode->i_lock);
586 	if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) {
587 		kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
588 		ip->i_qadata = NULL;
589 	}
590 	spin_unlock(&inode->i_lock);
591 }
592 
gfs2_quota_hold(struct gfs2_inode * ip,kuid_t uid,kgid_t gid)593 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
594 {
595 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
596 	struct gfs2_quota_data **qd;
597 	int error;
598 
599 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
600 		return 0;
601 
602 	error = gfs2_qa_get(ip);
603 	if (error)
604 		return error;
605 
606 	qd = ip->i_qadata->qa_qd;
607 
608 	if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) ||
609 	    gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) {
610 		error = -EIO;
611 		gfs2_qa_put(ip);
612 		goto out;
613 	}
614 
615 	error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
616 	if (error)
617 		goto out_unhold;
618 	ip->i_qadata->qa_qd_num++;
619 	qd++;
620 
621 	error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
622 	if (error)
623 		goto out_unhold;
624 	ip->i_qadata->qa_qd_num++;
625 	qd++;
626 
627 	if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
628 	    !uid_eq(uid, ip->i_inode.i_uid)) {
629 		error = qdsb_get(sdp, make_kqid_uid(uid), qd);
630 		if (error)
631 			goto out_unhold;
632 		ip->i_qadata->qa_qd_num++;
633 		qd++;
634 	}
635 
636 	if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
637 	    !gid_eq(gid, ip->i_inode.i_gid)) {
638 		error = qdsb_get(sdp, make_kqid_gid(gid), qd);
639 		if (error)
640 			goto out_unhold;
641 		ip->i_qadata->qa_qd_num++;
642 		qd++;
643 	}
644 
645 out_unhold:
646 	if (error)
647 		gfs2_quota_unhold(ip);
648 out:
649 	return error;
650 }
651 
gfs2_quota_unhold(struct gfs2_inode * ip)652 void gfs2_quota_unhold(struct gfs2_inode *ip)
653 {
654 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
655 	u32 x;
656 
657 	if (ip->i_qadata == NULL)
658 		return;
659 
660 	gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
661 
662 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
663 		qdsb_put(ip->i_qadata->qa_qd[x]);
664 		ip->i_qadata->qa_qd[x] = NULL;
665 	}
666 	ip->i_qadata->qa_qd_num = 0;
667 	gfs2_qa_put(ip);
668 }
669 
sort_qd(const void * a,const void * b)670 static int sort_qd(const void *a, const void *b)
671 {
672 	const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
673 	const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
674 
675 	if (qid_lt(qd_a->qd_id, qd_b->qd_id))
676 		return -1;
677 	if (qid_lt(qd_b->qd_id, qd_a->qd_id))
678 		return 1;
679 	return 0;
680 }
681 
do_qc(struct gfs2_quota_data * qd,s64 change)682 static void do_qc(struct gfs2_quota_data *qd, s64 change)
683 {
684 	struct gfs2_sbd *sdp = qd->qd_sbd;
685 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
686 	struct gfs2_quota_change *qc = qd->qd_bh_qc;
687 	s64 x;
688 
689 	mutex_lock(&sdp->sd_quota_mutex);
690 	gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
691 
692 	if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
693 		qc->qc_change = 0;
694 		qc->qc_flags = 0;
695 		if (qd->qd_id.type == USRQUOTA)
696 			qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
697 		qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
698 	}
699 
700 	x = be64_to_cpu(qc->qc_change) + change;
701 	qc->qc_change = cpu_to_be64(x);
702 
703 	spin_lock(&qd_lock);
704 	qd->qd_change = x;
705 	spin_unlock(&qd_lock);
706 
707 	if (!x) {
708 		gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
709 		clear_bit(QDF_CHANGE, &qd->qd_flags);
710 		qc->qc_flags = 0;
711 		qc->qc_id = 0;
712 		slot_put(qd);
713 		qd_put(qd);
714 	} else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
715 		qd_hold(qd);
716 		slot_hold(qd);
717 	}
718 
719 	if (change < 0) /* Reset quiet flag if we freed some blocks */
720 		clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
721 	mutex_unlock(&sdp->sd_quota_mutex);
722 }
723 
gfs2_write_buf_to_page(struct gfs2_sbd * sdp,unsigned long index,unsigned off,void * buf,unsigned bytes)724 static int gfs2_write_buf_to_page(struct gfs2_sbd *sdp, unsigned long index,
725 				  unsigned off, void *buf, unsigned bytes)
726 {
727 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
728 	struct inode *inode = &ip->i_inode;
729 	struct address_space *mapping = inode->i_mapping;
730 	struct page *page;
731 	struct buffer_head *bh;
732 	u64 blk;
733 	unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0;
734 	unsigned to_write = bytes, pg_off = off;
735 
736 	blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift);
737 	boff = off % bsize;
738 
739 	page = grab_cache_page(mapping, index);
740 	if (!page)
741 		return -ENOMEM;
742 	if (!page_has_buffers(page))
743 		create_empty_buffers(page, bsize, 0);
744 
745 	bh = page_buffers(page);
746 	for(;;) {
747 		/* Find the beginning block within the page */
748 		if (pg_off >= ((bnum * bsize) + bsize)) {
749 			bh = bh->b_this_page;
750 			bnum++;
751 			blk++;
752 			continue;
753 		}
754 		if (!buffer_mapped(bh)) {
755 			gfs2_block_map(inode, blk, bh, 1);
756 			if (!buffer_mapped(bh))
757 				goto unlock_out;
758 			/* If it's a newly allocated disk block, zero it */
759 			if (buffer_new(bh))
760 				zero_user(page, bnum * bsize, bh->b_size);
761 		}
762 		if (PageUptodate(page))
763 			set_buffer_uptodate(bh);
764 		if (bh_read(bh, REQ_META | REQ_PRIO) < 0)
765 			goto unlock_out;
766 		gfs2_trans_add_data(ip->i_gl, bh);
767 
768 		/* If we need to write to the next block as well */
769 		if (to_write > (bsize - boff)) {
770 			pg_off += (bsize - boff);
771 			to_write -= (bsize - boff);
772 			boff = pg_off % bsize;
773 			continue;
774 		}
775 		break;
776 	}
777 
778 	/* Write to the page, now that we have setup the buffer(s) */
779 	memcpy_to_page(page, off, buf, bytes);
780 	flush_dcache_page(page);
781 	unlock_page(page);
782 	put_page(page);
783 
784 	return 0;
785 
786 unlock_out:
787 	unlock_page(page);
788 	put_page(page);
789 	return -EIO;
790 }
791 
gfs2_write_disk_quota(struct gfs2_sbd * sdp,struct gfs2_quota * qp,loff_t loc)792 static int gfs2_write_disk_quota(struct gfs2_sbd *sdp, struct gfs2_quota *qp,
793 				 loff_t loc)
794 {
795 	unsigned long pg_beg;
796 	unsigned pg_off, nbytes, overflow = 0;
797 	int error;
798 	void *ptr;
799 
800 	nbytes = sizeof(struct gfs2_quota);
801 
802 	pg_beg = loc >> PAGE_SHIFT;
803 	pg_off = offset_in_page(loc);
804 
805 	/* If the quota straddles a page boundary, split the write in two */
806 	if ((pg_off + nbytes) > PAGE_SIZE)
807 		overflow = (pg_off + nbytes) - PAGE_SIZE;
808 
809 	ptr = qp;
810 	error = gfs2_write_buf_to_page(sdp, pg_beg, pg_off, ptr,
811 				       nbytes - overflow);
812 	/* If there's an overflow, write the remaining bytes to the next page */
813 	if (!error && overflow)
814 		error = gfs2_write_buf_to_page(sdp, pg_beg + 1, 0,
815 					       ptr + nbytes - overflow,
816 					       overflow);
817 	return error;
818 }
819 
820 /**
821  * gfs2_adjust_quota - adjust record of current block usage
822  * @sdp: The superblock
823  * @loc: Offset of the entry in the quota file
824  * @change: The amount of usage change to record
825  * @qd: The quota data
826  * @fdq: The updated limits to record
827  *
828  * This function was mostly borrowed from gfs2_block_truncate_page which was
829  * in turn mostly borrowed from ext3
830  *
831  * Returns: 0 or -ve on error
832  */
833 
gfs2_adjust_quota(struct gfs2_sbd * sdp,loff_t loc,s64 change,struct gfs2_quota_data * qd,struct qc_dqblk * fdq)834 static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc,
835 			     s64 change, struct gfs2_quota_data *qd,
836 			     struct qc_dqblk *fdq)
837 {
838 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
839 	struct inode *inode = &ip->i_inode;
840 	struct gfs2_quota q;
841 	int err;
842 	u64 size;
843 
844 	if (gfs2_is_stuffed(ip)) {
845 		err = gfs2_unstuff_dinode(ip);
846 		if (err)
847 			return err;
848 	}
849 
850 	memset(&q, 0, sizeof(struct gfs2_quota));
851 	err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
852 	if (err < 0)
853 		return err;
854 
855 	loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */
856 	be64_add_cpu(&q.qu_value, change);
857 	if (((s64)be64_to_cpu(q.qu_value)) < 0)
858 		q.qu_value = 0; /* Never go negative on quota usage */
859 	qd->qd_qb.qb_value = q.qu_value;
860 	if (fdq) {
861 		if (fdq->d_fieldmask & QC_SPC_SOFT) {
862 			q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift);
863 			qd->qd_qb.qb_warn = q.qu_warn;
864 		}
865 		if (fdq->d_fieldmask & QC_SPC_HARD) {
866 			q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift);
867 			qd->qd_qb.qb_limit = q.qu_limit;
868 		}
869 		if (fdq->d_fieldmask & QC_SPACE) {
870 			q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift);
871 			qd->qd_qb.qb_value = q.qu_value;
872 		}
873 	}
874 
875 	err = gfs2_write_disk_quota(sdp, &q, loc);
876 	if (!err) {
877 		size = loc + sizeof(struct gfs2_quota);
878 		if (size > inode->i_size)
879 			i_size_write(inode, size);
880 		inode->i_mtime = inode_set_ctime_current(inode);
881 		mark_inode_dirty(inode);
882 		set_bit(QDF_REFRESH, &qd->qd_flags);
883 	}
884 
885 	return err;
886 }
887 
do_sync(unsigned int num_qd,struct gfs2_quota_data ** qda)888 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
889 {
890 	struct gfs2_sbd *sdp = (*qda)->qd_sbd;
891 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
892 	struct gfs2_alloc_parms ap = { .aflags = 0, };
893 	unsigned int data_blocks, ind_blocks;
894 	struct gfs2_holder *ghs, i_gh;
895 	unsigned int qx, x;
896 	struct gfs2_quota_data *qd;
897 	unsigned reserved;
898 	loff_t offset;
899 	unsigned int nalloc = 0, blocks;
900 	int error;
901 
902 	gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
903 			      &data_blocks, &ind_blocks);
904 
905 	ghs = kmalloc_array(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
906 	if (!ghs)
907 		return -ENOMEM;
908 
909 	sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
910 	inode_lock(&ip->i_inode);
911 	for (qx = 0; qx < num_qd; qx++) {
912 		error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
913 					   GL_NOCACHE, &ghs[qx]);
914 		if (error)
915 			goto out_dq;
916 	}
917 
918 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
919 	if (error)
920 		goto out_dq;
921 
922 	for (x = 0; x < num_qd; x++) {
923 		offset = qd2offset(qda[x]);
924 		if (gfs2_write_alloc_required(ip, offset,
925 					      sizeof(struct gfs2_quota)))
926 			nalloc++;
927 	}
928 
929 	/*
930 	 * 1 blk for unstuffing inode if stuffed. We add this extra
931 	 * block to the reservation unconditionally. If the inode
932 	 * doesn't need unstuffing, the block will be released to the
933 	 * rgrp since it won't be allocated during the transaction
934 	 */
935 	/* +3 in the end for unstuffing block, inode size update block
936 	 * and another block in case quota straddles page boundary and
937 	 * two blocks need to be updated instead of 1 */
938 	blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
939 
940 	reserved = 1 + (nalloc * (data_blocks + ind_blocks));
941 	ap.target = reserved;
942 	error = gfs2_inplace_reserve(ip, &ap);
943 	if (error)
944 		goto out_alloc;
945 
946 	if (nalloc)
947 		blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
948 
949 	error = gfs2_trans_begin(sdp, blocks, 0);
950 	if (error)
951 		goto out_ipres;
952 
953 	for (x = 0; x < num_qd; x++) {
954 		qd = qda[x];
955 		offset = qd2offset(qd);
956 		error = gfs2_adjust_quota(sdp, offset, qd->qd_change_sync, qd,
957 							NULL);
958 		if (error)
959 			goto out_end_trans;
960 
961 		do_qc(qd, -qd->qd_change_sync);
962 		set_bit(QDF_REFRESH, &qd->qd_flags);
963 	}
964 
965 out_end_trans:
966 	gfs2_trans_end(sdp);
967 out_ipres:
968 	gfs2_inplace_release(ip);
969 out_alloc:
970 	gfs2_glock_dq_uninit(&i_gh);
971 out_dq:
972 	while (qx--)
973 		gfs2_glock_dq_uninit(&ghs[qx]);
974 	inode_unlock(&ip->i_inode);
975 	kfree(ghs);
976 	gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl,
977 		       GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
978 	if (!error) {
979 		for (x = 0; x < num_qd; x++)
980 			qda[x]->qd_sync_gen = sdp->sd_quota_sync_gen;
981 	}
982 	return error;
983 }
984 
update_qd(struct gfs2_sbd * sdp,struct gfs2_quota_data * qd)985 static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
986 {
987 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
988 	struct gfs2_quota q;
989 	struct gfs2_quota_lvb *qlvb;
990 	loff_t pos;
991 	int error;
992 
993 	memset(&q, 0, sizeof(struct gfs2_quota));
994 	pos = qd2offset(qd);
995 	error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
996 	if (error < 0)
997 		return error;
998 
999 	qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1000 	qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
1001 	qlvb->__pad = 0;
1002 	qlvb->qb_limit = q.qu_limit;
1003 	qlvb->qb_warn = q.qu_warn;
1004 	qlvb->qb_value = q.qu_value;
1005 	qd->qd_qb = *qlvb;
1006 
1007 	return 0;
1008 }
1009 
do_glock(struct gfs2_quota_data * qd,int force_refresh,struct gfs2_holder * q_gh)1010 static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
1011 		    struct gfs2_holder *q_gh)
1012 {
1013 	struct gfs2_sbd *sdp = qd->qd_sbd;
1014 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1015 	struct gfs2_holder i_gh;
1016 	int error;
1017 
1018 	gfs2_assert_warn(sdp, sdp == qd->qd_gl->gl_name.ln_sbd);
1019 restart:
1020 	error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
1021 	if (error)
1022 		return error;
1023 
1024 	if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
1025 		force_refresh = FORCE;
1026 
1027 	qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1028 
1029 	if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
1030 		gfs2_glock_dq_uninit(q_gh);
1031 		error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
1032 					   GL_NOCACHE, q_gh);
1033 		if (error)
1034 			return error;
1035 
1036 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1037 		if (error)
1038 			goto fail;
1039 
1040 		error = update_qd(sdp, qd);
1041 		if (error)
1042 			goto fail_gunlock;
1043 
1044 		gfs2_glock_dq_uninit(&i_gh);
1045 		gfs2_glock_dq_uninit(q_gh);
1046 		force_refresh = 0;
1047 		goto restart;
1048 	}
1049 
1050 	return 0;
1051 
1052 fail_gunlock:
1053 	gfs2_glock_dq_uninit(&i_gh);
1054 fail:
1055 	gfs2_glock_dq_uninit(q_gh);
1056 	return error;
1057 }
1058 
gfs2_quota_lock(struct gfs2_inode * ip,kuid_t uid,kgid_t gid)1059 int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
1060 {
1061 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1062 	struct gfs2_quota_data *qd;
1063 	u32 x;
1064 	int error;
1065 
1066 	if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON &&
1067 	    sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET)
1068 		return 0;
1069 
1070 	error = gfs2_quota_hold(ip, uid, gid);
1071 	if (error)
1072 		return error;
1073 
1074 	sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num,
1075 	     sizeof(struct gfs2_quota_data *), sort_qd, NULL);
1076 
1077 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1078 		qd = ip->i_qadata->qa_qd[x];
1079 		error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]);
1080 		if (error)
1081 			break;
1082 	}
1083 
1084 	if (!error)
1085 		set_bit(GIF_QD_LOCKED, &ip->i_flags);
1086 	else {
1087 		while (x--)
1088 			gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
1089 		gfs2_quota_unhold(ip);
1090 	}
1091 
1092 	return error;
1093 }
1094 
need_sync(struct gfs2_quota_data * qd)1095 static bool need_sync(struct gfs2_quota_data *qd)
1096 {
1097 	struct gfs2_sbd *sdp = qd->qd_sbd;
1098 	struct gfs2_tune *gt = &sdp->sd_tune;
1099 	s64 value;
1100 	unsigned int num, den;
1101 
1102 	if (!qd->qd_qb.qb_limit)
1103 		return false;
1104 
1105 	spin_lock(&qd_lock);
1106 	value = qd->qd_change;
1107 	spin_unlock(&qd_lock);
1108 
1109 	spin_lock(&gt->gt_spin);
1110 	num = gt->gt_quota_scale_num;
1111 	den = gt->gt_quota_scale_den;
1112 	spin_unlock(&gt->gt_spin);
1113 
1114 	if (value <= 0)
1115 		return false;
1116 	else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1117 		 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1118 		return false;
1119 	else {
1120 		value *= gfs2_jindex_size(sdp) * num;
1121 		value = div_s64(value, den);
1122 		value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
1123 		if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
1124 			return false;
1125 	}
1126 
1127 	return true;
1128 }
1129 
gfs2_quota_unlock(struct gfs2_inode * ip)1130 void gfs2_quota_unlock(struct gfs2_inode *ip)
1131 {
1132 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1133 	struct gfs2_quota_data *qda[2 * GFS2_MAXQUOTAS];
1134 	unsigned int count = 0;
1135 	u32 x;
1136 
1137 	if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1138 		return;
1139 
1140 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1141 		struct gfs2_quota_data *qd;
1142 		bool sync;
1143 		int error;
1144 
1145 		qd = ip->i_qadata->qa_qd[x];
1146 		sync = need_sync(qd);
1147 
1148 		gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
1149 		if (!sync)
1150 			continue;
1151 
1152 		spin_lock(&qd_lock);
1153 		sync = qd_grab_sync(sdp, qd, U64_MAX);
1154 		spin_unlock(&qd_lock);
1155 
1156 		if (!sync)
1157 			continue;
1158 
1159 		gfs2_assert_warn(sdp, qd->qd_change_sync);
1160 		error = bh_get(qd);
1161 		if (error) {
1162 			qd_ungrab_sync(qd);
1163 			continue;
1164 		}
1165 
1166 		qda[count++] = qd;
1167 	}
1168 
1169 	if (count) {
1170 		do_sync(count, qda);
1171 		for (x = 0; x < count; x++)
1172 			qd_unlock(qda[x]);
1173 	}
1174 
1175 	gfs2_quota_unhold(ip);
1176 }
1177 
1178 #define MAX_LINE 256
1179 
print_message(struct gfs2_quota_data * qd,char * type)1180 static int print_message(struct gfs2_quota_data *qd, char *type)
1181 {
1182 	struct gfs2_sbd *sdp = qd->qd_sbd;
1183 
1184 	if (sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET)
1185 		fs_info(sdp, "quota %s for %s %u\n",
1186 			type,
1187 			(qd->qd_id.type == USRQUOTA) ? "user" : "group",
1188 			from_kqid(&init_user_ns, qd->qd_id));
1189 
1190 	return 0;
1191 }
1192 
1193 /**
1194  * gfs2_quota_check - check if allocating new blocks will exceed quota
1195  * @ip:  The inode for which this check is being performed
1196  * @uid: The uid to check against
1197  * @gid: The gid to check against
1198  * @ap:  The allocation parameters. ap->target contains the requested
1199  *       blocks. ap->min_target, if set, contains the minimum blks
1200  *       requested.
1201  *
1202  * Returns: 0 on success.
1203  *                  min_req = ap->min_target ? ap->min_target : ap->target;
1204  *                  quota must allow at least min_req blks for success and
1205  *                  ap->allowed is set to the number of blocks allowed
1206  *
1207  *          -EDQUOT otherwise, quota violation. ap->allowed is set to number
1208  *                  of blocks available.
1209  */
gfs2_quota_check(struct gfs2_inode * ip,kuid_t uid,kgid_t gid,struct gfs2_alloc_parms * ap)1210 int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
1211 		     struct gfs2_alloc_parms *ap)
1212 {
1213 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1214 	struct gfs2_quota_data *qd;
1215 	s64 value, warn, limit;
1216 	u32 x;
1217 	int error = 0;
1218 
1219 	ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */
1220 	if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1221 		return 0;
1222 
1223 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1224 		qd = ip->i_qadata->qa_qd[x];
1225 
1226 		if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1227 		      qid_eq(qd->qd_id, make_kqid_gid(gid))))
1228 			continue;
1229 
1230 		warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
1231 		limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
1232 		value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
1233 		spin_lock(&qd_lock);
1234 		value += qd->qd_change;
1235 		spin_unlock(&qd_lock);
1236 
1237 		if (limit > 0 && (limit - value) < ap->allowed)
1238 			ap->allowed = limit - value;
1239 		/* If we can't meet the target */
1240 		if (limit && limit < (value + (s64)ap->target)) {
1241 			/* If no min_target specified or we don't meet
1242 			 * min_target, return -EDQUOT */
1243 			if (!ap->min_target || ap->min_target > ap->allowed) {
1244 				if (!test_and_set_bit(QDF_QMSG_QUIET,
1245 						      &qd->qd_flags)) {
1246 					print_message(qd, "exceeded");
1247 					quota_send_warning(qd->qd_id,
1248 							   sdp->sd_vfs->s_dev,
1249 							   QUOTA_NL_BHARDWARN);
1250 				}
1251 				error = -EDQUOT;
1252 				break;
1253 			}
1254 		} else if (warn && warn < value &&
1255 			   time_after_eq(jiffies, qd->qd_last_warn +
1256 					 gfs2_tune_get(sdp, gt_quota_warn_period)
1257 					 * HZ)) {
1258 			quota_send_warning(qd->qd_id,
1259 					   sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
1260 			error = print_message(qd, "warning");
1261 			qd->qd_last_warn = jiffies;
1262 		}
1263 	}
1264 	return error;
1265 }
1266 
gfs2_quota_change(struct gfs2_inode * ip,s64 change,kuid_t uid,kgid_t gid)1267 void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1268 		       kuid_t uid, kgid_t gid)
1269 {
1270 	struct gfs2_quota_data *qd;
1271 	u32 x;
1272 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1273 
1274 	if ((sdp->sd_args.ar_quota != GFS2_QUOTA_ON &&
1275 	    sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET) ||
1276 	    gfs2_assert_warn(sdp, change))
1277 		return;
1278 	if (ip->i_diskflags & GFS2_DIF_SYSTEM)
1279 		return;
1280 
1281 	if (gfs2_assert_withdraw(sdp, ip->i_qadata &&
1282 				 ip->i_qadata->qa_ref > 0))
1283 		return;
1284 	for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
1285 		qd = ip->i_qadata->qa_qd[x];
1286 
1287 		if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1288 		    qid_eq(qd->qd_id, make_kqid_gid(gid))) {
1289 			do_qc(qd, change);
1290 		}
1291 	}
1292 }
1293 
qd_changed(struct gfs2_sbd * sdp)1294 static bool qd_changed(struct gfs2_sbd *sdp)
1295 {
1296 	struct gfs2_quota_data *qd;
1297 	bool changed = false;
1298 
1299 	spin_lock(&qd_lock);
1300 	list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
1301 		if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
1302 		    !test_bit(QDF_CHANGE, &qd->qd_flags))
1303 			continue;
1304 
1305 		changed = true;
1306 		break;
1307 	}
1308 	spin_unlock(&qd_lock);
1309 	return changed;
1310 }
1311 
gfs2_quota_sync(struct super_block * sb,int type)1312 int gfs2_quota_sync(struct super_block *sb, int type)
1313 {
1314 	struct gfs2_sbd *sdp = sb->s_fs_info;
1315 	struct gfs2_quota_data **qda;
1316 	unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder);
1317 	unsigned int num_qd;
1318 	unsigned int x;
1319 	int error = 0;
1320 
1321 	if (!qd_changed(sdp))
1322 		return 0;
1323 
1324 	qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1325 	if (!qda)
1326 		return -ENOMEM;
1327 
1328 	mutex_lock(&sdp->sd_quota_sync_mutex);
1329 	sdp->sd_quota_sync_gen++;
1330 
1331 	do {
1332 		num_qd = 0;
1333 
1334 		for (;;) {
1335 			error = qd_fish(sdp, qda + num_qd);
1336 			if (error || !qda[num_qd])
1337 				break;
1338 			if (++num_qd == max_qd)
1339 				break;
1340 		}
1341 
1342 		if (num_qd) {
1343 			if (!error)
1344 				error = do_sync(num_qd, qda);
1345 
1346 			for (x = 0; x < num_qd; x++)
1347 				qd_unlock(qda[x]);
1348 		}
1349 	} while (!error && num_qd == max_qd);
1350 
1351 	mutex_unlock(&sdp->sd_quota_sync_mutex);
1352 	kfree(qda);
1353 
1354 	return error;
1355 }
1356 
gfs2_quota_refresh(struct gfs2_sbd * sdp,struct kqid qid)1357 int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
1358 {
1359 	struct gfs2_quota_data *qd;
1360 	struct gfs2_holder q_gh;
1361 	int error;
1362 
1363 	error = qd_get(sdp, qid, &qd);
1364 	if (error)
1365 		return error;
1366 
1367 	error = do_glock(qd, FORCE, &q_gh);
1368 	if (!error)
1369 		gfs2_glock_dq_uninit(&q_gh);
1370 
1371 	qd_put(qd);
1372 	return error;
1373 }
1374 
gfs2_quota_init(struct gfs2_sbd * sdp)1375 int gfs2_quota_init(struct gfs2_sbd *sdp)
1376 {
1377 	struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
1378 	u64 size = i_size_read(sdp->sd_qc_inode);
1379 	unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
1380 	unsigned int x, slot = 0;
1381 	unsigned int found = 0;
1382 	unsigned int hash;
1383 	unsigned int bm_size;
1384 	u64 dblock;
1385 	u32 extlen = 0;
1386 	int error;
1387 
1388 	if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
1389 		return -EIO;
1390 
1391 	sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
1392 	bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
1393 	bm_size *= sizeof(unsigned long);
1394 	error = -ENOMEM;
1395 	sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
1396 	if (sdp->sd_quota_bitmap == NULL)
1397 		sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
1398 						 __GFP_ZERO);
1399 	if (!sdp->sd_quota_bitmap)
1400 		return error;
1401 
1402 	for (x = 0; x < blocks; x++) {
1403 		struct buffer_head *bh;
1404 		const struct gfs2_quota_change *qc;
1405 		unsigned int y;
1406 
1407 		if (!extlen) {
1408 			extlen = 32;
1409 			error = gfs2_get_extent(&ip->i_inode, x, &dblock, &extlen);
1410 			if (error)
1411 				goto fail;
1412 		}
1413 		error = -EIO;
1414 		bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1415 		if (!bh)
1416 			goto fail;
1417 		if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1418 			brelse(bh);
1419 			goto fail;
1420 		}
1421 
1422 		qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
1423 		for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1424 		     y++, slot++) {
1425 			struct gfs2_quota_data *qd;
1426 			s64 qc_change = be64_to_cpu(qc->qc_change);
1427 			u32 qc_flags = be32_to_cpu(qc->qc_flags);
1428 			enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1429 						USRQUOTA : GRPQUOTA;
1430 			struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1431 						      be32_to_cpu(qc->qc_id));
1432 			qc++;
1433 			if (!qc_change)
1434 				continue;
1435 
1436 			hash = gfs2_qd_hash(sdp, qc_id);
1437 			qd = qd_alloc(hash, sdp, qc_id);
1438 			if (qd == NULL) {
1439 				brelse(bh);
1440 				goto fail;
1441 			}
1442 
1443 			set_bit(QDF_CHANGE, &qd->qd_flags);
1444 			qd->qd_change = qc_change;
1445 			qd->qd_slot = slot;
1446 			qd->qd_slot_ref = 1;
1447 
1448 			spin_lock(&qd_lock);
1449 			BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
1450 			list_add(&qd->qd_list, &sdp->sd_quota_list);
1451 			atomic_inc(&sdp->sd_quota_count);
1452 			spin_unlock(&qd_lock);
1453 
1454 			spin_lock_bucket(hash);
1455 			hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
1456 			spin_unlock_bucket(hash);
1457 
1458 			found++;
1459 		}
1460 
1461 		brelse(bh);
1462 		dblock++;
1463 		extlen--;
1464 	}
1465 
1466 	if (found)
1467 		fs_info(sdp, "found %u quota changes\n", found);
1468 
1469 	return 0;
1470 
1471 fail:
1472 	gfs2_quota_cleanup(sdp);
1473 	return error;
1474 }
1475 
gfs2_quota_cleanup(struct gfs2_sbd * sdp)1476 void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1477 {
1478 	struct gfs2_quota_data *qd;
1479 	LIST_HEAD(dispose);
1480 	int count;
1481 
1482 	BUG_ON(!test_bit(SDF_NORECOVERY, &sdp->sd_flags) &&
1483 		test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
1484 
1485 	spin_lock(&qd_lock);
1486 	list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
1487 		spin_lock(&qd->qd_lockref.lock);
1488 		if (qd->qd_lockref.count != 0) {
1489 			spin_unlock(&qd->qd_lockref.lock);
1490 			continue;
1491 		}
1492 		lockref_mark_dead(&qd->qd_lockref);
1493 		spin_unlock(&qd->qd_lockref.lock);
1494 
1495 		list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
1496 		list_add(&qd->qd_lru, &dispose);
1497 	}
1498 	spin_unlock(&qd_lock);
1499 
1500 	gfs2_qd_list_dispose(&dispose);
1501 
1502 	wait_event_timeout(sdp->sd_kill_wait,
1503 		(count = atomic_read(&sdp->sd_quota_count)) == 0,
1504 		HZ * 60);
1505 
1506 	if (count != 0)
1507 		fs_err(sdp, "%d left-over quota data objects\n", count);
1508 
1509 	kvfree(sdp->sd_quota_bitmap);
1510 	sdp->sd_quota_bitmap = NULL;
1511 }
1512 
quotad_error(struct gfs2_sbd * sdp,const char * msg,int error)1513 static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1514 {
1515 	if (error == 0 || error == -EROFS)
1516 		return;
1517 	if (!gfs2_withdrawing_or_withdrawn(sdp)) {
1518 		if (!cmpxchg(&sdp->sd_log_error, 0, error))
1519 			fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1520 		wake_up(&sdp->sd_logd_waitq);
1521 	}
1522 }
1523 
quotad_check_timeo(struct gfs2_sbd * sdp,const char * msg,int (* fxn)(struct super_block * sb,int type),unsigned long t,unsigned long * timeo,unsigned int * new_timeo)1524 static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
1525 			       int (*fxn)(struct super_block *sb, int type),
1526 			       unsigned long t, unsigned long *timeo,
1527 			       unsigned int *new_timeo)
1528 {
1529 	if (t >= *timeo) {
1530 		int error = fxn(sdp->sd_vfs, 0);
1531 		quotad_error(sdp, msg, error);
1532 		*timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1533 	} else {
1534 		*timeo -= t;
1535 	}
1536 }
1537 
gfs2_wake_up_statfs(struct gfs2_sbd * sdp)1538 void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1539 	if (!sdp->sd_statfs_force_sync) {
1540 		sdp->sd_statfs_force_sync = 1;
1541 		wake_up(&sdp->sd_quota_wait);
1542 	}
1543 }
1544 
1545 
1546 /**
1547  * gfs2_quotad - Write cached quota changes into the quota file
1548  * @data: Pointer to GFS2 superblock
1549  *
1550  */
1551 
gfs2_quotad(void * data)1552 int gfs2_quotad(void *data)
1553 {
1554 	struct gfs2_sbd *sdp = data;
1555 	struct gfs2_tune *tune = &sdp->sd_tune;
1556 	unsigned long statfs_timeo = 0;
1557 	unsigned long quotad_timeo = 0;
1558 	unsigned long t = 0;
1559 
1560 	while (!kthread_should_stop()) {
1561 		if (gfs2_withdrawing_or_withdrawn(sdp))
1562 			break;
1563 
1564 		/* Update the master statfs file */
1565 		if (sdp->sd_statfs_force_sync) {
1566 			int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1567 			quotad_error(sdp, "statfs", error);
1568 			statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1569 		}
1570 		else
1571 			quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1572 				   	   &statfs_timeo,
1573 					   &tune->gt_statfs_quantum);
1574 
1575 		/* Update quota file */
1576 		quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1577 				   &quotad_timeo, &tune->gt_quota_quantum);
1578 
1579 		try_to_freeze();
1580 
1581 		t = min(quotad_timeo, statfs_timeo);
1582 
1583 		t = wait_event_interruptible_timeout(sdp->sd_quota_wait,
1584 				sdp->sd_statfs_force_sync ||
1585 				gfs2_withdrawing_or_withdrawn(sdp) ||
1586 				kthread_should_stop(),
1587 				t);
1588 
1589 		if (sdp->sd_statfs_force_sync)
1590 			t = 0;
1591 	}
1592 
1593 	return 0;
1594 }
1595 
gfs2_quota_get_state(struct super_block * sb,struct qc_state * state)1596 static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state)
1597 {
1598 	struct gfs2_sbd *sdp = sb->s_fs_info;
1599 
1600 	memset(state, 0, sizeof(*state));
1601 
1602 	switch (sdp->sd_args.ar_quota) {
1603 	case GFS2_QUOTA_QUIET:
1604 		fallthrough;
1605 	case GFS2_QUOTA_ON:
1606 		state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
1607 		state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
1608 		fallthrough;
1609 	case GFS2_QUOTA_ACCOUNT:
1610 		state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
1611 						  QCI_SYSFILE;
1612 		state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED |
1613 						  QCI_SYSFILE;
1614 		break;
1615 	case GFS2_QUOTA_OFF:
1616 		break;
1617 	}
1618 	if (sdp->sd_quota_inode) {
1619 		state->s_state[USRQUOTA].ino =
1620 					GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1621 		state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks;
1622 	}
1623 	state->s_state[USRQUOTA].nextents = 1;	/* unsupported */
1624 	state->s_state[GRPQUOTA] = state->s_state[USRQUOTA];
1625 	state->s_incoredqs = list_lru_count(&gfs2_qd_lru);
1626 	return 0;
1627 }
1628 
gfs2_get_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * fdq)1629 static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
1630 			  struct qc_dqblk *fdq)
1631 {
1632 	struct gfs2_sbd *sdp = sb->s_fs_info;
1633 	struct gfs2_quota_lvb *qlvb;
1634 	struct gfs2_quota_data *qd;
1635 	struct gfs2_holder q_gh;
1636 	int error;
1637 
1638 	memset(fdq, 0, sizeof(*fdq));
1639 
1640 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1641 		return -ESRCH; /* Crazy XFS error code */
1642 
1643 	if ((qid.type != USRQUOTA) &&
1644 	    (qid.type != GRPQUOTA))
1645 		return -EINVAL;
1646 
1647 	error = qd_get(sdp, qid, &qd);
1648 	if (error)
1649 		return error;
1650 	error = do_glock(qd, FORCE, &q_gh);
1651 	if (error)
1652 		goto out;
1653 
1654 	qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
1655 	fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift;
1656 	fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift;
1657 	fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift;
1658 
1659 	gfs2_glock_dq_uninit(&q_gh);
1660 out:
1661 	qd_put(qd);
1662 	return error;
1663 }
1664 
1665 /* GFS2 only supports a subset of the XFS fields */
1666 #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE)
1667 
gfs2_set_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * fdq)1668 static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
1669 			  struct qc_dqblk *fdq)
1670 {
1671 	struct gfs2_sbd *sdp = sb->s_fs_info;
1672 	struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1673 	struct gfs2_quota_data *qd;
1674 	struct gfs2_holder q_gh, i_gh;
1675 	unsigned int data_blocks, ind_blocks;
1676 	unsigned int blocks = 0;
1677 	int alloc_required;
1678 	loff_t offset;
1679 	int error;
1680 
1681 	if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1682 		return -ESRCH; /* Crazy XFS error code */
1683 
1684 	if ((qid.type != USRQUOTA) &&
1685 	    (qid.type != GRPQUOTA))
1686 		return -EINVAL;
1687 
1688 	if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1689 		return -EINVAL;
1690 
1691 	error = qd_get(sdp, qid, &qd);
1692 	if (error)
1693 		return error;
1694 
1695 	error = gfs2_qa_get(ip);
1696 	if (error)
1697 		goto out_put;
1698 
1699 	inode_lock(&ip->i_inode);
1700 	error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1701 	if (error)
1702 		goto out_unlockput;
1703 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1704 	if (error)
1705 		goto out_q;
1706 
1707 	/* Check for existing entry, if none then alloc new blocks */
1708 	error = update_qd(sdp, qd);
1709 	if (error)
1710 		goto out_i;
1711 
1712 	/* If nothing has changed, this is a no-op */
1713 	if ((fdq->d_fieldmask & QC_SPC_SOFT) &&
1714 	    ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
1715 		fdq->d_fieldmask ^= QC_SPC_SOFT;
1716 
1717 	if ((fdq->d_fieldmask & QC_SPC_HARD) &&
1718 	    ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
1719 		fdq->d_fieldmask ^= QC_SPC_HARD;
1720 
1721 	if ((fdq->d_fieldmask & QC_SPACE) &&
1722 	    ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1723 		fdq->d_fieldmask ^= QC_SPACE;
1724 
1725 	if (fdq->d_fieldmask == 0)
1726 		goto out_i;
1727 
1728 	offset = qd2offset(qd);
1729 	alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
1730 	if (gfs2_is_stuffed(ip))
1731 		alloc_required = 1;
1732 	if (alloc_required) {
1733 		struct gfs2_alloc_parms ap = { .aflags = 0, };
1734 		gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1735 				       &data_blocks, &ind_blocks);
1736 		blocks = 1 + data_blocks + ind_blocks;
1737 		ap.target = blocks;
1738 		error = gfs2_inplace_reserve(ip, &ap);
1739 		if (error)
1740 			goto out_i;
1741 		blocks += gfs2_rg_blocks(ip, blocks);
1742 	}
1743 
1744 	/* Some quotas span block boundaries and can update two blocks,
1745 	   adding an extra block to the transaction to handle such quotas */
1746 	error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
1747 	if (error)
1748 		goto out_release;
1749 
1750 	/* Apply changes */
1751 	error = gfs2_adjust_quota(sdp, offset, 0, qd, fdq);
1752 	if (!error)
1753 		clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
1754 
1755 	gfs2_trans_end(sdp);
1756 out_release:
1757 	if (alloc_required)
1758 		gfs2_inplace_release(ip);
1759 out_i:
1760 	gfs2_glock_dq_uninit(&i_gh);
1761 out_q:
1762 	gfs2_glock_dq_uninit(&q_gh);
1763 out_unlockput:
1764 	gfs2_qa_put(ip);
1765 	inode_unlock(&ip->i_inode);
1766 out_put:
1767 	qd_put(qd);
1768 	return error;
1769 }
1770 
1771 const struct quotactl_ops gfs2_quotactl_ops = {
1772 	.quota_sync     = gfs2_quota_sync,
1773 	.get_state	= gfs2_quota_get_state,
1774 	.get_dqblk	= gfs2_get_dqblk,
1775 	.set_dqblk	= gfs2_set_dqblk,
1776 };
1777 
gfs2_quota_hash_init(void)1778 void __init gfs2_quota_hash_init(void)
1779 {
1780 	unsigned i;
1781 
1782 	for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
1783 		INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
1784 }
1785