blob: 636bccfd2bcf4dfa27cf2b703a976f3ae5185dfe [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.
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 Whitehouse18ec7d52006-02-08 11:50:51 +000062#include "ops_address.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050063#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000064
65#define QUOTA_USER 1
66#define QUOTA_GROUP 0
67
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010068struct gfs2_quota_host {
69 u64 qu_limit;
70 u64 qu_warn;
71 s64 qu_value;
Abhijith Das2d9a4bb2007-08-15 11:25:05 -050072 u32 qu_ll_next;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010073};
74
75struct gfs2_quota_change_host {
76 u64 qc_change;
77 u32 qc_flags; /* GFS2_QCF_... */
78 u32 qc_id;
79};
80
Steven Whitehousecd915492006-09-04 12:49:07 -040081static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000082{
Steven Whitehousecd915492006-09-04 12:49:07 -040083 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +000084
Steven Whitehousecd915492006-09-04 12:49:07 -040085 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 offset *= sizeof(struct gfs2_quota);
87
88 return offset;
89}
90
Steven Whitehousecd915492006-09-04 12:49:07 -040091static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 struct gfs2_quota_data **qdp)
93{
94 struct gfs2_quota_data *qd;
95 int error;
96
97 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
98 if (!qd)
99 return -ENOMEM;
100
101 qd->qd_count = 1;
102 qd->qd_id = id;
103 if (user)
104 set_bit(QDF_USER, &qd->qd_flags);
105 qd->qd_slot = -1;
106
Steven Whitehousecd915492006-09-04 12:49:07 -0400107 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 &gfs2_quota_glops, CREATE, &qd->qd_gl);
109 if (error)
110 goto fail;
111
112 error = gfs2_lvb_hold(qd->qd_gl);
113 gfs2_glock_put(qd->qd_gl);
114 if (error)
115 goto fail;
116
117 *qdp = qd;
118
119 return 0;
120
Steven Whitehousea91ea692006-09-04 12:04:26 -0400121fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 kfree(qd);
123 return error;
124}
125
Steven Whitehousecd915492006-09-04 12:49:07 -0400126static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 struct gfs2_quota_data **qdp)
128{
129 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
130 int error, found;
131
132 *qdp = NULL;
133
134 for (;;) {
135 found = 0;
136 spin_lock(&sdp->sd_quota_spin);
137 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
138 if (qd->qd_id == id &&
139 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
140 qd->qd_count++;
141 found = 1;
142 break;
143 }
144 }
145
146 if (!found)
147 qd = NULL;
148
149 if (!qd && new_qd) {
150 qd = new_qd;
151 list_add(&qd->qd_list, &sdp->sd_quota_list);
152 atomic_inc(&sdp->sd_quota_count);
153 new_qd = NULL;
154 }
155
156 spin_unlock(&sdp->sd_quota_spin);
157
158 if (qd || !create) {
159 if (new_qd) {
160 gfs2_lvb_unhold(new_qd->qd_gl);
161 kfree(new_qd);
162 }
163 *qdp = qd;
164 return 0;
165 }
166
167 error = qd_alloc(sdp, user, id, &new_qd);
168 if (error)
169 return error;
170 }
171}
172
173static void qd_hold(struct gfs2_quota_data *qd)
174{
175 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
176
177 spin_lock(&sdp->sd_quota_spin);
178 gfs2_assert(sdp, qd->qd_count);
179 qd->qd_count++;
180 spin_unlock(&sdp->sd_quota_spin);
181}
182
183static void qd_put(struct gfs2_quota_data *qd)
184{
185 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
186 spin_lock(&sdp->sd_quota_spin);
187 gfs2_assert(sdp, qd->qd_count);
188 if (!--qd->qd_count)
189 qd->qd_last_touched = jiffies;
190 spin_unlock(&sdp->sd_quota_spin);
191}
192
193static int slot_get(struct gfs2_quota_data *qd)
194{
195 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
196 unsigned int c, o = 0, b;
197 unsigned char byte = 0;
198
199 spin_lock(&sdp->sd_quota_spin);
200
201 if (qd->qd_slot_count++) {
202 spin_unlock(&sdp->sd_quota_spin);
203 return 0;
204 }
205
206 for (c = 0; c < sdp->sd_quota_chunks; c++)
207 for (o = 0; o < PAGE_SIZE; o++) {
208 byte = sdp->sd_quota_bitmap[c][o];
209 if (byte != 0xFF)
210 goto found;
211 }
212
213 goto fail;
214
Steven Whitehousea91ea692006-09-04 12:04:26 -0400215found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 for (b = 0; b < 8; b++)
217 if (!(byte & (1 << b)))
218 break;
219 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
220
221 if (qd->qd_slot >= sdp->sd_quota_slots)
222 goto fail;
223
224 sdp->sd_quota_bitmap[c][o] |= 1 << b;
225
226 spin_unlock(&sdp->sd_quota_spin);
227
228 return 0;
229
Steven Whitehousea91ea692006-09-04 12:04:26 -0400230fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 qd->qd_slot_count--;
232 spin_unlock(&sdp->sd_quota_spin);
233 return -ENOSPC;
234}
235
236static void slot_hold(struct gfs2_quota_data *qd)
237{
238 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
239
240 spin_lock(&sdp->sd_quota_spin);
241 gfs2_assert(sdp, qd->qd_slot_count);
242 qd->qd_slot_count++;
243 spin_unlock(&sdp->sd_quota_spin);
244}
245
246static void slot_put(struct gfs2_quota_data *qd)
247{
248 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
249
250 spin_lock(&sdp->sd_quota_spin);
251 gfs2_assert(sdp, qd->qd_slot_count);
252 if (!--qd->qd_slot_count) {
253 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
254 qd->qd_slot = -1;
255 }
256 spin_unlock(&sdp->sd_quota_spin);
257}
258
259static int bh_get(struct gfs2_quota_data *qd)
260{
261 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400262 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264 struct buffer_head *bh;
265 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400266 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267
Steven Whitehousef55ab262006-02-21 12:51:39 +0000268 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269
270 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000271 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272 return 0;
273 }
274
275 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600276 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277
Steven Whitehouse23591252006-10-13 17:25:45 -0400278 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600279 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 if (error)
281 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400282 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 if (error)
284 goto fail;
285 error = -EIO;
286 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
287 goto fail_brelse;
288
289 qd->qd_bh = bh;
290 qd->qd_bh_qc = (struct gfs2_quota_change *)
291 (bh->b_data + sizeof(struct gfs2_meta_header) +
292 offset * sizeof(struct gfs2_quota_change));
293
Josef Whiter2e95b662007-02-20 00:03:29 -0500294 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295
296 return 0;
297
Steven Whitehousea91ea692006-09-04 12:04:26 -0400298fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400300fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000302 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 return error;
304}
305
306static void bh_put(struct gfs2_quota_data *qd)
307{
308 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
309
Steven Whitehousef55ab262006-02-21 12:51:39 +0000310 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 gfs2_assert(sdp, qd->qd_bh_count);
312 if (!--qd->qd_bh_count) {
313 brelse(qd->qd_bh);
314 qd->qd_bh = NULL;
315 qd->qd_bh_qc = NULL;
316 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000317 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318}
319
320static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
321{
322 struct gfs2_quota_data *qd = NULL;
323 int error;
324 int found = 0;
325
326 *qdp = NULL;
327
328 if (sdp->sd_vfs->s_flags & MS_RDONLY)
329 return 0;
330
331 spin_lock(&sdp->sd_quota_spin);
332
333 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
334 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
335 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
336 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
337 continue;
338
339 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
340
341 set_bit(QDF_LOCKED, &qd->qd_flags);
342 gfs2_assert_warn(sdp, qd->qd_count);
343 qd->qd_count++;
344 qd->qd_change_sync = qd->qd_change;
345 gfs2_assert_warn(sdp, qd->qd_slot_count);
346 qd->qd_slot_count++;
347 found = 1;
348
349 break;
350 }
351
352 if (!found)
353 qd = NULL;
354
355 spin_unlock(&sdp->sd_quota_spin);
356
357 if (qd) {
358 gfs2_assert_warn(sdp, qd->qd_change_sync);
359 error = bh_get(qd);
360 if (error) {
361 clear_bit(QDF_LOCKED, &qd->qd_flags);
362 slot_put(qd);
363 qd_put(qd);
364 return error;
365 }
366 }
367
368 *qdp = qd;
369
370 return 0;
371}
372
373static int qd_trylock(struct gfs2_quota_data *qd)
374{
375 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
376
377 if (sdp->sd_vfs->s_flags & MS_RDONLY)
378 return 0;
379
380 spin_lock(&sdp->sd_quota_spin);
381
382 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
383 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
384 spin_unlock(&sdp->sd_quota_spin);
385 return 0;
386 }
387
388 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
389
390 set_bit(QDF_LOCKED, &qd->qd_flags);
391 gfs2_assert_warn(sdp, qd->qd_count);
392 qd->qd_count++;
393 qd->qd_change_sync = qd->qd_change;
394 gfs2_assert_warn(sdp, qd->qd_slot_count);
395 qd->qd_slot_count++;
396
397 spin_unlock(&sdp->sd_quota_spin);
398
399 gfs2_assert_warn(sdp, qd->qd_change_sync);
400 if (bh_get(qd)) {
401 clear_bit(QDF_LOCKED, &qd->qd_flags);
402 slot_put(qd);
403 qd_put(qd);
404 return 0;
405 }
406
407 return 1;
408}
409
410static void qd_unlock(struct gfs2_quota_data *qd)
411{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500412 gfs2_assert_warn(qd->qd_gl->gl_sbd,
413 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 clear_bit(QDF_LOCKED, &qd->qd_flags);
415 bh_put(qd);
416 slot_put(qd);
417 qd_put(qd);
418}
419
Steven Whitehousecd915492006-09-04 12:49:07 -0400420static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 struct gfs2_quota_data **qdp)
422{
423 int error;
424
425 error = qd_get(sdp, user, id, create, qdp);
426 if (error)
427 return error;
428
429 error = slot_get(*qdp);
430 if (error)
431 goto fail;
432
433 error = bh_get(*qdp);
434 if (error)
435 goto fail_slot;
436
437 return 0;
438
Steven Whitehousea91ea692006-09-04 12:04:26 -0400439fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400441fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 qd_put(*qdp);
443 return error;
444}
445
446static void qdsb_put(struct gfs2_quota_data *qd)
447{
448 bh_put(qd);
449 slot_put(qd);
450 qd_put(qd);
451}
452
Steven Whitehousecd915492006-09-04 12:49:07 -0400453int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400455 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000456 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 struct gfs2_quota_data **qd = al->al_qd;
458 int error;
459
460 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
461 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
462 return -EIO;
463
464 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
465 return 0;
466
Steven Whitehouse2933f922006-11-01 13:23:29 -0500467 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 if (error)
469 goto out;
470 al->al_qd_num++;
471 qd++;
472
Steven Whitehouse2933f922006-11-01 13:23:29 -0500473 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 if (error)
475 goto out;
476 al->al_qd_num++;
477 qd++;
478
Steven Whitehouse2933f922006-11-01 13:23:29 -0500479 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
481 if (error)
482 goto out;
483 al->al_qd_num++;
484 qd++;
485 }
486
Steven Whitehouse2933f922006-11-01 13:23:29 -0500487 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
489 if (error)
490 goto out;
491 al->al_qd_num++;
492 qd++;
493 }
494
Steven Whitehousea91ea692006-09-04 12:04:26 -0400495out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496 if (error)
497 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498 return error;
499}
500
501void gfs2_quota_unhold(struct gfs2_inode *ip)
502{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400503 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000504 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 unsigned int x;
506
507 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
508
509 for (x = 0; x < al->al_qd_num; x++) {
510 qdsb_put(al->al_qd[x]);
511 al->al_qd[x] = NULL;
512 }
513 al->al_qd_num = 0;
514}
515
516static int sort_qd(const void *a, const void *b)
517{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400518 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
519 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520
521 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
522 !test_bit(QDF_USER, &qd_b->qd_flags)) {
523 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400524 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400526 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400528 if (qd_a->qd_id < qd_b->qd_id)
529 return -1;
530 if (qd_a->qd_id > qd_b->qd_id)
531 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532
Steven Whitehouse48fac172006-09-05 15:17:12 -0400533 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534}
535
Steven Whitehousecd915492006-09-04 12:49:07 -0400536static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537{
538 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400539 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400541 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542
Steven Whitehousef55ab262006-02-21 12:51:39 +0000543 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000544 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545
546 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
547 qc->qc_change = 0;
548 qc->qc_flags = 0;
549 if (test_bit(QDF_USER, &qd->qd_flags))
550 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
551 qc->qc_id = cpu_to_be32(qd->qd_id);
552 }
553
Al Virob44b84d2006-10-14 10:46:30 -0400554 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 qc->qc_change = cpu_to_be64(x);
556
557 spin_lock(&sdp->sd_quota_spin);
558 qd->qd_change = x;
559 spin_unlock(&sdp->sd_quota_spin);
560
561 if (!x) {
562 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
563 clear_bit(QDF_CHANGE, &qd->qd_flags);
564 qc->qc_flags = 0;
565 qc->qc_id = 0;
566 slot_put(qd);
567 qd_put(qd);
568 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
569 qd_hold(qd);
570 slot_hold(qd);
571 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400572
Steven Whitehousef55ab262006-02-21 12:51:39 +0000573 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574}
575
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100576static void gfs2_quota_in(struct gfs2_quota_host *qu, const void *buf)
577{
578 const struct gfs2_quota *str = buf;
579
580 qu->qu_limit = be64_to_cpu(str->qu_limit);
581 qu->qu_warn = be64_to_cpu(str->qu_warn);
582 qu->qu_value = be64_to_cpu(str->qu_value);
Abhijith Das2d9a4bb2007-08-15 11:25:05 -0500583 qu->qu_ll_next = be32_to_cpu(str->qu_ll_next);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100584}
585
586static void gfs2_quota_out(const struct gfs2_quota_host *qu, void *buf)
587{
588 struct gfs2_quota *str = buf;
589
590 str->qu_limit = cpu_to_be64(qu->qu_limit);
591 str->qu_warn = cpu_to_be64(qu->qu_warn);
592 str->qu_value = cpu_to_be64(qu->qu_value);
Abhijith Das2d9a4bb2007-08-15 11:25:05 -0500593 str->qu_ll_next = cpu_to_be32(qu->qu_ll_next);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100594 memset(&str->qu_reserved, 0, sizeof(str->qu_reserved));
595}
596
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000597/**
598 * gfs2_adjust_quota
599 *
600 * This function was mostly borrowed from gfs2_block_truncate_page which was
601 * in turn mostly borrowed from ext3
602 */
603static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousecd915492006-09-04 12:49:07 -0400604 s64 change, struct gfs2_quota_data *qd)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000605{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400606 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000607 struct address_space *mapping = inode->i_mapping;
608 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500609 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000610 unsigned blocksize, iblock, pos;
611 struct buffer_head *bh;
612 struct page *page;
613 void *kaddr;
Abhijith Das1990e912007-05-31 17:52:02 -0500614 char *ptr;
615 struct gfs2_quota_host qp;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400616 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000617 int err = -EIO;
618
Abhijith Das0fd53552007-08-14 15:34:58 -0500619 if (gfs2_is_stuffed(ip)) {
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300620 struct gfs2_alloc *al = gfs2_alloc_get(ip);
621 if (!al)
622 return -ENOMEM;
Abhijith Das0fd53552007-08-14 15:34:58 -0500623 /* 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)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600649 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000650 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);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300733 if (!al) {
734 error = -ENOMEM;
735 goto out_gunlock;
736 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737
738 al->al_requested = nalloc * (data_blocks + ind_blocks);
739
740 error = gfs2_inplace_reserve(ip);
741 if (error)
742 goto out_alloc;
743
744 error = gfs2_trans_begin(sdp,
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100745 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 num_qd * data_blocks +
747 nalloc * ind_blocks +
748 RES_DINODE + num_qd +
749 RES_STATFS, 0);
750 if (error)
751 goto out_ipres;
752 } else {
753 error = gfs2_trans_begin(sdp,
754 num_qd * data_blocks +
755 RES_DINODE + num_qd, 0);
756 if (error)
757 goto out_gunlock;
758 }
759
760 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 qd = qda[x];
762 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000763 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500764 (struct gfs2_quota_data *)
Abhijith Das2a87ab02007-05-16 17:02:19 -0500765 qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000766 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 goto out_end_trans;
768
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770 }
771
772 error = 0;
773
Steven Whitehousea91ea692006-09-04 12:04:26 -0400774out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400776out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 if (nalloc)
778 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400779out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 if (nalloc)
781 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400782out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400784out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 while (qx--)
786 gfs2_glock_dq_uninit(&ghs[qx]);
787 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400788 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 return error;
790}
791
792static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
793 struct gfs2_holder *q_gh)
794{
795 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400796 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 struct gfs2_holder i_gh;
Al Virob5bc9e82006-10-13 23:31:55 -0400798 struct gfs2_quota_host q;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 char buf[sizeof(struct gfs2_quota)];
800 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400801 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802
Steven Whitehousea91ea692006-09-04 12:04:26 -0400803restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
805 if (error)
806 return error;
807
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400808 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400810 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000811 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 gfs2_glock_dq_uninit(q_gh);
813 error = gfs2_glock_nq_init(qd->qd_gl,
814 LM_ST_EXCLUSIVE, GL_NOCACHE,
815 q_gh);
816 if (error)
817 return error;
818
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400819 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 if (error)
821 goto fail;
822
823 memset(buf, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000824 pos = qd2offset(qd);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100825 error = gfs2_internal_read(ip, NULL, buf, &pos,
826 sizeof(struct gfs2_quota));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 if (error < 0)
828 goto fail_gunlock;
829
830 gfs2_glock_dq_uninit(&i_gh);
831
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400832
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 gfs2_quota_in(&q, buf);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400834 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
835 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
836 qlvb->__pad = 0;
837 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
838 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
839 qlvb->qb_value = cpu_to_be64(q.qu_value);
840 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841
842 if (gfs2_glock_is_blocking(qd->qd_gl)) {
843 gfs2_glock_dq_uninit(q_gh);
844 force_refresh = 0;
845 goto restart;
846 }
847 }
848
849 return 0;
850
Steven Whitehousea91ea692006-09-04 12:04:26 -0400851fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400853fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 return error;
856}
857
Steven Whitehousecd915492006-09-04 12:49:07 -0400858int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400860 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000861 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 unsigned int x;
863 int error = 0;
864
865 gfs2_quota_hold(ip, uid, gid);
866
867 if (capable(CAP_SYS_RESOURCE) ||
868 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
869 return 0;
870
871 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
872 sort_qd, NULL);
873
874 for (x = 0; x < al->al_qd_num; x++) {
875 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
876 if (error)
877 break;
878 }
879
880 if (!error)
881 set_bit(GIF_QD_LOCKED, &ip->i_flags);
882 else {
883 while (x--)
884 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
885 gfs2_quota_unhold(ip);
886 }
887
888 return error;
889}
890
891static int need_sync(struct gfs2_quota_data *qd)
892{
893 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
894 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400895 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 unsigned int num, den;
897 int do_sync = 1;
898
899 if (!qd->qd_qb.qb_limit)
900 return 0;
901
902 spin_lock(&sdp->sd_quota_spin);
903 value = qd->qd_change;
904 spin_unlock(&sdp->sd_quota_spin);
905
906 spin_lock(&gt->gt_spin);
907 num = gt->gt_quota_scale_num;
908 den = gt->gt_quota_scale_den;
909 spin_unlock(&gt->gt_spin);
910
911 if (value < 0)
912 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400913 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
914 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 do_sync = 0;
916 else {
917 value *= gfs2_jindex_size(sdp) * num;
918 do_div(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400919 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -0400920 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 do_sync = 0;
922 }
923
924 return do_sync;
925}
926
927void gfs2_quota_unlock(struct gfs2_inode *ip)
928{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000929 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 struct gfs2_quota_data *qda[4];
931 unsigned int count = 0;
932 unsigned int x;
933
934 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
935 goto out;
936
937 for (x = 0; x < al->al_qd_num; x++) {
938 struct gfs2_quota_data *qd;
939 int sync;
940
941 qd = al->al_qd[x];
942 sync = need_sync(qd);
943
944 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
945
946 if (sync && qd_trylock(qd))
947 qda[count++] = qd;
948 }
949
950 if (count) {
951 do_sync(count, qda);
952 for (x = 0; x < count; x++)
953 qd_unlock(qda[x]);
954 }
955
Steven Whitehousea91ea692006-09-04 12:04:26 -0400956out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957 gfs2_quota_unhold(ip);
958}
959
960#define MAX_LINE 256
961
962static int print_message(struct gfs2_quota_data *qd, char *type)
963{
964 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965
Steven Whitehouse02630a12006-07-03 11:20:06 -0400966 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
967 sdp->sd_fsname, type,
968 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
969 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970
971 return 0;
972}
973
Steven Whitehousecd915492006-09-04 12:49:07 -0400974int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400976 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000977 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -0400979 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 unsigned int x;
981 int error = 0;
982
983 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
984 return 0;
985
986 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
987 return 0;
988
989 for (x = 0; x < al->al_qd_num; x++) {
990 qd = al->al_qd[x];
991
992 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
993 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
994 continue;
995
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400996 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 spin_lock(&sdp->sd_quota_spin);
998 value += qd->qd_change;
999 spin_unlock(&sdp->sd_quota_spin);
1000
Steven Whitehousecd915492006-09-04 12:49:07 -04001001 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 +00001002 print_message(qd, "exceeded");
1003 error = -EDQUOT;
1004 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001005 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001006 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001008 gfs2_tune_get(sdp,
1009 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010 error = print_message(qd, "warning");
1011 qd->qd_last_warn = jiffies;
1012 }
1013 }
1014
1015 return error;
1016}
1017
Steven Whitehousecd915492006-09-04 12:49:07 -04001018void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1019 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001021 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 struct gfs2_quota_data *qd;
1023 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001025 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 return;
1027 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
1028 return;
1029
1030 for (x = 0; x < al->al_qd_num; x++) {
1031 qd = al->al_qd[x];
1032
1033 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1034 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1035 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 }
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