blob: 8bec0e3192ddec78a0e04c3f3872de5b622105b5 [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>
Steven Whitehouse1e3d3622014-01-15 12:57:25 +000059#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000060
61#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050062#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000063#include "bmap.h"
64#include "glock.h"
65#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000066#include "log.h"
67#include "meta_io.h"
68#include "quota.h"
69#include "rgrp.h"
70#include "super.h"
71#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000072#include "inode.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050073#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000074
Steven Whitehousec754fbb2013-12-12 10:47:59 +000075#define GFS2_QD_HASH_SHIFT 12
76#define GFS2_QD_HASH_SIZE (1 << GFS2_QD_HASH_SHIFT)
77#define GFS2_QD_HASH_MASK (GFS2_QD_HASH_SIZE - 1)
78
79/* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */
Steven Whitehouse2d9e7232013-12-13 11:46:28 +000080/* -> sd_bitmap_lock */
Steven Whitehouse7d808232013-11-01 14:52:08 -040081static DEFINE_SPINLOCK(qd_lock);
Steven Whitehouse2147dbf2013-11-04 10:15:08 +000082struct list_lru gfs2_qd_lru;
Abhijith Das0a7ab792009-01-07 16:03:37 -060083
Steven Whitehousec754fbb2013-12-12 10:47:59 +000084static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
85
86static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
87 const struct kqid qid)
88{
89 unsigned int h;
90
91 h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
92 h = jhash(&qid, sizeof(struct kqid), h);
93
94 return h & GFS2_QD_HASH_MASK;
95}
96
97static inline void spin_lock_bucket(unsigned int hash)
98{
99 hlist_bl_lock(&qd_hash_table[hash]);
100}
101
102static inline void spin_unlock_bucket(unsigned int hash)
103{
104 hlist_bl_unlock(&qd_hash_table[hash]);
105}
106
107static void gfs2_qd_dealloc(struct rcu_head *rcu)
108{
109 struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
110 kmem_cache_free(gfs2_quotad_cachep, qd);
111}
112
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000113static void gfs2_qd_dispose(struct list_head *list)
Abhijith Das0a7ab792009-01-07 16:03:37 -0600114{
115 struct gfs2_quota_data *qd;
116 struct gfs2_sbd *sdp;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600117
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000118 while (!list_empty(list)) {
119 qd = list_entry(list->next, struct gfs2_quota_data, qd_lru);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600120 sdp = qd->qd_gl->gl_sbd;
121
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000122 list_del(&qd->qd_lru);
123
Abhijith Das0a7ab792009-01-07 16:03:37 -0600124 /* Free from the filesystem-specific list */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000125 spin_lock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600126 list_del(&qd->qd_list);
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000127 spin_unlock(&qd_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600128
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000129 spin_lock_bucket(qd->qd_hash);
130 hlist_bl_del_rcu(&qd->qd_hlist);
131 spin_unlock_bucket(qd->qd_hash);
132
Abhijith Das0a7ab792009-01-07 16:03:37 -0600133 gfs2_assert_warn(sdp, !qd->qd_change);
134 gfs2_assert_warn(sdp, !qd->qd_slot_count);
135 gfs2_assert_warn(sdp, !qd->qd_bh_count);
136
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000137 gfs2_glock_put(qd->qd_gl);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600138 atomic_dec(&sdp->sd_quota_count);
139
140 /* Delete it from the common reclaim list */
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000141 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600142 }
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000143}
144
145
146static enum lru_status gfs2_qd_isolate(struct list_head *item, spinlock_t *lock, void *arg)
147{
148 struct list_head *dispose = arg;
149 struct gfs2_quota_data *qd = list_entry(item, struct gfs2_quota_data, qd_lru);
150
151 if (!spin_trylock(&qd->qd_lockref.lock))
152 return LRU_SKIP;
153
154 if (qd->qd_lockref.count == 0) {
155 lockref_mark_dead(&qd->qd_lockref);
156 list_move(&qd->qd_lru, dispose);
157 }
158
159 spin_unlock(&qd->qd_lockref.lock);
160 return LRU_REMOVED;
161}
162
163static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
164 struct shrink_control *sc)
165{
166 LIST_HEAD(dispose);
167 unsigned long freed;
168
169 if (!(sc->gfp_mask & __GFP_FS))
170 return SHRINK_STOP;
171
172 freed = list_lru_walk_node(&gfs2_qd_lru, sc->nid, gfs2_qd_isolate,
173 &dispose, &sc->nr_to_scan);
174
175 gfs2_qd_dispose(&dispose);
176
Dave Chinner1ab6c492013-08-28 10:18:09 +1000177 return freed;
178}
Abhijith Das0a7ab792009-01-07 16:03:37 -0600179
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000180static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
181 struct shrink_control *sc)
Dave Chinner1ab6c492013-08-28 10:18:09 +1000182{
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000183 return vfs_pressure_ratio(list_lru_count_node(&gfs2_qd_lru, sc->nid));
Abhijith Das0a7ab792009-01-07 16:03:37 -0600184}
185
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000186struct shrinker gfs2_qd_shrinker = {
187 .count_objects = gfs2_qd_shrink_count,
188 .scan_objects = gfs2_qd_shrink_scan,
189 .seeks = DEFAULT_SEEKS,
190 .flags = SHRINKER_NUMA_AWARE,
191};
192
193
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800194static u64 qd2index(struct gfs2_quota_data *qd)
195{
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800196 struct kqid qid = qd->qd_id;
197 return (2 * (u64)from_kqid(&init_user_ns, qid)) +
Bob Peterson37f71572013-05-10 11:59:18 -0400198 ((qid.type == USRQUOTA) ? 0 : 1);
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800199}
200
Steven Whitehousecd915492006-09-04 12:49:07 -0400201static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202{
Steven Whitehousecd915492006-09-04 12:49:07 -0400203 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800205 offset = qd2index(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 offset *= sizeof(struct gfs2_quota);
207
208 return offset;
209}
210
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000211static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212{
213 struct gfs2_quota_data *qd;
214 int error;
215
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000216 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 if (!qd)
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000218 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000220 qd->qd_sbd = sdp;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400221 qd->qd_lockref.count = 1;
222 spin_lock_init(&qd->qd_lockref.lock);
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800223 qd->qd_id = qid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 qd->qd_slot = -1;
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000225 INIT_LIST_HEAD(&qd->qd_lru);
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000226 qd->qd_hash = hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800228 error = gfs2_glock_get(sdp, qd2index(qd),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 &gfs2_quota_glops, CREATE, &qd->qd_gl);
230 if (error)
231 goto fail;
232
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000233 return qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234
Steven Whitehousea91ea692006-09-04 12:04:26 -0400235fail:
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000236 kmem_cache_free(gfs2_quotad_cachep, qd);
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000237 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238}
239
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000240static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
241 const struct gfs2_sbd *sdp,
242 struct kqid qid)
243{
244 struct gfs2_quota_data *qd;
245 struct hlist_bl_node *h;
246
247 hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
248 if (!qid_eq(qd->qd_id, qid))
249 continue;
250 if (qd->qd_sbd != sdp)
251 continue;
252 if (lockref_get_not_dead(&qd->qd_lockref)) {
253 list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
254 return qd;
255 }
256 }
257
258 return NULL;
259}
260
261
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800262static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263 struct gfs2_quota_data **qdp)
264{
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000265 struct gfs2_quota_data *qd, *new_qd;
266 unsigned int hash = gfs2_qd_hash(sdp, qid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000268 rcu_read_lock();
269 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
270 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000272 if (qd)
273 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000275 new_qd = qd_alloc(hash, sdp, qid);
276 if (!new_qd)
277 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000279 spin_lock(&qd_lock);
280 spin_lock_bucket(hash);
281 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
282 if (qd == NULL) {
283 *qdp = new_qd;
284 list_add(&new_qd->qd_list, &sdp->sd_quota_list);
285 hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
286 atomic_inc(&sdp->sd_quota_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 }
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000288 spin_unlock_bucket(hash);
289 spin_unlock(&qd_lock);
290
291 if (qd) {
292 gfs2_glock_put(new_qd->qd_gl);
293 kmem_cache_free(gfs2_quotad_cachep, new_qd);
294 }
295
296 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297}
298
Steven Whitehousec754fbb2013-12-12 10:47:59 +0000299
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300static void qd_hold(struct gfs2_quota_data *qd)
301{
302 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400303 gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
304 lockref_get(&qd->qd_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305}
306
307static void qd_put(struct gfs2_quota_data *qd)
308{
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000309 if (lockref_put_or_lock(&qd->qd_lockref))
310 return;
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400311
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000312 qd->qd_lockref.count = 0;
313 list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
314 spin_unlock(&qd->qd_lockref.lock);
Steven Whitehouse9b9f0392013-11-01 14:52:06 -0400315
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316}
317
318static int slot_get(struct gfs2_quota_data *qd)
319{
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000320 struct gfs2_sbd *sdp = qd->qd_sbd;
321 unsigned int bit;
322 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000324 spin_lock(&sdp->sd_bitmap_lock);
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000325 if (qd->qd_slot_count != 0)
326 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000328 error = -ENOSPC;
329 bit = find_first_zero_bit(sdp->sd_quota_bitmap, sdp->sd_quota_slots);
330 if (bit < sdp->sd_quota_slots) {
331 set_bit(bit, sdp->sd_quota_bitmap);
332 qd->qd_slot = bit;
333out:
334 qd->qd_slot_count++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 }
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000336 spin_unlock(&sdp->sd_bitmap_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000338 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339}
340
341static void slot_hold(struct gfs2_quota_data *qd)
342{
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000343 struct gfs2_sbd *sdp = qd->qd_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000345 spin_lock(&sdp->sd_bitmap_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 gfs2_assert(sdp, qd->qd_slot_count);
347 qd->qd_slot_count++;
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000348 spin_unlock(&sdp->sd_bitmap_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349}
350
351static void slot_put(struct gfs2_quota_data *qd)
352{
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000353 struct gfs2_sbd *sdp = qd->qd_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000355 spin_lock(&sdp->sd_bitmap_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 gfs2_assert(sdp, qd->qd_slot_count);
357 if (!--qd->qd_slot_count) {
Steven Whitehouseee2411a2013-12-12 17:29:32 +0000358 BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359 qd->qd_slot = -1;
360 }
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000361 spin_unlock(&sdp->sd_bitmap_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362}
363
364static int bh_get(struct gfs2_quota_data *qd)
365{
366 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400367 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 struct buffer_head *bh;
370 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400371 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372
Steven Whitehousef55ab262006-02-21 12:51:39 +0000373 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374
375 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000376 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000377 return 0;
378 }
379
380 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600381 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382
Steven Whitehouse23591252006-10-13 17:25:45 -0400383 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600384 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 if (error)
386 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400387 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 if (error)
389 goto fail;
390 error = -EIO;
391 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
392 goto fail_brelse;
393
394 qd->qd_bh = bh;
395 qd->qd_bh_qc = (struct gfs2_quota_change *)
396 (bh->b_data + sizeof(struct gfs2_meta_header) +
397 offset * sizeof(struct gfs2_quota_change));
398
Josef Whiter2e95b662007-02-20 00:03:29 -0500399 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400
401 return 0;
402
Steven Whitehousea91ea692006-09-04 12:04:26 -0400403fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400405fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000407 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 return error;
409}
410
411static void bh_put(struct gfs2_quota_data *qd)
412{
413 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
414
Steven Whitehousef55ab262006-02-21 12:51:39 +0000415 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 gfs2_assert(sdp, qd->qd_bh_count);
417 if (!--qd->qd_bh_count) {
418 brelse(qd->qd_bh);
419 qd->qd_bh = NULL;
420 qd->qd_bh_qc = NULL;
421 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000422 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423}
424
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100425static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
426 u64 *sync_gen)
427{
428 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
429 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
430 (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
431 return 0;
432
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000433 if (!lockref_get_not_dead(&qd->qd_lockref))
434 return 0;
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100435
Steven Whitehouse2147dbf2013-11-04 10:15:08 +0000436 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100437 set_bit(QDF_LOCKED, &qd->qd_flags);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100438 qd->qd_change_sync = qd->qd_change;
Steven Whitehouse2d9e7232013-12-13 11:46:28 +0000439 slot_hold(qd);
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100440 return 1;
441}
442
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
444{
445 struct gfs2_quota_data *qd = NULL;
446 int error;
447 int found = 0;
448
449 *qdp = NULL;
450
451 if (sdp->sd_vfs->s_flags & MS_RDONLY)
452 return 0;
453
Steven Whitehouse7d808232013-11-01 14:52:08 -0400454 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
456 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
Steven Whitehouse1bf59bf2013-10-04 11:14:46 +0100457 found = qd_check_sync(sdp, qd, &sdp->sd_quota_sync_gen);
458 if (found)
459 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 }
461
462 if (!found)
463 qd = NULL;
464
Steven Whitehouse7d808232013-11-01 14:52:08 -0400465 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466
467 if (qd) {
468 gfs2_assert_warn(sdp, qd->qd_change_sync);
469 error = bh_get(qd);
470 if (error) {
471 clear_bit(QDF_LOCKED, &qd->qd_flags);
472 slot_put(qd);
473 qd_put(qd);
474 return error;
475 }
476 }
477
478 *qdp = qd;
479
480 return 0;
481}
482
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483static void qd_unlock(struct gfs2_quota_data *qd)
484{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500485 gfs2_assert_warn(qd->qd_gl->gl_sbd,
486 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487 clear_bit(QDF_LOCKED, &qd->qd_flags);
488 bh_put(qd);
489 slot_put(qd);
490 qd_put(qd);
491}
492
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800493static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 struct gfs2_quota_data **qdp)
495{
496 int error;
497
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800498 error = qd_get(sdp, qid, qdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 if (error)
500 return error;
501
502 error = slot_get(*qdp);
503 if (error)
504 goto fail;
505
506 error = bh_get(*qdp);
507 if (error)
508 goto fail_slot;
509
510 return 0;
511
Steven Whitehousea91ea692006-09-04 12:04:26 -0400512fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400514fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 qd_put(*qdp);
516 return error;
517}
518
519static void qdsb_put(struct gfs2_quota_data *qd)
520{
521 bh_put(qd);
522 slot_put(qd);
523 qd_put(qd);
524}
525
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -0800526int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400528 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson5407e242012-05-18 09:28:23 -0400529 struct gfs2_quota_data **qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 int error;
531
Andrew Priceaaaf68c2012-10-12 16:45:08 +0100532 if (ip->i_res == NULL) {
533 error = gfs2_rs_alloc(ip);
534 if (error)
535 return error;
536 }
Bob Peterson5407e242012-05-18 09:28:23 -0400537
538 qd = ip->i_res->rs_qa_qd;
539
540 if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
542 return -EIO;
543
544 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
545 return 0;
546
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800547 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_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
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800553 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 if (error)
555 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400556 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557 qd++;
558
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800559 if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
560 !uid_eq(uid, ip->i_inode.i_uid)) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800561 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 if (error)
563 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400564 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 qd++;
566 }
567
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800568 if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
569 !gid_eq(gid, ip->i_inode.i_gid)) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800570 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 if (error)
572 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400573 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 qd++;
575 }
576
Steven Whitehousea91ea692006-09-04 12:04:26 -0400577out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 if (error)
579 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 return error;
581}
582
583void gfs2_quota_unhold(struct gfs2_inode *ip)
584{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400585 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 unsigned int x;
587
Bob Peterson5407e242012-05-18 09:28:23 -0400588 if (ip->i_res == NULL)
589 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
591
Bob Peterson5407e242012-05-18 09:28:23 -0400592 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
593 qdsb_put(ip->i_res->rs_qa_qd[x]);
594 ip->i_res->rs_qa_qd[x] = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 }
Bob Peterson5407e242012-05-18 09:28:23 -0400596 ip->i_res->rs_qa_qd_num = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597}
598
599static int sort_qd(const void *a, const void *b)
600{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400601 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
602 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800604 if (qid_lt(qd_a->qd_id, qd_b->qd_id))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400605 return -1;
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800606 if (qid_lt(qd_b->qd_id, qd_a->qd_id))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400607 return 1;
Steven Whitehouse48fac172006-09-05 15:17:12 -0400608 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609}
610
Steven Whitehousecd915492006-09-04 12:49:07 -0400611static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612{
613 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400614 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400616 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617
Steven Whitehousef55ab262006-02-21 12:51:39 +0000618 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000619 gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620
621 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
622 qc->qc_change = 0;
623 qc->qc_flags = 0;
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800624 if (qd->qd_id.type == USRQUOTA)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
Eric W. Biederman05e0a602013-01-31 19:52:08 -0800626 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627 }
628
Al Virob44b84d2006-10-14 10:46:30 -0400629 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 qc->qc_change = cpu_to_be64(x);
631
Steven Whitehouse7d808232013-11-01 14:52:08 -0400632 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 qd->qd_change = x;
Steven Whitehouse7d808232013-11-01 14:52:08 -0400634 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635
636 if (!x) {
637 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
638 clear_bit(QDF_CHANGE, &qd->qd_flags);
639 qc->qc_flags = 0;
640 qc->qc_id = 0;
641 slot_put(qd);
642 qd_put(qd);
643 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
644 qd_hold(qd);
645 slot_hold(qd);
646 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400647
Steven Whitehousef55ab262006-02-21 12:51:39 +0000648 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649}
650
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000651/**
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100652 * gfs2_adjust_quota - adjust record of current block usage
653 * @ip: The quota inode
654 * @loc: Offset of the entry in the quota file
Steven Whitehousee285c102009-09-23 13:50:49 +0100655 * @change: The amount of usage change to record
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100656 * @qd: The quota data
Steven Whitehousee285c102009-09-23 13:50:49 +0100657 * @fdq: The updated limits to record
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000658 *
659 * This function was mostly borrowed from gfs2_block_truncate_page which was
660 * in turn mostly borrowed from ext3
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100661 *
662 * Returns: 0 or -ve on error
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000663 */
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100664
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000665static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousee285c102009-09-23 13:50:49 +0100666 s64 change, struct gfs2_quota_data *qd,
667 struct fs_disk_quota *fdq)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000668{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400669 struct inode *inode = &ip->i_inode;
Abhijith Das14870b42010-11-18 11:24:24 -0500670 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000671 struct address_space *mapping = inode->i_mapping;
672 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500673 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000674 unsigned blocksize, iblock, pos;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100675 struct buffer_head *bh;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000676 struct page *page;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400677 void *kaddr, *ptr;
Al Viro951b4bd2013-06-02 19:53:40 -0400678 struct gfs2_quota q;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400679 int err, nbytes;
Steven Whitehousee285c102009-09-23 13:50:49 +0100680 u64 size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000681
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100682 if (gfs2_is_stuffed(ip)) {
683 err = gfs2_unstuff_dinode(ip, NULL);
684 if (err)
685 return err;
686 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400687
688 memset(&q, 0, sizeof(struct gfs2_quota));
Andrew Price43066292012-04-16 16:40:55 +0100689 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
Abhijith Das7e619bc2010-05-07 17:50:18 -0400690 if (err < 0)
691 return err;
692
693 err = -EIO;
Al Viro951b4bd2013-06-02 19:53:40 -0400694 be64_add_cpu(&q.qu_value, change);
695 qd->qd_qb.qb_value = q.qu_value;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400696 if (fdq) {
697 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
Al Viro951b4bd2013-06-02 19:53:40 -0400698 q.qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
699 qd->qd_qb.qb_warn = q.qu_warn;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400700 }
701 if (fdq->d_fieldmask & FS_DQ_BHARD) {
Al Viro951b4bd2013-06-02 19:53:40 -0400702 q.qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
703 qd->qd_qb.qb_limit = q.qu_limit;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400704 }
Abhijith Das802ec9b2010-11-18 11:26:46 -0500705 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
Al Viro951b4bd2013-06-02 19:53:40 -0400706 q.qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
707 qd->qd_qb.qb_value = q.qu_value;
Abhijith Das802ec9b2010-11-18 11:26:46 -0500708 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400709 }
710
711 /* Write the quota into the quota file on disk */
Al Viro951b4bd2013-06-02 19:53:40 -0400712 ptr = &q;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400713 nbytes = sizeof(struct gfs2_quota);
714get_a_page:
Bob Peterson220cca22012-03-19 15:25:50 -0400715 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000716 if (!page)
717 return -ENOMEM;
718
719 blocksize = inode->i_sb->s_blocksize;
720 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
721
722 if (!page_has_buffers(page))
723 create_empty_buffers(page, blocksize, 0);
724
725 bh = page_buffers(page);
726 pos = blocksize;
727 while (offset >= pos) {
728 bh = bh->b_this_page;
729 iblock++;
730 pos += blocksize;
731 }
732
733 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600734 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000735 if (!buffer_mapped(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400736 goto unlock_out;
737 /* If it's a newly allocated disk block for quota, zero it */
Abhijith Das8b421602010-07-04 01:33:24 -0400738 if (buffer_new(bh))
739 zero_user(page, pos - blocksize, bh->b_size);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000740 }
741
742 if (PageUptodate(page))
743 set_buffer_uptodate(bh);
744
745 if (!buffer_uptodate(bh)) {
Steven Whitehouse20ed0532011-10-31 09:52:02 +0000746 ll_rw_block(READ | REQ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000747 wait_on_buffer(bh);
748 if (!buffer_uptodate(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400749 goto unlock_out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000750 }
751
Bob Peterson37f71572013-05-10 11:59:18 -0400752 gfs2_trans_add_data(ip->i_gl, bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000753
Cong Wangd9349282011-11-25 23:14:30 +0800754 kaddr = kmap_atomic(page);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400755 if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
756 nbytes = PAGE_CACHE_SIZE - offset;
757 memcpy(kaddr + offset, ptr, nbytes);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000758 flush_dcache_page(page);
Cong Wangd9349282011-11-25 23:14:30 +0800759 kunmap_atomic(kaddr);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400760 unlock_page(page);
761 page_cache_release(page);
Steven Whitehousee285c102009-09-23 13:50:49 +0100762
Abhijith Das7e619bc2010-05-07 17:50:18 -0400763 /* If quota straddles page boundary, we need to update the rest of the
764 * quota at the beginning of the next page */
Abhijith Das8b421602010-07-04 01:33:24 -0400765 if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
Abhijith Das7e619bc2010-05-07 17:50:18 -0400766 ptr = ptr + nbytes;
767 nbytes = sizeof(struct gfs2_quota) - nbytes;
768 offset = 0;
769 index++;
770 goto get_a_page;
771 }
772
Steven Whitehousee285c102009-09-23 13:50:49 +0100773 size = loc + sizeof(struct gfs2_quota);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100774 if (size > inode->i_size)
Steven Whitehousee285c102009-09-23 13:50:49 +0100775 i_size_write(inode, size);
Steven Whitehousee285c102009-09-23 13:50:49 +0100776 inode->i_mtime = inode->i_atime = CURRENT_TIME;
Steven Whitehousee285c102009-09-23 13:50:49 +0100777 mark_inode_dirty(inode);
Bob Peterson500242a2012-05-15 14:51:54 -0400778 return 0;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100779
Abhijith Das7e619bc2010-05-07 17:50:18 -0400780unlock_out:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000781 unlock_page(page);
782 page_cache_release(page);
783 return err;
784}
785
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
787{
788 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400789 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100790 struct gfs2_alloc_parms ap = { .aflags = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 unsigned int data_blocks, ind_blocks;
792 struct gfs2_holder *ghs, i_gh;
793 unsigned int qx, x;
794 struct gfs2_quota_data *qd;
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100795 unsigned reserved;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000796 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600797 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 int error;
799
Bob Peterson0a305e42012-06-06 11:17:59 +0100800 error = gfs2_rs_alloc(ip);
801 if (error)
802 return error;
803
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
805 &data_blocks, &ind_blocks);
806
Josef Bacik16c5f062008-04-09 09:33:41 -0400807 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 if (!ghs)
809 return -ENOMEM;
810
811 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
Jan Kara56aa72d2012-09-05 16:55:11 -0400812 mutex_lock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813 for (qx = 0; qx < num_qd; qx++) {
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100814 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 GL_NOCACHE, &ghs[qx]);
816 if (error)
817 goto out;
818 }
819
820 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
821 if (error)
822 goto out;
823
824 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 offset = qd2offset(qda[x]);
Bob Peterson461cb412010-06-24 19:21:20 -0400826 if (gfs2_write_alloc_required(ip, offset,
827 sizeof(struct gfs2_quota)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 nalloc++;
829 }
830
Abhijith Das20b95bf2008-03-06 17:43:52 -0600831 /*
832 * 1 blk for unstuffing inode if stuffed. We add this extra
833 * block to the reservation unconditionally. If the inode
834 * doesn't need unstuffing, the block will be released to the
835 * rgrp since it won't be allocated during the transaction
836 */
Abhijith Das7e619bc2010-05-07 17:50:18 -0400837 /* +3 in the end for unstuffing block, inode size update block
838 * and another block in case quota straddles page boundary and
839 * two blocks need to be updated instead of 1 */
840 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600841
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100842 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100843 ap.target = reserved;
844 error = gfs2_inplace_reserve(ip, &ap);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600845 if (error)
846 goto out_alloc;
847
848 if (nalloc)
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100849 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600850
851 error = gfs2_trans_begin(sdp, blocks, 0);
852 if (error)
853 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854
855 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 qd = qda[x];
857 offset = qd2offset(qd);
Steven Whitehousee285c102009-09-23 13:50:49 +0100858 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000859 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860 goto out_end_trans;
861
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 do_qc(qd, -qd->qd_change_sync);
Abhijith Das662e3a52011-03-08 10:40:42 -0500863 set_bit(QDF_REFRESH, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 }
865
866 error = 0;
867
Steven Whitehousea91ea692006-09-04 12:04:26 -0400868out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400870out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600871 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400872out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400874out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 while (qx--)
876 gfs2_glock_dq_uninit(&ghs[qx]);
Steven Whitehousee285c102009-09-23 13:50:49 +0100877 mutex_unlock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400879 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880 return error;
881}
882
Steven Whitehousee285c102009-09-23 13:50:49 +0100883static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
884{
885 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
886 struct gfs2_quota q;
887 struct gfs2_quota_lvb *qlvb;
888 loff_t pos;
889 int error;
890
891 memset(&q, 0, sizeof(struct gfs2_quota));
892 pos = qd2offset(qd);
Andrew Price43066292012-04-16 16:40:55 +0100893 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
Steven Whitehousee285c102009-09-23 13:50:49 +0100894 if (error < 0)
895 return error;
896
David Teigland4e2f8842012-11-14 13:47:37 -0500897 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehousee285c102009-09-23 13:50:49 +0100898 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
899 qlvb->__pad = 0;
900 qlvb->qb_limit = q.qu_limit;
901 qlvb->qb_warn = q.qu_warn;
902 qlvb->qb_value = q.qu_value;
903 qd->qd_qb = *qlvb;
904
905 return 0;
906}
907
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
909 struct gfs2_holder *q_gh)
910{
911 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400912 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 struct gfs2_holder i_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 int error;
915
Steven Whitehousea91ea692006-09-04 12:04:26 -0400916restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
918 if (error)
919 return error;
920
David Teigland4e2f8842012-11-14 13:47:37 -0500921 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400923 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 gfs2_glock_dq_uninit(q_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100925 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
926 GL_NOCACHE, q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927 if (error)
928 return error;
929
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400930 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 if (error)
932 goto fail;
933
Steven Whitehousee285c102009-09-23 13:50:49 +0100934 error = update_qd(sdp, qd);
935 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 goto fail_gunlock;
Steven Whitehousee285c102009-09-23 13:50:49 +0100937
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100939 gfs2_glock_dq_uninit(q_gh);
940 force_refresh = 0;
941 goto restart;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 }
943
944 return 0;
945
Steven Whitehousea91ea692006-09-04 12:04:26 -0400946fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400948fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 return error;
951}
952
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -0800953int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400955 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Abhijith Das662e3a52011-03-08 10:40:42 -0500956 struct gfs2_quota_data *qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957 unsigned int x;
958 int error = 0;
959
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100960 error = gfs2_quota_hold(ip, uid, gid);
961 if (error)
962 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963
964 if (capable(CAP_SYS_RESOURCE) ||
965 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
966 return 0;
967
Bob Peterson5407e242012-05-18 09:28:23 -0400968 sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
969 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970
Bob Peterson5407e242012-05-18 09:28:23 -0400971 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
Abhijith Das662e3a52011-03-08 10:40:42 -0500972 int force = NO_FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400973 qd = ip->i_res->rs_qa_qd[x];
Abhijith Das662e3a52011-03-08 10:40:42 -0500974 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
975 force = FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400976 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977 if (error)
978 break;
979 }
980
981 if (!error)
982 set_bit(GIF_QD_LOCKED, &ip->i_flags);
983 else {
984 while (x--)
Bob Peterson5407e242012-05-18 09:28:23 -0400985 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 gfs2_quota_unhold(ip);
987 }
988
989 return error;
990}
991
992static int need_sync(struct gfs2_quota_data *qd)
993{
994 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
995 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400996 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 unsigned int num, den;
998 int do_sync = 1;
999
1000 if (!qd->qd_qb.qb_limit)
1001 return 0;
1002
Steven Whitehouse7d808232013-11-01 14:52:08 -04001003 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 value = qd->qd_change;
Steven Whitehouse7d808232013-11-01 14:52:08 -04001005 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006
1007 spin_lock(&gt->gt_spin);
1008 num = gt->gt_quota_scale_num;
1009 den = gt->gt_quota_scale_den;
1010 spin_unlock(&gt->gt_spin);
1011
1012 if (value < 0)
1013 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001014 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
1015 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 do_sync = 0;
1017 else {
1018 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +01001019 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001020 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -04001021 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 do_sync = 0;
1023 }
1024
1025 return do_sync;
1026}
1027
1028void gfs2_quota_unlock(struct gfs2_inode *ip)
1029{
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001030 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 struct gfs2_quota_data *qda[4];
1032 unsigned int count = 0;
1033 unsigned int x;
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001034 int found;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035
1036 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1037 goto out;
1038
Bob Peterson5407e242012-05-18 09:28:23 -04001039 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 struct gfs2_quota_data *qd;
1041 int sync;
1042
Bob Peterson5407e242012-05-18 09:28:23 -04001043 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 sync = need_sync(qd);
1045
Bob Peterson5407e242012-05-18 09:28:23 -04001046 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001047 if (!sync)
1048 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049
Steven Whitehouse7d808232013-11-01 14:52:08 -04001050 spin_lock(&qd_lock);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001051 found = qd_check_sync(sdp, qd, NULL);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001052 spin_unlock(&qd_lock);
Steven Whitehouseaabd7c72013-10-04 11:31:05 +01001053
1054 if (!found)
1055 continue;
1056
1057 gfs2_assert_warn(sdp, qd->qd_change_sync);
1058 if (bh_get(qd)) {
1059 clear_bit(QDF_LOCKED, &qd->qd_flags);
1060 slot_put(qd);
1061 qd_put(qd);
1062 continue;
1063 }
1064
1065 qda[count++] = qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 }
1067
1068 if (count) {
1069 do_sync(count, qda);
1070 for (x = 0; x < count; x++)
1071 qd_unlock(qda[x]);
1072 }
1073
Steven Whitehousea91ea692006-09-04 12:04:26 -04001074out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 gfs2_quota_unhold(ip);
1076}
1077
1078#define MAX_LINE 256
1079
1080static int print_message(struct gfs2_quota_data *qd, char *type)
1081{
1082 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001084 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
Steven Whitehouse02630a12006-07-03 11:20:06 -04001085 sdp->sd_fsname, type,
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001086 (qd->qd_id.type == USRQUOTA) ? "user" : "group",
1087 from_kqid(&init_user_ns, qd->qd_id));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088
1089 return 0;
1090}
1091
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001092int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001094 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001095 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001096 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 unsigned int x;
1098 int error = 0;
1099
1100 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1101 return 0;
1102
1103 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1104 return 0;
1105
Bob Peterson5407e242012-05-18 09:28:23 -04001106 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1107 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001109 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1110 qid_eq(qd->qd_id, make_kqid_gid(gid))))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 continue;
1112
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001113 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001114 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 value += qd->qd_change;
Steven Whitehouse7d808232013-11-01 14:52:08 -04001116 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117
Steven Whitehousecd915492006-09-04 12:49:07 -04001118 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 +00001119 print_message(qd, "exceeded");
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001120 quota_send_warning(qd->qd_id,
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001121 sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1122
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 error = -EDQUOT;
1124 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001125 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001126 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001128 gfs2_tune_get(sdp,
1129 gt_quota_warn_period) * HZ)) {
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001130 quota_send_warning(qd->qd_id,
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001131 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 error = print_message(qd, "warning");
1133 qd->qd_last_warn = jiffies;
1134 }
1135 }
1136
1137 return error;
1138}
1139
Steven Whitehousecd915492006-09-04 12:49:07 -04001140void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001141 kuid_t uid, kgid_t gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 struct gfs2_quota_data *qd;
1144 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001146 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001148 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 return;
1150
Bob Peterson5407e242012-05-18 09:28:23 -04001151 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1152 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001154 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
1155 qid_eq(qd->qd_id, make_kqid_gid(gid))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 }
1158 }
1159}
1160
Jan Karaceed1722012-07-03 16:45:28 +02001161int gfs2_quota_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162{
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001163 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 struct gfs2_quota_data **qda;
Steven Whitehousebef292a2013-10-03 18:43:20 +01001165 unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 unsigned int num_qd;
1167 unsigned int x;
1168 int error = 0;
1169
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1171 if (!qda)
1172 return -ENOMEM;
1173
Steven Whitehousee46c7722013-10-04 12:29:34 +01001174 mutex_lock(&sdp->sd_quota_sync_mutex);
1175 sdp->sd_quota_sync_gen++;
1176
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 do {
1178 num_qd = 0;
1179
1180 for (;;) {
1181 error = qd_fish(sdp, qda + num_qd);
1182 if (error || !qda[num_qd])
1183 break;
1184 if (++num_qd == max_qd)
1185 break;
1186 }
1187
1188 if (num_qd) {
1189 if (!error)
1190 error = do_sync(num_qd, qda);
1191 if (!error)
1192 for (x = 0; x < num_qd; x++)
1193 qda[x]->qd_sync_gen =
1194 sdp->sd_quota_sync_gen;
1195
1196 for (x = 0; x < num_qd; x++)
1197 qd_unlock(qda[x]);
1198 }
1199 } while (!error && num_qd == max_qd);
1200
Steven Whitehousee46c7722013-10-04 12:29:34 +01001201 mutex_unlock(&sdp->sd_quota_sync_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001202 kfree(qda);
1203
1204 return error;
1205}
1206
Eric W. Biedermaned87dab2013-01-31 19:42:40 -08001207int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208{
1209 struct gfs2_quota_data *qd;
1210 struct gfs2_holder q_gh;
1211 int error;
1212
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001213 error = qd_get(sdp, qid, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214 if (error)
1215 return error;
1216
1217 error = do_glock(qd, FORCE, &q_gh);
1218 if (!error)
1219 gfs2_glock_dq_uninit(&q_gh);
1220
1221 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222 return error;
1223}
1224
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225int gfs2_quota_init(struct gfs2_sbd *sdp)
1226{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001227 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001228 u64 size = i_size_read(sdp->sd_qc_inode);
1229 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001230 unsigned int x, slot = 0;
1231 unsigned int found = 0;
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001232 unsigned int hash;
Steven Whitehouseee2411a2013-12-12 17:29:32 +00001233 unsigned int bm_size;
Steven Whitehousecd915492006-09-04 12:49:07 -04001234 u64 dblock;
1235 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236 int error;
1237
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001238 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001239 return -EIO;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001240
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouseee2411a2013-12-12 17:29:32 +00001242 bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
1243 bm_size *= sizeof(unsigned long);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 error = -ENOMEM;
Steven Whitehouseee2411a2013-12-12 17:29:32 +00001245 sdp->sd_quota_bitmap = kmalloc(bm_size, GFP_NOFS|__GFP_NOWARN);
1246 if (sdp->sd_quota_bitmap == NULL)
1247 sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS, PAGE_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248 if (!sdp->sd_quota_bitmap)
1249 return error;
1250
Steven Whitehouseee2411a2013-12-12 17:29:32 +00001251 memset(sdp->sd_quota_bitmap, 0, bm_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252
1253 for (x = 0; x < blocks; x++) {
1254 struct buffer_head *bh;
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001255 const struct gfs2_quota_change *qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256 unsigned int y;
1257
1258 if (!extlen) {
1259 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001260 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261 if (error)
1262 goto fail;
1263 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001265 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1266 if (!bh)
1267 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1269 brelse(bh);
1270 goto fail;
1271 }
1272
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001273 qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001274 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001275 y++, slot++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276 struct gfs2_quota_data *qd;
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001277 s64 qc_change = be64_to_cpu(qc->qc_change);
1278 u32 qc_flags = be32_to_cpu(qc->qc_flags);
1279 enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
1280 USRQUOTA : GRPQUOTA;
1281 struct kqid qc_id = make_kqid(&init_user_ns, qtype,
1282 be32_to_cpu(qc->qc_id));
1283 qc++;
1284 if (!qc_change)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 continue;
1286
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001287 hash = gfs2_qd_hash(sdp, qc_id);
1288 qd = qd_alloc(hash, sdp, qc_id);
1289 if (qd == NULL) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290 brelse(bh);
1291 goto fail;
1292 }
1293
1294 set_bit(QDF_CHANGE, &qd->qd_flags);
Steven Whitehouse7aed98f2013-11-26 15:17:09 +00001295 qd->qd_change = qc_change;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296 qd->qd_slot = slot;
1297 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001298
Steven Whitehouse7d808232013-11-01 14:52:08 -04001299 spin_lock(&qd_lock);
Steven Whitehouseee2411a2013-12-12 17:29:32 +00001300 BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301 list_add(&qd->qd_list, &sdp->sd_quota_list);
1302 atomic_inc(&sdp->sd_quota_count);
Steven Whitehouse7d808232013-11-01 14:52:08 -04001303 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001305 spin_lock_bucket(hash);
1306 hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
1307 spin_unlock_bucket(hash);
1308
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 found++;
1310 }
1311
1312 brelse(bh);
1313 dblock++;
1314 extlen--;
1315 }
1316
1317 if (found)
1318 fs_info(sdp, "found %u quota changes\n", found);
1319
1320 return 0;
1321
Steven Whitehousea91ea692006-09-04 12:04:26 -04001322fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 gfs2_quota_cleanup(sdp);
1324 return error;
1325}
1326
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1328{
1329 struct list_head *head = &sdp->sd_quota_list;
1330 struct gfs2_quota_data *qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001331
Steven Whitehouse7d808232013-11-01 14:52:08 -04001332 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333 while (!list_empty(head)) {
1334 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1335
Abhijith Das0a7ab792009-01-07 16:03:37 -06001336 list_del(&qd->qd_list);
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001337
Abhijith Das0a7ab792009-01-07 16:03:37 -06001338 /* 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 Whitehousec754fbb2013-12-12 10:47:59 +00001343 spin_lock_bucket(qd->qd_hash);
1344 hlist_bl_del_rcu(&qd->qd_hlist);
1345 spin_unlock_bucket(qd->qd_hash);
1346
Steven Whitehouse8ad151c2013-12-12 11:34:09 +00001347 gfs2_assert_warn(sdp, !qd->qd_change);
1348 gfs2_assert_warn(sdp, !qd->qd_slot_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001349 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1350
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001351 gfs2_glock_put(qd->qd_gl);
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001352 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353
Steven Whitehouse7d808232013-11-01 14:52:08 -04001354 spin_lock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001355 }
Steven Whitehouse7d808232013-11-01 14:52:08 -04001356 spin_unlock(&qd_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357
1358 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1359
1360 if (sdp->sd_quota_bitmap) {
Steven Whitehouseee2411a2013-12-12 17:29:32 +00001361 if (is_vmalloc_addr(sdp->sd_quota_bitmap))
1362 vfree(sdp->sd_quota_bitmap);
1363 else
1364 kfree(sdp->sd_quota_bitmap);
1365 sdp->sd_quota_bitmap = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001366 }
1367}
1368
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001369static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1370{
1371 if (error == 0 || error == -EROFS)
1372 return;
1373 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1374 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1375}
1376
1377static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001378 int (*fxn)(struct super_block *sb, int type),
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001379 unsigned long t, unsigned long *timeo,
1380 unsigned int *new_timeo)
1381{
1382 if (t >= *timeo) {
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001383 int error = fxn(sdp->sd_vfs, 0);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001384 quotad_error(sdp, msg, error);
1385 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1386 } else {
1387 *timeo -= t;
1388 }
1389}
1390
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001391static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1392{
1393 struct gfs2_inode *ip;
1394
1395 while(1) {
1396 ip = NULL;
1397 spin_lock(&sdp->sd_trunc_lock);
1398 if (!list_empty(&sdp->sd_trunc_list)) {
1399 ip = list_entry(sdp->sd_trunc_list.next,
1400 struct gfs2_inode, i_trunc_list);
1401 list_del_init(&ip->i_trunc_list);
1402 }
1403 spin_unlock(&sdp->sd_trunc_lock);
1404 if (ip == NULL)
1405 return;
1406 gfs2_glock_finish_truncate(ip);
1407 }
1408}
1409
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001410void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1411 if (!sdp->sd_statfs_force_sync) {
1412 sdp->sd_statfs_force_sync = 1;
1413 wake_up(&sdp->sd_quota_wait);
1414 }
1415}
1416
1417
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001418/**
1419 * gfs2_quotad - Write cached quota changes into the quota file
1420 * @sdp: Pointer to GFS2 superblock
1421 *
1422 */
1423
1424int gfs2_quotad(void *data)
1425{
1426 struct gfs2_sbd *sdp = data;
1427 struct gfs2_tune *tune = &sdp->sd_tune;
1428 unsigned long statfs_timeo = 0;
1429 unsigned long quotad_timeo = 0;
1430 unsigned long t = 0;
1431 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001432 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001433
1434 while (!kthread_should_stop()) {
1435
1436 /* Update the master statfs file */
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001437 if (sdp->sd_statfs_force_sync) {
1438 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1439 quotad_error(sdp, "statfs", error);
1440 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1441 }
1442 else
1443 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1444 &statfs_timeo,
1445 &tune->gt_statfs_quantum);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001446
1447 /* Update quota file */
Steven Whitehouseedd2e9a2013-06-03 11:12:59 +01001448 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001449 &quotad_timeo, &tune->gt_quota_quantum);
1450
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001451 /* Check for & recover partially truncated inodes */
1452 quotad_check_trunc_list(sdp);
1453
Tejun Heoa0acae02011-11-21 12:32:22 -08001454 try_to_freeze();
1455
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001456 t = min(quotad_timeo, statfs_timeo);
1457
Steven Whitehouse7fa5d202009-03-31 15:49:08 +01001458 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001459 spin_lock(&sdp->sd_trunc_lock);
1460 empty = list_empty(&sdp->sd_trunc_list);
1461 spin_unlock(&sdp->sd_trunc_lock);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001462 if (empty && !sdp->sd_statfs_force_sync)
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001463 t -= schedule_timeout(t);
1464 else
1465 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001466 finish_wait(&sdp->sd_quota_wait, &wait);
1467 }
1468
1469 return 0;
1470}
1471
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001472static int gfs2_quota_get_xstate(struct super_block *sb,
1473 struct fs_quota_stat *fqs)
1474{
1475 struct gfs2_sbd *sdp = sb->s_fs_info;
1476
1477 memset(fqs, 0, sizeof(struct fs_quota_stat));
1478 fqs->qs_version = FS_QSTAT_VERSION;
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001479
1480 switch (sdp->sd_args.ar_quota) {
1481 case GFS2_QUOTA_ON:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001482 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001483 /*FALLTHRU*/
1484 case GFS2_QUOTA_ACCOUNT:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001485 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001486 break;
1487 case GFS2_QUOTA_OFF:
1488 break;
1489 }
1490
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001491 if (sdp->sd_quota_inode) {
1492 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1493 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1494 }
1495 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1496 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
Steven Whitehouse2147dbf2013-11-04 10:15:08 +00001497 fqs->qs_incoredqs = list_lru_count(&gfs2_qd_lru);
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001498 return 0;
1499}
1500
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001501static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001502 struct fs_disk_quota *fdq)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001503{
1504 struct gfs2_sbd *sdp = sb->s_fs_info;
1505 struct gfs2_quota_lvb *qlvb;
1506 struct gfs2_quota_data *qd;
1507 struct gfs2_holder q_gh;
1508 int error;
1509
1510 memset(fdq, 0, sizeof(struct fs_disk_quota));
1511
1512 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1513 return -ESRCH; /* Crazy XFS error code */
1514
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001515 if ((qid.type != USRQUOTA) &&
1516 (qid.type != GRPQUOTA))
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001517 return -EINVAL;
1518
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001519 error = qd_get(sdp, qid, &qd);
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001520 if (error)
1521 return error;
1522 error = do_glock(qd, FORCE, &q_gh);
1523 if (error)
1524 goto out;
1525
David Teigland4e2f8842012-11-14 13:47:37 -05001526 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001527 fdq->d_version = FS_DQUOT_VERSION;
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001528 fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
Eric W. Biederman558e8522013-01-31 18:15:33 -08001529 fdq->d_id = from_kqid_munged(current_user_ns(), qid);
Abhijith Das14870b42010-11-18 11:24:24 -05001530 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1531 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1532 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001533
1534 gfs2_glock_dq_uninit(&q_gh);
1535out:
1536 qd_put(qd);
1537 return error;
1538}
1539
Steven Whitehousee285c102009-09-23 13:50:49 +01001540/* GFS2 only supports a subset of the XFS fields */
Abhijith Das802ec9b2010-11-18 11:26:46 -05001541#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
Steven Whitehousee285c102009-09-23 13:50:49 +01001542
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001543static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001544 struct fs_disk_quota *fdq)
Steven Whitehousee285c102009-09-23 13:50:49 +01001545{
1546 struct gfs2_sbd *sdp = sb->s_fs_info;
1547 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1548 struct gfs2_quota_data *qd;
1549 struct gfs2_holder q_gh, i_gh;
1550 unsigned int data_blocks, ind_blocks;
1551 unsigned int blocks = 0;
1552 int alloc_required;
Steven Whitehousee285c102009-09-23 13:50:49 +01001553 loff_t offset;
1554 int error;
1555
1556 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1557 return -ESRCH; /* Crazy XFS error code */
1558
Eric W. Biederman236c64e2013-01-31 20:09:30 -08001559 if ((qid.type != USRQUOTA) &&
1560 (qid.type != GRPQUOTA))
Steven Whitehousee285c102009-09-23 13:50:49 +01001561 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001562
1563 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1564 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001565
Eric W. Biederman05e0a602013-01-31 19:52:08 -08001566 error = qd_get(sdp, qid, &qd);
Steven Whitehousee285c102009-09-23 13:50:49 +01001567 if (error)
1568 return error;
1569
Bob Peterson0a305e42012-06-06 11:17:59 +01001570 error = gfs2_rs_alloc(ip);
1571 if (error)
1572 goto out_put;
1573
Steven Whitehousee285c102009-09-23 13:50:49 +01001574 mutex_lock(&ip->i_inode.i_mutex);
1575 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1576 if (error)
Bob Peterson0a305e42012-06-06 11:17:59 +01001577 goto out_unlockput;
Steven Whitehousee285c102009-09-23 13:50:49 +01001578 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1579 if (error)
1580 goto out_q;
1581
1582 /* Check for existing entry, if none then alloc new blocks */
1583 error = update_qd(sdp, qd);
1584 if (error)
1585 goto out_i;
1586
1587 /* If nothing has changed, this is a no-op */
1588 if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001589 ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001590 fdq->d_fieldmask ^= FS_DQ_BSOFT;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001591
Steven Whitehousee285c102009-09-23 13:50:49 +01001592 if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001593 ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001594 fdq->d_fieldmask ^= FS_DQ_BHARD;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001595
1596 if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1597 ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1598 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1599
Steven Whitehousee285c102009-09-23 13:50:49 +01001600 if (fdq->d_fieldmask == 0)
1601 goto out_i;
1602
1603 offset = qd2offset(qd);
Bob Peterson461cb412010-06-24 19:21:20 -04001604 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
Abhijith Dase79a46a2011-02-07 11:22:41 -05001605 if (gfs2_is_stuffed(ip))
1606 alloc_required = 1;
Steven Whitehousee285c102009-09-23 13:50:49 +01001607 if (alloc_required) {
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001608 struct gfs2_alloc_parms ap = { .aflags = 0, };
Steven Whitehousee285c102009-09-23 13:50:49 +01001609 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1610 &data_blocks, &ind_blocks);
Bob Peterson564e12b2011-11-21 13:36:17 -05001611 blocks = 1 + data_blocks + ind_blocks;
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001612 ap.target = blocks;
1613 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousee285c102009-09-23 13:50:49 +01001614 if (error)
Bob Peterson564e12b2011-11-21 13:36:17 -05001615 goto out_i;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001616 blocks += gfs2_rg_blocks(ip, blocks);
Steven Whitehousee285c102009-09-23 13:50:49 +01001617 }
1618
Abhijith Dase79a46a2011-02-07 11:22:41 -05001619 /* Some quotas span block boundaries and can update two blocks,
1620 adding an extra block to the transaction to handle such quotas */
1621 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001622 if (error)
1623 goto out_release;
1624
1625 /* Apply changes */
1626 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1627
1628 gfs2_trans_end(sdp);
1629out_release:
Bob Peterson564e12b2011-11-21 13:36:17 -05001630 if (alloc_required)
Steven Whitehousee285c102009-09-23 13:50:49 +01001631 gfs2_inplace_release(ip);
Steven Whitehousee285c102009-09-23 13:50:49 +01001632out_i:
1633 gfs2_glock_dq_uninit(&i_gh);
1634out_q:
1635 gfs2_glock_dq_uninit(&q_gh);
Bob Peterson0a305e42012-06-06 11:17:59 +01001636out_unlockput:
Steven Whitehousee285c102009-09-23 13:50:49 +01001637 mutex_unlock(&ip->i_inode.i_mutex);
Bob Peterson0a305e42012-06-06 11:17:59 +01001638out_put:
Steven Whitehousee285c102009-09-23 13:50:49 +01001639 qd_put(qd);
1640 return error;
1641}
1642
Steven Whitehousecc632e72009-09-15 09:59:02 +01001643const struct quotactl_ops gfs2_quotactl_ops = {
1644 .quota_sync = gfs2_quota_sync,
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001645 .get_xstate = gfs2_quota_get_xstate,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001646 .get_dqblk = gfs2_get_dqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001647 .set_dqblk = gfs2_set_dqblk,
Steven Whitehousecc632e72009-09-15 09:59:02 +01001648};
Steven Whitehousec754fbb2013-12-12 10:47:59 +00001649
1650void __init gfs2_quota_hash_init(void)
1651{
1652 unsigned i;
1653
1654 for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
1655 INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
1656}