blob: 02913e95491cab4c93e1adf004dc7351fc3f862b [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
Steven Whitehouse33a82522009-09-15 16:25:40 +0100467static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 struct gfs2_quota_data **qdp)
469{
470 int error;
471
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100472 error = qd_get(sdp, user, id, qdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473 if (error)
474 return error;
475
476 error = slot_get(*qdp);
477 if (error)
478 goto fail;
479
480 error = bh_get(*qdp);
481 if (error)
482 goto fail_slot;
483
484 return 0;
485
Steven Whitehousea91ea692006-09-04 12:04:26 -0400486fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400488fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 qd_put(*qdp);
490 return error;
491}
492
493static void qdsb_put(struct gfs2_quota_data *qd)
494{
495 bh_put(qd);
496 slot_put(qd);
497 qd_put(qd);
498}
499
Steven Whitehousecd915492006-09-04 12:49:07 -0400500int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400502 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson5407e242012-05-18 09:28:23 -0400503 struct gfs2_quota_data **qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504 int error;
505
Andrew Priceaaaf68c2012-10-12 16:45:08 +0100506 if (ip->i_res == NULL) {
507 error = gfs2_rs_alloc(ip);
508 if (error)
509 return error;
510 }
Bob Peterson5407e242012-05-18 09:28:23 -0400511
512 qd = ip->i_res->rs_qa_qd;
513
514 if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
516 return -EIO;
517
518 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
519 return 0;
520
Steven Whitehouse33a82522009-09-15 16:25:40 +0100521 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 if (error)
523 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400524 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 qd++;
526
Steven Whitehouse33a82522009-09-15 16:25:40 +0100527 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 if (error)
529 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400530 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 qd++;
532
Eric W. Biedermanf4108a62013-01-31 17:49:26 -0800533 if (uid != NO_UID_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
Steven Whitehouse33a82522009-09-15 16:25:40 +0100534 error = qdsb_get(sdp, QUOTA_USER, uid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535 if (error)
536 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400537 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 qd++;
539 }
540
Eric W. Biedermanf4108a62013-01-31 17:49:26 -0800541 if (gid != NO_GID_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
Steven Whitehouse33a82522009-09-15 16:25:40 +0100542 error = qdsb_get(sdp, QUOTA_GROUP, gid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 if (error)
544 goto out;
Bob Peterson5407e242012-05-18 09:28:23 -0400545 ip->i_res->rs_qa_qd_num++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 qd++;
547 }
548
Steven Whitehousea91ea692006-09-04 12:04:26 -0400549out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 if (error)
551 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 return error;
553}
554
555void gfs2_quota_unhold(struct gfs2_inode *ip)
556{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400557 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 unsigned int x;
559
Bob Peterson5407e242012-05-18 09:28:23 -0400560 if (ip->i_res == NULL)
561 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
563
Bob Peterson5407e242012-05-18 09:28:23 -0400564 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
565 qdsb_put(ip->i_res->rs_qa_qd[x]);
566 ip->i_res->rs_qa_qd[x] = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567 }
Bob Peterson5407e242012-05-18 09:28:23 -0400568 ip->i_res->rs_qa_qd_num = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569}
570
571static int sort_qd(const void *a, const void *b)
572{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400573 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
574 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575
576 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
577 !test_bit(QDF_USER, &qd_b->qd_flags)) {
578 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400579 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400581 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400583 if (qd_a->qd_id < qd_b->qd_id)
584 return -1;
585 if (qd_a->qd_id > qd_b->qd_id)
586 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587
Steven Whitehouse48fac172006-09-05 15:17:12 -0400588 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589}
590
Steven Whitehousecd915492006-09-04 12:49:07 -0400591static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592{
593 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400594 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400596 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597
Steven Whitehousef55ab262006-02-21 12:51:39 +0000598 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000599 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600
601 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
602 qc->qc_change = 0;
603 qc->qc_flags = 0;
604 if (test_bit(QDF_USER, &qd->qd_flags))
605 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
606 qc->qc_id = cpu_to_be32(qd->qd_id);
607 }
608
Al Virob44b84d2006-10-14 10:46:30 -0400609 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 qc->qc_change = cpu_to_be64(x);
611
Steven Whitehouse22077f52009-01-08 14:28:42 +0000612 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 qd->qd_change = x;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000614 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615
616 if (!x) {
617 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
618 clear_bit(QDF_CHANGE, &qd->qd_flags);
619 qc->qc_flags = 0;
620 qc->qc_id = 0;
621 slot_put(qd);
622 qd_put(qd);
623 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
624 qd_hold(qd);
625 slot_hold(qd);
626 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400627
Steven Whitehousef55ab262006-02-21 12:51:39 +0000628 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629}
630
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000631/**
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100632 * gfs2_adjust_quota - adjust record of current block usage
633 * @ip: The quota inode
634 * @loc: Offset of the entry in the quota file
Steven Whitehousee285c102009-09-23 13:50:49 +0100635 * @change: The amount of usage change to record
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100636 * @qd: The quota data
Steven Whitehousee285c102009-09-23 13:50:49 +0100637 * @fdq: The updated limits to record
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000638 *
639 * This function was mostly borrowed from gfs2_block_truncate_page which was
640 * in turn mostly borrowed from ext3
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100641 *
642 * Returns: 0 or -ve on error
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000643 */
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100644
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000645static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousee285c102009-09-23 13:50:49 +0100646 s64 change, struct gfs2_quota_data *qd,
647 struct fs_disk_quota *fdq)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000648{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400649 struct inode *inode = &ip->i_inode;
Abhijith Das14870b42010-11-18 11:24:24 -0500650 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000651 struct address_space *mapping = inode->i_mapping;
652 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500653 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000654 unsigned blocksize, iblock, pos;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100655 struct buffer_head *bh;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000656 struct page *page;
Abhijith Das7e619bc2010-05-07 17:50:18 -0400657 void *kaddr, *ptr;
658 struct gfs2_quota q, *qp;
659 int err, nbytes;
Steven Whitehousee285c102009-09-23 13:50:49 +0100660 u64 size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000661
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100662 if (gfs2_is_stuffed(ip)) {
663 err = gfs2_unstuff_dinode(ip, NULL);
664 if (err)
665 return err;
666 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400667
668 memset(&q, 0, sizeof(struct gfs2_quota));
Andrew Price43066292012-04-16 16:40:55 +0100669 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
Abhijith Das7e619bc2010-05-07 17:50:18 -0400670 if (err < 0)
671 return err;
672
673 err = -EIO;
674 qp = &q;
675 qp->qu_value = be64_to_cpu(qp->qu_value);
676 qp->qu_value += change;
677 qp->qu_value = cpu_to_be64(qp->qu_value);
678 qd->qd_qb.qb_value = qp->qu_value;
679 if (fdq) {
680 if (fdq->d_fieldmask & FS_DQ_BSOFT) {
Abhijith Das14870b42010-11-18 11:24:24 -0500681 qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400682 qd->qd_qb.qb_warn = qp->qu_warn;
683 }
684 if (fdq->d_fieldmask & FS_DQ_BHARD) {
Abhijith Das14870b42010-11-18 11:24:24 -0500685 qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400686 qd->qd_qb.qb_limit = qp->qu_limit;
687 }
Abhijith Das802ec9b2010-11-18 11:26:46 -0500688 if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
689 qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
690 qd->qd_qb.qb_value = qp->qu_value;
691 }
Abhijith Das7e619bc2010-05-07 17:50:18 -0400692 }
693
694 /* Write the quota into the quota file on disk */
695 ptr = qp;
696 nbytes = sizeof(struct gfs2_quota);
697get_a_page:
Bob Peterson220cca22012-03-19 15:25:50 -0400698 page = find_or_create_page(mapping, index, GFP_NOFS);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000699 if (!page)
700 return -ENOMEM;
701
702 blocksize = inode->i_sb->s_blocksize;
703 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
704
705 if (!page_has_buffers(page))
706 create_empty_buffers(page, blocksize, 0);
707
708 bh = page_buffers(page);
709 pos = blocksize;
710 while (offset >= pos) {
711 bh = bh->b_this_page;
712 iblock++;
713 pos += blocksize;
714 }
715
716 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600717 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000718 if (!buffer_mapped(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400719 goto unlock_out;
720 /* If it's a newly allocated disk block for quota, zero it */
Abhijith Das8b421602010-07-04 01:33:24 -0400721 if (buffer_new(bh))
722 zero_user(page, pos - blocksize, bh->b_size);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000723 }
724
725 if (PageUptodate(page))
726 set_buffer_uptodate(bh);
727
728 if (!buffer_uptodate(bh)) {
Steven Whitehouse20ed0532011-10-31 09:52:02 +0000729 ll_rw_block(READ | REQ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000730 wait_on_buffer(bh);
731 if (!buffer_uptodate(bh))
Abhijith Das7e619bc2010-05-07 17:50:18 -0400732 goto unlock_out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000733 }
734
735 gfs2_trans_add_bh(ip->i_gl, bh, 0);
736
Cong Wangd9349282011-11-25 23:14:30 +0800737 kaddr = kmap_atomic(page);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400738 if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
739 nbytes = PAGE_CACHE_SIZE - offset;
740 memcpy(kaddr + offset, ptr, nbytes);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000741 flush_dcache_page(page);
Cong Wangd9349282011-11-25 23:14:30 +0800742 kunmap_atomic(kaddr);
Abhijith Das7e619bc2010-05-07 17:50:18 -0400743 unlock_page(page);
744 page_cache_release(page);
Steven Whitehousee285c102009-09-23 13:50:49 +0100745
Abhijith Das7e619bc2010-05-07 17:50:18 -0400746 /* If quota straddles page boundary, we need to update the rest of the
747 * quota at the beginning of the next page */
Abhijith Das8b421602010-07-04 01:33:24 -0400748 if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
Abhijith Das7e619bc2010-05-07 17:50:18 -0400749 ptr = ptr + nbytes;
750 nbytes = sizeof(struct gfs2_quota) - nbytes;
751 offset = 0;
752 index++;
753 goto get_a_page;
754 }
755
Steven Whitehousee285c102009-09-23 13:50:49 +0100756 size = loc + sizeof(struct gfs2_quota);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100757 if (size > inode->i_size)
Steven Whitehousee285c102009-09-23 13:50:49 +0100758 i_size_write(inode, size);
Steven Whitehousee285c102009-09-23 13:50:49 +0100759 inode->i_mtime = inode->i_atime = CURRENT_TIME;
Steven Whitehousee285c102009-09-23 13:50:49 +0100760 mark_inode_dirty(inode);
Bob Peterson500242a2012-05-15 14:51:54 -0400761 return 0;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100762
Abhijith Das7e619bc2010-05-07 17:50:18 -0400763unlock_out:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000764 unlock_page(page);
765 page_cache_release(page);
766 return err;
767}
768
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
770{
771 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400772 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 unsigned int data_blocks, ind_blocks;
774 struct gfs2_holder *ghs, i_gh;
775 unsigned int qx, x;
776 struct gfs2_quota_data *qd;
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100777 unsigned reserved;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000778 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600779 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 int error;
781
Bob Peterson0a305e42012-06-06 11:17:59 +0100782 error = gfs2_rs_alloc(ip);
783 if (error)
784 return error;
785
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
787 &data_blocks, &ind_blocks);
788
Josef Bacik16c5f062008-04-09 09:33:41 -0400789 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 if (!ghs)
791 return -ENOMEM;
792
793 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
Jan Kara56aa72d2012-09-05 16:55:11 -0400794 mutex_lock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 for (qx = 0; qx < num_qd; qx++) {
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100796 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 GL_NOCACHE, &ghs[qx]);
798 if (error)
799 goto out;
800 }
801
802 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
803 if (error)
804 goto out;
805
806 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 offset = qd2offset(qda[x]);
Bob Peterson461cb412010-06-24 19:21:20 -0400808 if (gfs2_write_alloc_required(ip, offset,
809 sizeof(struct gfs2_quota)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 nalloc++;
811 }
812
Abhijith Das20b95bf2008-03-06 17:43:52 -0600813 /*
814 * 1 blk for unstuffing inode if stuffed. We add this extra
815 * block to the reservation unconditionally. If the inode
816 * doesn't need unstuffing, the block will be released to the
817 * rgrp since it won't be allocated during the transaction
818 */
Abhijith Das7e619bc2010-05-07 17:50:18 -0400819 /* +3 in the end for unstuffing block, inode size update block
820 * and another block in case quota straddles page boundary and
821 * two blocks need to be updated instead of 1 */
822 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600823
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100824 reserved = 1 + (nalloc * (data_blocks + ind_blocks));
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000825 error = gfs2_inplace_reserve(ip, reserved, 0);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600826 if (error)
827 goto out_alloc;
828
829 if (nalloc)
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100830 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600831
832 error = gfs2_trans_begin(sdp, blocks, 0);
833 if (error)
834 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835
836 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837 qd = qda[x];
838 offset = qd2offset(qd);
Steven Whitehousee285c102009-09-23 13:50:49 +0100839 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000840 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 goto out_end_trans;
842
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 do_qc(qd, -qd->qd_change_sync);
Abhijith Das662e3a52011-03-08 10:40:42 -0500844 set_bit(QDF_REFRESH, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 }
846
847 error = 0;
848
Steven Whitehousea91ea692006-09-04 12:04:26 -0400849out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400851out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600852 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400853out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400855out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 while (qx--)
857 gfs2_glock_dq_uninit(&ghs[qx]);
Steven Whitehousee285c102009-09-23 13:50:49 +0100858 mutex_unlock(&ip->i_inode.i_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400860 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 return error;
862}
863
Steven Whitehousee285c102009-09-23 13:50:49 +0100864static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
865{
866 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
867 struct gfs2_quota q;
868 struct gfs2_quota_lvb *qlvb;
869 loff_t pos;
870 int error;
871
872 memset(&q, 0, sizeof(struct gfs2_quota));
873 pos = qd2offset(qd);
Andrew Price43066292012-04-16 16:40:55 +0100874 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
Steven Whitehousee285c102009-09-23 13:50:49 +0100875 if (error < 0)
876 return error;
877
David Teigland4e2f8842012-11-14 13:47:37 -0500878 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehousee285c102009-09-23 13:50:49 +0100879 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
880 qlvb->__pad = 0;
881 qlvb->qb_limit = q.qu_limit;
882 qlvb->qb_warn = q.qu_warn;
883 qlvb->qb_value = q.qu_value;
884 qd->qd_qb = *qlvb;
885
886 return 0;
887}
888
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
890 struct gfs2_holder *q_gh)
891{
892 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400893 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 struct gfs2_holder i_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 int error;
896
Steven Whitehousea91ea692006-09-04 12:04:26 -0400897restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
899 if (error)
900 return error;
901
David Teigland4e2f8842012-11-14 13:47:37 -0500902 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400904 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 gfs2_glock_dq_uninit(q_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100906 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
907 GL_NOCACHE, q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 if (error)
909 return error;
910
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400911 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 if (error)
913 goto fail;
914
Steven Whitehousee285c102009-09-23 13:50:49 +0100915 error = update_qd(sdp, qd);
916 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 goto fail_gunlock;
Steven Whitehousee285c102009-09-23 13:50:49 +0100918
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100920 gfs2_glock_dq_uninit(q_gh);
921 force_refresh = 0;
922 goto restart;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 }
924
925 return 0;
926
Steven Whitehousea91ea692006-09-04 12:04:26 -0400927fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400929fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 return error;
932}
933
Steven Whitehousecd915492006-09-04 12:49:07 -0400934int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400936 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Abhijith Das662e3a52011-03-08 10:40:42 -0500937 struct gfs2_quota_data *qd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 unsigned int x;
939 int error = 0;
940
Steven Whitehouse891a8e92011-09-19 10:25:49 +0100941 error = gfs2_quota_hold(ip, uid, gid);
942 if (error)
943 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944
945 if (capable(CAP_SYS_RESOURCE) ||
946 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
947 return 0;
948
Bob Peterson5407e242012-05-18 09:28:23 -0400949 sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
950 sizeof(struct gfs2_quota_data *), sort_qd, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951
Bob Peterson5407e242012-05-18 09:28:23 -0400952 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
Abhijith Das662e3a52011-03-08 10:40:42 -0500953 int force = NO_FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400954 qd = ip->i_res->rs_qa_qd[x];
Abhijith Das662e3a52011-03-08 10:40:42 -0500955 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
956 force = FORCE;
Bob Peterson5407e242012-05-18 09:28:23 -0400957 error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 if (error)
959 break;
960 }
961
962 if (!error)
963 set_bit(GIF_QD_LOCKED, &ip->i_flags);
964 else {
965 while (x--)
Bob Peterson5407e242012-05-18 09:28:23 -0400966 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 gfs2_quota_unhold(ip);
968 }
969
970 return error;
971}
972
973static int need_sync(struct gfs2_quota_data *qd)
974{
975 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
976 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400977 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 unsigned int num, den;
979 int do_sync = 1;
980
981 if (!qd->qd_qb.qb_limit)
982 return 0;
983
Steven Whitehouse22077f52009-01-08 14:28:42 +0000984 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 value = qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000986 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987
988 spin_lock(&gt->gt_spin);
989 num = gt->gt_quota_scale_num;
990 den = gt->gt_quota_scale_den;
991 spin_unlock(&gt->gt_spin);
992
993 if (value < 0)
994 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400995 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
996 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 do_sync = 0;
998 else {
999 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +01001000 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001001 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -04001002 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 do_sync = 0;
1004 }
1005
1006 return do_sync;
1007}
1008
1009void gfs2_quota_unlock(struct gfs2_inode *ip)
1010{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 struct gfs2_quota_data *qda[4];
1012 unsigned int count = 0;
1013 unsigned int x;
1014
1015 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1016 goto out;
1017
Bob Peterson5407e242012-05-18 09:28:23 -04001018 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 struct gfs2_quota_data *qd;
1020 int sync;
1021
Bob Peterson5407e242012-05-18 09:28:23 -04001022 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023 sync = need_sync(qd);
1024
Bob Peterson5407e242012-05-18 09:28:23 -04001025 gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026
1027 if (sync && qd_trylock(qd))
1028 qda[count++] = qd;
1029 }
1030
1031 if (count) {
1032 do_sync(count, qda);
1033 for (x = 0; x < count; x++)
1034 qd_unlock(qda[x]);
1035 }
1036
Steven Whitehousea91ea692006-09-04 12:04:26 -04001037out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 gfs2_quota_unhold(ip);
1039}
1040
1041#define MAX_LINE 256
1042
1043static int print_message(struct gfs2_quota_data *qd, char *type)
1044{
1045 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001047 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
Steven Whitehouse02630a12006-07-03 11:20:06 -04001048 sdp->sd_fsname, type,
1049 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
1050 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051
1052 return 0;
1053}
1054
Steven Whitehousecd915492006-09-04 12:49:07 -04001055int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001057 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001059 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 unsigned int x;
1061 int error = 0;
1062
1063 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1064 return 0;
1065
1066 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1067 return 0;
1068
Bob Peterson5407e242012-05-18 09:28:23 -04001069 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1070 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071
1072 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1073 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
1074 continue;
1075
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001076 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse22077f52009-01-08 14:28:42 +00001077 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001078 value += qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +00001079 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080
Steven Whitehousecd915492006-09-04 12:49:07 -04001081 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 +00001082 print_message(qd, "exceeded");
Eric W. Biederman431f1972012-09-16 02:32:43 -07001083 quota_send_warning(make_kqid(&init_user_ns,
1084 test_bit(QDF_USER, &qd->qd_flags) ?
1085 USRQUOTA : GRPQUOTA,
1086 qd->qd_id),
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001087 sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
1088
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089 error = -EDQUOT;
1090 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001091 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001092 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001094 gfs2_tune_get(sdp,
1095 gt_quota_warn_period) * HZ)) {
Eric W. Biederman431f1972012-09-16 02:32:43 -07001096 quota_send_warning(make_kqid(&init_user_ns,
1097 test_bit(QDF_USER, &qd->qd_flags) ?
1098 USRQUOTA : GRPQUOTA,
1099 qd->qd_id),
Steven Whitehouse2ec46502009-09-28 12:49:15 +01001100 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 error = print_message(qd, "warning");
1102 qd->qd_last_warn = jiffies;
1103 }
1104 }
1105
1106 return error;
1107}
1108
Steven Whitehousecd915492006-09-04 12:49:07 -04001109void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1110 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112 struct gfs2_quota_data *qd;
1113 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001115 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001117 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 return;
1119
Bob Peterson5407e242012-05-18 09:28:23 -04001120 for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
1121 qd = ip->i_res->rs_qa_qd[x];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122
1123 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1124 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1125 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 }
1127 }
1128}
1129
Jan Karaceed1722012-07-03 16:45:28 +02001130int gfs2_quota_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131{
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001132 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 struct gfs2_quota_data **qda;
1134 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1135 unsigned int num_qd;
1136 unsigned int x;
1137 int error = 0;
1138
1139 sdp->sd_quota_sync_gen++;
1140
1141 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1142 if (!qda)
1143 return -ENOMEM;
1144
1145 do {
1146 num_qd = 0;
1147
1148 for (;;) {
1149 error = qd_fish(sdp, qda + num_qd);
1150 if (error || !qda[num_qd])
1151 break;
1152 if (++num_qd == max_qd)
1153 break;
1154 }
1155
1156 if (num_qd) {
1157 if (!error)
1158 error = do_sync(num_qd, qda);
1159 if (!error)
1160 for (x = 0; x < num_qd; x++)
1161 qda[x]->qd_sync_gen =
1162 sdp->sd_quota_sync_gen;
1163
1164 for (x = 0; x < num_qd; x++)
1165 qd_unlock(qda[x]);
1166 }
1167 } while (!error && num_qd == max_qd);
1168
1169 kfree(qda);
1170
1171 return error;
1172}
1173
Christoph Hellwig5fb324a2010-02-16 03:44:52 -05001174static int gfs2_quota_sync_timeo(struct super_block *sb, int type)
1175{
Jan Karaceed1722012-07-03 16:45:28 +02001176 return gfs2_quota_sync(sb, type);
Christoph Hellwig5fb324a2010-02-16 03:44:52 -05001177}
1178
Steven Whitehousecd915492006-09-04 12:49:07 -04001179int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180{
1181 struct gfs2_quota_data *qd;
1182 struct gfs2_holder q_gh;
1183 int error;
1184
Steven Whitehouse6a6ada82009-09-15 16:30:38 +01001185 error = qd_get(sdp, user, id, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 if (error)
1187 return error;
1188
1189 error = do_glock(qd, FORCE, &q_gh);
1190 if (!error)
1191 gfs2_glock_dq_uninit(&q_gh);
1192
1193 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 return error;
1195}
1196
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001197static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1198{
1199 const struct gfs2_quota_change *str = buf;
1200
1201 qc->qc_change = be64_to_cpu(str->qc_change);
1202 qc->qc_flags = be32_to_cpu(str->qc_flags);
Eric W. Biedermane08d8d72013-01-31 19:25:50 -08001203 qc->qc_id = make_kqid(&init_user_ns,
1204 (qc->qc_flags & GFS2_QCF_USER)?USRQUOTA:GRPQUOTA,
1205 be32_to_cpu(str->qc_id));
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001206}
1207
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208int gfs2_quota_init(struct gfs2_sbd *sdp)
1209{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001210 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001211 u64 size = i_size_read(sdp->sd_qc_inode);
1212 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 unsigned int x, slot = 0;
1214 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001215 u64 dblock;
1216 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 int error;
1218
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001219 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001220 return -EIO;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001221
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001223 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224
1225 error = -ENOMEM;
1226
1227 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
Josef Bacik16c5f062008-04-09 09:33:41 -04001228 sizeof(unsigned char *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229 if (!sdp->sd_quota_bitmap)
1230 return error;
1231
1232 for (x = 0; x < sdp->sd_quota_chunks; x++) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001233 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001234 if (!sdp->sd_quota_bitmap[x])
1235 goto fail;
1236 }
1237
1238 for (x = 0; x < blocks; x++) {
1239 struct buffer_head *bh;
1240 unsigned int y;
1241
1242 if (!extlen) {
1243 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001244 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001245 if (error)
1246 goto fail;
1247 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001249 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1250 if (!bh)
1251 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1253 brelse(bh);
1254 goto fail;
1255 }
1256
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001257 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 y++, slot++) {
Al Virob62f9632006-10-13 23:46:46 -04001259 struct gfs2_quota_change_host qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260 struct gfs2_quota_data *qd;
1261
1262 gfs2_quota_change_in(&qc, bh->b_data +
1263 sizeof(struct gfs2_meta_header) +
1264 y * sizeof(struct gfs2_quota_change));
1265 if (!qc.qc_change)
1266 continue;
1267
1268 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
Eric W. Biedermane08d8d72013-01-31 19:25:50 -08001269 from_kqid(&init_user_ns, qc.qc_id), &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270 if (error) {
1271 brelse(bh);
1272 goto fail;
1273 }
1274
1275 set_bit(QDF_CHANGE, &qd->qd_flags);
1276 qd->qd_change = qc.qc_change;
1277 qd->qd_slot = slot;
1278 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279
Abhijith Das0a7ab792009-01-07 16:03:37 -06001280 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1282 list_add(&qd->qd_list, &sdp->sd_quota_list);
1283 atomic_inc(&sdp->sd_quota_count);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001284 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285
1286 found++;
1287 }
1288
1289 brelse(bh);
1290 dblock++;
1291 extlen--;
1292 }
1293
1294 if (found)
1295 fs_info(sdp, "found %u quota changes\n", found);
1296
1297 return 0;
1298
Steven Whitehousea91ea692006-09-04 12:04:26 -04001299fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 gfs2_quota_cleanup(sdp);
1301 return error;
1302}
1303
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1305{
1306 struct list_head *head = &sdp->sd_quota_list;
1307 struct gfs2_quota_data *qd;
1308 unsigned int x;
1309
Abhijith Das0a7ab792009-01-07 16:03:37 -06001310 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001311 while (!list_empty(head)) {
1312 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1313
Abhijith Das0a7ab792009-01-07 16:03:37 -06001314 if (atomic_read(&qd->qd_count) > 1 ||
1315 (atomic_read(&qd->qd_count) &&
1316 !test_bit(QDF_CHANGE, &qd->qd_flags))) {
Abhijith Das0a7ab792009-01-07 16:03:37 -06001317 list_move(&qd->qd_list, head);
1318 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319 schedule();
Abhijith Das0a7ab792009-01-07 16:03:37 -06001320 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321 continue;
1322 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323
Abhijith Das0a7ab792009-01-07 16:03:37 -06001324 list_del(&qd->qd_list);
1325 /* Also remove if this qd exists in the reclaim list */
1326 if (!list_empty(&qd->qd_reclaim)) {
1327 list_del_init(&qd->qd_reclaim);
1328 atomic_dec(&qd_lru_count);
1329 }
1330 atomic_dec(&sdp->sd_quota_count);
1331 spin_unlock(&qd_lru_lock);
1332
1333 if (!atomic_read(&qd->qd_count)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334 gfs2_assert_warn(sdp, !qd->qd_change);
1335 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1336 } else
1337 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1338 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1339
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001340 gfs2_glock_put(qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001341 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001342
Abhijith Das0a7ab792009-01-07 16:03:37 -06001343 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 }
Abhijith Das0a7ab792009-01-07 16:03:37 -06001345 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346
1347 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1348
1349 if (sdp->sd_quota_bitmap) {
1350 for (x = 0; x < sdp->sd_quota_chunks; x++)
1351 kfree(sdp->sd_quota_bitmap[x]);
1352 kfree(sdp->sd_quota_bitmap);
1353 }
1354}
1355
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001356static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1357{
1358 if (error == 0 || error == -EROFS)
1359 return;
1360 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1361 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1362}
1363
1364static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001365 int (*fxn)(struct super_block *sb, int type),
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001366 unsigned long t, unsigned long *timeo,
1367 unsigned int *new_timeo)
1368{
1369 if (t >= *timeo) {
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001370 int error = fxn(sdp->sd_vfs, 0);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001371 quotad_error(sdp, msg, error);
1372 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1373 } else {
1374 *timeo -= t;
1375 }
1376}
1377
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001378static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1379{
1380 struct gfs2_inode *ip;
1381
1382 while(1) {
1383 ip = NULL;
1384 spin_lock(&sdp->sd_trunc_lock);
1385 if (!list_empty(&sdp->sd_trunc_list)) {
1386 ip = list_entry(sdp->sd_trunc_list.next,
1387 struct gfs2_inode, i_trunc_list);
1388 list_del_init(&ip->i_trunc_list);
1389 }
1390 spin_unlock(&sdp->sd_trunc_lock);
1391 if (ip == NULL)
1392 return;
1393 gfs2_glock_finish_truncate(ip);
1394 }
1395}
1396
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001397void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
1398 if (!sdp->sd_statfs_force_sync) {
1399 sdp->sd_statfs_force_sync = 1;
1400 wake_up(&sdp->sd_quota_wait);
1401 }
1402}
1403
1404
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001405/**
1406 * gfs2_quotad - Write cached quota changes into the quota file
1407 * @sdp: Pointer to GFS2 superblock
1408 *
1409 */
1410
1411int gfs2_quotad(void *data)
1412{
1413 struct gfs2_sbd *sdp = data;
1414 struct gfs2_tune *tune = &sdp->sd_tune;
1415 unsigned long statfs_timeo = 0;
1416 unsigned long quotad_timeo = 0;
1417 unsigned long t = 0;
1418 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001419 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001420
1421 while (!kthread_should_stop()) {
1422
1423 /* Update the master statfs file */
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001424 if (sdp->sd_statfs_force_sync) {
1425 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
1426 quotad_error(sdp, "statfs", error);
1427 statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
1428 }
1429 else
1430 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1431 &statfs_timeo,
1432 &tune->gt_statfs_quantum);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001433
1434 /* Update quota file */
Christoph Hellwig5fb324a2010-02-16 03:44:52 -05001435 quotad_check_timeo(sdp, "sync", gfs2_quota_sync_timeo, t,
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001436 &quotad_timeo, &tune->gt_quota_quantum);
1437
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001438 /* Check for & recover partially truncated inodes */
1439 quotad_check_trunc_list(sdp);
1440
Tejun Heoa0acae02011-11-21 12:32:22 -08001441 try_to_freeze();
1442
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001443 t = min(quotad_timeo, statfs_timeo);
1444
Steven Whitehouse7fa5d202009-03-31 15:49:08 +01001445 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001446 spin_lock(&sdp->sd_trunc_lock);
1447 empty = list_empty(&sdp->sd_trunc_list);
1448 spin_unlock(&sdp->sd_trunc_lock);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001449 if (empty && !sdp->sd_statfs_force_sync)
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001450 t -= schedule_timeout(t);
1451 else
1452 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001453 finish_wait(&sdp->sd_quota_wait, &wait);
1454 }
1455
1456 return 0;
1457}
1458
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001459static int gfs2_quota_get_xstate(struct super_block *sb,
1460 struct fs_quota_stat *fqs)
1461{
1462 struct gfs2_sbd *sdp = sb->s_fs_info;
1463
1464 memset(fqs, 0, sizeof(struct fs_quota_stat));
1465 fqs->qs_version = FS_QSTAT_VERSION;
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001466
1467 switch (sdp->sd_args.ar_quota) {
1468 case GFS2_QUOTA_ON:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001469 fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001470 /*FALLTHRU*/
1471 case GFS2_QUOTA_ACCOUNT:
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001472 fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
Christoph Hellwigad6bb902010-05-05 00:10:56 +02001473 break;
1474 case GFS2_QUOTA_OFF:
1475 break;
1476 }
1477
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001478 if (sdp->sd_quota_inode) {
1479 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1480 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1481 }
1482 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1483 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
1484 fqs->qs_incoredqs = atomic_read(&qd_lru_count);
1485 return 0;
1486}
1487
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001488static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001489 struct fs_disk_quota *fdq)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001490{
1491 struct gfs2_sbd *sdp = sb->s_fs_info;
1492 struct gfs2_quota_lvb *qlvb;
1493 struct gfs2_quota_data *qd;
1494 struct gfs2_holder q_gh;
1495 int error;
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001496 int type;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001497
1498 memset(fdq, 0, sizeof(struct fs_disk_quota));
1499
1500 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1501 return -ESRCH; /* Crazy XFS error code */
1502
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001503 if (qid.type == USRQUOTA)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001504 type = QUOTA_USER;
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001505 else if (qid.type == GRPQUOTA)
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001506 type = QUOTA_GROUP;
1507 else
1508 return -EINVAL;
1509
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001510 error = qd_get(sdp, type, from_kqid(&init_user_ns, qid), &qd);
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001511 if (error)
1512 return error;
1513 error = do_glock(qd, FORCE, &q_gh);
1514 if (error)
1515 goto out;
1516
David Teigland4e2f8842012-11-14 13:47:37 -05001517 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001518 fdq->d_version = FS_DQUOT_VERSION;
Christoph Hellwigade7ce32010-06-04 10:56:01 +02001519 fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
Eric W. Biederman558e8522013-01-31 18:15:33 -08001520 fdq->d_id = from_kqid_munged(current_user_ns(), qid);
Abhijith Das14870b42010-11-18 11:24:24 -05001521 fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
1522 fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
1523 fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
Steven Whitehouse113d6b32009-09-28 11:52:16 +01001524
1525 gfs2_glock_dq_uninit(&q_gh);
1526out:
1527 qd_put(qd);
1528 return error;
1529}
1530
Steven Whitehousee285c102009-09-23 13:50:49 +01001531/* GFS2 only supports a subset of the XFS fields */
Abhijith Das802ec9b2010-11-18 11:26:46 -05001532#define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
Steven Whitehousee285c102009-09-23 13:50:49 +01001533
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001534static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001535 struct fs_disk_quota *fdq)
Steven Whitehousee285c102009-09-23 13:50:49 +01001536{
1537 struct gfs2_sbd *sdp = sb->s_fs_info;
1538 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
1539 struct gfs2_quota_data *qd;
1540 struct gfs2_holder q_gh, i_gh;
1541 unsigned int data_blocks, ind_blocks;
1542 unsigned int blocks = 0;
1543 int alloc_required;
Steven Whitehousee285c102009-09-23 13:50:49 +01001544 loff_t offset;
1545 int error;
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001546 int type;
Steven Whitehousee285c102009-09-23 13:50:49 +01001547
1548 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
1549 return -ESRCH; /* Crazy XFS error code */
1550
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001551 switch(qid.type) {
Steven Whitehousee285c102009-09-23 13:50:49 +01001552 case USRQUOTA:
1553 type = QUOTA_USER;
Steven Whitehousee285c102009-09-23 13:50:49 +01001554 break;
1555 case GRPQUOTA:
1556 type = QUOTA_GROUP;
Steven Whitehousee285c102009-09-23 13:50:49 +01001557 break;
1558 default:
1559 return -EINVAL;
1560 }
1561
1562 if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
1563 return -EINVAL;
Steven Whitehousee285c102009-09-23 13:50:49 +01001564
Eric W. Biederman74a8a102012-09-16 02:07:49 -07001565 error = qd_get(sdp, type, from_kqid(&init_user_ns, qid), &qd);
Steven Whitehousee285c102009-09-23 13:50:49 +01001566 if (error)
1567 return error;
1568
Bob Peterson0a305e42012-06-06 11:17:59 +01001569 error = gfs2_rs_alloc(ip);
1570 if (error)
1571 goto out_put;
1572
Steven Whitehousee285c102009-09-23 13:50:49 +01001573 mutex_lock(&ip->i_inode.i_mutex);
1574 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
1575 if (error)
Bob Peterson0a305e42012-06-06 11:17:59 +01001576 goto out_unlockput;
Steven Whitehousee285c102009-09-23 13:50:49 +01001577 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1578 if (error)
1579 goto out_q;
1580
1581 /* Check for existing entry, if none then alloc new blocks */
1582 error = update_qd(sdp, qd);
1583 if (error)
1584 goto out_i;
1585
1586 /* If nothing has changed, this is a no-op */
1587 if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001588 ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001589 fdq->d_fieldmask ^= FS_DQ_BSOFT;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001590
Steven Whitehousee285c102009-09-23 13:50:49 +01001591 if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
Abhijith Das14870b42010-11-18 11:24:24 -05001592 ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
Steven Whitehousee285c102009-09-23 13:50:49 +01001593 fdq->d_fieldmask ^= FS_DQ_BHARD;
Abhijith Das802ec9b2010-11-18 11:26:46 -05001594
1595 if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
1596 ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
1597 fdq->d_fieldmask ^= FS_DQ_BCOUNT;
1598
Steven Whitehousee285c102009-09-23 13:50:49 +01001599 if (fdq->d_fieldmask == 0)
1600 goto out_i;
1601
1602 offset = qd2offset(qd);
Bob Peterson461cb412010-06-24 19:21:20 -04001603 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
Abhijith Dase79a46a2011-02-07 11:22:41 -05001604 if (gfs2_is_stuffed(ip))
1605 alloc_required = 1;
Steven Whitehousee285c102009-09-23 13:50:49 +01001606 if (alloc_required) {
Steven Whitehousee285c102009-09-23 13:50:49 +01001607 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
1608 &data_blocks, &ind_blocks);
Bob Peterson564e12b2011-11-21 13:36:17 -05001609 blocks = 1 + data_blocks + ind_blocks;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +00001610 error = gfs2_inplace_reserve(ip, blocks, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001611 if (error)
Bob Peterson564e12b2011-11-21 13:36:17 -05001612 goto out_i;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001613 blocks += gfs2_rg_blocks(ip, blocks);
Steven Whitehousee285c102009-09-23 13:50:49 +01001614 }
1615
Abhijith Dase79a46a2011-02-07 11:22:41 -05001616 /* Some quotas span block boundaries and can update two blocks,
1617 adding an extra block to the transaction to handle such quotas */
1618 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
Steven Whitehousee285c102009-09-23 13:50:49 +01001619 if (error)
1620 goto out_release;
1621
1622 /* Apply changes */
1623 error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
1624
1625 gfs2_trans_end(sdp);
1626out_release:
Bob Peterson564e12b2011-11-21 13:36:17 -05001627 if (alloc_required)
Steven Whitehousee285c102009-09-23 13:50:49 +01001628 gfs2_inplace_release(ip);
Steven Whitehousee285c102009-09-23 13:50:49 +01001629out_i:
1630 gfs2_glock_dq_uninit(&i_gh);
1631out_q:
1632 gfs2_glock_dq_uninit(&q_gh);
Bob Peterson0a305e42012-06-06 11:17:59 +01001633out_unlockput:
Steven Whitehousee285c102009-09-23 13:50:49 +01001634 mutex_unlock(&ip->i_inode.i_mutex);
Bob Peterson0a305e42012-06-06 11:17:59 +01001635out_put:
Steven Whitehousee285c102009-09-23 13:50:49 +01001636 qd_put(qd);
1637 return error;
1638}
1639
Steven Whitehousecc632e72009-09-15 09:59:02 +01001640const struct quotactl_ops gfs2_quotactl_ops = {
1641 .quota_sync = gfs2_quota_sync,
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001642 .get_xstate = gfs2_quota_get_xstate,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -04001643 .get_dqblk = gfs2_get_dqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -04001644 .set_dqblk = gfs2_set_dqblk,
Steven Whitehousecc632e72009-09-15 09:59:02 +01001645};