blob: 20762ae2a9c44065bc4bc4ce69ed5697a62ffc2f [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000053
54#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050055#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000056#include "bmap.h"
57#include "glock.h"
58#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000059#include "log.h"
60#include "meta_io.h"
61#include "quota.h"
62#include "rgrp.h"
63#include "super.h"
64#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000065#include "inode.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050066#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000067
68#define QUOTA_USER 1
69#define QUOTA_GROUP 0
70
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010071struct gfs2_quota_change_host {
72 u64 qc_change;
73 u32 qc_flags; /* GFS2_QCF_... */
Eric W. Biedermane08d8d72013-01-31 19:25:50 -080074 struct kqid qc_id;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010075};
76
Abhijith Das0a7ab792009-01-07 16:03:37 -060077static LIST_HEAD(qd_lru_list);
78static atomic_t qd_lru_count = ATOMIC_INIT(0);
Xu Gang1328df72009-04-14 14:54:14 +080079static DEFINE_SPINLOCK(qd_lru_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -060080
Ying Han1495f232011-05-24 17:12:27 -070081int gfs2_shrink_qd_memory(struct shrinker *shrink, struct shrink_control *sc)
Abhijith Das0a7ab792009-01-07 16:03:37 -060082{
83 struct gfs2_quota_data *qd;
84 struct gfs2_sbd *sdp;
Ying Han1495f232011-05-24 17:12:27 -070085 int nr_to_scan = sc->nr_to_scan;
Abhijith Das0a7ab792009-01-07 16:03:37 -060086
Ying Han1495f232011-05-24 17:12:27 -070087 if (nr_to_scan == 0)
Abhijith Das0a7ab792009-01-07 16:03:37 -060088 goto out;
89
Ying Han1495f232011-05-24 17:12:27 -070090 if (!(sc->gfp_mask & __GFP_FS))
Abhijith Das0a7ab792009-01-07 16:03:37 -060091 return -1;
92
93 spin_lock(&qd_lru_lock);
Ying Han1495f232011-05-24 17:12:27 -070094 while (nr_to_scan && !list_empty(&qd_lru_list)) {
Abhijith Das0a7ab792009-01-07 16:03:37 -060095 qd = list_entry(qd_lru_list.next,
96 struct gfs2_quota_data, qd_reclaim);
97 sdp = qd->qd_gl->gl_sbd;
98
99 /* Free from the filesystem-specific list */
100 list_del(&qd->qd_list);
101
Abhijith Das0a7ab792009-01-07 16:03:37 -0600102 gfs2_assert_warn(sdp, !qd->qd_change);
103 gfs2_assert_warn(sdp, !qd->qd_slot_count);
104 gfs2_assert_warn(sdp, !qd->qd_bh_count);
105
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000106 gfs2_glock_put(qd->qd_gl);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600107 atomic_dec(&sdp->sd_quota_count);
108
109 /* Delete it from the common reclaim list */
110 list_del_init(&qd->qd_reclaim);
111 atomic_dec(&qd_lru_count);
112 spin_unlock(&qd_lru_lock);
113 kmem_cache_free(gfs2_quotad_cachep, qd);
114 spin_lock(&qd_lru_lock);
Ying Han1495f232011-05-24 17:12:27 -0700115 nr_to_scan--;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600116 }
117 spin_unlock(&qd_lru_lock);
118
119out:
120 return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
121}
122
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800123static u64 qd2index(struct gfs2_quota_data *qd)
124{
125 return (2 * (u64)qd->qd_id) +
126 test_bit(QDF_USER, &qd->qd_flags) ? 0 : 1;
127}
128
Steven Whitehousecd915492006-09-04 12:49:07 -0400129static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130{
Steven Whitehousecd915492006-09-04 12:49:07 -0400131 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800133 offset = qd2index(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 offset *= sizeof(struct gfs2_quota);
135
136 return offset;
137}
138
Steven Whitehousecd915492006-09-04 12:49:07 -0400139static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 struct gfs2_quota_data **qdp)
141{
142 struct gfs2_quota_data *qd;
143 int error;
144
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000145 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 if (!qd)
147 return -ENOMEM;
148
Abhijith Das0a7ab792009-01-07 16:03:37 -0600149 atomic_set(&qd->qd_count, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 qd->qd_id = id;
151 if (user)
152 set_bit(QDF_USER, &qd->qd_flags);
153 qd->qd_slot = -1;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600154 INIT_LIST_HEAD(&qd->qd_reclaim);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155
Eric W. Biederman2f6c9892013-01-31 18:33:38 -0800156 error = gfs2_glock_get(sdp, qd2index(qd),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 &gfs2_quota_glops, CREATE, &qd->qd_gl);
158 if (error)
159 goto fail;
160
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 *qdp = qd;
162
163 return 0;
164
Steven Whitehousea91ea692006-09-04 12:04:26 -0400165fail:
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000166 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167 return error;
168}
169
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100170static int qd_get(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 struct gfs2_quota_data **qdp)
172{
173 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
174 int error, found;
175
176 *qdp = NULL;
177
178 for (;;) {
179 found = 0;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600180 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
182 if (qd->qd_id == id &&
183 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
Abhijith Das0a7ab792009-01-07 16:03:37 -0600184 if (!atomic_read(&qd->qd_count) &&
185 !list_empty(&qd->qd_reclaim)) {
186 /* Remove it from reclaim list */
187 list_del_init(&qd->qd_reclaim);
188 atomic_dec(&qd_lru_count);
189 }
190 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 found = 1;
192 break;
193 }
194 }
195
196 if (!found)
197 qd = NULL;
198
199 if (!qd && new_qd) {
200 qd = new_qd;
201 list_add(&qd->qd_list, &sdp->sd_quota_list);
202 atomic_inc(&sdp->sd_quota_count);
203 new_qd = NULL;
204 }
205
Abhijith Das0a7ab792009-01-07 16:03:37 -0600206 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100208 if (qd) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 if (new_qd) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000210 gfs2_glock_put(new_qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000211 kmem_cache_free(gfs2_quotad_cachep, new_qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 }
213 *qdp = qd;
214 return 0;
215 }
216
217 error = qd_alloc(sdp, user, id, &new_qd);
218 if (error)
219 return error;
220 }
221}
222
223static void qd_hold(struct gfs2_quota_data *qd)
224{
225 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600226 gfs2_assert(sdp, atomic_read(&qd->qd_count));
227 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228}
229
230static void qd_put(struct gfs2_quota_data *qd)
231{
Abhijith Das0a7ab792009-01-07 16:03:37 -0600232 if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
233 /* Add to the reclaim list */
234 list_add_tail(&qd->qd_reclaim, &qd_lru_list);
235 atomic_inc(&qd_lru_count);
236 spin_unlock(&qd_lru_lock);
237 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238}
239
240static int slot_get(struct gfs2_quota_data *qd)
241{
242 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
243 unsigned int c, o = 0, b;
244 unsigned char byte = 0;
245
Steven Whitehouse22077f52009-01-08 14:28:42 +0000246 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247
248 if (qd->qd_slot_count++) {
Steven Whitehouse22077f52009-01-08 14:28:42 +0000249 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 return 0;
251 }
252
253 for (c = 0; c < sdp->sd_quota_chunks; c++)
254 for (o = 0; o < PAGE_SIZE; o++) {
255 byte = sdp->sd_quota_bitmap[c][o];
256 if (byte != 0xFF)
257 goto found;
258 }
259
260 goto fail;
261
Steven Whitehousea91ea692006-09-04 12:04:26 -0400262found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263 for (b = 0; b < 8; b++)
264 if (!(byte & (1 << b)))
265 break;
266 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
267
268 if (qd->qd_slot >= sdp->sd_quota_slots)
269 goto fail;
270
271 sdp->sd_quota_bitmap[c][o] |= 1 << b;
272
Steven Whitehouse22077f52009-01-08 14:28:42 +0000273 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274
275 return 0;
276
Steven Whitehousea91ea692006-09-04 12:04:26 -0400277fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 qd->qd_slot_count--;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000279 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 return -ENOSPC;
281}
282
283static void slot_hold(struct gfs2_quota_data *qd)
284{
285 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
286
Steven Whitehouse22077f52009-01-08 14:28:42 +0000287 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 gfs2_assert(sdp, qd->qd_slot_count);
289 qd->qd_slot_count++;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000290 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291}
292
293static void slot_put(struct gfs2_quota_data *qd)
294{
295 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
296
Steven Whitehouse22077f52009-01-08 14:28:42 +0000297 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 gfs2_assert(sdp, qd->qd_slot_count);
299 if (!--qd->qd_slot_count) {
300 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
301 qd->qd_slot = -1;
302 }
Steven Whitehouse22077f52009-01-08 14:28:42 +0000303 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304}
305
306static int bh_get(struct gfs2_quota_data *qd)
307{
308 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400309 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 struct buffer_head *bh;
312 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400313 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314
Steven Whitehousef55ab262006-02-21 12:51:39 +0000315 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316
317 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000318 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 return 0;
320 }
321
322 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600323 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324
Steven Whitehouse23591252006-10-13 17:25:45 -0400325 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600326 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327 if (error)
328 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400329 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 if (error)
331 goto fail;
332 error = -EIO;
333 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
334 goto fail_brelse;
335
336 qd->qd_bh = bh;
337 qd->qd_bh_qc = (struct gfs2_quota_change *)
338 (bh->b_data + sizeof(struct gfs2_meta_header) +
339 offset * sizeof(struct gfs2_quota_change));
340
Josef Whiter2e95b662007-02-20 00:03:29 -0500341 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342
343 return 0;
344
Steven Whitehousea91ea692006-09-04 12:04:26 -0400345fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400347fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000349 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 return error;
351}
352
353static void bh_put(struct gfs2_quota_data *qd)
354{
355 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
356
Steven Whitehousef55ab262006-02-21 12:51:39 +0000357 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 gfs2_assert(sdp, qd->qd_bh_count);
359 if (!--qd->qd_bh_count) {
360 brelse(qd->qd_bh);
361 qd->qd_bh = NULL;
362 qd->qd_bh_qc = NULL;
363 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000364 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365}
366
367static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
368{
369 struct gfs2_quota_data *qd = NULL;
370 int error;
371 int found = 0;
372
373 *qdp = NULL;
374
375 if (sdp->sd_vfs->s_flags & MS_RDONLY)
376 return 0;
377
Abhijith Das0a7ab792009-01-07 16:03:37 -0600378 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379
380 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
381 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
382 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
383 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
384 continue;
385
386 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
387
388 set_bit(QDF_LOCKED, &qd->qd_flags);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600389 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
390 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 qd->qd_change_sync = qd->qd_change;
392 gfs2_assert_warn(sdp, qd->qd_slot_count);
393 qd->qd_slot_count++;
394 found = 1;
395
396 break;
397 }
398
399 if (!found)
400 qd = NULL;
401
Abhijith Das0a7ab792009-01-07 16:03:37 -0600402 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403
404 if (qd) {
405 gfs2_assert_warn(sdp, qd->qd_change_sync);
406 error = bh_get(qd);
407 if (error) {
408 clear_bit(QDF_LOCKED, &qd->qd_flags);
409 slot_put(qd);
410 qd_put(qd);
411 return error;
412 }
413 }
414
415 *qdp = qd;
416
417 return 0;
418}
419
420static int qd_trylock(struct gfs2_quota_data *qd)
421{
422 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
423
424 if (sdp->sd_vfs->s_flags & MS_RDONLY)
425 return 0;
426
Abhijith Das0a7ab792009-01-07 16:03:37 -0600427 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428
429 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
430 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
Abhijith Das0a7ab792009-01-07 16:03:37 -0600431 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 return 0;
433 }
434
435 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
436
437 set_bit(QDF_LOCKED, &qd->qd_flags);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600438 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
439 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 qd->qd_change_sync = qd->qd_change;
441 gfs2_assert_warn(sdp, qd->qd_slot_count);
442 qd->qd_slot_count++;
443
Abhijith Das0a7ab792009-01-07 16:03:37 -0600444 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445
446 gfs2_assert_warn(sdp, qd->qd_change_sync);
447 if (bh_get(qd)) {
448 clear_bit(QDF_LOCKED, &qd->qd_flags);
449 slot_put(qd);
450 qd_put(qd);
451 return 0;
452 }
453
454 return 1;
455}
456
457static void qd_unlock(struct gfs2_quota_data *qd)
458{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500459 gfs2_assert_warn(qd->qd_gl->gl_sbd,
460 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 clear_bit(QDF_LOCKED, &qd->qd_flags);
462 bh_put(qd);
463 slot_put(qd);
464 qd_put(qd);
465}
466
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800467static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 struct gfs2_quota_data **qdp)
469{
470 int error;
471
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800472 error = qd_get(sdp, qid.type == USRQUOTA ? QUOTA_USER : QUOTA_GROUP,
473 from_kqid(&init_user_ns, qid), qdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 if (error)
475 return error;
476
477 error = slot_get(*qdp);
478 if (error)
479 goto fail;
480
481 error = bh_get(*qdp);
482 if (error)
483 goto fail_slot;
484
485 return 0;
486
Steven Whitehousea91ea692006-09-04 12:04:26 -0400487fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400489fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 qd_put(*qdp);
491 return error;
492}
493
494static void qdsb_put(struct gfs2_quota_data *qd)
495{
496 bh_put(qd);
497 slot_put(qd);
498 qd_put(qd);
499}
500
Steven Whitehousecd915492006-09-04 12:49:07 -0400501int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400503 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson5407e242012-05-18 09:28:23 -0400504 struct gfs2_quota_data **qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 int error;
506
Andrew Priceaaaf68c2012-10-12 16:45:08 +0100507 if (ip->i_res == NULL) {
508 error = gfs2_rs_alloc(ip);
509 if (error)
510 return error;
511 }
Bob Peterson5407e242012-05-18 09:28:23 -0400512
513 qd = ip->i_res->rs_qa_qd;
514
515 if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
517 return -EIO;
518
519 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
520 return 0;
521
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800522 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 if (error)
524 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400525 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 qd++;
527
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800528 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 if (error)
530 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400531 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 qd++;
533
Eric W. Biedermanf4108a62013-01-31 17:49:26 -0800534 if (uid != NO_UID_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800535 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 if (error)
537 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400538 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 qd++;
540 }
541
Eric W. Biedermanf4108a62013-01-31 17:49:26 -0800542 if (gid != NO_GID_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
Eric W. Biedermanb59c8b62013-01-31 19:35:56 -0800543 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 if (error)
545 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400546 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 qd++;
548 }
549
Steven Whitehousea91ea692006-09-04 12:04:26 -0400550out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551 if (error)
552 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 return error;
554}
555
556void gfs2_quota_unhold(struct gfs2_inode *ip)
557{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400558 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 unsigned int x;
560
Bob Peterson5407e242012-05-18 09:28:23 -0400561 if (ip->i_res == NULL)
562 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
564
Bob Peterson5407e242012-05-18 09:28:23 -0400565 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
566 qdsb_put(ip->i_res->rs_qa_qd[x]);
567 ip->i_res->rs_qa_qd[x] = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 }
Bob Peterson5407e242012-05-18 09:28:23 -0400569 ip->i_res->rs_qa_qd_num = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570}
571
572static int sort_qd(const void *a, const void *b)
573{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400574 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
575 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576
577 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
578 !test_bit(QDF_USER, &qd_b->qd_flags)) {
579 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400580 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400582 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400584 if (qd_a->qd_id < qd_b->qd_id)
585 return -1;
586 if (qd_a->qd_id > qd_b->qd_id)
587 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588
Steven Whitehouse48fac172006-09-05 15:17:12 -0400589 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590}
591
Steven Whitehousecd915492006-09-04 12:49:07 -0400592static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593{
594 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400595 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400597 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598
Steven Whitehousef55ab262006-02-21 12:51:39 +0000599 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000600 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601
602 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
603 qc->qc_change = 0;
604 qc->qc_flags = 0;
605 if (test_bit(QDF_USER, &qd->qd_flags))
606 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
607 qc->qc_id = cpu_to_be32(qd->qd_id);
608 }
609
Al Virob44b84d2006-10-14 10:46:30 -0400610 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611 qc->qc_change = cpu_to_be64(x);
612
Steven Whitehouse22077f52009-01-08 14:28:42 +0000613 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 qd->qd_change = x;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000615 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616
617 if (!x) {
618 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
619 clear_bit(QDF_CHANGE, &qd->qd_flags);
620 qc->qc_flags = 0;
621 qc->qc_id = 0;
622 slot_put(qd);
623 qd_put(qd);
624 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
625 qd_hold(qd);
626 slot_hold(qd);
627 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400628
Steven Whitehousef55ab262006-02-21 12:51:39 +0000629 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630}
631
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000632/**
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100633 * gfs2_adjust_quota - adjust record of current block usage
634 * @ip: The quota inode
635 * @loc: Offset of the entry in the quota file
Steven Whitehousee285c102009-09-23 13:50:49 +0100636 * @change: The amount of usage change to record
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100637 * @qd: The quota data
Steven Whitehousee285c102009-09-23 13:50:49 +0100638 * @fdq: The updated limits to record
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000639 *
640 * This function was mostly borrowed from gfs2_block_truncate_page which was
641 * in turn mostly borrowed from ext3
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100642 *
643 * Returns: 0 or -ve on error
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000644 */
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100645
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000646static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousee285c102009-09-23 13:50:49 +0100647 s64 change, struct gfs2_quota_data *qd,
648 struct fs_disk_quota *fdq)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000649{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400650 struct inode *inode = &ip->i_inode;
Abhijith Das14870b42010-11-18 11:24:24 -0500651 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000652 struct address_space *mapping = inode->i_mapping;
653 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500654 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000655 unsigned blocksize, iblock, pos;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100656 struct buffer_head *bh;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000657 struct page *page;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400658 void *kaddr, *ptr;
659 struct gfs2_quota q, *qp;
660 int err, nbytes;
Steven Whitehousee285c102009-09-23 13:50:49 +0100661 u64 size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000662
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100663 if (gfs2_is_stuffed(ip)) {
664 err = gfs2_unstuff_dinode(ip, NULL);
665 if (err)
666 return err;
667 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400668
669 memset(&q, 0, sizeof(struct gfs2_quota));
Andrew Price43066292012-04-16 16:40:55 +0100670 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
Abhijith Das7e619bc2010-05-07 17:50:18 -0400671 if (err < 0)
672 return err;
673
674 err = -EIO;
675 qp = &q;
676 qp->qu_value = be64_to_cpu(qp->qu_value);
677 qp->qu_value += change;
678 qp->qu_value = cpu_to_be64(qp->qu_value);
679 qd->qd_qb.qb_value = qp->qu_value;
680 if (fdq) {
681 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
Abhijith Das14870b42010-11-18 11:24:24 -0500682 qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400683 qd->qd_qb.qb_warn = qp->qu_warn;
684 }
685 if (fdq->d_fieldmask & FS_DQ_BHARD) {
Abhijith Das14870b42010-11-18 11:24:24 -0500686 qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400687 qd->qd_qb.qb_limit = qp->qu_limit;
688 }
Abhijith Das802ec9b2010-11-18 11:26:46 -0500689 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
690 qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
691 qd->qd_qb.qb_value = qp->qu_value;
692 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400693 }
694
695 /* Write the quota into the quota file on disk */
696 ptr = qp;
697 nbytes = sizeof(struct gfs2_quota);
698get_a_page:
Bob Peterson220cca22012-03-19 15:25:50 -0400699 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000700 if (!page)
701 return -ENOMEM;
702
703 blocksize = inode->i_sb->s_blocksize;
704 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
705
706 if (!page_has_buffers(page))
707 create_empty_buffers(page, blocksize, 0);
708
709 bh = page_buffers(page);
710 pos = blocksize;
711 while (offset >= pos) {
712 bh = bh->b_this_page;
713 iblock++;
714 pos += blocksize;
715 }
716
717 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600718 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000719 if (!buffer_mapped(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400720 goto unlock_out;
721 /* If it's a newly allocated disk block for quota, zero it */
Abhijith Das8b421602010-07-04 01:33:24 -0400722 if (buffer_new(bh))
723 zero_user(page, pos - blocksize, bh->b_size);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000724 }
725
726 if (PageUptodate(page))
727 set_buffer_uptodate(bh);
728
729 if (!buffer_uptodate(bh)) {
Steven Whitehouse20ed0532011-10-31 09:52:02 +0000730 ll_rw_block(READ | REQ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000731 wait_on_buffer(bh);
732 if (!buffer_uptodate(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400733 goto unlock_out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000734 }
735
736 gfs2_trans_add_bh(ip->i_gl, bh, 0);
737
Cong Wangd9349282011-11-25 23:14:30 +0800738 kaddr = kmap_atomic(page);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400739 if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
740 nbytes = PAGE_CACHE_SIZE - offset;
741 memcpy(kaddr + offset, ptr, nbytes);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000742 flush_dcache_page(page);
Cong Wangd9349282011-11-25 23:14:30 +0800743 kunmap_atomic(kaddr);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400744 unlock_page(page);
745 page_cache_release(page);
Steven Whitehousee285c102009-09-23 13:50:49 +0100746
Abhijith Das7e619bc2010-05-07 17:50:18 -0400747 /* If quota straddles page boundary, we need to update the rest of the
748 * quota at the beginning of the next page */
Abhijith Das8b421602010-07-04 01:33:24 -0400749 if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
Abhijith Das7e619bc2010-05-07 17:50:18 -0400750 ptr = ptr + nbytes;
751 nbytes = sizeof(struct gfs2_quota) - nbytes;
752 offset = 0;
753 index++;
754 goto get_a_page;
755 }
756
Steven Whitehousee285c102009-09-23 13:50:49 +0100757 size = loc + sizeof(struct gfs2_quota);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100758 if (size > inode->i_size)
Steven Whitehousee285c102009-09-23 13:50:49 +0100759 i_size_write(inode, size);
Steven Whitehousee285c102009-09-23 13:50:49 +0100760 inode->i_mtime = inode->i_atime = CURRENT_TIME;
Steven Whitehousee285c102009-09-23 13:50:49 +0100761 mark_inode_dirty(inode);
Bob Peterson500242a2012-05-15 14:51:54 -0400762 return 0;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100763
Abhijith Das7e619bc2010-05-07 17:50:18 -0400764unlock_out:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000765 unlock_page(page);
766 page_cache_release(page);
767 return err;
768}
769
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
771{
772 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400773 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 unsigned int data_blocks, ind_blocks;
775 struct gfs2_holder *ghs, i_gh;
776 unsigned int qx, x;
777 struct gfs2_quota_data *qd;
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100778 unsigned reserved;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000779 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600780 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 int error;
782
Bob Peterson0a305e42012-06-06 11:17:59 +0100783 error = gfs2_rs_alloc(ip);
784 if (error)
785 return error;
786
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
788 &data_blocks, &ind_blocks);
789
Josef Bacik16c5f062008-04-09 09:33:41 -0400790 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 if (!ghs)
792 return -ENOMEM;
793
794 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
Jan Kara56aa72d2012-09-05 16:55:11 -0400795 mutex_lock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 for (qx = 0; qx < num_qd; qx++) {
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100797 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 GL_NOCACHE, &ghs[qx]);
799 if (error)
800 goto out;
801 }
802
803 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
804 if (error)
805 goto out;
806
807 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 offset = qd2offset(qda[x]);
Bob Peterson461cb412010-06-24 19:21:20 -0400809 if (gfs2_write_alloc_required(ip, offset,
810 sizeof(struct gfs2_quota)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 nalloc++;
812 }
813
Abhijith Das20b95bf2008-03-06 17:43:52 -0600814 /*
815 * 1 blk for unstuffing inode if stuffed. We add this extra
816 * block to the reservation unconditionally. If the inode
817 * doesn't need unstuffing, the block will be released to the
818 * rgrp since it won't be allocated during the transaction
819 */
Abhijith Das7e619bc2010-05-07 17:50:18 -0400820 /* +3 in the end for unstuffing block, inode size update block
821 * and another block in case quota straddles page boundary and
822 * two blocks need to be updated instead of 1 */
823 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600824
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100825 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000826 error = gfs2_inplace_reserve(ip, reserved, 0);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600827 if (error)
828 goto out_alloc;
829
830 if (nalloc)
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100831 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600832
833 error = gfs2_trans_begin(sdp, blocks, 0);
834 if (error)
835 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836
837 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 qd = qda[x];
839 offset = qd2offset(qd);
Steven Whitehousee285c102009-09-23 13:50:49 +0100840 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000841 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 goto out_end_trans;
843
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 do_qc(qd, -qd->qd_change_sync);
Abhijith Das662e3a52011-03-08 10:40:42 -0500845 set_bit(QDF_REFRESH, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 }
847
848 error = 0;
849
Steven Whitehousea91ea692006-09-04 12:04:26 -0400850out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400852out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600853 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400854out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400856out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 while (qx--)
858 gfs2_glock_dq_uninit(&ghs[qx]);
Steven Whitehousee285c102009-09-23 13:50:49 +0100859 mutex_unlock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400861 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 return error;
863}
864
Steven Whitehousee285c102009-09-23 13:50:49 +0100865static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
866{
867 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
868 struct gfs2_quota q;
869 struct gfs2_quota_lvb *qlvb;
870 loff_t pos;
871 int error;
872
873 memset(&q, 0, sizeof(struct gfs2_quota));
874 pos = qd2offset(qd);
Andrew Price43066292012-04-16 16:40:55 +0100875 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
Steven Whitehousee285c102009-09-23 13:50:49 +0100876 if (error < 0)
877 return error;
878
David Teigland4e2f8842012-11-14 13:47:37 -0500879 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehousee285c102009-09-23 13:50:49 +0100880 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
881 qlvb->__pad = 0;
882 qlvb->qb_limit = q.qu_limit;
883 qlvb->qb_warn = q.qu_warn;
884 qlvb->qb_value = q.qu_value;
885 qd->qd_qb = *qlvb;
886
887 return 0;
888}
889
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
891 struct gfs2_holder *q_gh)
892{
893 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400894 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 struct gfs2_holder i_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 int error;
897
Steven Whitehousea91ea692006-09-04 12:04:26 -0400898restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
900 if (error)
901 return error;
902
David Teigland4e2f8842012-11-14 13:47:37 -0500903 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400905 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 gfs2_glock_dq_uninit(q_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100907 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
908 GL_NOCACHE, q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 if (error)
910 return error;
911
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400912 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 if (error)
914 goto fail;
915
Steven Whitehousee285c102009-09-23 13:50:49 +0100916 error = update_qd(sdp, qd);
917 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 goto fail_gunlock;
Steven Whitehousee285c102009-09-23 13:50:49 +0100919
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100921 gfs2_glock_dq_uninit(q_gh);
922 force_refresh = 0;
923 goto restart;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 }
925
926 return 0;
927
Steven Whitehousea91ea692006-09-04 12:04:26 -0400928fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400930fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 return error;
933}
934
Steven Whitehousecd915492006-09-04 12:49:07 -0400935int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400937 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Abhijith Das662e3a52011-03-08 10:40:42 -0500938 struct gfs2_quota_data *qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 unsigned int x;
940 int error = 0;
941
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100942 error = gfs2_quota_hold(ip, uid, gid);
943 if (error)
944 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945
946 if (capable(CAP_SYS_RESOURCE) ||
947 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
948 return 0;
949
Bob Peterson5407e242012-05-18 09:28:23 -0400950 sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
951 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952
Bob Peterson5407e242012-05-18 09:28:23 -0400953 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
Abhijith Das662e3a52011-03-08 10:40:42 -0500954 int force = NO_FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400955 qd = ip->i_res->rs_qa_qd[x];
Abhijith Das662e3a52011-03-08 10:40:42 -0500956 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
957 force = FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400958 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 if (error)
960 break;
961 }
962
963 if (!error)
964 set_bit(GIF_QD_LOCKED, &ip->i_flags);
965 else {
966 while (x--)
Bob Peterson5407e242012-05-18 09:28:23 -0400967 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 gfs2_quota_unhold(ip);
969 }
970
971 return error;
972}
973
974static int need_sync(struct gfs2_quota_data *qd)
975{
976 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
977 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400978 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 unsigned int num, den;
980 int do_sync = 1;
981
982 if (!qd->qd_qb.qb_limit)
983 return 0;
984
Steven Whitehouse22077f52009-01-08 14:28:42 +0000985 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 value = qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000987 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988
989 spin_lock(&gt->gt_spin);
990 num = gt->gt_quota_scale_num;
991 den = gt->gt_quota_scale_den;
992 spin_unlock(&gt->gt_spin);
993
994 if (value < 0)
995 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400996 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
997 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998 do_sync = 0;
999 else {
1000 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +01001001 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001002 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -04001003 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 do_sync = 0;
1005 }
1006
1007 return do_sync;
1008}
1009
1010void gfs2_quota_unlock(struct gfs2_inode *ip)
1011{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 struct gfs2_quota_data *qda[4];
1013 unsigned int count = 0;
1014 unsigned int x;
1015
1016 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1017 goto out;
1018
Bob Peterson5407e242012-05-18 09:28:23 -04001019 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 struct gfs2_quota_data *qd;
1021 int sync;
1022
Bob Peterson5407e242012-05-18 09:28:23 -04001023 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 sync = need_sync(qd);
1025
Bob Peterson5407e242012-05-18 09:28:23 -04001026 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
1028 if (sync && qd_trylock(qd))
1029 qda[count++] = qd;
1030 }
1031
1032 if (count) {
1033 do_sync(count, qda);
1034 for (x = 0; x < count; x++)
1035 qd_unlock(qda[x]);
1036 }
1037
Steven Whitehousea91ea692006-09-04 12:04:26 -04001038out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 gfs2_quota_unhold(ip);
1040}
1041
1042#define MAX_LINE 256
1043
1044static int print_message(struct gfs2_quota_data *qd, char *type)
1045{
1046 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001048 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
Steven Whitehouse02630a12006-07-03 11:20:06 -04001049 sdp->sd_fsname, type,
1050 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
1051 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052
1053 return 0;
1054}
1055
Steven Whitehousecd915492006-09-04 12:49:07 -04001056int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001057{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001058 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001060 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 unsigned int x;
1062 int error = 0;
1063
1064 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1065 return 0;
1066
1067 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1068 return 0;
1069
Bob Peterson5407e242012-05-18 09:28:23 -04001070 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1071 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072
1073 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1074 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
1075 continue;
1076
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001077 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse22077f52009-01-08 14:28:42 +00001078 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 value += qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +00001080 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081
Steven Whitehousecd915492006-09-04 12:49:07 -04001082 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 +00001083 print_message(qd, "exceeded");
Eric W. Biederman431f1972012-09-16 02:32:43 -07001084 quota_send_warning(make_kqid(&init_user_ns,
1085 test_bit(QDF_USER, &qd->qd_flags) ?
1086 USRQUOTA : GRPQUOTA,
1087 qd->qd_id),
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001088 sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1089
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 error = -EDQUOT;
1091 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001092 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001093 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001095 gfs2_tune_get(sdp,
1096 gt_quota_warn_period) * HZ)) {
Eric W. Biederman431f1972012-09-16 02:32:43 -07001097 quota_send_warning(make_kqid(&init_user_ns,
1098 test_bit(QDF_USER, &qd->qd_flags) ?
1099 USRQUOTA : GRPQUOTA,
1100 qd->qd_id),
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001101 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102 error = print_message(qd, "warning");
1103 qd->qd_last_warn = jiffies;
1104 }
1105 }
1106
1107 return error;
1108}
1109
Steven Whitehousecd915492006-09-04 12:49:07 -04001110void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1111 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 struct gfs2_quota_data *qd;
1114 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001116 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001118 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 return;
1120
Bob Peterson5407e242012-05-18 09:28:23 -04001121 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1122 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123
1124 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1125 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1126 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127 }
1128 }
1129}
1130
Jan Karaceed1722012-07-03 16:45:28 +02001131int gfs2_quota_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132{
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001133 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 struct gfs2_quota_data **qda;
1135 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1136 unsigned int num_qd;
1137 unsigned int x;
1138 int error = 0;
1139
1140 sdp->sd_quota_sync_gen++;
1141
1142 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1143 if (!qda)
1144 return -ENOMEM;
1145
1146 do {
1147 num_qd = 0;
1148
1149 for (;;) {
1150 error = qd_fish(sdp, qda + num_qd);
1151 if (error || !qda[num_qd])
1152 break;
1153 if (++num_qd == max_qd)
1154 break;
1155 }
1156
1157 if (num_qd) {
1158 if (!error)
1159 error = do_sync(num_qd, qda);
1160 if (!error)
1161 for (x = 0; x < num_qd; x++)
1162 qda[x]->qd_sync_gen =
1163 sdp->sd_quota_sync_gen;
1164
1165 for (x = 0; x < num_qd; x++)
1166 qd_unlock(qda[x]);
1167 }
1168 } while (!error && num_qd == max_qd);
1169
1170 kfree(qda);
1171
1172 return error;
1173}
1174
Christoph Hellwig5fb324a2010-02-16 03:44:52 -05001175static int gfs2_quota_sync_timeo(struct super_block *sb, int type)
1176{
Jan Karaceed1722012-07-03 16:45:28 +02001177 return gfs2_quota_sync(sb, type);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -05001178}
1179
Steven Whitehousecd915492006-09-04 12:49:07 -04001180int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181{
1182 struct gfs2_quota_data *qd;
1183 struct gfs2_holder q_gh;
1184 int error;
1185
Steven Whitehouse6a6ada82009-09-15 16:30:38 +01001186 error = qd_get(sdp, user, id, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001187 if (error)
1188 return error;
1189
1190 error = do_glock(qd, FORCE, &q_gh);
1191 if (!error)
1192 gfs2_glock_dq_uninit(&q_gh);
1193
1194 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 return error;
1196}
1197
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001198static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1199{
1200 const struct gfs2_quota_change *str = buf;
1201
1202 qc->qc_change = be64_to_cpu(str->qc_change);
1203 qc->qc_flags = be32_to_cpu(str->qc_flags);
Eric W. Biedermane08d8d72013-01-31 19:25:50 -08001204 qc->qc_id = make_kqid(&init_user_ns,
1205 (qc->qc_flags & GFS2_QCF_USER)?USRQUOTA:GRPQUOTA,
1206 be32_to_cpu(str->qc_id));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001207}
1208
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209int gfs2_quota_init(struct gfs2_sbd *sdp)
1210{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001211 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001212 u64 size = i_size_read(sdp->sd_qc_inode);
1213 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214 unsigned int x, slot = 0;
1215 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001216 u64 dblock;
1217 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001218 int error;
1219
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001220 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001221 return -EIO;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001222
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001224 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225
1226 error = -ENOMEM;
1227
1228 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
Josef Bacik16c5f062008-04-09 09:33:41 -04001229 sizeof(unsigned char *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001230 if (!sdp->sd_quota_bitmap)
1231 return error;
1232
1233 for (x = 0; x < sdp->sd_quota_chunks; x++) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001234 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 if (!sdp->sd_quota_bitmap[x])
1236 goto fail;
1237 }
1238
1239 for (x = 0; x < blocks; x++) {
1240 struct buffer_head *bh;
1241 unsigned int y;
1242
1243 if (!extlen) {
1244 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001245 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246 if (error)
1247 goto fail;
1248 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001250 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1251 if (!bh)
1252 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1254 brelse(bh);
1255 goto fail;
1256 }
1257
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001258 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259 y++, slot++) {
Al Virob62f9632006-10-13 23:46:46 -04001260 struct gfs2_quota_change_host qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261 struct gfs2_quota_data *qd;
1262
1263 gfs2_quota_change_in(&qc, bh->b_data +
1264 sizeof(struct gfs2_meta_header) +
1265 y * sizeof(struct gfs2_quota_change));
1266 if (!qc.qc_change)
1267 continue;
1268
1269 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
Eric W. Biedermane08d8d72013-01-31 19:25:50 -08001270 from_kqid(&init_user_ns, qc.qc_id), &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 if (error) {
1272 brelse(bh);
1273 goto fail;
1274 }
1275
1276 set_bit(QDF_CHANGE, &qd->qd_flags);
1277 qd->qd_change = qc.qc_change;
1278 qd->qd_slot = slot;
1279 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280
Abhijith Das0a7ab792009-01-07 16:03:37 -06001281 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1283 list_add(&qd->qd_list, &sdp->sd_quota_list);
1284 atomic_inc(&sdp->sd_quota_count);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001285 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286
1287 found++;
1288 }
1289
1290 brelse(bh);
1291 dblock++;
1292 extlen--;
1293 }
1294
1295 if (found)
1296 fs_info(sdp, "found %u quota changes\n", found);
1297
1298 return 0;
1299
Steven Whitehousea91ea692006-09-04 12:04:26 -04001300fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301 gfs2_quota_cleanup(sdp);
1302 return error;
1303}
1304
David Teiglandb3b94fa2006-01-16 16:50:04 +00001305void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1306{
1307 struct list_head *head = &sdp->sd_quota_list;
1308 struct gfs2_quota_data *qd;
1309 unsigned int x;
1310
Abhijith Das0a7ab792009-01-07 16:03:37 -06001311 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312 while (!list_empty(head)) {
1313 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1314
Abhijith Das0a7ab792009-01-07 16:03:37 -06001315 if (atomic_read(&qd->qd_count) > 1 ||
1316 (atomic_read(&qd->qd_count) &&
1317 !test_bit(QDF_CHANGE, &qd->qd_flags))) {
Abhijith Das0a7ab792009-01-07 16:03:37 -06001318 list_move(&qd->qd_list, head);
1319 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320 schedule();
Abhijith Das0a7ab792009-01-07 16:03:37 -06001321 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322 continue;
1323 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001324
Abhijith Das0a7ab792009-01-07 16:03:37 -06001325 list_del(&qd->qd_list);
1326 /* Also remove if this qd exists in the reclaim list */
1327 if (!list_empty(&qd->qd_reclaim)) {
1328 list_del_init(&qd->qd_reclaim);
1329 atomic_dec(&qd_lru_count);
1330 }
1331 atomic_dec(&sdp->sd_quota_count);
1332 spin_unlock(&qd_lru_lock);
1333
1334 if (!atomic_read(&qd->qd_count)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335 gfs2_assert_warn(sdp, !qd->qd_change);
1336 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1337 } else
1338 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1339 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1340
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001341 gfs2_glock_put(qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001342 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343
Abhijith Das0a7ab792009-01-07 16:03:37 -06001344 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001345 }
Abhijith Das0a7ab792009-01-07 16:03:37 -06001346 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001347
1348 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1349
1350 if (sdp->sd_quota_bitmap) {
1351 for (x = 0; x < sdp->sd_quota_chunks; x++)
1352 kfree(sdp->sd_quota_bitmap[x]);
1353 kfree(sdp->sd_quota_bitmap);
1354 }
1355}
1356
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001357static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1358{
1359 if (error == 0 || error == -EROFS)
1360 return;
1361 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1362 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1363}
1364
1365static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001366 int (*fxn)(struct super_block *sb, int type),
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001367 unsigned long t, unsigned long *timeo,
1368 unsigned int *new_timeo)
1369{
1370 if (t >= *timeo) {
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001371 int error = fxn(sdp->sd_vfs, 0);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001372 quotad_error(sdp, msg, error);
1373 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1374 } else {
1375 *timeo -= t;
1376 }
1377}
1378
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001379static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1380{
1381 struct gfs2_inode *ip;
1382
1383 while(1) {
1384 ip = NULL;
1385 spin_lock(&sdp->sd_trunc_lock);
1386 if (!list_empty(&sdp->sd_trunc_list)) {
1387 ip = list_entry(sdp->sd_trunc_list.next,
1388 struct gfs2_inode, i_trunc_list);
1389 list_del_init(&ip->i_trunc_list);
1390 }
1391 spin_unlock(&sdp->sd_trunc_lock);
1392 if (ip == NULL)
1393 return;
1394 gfs2_glock_finish_truncate(ip);
1395 }
1396}
1397
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001398void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1399 if (!sdp->sd_statfs_force_sync) {
1400 sdp->sd_statfs_force_sync = 1;
1401 wake_up(&sdp->sd_quota_wait);
1402 }
1403}
1404
1405
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001406/**
1407 * gfs2_quotad - Write cached quota changes into the quota file
1408 * @sdp: Pointer to GFS2 superblock
1409 *
1410 */
1411
1412int gfs2_quotad(void *data)
1413{
1414 struct gfs2_sbd *sdp = data;
1415 struct gfs2_tune *tune = &sdp->sd_tune;
1416 unsigned long statfs_timeo = 0;
1417 unsigned long quotad_timeo = 0;
1418 unsigned long t = 0;
1419 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001420 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001421
1422 while (!kthread_should_stop()) {
1423
1424 /* Update the master statfs file */
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001425 if (sdp->sd_statfs_force_sync) {
1426 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1427 quotad_error(sdp, "statfs", error);
1428 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1429 }
1430 else
1431 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1432 &statfs_timeo,
1433 &tune->gt_statfs_quantum);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001434
1435 /* Update quota file */
Christoph Hellwig5fb324a2010-02-16 03:44:52 -05001436 quotad_check_timeo(sdp, "sync", gfs2_quota_sync_timeo, t,
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001437 &quotad_timeo, &tune->gt_quota_quantum);
1438
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001439 /* Check for & recover partially truncated inodes */
1440 quotad_check_trunc_list(sdp);
1441
Tejun Heoa0acae02011-11-21 12:32:22 -08001442 try_to_freeze();
1443
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001444 t = min(quotad_timeo, statfs_timeo);
1445
Steven Whitehouse7fa5d202009-03-31 15:49:08 +01001446 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001447 spin_lock(&sdp->sd_trunc_lock);
1448 empty = list_empty(&sdp->sd_trunc_list);
1449 spin_unlock(&sdp->sd_trunc_lock);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001450 if (empty && !sdp->sd_statfs_force_sync)
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001451 t -= schedule_timeout(t);
1452 else
1453 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001454 finish_wait(&sdp->sd_quota_wait, &wait);
1455 }
1456
1457 return 0;
1458}
1459
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001460static int gfs2_quota_get_xstate(struct super_block *sb,
1461 struct fs_quota_stat *fqs)
1462{
1463 struct gfs2_sbd *sdp = sb->s_fs_info;
1464
1465 memset(fqs, 0, sizeof(struct fs_quota_stat));
1466 fqs->qs_version = FS_QSTAT_VERSION;
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001467
1468 switch (sdp->sd_args.ar_quota) {
1469 case GFS2_QUOTA_ON:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001470 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001471 /*FALLTHRU*/
1472 case GFS2_QUOTA_ACCOUNT:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001473 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001474 break;
1475 case GFS2_QUOTA_OFF:
1476 break;
1477 }
1478
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001479 if (sdp->sd_quota_inode) {
1480 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1481 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1482 }
1483 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1484 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
1485 fqs->qs_incoredqs = atomic_read(&qd_lru_count);
1486 return 0;
1487}
1488
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001489static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001490 struct fs_disk_quota *fdq)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001491{
1492 struct gfs2_sbd *sdp = sb->s_fs_info;
1493 struct gfs2_quota_lvb *qlvb;
1494 struct gfs2_quota_data *qd;
1495 struct gfs2_holder q_gh;
1496 int error;
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001497 int type;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001498
1499 memset(fdq, 0, sizeof(struct fs_disk_quota));
1500
1501 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1502 return -ESRCH; /* Crazy XFS error code */
1503
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001504 if (qid.type == USRQUOTA)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001505 type = QUOTA_USER;
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001506 else if (qid.type == GRPQUOTA)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001507 type = QUOTA_GROUP;
1508 else
1509 return -EINVAL;
1510
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001511 error = qd_get(sdp, type, from_kqid(&init_user_ns, qid), &qd);
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001512 if (error)
1513 return error;
1514 error = do_glock(qd, FORCE, &q_gh);
1515 if (error)
1516 goto out;
1517
David Teigland4e2f8842012-11-14 13:47:37 -05001518 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001519 fdq->d_version = FS_DQUOT_VERSION;
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001520 fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
Eric W. Biederman558e8522013-01-31 18:15:33 -08001521 fdq->d_id = from_kqid_munged(current_user_ns(), qid);
Abhijith Das14870b42010-11-18 11:24:24 -05001522 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1523 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1524 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001525
1526 gfs2_glock_dq_uninit(&q_gh);
1527out:
1528 qd_put(qd);
1529 return error;
1530}
1531
Steven Whitehousee285c102009-09-23 13:50:49 +01001532/* GFS2 only supports a subset of the XFS fields */
Abhijith Das802ec9b2010-11-18 11:26:46 -05001533#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
Steven Whitehousee285c102009-09-23 13:50:49 +01001534
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001535static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001536 struct fs_disk_quota *fdq)
Steven Whitehousee285c102009-09-23 13:50:49 +01001537{
1538 struct gfs2_sbd *sdp = sb->s_fs_info;
1539 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1540 struct gfs2_quota_data *qd;
1541 struct gfs2_holder q_gh, i_gh;
1542 unsigned int data_blocks, ind_blocks;
1543 unsigned int blocks = 0;
1544 int alloc_required;
Steven Whitehousee285c102009-09-23 13:50:49 +01001545 loff_t offset;
1546 int error;
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001547 int type;
Steven Whitehousee285c102009-09-23 13:50:49 +01001548
1549 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1550 return -ESRCH; /* Crazy XFS error code */
1551
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001552 switch(qid.type) {
Steven Whitehousee285c102009-09-23 13:50:49 +01001553 case USRQUOTA:
1554 type = QUOTA_USER;
Steven Whitehousee285c102009-09-23 13:50:49 +01001555 break;
1556 case GRPQUOTA:
1557 type = QUOTA_GROUP;
Steven Whitehousee285c102009-09-23 13:50:49 +01001558 break;
1559 default:
1560 return -EINVAL;
1561 }
1562
1563 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1564 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001565
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001566 error = qd_get(sdp, type, from_kqid(&init_user_ns, 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 Whitehousee285c102009-09-23 13:50:49 +01001608 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1609 &data_blocks, &ind_blocks);
Bob Peterson564e12b2011-11-21 13:36:17 -05001610 blocks = 1 + data_blocks + ind_blocks;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +00001611 error = gfs2_inplace_reserve(ip, blocks, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001612 if (error)
Bob Peterson564e12b2011-11-21 13:36:17 -05001613 goto out_i;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001614 blocks += gfs2_rg_blocks(ip, blocks);
Steven Whitehousee285c102009-09-23 13:50:49 +01001615 }
1616
Abhijith Dase79a46a2011-02-07 11:22:41 -05001617 /* Some quotas span block boundaries and can update two blocks,
1618 adding an extra block to the transaction to handle such quotas */
1619 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001620 if (error)
1621 goto out_release;
1622
1623 /* Apply changes */
1624 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1625
1626 gfs2_trans_end(sdp);
1627out_release:
Bob Peterson564e12b2011-11-21 13:36:17 -05001628 if (alloc_required)
Steven Whitehousee285c102009-09-23 13:50:49 +01001629 gfs2_inplace_release(ip);
Steven Whitehousee285c102009-09-23 13:50:49 +01001630out_i:
1631 gfs2_glock_dq_uninit(&i_gh);
1632out_q:
1633 gfs2_glock_dq_uninit(&q_gh);
Bob Peterson0a305e42012-06-06 11:17:59 +01001634out_unlockput:
Steven Whitehousee285c102009-09-23 13:50:49 +01001635 mutex_unlock(&ip->i_inode.i_mutex);
Bob Peterson0a305e42012-06-06 11:17:59 +01001636out_put:
Steven Whitehousee285c102009-09-23 13:50:49 +01001637 qd_put(qd);
1638 return error;
1639}
1640
Steven Whitehousecc632e72009-09-15 09:59:02 +01001641const struct quotactl_ops gfs2_quotactl_ops = {
1642 .quota_sync = gfs2_quota_sync,
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001643 .get_xstate = gfs2_quota_get_xstate,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001644 .get_dqblk = gfs2_get_dqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001645 .set_dqblk = gfs2_set_dqblk,
Steven Whitehousecc632e72009-09-15 09:59:02 +01001646};