blob: 1b6b3675ee1d546b15b78bdf5dc1ee9fb5823b0d [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Peterson0d0868b2007-12-11 18:51:25 -06003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
10/*
11 * Quota change tags are associated with each transaction that allocates or
12 * deallocates space. Those changes are accumulated locally to each node (in a
13 * per-node file) and then are periodically synced to the quota file. This
14 * avoids the bottleneck of constantly touching the quota file, but introduces
15 * fuzziness in the current usage value of IDs that are being used on different
16 * nodes in the cluster simultaneously. So, it is possible for a user on
17 * multiple nodes to overrun their quota, but that overrun is controlable.
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +010018 * Since quota tags are part of transactions, there is no need for a quota check
David Teiglandb3b94fa2006-01-16 16:50:04 +000019 * program to be run on node crashes or anything like that.
20 *
21 * There are couple of knobs that let the administrator manage the quota
22 * fuzziness. "quota_quantum" sets the maximum time a quota change can be
23 * sitting on one node before being synced to the quota file. (The default is
24 * 60 seconds.) Another knob, "quota_scale" controls how quickly the frequency
25 * of quota file syncs increases as the user moves closer to their limit. The
26 * more frequent the syncs, the more accurate the quota enforcement, but that
27 * means that there is more contention between the nodes for the quota file.
28 * The default value is one. This sets the maximum theoretical quota overrun
29 * (with infinite node with infinite bandwidth) to twice the user's limit. (In
30 * practice, the maximum overrun you see should be much less.) A "quota_scale"
31 * number greater than one makes quota syncs more frequent and reduces the
32 * maximum overrun. Numbers less than one (but greater than zero) make quota
33 * syncs less frequent.
34 *
35 * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of
36 * the quota file, so it is not being constantly read.
37 */
38
39#include <linux/sched.h>
40#include <linux/slab.h>
Ying Han1495f232011-05-24 17:12:27 -070041#include <linux/mm.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000042#include <linux/spinlock.h>
43#include <linux/completion.h>
44#include <linux/buffer_head.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000045#include <linux/sort.h>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000046#include <linux/fs.h>
Steven Whitehouse2e565bb2006-10-02 11:38:25 -040047#include <linux/bio.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050048#include <linux/gfs2_ondisk.h>
Steven Whitehouse37b2c832008-11-17 14:25:37 +000049#include <linux/kthread.h>
50#include <linux/freezer.h>
Steven Whitehouse2ec46502009-09-28 12:49:15 +010051#include <linux/quota.h>
Steven Whitehouse1d371b52009-09-11 15:57:27 +010052#include <linux/dqblk_xfs.h>
Steven Whitehouse9b9f0392013-11-01 14:52:06 -040053#include <linux/lockref.h>
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000054#include <linux/list_lru.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
56#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050057#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000058#include "bmap.h"
59#include "glock.h"
60#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000061#include "log.h"
62#include "meta_io.h"
63#include "quota.h"
64#include "rgrp.h"
65#include "super.h"
66#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000067#include "inode.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050068#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000069
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000070/* Lock order: qd_lock -> qd->lockref.lock -> lru lock */
Steven Whitehouse7d808232013-11-01 14:52:08 -040071static DEFINE_SPINLOCK(qd_lock);
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000072struct list_lru gfs2_qd_lru;
Abhijith Das0a7ab792009-01-07 16:03:37 -060073
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000074static void gfs2_qd_dispose(struct list_head *list)
Abhijith Das0a7ab792009-01-07 16:03:37 -060075{
76 struct gfs2_quota_data *qd;
77 struct gfs2_sbd *sdp;
Abhijith Das0a7ab792009-01-07 16:03:37 -060078
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000079 while (!list_empty(list)) {
80 qd = list_entry(list->next, struct gfs2_quota_data, qd_lru);
Abhijith Das0a7ab792009-01-07 16:03:37 -060081 sdp = qd->qd_gl->gl_sbd;
82
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000083 list_del(&qd->qd_lru);
84
Abhijith Das0a7ab792009-01-07 16:03:37 -060085 /* Free from the filesystem-specific list */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000086 spin_lock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -060087 list_del(&qd->qd_list);
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000088 spin_unlock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -060089
Abhijith Das0a7ab792009-01-07 16:03:37 -060090 gfs2_assert_warn(sdp, !qd->qd_change);
91 gfs2_assert_warn(sdp, !qd->qd_slot_count);
92 gfs2_assert_warn(sdp, !qd->qd_bh_count);
93
Steven Whitehousef057f6c2009-01-12 10:43:39 +000094 gfs2_glock_put(qd->qd_gl);
Abhijith Das0a7ab792009-01-07 16:03:37 -060095 atomic_dec(&sdp->sd_quota_count);
96
97 /* Delete it from the common reclaim list */
Abhijith Das0a7ab792009-01-07 16:03:37 -060098 kmem_cache_free(gfs2_quotad_cachep, qd);
Abhijith Das0a7ab792009-01-07 16:03:37 -060099 }
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000100}
101
102
103static enum lru_status gfs2_qd_isolate(struct list_head *item, spinlock_t *lock, void *arg)
104{
105 struct list_head *dispose = arg;
106 struct gfs2_quota_data *qd = list_entry(item, struct gfs2_quota_data, qd_lru);
107
108 if (!spin_trylock(&qd->qd_lockref.lock))
109 return LRU_SKIP;
110
111 if (qd->qd_lockref.count == 0) {
112 lockref_mark_dead(&qd->qd_lockref);
113 list_move(&qd->qd_lru, dispose);
114 }
115
116 spin_unlock(&qd->qd_lockref.lock);
117 return LRU_REMOVED;
118}
119
120static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
121 struct shrink_control *sc)
122{
123 LIST_HEAD(dispose);
124 unsigned long freed;
125
126 if (!(sc->gfp_mask & __GFP_FS))
127 return SHRINK_STOP;
128
129 freed = list_lru_walk_node(&gfs2_qd_lru, sc->nid, gfs2_qd_isolate,
130 &dispose, &sc->nr_to_scan);
131
132 gfs2_qd_dispose(&dispose);
133
Dave Chinner1ab6c492013-08-28 10:18:09 +1000134 return freed;
135}
Abhijith Das0a7ab792009-01-07 16:03:37 -0600136
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000137static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
138 struct shrink_control *sc)
Dave Chinner1ab6c492013-08-28 10:18:09 +1000139{
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000140 return vfs_pressure_ratio(list_lru_count_node(&gfs2_qd_lru, sc->nid));
Abhijith Das0a7ab792009-01-07 16:03:37 -0600141}
142
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000143struct shrinker gfs2_qd_shrinker = {
144 .count_objects = gfs2_qd_shrink_count,
145 .scan_objects = gfs2_qd_shrink_scan,
146 .seeks = DEFAULT_SEEKS,
147 .flags = SHRINKER_NUMA_AWARE,
148};
149
150
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800151static u64 qd2index(struct gfs2_quota_data *qd)
152{
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800153 struct kqid qid = qd->qd_id;
154 return (2 * (u64)from_kqid(&init_user_ns, qid)) +
Bob Peterson37f71572013-05-10 11:59:18 -0400155 ((qid.type == USRQUOTA) ? 0 : 1);
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800156}
157
Steven Whitehousecd915492006-09-04 12:49:07 -0400158static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159{
Steven Whitehousecd915492006-09-04 12:49:07 -0400160 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800162 offset = qd2index(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 offset *= sizeof(struct gfs2_quota);
164
165 return offset;
166}
167
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800168static int qd_alloc(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 struct gfs2_quota_data **qdp)
170{
171 struct gfs2_quota_data *qd;
172 int error;
173
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000174 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 if (!qd)
176 return -ENOMEM;
177
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400178 qd->qd_lockref.count = 1;
179 spin_lock_init(&qd->qd_lockref.lock);
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800180 qd->qd_id = qid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 qd->qd_slot = -1;
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000182 INIT_LIST_HEAD(&qd->qd_lru);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800184 error = gfs2_glock_get(sdp, qd2index(qd),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 &gfs2_quota_glops, CREATE, &qd->qd_gl);
186 if (error)
187 goto fail;
188
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189 *qdp = qd;
190
191 return 0;
192
Steven Whitehousea91ea692006-09-04 12:04:26 -0400193fail:
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000194 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 return error;
196}
197
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800198static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 struct gfs2_quota_data **qdp)
200{
201 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
202 int error, found;
203
204 *qdp = NULL;
205
206 for (;;) {
207 found = 0;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400208 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000210 if (qid_eq(qd->qd_id, qid) &&
211 lockref_get_not_dead(&qd->qd_lockref)) {
212 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 found = 1;
214 break;
215 }
216 }
217
218 if (!found)
219 qd = NULL;
220
221 if (!qd && new_qd) {
222 qd = new_qd;
223 list_add(&qd->qd_list, &sdp->sd_quota_list);
224 atomic_inc(&sdp->sd_quota_count);
225 new_qd = NULL;
226 }
227
Steven Whitehouse7d808232013-11-01 14:52:08 -0400228 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100230 if (qd) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 if (new_qd) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000232 gfs2_glock_put(new_qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000233 kmem_cache_free(gfs2_quotad_cachep, new_qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 }
235 *qdp = qd;
236 return 0;
237 }
238
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800239 error = qd_alloc(sdp, qid, &new_qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 if (error)
241 return error;
242 }
243}
244
245static void qd_hold(struct gfs2_quota_data *qd)
246{
247 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400248 gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
249 lockref_get(&qd->qd_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250}
251
252static void qd_put(struct gfs2_quota_data *qd)
253{
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000254 if (lockref_put_or_lock(&qd->qd_lockref))
255 return;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400256
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000257 qd->qd_lockref.count = 0;
258 list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
259 spin_unlock(&qd->qd_lockref.lock);
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400260
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261}
262
263static int slot_get(struct gfs2_quota_data *qd)
264{
265 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
266 unsigned int c, o = 0, b;
267 unsigned char byte = 0;
268
Steven Whitehouse7d808232013-11-01 14:52:08 -0400269 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270
271 if (qd->qd_slot_count++) {
Steven Whitehouse7d808232013-11-01 14:52:08 -0400272 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 return 0;
274 }
275
276 for (c = 0; c < sdp->sd_quota_chunks; c++)
277 for (o = 0; o < PAGE_SIZE; o++) {
278 byte = sdp->sd_quota_bitmap[c][o];
279 if (byte != 0xFF)
280 goto found;
281 }
282
283 goto fail;
284
Steven Whitehousea91ea692006-09-04 12:04:26 -0400285found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 for (b = 0; b < 8; b++)
287 if (!(byte & (1 << b)))
288 break;
289 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
290
291 if (qd->qd_slot >= sdp->sd_quota_slots)
292 goto fail;
293
294 sdp->sd_quota_bitmap[c][o] |= 1 << b;
295
Steven Whitehouse7d808232013-11-01 14:52:08 -0400296 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297
298 return 0;
299
Steven Whitehousea91ea692006-09-04 12:04:26 -0400300fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 qd->qd_slot_count--;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400302 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 return -ENOSPC;
304}
305
306static void slot_hold(struct gfs2_quota_data *qd)
307{
308 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
309
Steven Whitehouse7d808232013-11-01 14:52:08 -0400310 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 gfs2_assert(sdp, qd->qd_slot_count);
312 qd->qd_slot_count++;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400313 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314}
315
Steven Whitehouse26e43a12013-10-02 14:47:02 +0100316static void gfs2_icbit_munge(struct gfs2_sbd *sdp, unsigned char **bitmap,
317 unsigned int bit, int new_value)
318{
319 unsigned int c, o, b = bit;
320 int old_value;
321
322 c = b / (8 * PAGE_SIZE);
323 b %= 8 * PAGE_SIZE;
324 o = b / 8;
325 b %= 8;
326
327 old_value = (bitmap[c][o] & (1 << b));
328 gfs2_assert_withdraw(sdp, !old_value != !new_value);
329
330 if (new_value)
331 bitmap[c][o] |= 1 << b;
332 else
333 bitmap[c][o] &= ~(1 << b);
334}
335
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336static void slot_put(struct gfs2_quota_data *qd)
337{
338 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
339
Steven Whitehouse7d808232013-11-01 14:52:08 -0400340 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 gfs2_assert(sdp, qd->qd_slot_count);
342 if (!--qd->qd_slot_count) {
343 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
344 qd->qd_slot = -1;
345 }
Steven Whitehouse7d808232013-11-01 14:52:08 -0400346 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347}
348
349static int bh_get(struct gfs2_quota_data *qd)
350{
351 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400352 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 struct buffer_head *bh;
355 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400356 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357
Steven Whitehousef55ab262006-02-21 12:51:39 +0000358 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359
360 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000361 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 return 0;
363 }
364
365 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600366 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367
Steven Whitehouse23591252006-10-13 17:25:45 -0400368 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600369 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 if (error)
371 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400372 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 if (error)
374 goto fail;
375 error = -EIO;
376 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
377 goto fail_brelse;
378
379 qd->qd_bh = bh;
380 qd->qd_bh_qc = (struct gfs2_quota_change *)
381 (bh->b_data + sizeof(struct gfs2_meta_header) +
382 offset * sizeof(struct gfs2_quota_change));
383
Josef Whiter2e95b662007-02-20 00:03:29 -0500384 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385
386 return 0;
387
Steven Whitehousea91ea692006-09-04 12:04:26 -0400388fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400390fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000392 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393 return error;
394}
395
396static void bh_put(struct gfs2_quota_data *qd)
397{
398 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
399
Steven Whitehousef55ab262006-02-21 12:51:39 +0000400 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 gfs2_assert(sdp, qd->qd_bh_count);
402 if (!--qd->qd_bh_count) {
403 brelse(qd->qd_bh);
404 qd->qd_bh = NULL;
405 qd->qd_bh_qc = NULL;
406 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000407 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408}
409
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100410static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
411 u64 *sync_gen)
412{
413 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
414 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
415 (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
416 return 0;
417
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000418 if (!lockref_get_not_dead(&qd->qd_lockref))
419 return 0;
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100420
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000421 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100422 set_bit(QDF_LOCKED, &qd->qd_flags);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100423 qd->qd_change_sync = qd->qd_change;
424 gfs2_assert_warn(sdp, qd->qd_slot_count);
425 qd->qd_slot_count++;
426 return 1;
427}
428
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
430{
431 struct gfs2_quota_data *qd = NULL;
432 int error;
433 int found = 0;
434
435 *qdp = NULL;
436
437 if (sdp->sd_vfs->s_flags & MS_RDONLY)
438 return 0;
439
Steven Whitehouse7d808232013-11-01 14:52:08 -0400440 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441
442 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100443 found = qd_check_sync(sdp, qd, &sdp->sd_quota_sync_gen);
444 if (found)
445 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 }
447
448 if (!found)
449 qd = NULL;
450
Steven Whitehouse7d808232013-11-01 14:52:08 -0400451 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452
453 if (qd) {
454 gfs2_assert_warn(sdp, qd->qd_change_sync);
455 error = bh_get(qd);
456 if (error) {
457 clear_bit(QDF_LOCKED, &qd->qd_flags);
458 slot_put(qd);
459 qd_put(qd);
460 return error;
461 }
462 }
463
464 *qdp = qd;
465
466 return 0;
467}
468
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469static void qd_unlock(struct gfs2_quota_data *qd)
470{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500471 gfs2_assert_warn(qd->qd_gl->gl_sbd,
472 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473 clear_bit(QDF_LOCKED, &qd->qd_flags);
474 bh_put(qd);
475 slot_put(qd);
476 qd_put(qd);
477}
478
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800479static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 struct gfs2_quota_data **qdp)
481{
482 int error;
483
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800484 error = qd_get(sdp, qid, qdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 if (error)
486 return error;
487
488 error = slot_get(*qdp);
489 if (error)
490 goto fail;
491
492 error = bh_get(*qdp);
493 if (error)
494 goto fail_slot;
495
496 return 0;
497
Steven Whitehousea91ea692006-09-04 12:04:26 -0400498fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400500fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501 qd_put(*qdp);
502 return error;
503}
504
505static void qdsb_put(struct gfs2_quota_data *qd)
506{
507 bh_put(qd);
508 slot_put(qd);
509 qd_put(qd);
510}
511
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -0800512int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400514 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson5407e242012-05-18 09:28:23 -0400515 struct gfs2_quota_data **qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 int error;
517
Andrew Priceaaaf68c2012-10-12 16:45:08 +0100518 if (ip->i_res == NULL) {
519 error = gfs2_rs_alloc(ip);
520 if (error)
521 return error;
522 }
Bob Peterson5407e242012-05-18 09:28:23 -0400523
524 qd = ip->i_res->rs_qa_qd;
525
526 if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
528 return -EIO;
529
530 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
531 return 0;
532
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800533 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 if (error)
535 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400536 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537 qd++;
538
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800539 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 if (error)
541 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400542 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 qd++;
544
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800545 if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
546 !uid_eq(uid, ip->i_inode.i_uid)) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800547 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 if (error)
549 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400550 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551 qd++;
552 }
553
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800554 if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
555 !gid_eq(gid, ip->i_inode.i_gid)) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800556 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557 if (error)
558 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400559 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 qd++;
561 }
562
Steven Whitehousea91ea692006-09-04 12:04:26 -0400563out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 if (error)
565 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 return error;
567}
568
569void gfs2_quota_unhold(struct gfs2_inode *ip)
570{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400571 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 unsigned int x;
573
Bob Peterson5407e242012-05-18 09:28:23 -0400574 if (ip->i_res == NULL)
575 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
577
Bob Peterson5407e242012-05-18 09:28:23 -0400578 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
579 qdsb_put(ip->i_res->rs_qa_qd[x]);
580 ip->i_res->rs_qa_qd[x] = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 }
Bob Peterson5407e242012-05-18 09:28:23 -0400582 ip->i_res->rs_qa_qd_num = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583}
584
585static int sort_qd(const void *a, const void *b)
586{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400587 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
588 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800590 if (qid_lt(qd_a->qd_id, qd_b->qd_id))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400591 return -1;
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800592 if (qid_lt(qd_b->qd_id, qd_a->qd_id))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400593 return 1;
Steven Whitehouse48fac172006-09-05 15:17:12 -0400594 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595}
596
Steven Whitehousecd915492006-09-04 12:49:07 -0400597static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598{
599 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400600 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400602 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603
Steven Whitehousef55ab262006-02-21 12:51:39 +0000604 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000605 gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606
607 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
608 qc->qc_change = 0;
609 qc->qc_flags = 0;
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800610 if (qd->qd_id.type == USRQUOTA)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800612 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 }
614
Al Virob44b84d2006-10-14 10:46:30 -0400615 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 qc->qc_change = cpu_to_be64(x);
617
Steven Whitehouse7d808232013-11-01 14:52:08 -0400618 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 qd->qd_change = x;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400620 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
622 if (!x) {
623 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
624 clear_bit(QDF_CHANGE, &qd->qd_flags);
625 qc->qc_flags = 0;
626 qc->qc_id = 0;
627 slot_put(qd);
628 qd_put(qd);
629 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
630 qd_hold(qd);
631 slot_hold(qd);
632 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400633
Steven Whitehousef55ab262006-02-21 12:51:39 +0000634 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635}
636
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000637/**
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100638 * gfs2_adjust_quota - adjust record of current block usage
639 * @ip: The quota inode
640 * @loc: Offset of the entry in the quota file
Steven Whitehousee285c102009-09-23 13:50:49 +0100641 * @change: The amount of usage change to record
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100642 * @qd: The quota data
Steven Whitehousee285c102009-09-23 13:50:49 +0100643 * @fdq: The updated limits to record
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000644 *
645 * This function was mostly borrowed from gfs2_block_truncate_page which was
646 * in turn mostly borrowed from ext3
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100647 *
648 * Returns: 0 or -ve on error
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000649 */
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100650
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000651static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousee285c102009-09-23 13:50:49 +0100652 s64 change, struct gfs2_quota_data *qd,
653 struct fs_disk_quota *fdq)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000654{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400655 struct inode *inode = &ip->i_inode;
Abhijith Das14870b42010-11-18 11:24:24 -0500656 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000657 struct address_space *mapping = inode->i_mapping;
658 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500659 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000660 unsigned blocksize, iblock, pos;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100661 struct buffer_head *bh;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000662 struct page *page;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400663 void *kaddr, *ptr;
Al Viro951b4bd2013-06-02 19:53:40 -0400664 struct gfs2_quota q;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400665 int err, nbytes;
Steven Whitehousee285c102009-09-23 13:50:49 +0100666 u64 size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000667
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100668 if (gfs2_is_stuffed(ip)) {
669 err = gfs2_unstuff_dinode(ip, NULL);
670 if (err)
671 return err;
672 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400673
674 memset(&q, 0, sizeof(struct gfs2_quota));
Andrew Price43066292012-04-16 16:40:55 +0100675 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
Abhijith Das7e619bc2010-05-07 17:50:18 -0400676 if (err < 0)
677 return err;
678
679 err = -EIO;
Al Viro951b4bd2013-06-02 19:53:40 -0400680 be64_add_cpu(&q.qu_value, change);
681 qd->qd_qb.qb_value = q.qu_value;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400682 if (fdq) {
683 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
Al Viro951b4bd2013-06-02 19:53:40 -0400684 q.qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
685 qd->qd_qb.qb_warn = q.qu_warn;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400686 }
687 if (fdq->d_fieldmask & FS_DQ_BHARD) {
Al Viro951b4bd2013-06-02 19:53:40 -0400688 q.qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
689 qd->qd_qb.qb_limit = q.qu_limit;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400690 }
Abhijith Das802ec9b2010-11-18 11:26:46 -0500691 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
Al Viro951b4bd2013-06-02 19:53:40 -0400692 q.qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
693 qd->qd_qb.qb_value = q.qu_value;
Abhijith Das802ec9b2010-11-18 11:26:46 -0500694 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400695 }
696
697 /* Write the quota into the quota file on disk */
Al Viro951b4bd2013-06-02 19:53:40 -0400698 ptr = &q;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400699 nbytes = sizeof(struct gfs2_quota);
700get_a_page:
Bob Peterson220cca22012-03-19 15:25:50 -0400701 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000702 if (!page)
703 return -ENOMEM;
704
705 blocksize = inode->i_sb->s_blocksize;
706 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
707
708 if (!page_has_buffers(page))
709 create_empty_buffers(page, blocksize, 0);
710
711 bh = page_buffers(page);
712 pos = blocksize;
713 while (offset >= pos) {
714 bh = bh->b_this_page;
715 iblock++;
716 pos += blocksize;
717 }
718
719 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600720 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000721 if (!buffer_mapped(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400722 goto unlock_out;
723 /* If it's a newly allocated disk block for quota, zero it */
Abhijith Das8b421602010-07-04 01:33:24 -0400724 if (buffer_new(bh))
725 zero_user(page, pos - blocksize, bh->b_size);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000726 }
727
728 if (PageUptodate(page))
729 set_buffer_uptodate(bh);
730
731 if (!buffer_uptodate(bh)) {
Steven Whitehouse20ed0532011-10-31 09:52:02 +0000732 ll_rw_block(READ | REQ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000733 wait_on_buffer(bh);
734 if (!buffer_uptodate(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400735 goto unlock_out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000736 }
737
Bob Peterson37f71572013-05-10 11:59:18 -0400738 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000739
Cong Wangd9349282011-11-25 23:14:30 +0800740 kaddr = kmap_atomic(page);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400741 if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
742 nbytes = PAGE_CACHE_SIZE - offset;
743 memcpy(kaddr + offset, ptr, nbytes);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000744 flush_dcache_page(page);
Cong Wangd9349282011-11-25 23:14:30 +0800745 kunmap_atomic(kaddr);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400746 unlock_page(page);
747 page_cache_release(page);
Steven Whitehousee285c102009-09-23 13:50:49 +0100748
Abhijith Das7e619bc2010-05-07 17:50:18 -0400749 /* If quota straddles page boundary, we need to update the rest of the
750 * quota at the beginning of the next page */
Abhijith Das8b421602010-07-04 01:33:24 -0400751 if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
Abhijith Das7e619bc2010-05-07 17:50:18 -0400752 ptr = ptr + nbytes;
753 nbytes = sizeof(struct gfs2_quota) - nbytes;
754 offset = 0;
755 index++;
756 goto get_a_page;
757 }
758
Steven Whitehousee285c102009-09-23 13:50:49 +0100759 size = loc + sizeof(struct gfs2_quota);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100760 if (size > inode->i_size)
Steven Whitehousee285c102009-09-23 13:50:49 +0100761 i_size_write(inode, size);
Steven Whitehousee285c102009-09-23 13:50:49 +0100762 inode->i_mtime = inode->i_atime = CURRENT_TIME;
Steven Whitehousee285c102009-09-23 13:50:49 +0100763 mark_inode_dirty(inode);
Bob Peterson500242a2012-05-15 14:51:54 -0400764 return 0;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100765
Abhijith Das7e619bc2010-05-07 17:50:18 -0400766unlock_out:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000767 unlock_page(page);
768 page_cache_release(page);
769 return err;
770}
771
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
773{
774 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400775 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100776 struct gfs2_alloc_parms ap = { .aflags = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 unsigned int data_blocks, ind_blocks;
778 struct gfs2_holder *ghs, i_gh;
779 unsigned int qx, x;
780 struct gfs2_quota_data *qd;
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100781 unsigned reserved;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000782 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600783 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 int error;
785
Bob Peterson0a305e42012-06-06 11:17:59 +0100786 error = gfs2_rs_alloc(ip);
787 if (error)
788 return error;
789
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
791 &data_blocks, &ind_blocks);
792
Josef Bacik16c5f062008-04-09 09:33:41 -0400793 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 if (!ghs)
795 return -ENOMEM;
796
797 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
Jan Kara56aa72d2012-09-05 16:55:11 -0400798 mutex_lock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 for (qx = 0; qx < num_qd; qx++) {
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100800 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801 GL_NOCACHE, &ghs[qx]);
802 if (error)
803 goto out;
804 }
805
806 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
807 if (error)
808 goto out;
809
810 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 offset = qd2offset(qda[x]);
Bob Peterson461cb412010-06-24 19:21:20 -0400812 if (gfs2_write_alloc_required(ip, offset,
813 sizeof(struct gfs2_quota)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814 nalloc++;
815 }
816
Abhijith Das20b95bf2008-03-06 17:43:52 -0600817 /*
818 * 1 blk for unstuffing inode if stuffed. We add this extra
819 * block to the reservation unconditionally. If the inode
820 * doesn't need unstuffing, the block will be released to the
821 * rgrp since it won't be allocated during the transaction
822 */
Abhijith Das7e619bc2010-05-07 17:50:18 -0400823 /* +3 in the end for unstuffing block, inode size update block
824 * and another block in case quota straddles page boundary and
825 * two blocks need to be updated instead of 1 */
826 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600827
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100828 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100829 ap.target = reserved;
830 error = gfs2_inplace_reserve(ip, &ap);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600831 if (error)
832 goto out_alloc;
833
834 if (nalloc)
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100835 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600836
837 error = gfs2_trans_begin(sdp, blocks, 0);
838 if (error)
839 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840
841 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 qd = qda[x];
843 offset = qd2offset(qd);
Steven Whitehousee285c102009-09-23 13:50:49 +0100844 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000845 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 goto out_end_trans;
847
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 do_qc(qd, -qd->qd_change_sync);
Abhijith Das662e3a52011-03-08 10:40:42 -0500849 set_bit(QDF_REFRESH, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 }
851
852 error = 0;
853
Steven Whitehousea91ea692006-09-04 12:04:26 -0400854out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400856out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600857 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400858out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400860out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 while (qx--)
862 gfs2_glock_dq_uninit(&ghs[qx]);
Steven Whitehousee285c102009-09-23 13:50:49 +0100863 mutex_unlock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400865 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 return error;
867}
868
Steven Whitehousee285c102009-09-23 13:50:49 +0100869static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
870{
871 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
872 struct gfs2_quota q;
873 struct gfs2_quota_lvb *qlvb;
874 loff_t pos;
875 int error;
876
877 memset(&q, 0, sizeof(struct gfs2_quota));
878 pos = qd2offset(qd);
Andrew Price43066292012-04-16 16:40:55 +0100879 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
Steven Whitehousee285c102009-09-23 13:50:49 +0100880 if (error < 0)
881 return error;
882
David Teigland4e2f8842012-11-14 13:47:37 -0500883 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehousee285c102009-09-23 13:50:49 +0100884 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
885 qlvb->__pad = 0;
886 qlvb->qb_limit = q.qu_limit;
887 qlvb->qb_warn = q.qu_warn;
888 qlvb->qb_value = q.qu_value;
889 qd->qd_qb = *qlvb;
890
891 return 0;
892}
893
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
895 struct gfs2_holder *q_gh)
896{
897 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400898 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 struct gfs2_holder i_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 int error;
901
Steven Whitehousea91ea692006-09-04 12:04:26 -0400902restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
904 if (error)
905 return error;
906
David Teigland4e2f8842012-11-14 13:47:37 -0500907 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400909 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 gfs2_glock_dq_uninit(q_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100911 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
912 GL_NOCACHE, q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 if (error)
914 return error;
915
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400916 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 if (error)
918 goto fail;
919
Steven Whitehousee285c102009-09-23 13:50:49 +0100920 error = update_qd(sdp, qd);
921 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 goto fail_gunlock;
Steven Whitehousee285c102009-09-23 13:50:49 +0100923
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100925 gfs2_glock_dq_uninit(q_gh);
926 force_refresh = 0;
927 goto restart;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 }
929
930 return 0;
931
Steven Whitehousea91ea692006-09-04 12:04:26 -0400932fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400934fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 return error;
937}
938
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -0800939int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000940{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400941 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Abhijith Das662e3a52011-03-08 10:40:42 -0500942 struct gfs2_quota_data *qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 unsigned int x;
944 int error = 0;
945
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100946 error = gfs2_quota_hold(ip, uid, gid);
947 if (error)
948 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949
950 if (capable(CAP_SYS_RESOURCE) ||
951 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
952 return 0;
953
Bob Peterson5407e242012-05-18 09:28:23 -0400954 sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
955 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956
Bob Peterson5407e242012-05-18 09:28:23 -0400957 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
Abhijith Das662e3a52011-03-08 10:40:42 -0500958 int force = NO_FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400959 qd = ip->i_res->rs_qa_qd[x];
Abhijith Das662e3a52011-03-08 10:40:42 -0500960 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
961 force = FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400962 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 if (error)
964 break;
965 }
966
967 if (!error)
968 set_bit(GIF_QD_LOCKED, &ip->i_flags);
969 else {
970 while (x--)
Bob Peterson5407e242012-05-18 09:28:23 -0400971 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 gfs2_quota_unhold(ip);
973 }
974
975 return error;
976}
977
978static int need_sync(struct gfs2_quota_data *qd)
979{
980 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
981 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400982 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 unsigned int num, den;
984 int do_sync = 1;
985
986 if (!qd->qd_qb.qb_limit)
987 return 0;
988
Steven Whitehouse7d808232013-11-01 14:52:08 -0400989 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 value = qd->qd_change;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400991 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992
993 spin_lock(&gt->gt_spin);
994 num = gt->gt_quota_scale_num;
995 den = gt->gt_quota_scale_den;
996 spin_unlock(&gt->gt_spin);
997
998 if (value < 0)
999 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001000 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1001 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 do_sync = 0;
1003 else {
1004 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +01001005 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001006 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -04001007 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 do_sync = 0;
1009 }
1010
1011 return do_sync;
1012}
1013
1014void gfs2_quota_unlock(struct gfs2_inode *ip)
1015{
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001016 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 struct gfs2_quota_data *qda[4];
1018 unsigned int count = 0;
1019 unsigned int x;
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001020 int found;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021
1022 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1023 goto out;
1024
Bob Peterson5407e242012-05-18 09:28:23 -04001025 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 struct gfs2_quota_data *qd;
1027 int sync;
1028
Bob Peterson5407e242012-05-18 09:28:23 -04001029 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030 sync = need_sync(qd);
1031
Bob Peterson5407e242012-05-18 09:28:23 -04001032 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001033 if (!sync)
1034 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035
Steven Whitehouse7d808232013-11-01 14:52:08 -04001036 spin_lock(&qd_lock);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001037 found = qd_check_sync(sdp, qd, NULL);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001038 spin_unlock(&qd_lock);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001039
1040 if (!found)
1041 continue;
1042
1043 gfs2_assert_warn(sdp, qd->qd_change_sync);
1044 if (bh_get(qd)) {
1045 clear_bit(QDF_LOCKED, &qd->qd_flags);
1046 slot_put(qd);
1047 qd_put(qd);
1048 continue;
1049 }
1050
1051 qda[count++] = qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 }
1053
1054 if (count) {
1055 do_sync(count, qda);
1056 for (x = 0; x < count; x++)
1057 qd_unlock(qda[x]);
1058 }
1059
Steven Whitehousea91ea692006-09-04 12:04:26 -04001060out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 gfs2_quota_unhold(ip);
1062}
1063
1064#define MAX_LINE 256
1065
1066static int print_message(struct gfs2_quota_data *qd, char *type)
1067{
1068 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001070 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
Steven Whitehouse02630a12006-07-03 11:20:06 -04001071 sdp->sd_fsname, type,
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001072 (qd->qd_id.type == USRQUOTA) ? "user" : "group",
1073 from_kqid(&init_user_ns, qd->qd_id));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074
1075 return 0;
1076}
1077
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001078int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001080 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001082 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083 unsigned int x;
1084 int error = 0;
1085
1086 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1087 return 0;
1088
1089 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1090 return 0;
1091
Bob Peterson5407e242012-05-18 09:28:23 -04001092 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1093 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001095 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1096 qid_eq(qd->qd_id, make_kqid_gid(gid))))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 continue;
1098
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001099 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001100 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 value += qd->qd_change;
Steven Whitehouse7d808232013-11-01 14:52:08 -04001102 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103
Steven Whitehousecd915492006-09-04 12:49:07 -04001104 if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 print_message(qd, "exceeded");
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001106 quota_send_warning(qd->qd_id,
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001107 sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1108
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109 error = -EDQUOT;
1110 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001111 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001112 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001114 gfs2_tune_get(sdp,
1115 gt_quota_warn_period) * HZ)) {
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001116 quota_send_warning(qd->qd_id,
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001117 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 error = print_message(qd, "warning");
1119 qd->qd_last_warn = jiffies;
1120 }
1121 }
1122
1123 return error;
1124}
1125
Steven Whitehousecd915492006-09-04 12:49:07 -04001126void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001127 kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 struct gfs2_quota_data *qd;
1130 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001132 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001134 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 return;
1136
Bob Peterson5407e242012-05-18 09:28:23 -04001137 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1138 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001140 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1141 qid_eq(qd->qd_id, make_kqid_gid(gid))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 }
1144 }
1145}
1146
Jan Karaceed1722012-07-03 16:45:28 +02001147int gfs2_quota_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001148{
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001149 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150 struct gfs2_quota_data **qda;
Steven Whitehousebef292a2013-10-03 18:43:20 +01001151 unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 unsigned int num_qd;
1153 unsigned int x;
1154 int error = 0;
1155
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1157 if (!qda)
1158 return -ENOMEM;
1159
Steven Whitehousee46c7722013-10-04 12:29:34 +01001160 mutex_lock(&sdp->sd_quota_sync_mutex);
1161 sdp->sd_quota_sync_gen++;
1162
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163 do {
1164 num_qd = 0;
1165
1166 for (;;) {
1167 error = qd_fish(sdp, qda + num_qd);
1168 if (error || !qda[num_qd])
1169 break;
1170 if (++num_qd == max_qd)
1171 break;
1172 }
1173
1174 if (num_qd) {
1175 if (!error)
1176 error = do_sync(num_qd, qda);
1177 if (!error)
1178 for (x = 0; x < num_qd; x++)
1179 qda[x]->qd_sync_gen =
1180 sdp->sd_quota_sync_gen;
1181
1182 for (x = 0; x < num_qd; x++)
1183 qd_unlock(qda[x]);
1184 }
1185 } while (!error && num_qd == max_qd);
1186
Steven Whitehousee46c7722013-10-04 12:29:34 +01001187 mutex_unlock(&sdp->sd_quota_sync_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188 kfree(qda);
1189
1190 return error;
1191}
1192
Eric W. Biedermaned87dab2013-01-31 19:42:40 -08001193int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194{
1195 struct gfs2_quota_data *qd;
1196 struct gfs2_holder q_gh;
1197 int error;
1198
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001199 error = qd_get(sdp, qid, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200 if (error)
1201 return error;
1202
1203 error = do_glock(qd, FORCE, &q_gh);
1204 if (!error)
1205 gfs2_glock_dq_uninit(&q_gh);
1206
1207 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208 return error;
1209}
1210
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211int gfs2_quota_init(struct gfs2_sbd *sdp)
1212{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001213 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001214 u64 size = i_size_read(sdp->sd_qc_inode);
1215 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216 unsigned int x, slot = 0;
1217 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001218 u64 dblock;
1219 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220 int error;
1221
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001222 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001223 return -EIO;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001224
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001226 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227
1228 error = -ENOMEM;
1229
1230 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
Josef Bacik16c5f062008-04-09 09:33:41 -04001231 sizeof(unsigned char *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232 if (!sdp->sd_quota_bitmap)
1233 return error;
1234
1235 for (x = 0; x < sdp->sd_quota_chunks; x++) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001236 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 if (!sdp->sd_quota_bitmap[x])
1238 goto fail;
1239 }
1240
1241 for (x = 0; x < blocks; x++) {
1242 struct buffer_head *bh;
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001243 const struct gfs2_quota_change *qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 unsigned int y;
1245
1246 if (!extlen) {
1247 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001248 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 if (error)
1250 goto fail;
1251 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001253 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1254 if (!bh)
1255 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1257 brelse(bh);
1258 goto fail;
1259 }
1260
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001261 qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001262 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263 y++, slot++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 struct gfs2_quota_data *qd;
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001265 s64 qc_change = be64_to_cpu(qc->qc_change);
1266 u32 qc_flags = be32_to_cpu(qc->qc_flags);
1267 enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1268 USRQUOTA : GRPQUOTA;
1269 struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1270 be32_to_cpu(qc->qc_id));
1271 qc++;
1272 if (!qc_change)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 continue;
1274
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001275 error = qd_alloc(sdp, qc_id, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276 if (error) {
1277 brelse(bh);
1278 goto fail;
1279 }
1280
1281 set_bit(QDF_CHANGE, &qd->qd_flags);
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001282 qd->qd_change = qc_change;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283 qd->qd_slot = slot;
1284 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285
Steven Whitehouse7d808232013-11-01 14:52:08 -04001286 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1288 list_add(&qd->qd_list, &sdp->sd_quota_list);
1289 atomic_inc(&sdp->sd_quota_count);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001290 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291
1292 found++;
1293 }
1294
1295 brelse(bh);
1296 dblock++;
1297 extlen--;
1298 }
1299
1300 if (found)
1301 fs_info(sdp, "found %u quota changes\n", found);
1302
1303 return 0;
1304
Steven Whitehousea91ea692006-09-04 12:04:26 -04001305fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306 gfs2_quota_cleanup(sdp);
1307 return error;
1308}
1309
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1311{
1312 struct list_head *head = &sdp->sd_quota_list;
1313 struct gfs2_quota_data *qd;
1314 unsigned int x;
1315
Steven Whitehouse7d808232013-11-01 14:52:08 -04001316 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 while (!list_empty(head)) {
1318 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1319
Steven Whitehouse9b9f0392013-11-01 14:52:06 -04001320 /*
1321 * To be removed in due course... we should be able to
1322 * ensure that all refs to the qd have done by this point
1323 * so that this rather odd test is not required
1324 */
1325 spin_lock(&qd->qd_lockref.lock);
1326 if (qd->qd_lockref.count > 1 ||
1327 (qd->qd_lockref.count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1328 spin_unlock(&qd->qd_lockref.lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001329 list_move(&qd->qd_list, head);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001330 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001331 schedule();
Steven Whitehouse7d808232013-11-01 14:52:08 -04001332 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333 continue;
1334 }
Steven Whitehouse9b9f0392013-11-01 14:52:06 -04001335 spin_unlock(&qd->qd_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001336
Abhijith Das0a7ab792009-01-07 16:03:37 -06001337 list_del(&qd->qd_list);
1338 /* Also remove if this qd exists in the reclaim list */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +00001339 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001340 atomic_dec(&sdp->sd_quota_count);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001341 spin_unlock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001342
Steven Whitehouse9b9f0392013-11-01 14:52:06 -04001343 if (!qd->qd_lockref.count) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 gfs2_assert_warn(sdp, !qd->qd_change);
1345 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1346 } else
1347 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1348 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1349
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001350 gfs2_glock_put(qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001351 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352
Steven Whitehouse7d808232013-11-01 14:52:08 -04001353 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 }
Steven Whitehouse7d808232013-11-01 14:52:08 -04001355 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356
1357 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1358
1359 if (sdp->sd_quota_bitmap) {
1360 for (x = 0; x < sdp->sd_quota_chunks; x++)
1361 kfree(sdp->sd_quota_bitmap[x]);
1362 kfree(sdp->sd_quota_bitmap);
1363 }
1364}
1365
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001366static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1367{
1368 if (error == 0 || error == -EROFS)
1369 return;
1370 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1371 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1372}
1373
1374static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001375 int (*fxn)(struct super_block *sb, int type),
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001376 unsigned long t, unsigned long *timeo,
1377 unsigned int *new_timeo)
1378{
1379 if (t >= *timeo) {
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001380 int error = fxn(sdp->sd_vfs, 0);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001381 quotad_error(sdp, msg, error);
1382 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1383 } else {
1384 *timeo -= t;
1385 }
1386}
1387
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001388static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1389{
1390 struct gfs2_inode *ip;
1391
1392 while(1) {
1393 ip = NULL;
1394 spin_lock(&sdp->sd_trunc_lock);
1395 if (!list_empty(&sdp->sd_trunc_list)) {
1396 ip = list_entry(sdp->sd_trunc_list.next,
1397 struct gfs2_inode, i_trunc_list);
1398 list_del_init(&ip->i_trunc_list);
1399 }
1400 spin_unlock(&sdp->sd_trunc_lock);
1401 if (ip == NULL)
1402 return;
1403 gfs2_glock_finish_truncate(ip);
1404 }
1405}
1406
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001407void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1408 if (!sdp->sd_statfs_force_sync) {
1409 sdp->sd_statfs_force_sync = 1;
1410 wake_up(&sdp->sd_quota_wait);
1411 }
1412}
1413
1414
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001415/**
1416 * gfs2_quotad - Write cached quota changes into the quota file
1417 * @sdp: Pointer to GFS2 superblock
1418 *
1419 */
1420
1421int gfs2_quotad(void *data)
1422{
1423 struct gfs2_sbd *sdp = data;
1424 struct gfs2_tune *tune = &sdp->sd_tune;
1425 unsigned long statfs_timeo = 0;
1426 unsigned long quotad_timeo = 0;
1427 unsigned long t = 0;
1428 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001429 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001430
1431 while (!kthread_should_stop()) {
1432
1433 /* Update the master statfs file */
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001434 if (sdp->sd_statfs_force_sync) {
1435 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1436 quotad_error(sdp, "statfs", error);
1437 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1438 }
1439 else
1440 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1441 &statfs_timeo,
1442 &tune->gt_statfs_quantum);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001443
1444 /* Update quota file */
Steven Whitehouseedd2e9a2013-06-03 11:12:59 +01001445 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001446 &quotad_timeo, &tune->gt_quota_quantum);
1447
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001448 /* Check for & recover partially truncated inodes */
1449 quotad_check_trunc_list(sdp);
1450
Tejun Heoa0acae02011-11-21 12:32:22 -08001451 try_to_freeze();
1452
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001453 t = min(quotad_timeo, statfs_timeo);
1454
Steven Whitehouse7fa5d202009-03-31 15:49:08 +01001455 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001456 spin_lock(&sdp->sd_trunc_lock);
1457 empty = list_empty(&sdp->sd_trunc_list);
1458 spin_unlock(&sdp->sd_trunc_lock);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001459 if (empty && !sdp->sd_statfs_force_sync)
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001460 t -= schedule_timeout(t);
1461 else
1462 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001463 finish_wait(&sdp->sd_quota_wait, &wait);
1464 }
1465
1466 return 0;
1467}
1468
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001469static int gfs2_quota_get_xstate(struct super_block *sb,
1470 struct fs_quota_stat *fqs)
1471{
1472 struct gfs2_sbd *sdp = sb->s_fs_info;
1473
1474 memset(fqs, 0, sizeof(struct fs_quota_stat));
1475 fqs->qs_version = FS_QSTAT_VERSION;
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001476
1477 switch (sdp->sd_args.ar_quota) {
1478 case GFS2_QUOTA_ON:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001479 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001480 /*FALLTHRU*/
1481 case GFS2_QUOTA_ACCOUNT:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001482 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001483 break;
1484 case GFS2_QUOTA_OFF:
1485 break;
1486 }
1487
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001488 if (sdp->sd_quota_inode) {
1489 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1490 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1491 }
1492 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1493 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +00001494 fqs->qs_incoredqs = list_lru_count(&gfs2_qd_lru);
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001495 return 0;
1496}
1497
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001498static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001499 struct fs_disk_quota *fdq)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001500{
1501 struct gfs2_sbd *sdp = sb->s_fs_info;
1502 struct gfs2_quota_lvb *qlvb;
1503 struct gfs2_quota_data *qd;
1504 struct gfs2_holder q_gh;
1505 int error;
1506
1507 memset(fdq, 0, sizeof(struct fs_disk_quota));
1508
1509 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1510 return -ESRCH; /* Crazy XFS error code */
1511
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001512 if ((qid.type != USRQUOTA) &&
1513 (qid.type != GRPQUOTA))
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001514 return -EINVAL;
1515
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001516 error = qd_get(sdp, qid, &qd);
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001517 if (error)
1518 return error;
1519 error = do_glock(qd, FORCE, &q_gh);
1520 if (error)
1521 goto out;
1522
David Teigland4e2f8842012-11-14 13:47:37 -05001523 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001524 fdq->d_version = FS_DQUOT_VERSION;
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001525 fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
Eric W. Biederman558e8522013-01-31 18:15:33 -08001526 fdq->d_id = from_kqid_munged(current_user_ns(), qid);
Abhijith Das14870b42010-11-18 11:24:24 -05001527 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1528 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1529 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001530
1531 gfs2_glock_dq_uninit(&q_gh);
1532out:
1533 qd_put(qd);
1534 return error;
1535}
1536
Steven Whitehousee285c102009-09-23 13:50:49 +01001537/* GFS2 only supports a subset of the XFS fields */
Abhijith Das802ec9b2010-11-18 11:26:46 -05001538#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
Steven Whitehousee285c102009-09-23 13:50:49 +01001539
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001540static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001541 struct fs_disk_quota *fdq)
Steven Whitehousee285c102009-09-23 13:50:49 +01001542{
1543 struct gfs2_sbd *sdp = sb->s_fs_info;
1544 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1545 struct gfs2_quota_data *qd;
1546 struct gfs2_holder q_gh, i_gh;
1547 unsigned int data_blocks, ind_blocks;
1548 unsigned int blocks = 0;
1549 int alloc_required;
Steven Whitehousee285c102009-09-23 13:50:49 +01001550 loff_t offset;
1551 int error;
1552
1553 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1554 return -ESRCH; /* Crazy XFS error code */
1555
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001556 if ((qid.type != USRQUOTA) &&
1557 (qid.type != GRPQUOTA))
Steven Whitehousee285c102009-09-23 13:50:49 +01001558 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001559
1560 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1561 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001562
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001563 error = qd_get(sdp, qid, &qd);
Steven Whitehousee285c102009-09-23 13:50:49 +01001564 if (error)
1565 return error;
1566
Bob Peterson0a305e42012-06-06 11:17:59 +01001567 error = gfs2_rs_alloc(ip);
1568 if (error)
1569 goto out_put;
1570
Steven Whitehousee285c102009-09-23 13:50:49 +01001571 mutex_lock(&ip->i_inode.i_mutex);
1572 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1573 if (error)
Bob Peterson0a305e42012-06-06 11:17:59 +01001574 goto out_unlockput;
Steven Whitehousee285c102009-09-23 13:50:49 +01001575 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1576 if (error)
1577 goto out_q;
1578
1579 /* Check for existing entry, if none then alloc new blocks */
1580 error = update_qd(sdp, qd);
1581 if (error)
1582 goto out_i;
1583
1584 /* If nothing has changed, this is a no-op */
1585 if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001586 ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001587 fdq->d_fieldmask ^= FS_DQ_BSOFT;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001588
Steven Whitehousee285c102009-09-23 13:50:49 +01001589 if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001590 ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001591 fdq->d_fieldmask ^= FS_DQ_BHARD;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001592
1593 if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1594 ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1595 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1596
Steven Whitehousee285c102009-09-23 13:50:49 +01001597 if (fdq->d_fieldmask == 0)
1598 goto out_i;
1599
1600 offset = qd2offset(qd);
Bob Peterson461cb412010-06-24 19:21:20 -04001601 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
Abhijith Dase79a46a2011-02-07 11:22:41 -05001602 if (gfs2_is_stuffed(ip))
1603 alloc_required = 1;
Steven Whitehousee285c102009-09-23 13:50:49 +01001604 if (alloc_required) {
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001605 struct gfs2_alloc_parms ap = { .aflags = 0, };
Steven Whitehousee285c102009-09-23 13:50:49 +01001606 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1607 &data_blocks, &ind_blocks);
Bob Peterson564e12b2011-11-21 13:36:17 -05001608 blocks = 1 + data_blocks + ind_blocks;
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001609 ap.target = blocks;
1610 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousee285c102009-09-23 13:50:49 +01001611 if (error)
Bob Peterson564e12b2011-11-21 13:36:17 -05001612 goto out_i;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001613 blocks += gfs2_rg_blocks(ip, blocks);
Steven Whitehousee285c102009-09-23 13:50:49 +01001614 }
1615
Abhijith Dase79a46a2011-02-07 11:22:41 -05001616 /* Some quotas span block boundaries and can update two blocks,
1617 adding an extra block to the transaction to handle such quotas */
1618 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001619 if (error)
1620 goto out_release;
1621
1622 /* Apply changes */
1623 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1624
1625 gfs2_trans_end(sdp);
1626out_release:
Bob Peterson564e12b2011-11-21 13:36:17 -05001627 if (alloc_required)
Steven Whitehousee285c102009-09-23 13:50:49 +01001628 gfs2_inplace_release(ip);
Steven Whitehousee285c102009-09-23 13:50:49 +01001629out_i:
1630 gfs2_glock_dq_uninit(&i_gh);
1631out_q:
1632 gfs2_glock_dq_uninit(&q_gh);
Bob Peterson0a305e42012-06-06 11:17:59 +01001633out_unlockput:
Steven Whitehousee285c102009-09-23 13:50:49 +01001634 mutex_unlock(&ip->i_inode.i_mutex);
Bob Peterson0a305e42012-06-06 11:17:59 +01001635out_put:
Steven Whitehousee285c102009-09-23 13:50:49 +01001636 qd_put(qd);
1637 return error;
1638}
1639
Steven Whitehousecc632e72009-09-15 09:59:02 +01001640const struct quotactl_ops gfs2_quotactl_ops = {
1641 .quota_sync = gfs2_quota_sync,
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001642 .get_xstate = gfs2_quota_get_xstate,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001643 .get_dqblk = gfs2_get_dqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001644 .set_dqblk = gfs2_set_dqblk,
Steven Whitehousecc632e72009-09-15 09:59:02 +01001645};