blob: 572b92eac3671229735a639032d185e40ed58e33 [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 Whitehouse5c676f62006-02-27 17:23:27 -050046#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000047
48#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050049#include "lm_interface.h"
50#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000051#include "bmap.h"
52#include "glock.h"
53#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000054#include "log.h"
55#include "meta_io.h"
56#include "quota.h"
57#include "rgrp.h"
58#include "super.h"
59#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000060#include "inode.h"
Steven Whitehousef42faf42006-01-30 18:34:10 +000061#include "ops_file.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
68static uint64_t qd2offset(struct gfs2_quota_data *qd)
69{
70 uint64_t offset;
71
72 offset = 2 * (uint64_t)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
73 offset *= sizeof(struct gfs2_quota);
74
75 return offset;
76}
77
78static int qd_alloc(struct gfs2_sbd *sdp, int user, uint32_t id,
79 struct gfs2_quota_data **qdp)
80{
81 struct gfs2_quota_data *qd;
82 int error;
83
84 qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_KERNEL);
85 if (!qd)
86 return -ENOMEM;
87
88 qd->qd_count = 1;
89 qd->qd_id = id;
90 if (user)
91 set_bit(QDF_USER, &qd->qd_flags);
92 qd->qd_slot = -1;
93
94 error = gfs2_glock_get(sdp, 2 * (uint64_t)id + !user,
95 &gfs2_quota_glops, CREATE, &qd->qd_gl);
96 if (error)
97 goto fail;
98
99 error = gfs2_lvb_hold(qd->qd_gl);
100 gfs2_glock_put(qd->qd_gl);
101 if (error)
102 goto fail;
103
104 *qdp = qd;
105
106 return 0;
107
Steven Whitehousea91ea692006-09-04 12:04:26 -0400108fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 kfree(qd);
110 return error;
111}
112
113static int qd_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
114 struct gfs2_quota_data **qdp)
115{
116 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
117 int error, found;
118
119 *qdp = NULL;
120
121 for (;;) {
122 found = 0;
123 spin_lock(&sdp->sd_quota_spin);
124 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
125 if (qd->qd_id == id &&
126 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
127 qd->qd_count++;
128 found = 1;
129 break;
130 }
131 }
132
133 if (!found)
134 qd = NULL;
135
136 if (!qd && new_qd) {
137 qd = new_qd;
138 list_add(&qd->qd_list, &sdp->sd_quota_list);
139 atomic_inc(&sdp->sd_quota_count);
140 new_qd = NULL;
141 }
142
143 spin_unlock(&sdp->sd_quota_spin);
144
145 if (qd || !create) {
146 if (new_qd) {
147 gfs2_lvb_unhold(new_qd->qd_gl);
148 kfree(new_qd);
149 }
150 *qdp = qd;
151 return 0;
152 }
153
154 error = qd_alloc(sdp, user, id, &new_qd);
155 if (error)
156 return error;
157 }
158}
159
160static void qd_hold(struct gfs2_quota_data *qd)
161{
162 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
163
164 spin_lock(&sdp->sd_quota_spin);
165 gfs2_assert(sdp, qd->qd_count);
166 qd->qd_count++;
167 spin_unlock(&sdp->sd_quota_spin);
168}
169
170static void qd_put(struct gfs2_quota_data *qd)
171{
172 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
173 spin_lock(&sdp->sd_quota_spin);
174 gfs2_assert(sdp, qd->qd_count);
175 if (!--qd->qd_count)
176 qd->qd_last_touched = jiffies;
177 spin_unlock(&sdp->sd_quota_spin);
178}
179
180static int slot_get(struct gfs2_quota_data *qd)
181{
182 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
183 unsigned int c, o = 0, b;
184 unsigned char byte = 0;
185
186 spin_lock(&sdp->sd_quota_spin);
187
188 if (qd->qd_slot_count++) {
189 spin_unlock(&sdp->sd_quota_spin);
190 return 0;
191 }
192
193 for (c = 0; c < sdp->sd_quota_chunks; c++)
194 for (o = 0; o < PAGE_SIZE; o++) {
195 byte = sdp->sd_quota_bitmap[c][o];
196 if (byte != 0xFF)
197 goto found;
198 }
199
200 goto fail;
201
Steven Whitehousea91ea692006-09-04 12:04:26 -0400202found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 for (b = 0; b < 8; b++)
204 if (!(byte & (1 << b)))
205 break;
206 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
207
208 if (qd->qd_slot >= sdp->sd_quota_slots)
209 goto fail;
210
211 sdp->sd_quota_bitmap[c][o] |= 1 << b;
212
213 spin_unlock(&sdp->sd_quota_spin);
214
215 return 0;
216
Steven Whitehousea91ea692006-09-04 12:04:26 -0400217fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218 qd->qd_slot_count--;
219 spin_unlock(&sdp->sd_quota_spin);
220 return -ENOSPC;
221}
222
223static void slot_hold(struct gfs2_quota_data *qd)
224{
225 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
226
227 spin_lock(&sdp->sd_quota_spin);
228 gfs2_assert(sdp, qd->qd_slot_count);
229 qd->qd_slot_count++;
230 spin_unlock(&sdp->sd_quota_spin);
231}
232
233static void slot_put(struct gfs2_quota_data *qd)
234{
235 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
236
237 spin_lock(&sdp->sd_quota_spin);
238 gfs2_assert(sdp, qd->qd_slot_count);
239 if (!--qd->qd_slot_count) {
240 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
241 qd->qd_slot = -1;
242 }
243 spin_unlock(&sdp->sd_quota_spin);
244}
245
246static int bh_get(struct gfs2_quota_data *qd)
247{
248 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400249 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 unsigned int block, offset;
251 uint64_t dblock;
252 int new = 0;
253 struct buffer_head *bh;
254 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400255 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256
Steven Whitehousef55ab262006-02-21 12:51:39 +0000257 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258
259 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000260 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 return 0;
262 }
263
264 block = qd->qd_slot / sdp->sd_qc_per_block;
265 offset = qd->qd_slot % sdp->sd_qc_per_block;;
266
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400267 error = gfs2_block_map(&ip->i_inode, block, &new, &dblock, &boundary);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 if (error)
269 goto fail;
270 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT, &bh);
271 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
Steven Whitehousef55ab262006-02-21 12:51:39 +0000282 mutex_lock(&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
408static int qdsb_get(struct gfs2_sbd *sdp, int user, uint32_t id, int create,
409 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
441int gfs2_quota_hold(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
442{
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
455 error = qdsb_get(sdp, QUOTA_USER, ip->i_di.di_uid, CREATE, qd);
456 if (error)
457 goto out;
458 al->al_qd_num++;
459 qd++;
460
461 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_di.di_gid, CREATE, qd);
462 if (error)
463 goto out;
464 al->al_qd_num++;
465 qd++;
466
467 if (uid != NO_QUOTA_CHANGE && uid != ip->i_di.di_uid) {
468 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
469 if (error)
470 goto out;
471 al->al_qd_num++;
472 qd++;
473 }
474
475 if (gid != NO_QUOTA_CHANGE && gid != ip->i_di.di_gid) {
476 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{
506 struct gfs2_quota_data *qd_a = *(struct gfs2_quota_data **)a;
507 struct gfs2_quota_data *qd_b = *(struct gfs2_quota_data **)b;
508 int ret = 0;
509
510 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
511 !test_bit(QDF_USER, &qd_b->qd_flags)) {
512 if (test_bit(QDF_USER, &qd_a->qd_flags))
513 ret = -1;
514 else
515 ret = 1;
516 } else {
517 if (qd_a->qd_id < qd_b->qd_id)
518 ret = -1;
519 else if (qd_a->qd_id > qd_b->qd_id)
520 ret = 1;
521 }
522
523 return ret;
524}
525
526static void do_qc(struct gfs2_quota_data *qd, int64_t change)
527{
528 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400529 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 struct gfs2_quota_change *qc = qd->qd_bh_qc;
531 int64_t x;
532
Steven Whitehousef55ab262006-02-21 12:51:39 +0000533 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000534 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535
536 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
537 qc->qc_change = 0;
538 qc->qc_flags = 0;
539 if (test_bit(QDF_USER, &qd->qd_flags))
540 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
541 qc->qc_id = cpu_to_be32(qd->qd_id);
542 }
543
544 x = qc->qc_change;
545 x = be64_to_cpu(x) + change;
546 qc->qc_change = cpu_to_be64(x);
547
548 spin_lock(&sdp->sd_quota_spin);
549 qd->qd_change = x;
550 spin_unlock(&sdp->sd_quota_spin);
551
552 if (!x) {
553 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
554 clear_bit(QDF_CHANGE, &qd->qd_flags);
555 qc->qc_flags = 0;
556 qc->qc_id = 0;
557 slot_put(qd);
558 qd_put(qd);
559 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
560 qd_hold(qd);
561 slot_hold(qd);
562 }
563
Steven Whitehousef55ab262006-02-21 12:51:39 +0000564 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565}
566
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000567/**
568 * gfs2_adjust_quota
569 *
570 * This function was mostly borrowed from gfs2_block_truncate_page which was
571 * in turn mostly borrowed from ext3
572 */
573static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
574 int64_t change, struct gfs2_quota_data *qd)
575{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400576 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000577 struct address_space *mapping = inode->i_mapping;
578 unsigned long index = loc >> PAGE_CACHE_SHIFT;
579 unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
580 unsigned blocksize, iblock, pos;
581 struct buffer_head *bh;
582 struct page *page;
583 void *kaddr;
584 __be64 *ptr;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400585 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000586 int err = -EIO;
587
588 page = grab_cache_page(mapping, index);
589 if (!page)
590 return -ENOMEM;
591
592 blocksize = inode->i_sb->s_blocksize;
593 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
594
595 if (!page_has_buffers(page))
596 create_empty_buffers(page, blocksize, 0);
597
598 bh = page_buffers(page);
599 pos = blocksize;
600 while (offset >= pos) {
601 bh = bh->b_this_page;
602 iblock++;
603 pos += blocksize;
604 }
605
606 if (!buffer_mapped(bh)) {
607 gfs2_get_block(inode, iblock, bh, 1);
608 if (!buffer_mapped(bh))
609 goto unlock;
610 }
611
612 if (PageUptodate(page))
613 set_buffer_uptodate(bh);
614
615 if (!buffer_uptodate(bh)) {
616 ll_rw_block(READ, 1, &bh);
617 wait_on_buffer(bh);
618 if (!buffer_uptodate(bh))
619 goto unlock;
620 }
621
622 gfs2_trans_add_bh(ip->i_gl, bh, 0);
623
624 kaddr = kmap_atomic(page, KM_USER0);
625 ptr = (__be64 *)(kaddr + offset);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400626 value = (s64)be64_to_cpu(*ptr) + change;
627 *ptr = cpu_to_be64(value);
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);
632#if 0
633 qd->qd_qb.qb_limit = cpu_to_be64(q.qu_limit);
634 qd->qd_qb.qb_warn = cpu_to_be64(q.qu_warn);
635#endif
636 qd->qd_qb.qb_value = cpu_to_be64(value);
637unlock:
638 unlock_page(page);
639 page_cache_release(page);
640 return err;
641}
642
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
644{
645 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400646 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647 unsigned int data_blocks, ind_blocks;
648 struct gfs2_holder *ghs, i_gh;
649 unsigned int qx, x;
650 struct gfs2_quota_data *qd;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000651 loff_t offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652 unsigned int nalloc = 0;
653 struct gfs2_alloc *al = NULL;
654 int error;
655
656 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
657 &data_blocks, &ind_blocks);
658
659 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
660 if (!ghs)
661 return -ENOMEM;
662
663 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
664 for (qx = 0; qx < num_qd; qx++) {
665 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
666 LM_ST_EXCLUSIVE,
667 GL_NOCACHE, &ghs[qx]);
668 if (error)
669 goto out;
670 }
671
672 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
673 if (error)
674 goto out;
675
676 for (x = 0; x < num_qd; x++) {
677 int alloc_required;
678
679 offset = qd2offset(qda[x]);
680 error = gfs2_write_alloc_required(ip, offset,
681 sizeof(struct gfs2_quota),
682 &alloc_required);
683 if (error)
684 goto out_gunlock;
685 if (alloc_required)
686 nalloc++;
687 }
688
689 if (nalloc) {
690 al = gfs2_alloc_get(ip);
691
692 al->al_requested = nalloc * (data_blocks + ind_blocks);
693
694 error = gfs2_inplace_reserve(ip);
695 if (error)
696 goto out_alloc;
697
698 error = gfs2_trans_begin(sdp,
699 al->al_rgd->rd_ri.ri_length +
700 num_qd * data_blocks +
701 nalloc * ind_blocks +
702 RES_DINODE + num_qd +
703 RES_STATFS, 0);
704 if (error)
705 goto out_ipres;
706 } else {
707 error = gfs2_trans_begin(sdp,
708 num_qd * data_blocks +
709 RES_DINODE + num_qd, 0);
710 if (error)
711 goto out_gunlock;
712 }
713
714 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 qd = qda[x];
716 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000717 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500718 (struct gfs2_quota_data *)
719 qd->qd_gl->gl_lvb);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000720 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 goto out_end_trans;
722
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 }
725
726 error = 0;
727
Steven Whitehousea91ea692006-09-04 12:04:26 -0400728out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400730out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 if (nalloc)
732 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400733out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 if (nalloc)
735 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400736out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400738out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 while (qx--)
740 gfs2_glock_dq_uninit(&ghs[qx]);
741 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400742 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 return error;
744}
745
746static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
747 struct gfs2_holder *q_gh)
748{
749 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400750 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 struct gfs2_holder i_gh;
752 struct gfs2_quota q;
753 char buf[sizeof(struct gfs2_quota)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000754 struct file_ra_state ra_state;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400756 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757
Steven Whitehousef42faf42006-01-30 18:34:10 +0000758 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400759restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
761 if (error)
762 return error;
763
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400764 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400766 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000767 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 gfs2_glock_dq_uninit(q_gh);
769 error = gfs2_glock_nq_init(qd->qd_gl,
770 LM_ST_EXCLUSIVE, GL_NOCACHE,
771 q_gh);
772 if (error)
773 return error;
774
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400775 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 if (error)
777 goto fail;
778
779 memset(buf, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000780 pos = qd2offset(qd);
Steven Whitehouse0d42e542006-06-20 16:13:49 -0400781 error = gfs2_internal_read(ip, &ra_state, buf,
782 &pos, sizeof(struct gfs2_quota));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 if (error < 0)
784 goto fail_gunlock;
785
786 gfs2_glock_dq_uninit(&i_gh);
787
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400788
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 gfs2_quota_in(&q, buf);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400790 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
791 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
792 qlvb->__pad = 0;
793 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
794 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
795 qlvb->qb_value = cpu_to_be64(q.qu_value);
796 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797
798 if (gfs2_glock_is_blocking(qd->qd_gl)) {
799 gfs2_glock_dq_uninit(q_gh);
800 force_refresh = 0;
801 goto restart;
802 }
803 }
804
805 return 0;
806
Steven Whitehousea91ea692006-09-04 12:04:26 -0400807fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400809fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 return error;
812}
813
814int gfs2_quota_lock(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
815{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400816 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 struct gfs2_alloc *al = &ip->i_alloc;
818 unsigned int x;
819 int error = 0;
820
821 gfs2_quota_hold(ip, uid, gid);
822
823 if (capable(CAP_SYS_RESOURCE) ||
824 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
825 return 0;
826
827 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
828 sort_qd, NULL);
829
830 for (x = 0; x < al->al_qd_num; x++) {
831 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
832 if (error)
833 break;
834 }
835
836 if (!error)
837 set_bit(GIF_QD_LOCKED, &ip->i_flags);
838 else {
839 while (x--)
840 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
841 gfs2_quota_unhold(ip);
842 }
843
844 return error;
845}
846
847static int need_sync(struct gfs2_quota_data *qd)
848{
849 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
850 struct gfs2_tune *gt = &sdp->sd_tune;
851 int64_t value;
852 unsigned int num, den;
853 int do_sync = 1;
854
855 if (!qd->qd_qb.qb_limit)
856 return 0;
857
858 spin_lock(&sdp->sd_quota_spin);
859 value = qd->qd_change;
860 spin_unlock(&sdp->sd_quota_spin);
861
862 spin_lock(&gt->gt_spin);
863 num = gt->gt_quota_scale_num;
864 den = gt->gt_quota_scale_den;
865 spin_unlock(&gt->gt_spin);
866
867 if (value < 0)
868 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400869 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
870 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 do_sync = 0;
872 else {
873 value *= gfs2_jindex_size(sdp) * num;
874 do_div(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400875 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
876 if (value < (int64_t)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 do_sync = 0;
878 }
879
880 return do_sync;
881}
882
883void gfs2_quota_unlock(struct gfs2_inode *ip)
884{
885 struct gfs2_alloc *al = &ip->i_alloc;
886 struct gfs2_quota_data *qda[4];
887 unsigned int count = 0;
888 unsigned int x;
889
890 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
891 goto out;
892
893 for (x = 0; x < al->al_qd_num; x++) {
894 struct gfs2_quota_data *qd;
895 int sync;
896
897 qd = al->al_qd[x];
898 sync = need_sync(qd);
899
900 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
901
902 if (sync && qd_trylock(qd))
903 qda[count++] = qd;
904 }
905
906 if (count) {
907 do_sync(count, qda);
908 for (x = 0; x < count; x++)
909 qd_unlock(qda[x]);
910 }
911
Steven Whitehousea91ea692006-09-04 12:04:26 -0400912out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 gfs2_quota_unhold(ip);
914}
915
916#define MAX_LINE 256
917
918static int print_message(struct gfs2_quota_data *qd, char *type)
919{
920 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921
Steven Whitehouse02630a12006-07-03 11:20:06 -0400922 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
923 sdp->sd_fsname, type,
924 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
925 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926
927 return 0;
928}
929
930int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
931{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400932 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933 struct gfs2_alloc *al = &ip->i_alloc;
934 struct gfs2_quota_data *qd;
935 int64_t value;
936 unsigned int x;
937 int error = 0;
938
939 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
940 return 0;
941
942 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
943 return 0;
944
945 for (x = 0; x < al->al_qd_num; x++) {
946 qd = al->al_qd[x];
947
948 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
949 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
950 continue;
951
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400952 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 spin_lock(&sdp->sd_quota_spin);
954 value += qd->qd_change;
955 spin_unlock(&sdp->sd_quota_spin);
956
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400957 if (be64_to_cpu(qd->qd_qb.qb_limit) && (int64_t)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 print_message(qd, "exceeded");
959 error = -EDQUOT;
960 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400961 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
962 (int64_t)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500964 gfs2_tune_get(sdp,
965 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 error = print_message(qd, "warning");
967 qd->qd_last_warn = jiffies;
968 }
969 }
970
971 return error;
972}
973
974void gfs2_quota_change(struct gfs2_inode *ip, int64_t change,
975 uint32_t uid, uint32_t gid)
976{
977 struct gfs2_alloc *al = &ip->i_alloc;
978 struct gfs2_quota_data *qd;
979 unsigned int x;
980 unsigned int found = 0;
981
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400982 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 return;
984 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
985 return;
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 do_qc(qd, change);
993 found++;
994 }
995 }
996}
997
998int gfs2_quota_sync(struct gfs2_sbd *sdp)
999{
1000 struct gfs2_quota_data **qda;
1001 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1002 unsigned int num_qd;
1003 unsigned int x;
1004 int error = 0;
1005
1006 sdp->sd_quota_sync_gen++;
1007
1008 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1009 if (!qda)
1010 return -ENOMEM;
1011
1012 do {
1013 num_qd = 0;
1014
1015 for (;;) {
1016 error = qd_fish(sdp, qda + num_qd);
1017 if (error || !qda[num_qd])
1018 break;
1019 if (++num_qd == max_qd)
1020 break;
1021 }
1022
1023 if (num_qd) {
1024 if (!error)
1025 error = do_sync(num_qd, qda);
1026 if (!error)
1027 for (x = 0; x < num_qd; x++)
1028 qda[x]->qd_sync_gen =
1029 sdp->sd_quota_sync_gen;
1030
1031 for (x = 0; x < num_qd; x++)
1032 qd_unlock(qda[x]);
1033 }
1034 } while (!error && num_qd == max_qd);
1035
1036 kfree(qda);
1037
1038 return error;
1039}
1040
1041int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
1042{
1043 struct gfs2_quota_data *qd;
1044 struct gfs2_holder q_gh;
1045 int error;
1046
1047 error = qd_get(sdp, user, id, CREATE, &qd);
1048 if (error)
1049 return error;
1050
1051 error = do_glock(qd, FORCE, &q_gh);
1052 if (!error)
1053 gfs2_glock_dq_uninit(&q_gh);
1054
1055 qd_put(qd);
1056
1057 return error;
1058}
1059
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001060#if 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
1062 struct gfs2_quota *q)
1063{
1064 struct gfs2_quota_data *qd;
1065 struct gfs2_holder q_gh;
1066 int error;
1067
1068 if (((user) ? (id != current->fsuid) : (!in_group_p(id))) &&
1069 !capable(CAP_SYS_ADMIN))
1070 return -EACCES;
1071
1072 error = qd_get(sdp, user, id, CREATE, &qd);
1073 if (error)
1074 return error;
1075
1076 error = do_glock(qd, NO_FORCE, &q_gh);
1077 if (error)
1078 goto out;
1079
1080 memset(q, 0, sizeof(struct gfs2_quota));
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001081 q->qu_limit = be64_to_cpu(qd->qd_qb.qb_limit);
1082 q->qu_warn = be64_to_cpu(qd->qd_qb.qb_warn);
1083 q->qu_value = be64_to_cpu(qd->qd_qb.qb_value);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084
1085 spin_lock(&sdp->sd_quota_spin);
1086 q->qu_value += qd->qd_change;
1087 spin_unlock(&sdp->sd_quota_spin);
1088
1089 gfs2_glock_dq_uninit(&q_gh);
1090
Steven Whitehousea91ea692006-09-04 12:04:26 -04001091out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 return error;
1094}
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001095#endif /* 0 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096
1097int gfs2_quota_init(struct gfs2_sbd *sdp)
1098{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001099 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1101 unsigned int x, slot = 0;
1102 unsigned int found = 0;
1103 uint64_t dblock;
1104 uint32_t extlen = 0;
1105 int error;
1106
1107 if (!ip->i_di.di_size ||
1108 ip->i_di.di_size > (64 << 20) ||
1109 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1110 gfs2_consist_inode(ip);
1111 return -EIO;
1112 }
1113 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001114 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115
1116 error = -ENOMEM;
1117
1118 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1119 sizeof(unsigned char *), GFP_KERNEL);
1120 if (!sdp->sd_quota_bitmap)
1121 return error;
1122
1123 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1124 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1125 if (!sdp->sd_quota_bitmap[x])
1126 goto fail;
1127 }
1128
1129 for (x = 0; x < blocks; x++) {
1130 struct buffer_head *bh;
1131 unsigned int y;
1132
1133 if (!extlen) {
1134 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001135 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 if (error)
1137 goto fail;
1138 }
1139 gfs2_meta_ra(ip->i_gl, dblock, extlen);
1140 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
1141 &bh);
1142 if (error)
1143 goto fail;
1144 error = -EIO;
1145 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1146 brelse(bh);
1147 goto fail;
1148 }
1149
1150 for (y = 0;
1151 y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
1152 y++, slot++) {
1153 struct gfs2_quota_change qc;
1154 struct gfs2_quota_data *qd;
1155
1156 gfs2_quota_change_in(&qc, bh->b_data +
1157 sizeof(struct gfs2_meta_header) +
1158 y * sizeof(struct gfs2_quota_change));
1159 if (!qc.qc_change)
1160 continue;
1161
1162 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1163 qc.qc_id, &qd);
1164 if (error) {
1165 brelse(bh);
1166 goto fail;
1167 }
1168
1169 set_bit(QDF_CHANGE, &qd->qd_flags);
1170 qd->qd_change = qc.qc_change;
1171 qd->qd_slot = slot;
1172 qd->qd_slot_count = 1;
1173 qd->qd_last_touched = jiffies;
1174
1175 spin_lock(&sdp->sd_quota_spin);
1176 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1177 list_add(&qd->qd_list, &sdp->sd_quota_list);
1178 atomic_inc(&sdp->sd_quota_count);
1179 spin_unlock(&sdp->sd_quota_spin);
1180
1181 found++;
1182 }
1183
1184 brelse(bh);
1185 dblock++;
1186 extlen--;
1187 }
1188
1189 if (found)
1190 fs_info(sdp, "found %u quota changes\n", found);
1191
1192 return 0;
1193
Steven Whitehousea91ea692006-09-04 12:04:26 -04001194fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 gfs2_quota_cleanup(sdp);
1196 return error;
1197}
1198
1199void gfs2_quota_scan(struct gfs2_sbd *sdp)
1200{
1201 struct gfs2_quota_data *qd, *safe;
1202 LIST_HEAD(dead);
1203
1204 spin_lock(&sdp->sd_quota_spin);
1205 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1206 if (!qd->qd_count &&
1207 time_after_eq(jiffies, qd->qd_last_touched +
1208 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1209 list_move(&qd->qd_list, &dead);
1210 gfs2_assert_warn(sdp,
1211 atomic_read(&sdp->sd_quota_count) > 0);
1212 atomic_dec(&sdp->sd_quota_count);
1213 }
1214 }
1215 spin_unlock(&sdp->sd_quota_spin);
1216
1217 while (!list_empty(&dead)) {
1218 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1219 list_del(&qd->qd_list);
1220
1221 gfs2_assert_warn(sdp, !qd->qd_change);
1222 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1223 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1224
1225 gfs2_lvb_unhold(qd->qd_gl);
1226 kfree(qd);
1227 }
1228}
1229
1230void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1231{
1232 struct list_head *head = &sdp->sd_quota_list;
1233 struct gfs2_quota_data *qd;
1234 unsigned int x;
1235
1236 spin_lock(&sdp->sd_quota_spin);
1237 while (!list_empty(head)) {
1238 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1239
1240 if (qd->qd_count > 1 ||
1241 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1242 list_move(&qd->qd_list, head);
1243 spin_unlock(&sdp->sd_quota_spin);
1244 schedule();
1245 spin_lock(&sdp->sd_quota_spin);
1246 continue;
1247 }
1248
1249 list_del(&qd->qd_list);
1250 atomic_dec(&sdp->sd_quota_count);
1251 spin_unlock(&sdp->sd_quota_spin);
1252
1253 if (!qd->qd_count) {
1254 gfs2_assert_warn(sdp, !qd->qd_change);
1255 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1256 } else
1257 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1258 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1259
1260 gfs2_lvb_unhold(qd->qd_gl);
1261 kfree(qd);
1262
1263 spin_lock(&sdp->sd_quota_spin);
1264 }
1265 spin_unlock(&sdp->sd_quota_spin);
1266
1267 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1268
1269 if (sdp->sd_quota_bitmap) {
1270 for (x = 0; x < sdp->sd_quota_chunks; x++)
1271 kfree(sdp->sd_quota_bitmap[x]);
1272 kfree(sdp->sd_quota_bitmap);
1273 }
1274}
1275