blob: addb51e0f1353118267fff55ebe74f3d1a8f6eb9 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 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.
18 * Since quota tags are part of transactions, there is no need to a quota check
19 * 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>
41#include <linux/spinlock.h>
42#include <linux/completion.h>
43#include <linux/buffer_head.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000044#include <linux/sort.h>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000045#include <linux/fs.h>
Steven Whitehouse2e565bb2006-10-02 11:38:25 -040046#include <linux/bio.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050047#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020048#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000049
50#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050051#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000052#include "bmap.h"
53#include "glock.h"
54#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000055#include "log.h"
56#include "meta_io.h"
57#include "quota.h"
58#include "rgrp.h"
59#include "super.h"
60#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000061#include "inode.h"
Steven Whitehousef42faf42006-01-30 18:34:10 +000062#include "ops_file.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000063#include "ops_address.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050064#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
66#define QUOTA_USER 1
67#define QUOTA_GROUP 0
68
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010069struct gfs2_quota_host {
70 u64 qu_limit;
71 u64 qu_warn;
72 s64 qu_value;
Abhijith Das2d9a4bb2007-08-15 11:25:05 -050073 u32 qu_ll_next;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010074};
75
76struct gfs2_quota_change_host {
77 u64 qc_change;
78 u32 qc_flags; /* GFS2_QCF_... */
79 u32 qc_id;
80};
81
Steven Whitehousecd915492006-09-04 12:49:07 -040082static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000083{
Steven Whitehousecd915492006-09-04 12:49:07 -040084 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +000085
Steven Whitehousecd915492006-09-04 12:49:07 -040086 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 offset *= sizeof(struct gfs2_quota);
88
89 return offset;
90}
91
Steven Whitehousecd915492006-09-04 12:49:07 -040092static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +000093 struct gfs2_quota_data **qdp)
94{
95 struct gfs2_quota_data *qd;
96 int error;
97
98 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
99 if (!qd)
100 return -ENOMEM;
101
102 qd->qd_count = 1;
103 qd->qd_id = id;
104 if (user)
105 set_bit(QDF_USER, &qd->qd_flags);
106 qd->qd_slot = -1;
107
Steven Whitehousecd915492006-09-04 12:49:07 -0400108 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 &gfs2_quota_glops, CREATE, &qd->qd_gl);
110 if (error)
111 goto fail;
112
113 error = gfs2_lvb_hold(qd->qd_gl);
114 gfs2_glock_put(qd->qd_gl);
115 if (error)
116 goto fail;
117
118 *qdp = qd;
119
120 return 0;
121
Steven Whitehousea91ea692006-09-04 12:04:26 -0400122fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 kfree(qd);
124 return error;
125}
126
Steven Whitehousecd915492006-09-04 12:49:07 -0400127static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 struct gfs2_quota_data **qdp)
129{
130 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
131 int error, found;
132
133 *qdp = NULL;
134
135 for (;;) {
136 found = 0;
137 spin_lock(&sdp->sd_quota_spin);
138 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
139 if (qd->qd_id == id &&
140 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
141 qd->qd_count++;
142 found = 1;
143 break;
144 }
145 }
146
147 if (!found)
148 qd = NULL;
149
150 if (!qd && new_qd) {
151 qd = new_qd;
152 list_add(&qd->qd_list, &sdp->sd_quota_list);
153 atomic_inc(&sdp->sd_quota_count);
154 new_qd = NULL;
155 }
156
157 spin_unlock(&sdp->sd_quota_spin);
158
159 if (qd || !create) {
160 if (new_qd) {
161 gfs2_lvb_unhold(new_qd->qd_gl);
162 kfree(new_qd);
163 }
164 *qdp = qd;
165 return 0;
166 }
167
168 error = qd_alloc(sdp, user, id, &new_qd);
169 if (error)
170 return error;
171 }
172}
173
174static void qd_hold(struct gfs2_quota_data *qd)
175{
176 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
177
178 spin_lock(&sdp->sd_quota_spin);
179 gfs2_assert(sdp, qd->qd_count);
180 qd->qd_count++;
181 spin_unlock(&sdp->sd_quota_spin);
182}
183
184static void qd_put(struct gfs2_quota_data *qd)
185{
186 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
187 spin_lock(&sdp->sd_quota_spin);
188 gfs2_assert(sdp, qd->qd_count);
189 if (!--qd->qd_count)
190 qd->qd_last_touched = jiffies;
191 spin_unlock(&sdp->sd_quota_spin);
192}
193
194static int slot_get(struct gfs2_quota_data *qd)
195{
196 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
197 unsigned int c, o = 0, b;
198 unsigned char byte = 0;
199
200 spin_lock(&sdp->sd_quota_spin);
201
202 if (qd->qd_slot_count++) {
203 spin_unlock(&sdp->sd_quota_spin);
204 return 0;
205 }
206
207 for (c = 0; c < sdp->sd_quota_chunks; c++)
208 for (o = 0; o < PAGE_SIZE; o++) {
209 byte = sdp->sd_quota_bitmap[c][o];
210 if (byte != 0xFF)
211 goto found;
212 }
213
214 goto fail;
215
Steven Whitehousea91ea692006-09-04 12:04:26 -0400216found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 for (b = 0; b < 8; b++)
218 if (!(byte & (1 << b)))
219 break;
220 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
221
222 if (qd->qd_slot >= sdp->sd_quota_slots)
223 goto fail;
224
225 sdp->sd_quota_bitmap[c][o] |= 1 << b;
226
227 spin_unlock(&sdp->sd_quota_spin);
228
229 return 0;
230
Steven Whitehousea91ea692006-09-04 12:04:26 -0400231fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 qd->qd_slot_count--;
233 spin_unlock(&sdp->sd_quota_spin);
234 return -ENOSPC;
235}
236
237static void slot_hold(struct gfs2_quota_data *qd)
238{
239 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
240
241 spin_lock(&sdp->sd_quota_spin);
242 gfs2_assert(sdp, qd->qd_slot_count);
243 qd->qd_slot_count++;
244 spin_unlock(&sdp->sd_quota_spin);
245}
246
247static void slot_put(struct gfs2_quota_data *qd)
248{
249 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
250
251 spin_lock(&sdp->sd_quota_spin);
252 gfs2_assert(sdp, qd->qd_slot_count);
253 if (!--qd->qd_slot_count) {
254 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
255 qd->qd_slot = -1;
256 }
257 spin_unlock(&sdp->sd_quota_spin);
258}
259
260static int bh_get(struct gfs2_quota_data *qd)
261{
262 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400263 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265 struct buffer_head *bh;
266 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400267 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268
Steven Whitehousef55ab262006-02-21 12:51:39 +0000269 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270
271 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000272 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 return 0;
274 }
275
276 block = qd->qd_slot / sdp->sd_qc_per_block;
277 offset = qd->qd_slot % sdp->sd_qc_per_block;;
278
Steven Whitehouse23591252006-10-13 17:25:45 -0400279 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
280 error = gfs2_block_map(&ip->i_inode, block, 0, &bh_map);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 if (error)
282 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400283 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 if (error)
285 goto fail;
286 error = -EIO;
287 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
288 goto fail_brelse;
289
290 qd->qd_bh = bh;
291 qd->qd_bh_qc = (struct gfs2_quota_change *)
292 (bh->b_data + sizeof(struct gfs2_meta_header) +
293 offset * sizeof(struct gfs2_quota_change));
294
Josef Whiter2e95b662007-02-20 00:03:29 -0500295 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296
297 return 0;
298
Steven Whitehousea91ea692006-09-04 12:04:26 -0400299fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400301fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000303 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304 return error;
305}
306
307static void bh_put(struct gfs2_quota_data *qd)
308{
309 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
310
Steven Whitehousef55ab262006-02-21 12:51:39 +0000311 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 gfs2_assert(sdp, qd->qd_bh_count);
313 if (!--qd->qd_bh_count) {
314 brelse(qd->qd_bh);
315 qd->qd_bh = NULL;
316 qd->qd_bh_qc = NULL;
317 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000318 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319}
320
321static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
322{
323 struct gfs2_quota_data *qd = NULL;
324 int error;
325 int found = 0;
326
327 *qdp = NULL;
328
329 if (sdp->sd_vfs->s_flags & MS_RDONLY)
330 return 0;
331
332 spin_lock(&sdp->sd_quota_spin);
333
334 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
335 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
336 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
337 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
338 continue;
339
340 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
341
342 set_bit(QDF_LOCKED, &qd->qd_flags);
343 gfs2_assert_warn(sdp, qd->qd_count);
344 qd->qd_count++;
345 qd->qd_change_sync = qd->qd_change;
346 gfs2_assert_warn(sdp, qd->qd_slot_count);
347 qd->qd_slot_count++;
348 found = 1;
349
350 break;
351 }
352
353 if (!found)
354 qd = NULL;
355
356 spin_unlock(&sdp->sd_quota_spin);
357
358 if (qd) {
359 gfs2_assert_warn(sdp, qd->qd_change_sync);
360 error = bh_get(qd);
361 if (error) {
362 clear_bit(QDF_LOCKED, &qd->qd_flags);
363 slot_put(qd);
364 qd_put(qd);
365 return error;
366 }
367 }
368
369 *qdp = qd;
370
371 return 0;
372}
373
374static int qd_trylock(struct gfs2_quota_data *qd)
375{
376 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
377
378 if (sdp->sd_vfs->s_flags & MS_RDONLY)
379 return 0;
380
381 spin_lock(&sdp->sd_quota_spin);
382
383 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
384 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
385 spin_unlock(&sdp->sd_quota_spin);
386 return 0;
387 }
388
389 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
390
391 set_bit(QDF_LOCKED, &qd->qd_flags);
392 gfs2_assert_warn(sdp, qd->qd_count);
393 qd->qd_count++;
394 qd->qd_change_sync = qd->qd_change;
395 gfs2_assert_warn(sdp, qd->qd_slot_count);
396 qd->qd_slot_count++;
397
398 spin_unlock(&sdp->sd_quota_spin);
399
400 gfs2_assert_warn(sdp, qd->qd_change_sync);
401 if (bh_get(qd)) {
402 clear_bit(QDF_LOCKED, &qd->qd_flags);
403 slot_put(qd);
404 qd_put(qd);
405 return 0;
406 }
407
408 return 1;
409}
410
411static void qd_unlock(struct gfs2_quota_data *qd)
412{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500413 gfs2_assert_warn(qd->qd_gl->gl_sbd,
414 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 clear_bit(QDF_LOCKED, &qd->qd_flags);
416 bh_put(qd);
417 slot_put(qd);
418 qd_put(qd);
419}
420
Steven Whitehousecd915492006-09-04 12:49:07 -0400421static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 struct gfs2_quota_data **qdp)
423{
424 int error;
425
426 error = qd_get(sdp, user, id, create, qdp);
427 if (error)
428 return error;
429
430 error = slot_get(*qdp);
431 if (error)
432 goto fail;
433
434 error = bh_get(*qdp);
435 if (error)
436 goto fail_slot;
437
438 return 0;
439
Steven Whitehousea91ea692006-09-04 12:04:26 -0400440fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400442fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 qd_put(*qdp);
444 return error;
445}
446
447static void qdsb_put(struct gfs2_quota_data *qd)
448{
449 bh_put(qd);
450 slot_put(qd);
451 qd_put(qd);
452}
453
Steven Whitehousecd915492006-09-04 12:49:07 -0400454int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400456 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 struct gfs2_alloc *al = &ip->i_alloc;
458 struct gfs2_quota_data **qd = al->al_qd;
459 int error;
460
461 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
462 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
463 return -EIO;
464
465 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
466 return 0;
467
Steven Whitehouse2933f922006-11-01 13:23:29 -0500468 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 if (error)
470 goto out;
471 al->al_qd_num++;
472 qd++;
473
Steven Whitehouse2933f922006-11-01 13:23:29 -0500474 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 if (error)
476 goto out;
477 al->al_qd_num++;
478 qd++;
479
Steven Whitehouse2933f922006-11-01 13:23:29 -0500480 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
482 if (error)
483 goto out;
484 al->al_qd_num++;
485 qd++;
486 }
487
Steven Whitehouse2933f922006-11-01 13:23:29 -0500488 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
490 if (error)
491 goto out;
492 al->al_qd_num++;
493 qd++;
494 }
495
Steven Whitehousea91ea692006-09-04 12:04:26 -0400496out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 if (error)
498 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 return error;
500}
501
502void gfs2_quota_unhold(struct gfs2_inode *ip)
503{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400504 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 struct gfs2_alloc *al = &ip->i_alloc;
506 unsigned int x;
507
508 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
509
510 for (x = 0; x < al->al_qd_num; x++) {
511 qdsb_put(al->al_qd[x]);
512 al->al_qd[x] = NULL;
513 }
514 al->al_qd_num = 0;
515}
516
517static int sort_qd(const void *a, const void *b)
518{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400519 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
520 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521
522 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
523 !test_bit(QDF_USER, &qd_b->qd_flags)) {
524 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400525 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400527 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400529 if (qd_a->qd_id < qd_b->qd_id)
530 return -1;
531 if (qd_a->qd_id > qd_b->qd_id)
532 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533
Steven Whitehouse48fac172006-09-05 15:17:12 -0400534 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535}
536
Steven Whitehousecd915492006-09-04 12:49:07 -0400537static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538{
539 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400540 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400542 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543
Steven Whitehousef55ab262006-02-21 12:51:39 +0000544 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000545 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546
547 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
548 qc->qc_change = 0;
549 qc->qc_flags = 0;
550 if (test_bit(QDF_USER, &qd->qd_flags))
551 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
552 qc->qc_id = cpu_to_be32(qd->qd_id);
553 }
554
Al Virob44b84d2006-10-14 10:46:30 -0400555 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 qc->qc_change = cpu_to_be64(x);
557
558 spin_lock(&sdp->sd_quota_spin);
559 qd->qd_change = x;
560 spin_unlock(&sdp->sd_quota_spin);
561
562 if (!x) {
563 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
564 clear_bit(QDF_CHANGE, &qd->qd_flags);
565 qc->qc_flags = 0;
566 qc->qc_id = 0;
567 slot_put(qd);
568 qd_put(qd);
569 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
570 qd_hold(qd);
571 slot_hold(qd);
572 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400573
Steven Whitehousef55ab262006-02-21 12:51:39 +0000574 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575}
576
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100577static void gfs2_quota_in(struct gfs2_quota_host *qu, const void *buf)
578{
579 const struct gfs2_quota *str = buf;
580
581 qu->qu_limit = be64_to_cpu(str->qu_limit);
582 qu->qu_warn = be64_to_cpu(str->qu_warn);
583 qu->qu_value = be64_to_cpu(str->qu_value);
Abhijith Das2d9a4bb2007-08-15 11:25:05 -0500584 qu->qu_ll_next = be32_to_cpu(str->qu_ll_next);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100585}
586
587static void gfs2_quota_out(const struct gfs2_quota_host *qu, void *buf)
588{
589 struct gfs2_quota *str = buf;
590
591 str->qu_limit = cpu_to_be64(qu->qu_limit);
592 str->qu_warn = cpu_to_be64(qu->qu_warn);
593 str->qu_value = cpu_to_be64(qu->qu_value);
Abhijith Das2d9a4bb2007-08-15 11:25:05 -0500594 str->qu_ll_next = cpu_to_be32(qu->qu_ll_next);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100595 memset(&str->qu_reserved, 0, sizeof(str->qu_reserved));
596}
597
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000598/**
599 * gfs2_adjust_quota
600 *
601 * This function was mostly borrowed from gfs2_block_truncate_page which was
602 * in turn mostly borrowed from ext3
603 */
604static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousecd915492006-09-04 12:49:07 -0400605 s64 change, struct gfs2_quota_data *qd)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000606{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400607 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000608 struct address_space *mapping = inode->i_mapping;
609 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500610 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000611 unsigned blocksize, iblock, pos;
612 struct buffer_head *bh;
613 struct page *page;
614 void *kaddr;
Abhijith Das1990e912007-05-31 17:52:02 -0500615 char *ptr;
616 struct gfs2_quota_host qp;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400617 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000618 int err = -EIO;
619
Abhijith Das0fd53552007-08-14 15:34:58 -0500620 if (gfs2_is_stuffed(ip)) {
621 struct gfs2_alloc *al = NULL;
622 al = gfs2_alloc_get(ip);
623 /* just request 1 blk */
624 al->al_requested = 1;
625 gfs2_inplace_reserve(ip);
626 gfs2_unstuff_dinode(ip, NULL);
627 gfs2_inplace_release(ip);
628 gfs2_alloc_put(ip);
629 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000630 page = grab_cache_page(mapping, index);
631 if (!page)
632 return -ENOMEM;
633
634 blocksize = inode->i_sb->s_blocksize;
635 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
636
637 if (!page_has_buffers(page))
638 create_empty_buffers(page, blocksize, 0);
639
640 bh = page_buffers(page);
641 pos = blocksize;
642 while (offset >= pos) {
643 bh = bh->b_this_page;
644 iblock++;
645 pos += blocksize;
646 }
647
648 if (!buffer_mapped(bh)) {
649 gfs2_get_block(inode, iblock, bh, 1);
650 if (!buffer_mapped(bh))
651 goto unlock;
652 }
653
654 if (PageUptodate(page))
655 set_buffer_uptodate(bh);
656
657 if (!buffer_uptodate(bh)) {
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400658 ll_rw_block(READ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000659 wait_on_buffer(bh);
660 if (!buffer_uptodate(bh))
661 goto unlock;
662 }
663
664 gfs2_trans_add_bh(ip->i_gl, bh, 0);
665
666 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse48fac172006-09-05 15:17:12 -0400667 ptr = kaddr + offset;
Abhijith Das1990e912007-05-31 17:52:02 -0500668 gfs2_quota_in(&qp, ptr);
669 qp.qu_value += change;
670 value = qp.qu_value;
671 gfs2_quota_out(&qp, ptr);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000672 flush_dcache_page(page);
673 kunmap_atomic(kaddr, KM_USER0);
674 err = 0;
675 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000676 qd->qd_qb.qb_value = cpu_to_be64(value);
Abhijith Das2a87ab02007-05-16 17:02:19 -0500677 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC);
678 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000679unlock:
680 unlock_page(page);
681 page_cache_release(page);
682 return err;
683}
684
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
686{
687 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400688 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 unsigned int data_blocks, ind_blocks;
690 struct gfs2_holder *ghs, i_gh;
691 unsigned int qx, x;
692 struct gfs2_quota_data *qd;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000693 loff_t offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 unsigned int nalloc = 0;
695 struct gfs2_alloc *al = NULL;
696 int error;
697
698 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
699 &data_blocks, &ind_blocks);
700
701 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
702 if (!ghs)
703 return -ENOMEM;
704
705 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
706 for (qx = 0; qx < num_qd; qx++) {
707 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
708 LM_ST_EXCLUSIVE,
709 GL_NOCACHE, &ghs[qx]);
710 if (error)
711 goto out;
712 }
713
714 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
715 if (error)
716 goto out;
717
718 for (x = 0; x < num_qd; x++) {
719 int alloc_required;
720
721 offset = qd2offset(qda[x]);
722 error = gfs2_write_alloc_required(ip, offset,
723 sizeof(struct gfs2_quota),
724 &alloc_required);
725 if (error)
726 goto out_gunlock;
727 if (alloc_required)
728 nalloc++;
729 }
730
731 if (nalloc) {
732 al = gfs2_alloc_get(ip);
733
734 al->al_requested = nalloc * (data_blocks + ind_blocks);
735
736 error = gfs2_inplace_reserve(ip);
737 if (error)
738 goto out_alloc;
739
740 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100741 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 num_qd * data_blocks +
743 nalloc * ind_blocks +
744 RES_DINODE + num_qd +
745 RES_STATFS, 0);
746 if (error)
747 goto out_ipres;
748 } else {
749 error = gfs2_trans_begin(sdp,
750 num_qd * data_blocks +
751 RES_DINODE + num_qd, 0);
752 if (error)
753 goto out_gunlock;
754 }
755
756 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 qd = qda[x];
758 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000759 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500760 (struct gfs2_quota_data *)
Abhijith Das2a87ab02007-05-16 17:02:19 -0500761 qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000762 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 goto out_end_trans;
764
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 }
767
768 error = 0;
769
Steven Whitehousea91ea692006-09-04 12:04:26 -0400770out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400772out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 if (nalloc)
774 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400775out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 if (nalloc)
777 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400778out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400780out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 while (qx--)
782 gfs2_glock_dq_uninit(&ghs[qx]);
783 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400784 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 return error;
786}
787
788static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
789 struct gfs2_holder *q_gh)
790{
791 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400792 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 struct gfs2_holder i_gh;
Al Virob5bc9e82006-10-13 23:31:55 -0400794 struct gfs2_quota_host q;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 char buf[sizeof(struct gfs2_quota)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000796 struct file_ra_state ra_state;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400798 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799
Steven Whitehousef42faf42006-01-30 18:34:10 +0000800 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400801restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
803 if (error)
804 return error;
805
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400806 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400808 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000809 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 gfs2_glock_dq_uninit(q_gh);
811 error = gfs2_glock_nq_init(qd->qd_gl,
812 LM_ST_EXCLUSIVE, GL_NOCACHE,
813 q_gh);
814 if (error)
815 return error;
816
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400817 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818 if (error)
819 goto fail;
820
821 memset(buf, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000822 pos = qd2offset(qd);
Steven Whitehouse0d42e542006-06-20 16:13:49 -0400823 error = gfs2_internal_read(ip, &ra_state, buf,
824 &pos, sizeof(struct gfs2_quota));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 if (error < 0)
826 goto fail_gunlock;
827
828 gfs2_glock_dq_uninit(&i_gh);
829
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400830
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 gfs2_quota_in(&q, buf);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400832 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
833 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
834 qlvb->__pad = 0;
835 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
836 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
837 qlvb->qb_value = cpu_to_be64(q.qu_value);
838 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839
840 if (gfs2_glock_is_blocking(qd->qd_gl)) {
841 gfs2_glock_dq_uninit(q_gh);
842 force_refresh = 0;
843 goto restart;
844 }
845 }
846
847 return 0;
848
Steven Whitehousea91ea692006-09-04 12:04:26 -0400849fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400851fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 return error;
854}
855
Steven Whitehousecd915492006-09-04 12:49:07 -0400856int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400858 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 struct gfs2_alloc *al = &ip->i_alloc;
860 unsigned int x;
861 int error = 0;
862
863 gfs2_quota_hold(ip, uid, gid);
864
865 if (capable(CAP_SYS_RESOURCE) ||
866 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
867 return 0;
868
869 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
870 sort_qd, NULL);
871
872 for (x = 0; x < al->al_qd_num; x++) {
873 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
874 if (error)
875 break;
876 }
877
878 if (!error)
879 set_bit(GIF_QD_LOCKED, &ip->i_flags);
880 else {
881 while (x--)
882 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
883 gfs2_quota_unhold(ip);
884 }
885
886 return error;
887}
888
889static int need_sync(struct gfs2_quota_data *qd)
890{
891 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
892 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400893 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 unsigned int num, den;
895 int do_sync = 1;
896
897 if (!qd->qd_qb.qb_limit)
898 return 0;
899
900 spin_lock(&sdp->sd_quota_spin);
901 value = qd->qd_change;
902 spin_unlock(&sdp->sd_quota_spin);
903
904 spin_lock(&gt->gt_spin);
905 num = gt->gt_quota_scale_num;
906 den = gt->gt_quota_scale_den;
907 spin_unlock(&gt->gt_spin);
908
909 if (value < 0)
910 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400911 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
912 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 do_sync = 0;
914 else {
915 value *= gfs2_jindex_size(sdp) * num;
916 do_div(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400917 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -0400918 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 do_sync = 0;
920 }
921
922 return do_sync;
923}
924
925void gfs2_quota_unlock(struct gfs2_inode *ip)
926{
927 struct gfs2_alloc *al = &ip->i_alloc;
928 struct gfs2_quota_data *qda[4];
929 unsigned int count = 0;
930 unsigned int x;
931
932 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
933 goto out;
934
935 for (x = 0; x < al->al_qd_num; x++) {
936 struct gfs2_quota_data *qd;
937 int sync;
938
939 qd = al->al_qd[x];
940 sync = need_sync(qd);
941
942 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
943
944 if (sync && qd_trylock(qd))
945 qda[count++] = qd;
946 }
947
948 if (count) {
949 do_sync(count, qda);
950 for (x = 0; x < count; x++)
951 qd_unlock(qda[x]);
952 }
953
Steven Whitehousea91ea692006-09-04 12:04:26 -0400954out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955 gfs2_quota_unhold(ip);
956}
957
958#define MAX_LINE 256
959
960static int print_message(struct gfs2_quota_data *qd, char *type)
961{
962 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963
Steven Whitehouse02630a12006-07-03 11:20:06 -0400964 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
965 sdp->sd_fsname, type,
966 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
967 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968
969 return 0;
970}
971
Steven Whitehousecd915492006-09-04 12:49:07 -0400972int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400974 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 struct gfs2_alloc *al = &ip->i_alloc;
976 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -0400977 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 unsigned int x;
979 int error = 0;
980
981 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
982 return 0;
983
984 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
985 return 0;
986
987 for (x = 0; x < al->al_qd_num; x++) {
988 qd = al->al_qd[x];
989
990 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
991 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
992 continue;
993
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400994 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 spin_lock(&sdp->sd_quota_spin);
996 value += qd->qd_change;
997 spin_unlock(&sdp->sd_quota_spin);
998
Steven Whitehousecd915492006-09-04 12:49:07 -0400999 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 +00001000 print_message(qd, "exceeded");
1001 error = -EDQUOT;
1002 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001003 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001004 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001006 gfs2_tune_get(sdp,
1007 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 error = print_message(qd, "warning");
1009 qd->qd_last_warn = jiffies;
1010 }
1011 }
1012
1013 return error;
1014}
1015
Steven Whitehousecd915492006-09-04 12:49:07 -04001016void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1017 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018{
1019 struct gfs2_alloc *al = &ip->i_alloc;
1020 struct gfs2_quota_data *qd;
1021 unsigned int x;
1022 unsigned int found = 0;
1023
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001024 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 return;
1026 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
1027 return;
1028
1029 for (x = 0; x < al->al_qd_num; x++) {
1030 qd = al->al_qd[x];
1031
1032 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1033 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1034 do_qc(qd, change);
1035 found++;
1036 }
1037 }
1038}
1039
1040int gfs2_quota_sync(struct gfs2_sbd *sdp)
1041{
1042 struct gfs2_quota_data **qda;
1043 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1044 unsigned int num_qd;
1045 unsigned int x;
1046 int error = 0;
1047
1048 sdp->sd_quota_sync_gen++;
1049
1050 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1051 if (!qda)
1052 return -ENOMEM;
1053
1054 do {
1055 num_qd = 0;
1056
1057 for (;;) {
1058 error = qd_fish(sdp, qda + num_qd);
1059 if (error || !qda[num_qd])
1060 break;
1061 if (++num_qd == max_qd)
1062 break;
1063 }
1064
1065 if (num_qd) {
1066 if (!error)
1067 error = do_sync(num_qd, qda);
1068 if (!error)
1069 for (x = 0; x < num_qd; x++)
1070 qda[x]->qd_sync_gen =
1071 sdp->sd_quota_sync_gen;
1072
1073 for (x = 0; x < num_qd; x++)
1074 qd_unlock(qda[x]);
1075 }
1076 } while (!error && num_qd == max_qd);
1077
1078 kfree(qda);
1079
1080 return error;
1081}
1082
Steven Whitehousecd915492006-09-04 12:49:07 -04001083int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084{
1085 struct gfs2_quota_data *qd;
1086 struct gfs2_holder q_gh;
1087 int error;
1088
1089 error = qd_get(sdp, user, id, CREATE, &qd);
1090 if (error)
1091 return error;
1092
1093 error = do_glock(qd, FORCE, &q_gh);
1094 if (!error)
1095 gfs2_glock_dq_uninit(&q_gh);
1096
1097 qd_put(qd);
1098
1099 return error;
1100}
1101
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001102static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1103{
1104 const struct gfs2_quota_change *str = buf;
1105
1106 qc->qc_change = be64_to_cpu(str->qc_change);
1107 qc->qc_flags = be32_to_cpu(str->qc_flags);
1108 qc->qc_id = be32_to_cpu(str->qc_id);
1109}
1110
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111int gfs2_quota_init(struct gfs2_sbd *sdp)
1112{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001113 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1115 unsigned int x, slot = 0;
1116 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001117 u64 dblock;
1118 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 int error;
1120
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001121 if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1123 gfs2_consist_inode(ip);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001124 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 }
1126 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001127 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128
1129 error = -ENOMEM;
1130
1131 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1132 sizeof(unsigned char *), GFP_KERNEL);
1133 if (!sdp->sd_quota_bitmap)
1134 return error;
1135
1136 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1137 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1138 if (!sdp->sd_quota_bitmap[x])
1139 goto fail;
1140 }
1141
1142 for (x = 0; x < blocks; x++) {
1143 struct buffer_head *bh;
1144 unsigned int y;
1145
1146 if (!extlen) {
1147 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001148 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 if (error)
1150 goto fail;
1151 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001153 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1154 if (!bh)
1155 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1157 brelse(bh);
1158 goto fail;
1159 }
1160
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001161 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 y++, slot++) {
Al Virob62f9632006-10-13 23:46:46 -04001163 struct gfs2_quota_change_host qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 struct gfs2_quota_data *qd;
1165
1166 gfs2_quota_change_in(&qc, bh->b_data +
1167 sizeof(struct gfs2_meta_header) +
1168 y * sizeof(struct gfs2_quota_change));
1169 if (!qc.qc_change)
1170 continue;
1171
1172 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1173 qc.qc_id, &qd);
1174 if (error) {
1175 brelse(bh);
1176 goto fail;
1177 }
1178
1179 set_bit(QDF_CHANGE, &qd->qd_flags);
1180 qd->qd_change = qc.qc_change;
1181 qd->qd_slot = slot;
1182 qd->qd_slot_count = 1;
1183 qd->qd_last_touched = jiffies;
1184
1185 spin_lock(&sdp->sd_quota_spin);
1186 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1187 list_add(&qd->qd_list, &sdp->sd_quota_list);
1188 atomic_inc(&sdp->sd_quota_count);
1189 spin_unlock(&sdp->sd_quota_spin);
1190
1191 found++;
1192 }
1193
1194 brelse(bh);
1195 dblock++;
1196 extlen--;
1197 }
1198
1199 if (found)
1200 fs_info(sdp, "found %u quota changes\n", found);
1201
1202 return 0;
1203
Steven Whitehousea91ea692006-09-04 12:04:26 -04001204fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 gfs2_quota_cleanup(sdp);
1206 return error;
1207}
1208
1209void gfs2_quota_scan(struct gfs2_sbd *sdp)
1210{
1211 struct gfs2_quota_data *qd, *safe;
1212 LIST_HEAD(dead);
1213
1214 spin_lock(&sdp->sd_quota_spin);
1215 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1216 if (!qd->qd_count &&
1217 time_after_eq(jiffies, qd->qd_last_touched +
1218 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1219 list_move(&qd->qd_list, &dead);
1220 gfs2_assert_warn(sdp,
1221 atomic_read(&sdp->sd_quota_count) > 0);
1222 atomic_dec(&sdp->sd_quota_count);
1223 }
1224 }
1225 spin_unlock(&sdp->sd_quota_spin);
1226
1227 while (!list_empty(&dead)) {
1228 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1229 list_del(&qd->qd_list);
1230
1231 gfs2_assert_warn(sdp, !qd->qd_change);
1232 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1233 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1234
1235 gfs2_lvb_unhold(qd->qd_gl);
1236 kfree(qd);
1237 }
1238}
1239
1240void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1241{
1242 struct list_head *head = &sdp->sd_quota_list;
1243 struct gfs2_quota_data *qd;
1244 unsigned int x;
1245
1246 spin_lock(&sdp->sd_quota_spin);
1247 while (!list_empty(head)) {
1248 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1249
1250 if (qd->qd_count > 1 ||
1251 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1252 list_move(&qd->qd_list, head);
1253 spin_unlock(&sdp->sd_quota_spin);
1254 schedule();
1255 spin_lock(&sdp->sd_quota_spin);
1256 continue;
1257 }
1258
1259 list_del(&qd->qd_list);
1260 atomic_dec(&sdp->sd_quota_count);
1261 spin_unlock(&sdp->sd_quota_spin);
1262
1263 if (!qd->qd_count) {
1264 gfs2_assert_warn(sdp, !qd->qd_change);
1265 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1266 } else
1267 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1268 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1269
1270 gfs2_lvb_unhold(qd->qd_gl);
1271 kfree(qd);
1272
1273 spin_lock(&sdp->sd_quota_spin);
1274 }
1275 spin_unlock(&sdp->sd_quota_spin);
1276
1277 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1278
1279 if (sdp->sd_quota_bitmap) {
1280 for (x = 0; x < sdp->sd_quota_chunks; x++)
1281 kfree(sdp->sd_quota_bitmap[x]);
1282 kfree(sdp->sd_quota_bitmap);
1283 }
1284}
1285