blob: 3287d987150828f6b33f3d1c64a584d0afc7afc0 [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>
Steven Whitehousec754fbb2013-12-12 10:47:59 +000055#include <linux/rcupdate.h>
56#include <linux/rculist_bl.h>
57#include <linux/bit_spinlock.h>
58#include <linux/jhash.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000059
60#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050061#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000062#include "bmap.h"
63#include "glock.h"
64#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000065#include "log.h"
66#include "meta_io.h"
67#include "quota.h"
68#include "rgrp.h"
69#include "super.h"
70#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000071#include "inode.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050072#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000073
Steven Whitehousec754fbb2013-12-12 10:47:59 +000074#define GFS2_QD_HASH_SHIFT 12
75#define GFS2_QD_HASH_SIZE (1 << 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 */
Steven Whitehouse7d808232013-11-01 14:52:08 -040079static DEFINE_SPINLOCK(qd_lock);
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000080struct list_lru gfs2_qd_lru;
Abhijith Das0a7ab792009-01-07 16:03:37 -060081
Steven Whitehousec754fbb2013-12-12 10:47:59 +000082static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
83
84static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
85 const struct kqid qid)
86{
87 unsigned int h;
88
89 h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
90 h = jhash(&qid, sizeof(struct kqid), h);
91
92 return h & GFS2_QD_HASH_MASK;
93}
94
95static inline void spin_lock_bucket(unsigned int hash)
96{
97 hlist_bl_lock(&qd_hash_table[hash]);
98}
99
100static inline void spin_unlock_bucket(unsigned int hash)
101{
102 hlist_bl_unlock(&qd_hash_table[hash]);
103}
104
105static void gfs2_qd_dealloc(struct rcu_head *rcu)
106{
107 struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
108 kmem_cache_free(gfs2_quotad_cachep, qd);
109}
110
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000111static void gfs2_qd_dispose(struct list_head *list)
Abhijith Das0a7ab792009-01-07 16:03:37 -0600112{
113 struct gfs2_quota_data *qd;
114 struct gfs2_sbd *sdp;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600115
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000116 while (!list_empty(list)) {
117 qd = list_entry(list->next, struct gfs2_quota_data, qd_lru);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600118 sdp = qd->qd_gl->gl_sbd;
119
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000120 list_del(&qd->qd_lru);
121
Abhijith Das0a7ab792009-01-07 16:03:37 -0600122 /* Free from the filesystem-specific list */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000123 spin_lock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600124 list_del(&qd->qd_list);
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000125 spin_unlock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600126
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000127 spin_lock_bucket(qd->qd_hash);
128 hlist_bl_del_rcu(&qd->qd_hlist);
129 spin_unlock_bucket(qd->qd_hash);
130
Abhijith Das0a7ab792009-01-07 16:03:37 -0600131 gfs2_assert_warn(sdp, !qd->qd_change);
132 gfs2_assert_warn(sdp, !qd->qd_slot_count);
133 gfs2_assert_warn(sdp, !qd->qd_bh_count);
134
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000135 gfs2_glock_put(qd->qd_gl);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600136 atomic_dec(&sdp->sd_quota_count);
137
138 /* Delete it from the common reclaim list */
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000139 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600140 }
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000141}
142
143
144static enum lru_status gfs2_qd_isolate(struct list_head *item, spinlock_t *lock, void *arg)
145{
146 struct list_head *dispose = arg;
147 struct gfs2_quota_data *qd = list_entry(item, struct gfs2_quota_data, qd_lru);
148
149 if (!spin_trylock(&qd->qd_lockref.lock))
150 return LRU_SKIP;
151
152 if (qd->qd_lockref.count == 0) {
153 lockref_mark_dead(&qd->qd_lockref);
154 list_move(&qd->qd_lru, dispose);
155 }
156
157 spin_unlock(&qd->qd_lockref.lock);
158 return LRU_REMOVED;
159}
160
161static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
162 struct shrink_control *sc)
163{
164 LIST_HEAD(dispose);
165 unsigned long freed;
166
167 if (!(sc->gfp_mask & __GFP_FS))
168 return SHRINK_STOP;
169
170 freed = list_lru_walk_node(&gfs2_qd_lru, sc->nid, gfs2_qd_isolate,
171 &dispose, &sc->nr_to_scan);
172
173 gfs2_qd_dispose(&dispose);
174
Dave Chinner1ab6c492013-08-28 10:18:09 +1000175 return freed;
176}
Abhijith Das0a7ab792009-01-07 16:03:37 -0600177
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000178static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
179 struct shrink_control *sc)
Dave Chinner1ab6c492013-08-28 10:18:09 +1000180{
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000181 return vfs_pressure_ratio(list_lru_count_node(&gfs2_qd_lru, sc->nid));
Abhijith Das0a7ab792009-01-07 16:03:37 -0600182}
183
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000184struct shrinker gfs2_qd_shrinker = {
185 .count_objects = gfs2_qd_shrink_count,
186 .scan_objects = gfs2_qd_shrink_scan,
187 .seeks = DEFAULT_SEEKS,
188 .flags = SHRINKER_NUMA_AWARE,
189};
190
191
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800192static u64 qd2index(struct gfs2_quota_data *qd)
193{
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800194 struct kqid qid = qd->qd_id;
195 return (2 * (u64)from_kqid(&init_user_ns, qid)) +
Bob Peterson37f71572013-05-10 11:59:18 -0400196 ((qid.type == USRQUOTA) ? 0 : 1);
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800197}
198
Steven Whitehousecd915492006-09-04 12:49:07 -0400199static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200{
Steven Whitehousecd915492006-09-04 12:49:07 -0400201 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800203 offset = qd2index(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 offset *= sizeof(struct gfs2_quota);
205
206 return offset;
207}
208
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000209static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210{
211 struct gfs2_quota_data *qd;
212 int error;
213
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000214 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 if (!qd)
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000216 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000218 qd->qd_sbd = sdp;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400219 qd->qd_lockref.count = 1;
220 spin_lock_init(&qd->qd_lockref.lock);
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800221 qd->qd_id = qid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 qd->qd_slot = -1;
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000223 INIT_LIST_HEAD(&qd->qd_lru);
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000224 qd->qd_hash = hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800226 error = gfs2_glock_get(sdp, qd2index(qd),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227 &gfs2_quota_glops, CREATE, &qd->qd_gl);
228 if (error)
229 goto fail;
230
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000231 return qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232
Steven Whitehousea91ea692006-09-04 12:04:26 -0400233fail:
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000234 kmem_cache_free(gfs2_quotad_cachep, qd);
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000235 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236}
237
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000238static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
239 const struct gfs2_sbd *sdp,
240 struct kqid qid)
241{
242 struct gfs2_quota_data *qd;
243 struct hlist_bl_node *h;
244
245 hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
246 if (!qid_eq(qd->qd_id, qid))
247 continue;
248 if (qd->qd_sbd != sdp)
249 continue;
250 if (lockref_get_not_dead(&qd->qd_lockref)) {
251 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
252 return qd;
253 }
254 }
255
256 return NULL;
257}
258
259
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800260static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 struct gfs2_quota_data **qdp)
262{
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000263 struct gfs2_quota_data *qd, *new_qd;
264 unsigned int hash = gfs2_qd_hash(sdp, qid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000266 rcu_read_lock();
267 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
268 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000270 if (qd)
271 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000273 new_qd = qd_alloc(hash, sdp, qid);
274 if (!new_qd)
275 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000277 spin_lock(&qd_lock);
278 spin_lock_bucket(hash);
279 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
280 if (qd == NULL) {
281 *qdp = new_qd;
282 list_add(&new_qd->qd_list, &sdp->sd_quota_list);
283 hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
284 atomic_inc(&sdp->sd_quota_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 }
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000286 spin_unlock_bucket(hash);
287 spin_unlock(&qd_lock);
288
289 if (qd) {
290 gfs2_glock_put(new_qd->qd_gl);
291 kmem_cache_free(gfs2_quotad_cachep, new_qd);
292 }
293
294 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295}
296
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000297
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298static void qd_hold(struct gfs2_quota_data *qd)
299{
300 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400301 gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
302 lockref_get(&qd->qd_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303}
304
305static void qd_put(struct gfs2_quota_data *qd)
306{
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000307 if (lockref_put_or_lock(&qd->qd_lockref))
308 return;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400309
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000310 qd->qd_lockref.count = 0;
311 list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
312 spin_unlock(&qd->qd_lockref.lock);
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400313
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314}
315
316static int slot_get(struct gfs2_quota_data *qd)
317{
318 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
319 unsigned int c, o = 0, b;
320 unsigned char byte = 0;
321
Steven Whitehouse7d808232013-11-01 14:52:08 -0400322 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323
324 if (qd->qd_slot_count++) {
Steven Whitehouse7d808232013-11-01 14:52:08 -0400325 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326 return 0;
327 }
328
329 for (c = 0; c < sdp->sd_quota_chunks; c++)
330 for (o = 0; o < PAGE_SIZE; o++) {
331 byte = sdp->sd_quota_bitmap[c][o];
332 if (byte != 0xFF)
333 goto found;
334 }
335
336 goto fail;
337
Steven Whitehousea91ea692006-09-04 12:04:26 -0400338found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 for (b = 0; b < 8; b++)
340 if (!(byte & (1 << b)))
341 break;
342 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
343
344 if (qd->qd_slot >= sdp->sd_quota_slots)
345 goto fail;
346
347 sdp->sd_quota_bitmap[c][o] |= 1 << b;
348
Steven Whitehouse7d808232013-11-01 14:52:08 -0400349 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350
351 return 0;
352
Steven Whitehousea91ea692006-09-04 12:04:26 -0400353fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 qd->qd_slot_count--;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400355 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 return -ENOSPC;
357}
358
359static void slot_hold(struct gfs2_quota_data *qd)
360{
361 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
362
Steven Whitehouse7d808232013-11-01 14:52:08 -0400363 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000364 gfs2_assert(sdp, qd->qd_slot_count);
365 qd->qd_slot_count++;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400366 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367}
368
Steven Whitehouse26e43a12013-10-02 14:47:02 +0100369static void gfs2_icbit_munge(struct gfs2_sbd *sdp, unsigned char **bitmap,
370 unsigned int bit, int new_value)
371{
372 unsigned int c, o, b = bit;
373 int old_value;
374
375 c = b / (8 * PAGE_SIZE);
376 b %= 8 * PAGE_SIZE;
377 o = b / 8;
378 b %= 8;
379
380 old_value = (bitmap[c][o] & (1 << b));
381 gfs2_assert_withdraw(sdp, !old_value != !new_value);
382
383 if (new_value)
384 bitmap[c][o] |= 1 << b;
385 else
386 bitmap[c][o] &= ~(1 << b);
387}
388
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389static void slot_put(struct gfs2_quota_data *qd)
390{
391 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
392
Steven Whitehouse7d808232013-11-01 14:52:08 -0400393 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 gfs2_assert(sdp, qd->qd_slot_count);
395 if (!--qd->qd_slot_count) {
396 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
397 qd->qd_slot = -1;
398 }
Steven Whitehouse7d808232013-11-01 14:52:08 -0400399 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400}
401
402static int bh_get(struct gfs2_quota_data *qd)
403{
404 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400405 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 struct buffer_head *bh;
408 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400409 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410
Steven Whitehousef55ab262006-02-21 12:51:39 +0000411 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
413 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000414 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 return 0;
416 }
417
418 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600419 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420
Steven Whitehouse23591252006-10-13 17:25:45 -0400421 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600422 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 if (error)
424 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400425 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426 if (error)
427 goto fail;
428 error = -EIO;
429 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
430 goto fail_brelse;
431
432 qd->qd_bh = bh;
433 qd->qd_bh_qc = (struct gfs2_quota_change *)
434 (bh->b_data + sizeof(struct gfs2_meta_header) +
435 offset * sizeof(struct gfs2_quota_change));
436
Josef Whiter2e95b662007-02-20 00:03:29 -0500437 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438
439 return 0;
440
Steven Whitehousea91ea692006-09-04 12:04:26 -0400441fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400443fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000445 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 return error;
447}
448
449static void bh_put(struct gfs2_quota_data *qd)
450{
451 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
452
Steven Whitehousef55ab262006-02-21 12:51:39 +0000453 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 gfs2_assert(sdp, qd->qd_bh_count);
455 if (!--qd->qd_bh_count) {
456 brelse(qd->qd_bh);
457 qd->qd_bh = NULL;
458 qd->qd_bh_qc = NULL;
459 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000460 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461}
462
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100463static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
464 u64 *sync_gen)
465{
466 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
467 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
468 (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
469 return 0;
470
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000471 if (!lockref_get_not_dead(&qd->qd_lockref))
472 return 0;
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100473
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000474 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100475 set_bit(QDF_LOCKED, &qd->qd_flags);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100476 qd->qd_change_sync = qd->qd_change;
477 gfs2_assert_warn(sdp, qd->qd_slot_count);
478 qd->qd_slot_count++;
479 return 1;
480}
481
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
483{
484 struct gfs2_quota_data *qd = NULL;
485 int error;
486 int found = 0;
487
488 *qdp = NULL;
489
490 if (sdp->sd_vfs->s_flags & MS_RDONLY)
491 return 0;
492
Steven Whitehouse7d808232013-11-01 14:52:08 -0400493 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494
495 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100496 found = qd_check_sync(sdp, qd, &sdp->sd_quota_sync_gen);
497 if (found)
498 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 }
500
501 if (!found)
502 qd = NULL;
503
Steven Whitehouse7d808232013-11-01 14:52:08 -0400504 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505
506 if (qd) {
507 gfs2_assert_warn(sdp, qd->qd_change_sync);
508 error = bh_get(qd);
509 if (error) {
510 clear_bit(QDF_LOCKED, &qd->qd_flags);
511 slot_put(qd);
512 qd_put(qd);
513 return error;
514 }
515 }
516
517 *qdp = qd;
518
519 return 0;
520}
521
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522static void qd_unlock(struct gfs2_quota_data *qd)
523{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500524 gfs2_assert_warn(qd->qd_gl->gl_sbd,
525 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 clear_bit(QDF_LOCKED, &qd->qd_flags);
527 bh_put(qd);
528 slot_put(qd);
529 qd_put(qd);
530}
531
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800532static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 struct gfs2_quota_data **qdp)
534{
535 int error;
536
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800537 error = qd_get(sdp, qid, qdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 if (error)
539 return error;
540
541 error = slot_get(*qdp);
542 if (error)
543 goto fail;
544
545 error = bh_get(*qdp);
546 if (error)
547 goto fail_slot;
548
549 return 0;
550
Steven Whitehousea91ea692006-09-04 12:04:26 -0400551fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400553fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 qd_put(*qdp);
555 return error;
556}
557
558static void qdsb_put(struct gfs2_quota_data *qd)
559{
560 bh_put(qd);
561 slot_put(qd);
562 qd_put(qd);
563}
564
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -0800565int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400567 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson5407e242012-05-18 09:28:23 -0400568 struct gfs2_quota_data **qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 int error;
570
Andrew Priceaaaf68c2012-10-12 16:45:08 +0100571 if (ip->i_res == NULL) {
572 error = gfs2_rs_alloc(ip);
573 if (error)
574 return error;
575 }
Bob Peterson5407e242012-05-18 09:28:23 -0400576
577 qd = ip->i_res->rs_qa_qd;
578
579 if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
581 return -EIO;
582
583 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
584 return 0;
585
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800586 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 if (error)
588 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400589 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 qd++;
591
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800592 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 if (error)
594 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400595 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 qd++;
597
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800598 if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
599 !uid_eq(uid, ip->i_inode.i_uid)) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800600 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 if (error)
602 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400603 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 qd++;
605 }
606
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800607 if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
608 !gid_eq(gid, ip->i_inode.i_gid)) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800609 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 if (error)
611 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400612 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 qd++;
614 }
615
Steven Whitehousea91ea692006-09-04 12:04:26 -0400616out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 if (error)
618 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 return error;
620}
621
622void gfs2_quota_unhold(struct gfs2_inode *ip)
623{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400624 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625 unsigned int x;
626
Bob Peterson5407e242012-05-18 09:28:23 -0400627 if (ip->i_res == NULL)
628 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
630
Bob Peterson5407e242012-05-18 09:28:23 -0400631 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
632 qdsb_put(ip->i_res->rs_qa_qd[x]);
633 ip->i_res->rs_qa_qd[x] = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 }
Bob Peterson5407e242012-05-18 09:28:23 -0400635 ip->i_res->rs_qa_qd_num = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636}
637
638static int sort_qd(const void *a, const void *b)
639{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400640 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
641 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800643 if (qid_lt(qd_a->qd_id, qd_b->qd_id))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400644 return -1;
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800645 if (qid_lt(qd_b->qd_id, qd_a->qd_id))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400646 return 1;
Steven Whitehouse48fac172006-09-05 15:17:12 -0400647 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648}
649
Steven Whitehousecd915492006-09-04 12:49:07 -0400650static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651{
652 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400653 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400655 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656
Steven Whitehousef55ab262006-02-21 12:51:39 +0000657 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000658 gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659
660 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
661 qc->qc_change = 0;
662 qc->qc_flags = 0;
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800663 if (qd->qd_id.type == USRQUOTA)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800665 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 }
667
Al Virob44b84d2006-10-14 10:46:30 -0400668 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 qc->qc_change = cpu_to_be64(x);
670
Steven Whitehouse7d808232013-11-01 14:52:08 -0400671 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 qd->qd_change = x;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400673 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674
675 if (!x) {
676 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
677 clear_bit(QDF_CHANGE, &qd->qd_flags);
678 qc->qc_flags = 0;
679 qc->qc_id = 0;
680 slot_put(qd);
681 qd_put(qd);
682 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
683 qd_hold(qd);
684 slot_hold(qd);
685 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400686
Steven Whitehousef55ab262006-02-21 12:51:39 +0000687 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688}
689
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000690/**
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100691 * gfs2_adjust_quota - adjust record of current block usage
692 * @ip: The quota inode
693 * @loc: Offset of the entry in the quota file
Steven Whitehousee285c102009-09-23 13:50:49 +0100694 * @change: The amount of usage change to record
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100695 * @qd: The quota data
Steven Whitehousee285c102009-09-23 13:50:49 +0100696 * @fdq: The updated limits to record
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000697 *
698 * This function was mostly borrowed from gfs2_block_truncate_page which was
699 * in turn mostly borrowed from ext3
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100700 *
701 * Returns: 0 or -ve on error
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000702 */
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100703
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000704static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousee285c102009-09-23 13:50:49 +0100705 s64 change, struct gfs2_quota_data *qd,
706 struct fs_disk_quota *fdq)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000707{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400708 struct inode *inode = &ip->i_inode;
Abhijith Das14870b42010-11-18 11:24:24 -0500709 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000710 struct address_space *mapping = inode->i_mapping;
711 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500712 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000713 unsigned blocksize, iblock, pos;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100714 struct buffer_head *bh;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000715 struct page *page;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400716 void *kaddr, *ptr;
Al Viro951b4bd2013-06-02 19:53:40 -0400717 struct gfs2_quota q;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400718 int err, nbytes;
Steven Whitehousee285c102009-09-23 13:50:49 +0100719 u64 size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000720
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100721 if (gfs2_is_stuffed(ip)) {
722 err = gfs2_unstuff_dinode(ip, NULL);
723 if (err)
724 return err;
725 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400726
727 memset(&q, 0, sizeof(struct gfs2_quota));
Andrew Price43066292012-04-16 16:40:55 +0100728 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
Abhijith Das7e619bc2010-05-07 17:50:18 -0400729 if (err < 0)
730 return err;
731
732 err = -EIO;
Al Viro951b4bd2013-06-02 19:53:40 -0400733 be64_add_cpu(&q.qu_value, change);
734 qd->qd_qb.qb_value = q.qu_value;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400735 if (fdq) {
736 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
Al Viro951b4bd2013-06-02 19:53:40 -0400737 q.qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
738 qd->qd_qb.qb_warn = q.qu_warn;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400739 }
740 if (fdq->d_fieldmask & FS_DQ_BHARD) {
Al Viro951b4bd2013-06-02 19:53:40 -0400741 q.qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
742 qd->qd_qb.qb_limit = q.qu_limit;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400743 }
Abhijith Das802ec9b2010-11-18 11:26:46 -0500744 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
Al Viro951b4bd2013-06-02 19:53:40 -0400745 q.qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
746 qd->qd_qb.qb_value = q.qu_value;
Abhijith Das802ec9b2010-11-18 11:26:46 -0500747 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400748 }
749
750 /* Write the quota into the quota file on disk */
Al Viro951b4bd2013-06-02 19:53:40 -0400751 ptr = &q;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400752 nbytes = sizeof(struct gfs2_quota);
753get_a_page:
Bob Peterson220cca22012-03-19 15:25:50 -0400754 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000755 if (!page)
756 return -ENOMEM;
757
758 blocksize = inode->i_sb->s_blocksize;
759 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
760
761 if (!page_has_buffers(page))
762 create_empty_buffers(page, blocksize, 0);
763
764 bh = page_buffers(page);
765 pos = blocksize;
766 while (offset >= pos) {
767 bh = bh->b_this_page;
768 iblock++;
769 pos += blocksize;
770 }
771
772 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600773 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000774 if (!buffer_mapped(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400775 goto unlock_out;
776 /* If it's a newly allocated disk block for quota, zero it */
Abhijith Das8b421602010-07-04 01:33:24 -0400777 if (buffer_new(bh))
778 zero_user(page, pos - blocksize, bh->b_size);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000779 }
780
781 if (PageUptodate(page))
782 set_buffer_uptodate(bh);
783
784 if (!buffer_uptodate(bh)) {
Steven Whitehouse20ed0532011-10-31 09:52:02 +0000785 ll_rw_block(READ | REQ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000786 wait_on_buffer(bh);
787 if (!buffer_uptodate(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400788 goto unlock_out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000789 }
790
Bob Peterson37f71572013-05-10 11:59:18 -0400791 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000792
Cong Wangd9349282011-11-25 23:14:30 +0800793 kaddr = kmap_atomic(page);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400794 if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
795 nbytes = PAGE_CACHE_SIZE - offset;
796 memcpy(kaddr + offset, ptr, nbytes);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000797 flush_dcache_page(page);
Cong Wangd9349282011-11-25 23:14:30 +0800798 kunmap_atomic(kaddr);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400799 unlock_page(page);
800 page_cache_release(page);
Steven Whitehousee285c102009-09-23 13:50:49 +0100801
Abhijith Das7e619bc2010-05-07 17:50:18 -0400802 /* If quota straddles page boundary, we need to update the rest of the
803 * quota at the beginning of the next page */
Abhijith Das8b421602010-07-04 01:33:24 -0400804 if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
Abhijith Das7e619bc2010-05-07 17:50:18 -0400805 ptr = ptr + nbytes;
806 nbytes = sizeof(struct gfs2_quota) - nbytes;
807 offset = 0;
808 index++;
809 goto get_a_page;
810 }
811
Steven Whitehousee285c102009-09-23 13:50:49 +0100812 size = loc + sizeof(struct gfs2_quota);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100813 if (size > inode->i_size)
Steven Whitehousee285c102009-09-23 13:50:49 +0100814 i_size_write(inode, size);
Steven Whitehousee285c102009-09-23 13:50:49 +0100815 inode->i_mtime = inode->i_atime = CURRENT_TIME;
Steven Whitehousee285c102009-09-23 13:50:49 +0100816 mark_inode_dirty(inode);
Bob Peterson500242a2012-05-15 14:51:54 -0400817 return 0;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100818
Abhijith Das7e619bc2010-05-07 17:50:18 -0400819unlock_out:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000820 unlock_page(page);
821 page_cache_release(page);
822 return err;
823}
824
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
826{
827 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400828 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100829 struct gfs2_alloc_parms ap = { .aflags = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 unsigned int data_blocks, ind_blocks;
831 struct gfs2_holder *ghs, i_gh;
832 unsigned int qx, x;
833 struct gfs2_quota_data *qd;
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100834 unsigned reserved;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000835 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600836 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837 int error;
838
Bob Peterson0a305e42012-06-06 11:17:59 +0100839 error = gfs2_rs_alloc(ip);
840 if (error)
841 return error;
842
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
844 &data_blocks, &ind_blocks);
845
Josef Bacik16c5f062008-04-09 09:33:41 -0400846 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847 if (!ghs)
848 return -ENOMEM;
849
850 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
Jan Kara56aa72d2012-09-05 16:55:11 -0400851 mutex_lock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 for (qx = 0; qx < num_qd; qx++) {
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100853 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 GL_NOCACHE, &ghs[qx]);
855 if (error)
856 goto out;
857 }
858
859 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
860 if (error)
861 goto out;
862
863 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 offset = qd2offset(qda[x]);
Bob Peterson461cb412010-06-24 19:21:20 -0400865 if (gfs2_write_alloc_required(ip, offset,
866 sizeof(struct gfs2_quota)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 nalloc++;
868 }
869
Abhijith Das20b95bf2008-03-06 17:43:52 -0600870 /*
871 * 1 blk for unstuffing inode if stuffed. We add this extra
872 * block to the reservation unconditionally. If the inode
873 * doesn't need unstuffing, the block will be released to the
874 * rgrp since it won't be allocated during the transaction
875 */
Abhijith Das7e619bc2010-05-07 17:50:18 -0400876 /* +3 in the end for unstuffing block, inode size update block
877 * and another block in case quota straddles page boundary and
878 * two blocks need to be updated instead of 1 */
879 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600880
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100881 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100882 ap.target = reserved;
883 error = gfs2_inplace_reserve(ip, &ap);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600884 if (error)
885 goto out_alloc;
886
887 if (nalloc)
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100888 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600889
890 error = gfs2_trans_begin(sdp, blocks, 0);
891 if (error)
892 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893
894 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 qd = qda[x];
896 offset = qd2offset(qd);
Steven Whitehousee285c102009-09-23 13:50:49 +0100897 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000898 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 goto out_end_trans;
900
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 do_qc(qd, -qd->qd_change_sync);
Abhijith Das662e3a52011-03-08 10:40:42 -0500902 set_bit(QDF_REFRESH, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 }
904
905 error = 0;
906
Steven Whitehousea91ea692006-09-04 12:04:26 -0400907out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400909out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600910 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400911out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400913out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 while (qx--)
915 gfs2_glock_dq_uninit(&ghs[qx]);
Steven Whitehousee285c102009-09-23 13:50:49 +0100916 mutex_unlock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400918 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 return error;
920}
921
Steven Whitehousee285c102009-09-23 13:50:49 +0100922static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
923{
924 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
925 struct gfs2_quota q;
926 struct gfs2_quota_lvb *qlvb;
927 loff_t pos;
928 int error;
929
930 memset(&q, 0, sizeof(struct gfs2_quota));
931 pos = qd2offset(qd);
Andrew Price43066292012-04-16 16:40:55 +0100932 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
Steven Whitehousee285c102009-09-23 13:50:49 +0100933 if (error < 0)
934 return error;
935
David Teigland4e2f8842012-11-14 13:47:37 -0500936 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehousee285c102009-09-23 13:50:49 +0100937 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
938 qlvb->__pad = 0;
939 qlvb->qb_limit = q.qu_limit;
940 qlvb->qb_warn = q.qu_warn;
941 qlvb->qb_value = q.qu_value;
942 qd->qd_qb = *qlvb;
943
944 return 0;
945}
946
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
948 struct gfs2_holder *q_gh)
949{
950 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400951 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 struct gfs2_holder i_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 int error;
954
Steven Whitehousea91ea692006-09-04 12:04:26 -0400955restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
957 if (error)
958 return error;
959
David Teigland4e2f8842012-11-14 13:47:37 -0500960 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400962 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 gfs2_glock_dq_uninit(q_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100964 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
965 GL_NOCACHE, q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 if (error)
967 return error;
968
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400969 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 if (error)
971 goto fail;
972
Steven Whitehousee285c102009-09-23 13:50:49 +0100973 error = update_qd(sdp, qd);
974 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 goto fail_gunlock;
Steven Whitehousee285c102009-09-23 13:50:49 +0100976
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100978 gfs2_glock_dq_uninit(q_gh);
979 force_refresh = 0;
980 goto restart;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 }
982
983 return 0;
984
Steven Whitehousea91ea692006-09-04 12:04:26 -0400985fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400987fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 return error;
990}
991
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -0800992int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400994 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Abhijith Das662e3a52011-03-08 10:40:42 -0500995 struct gfs2_quota_data *qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 unsigned int x;
997 int error = 0;
998
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100999 error = gfs2_quota_hold(ip, uid, gid);
1000 if (error)
1001 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002
1003 if (capable(CAP_SYS_RESOURCE) ||
1004 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1005 return 0;
1006
Bob Peterson5407e242012-05-18 09:28:23 -04001007 sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
1008 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009
Bob Peterson5407e242012-05-18 09:28:23 -04001010 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
Abhijith Das662e3a52011-03-08 10:40:42 -05001011 int force = NO_FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -04001012 qd = ip->i_res->rs_qa_qd[x];
Abhijith Das662e3a52011-03-08 10:40:42 -05001013 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
1014 force = FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -04001015 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 if (error)
1017 break;
1018 }
1019
1020 if (!error)
1021 set_bit(GIF_QD_LOCKED, &ip->i_flags);
1022 else {
1023 while (x--)
Bob Peterson5407e242012-05-18 09:28:23 -04001024 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 gfs2_quota_unhold(ip);
1026 }
1027
1028 return error;
1029}
1030
1031static int need_sync(struct gfs2_quota_data *qd)
1032{
1033 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
1034 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -04001035 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 unsigned int num, den;
1037 int do_sync = 1;
1038
1039 if (!qd->qd_qb.qb_limit)
1040 return 0;
1041
Steven Whitehouse7d808232013-11-01 14:52:08 -04001042 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043 value = qd->qd_change;
Steven Whitehouse7d808232013-11-01 14:52:08 -04001044 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045
1046 spin_lock(&gt->gt_spin);
1047 num = gt->gt_quota_scale_num;
1048 den = gt->gt_quota_scale_den;
1049 spin_unlock(&gt->gt_spin);
1050
1051 if (value < 0)
1052 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001053 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1054 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055 do_sync = 0;
1056 else {
1057 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +01001058 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001059 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -04001060 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 do_sync = 0;
1062 }
1063
1064 return do_sync;
1065}
1066
1067void gfs2_quota_unlock(struct gfs2_inode *ip)
1068{
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001069 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 struct gfs2_quota_data *qda[4];
1071 unsigned int count = 0;
1072 unsigned int x;
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001073 int found;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074
1075 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1076 goto out;
1077
Bob Peterson5407e242012-05-18 09:28:23 -04001078 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 struct gfs2_quota_data *qd;
1080 int sync;
1081
Bob Peterson5407e242012-05-18 09:28:23 -04001082 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083 sync = need_sync(qd);
1084
Bob Peterson5407e242012-05-18 09:28:23 -04001085 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001086 if (!sync)
1087 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088
Steven Whitehouse7d808232013-11-01 14:52:08 -04001089 spin_lock(&qd_lock);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001090 found = qd_check_sync(sdp, qd, NULL);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001091 spin_unlock(&qd_lock);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001092
1093 if (!found)
1094 continue;
1095
1096 gfs2_assert_warn(sdp, qd->qd_change_sync);
1097 if (bh_get(qd)) {
1098 clear_bit(QDF_LOCKED, &qd->qd_flags);
1099 slot_put(qd);
1100 qd_put(qd);
1101 continue;
1102 }
1103
1104 qda[count++] = qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 }
1106
1107 if (count) {
1108 do_sync(count, qda);
1109 for (x = 0; x < count; x++)
1110 qd_unlock(qda[x]);
1111 }
1112
Steven Whitehousea91ea692006-09-04 12:04:26 -04001113out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 gfs2_quota_unhold(ip);
1115}
1116
1117#define MAX_LINE 256
1118
1119static int print_message(struct gfs2_quota_data *qd, char *type)
1120{
1121 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001123 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
Steven Whitehouse02630a12006-07-03 11:20:06 -04001124 sdp->sd_fsname, type,
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001125 (qd->qd_id.type == USRQUOTA) ? "user" : "group",
1126 from_kqid(&init_user_ns, qd->qd_id));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127
1128 return 0;
1129}
1130
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001131int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001133 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001135 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 unsigned int x;
1137 int error = 0;
1138
1139 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1140 return 0;
1141
1142 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1143 return 0;
1144
Bob Peterson5407e242012-05-18 09:28:23 -04001145 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1146 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001148 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1149 qid_eq(qd->qd_id, make_kqid_gid(gid))))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150 continue;
1151
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001152 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001153 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 value += qd->qd_change;
Steven Whitehouse7d808232013-11-01 14:52:08 -04001155 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156
Steven Whitehousecd915492006-09-04 12:49:07 -04001157 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 +00001158 print_message(qd, "exceeded");
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001159 quota_send_warning(qd->qd_id,
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001160 sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1161
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 error = -EDQUOT;
1163 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001164 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001165 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001167 gfs2_tune_get(sdp,
1168 gt_quota_warn_period) * HZ)) {
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001169 quota_send_warning(qd->qd_id,
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001170 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171 error = print_message(qd, "warning");
1172 qd->qd_last_warn = jiffies;
1173 }
1174 }
1175
1176 return error;
1177}
1178
Steven Whitehousecd915492006-09-04 12:49:07 -04001179void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001180 kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182 struct gfs2_quota_data *qd;
1183 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001185 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001187 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188 return;
1189
Bob Peterson5407e242012-05-18 09:28:23 -04001190 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1191 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001192
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001193 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1194 qid_eq(qd->qd_id, make_kqid_gid(gid))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 }
1197 }
1198}
1199
Jan Karaceed1722012-07-03 16:45:28 +02001200int gfs2_quota_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201{
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001202 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 struct gfs2_quota_data **qda;
Steven Whitehousebef292a2013-10-03 18:43:20 +01001204 unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 unsigned int num_qd;
1206 unsigned int x;
1207 int error = 0;
1208
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1210 if (!qda)
1211 return -ENOMEM;
1212
Steven Whitehousee46c7722013-10-04 12:29:34 +01001213 mutex_lock(&sdp->sd_quota_sync_mutex);
1214 sdp->sd_quota_sync_gen++;
1215
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216 do {
1217 num_qd = 0;
1218
1219 for (;;) {
1220 error = qd_fish(sdp, qda + num_qd);
1221 if (error || !qda[num_qd])
1222 break;
1223 if (++num_qd == max_qd)
1224 break;
1225 }
1226
1227 if (num_qd) {
1228 if (!error)
1229 error = do_sync(num_qd, qda);
1230 if (!error)
1231 for (x = 0; x < num_qd; x++)
1232 qda[x]->qd_sync_gen =
1233 sdp->sd_quota_sync_gen;
1234
1235 for (x = 0; x < num_qd; x++)
1236 qd_unlock(qda[x]);
1237 }
1238 } while (!error && num_qd == max_qd);
1239
Steven Whitehousee46c7722013-10-04 12:29:34 +01001240 mutex_unlock(&sdp->sd_quota_sync_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241 kfree(qda);
1242
1243 return error;
1244}
1245
Eric W. Biedermaned87dab2013-01-31 19:42:40 -08001246int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247{
1248 struct gfs2_quota_data *qd;
1249 struct gfs2_holder q_gh;
1250 int error;
1251
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001252 error = qd_get(sdp, qid, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253 if (error)
1254 return error;
1255
1256 error = do_glock(qd, FORCE, &q_gh);
1257 if (!error)
1258 gfs2_glock_dq_uninit(&q_gh);
1259
1260 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261 return error;
1262}
1263
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264int gfs2_quota_init(struct gfs2_sbd *sdp)
1265{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001266 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001267 u64 size = i_size_read(sdp->sd_qc_inode);
1268 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001269 unsigned int x, slot = 0;
1270 unsigned int found = 0;
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001271 unsigned int hash;
Steven Whitehousecd915492006-09-04 12:49:07 -04001272 u64 dblock;
1273 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001274 int error;
1275
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001276 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001277 return -EIO;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001278
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001280 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281
1282 error = -ENOMEM;
1283
1284 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
Josef Bacik16c5f062008-04-09 09:33:41 -04001285 sizeof(unsigned char *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286 if (!sdp->sd_quota_bitmap)
1287 return error;
1288
1289 for (x = 0; x < sdp->sd_quota_chunks; x++) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001290 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291 if (!sdp->sd_quota_bitmap[x])
1292 goto fail;
1293 }
1294
1295 for (x = 0; x < blocks; x++) {
1296 struct buffer_head *bh;
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001297 const struct gfs2_quota_change *qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001298 unsigned int y;
1299
1300 if (!extlen) {
1301 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001302 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 if (error)
1304 goto fail;
1305 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001307 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1308 if (!bh)
1309 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1311 brelse(bh);
1312 goto fail;
1313 }
1314
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001315 qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001316 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 y++, slot++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318 struct gfs2_quota_data *qd;
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001319 s64 qc_change = be64_to_cpu(qc->qc_change);
1320 u32 qc_flags = be32_to_cpu(qc->qc_flags);
1321 enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1322 USRQUOTA : GRPQUOTA;
1323 struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1324 be32_to_cpu(qc->qc_id));
1325 qc++;
1326 if (!qc_change)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327 continue;
1328
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001329 hash = gfs2_qd_hash(sdp, qc_id);
1330 qd = qd_alloc(hash, sdp, qc_id);
1331 if (qd == NULL) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332 brelse(bh);
1333 goto fail;
1334 }
1335
1336 set_bit(QDF_CHANGE, &qd->qd_flags);
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001337 qd->qd_change = qc_change;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 qd->qd_slot = slot;
1339 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340
Steven Whitehouse7d808232013-11-01 14:52:08 -04001341 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001342 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1343 list_add(&qd->qd_list, &sdp->sd_quota_list);
1344 atomic_inc(&sdp->sd_quota_count);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001345 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001347 spin_lock_bucket(hash);
1348 hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
1349 spin_unlock_bucket(hash);
1350
David Teiglandb3b94fa2006-01-16 16:50:04 +00001351 found++;
1352 }
1353
1354 brelse(bh);
1355 dblock++;
1356 extlen--;
1357 }
1358
1359 if (found)
1360 fs_info(sdp, "found %u quota changes\n", found);
1361
1362 return 0;
1363
Steven Whitehousea91ea692006-09-04 12:04:26 -04001364fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365 gfs2_quota_cleanup(sdp);
1366 return error;
1367}
1368
David Teiglandb3b94fa2006-01-16 16:50:04 +00001369void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1370{
1371 struct list_head *head = &sdp->sd_quota_list;
1372 struct gfs2_quota_data *qd;
1373 unsigned int x;
1374
Steven Whitehouse7d808232013-11-01 14:52:08 -04001375 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 while (!list_empty(head)) {
1377 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1378
Abhijith Das0a7ab792009-01-07 16:03:37 -06001379 list_del(&qd->qd_list);
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001380
Abhijith Das0a7ab792009-01-07 16:03:37 -06001381 /* Also remove if this qd exists in the reclaim list */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +00001382 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001383 atomic_dec(&sdp->sd_quota_count);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001384 spin_unlock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001385
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001386 spin_lock_bucket(qd->qd_hash);
1387 hlist_bl_del_rcu(&qd->qd_hlist);
1388 spin_unlock_bucket(qd->qd_hash);
1389
Steven Whitehouse8ad151c2013-12-12 11:34:09 +00001390 gfs2_assert_warn(sdp, !qd->qd_change);
1391 gfs2_assert_warn(sdp, !qd->qd_slot_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1393
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001394 gfs2_glock_put(qd->qd_gl);
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001395 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396
Steven Whitehouse7d808232013-11-01 14:52:08 -04001397 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 }
Steven Whitehouse7d808232013-11-01 14:52:08 -04001399 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400
1401 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1402
1403 if (sdp->sd_quota_bitmap) {
1404 for (x = 0; x < sdp->sd_quota_chunks; x++)
1405 kfree(sdp->sd_quota_bitmap[x]);
1406 kfree(sdp->sd_quota_bitmap);
1407 }
1408}
1409
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001410static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1411{
1412 if (error == 0 || error == -EROFS)
1413 return;
1414 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1415 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1416}
1417
1418static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001419 int (*fxn)(struct super_block *sb, int type),
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001420 unsigned long t, unsigned long *timeo,
1421 unsigned int *new_timeo)
1422{
1423 if (t >= *timeo) {
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001424 int error = fxn(sdp->sd_vfs, 0);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001425 quotad_error(sdp, msg, error);
1426 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1427 } else {
1428 *timeo -= t;
1429 }
1430}
1431
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001432static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1433{
1434 struct gfs2_inode *ip;
1435
1436 while(1) {
1437 ip = NULL;
1438 spin_lock(&sdp->sd_trunc_lock);
1439 if (!list_empty(&sdp->sd_trunc_list)) {
1440 ip = list_entry(sdp->sd_trunc_list.next,
1441 struct gfs2_inode, i_trunc_list);
1442 list_del_init(&ip->i_trunc_list);
1443 }
1444 spin_unlock(&sdp->sd_trunc_lock);
1445 if (ip == NULL)
1446 return;
1447 gfs2_glock_finish_truncate(ip);
1448 }
1449}
1450
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001451void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1452 if (!sdp->sd_statfs_force_sync) {
1453 sdp->sd_statfs_force_sync = 1;
1454 wake_up(&sdp->sd_quota_wait);
1455 }
1456}
1457
1458
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001459/**
1460 * gfs2_quotad - Write cached quota changes into the quota file
1461 * @sdp: Pointer to GFS2 superblock
1462 *
1463 */
1464
1465int gfs2_quotad(void *data)
1466{
1467 struct gfs2_sbd *sdp = data;
1468 struct gfs2_tune *tune = &sdp->sd_tune;
1469 unsigned long statfs_timeo = 0;
1470 unsigned long quotad_timeo = 0;
1471 unsigned long t = 0;
1472 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001473 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001474
1475 while (!kthread_should_stop()) {
1476
1477 /* Update the master statfs file */
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001478 if (sdp->sd_statfs_force_sync) {
1479 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1480 quotad_error(sdp, "statfs", error);
1481 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1482 }
1483 else
1484 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1485 &statfs_timeo,
1486 &tune->gt_statfs_quantum);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001487
1488 /* Update quota file */
Steven Whitehouseedd2e9a2013-06-03 11:12:59 +01001489 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001490 &quotad_timeo, &tune->gt_quota_quantum);
1491
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001492 /* Check for & recover partially truncated inodes */
1493 quotad_check_trunc_list(sdp);
1494
Tejun Heoa0acae02011-11-21 12:32:22 -08001495 try_to_freeze();
1496
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001497 t = min(quotad_timeo, statfs_timeo);
1498
Steven Whitehouse7fa5d202009-03-31 15:49:08 +01001499 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001500 spin_lock(&sdp->sd_trunc_lock);
1501 empty = list_empty(&sdp->sd_trunc_list);
1502 spin_unlock(&sdp->sd_trunc_lock);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001503 if (empty && !sdp->sd_statfs_force_sync)
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001504 t -= schedule_timeout(t);
1505 else
1506 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001507 finish_wait(&sdp->sd_quota_wait, &wait);
1508 }
1509
1510 return 0;
1511}
1512
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001513static int gfs2_quota_get_xstate(struct super_block *sb,
1514 struct fs_quota_stat *fqs)
1515{
1516 struct gfs2_sbd *sdp = sb->s_fs_info;
1517
1518 memset(fqs, 0, sizeof(struct fs_quota_stat));
1519 fqs->qs_version = FS_QSTAT_VERSION;
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001520
1521 switch (sdp->sd_args.ar_quota) {
1522 case GFS2_QUOTA_ON:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001523 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001524 /*FALLTHRU*/
1525 case GFS2_QUOTA_ACCOUNT:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001526 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001527 break;
1528 case GFS2_QUOTA_OFF:
1529 break;
1530 }
1531
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001532 if (sdp->sd_quota_inode) {
1533 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1534 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1535 }
1536 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1537 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +00001538 fqs->qs_incoredqs = list_lru_count(&gfs2_qd_lru);
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001539 return 0;
1540}
1541
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001542static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001543 struct fs_disk_quota *fdq)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001544{
1545 struct gfs2_sbd *sdp = sb->s_fs_info;
1546 struct gfs2_quota_lvb *qlvb;
1547 struct gfs2_quota_data *qd;
1548 struct gfs2_holder q_gh;
1549 int error;
1550
1551 memset(fdq, 0, sizeof(struct fs_disk_quota));
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 Whitehouse113d6b32009-09-28 11:52:16 +01001558 return -EINVAL;
1559
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001560 error = qd_get(sdp, qid, &qd);
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001561 if (error)
1562 return error;
1563 error = do_glock(qd, FORCE, &q_gh);
1564 if (error)
1565 goto out;
1566
David Teigland4e2f8842012-11-14 13:47:37 -05001567 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001568 fdq->d_version = FS_DQUOT_VERSION;
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001569 fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
Eric W. Biederman558e8522013-01-31 18:15:33 -08001570 fdq->d_id = from_kqid_munged(current_user_ns(), qid);
Abhijith Das14870b42010-11-18 11:24:24 -05001571 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1572 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1573 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001574
1575 gfs2_glock_dq_uninit(&q_gh);
1576out:
1577 qd_put(qd);
1578 return error;
1579}
1580
Steven Whitehousee285c102009-09-23 13:50:49 +01001581/* GFS2 only supports a subset of the XFS fields */
Abhijith Das802ec9b2010-11-18 11:26:46 -05001582#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
Steven Whitehousee285c102009-09-23 13:50:49 +01001583
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001584static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001585 struct fs_disk_quota *fdq)
Steven Whitehousee285c102009-09-23 13:50:49 +01001586{
1587 struct gfs2_sbd *sdp = sb->s_fs_info;
1588 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1589 struct gfs2_quota_data *qd;
1590 struct gfs2_holder q_gh, i_gh;
1591 unsigned int data_blocks, ind_blocks;
1592 unsigned int blocks = 0;
1593 int alloc_required;
Steven Whitehousee285c102009-09-23 13:50:49 +01001594 loff_t offset;
1595 int error;
1596
1597 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1598 return -ESRCH; /* Crazy XFS error code */
1599
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001600 if ((qid.type != USRQUOTA) &&
1601 (qid.type != GRPQUOTA))
Steven Whitehousee285c102009-09-23 13:50:49 +01001602 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001603
1604 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1605 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001606
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001607 error = qd_get(sdp, qid, &qd);
Steven Whitehousee285c102009-09-23 13:50:49 +01001608 if (error)
1609 return error;
1610
Bob Peterson0a305e42012-06-06 11:17:59 +01001611 error = gfs2_rs_alloc(ip);
1612 if (error)
1613 goto out_put;
1614
Steven Whitehousee285c102009-09-23 13:50:49 +01001615 mutex_lock(&ip->i_inode.i_mutex);
1616 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1617 if (error)
Bob Peterson0a305e42012-06-06 11:17:59 +01001618 goto out_unlockput;
Steven Whitehousee285c102009-09-23 13:50:49 +01001619 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1620 if (error)
1621 goto out_q;
1622
1623 /* Check for existing entry, if none then alloc new blocks */
1624 error = update_qd(sdp, qd);
1625 if (error)
1626 goto out_i;
1627
1628 /* If nothing has changed, this is a no-op */
1629 if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001630 ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001631 fdq->d_fieldmask ^= FS_DQ_BSOFT;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001632
Steven Whitehousee285c102009-09-23 13:50:49 +01001633 if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001634 ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001635 fdq->d_fieldmask ^= FS_DQ_BHARD;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001636
1637 if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1638 ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1639 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1640
Steven Whitehousee285c102009-09-23 13:50:49 +01001641 if (fdq->d_fieldmask == 0)
1642 goto out_i;
1643
1644 offset = qd2offset(qd);
Bob Peterson461cb412010-06-24 19:21:20 -04001645 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
Abhijith Dase79a46a2011-02-07 11:22:41 -05001646 if (gfs2_is_stuffed(ip))
1647 alloc_required = 1;
Steven Whitehousee285c102009-09-23 13:50:49 +01001648 if (alloc_required) {
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001649 struct gfs2_alloc_parms ap = { .aflags = 0, };
Steven Whitehousee285c102009-09-23 13:50:49 +01001650 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1651 &data_blocks, &ind_blocks);
Bob Peterson564e12b2011-11-21 13:36:17 -05001652 blocks = 1 + data_blocks + ind_blocks;
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001653 ap.target = blocks;
1654 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousee285c102009-09-23 13:50:49 +01001655 if (error)
Bob Peterson564e12b2011-11-21 13:36:17 -05001656 goto out_i;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001657 blocks += gfs2_rg_blocks(ip, blocks);
Steven Whitehousee285c102009-09-23 13:50:49 +01001658 }
1659
Abhijith Dase79a46a2011-02-07 11:22:41 -05001660 /* Some quotas span block boundaries and can update two blocks,
1661 adding an extra block to the transaction to handle such quotas */
1662 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001663 if (error)
1664 goto out_release;
1665
1666 /* Apply changes */
1667 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1668
1669 gfs2_trans_end(sdp);
1670out_release:
Bob Peterson564e12b2011-11-21 13:36:17 -05001671 if (alloc_required)
Steven Whitehousee285c102009-09-23 13:50:49 +01001672 gfs2_inplace_release(ip);
Steven Whitehousee285c102009-09-23 13:50:49 +01001673out_i:
1674 gfs2_glock_dq_uninit(&i_gh);
1675out_q:
1676 gfs2_glock_dq_uninit(&q_gh);
Bob Peterson0a305e42012-06-06 11:17:59 +01001677out_unlockput:
Steven Whitehousee285c102009-09-23 13:50:49 +01001678 mutex_unlock(&ip->i_inode.i_mutex);
Bob Peterson0a305e42012-06-06 11:17:59 +01001679out_put:
Steven Whitehousee285c102009-09-23 13:50:49 +01001680 qd_put(qd);
1681 return error;
1682}
1683
Steven Whitehousecc632e72009-09-15 09:59:02 +01001684const struct quotactl_ops gfs2_quotactl_ops = {
1685 .quota_sync = gfs2_quota_sync,
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001686 .get_xstate = gfs2_quota_get_xstate,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001687 .get_dqblk = gfs2_get_dqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001688 .set_dqblk = gfs2_set_dqblk,
Steven Whitehousecc632e72009-09-15 09:59:02 +01001689};
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001690
1691void __init gfs2_quota_hash_init(void)
1692{
1693 unsigned i;
1694
1695 for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
1696 INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
1697}