blob: 8a58815dea0893d39c6e36b4a3a2342e750036b8 [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 Whitehousecd915492006-09-04 12:49:07 -040069static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000070{
Steven Whitehousecd915492006-09-04 12:49:07 -040071 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +000072
Steven Whitehousecd915492006-09-04 12:49:07 -040073 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 offset *= sizeof(struct gfs2_quota);
75
76 return offset;
77}
78
Steven Whitehousecd915492006-09-04 12:49:07 -040079static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +000080 struct gfs2_quota_data **qdp)
81{
82 struct gfs2_quota_data *qd;
83 int error;
84
85 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
86 if (!qd)
87 return -ENOMEM;
88
89 qd->qd_count = 1;
90 qd->qd_id = id;
91 if (user)
92 set_bit(QDF_USER, &qd->qd_flags);
93 qd->qd_slot = -1;
94
Steven Whitehousecd915492006-09-04 12:49:07 -040095 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
David Teiglandb3b94fa2006-01-16 16:50:04 +000096 &gfs2_quota_glops, CREATE, &qd->qd_gl);
97 if (error)
98 goto fail;
99
100 error = gfs2_lvb_hold(qd->qd_gl);
101 gfs2_glock_put(qd->qd_gl);
102 if (error)
103 goto fail;
104
105 *qdp = qd;
106
107 return 0;
108
Steven Whitehousea91ea692006-09-04 12:04:26 -0400109fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 kfree(qd);
111 return error;
112}
113
Steven Whitehousecd915492006-09-04 12:49:07 -0400114static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115 struct gfs2_quota_data **qdp)
116{
117 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
118 int error, found;
119
120 *qdp = NULL;
121
122 for (;;) {
123 found = 0;
124 spin_lock(&sdp->sd_quota_spin);
125 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
126 if (qd->qd_id == id &&
127 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
128 qd->qd_count++;
129 found = 1;
130 break;
131 }
132 }
133
134 if (!found)
135 qd = NULL;
136
137 if (!qd && new_qd) {
138 qd = new_qd;
139 list_add(&qd->qd_list, &sdp->sd_quota_list);
140 atomic_inc(&sdp->sd_quota_count);
141 new_qd = NULL;
142 }
143
144 spin_unlock(&sdp->sd_quota_spin);
145
146 if (qd || !create) {
147 if (new_qd) {
148 gfs2_lvb_unhold(new_qd->qd_gl);
149 kfree(new_qd);
150 }
151 *qdp = qd;
152 return 0;
153 }
154
155 error = qd_alloc(sdp, user, id, &new_qd);
156 if (error)
157 return error;
158 }
159}
160
161static void qd_hold(struct gfs2_quota_data *qd)
162{
163 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
164
165 spin_lock(&sdp->sd_quota_spin);
166 gfs2_assert(sdp, qd->qd_count);
167 qd->qd_count++;
168 spin_unlock(&sdp->sd_quota_spin);
169}
170
171static void qd_put(struct gfs2_quota_data *qd)
172{
173 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
174 spin_lock(&sdp->sd_quota_spin);
175 gfs2_assert(sdp, qd->qd_count);
176 if (!--qd->qd_count)
177 qd->qd_last_touched = jiffies;
178 spin_unlock(&sdp->sd_quota_spin);
179}
180
181static int slot_get(struct gfs2_quota_data *qd)
182{
183 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
184 unsigned int c, o = 0, b;
185 unsigned char byte = 0;
186
187 spin_lock(&sdp->sd_quota_spin);
188
189 if (qd->qd_slot_count++) {
190 spin_unlock(&sdp->sd_quota_spin);
191 return 0;
192 }
193
194 for (c = 0; c < sdp->sd_quota_chunks; c++)
195 for (o = 0; o < PAGE_SIZE; o++) {
196 byte = sdp->sd_quota_bitmap[c][o];
197 if (byte != 0xFF)
198 goto found;
199 }
200
201 goto fail;
202
Steven Whitehousea91ea692006-09-04 12:04:26 -0400203found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 for (b = 0; b < 8; b++)
205 if (!(byte & (1 << b)))
206 break;
207 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
208
209 if (qd->qd_slot >= sdp->sd_quota_slots)
210 goto fail;
211
212 sdp->sd_quota_bitmap[c][o] |= 1 << b;
213
214 spin_unlock(&sdp->sd_quota_spin);
215
216 return 0;
217
Steven Whitehousea91ea692006-09-04 12:04:26 -0400218fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 qd->qd_slot_count--;
220 spin_unlock(&sdp->sd_quota_spin);
221 return -ENOSPC;
222}
223
224static void slot_hold(struct gfs2_quota_data *qd)
225{
226 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
227
228 spin_lock(&sdp->sd_quota_spin);
229 gfs2_assert(sdp, qd->qd_slot_count);
230 qd->qd_slot_count++;
231 spin_unlock(&sdp->sd_quota_spin);
232}
233
234static void slot_put(struct gfs2_quota_data *qd)
235{
236 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
237
238 spin_lock(&sdp->sd_quota_spin);
239 gfs2_assert(sdp, qd->qd_slot_count);
240 if (!--qd->qd_slot_count) {
241 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
242 qd->qd_slot = -1;
243 }
244 spin_unlock(&sdp->sd_quota_spin);
245}
246
247static int bh_get(struct gfs2_quota_data *qd)
248{
249 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252 struct buffer_head *bh;
253 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400254 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255
Steven Whitehousef55ab262006-02-21 12:51:39 +0000256 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257
258 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000259 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 return 0;
261 }
262
263 block = qd->qd_slot / sdp->sd_qc_per_block;
264 offset = qd->qd_slot % sdp->sd_qc_per_block;;
265
Steven Whitehouse23591252006-10-13 17:25:45 -0400266 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
267 error = gfs2_block_map(&ip->i_inode, block, 0, &bh_map);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 if (error)
269 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400270 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 if (error)
272 goto fail;
273 error = -EIO;
274 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
275 goto fail_brelse;
276
277 qd->qd_bh = bh;
278 qd->qd_bh_qc = (struct gfs2_quota_change *)
279 (bh->b_data + sizeof(struct gfs2_meta_header) +
280 offset * sizeof(struct gfs2_quota_change));
281
Josef Whiter2e95b662007-02-20 00:03:29 -0500282 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283
284 return 0;
285
Steven Whitehousea91ea692006-09-04 12:04:26 -0400286fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400288fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000290 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291 return error;
292}
293
294static void bh_put(struct gfs2_quota_data *qd)
295{
296 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
297
Steven Whitehousef55ab262006-02-21 12:51:39 +0000298 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 gfs2_assert(sdp, qd->qd_bh_count);
300 if (!--qd->qd_bh_count) {
301 brelse(qd->qd_bh);
302 qd->qd_bh = NULL;
303 qd->qd_bh_qc = NULL;
304 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000305 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306}
307
308static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
309{
310 struct gfs2_quota_data *qd = NULL;
311 int error;
312 int found = 0;
313
314 *qdp = NULL;
315
316 if (sdp->sd_vfs->s_flags & MS_RDONLY)
317 return 0;
318
319 spin_lock(&sdp->sd_quota_spin);
320
321 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
322 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
323 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
324 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
325 continue;
326
327 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
328
329 set_bit(QDF_LOCKED, &qd->qd_flags);
330 gfs2_assert_warn(sdp, qd->qd_count);
331 qd->qd_count++;
332 qd->qd_change_sync = qd->qd_change;
333 gfs2_assert_warn(sdp, qd->qd_slot_count);
334 qd->qd_slot_count++;
335 found = 1;
336
337 break;
338 }
339
340 if (!found)
341 qd = NULL;
342
343 spin_unlock(&sdp->sd_quota_spin);
344
345 if (qd) {
346 gfs2_assert_warn(sdp, qd->qd_change_sync);
347 error = bh_get(qd);
348 if (error) {
349 clear_bit(QDF_LOCKED, &qd->qd_flags);
350 slot_put(qd);
351 qd_put(qd);
352 return error;
353 }
354 }
355
356 *qdp = qd;
357
358 return 0;
359}
360
361static int qd_trylock(struct gfs2_quota_data *qd)
362{
363 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
364
365 if (sdp->sd_vfs->s_flags & MS_RDONLY)
366 return 0;
367
368 spin_lock(&sdp->sd_quota_spin);
369
370 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
371 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
372 spin_unlock(&sdp->sd_quota_spin);
373 return 0;
374 }
375
376 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
377
378 set_bit(QDF_LOCKED, &qd->qd_flags);
379 gfs2_assert_warn(sdp, qd->qd_count);
380 qd->qd_count++;
381 qd->qd_change_sync = qd->qd_change;
382 gfs2_assert_warn(sdp, qd->qd_slot_count);
383 qd->qd_slot_count++;
384
385 spin_unlock(&sdp->sd_quota_spin);
386
387 gfs2_assert_warn(sdp, qd->qd_change_sync);
388 if (bh_get(qd)) {
389 clear_bit(QDF_LOCKED, &qd->qd_flags);
390 slot_put(qd);
391 qd_put(qd);
392 return 0;
393 }
394
395 return 1;
396}
397
398static void qd_unlock(struct gfs2_quota_data *qd)
399{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500400 gfs2_assert_warn(qd->qd_gl->gl_sbd,
401 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402 clear_bit(QDF_LOCKED, &qd->qd_flags);
403 bh_put(qd);
404 slot_put(qd);
405 qd_put(qd);
406}
407
Steven Whitehousecd915492006-09-04 12:49:07 -0400408static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 struct gfs2_quota_data **qdp)
410{
411 int error;
412
413 error = qd_get(sdp, user, id, create, qdp);
414 if (error)
415 return error;
416
417 error = slot_get(*qdp);
418 if (error)
419 goto fail;
420
421 error = bh_get(*qdp);
422 if (error)
423 goto fail_slot;
424
425 return 0;
426
Steven Whitehousea91ea692006-09-04 12:04:26 -0400427fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400429fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 qd_put(*qdp);
431 return error;
432}
433
434static void qdsb_put(struct gfs2_quota_data *qd)
435{
436 bh_put(qd);
437 slot_put(qd);
438 qd_put(qd);
439}
440
Steven Whitehousecd915492006-09-04 12:49:07 -0400441int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400443 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 struct gfs2_alloc *al = &ip->i_alloc;
445 struct gfs2_quota_data **qd = al->al_qd;
446 int error;
447
448 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
449 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
450 return -EIO;
451
452 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
453 return 0;
454
Steven Whitehouse2933f922006-11-01 13:23:29 -0500455 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 if (error)
457 goto out;
458 al->al_qd_num++;
459 qd++;
460
Steven Whitehouse2933f922006-11-01 13:23:29 -0500461 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 if (error)
463 goto out;
464 al->al_qd_num++;
465 qd++;
466
Steven Whitehouse2933f922006-11-01 13:23:29 -0500467 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
469 if (error)
470 goto out;
471 al->al_qd_num++;
472 qd++;
473 }
474
Steven Whitehouse2933f922006-11-01 13:23:29 -0500475 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
477 if (error)
478 goto out;
479 al->al_qd_num++;
480 qd++;
481 }
482
Steven Whitehousea91ea692006-09-04 12:04:26 -0400483out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 if (error)
485 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 return error;
487}
488
489void gfs2_quota_unhold(struct gfs2_inode *ip)
490{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400491 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 struct gfs2_alloc *al = &ip->i_alloc;
493 unsigned int x;
494
495 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
496
497 for (x = 0; x < al->al_qd_num; x++) {
498 qdsb_put(al->al_qd[x]);
499 al->al_qd[x] = NULL;
500 }
501 al->al_qd_num = 0;
502}
503
504static int sort_qd(const void *a, const void *b)
505{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400506 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
507 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508
509 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
510 !test_bit(QDF_USER, &qd_b->qd_flags)) {
511 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400512 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400514 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400516 if (qd_a->qd_id < qd_b->qd_id)
517 return -1;
518 if (qd_a->qd_id > qd_b->qd_id)
519 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520
Steven Whitehouse48fac172006-09-05 15:17:12 -0400521 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522}
523
Steven Whitehousecd915492006-09-04 12:49:07 -0400524static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525{
526 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400527 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400529 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
Steven Whitehousef55ab262006-02-21 12:51:39 +0000531 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000532 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533
534 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
535 qc->qc_change = 0;
536 qc->qc_flags = 0;
537 if (test_bit(QDF_USER, &qd->qd_flags))
538 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
539 qc->qc_id = cpu_to_be32(qd->qd_id);
540 }
541
Al Virob44b84d2006-10-14 10:46:30 -0400542 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 qc->qc_change = cpu_to_be64(x);
544
545 spin_lock(&sdp->sd_quota_spin);
546 qd->qd_change = x;
547 spin_unlock(&sdp->sd_quota_spin);
548
549 if (!x) {
550 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
551 clear_bit(QDF_CHANGE, &qd->qd_flags);
552 qc->qc_flags = 0;
553 qc->qc_id = 0;
554 slot_put(qd);
555 qd_put(qd);
556 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
557 qd_hold(qd);
558 slot_hold(qd);
559 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400560
Steven Whitehousef55ab262006-02-21 12:51:39 +0000561 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562}
563
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000564/**
565 * gfs2_adjust_quota
566 *
567 * This function was mostly borrowed from gfs2_block_truncate_page which was
568 * in turn mostly borrowed from ext3
569 */
570static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousecd915492006-09-04 12:49:07 -0400571 s64 change, struct gfs2_quota_data *qd)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000572{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400573 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000574 struct address_space *mapping = inode->i_mapping;
575 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500576 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000577 unsigned blocksize, iblock, pos;
578 struct buffer_head *bh;
579 struct page *page;
580 void *kaddr;
Abhijith Das1990e912007-05-31 17:52:02 -0500581 char *ptr;
582 struct gfs2_quota_host qp;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400583 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000584 int err = -EIO;
585
586 page = grab_cache_page(mapping, index);
587 if (!page)
588 return -ENOMEM;
589
590 blocksize = inode->i_sb->s_blocksize;
591 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
592
593 if (!page_has_buffers(page))
594 create_empty_buffers(page, blocksize, 0);
595
596 bh = page_buffers(page);
597 pos = blocksize;
598 while (offset >= pos) {
599 bh = bh->b_this_page;
600 iblock++;
601 pos += blocksize;
602 }
603
604 if (!buffer_mapped(bh)) {
605 gfs2_get_block(inode, iblock, bh, 1);
606 if (!buffer_mapped(bh))
607 goto unlock;
608 }
609
610 if (PageUptodate(page))
611 set_buffer_uptodate(bh);
612
613 if (!buffer_uptodate(bh)) {
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400614 ll_rw_block(READ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000615 wait_on_buffer(bh);
616 if (!buffer_uptodate(bh))
617 goto unlock;
618 }
619
620 gfs2_trans_add_bh(ip->i_gl, bh, 0);
621
622 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse48fac172006-09-05 15:17:12 -0400623 ptr = kaddr + offset;
Abhijith Das1990e912007-05-31 17:52:02 -0500624 gfs2_quota_in(&qp, ptr);
625 qp.qu_value += change;
626 value = qp.qu_value;
627 gfs2_quota_out(&qp, ptr);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000628 flush_dcache_page(page);
629 kunmap_atomic(kaddr, KM_USER0);
630 err = 0;
631 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000632 qd->qd_qb.qb_value = cpu_to_be64(value);
Abhijith Das2a87ab02007-05-16 17:02:19 -0500633 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC);
634 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000635unlock:
636 unlock_page(page);
637 page_cache_release(page);
638 return err;
639}
640
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
642{
643 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400644 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645 unsigned int data_blocks, ind_blocks;
646 struct gfs2_holder *ghs, i_gh;
647 unsigned int qx, x;
648 struct gfs2_quota_data *qd;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000649 loff_t offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 unsigned int nalloc = 0;
651 struct gfs2_alloc *al = NULL;
652 int error;
653
654 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
655 &data_blocks, &ind_blocks);
656
657 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
658 if (!ghs)
659 return -ENOMEM;
660
661 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
662 for (qx = 0; qx < num_qd; qx++) {
663 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
664 LM_ST_EXCLUSIVE,
665 GL_NOCACHE, &ghs[qx]);
666 if (error)
667 goto out;
668 }
669
670 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
671 if (error)
672 goto out;
673
674 for (x = 0; x < num_qd; x++) {
675 int alloc_required;
676
677 offset = qd2offset(qda[x]);
678 error = gfs2_write_alloc_required(ip, offset,
679 sizeof(struct gfs2_quota),
680 &alloc_required);
681 if (error)
682 goto out_gunlock;
683 if (alloc_required)
684 nalloc++;
685 }
686
687 if (nalloc) {
688 al = gfs2_alloc_get(ip);
689
690 al->al_requested = nalloc * (data_blocks + ind_blocks);
691
692 error = gfs2_inplace_reserve(ip);
693 if (error)
694 goto out_alloc;
695
696 error = gfs2_trans_begin(sdp,
697 al->al_rgd->rd_ri.ri_length +
698 num_qd * data_blocks +
699 nalloc * ind_blocks +
700 RES_DINODE + num_qd +
701 RES_STATFS, 0);
702 if (error)
703 goto out_ipres;
704 } else {
705 error = gfs2_trans_begin(sdp,
706 num_qd * data_blocks +
707 RES_DINODE + num_qd, 0);
708 if (error)
709 goto out_gunlock;
710 }
711
712 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 qd = qda[x];
714 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000715 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500716 (struct gfs2_quota_data *)
Abhijith Das2a87ab02007-05-16 17:02:19 -0500717 qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000718 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 goto out_end_trans;
720
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722 }
723
724 error = 0;
725
Steven Whitehousea91ea692006-09-04 12:04:26 -0400726out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400728out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 if (nalloc)
730 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400731out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 if (nalloc)
733 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400734out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400736out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 while (qx--)
738 gfs2_glock_dq_uninit(&ghs[qx]);
739 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400740 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 return error;
742}
743
744static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
745 struct gfs2_holder *q_gh)
746{
747 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400748 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 struct gfs2_holder i_gh;
Al Virob5bc9e82006-10-13 23:31:55 -0400750 struct gfs2_quota_host q;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 char buf[sizeof(struct gfs2_quota)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000752 struct file_ra_state ra_state;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400754 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755
Steven Whitehousef42faf42006-01-30 18:34:10 +0000756 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400757restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
759 if (error)
760 return error;
761
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400762 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400764 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000765 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 gfs2_glock_dq_uninit(q_gh);
767 error = gfs2_glock_nq_init(qd->qd_gl,
768 LM_ST_EXCLUSIVE, GL_NOCACHE,
769 q_gh);
770 if (error)
771 return error;
772
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400773 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 if (error)
775 goto fail;
776
777 memset(buf, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000778 pos = qd2offset(qd);
Steven Whitehouse0d42e542006-06-20 16:13:49 -0400779 error = gfs2_internal_read(ip, &ra_state, buf,
780 &pos, sizeof(struct gfs2_quota));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 if (error < 0)
782 goto fail_gunlock;
783
784 gfs2_glock_dq_uninit(&i_gh);
785
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400786
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_quota_in(&q, buf);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400788 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
789 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
790 qlvb->__pad = 0;
791 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
792 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
793 qlvb->qb_value = cpu_to_be64(q.qu_value);
794 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795
796 if (gfs2_glock_is_blocking(qd->qd_gl)) {
797 gfs2_glock_dq_uninit(q_gh);
798 force_refresh = 0;
799 goto restart;
800 }
801 }
802
803 return 0;
804
Steven Whitehousea91ea692006-09-04 12:04:26 -0400805fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400807fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 return error;
810}
811
Steven Whitehousecd915492006-09-04 12:49:07 -0400812int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400814 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 struct gfs2_alloc *al = &ip->i_alloc;
816 unsigned int x;
817 int error = 0;
818
819 gfs2_quota_hold(ip, uid, gid);
820
821 if (capable(CAP_SYS_RESOURCE) ||
822 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
823 return 0;
824
825 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
826 sort_qd, NULL);
827
828 for (x = 0; x < al->al_qd_num; x++) {
829 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
830 if (error)
831 break;
832 }
833
834 if (!error)
835 set_bit(GIF_QD_LOCKED, &ip->i_flags);
836 else {
837 while (x--)
838 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
839 gfs2_quota_unhold(ip);
840 }
841
842 return error;
843}
844
845static int need_sync(struct gfs2_quota_data *qd)
846{
847 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
848 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400849 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 unsigned int num, den;
851 int do_sync = 1;
852
853 if (!qd->qd_qb.qb_limit)
854 return 0;
855
856 spin_lock(&sdp->sd_quota_spin);
857 value = qd->qd_change;
858 spin_unlock(&sdp->sd_quota_spin);
859
860 spin_lock(&gt->gt_spin);
861 num = gt->gt_quota_scale_num;
862 den = gt->gt_quota_scale_den;
863 spin_unlock(&gt->gt_spin);
864
865 if (value < 0)
866 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400867 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
868 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 do_sync = 0;
870 else {
871 value *= gfs2_jindex_size(sdp) * num;
872 do_div(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400873 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -0400874 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 do_sync = 0;
876 }
877
878 return do_sync;
879}
880
881void gfs2_quota_unlock(struct gfs2_inode *ip)
882{
883 struct gfs2_alloc *al = &ip->i_alloc;
884 struct gfs2_quota_data *qda[4];
885 unsigned int count = 0;
886 unsigned int x;
887
888 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
889 goto out;
890
891 for (x = 0; x < al->al_qd_num; x++) {
892 struct gfs2_quota_data *qd;
893 int sync;
894
895 qd = al->al_qd[x];
896 sync = need_sync(qd);
897
898 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
899
900 if (sync && qd_trylock(qd))
901 qda[count++] = qd;
902 }
903
904 if (count) {
905 do_sync(count, qda);
906 for (x = 0; x < count; x++)
907 qd_unlock(qda[x]);
908 }
909
Steven Whitehousea91ea692006-09-04 12:04:26 -0400910out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 gfs2_quota_unhold(ip);
912}
913
914#define MAX_LINE 256
915
916static int print_message(struct gfs2_quota_data *qd, char *type)
917{
918 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919
Steven Whitehouse02630a12006-07-03 11:20:06 -0400920 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
921 sdp->sd_fsname, type,
922 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
923 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924
925 return 0;
926}
927
Steven Whitehousecd915492006-09-04 12:49:07 -0400928int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400930 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 struct gfs2_alloc *al = &ip->i_alloc;
932 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -0400933 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 unsigned int x;
935 int error = 0;
936
937 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
938 return 0;
939
940 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
941 return 0;
942
943 for (x = 0; x < al->al_qd_num; x++) {
944 qd = al->al_qd[x];
945
946 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
947 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
948 continue;
949
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400950 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 spin_lock(&sdp->sd_quota_spin);
952 value += qd->qd_change;
953 spin_unlock(&sdp->sd_quota_spin);
954
Steven Whitehousecd915492006-09-04 12:49:07 -0400955 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 +0000956 print_message(qd, "exceeded");
957 error = -EDQUOT;
958 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400959 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -0400960 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500962 gfs2_tune_get(sdp,
963 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964 error = print_message(qd, "warning");
965 qd->qd_last_warn = jiffies;
966 }
967 }
968
969 return error;
970}
971
Steven Whitehousecd915492006-09-04 12:49:07 -0400972void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
973 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974{
975 struct gfs2_alloc *al = &ip->i_alloc;
976 struct gfs2_quota_data *qd;
977 unsigned int x;
978 unsigned int found = 0;
979
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400980 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 return;
982 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
983 return;
984
985 for (x = 0; x < al->al_qd_num; x++) {
986 qd = al->al_qd[x];
987
988 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
989 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
990 do_qc(qd, change);
991 found++;
992 }
993 }
994}
995
996int gfs2_quota_sync(struct gfs2_sbd *sdp)
997{
998 struct gfs2_quota_data **qda;
999 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1000 unsigned int num_qd;
1001 unsigned int x;
1002 int error = 0;
1003
1004 sdp->sd_quota_sync_gen++;
1005
1006 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1007 if (!qda)
1008 return -ENOMEM;
1009
1010 do {
1011 num_qd = 0;
1012
1013 for (;;) {
1014 error = qd_fish(sdp, qda + num_qd);
1015 if (error || !qda[num_qd])
1016 break;
1017 if (++num_qd == max_qd)
1018 break;
1019 }
1020
1021 if (num_qd) {
1022 if (!error)
1023 error = do_sync(num_qd, qda);
1024 if (!error)
1025 for (x = 0; x < num_qd; x++)
1026 qda[x]->qd_sync_gen =
1027 sdp->sd_quota_sync_gen;
1028
1029 for (x = 0; x < num_qd; x++)
1030 qd_unlock(qda[x]);
1031 }
1032 } while (!error && num_qd == max_qd);
1033
1034 kfree(qda);
1035
1036 return error;
1037}
1038
Steven Whitehousecd915492006-09-04 12:49:07 -04001039int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040{
1041 struct gfs2_quota_data *qd;
1042 struct gfs2_holder q_gh;
1043 int error;
1044
1045 error = qd_get(sdp, user, id, CREATE, &qd);
1046 if (error)
1047 return error;
1048
1049 error = do_glock(qd, FORCE, &q_gh);
1050 if (!error)
1051 gfs2_glock_dq_uninit(&q_gh);
1052
1053 qd_put(qd);
1054
1055 return error;
1056}
1057
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058int gfs2_quota_init(struct gfs2_sbd *sdp)
1059{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001060 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1062 unsigned int x, slot = 0;
1063 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001064 u64 dblock;
1065 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 int error;
1067
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001068 if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1070 gfs2_consist_inode(ip);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001071 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 }
1073 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001074 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075
1076 error = -ENOMEM;
1077
1078 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1079 sizeof(unsigned char *), GFP_KERNEL);
1080 if (!sdp->sd_quota_bitmap)
1081 return error;
1082
1083 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1084 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1085 if (!sdp->sd_quota_bitmap[x])
1086 goto fail;
1087 }
1088
1089 for (x = 0; x < blocks; x++) {
1090 struct buffer_head *bh;
1091 unsigned int y;
1092
1093 if (!extlen) {
1094 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001095 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 if (error)
1097 goto fail;
1098 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001100 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1101 if (!bh)
1102 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1104 brelse(bh);
1105 goto fail;
1106 }
1107
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001108 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109 y++, slot++) {
Al Virob62f9632006-10-13 23:46:46 -04001110 struct gfs2_quota_change_host qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 struct gfs2_quota_data *qd;
1112
1113 gfs2_quota_change_in(&qc, bh->b_data +
1114 sizeof(struct gfs2_meta_header) +
1115 y * sizeof(struct gfs2_quota_change));
1116 if (!qc.qc_change)
1117 continue;
1118
1119 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1120 qc.qc_id, &qd);
1121 if (error) {
1122 brelse(bh);
1123 goto fail;
1124 }
1125
1126 set_bit(QDF_CHANGE, &qd->qd_flags);
1127 qd->qd_change = qc.qc_change;
1128 qd->qd_slot = slot;
1129 qd->qd_slot_count = 1;
1130 qd->qd_last_touched = jiffies;
1131
1132 spin_lock(&sdp->sd_quota_spin);
1133 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1134 list_add(&qd->qd_list, &sdp->sd_quota_list);
1135 atomic_inc(&sdp->sd_quota_count);
1136 spin_unlock(&sdp->sd_quota_spin);
1137
1138 found++;
1139 }
1140
1141 brelse(bh);
1142 dblock++;
1143 extlen--;
1144 }
1145
1146 if (found)
1147 fs_info(sdp, "found %u quota changes\n", found);
1148
1149 return 0;
1150
Steven Whitehousea91ea692006-09-04 12:04:26 -04001151fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 gfs2_quota_cleanup(sdp);
1153 return error;
1154}
1155
1156void gfs2_quota_scan(struct gfs2_sbd *sdp)
1157{
1158 struct gfs2_quota_data *qd, *safe;
1159 LIST_HEAD(dead);
1160
1161 spin_lock(&sdp->sd_quota_spin);
1162 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1163 if (!qd->qd_count &&
1164 time_after_eq(jiffies, qd->qd_last_touched +
1165 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1166 list_move(&qd->qd_list, &dead);
1167 gfs2_assert_warn(sdp,
1168 atomic_read(&sdp->sd_quota_count) > 0);
1169 atomic_dec(&sdp->sd_quota_count);
1170 }
1171 }
1172 spin_unlock(&sdp->sd_quota_spin);
1173
1174 while (!list_empty(&dead)) {
1175 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1176 list_del(&qd->qd_list);
1177
1178 gfs2_assert_warn(sdp, !qd->qd_change);
1179 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1180 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1181
1182 gfs2_lvb_unhold(qd->qd_gl);
1183 kfree(qd);
1184 }
1185}
1186
1187void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1188{
1189 struct list_head *head = &sdp->sd_quota_list;
1190 struct gfs2_quota_data *qd;
1191 unsigned int x;
1192
1193 spin_lock(&sdp->sd_quota_spin);
1194 while (!list_empty(head)) {
1195 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1196
1197 if (qd->qd_count > 1 ||
1198 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1199 list_move(&qd->qd_list, head);
1200 spin_unlock(&sdp->sd_quota_spin);
1201 schedule();
1202 spin_lock(&sdp->sd_quota_spin);
1203 continue;
1204 }
1205
1206 list_del(&qd->qd_list);
1207 atomic_dec(&sdp->sd_quota_count);
1208 spin_unlock(&sdp->sd_quota_spin);
1209
1210 if (!qd->qd_count) {
1211 gfs2_assert_warn(sdp, !qd->qd_change);
1212 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1213 } else
1214 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1215 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1216
1217 gfs2_lvb_unhold(qd->qd_gl);
1218 kfree(qd);
1219
1220 spin_lock(&sdp->sd_quota_spin);
1221 }
1222 spin_unlock(&sdp->sd_quota_spin);
1223
1224 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1225
1226 if (sdp->sd_quota_bitmap) {
1227 for (x = 0; x < sdp->sd_quota_chunks; x++)
1228 kfree(sdp->sd_quota_bitmap[x]);
1229 kfree(sdp->sd_quota_bitmap);
1230 }
1231}
1232