blob: da46e14388f4bd7b1eda99457738592fa511b939 [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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020047#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000048
49#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050050#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
Steven Whitehousecd915492006-09-04 12:49:07 -040068static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000069{
Steven Whitehousecd915492006-09-04 12:49:07 -040070 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +000071
Steven Whitehousecd915492006-09-04 12:49:07 -040072 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000073 offset *= sizeof(struct gfs2_quota);
74
75 return offset;
76}
77
Steven Whitehousecd915492006-09-04 12:49:07 -040078static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 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
Steven Whitehousecd915492006-09-04 12:49:07 -040094 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
David Teiglandb3b94fa2006-01-16 16:50:04 +000095 &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
Steven Whitehousecd915492006-09-04 12:49:07 -0400113static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251 struct buffer_head *bh;
252 int error;
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400253 struct buffer_head bh_map;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254
Steven Whitehousef55ab262006-02-21 12:51:39 +0000255 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256
257 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000258 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 return 0;
260 }
261
262 block = qd->qd_slot / sdp->sd_qc_per_block;
263 offset = qd->qd_slot % sdp->sd_qc_per_block;;
264
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400265 error = gfs2_block_map(&ip->i_inode, block, 0, &bh_map, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 if (error)
267 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400268 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 if (error)
270 goto fail;
271 error = -EIO;
272 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
273 goto fail_brelse;
274
275 qd->qd_bh = bh;
276 qd->qd_bh_qc = (struct gfs2_quota_change *)
277 (bh->b_data + sizeof(struct gfs2_meta_header) +
278 offset * sizeof(struct gfs2_quota_change));
279
Steven Whitehousef55ab262006-02-21 12:51:39 +0000280 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281
282 return 0;
283
Steven Whitehousea91ea692006-09-04 12:04:26 -0400284fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400286fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000288 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 return error;
290}
291
292static void bh_put(struct gfs2_quota_data *qd)
293{
294 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
295
Steven Whitehousef55ab262006-02-21 12:51:39 +0000296 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 gfs2_assert(sdp, qd->qd_bh_count);
298 if (!--qd->qd_bh_count) {
299 brelse(qd->qd_bh);
300 qd->qd_bh = NULL;
301 qd->qd_bh_qc = NULL;
302 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000303 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304}
305
306static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
307{
308 struct gfs2_quota_data *qd = NULL;
309 int error;
310 int found = 0;
311
312 *qdp = NULL;
313
314 if (sdp->sd_vfs->s_flags & MS_RDONLY)
315 return 0;
316
317 spin_lock(&sdp->sd_quota_spin);
318
319 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
320 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
321 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
322 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
323 continue;
324
325 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
326
327 set_bit(QDF_LOCKED, &qd->qd_flags);
328 gfs2_assert_warn(sdp, qd->qd_count);
329 qd->qd_count++;
330 qd->qd_change_sync = qd->qd_change;
331 gfs2_assert_warn(sdp, qd->qd_slot_count);
332 qd->qd_slot_count++;
333 found = 1;
334
335 break;
336 }
337
338 if (!found)
339 qd = NULL;
340
341 spin_unlock(&sdp->sd_quota_spin);
342
343 if (qd) {
344 gfs2_assert_warn(sdp, qd->qd_change_sync);
345 error = bh_get(qd);
346 if (error) {
347 clear_bit(QDF_LOCKED, &qd->qd_flags);
348 slot_put(qd);
349 qd_put(qd);
350 return error;
351 }
352 }
353
354 *qdp = qd;
355
356 return 0;
357}
358
359static int qd_trylock(struct gfs2_quota_data *qd)
360{
361 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
362
363 if (sdp->sd_vfs->s_flags & MS_RDONLY)
364 return 0;
365
366 spin_lock(&sdp->sd_quota_spin);
367
368 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
369 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
370 spin_unlock(&sdp->sd_quota_spin);
371 return 0;
372 }
373
374 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
375
376 set_bit(QDF_LOCKED, &qd->qd_flags);
377 gfs2_assert_warn(sdp, qd->qd_count);
378 qd->qd_count++;
379 qd->qd_change_sync = qd->qd_change;
380 gfs2_assert_warn(sdp, qd->qd_slot_count);
381 qd->qd_slot_count++;
382
383 spin_unlock(&sdp->sd_quota_spin);
384
385 gfs2_assert_warn(sdp, qd->qd_change_sync);
386 if (bh_get(qd)) {
387 clear_bit(QDF_LOCKED, &qd->qd_flags);
388 slot_put(qd);
389 qd_put(qd);
390 return 0;
391 }
392
393 return 1;
394}
395
396static void qd_unlock(struct gfs2_quota_data *qd)
397{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500398 gfs2_assert_warn(qd->qd_gl->gl_sbd,
399 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 clear_bit(QDF_LOCKED, &qd->qd_flags);
401 bh_put(qd);
402 slot_put(qd);
403 qd_put(qd);
404}
405
Steven Whitehousecd915492006-09-04 12:49:07 -0400406static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 struct gfs2_quota_data **qdp)
408{
409 int error;
410
411 error = qd_get(sdp, user, id, create, qdp);
412 if (error)
413 return error;
414
415 error = slot_get(*qdp);
416 if (error)
417 goto fail;
418
419 error = bh_get(*qdp);
420 if (error)
421 goto fail_slot;
422
423 return 0;
424
Steven Whitehousea91ea692006-09-04 12:04:26 -0400425fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400427fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428 qd_put(*qdp);
429 return error;
430}
431
432static void qdsb_put(struct gfs2_quota_data *qd)
433{
434 bh_put(qd);
435 slot_put(qd);
436 qd_put(qd);
437}
438
Steven Whitehousecd915492006-09-04 12:49:07 -0400439int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 struct gfs2_alloc *al = &ip->i_alloc;
443 struct gfs2_quota_data **qd = al->al_qd;
444 int error;
445
446 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
447 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
448 return -EIO;
449
450 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
451 return 0;
452
453 error = qdsb_get(sdp, QUOTA_USER, ip->i_di.di_uid, CREATE, qd);
454 if (error)
455 goto out;
456 al->al_qd_num++;
457 qd++;
458
459 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_di.di_gid, CREATE, qd);
460 if (error)
461 goto out;
462 al->al_qd_num++;
463 qd++;
464
465 if (uid != NO_QUOTA_CHANGE && uid != ip->i_di.di_uid) {
466 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
467 if (error)
468 goto out;
469 al->al_qd_num++;
470 qd++;
471 }
472
473 if (gid != NO_QUOTA_CHANGE && gid != ip->i_di.di_gid) {
474 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
475 if (error)
476 goto out;
477 al->al_qd_num++;
478 qd++;
479 }
480
Steven Whitehousea91ea692006-09-04 12:04:26 -0400481out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 if (error)
483 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 return error;
485}
486
487void gfs2_quota_unhold(struct gfs2_inode *ip)
488{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400489 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 struct gfs2_alloc *al = &ip->i_alloc;
491 unsigned int x;
492
493 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
494
495 for (x = 0; x < al->al_qd_num; x++) {
496 qdsb_put(al->al_qd[x]);
497 al->al_qd[x] = NULL;
498 }
499 al->al_qd_num = 0;
500}
501
502static int sort_qd(const void *a, const void *b)
503{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400504 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
505 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506
507 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
508 !test_bit(QDF_USER, &qd_b->qd_flags)) {
509 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400510 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400512 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400514 if (qd_a->qd_id < qd_b->qd_id)
515 return -1;
516 if (qd_a->qd_id > qd_b->qd_id)
517 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518
Steven Whitehouse48fac172006-09-05 15:17:12 -0400519 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520}
521
Steven Whitehousecd915492006-09-04 12:49:07 -0400522static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523{
524 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400525 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400527 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528
Steven Whitehousef55ab262006-02-21 12:51:39 +0000529 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000530 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531
532 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
533 qc->qc_change = 0;
534 qc->qc_flags = 0;
535 if (test_bit(QDF_USER, &qd->qd_flags))
536 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
537 qc->qc_id = cpu_to_be32(qd->qd_id);
538 }
539
540 x = qc->qc_change;
541 x = be64_to_cpu(x) + change;
542 qc->qc_change = cpu_to_be64(x);
543
544 spin_lock(&sdp->sd_quota_spin);
545 qd->qd_change = x;
546 spin_unlock(&sdp->sd_quota_spin);
547
548 if (!x) {
549 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
550 clear_bit(QDF_CHANGE, &qd->qd_flags);
551 qc->qc_flags = 0;
552 qc->qc_id = 0;
553 slot_put(qd);
554 qd_put(qd);
555 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
556 qd_hold(qd);
557 slot_hold(qd);
558 }
559
Steven Whitehousef55ab262006-02-21 12:51:39 +0000560 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561}
562
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000563/**
564 * gfs2_adjust_quota
565 *
566 * This function was mostly borrowed from gfs2_block_truncate_page which was
567 * in turn mostly borrowed from ext3
568 */
569static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousecd915492006-09-04 12:49:07 -0400570 s64 change, struct gfs2_quota_data *qd)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000571{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400572 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000573 struct address_space *mapping = inode->i_mapping;
574 unsigned long index = loc >> PAGE_CACHE_SHIFT;
575 unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
576 unsigned blocksize, iblock, pos;
577 struct buffer_head *bh;
578 struct page *page;
579 void *kaddr;
580 __be64 *ptr;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400581 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000582 int err = -EIO;
583
584 page = grab_cache_page(mapping, index);
585 if (!page)
586 return -ENOMEM;
587
588 blocksize = inode->i_sb->s_blocksize;
589 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
590
591 if (!page_has_buffers(page))
592 create_empty_buffers(page, blocksize, 0);
593
594 bh = page_buffers(page);
595 pos = blocksize;
596 while (offset >= pos) {
597 bh = bh->b_this_page;
598 iblock++;
599 pos += blocksize;
600 }
601
602 if (!buffer_mapped(bh)) {
603 gfs2_get_block(inode, iblock, bh, 1);
604 if (!buffer_mapped(bh))
605 goto unlock;
606 }
607
608 if (PageUptodate(page))
609 set_buffer_uptodate(bh);
610
611 if (!buffer_uptodate(bh)) {
612 ll_rw_block(READ, 1, &bh);
613 wait_on_buffer(bh);
614 if (!buffer_uptodate(bh))
615 goto unlock;
616 }
617
618 gfs2_trans_add_bh(ip->i_gl, bh, 0);
619
620 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse48fac172006-09-05 15:17:12 -0400621 ptr = kaddr + offset;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400622 value = (s64)be64_to_cpu(*ptr) + change;
623 *ptr = cpu_to_be64(value);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000624 flush_dcache_page(page);
625 kunmap_atomic(kaddr, KM_USER0);
626 err = 0;
627 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000628 qd->qd_qb.qb_value = cpu_to_be64(value);
629unlock:
630 unlock_page(page);
631 page_cache_release(page);
632 return err;
633}
634
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
636{
637 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400638 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 unsigned int data_blocks, ind_blocks;
640 struct gfs2_holder *ghs, i_gh;
641 unsigned int qx, x;
642 struct gfs2_quota_data *qd;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000643 loff_t offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 unsigned int nalloc = 0;
645 struct gfs2_alloc *al = NULL;
646 int error;
647
648 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
649 &data_blocks, &ind_blocks);
650
651 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
652 if (!ghs)
653 return -ENOMEM;
654
655 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
656 for (qx = 0; qx < num_qd; qx++) {
657 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
658 LM_ST_EXCLUSIVE,
659 GL_NOCACHE, &ghs[qx]);
660 if (error)
661 goto out;
662 }
663
664 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
665 if (error)
666 goto out;
667
668 for (x = 0; x < num_qd; x++) {
669 int alloc_required;
670
671 offset = qd2offset(qda[x]);
672 error = gfs2_write_alloc_required(ip, offset,
673 sizeof(struct gfs2_quota),
674 &alloc_required);
675 if (error)
676 goto out_gunlock;
677 if (alloc_required)
678 nalloc++;
679 }
680
681 if (nalloc) {
682 al = gfs2_alloc_get(ip);
683
684 al->al_requested = nalloc * (data_blocks + ind_blocks);
685
686 error = gfs2_inplace_reserve(ip);
687 if (error)
688 goto out_alloc;
689
690 error = gfs2_trans_begin(sdp,
691 al->al_rgd->rd_ri.ri_length +
692 num_qd * data_blocks +
693 nalloc * ind_blocks +
694 RES_DINODE + num_qd +
695 RES_STATFS, 0);
696 if (error)
697 goto out_ipres;
698 } else {
699 error = gfs2_trans_begin(sdp,
700 num_qd * data_blocks +
701 RES_DINODE + num_qd, 0);
702 if (error)
703 goto out_gunlock;
704 }
705
706 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 qd = qda[x];
708 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000709 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500710 (struct gfs2_quota_data *)
711 qd->qd_gl->gl_lvb);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000712 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 goto out_end_trans;
714
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 }
717
718 error = 0;
719
Steven Whitehousea91ea692006-09-04 12:04:26 -0400720out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400722out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 if (nalloc)
724 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400725out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 if (nalloc)
727 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400728out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400730out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 while (qx--)
732 gfs2_glock_dq_uninit(&ghs[qx]);
733 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400734 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 return error;
736}
737
738static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
739 struct gfs2_holder *q_gh)
740{
741 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400742 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 struct gfs2_holder i_gh;
744 struct gfs2_quota q;
745 char buf[sizeof(struct gfs2_quota)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000746 struct file_ra_state ra_state;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400748 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749
Steven Whitehousef42faf42006-01-30 18:34:10 +0000750 file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400751restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
753 if (error)
754 return error;
755
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400756 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400758 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000759 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 gfs2_glock_dq_uninit(q_gh);
761 error = gfs2_glock_nq_init(qd->qd_gl,
762 LM_ST_EXCLUSIVE, GL_NOCACHE,
763 q_gh);
764 if (error)
765 return error;
766
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400767 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 if (error)
769 goto fail;
770
771 memset(buf, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000772 pos = qd2offset(qd);
Steven Whitehouse0d42e542006-06-20 16:13:49 -0400773 error = gfs2_internal_read(ip, &ra_state, buf,
774 &pos, sizeof(struct gfs2_quota));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775 if (error < 0)
776 goto fail_gunlock;
777
778 gfs2_glock_dq_uninit(&i_gh);
779
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400780
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 gfs2_quota_in(&q, buf);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400782 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
783 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
784 qlvb->__pad = 0;
785 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
786 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
787 qlvb->qb_value = cpu_to_be64(q.qu_value);
788 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789
790 if (gfs2_glock_is_blocking(qd->qd_gl)) {
791 gfs2_glock_dq_uninit(q_gh);
792 force_refresh = 0;
793 goto restart;
794 }
795 }
796
797 return 0;
798
Steven Whitehousea91ea692006-09-04 12:04:26 -0400799fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400801fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803 return error;
804}
805
Steven Whitehousecd915492006-09-04 12:49:07 -0400806int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400808 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 struct gfs2_alloc *al = &ip->i_alloc;
810 unsigned int x;
811 int error = 0;
812
813 gfs2_quota_hold(ip, uid, gid);
814
815 if (capable(CAP_SYS_RESOURCE) ||
816 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
817 return 0;
818
819 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
820 sort_qd, NULL);
821
822 for (x = 0; x < al->al_qd_num; x++) {
823 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
824 if (error)
825 break;
826 }
827
828 if (!error)
829 set_bit(GIF_QD_LOCKED, &ip->i_flags);
830 else {
831 while (x--)
832 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
833 gfs2_quota_unhold(ip);
834 }
835
836 return error;
837}
838
839static int need_sync(struct gfs2_quota_data *qd)
840{
841 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
842 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400843 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 unsigned int num, den;
845 int do_sync = 1;
846
847 if (!qd->qd_qb.qb_limit)
848 return 0;
849
850 spin_lock(&sdp->sd_quota_spin);
851 value = qd->qd_change;
852 spin_unlock(&sdp->sd_quota_spin);
853
854 spin_lock(&gt->gt_spin);
855 num = gt->gt_quota_scale_num;
856 den = gt->gt_quota_scale_den;
857 spin_unlock(&gt->gt_spin);
858
859 if (value < 0)
860 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400861 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
862 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000863 do_sync = 0;
864 else {
865 value *= gfs2_jindex_size(sdp) * num;
866 do_div(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400867 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -0400868 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 do_sync = 0;
870 }
871
872 return do_sync;
873}
874
875void gfs2_quota_unlock(struct gfs2_inode *ip)
876{
877 struct gfs2_alloc *al = &ip->i_alloc;
878 struct gfs2_quota_data *qda[4];
879 unsigned int count = 0;
880 unsigned int x;
881
882 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
883 goto out;
884
885 for (x = 0; x < al->al_qd_num; x++) {
886 struct gfs2_quota_data *qd;
887 int sync;
888
889 qd = al->al_qd[x];
890 sync = need_sync(qd);
891
892 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
893
894 if (sync && qd_trylock(qd))
895 qda[count++] = qd;
896 }
897
898 if (count) {
899 do_sync(count, qda);
900 for (x = 0; x < count; x++)
901 qd_unlock(qda[x]);
902 }
903
Steven Whitehousea91ea692006-09-04 12:04:26 -0400904out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 gfs2_quota_unhold(ip);
906}
907
908#define MAX_LINE 256
909
910static int print_message(struct gfs2_quota_data *qd, char *type)
911{
912 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913
Steven Whitehouse02630a12006-07-03 11:20:06 -0400914 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
915 sdp->sd_fsname, type,
916 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
917 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918
919 return 0;
920}
921
Steven Whitehousecd915492006-09-04 12:49:07 -0400922int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400924 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 struct gfs2_alloc *al = &ip->i_alloc;
926 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -0400927 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 unsigned int x;
929 int error = 0;
930
931 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
932 return 0;
933
934 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
935 return 0;
936
937 for (x = 0; x < al->al_qd_num; x++) {
938 qd = al->al_qd[x];
939
940 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
941 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
942 continue;
943
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400944 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945 spin_lock(&sdp->sd_quota_spin);
946 value += qd->qd_change;
947 spin_unlock(&sdp->sd_quota_spin);
948
Steven Whitehousecd915492006-09-04 12:49:07 -0400949 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 +0000950 print_message(qd, "exceeded");
951 error = -EDQUOT;
952 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400953 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -0400954 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500956 gfs2_tune_get(sdp,
957 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 error = print_message(qd, "warning");
959 qd->qd_last_warn = jiffies;
960 }
961 }
962
963 return error;
964}
965
Steven Whitehousecd915492006-09-04 12:49:07 -0400966void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
967 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968{
969 struct gfs2_alloc *al = &ip->i_alloc;
970 struct gfs2_quota_data *qd;
971 unsigned int x;
972 unsigned int found = 0;
973
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400974 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 return;
976 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
977 return;
978
979 for (x = 0; x < al->al_qd_num; x++) {
980 qd = al->al_qd[x];
981
982 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
983 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
984 do_qc(qd, change);
985 found++;
986 }
987 }
988}
989
990int gfs2_quota_sync(struct gfs2_sbd *sdp)
991{
992 struct gfs2_quota_data **qda;
993 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
994 unsigned int num_qd;
995 unsigned int x;
996 int error = 0;
997
998 sdp->sd_quota_sync_gen++;
999
1000 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1001 if (!qda)
1002 return -ENOMEM;
1003
1004 do {
1005 num_qd = 0;
1006
1007 for (;;) {
1008 error = qd_fish(sdp, qda + num_qd);
1009 if (error || !qda[num_qd])
1010 break;
1011 if (++num_qd == max_qd)
1012 break;
1013 }
1014
1015 if (num_qd) {
1016 if (!error)
1017 error = do_sync(num_qd, qda);
1018 if (!error)
1019 for (x = 0; x < num_qd; x++)
1020 qda[x]->qd_sync_gen =
1021 sdp->sd_quota_sync_gen;
1022
1023 for (x = 0; x < num_qd; x++)
1024 qd_unlock(qda[x]);
1025 }
1026 } while (!error && num_qd == max_qd);
1027
1028 kfree(qda);
1029
1030 return error;
1031}
1032
Steven Whitehousecd915492006-09-04 12:49:07 -04001033int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034{
1035 struct gfs2_quota_data *qd;
1036 struct gfs2_holder q_gh;
1037 int error;
1038
1039 error = qd_get(sdp, user, id, CREATE, &qd);
1040 if (error)
1041 return error;
1042
1043 error = do_glock(qd, FORCE, &q_gh);
1044 if (!error)
1045 gfs2_glock_dq_uninit(&q_gh);
1046
1047 qd_put(qd);
1048
1049 return error;
1050}
1051
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052int gfs2_quota_init(struct gfs2_sbd *sdp)
1053{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001054 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055 unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
1056 unsigned int x, slot = 0;
1057 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001058 u64 dblock;
1059 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 int error;
1061
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001062 if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
1064 gfs2_consist_inode(ip);
1065 return -EIO;
1066 }
1067 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001068 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069
1070 error = -ENOMEM;
1071
1072 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
1073 sizeof(unsigned char *), GFP_KERNEL);
1074 if (!sdp->sd_quota_bitmap)
1075 return error;
1076
1077 for (x = 0; x < sdp->sd_quota_chunks; x++) {
1078 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
1079 if (!sdp->sd_quota_bitmap[x])
1080 goto fail;
1081 }
1082
1083 for (x = 0; x < blocks; x++) {
1084 struct buffer_head *bh;
1085 unsigned int y;
1086
1087 if (!extlen) {
1088 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001089 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 if (error)
1091 goto fail;
1092 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001094 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1095 if (!bh)
1096 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1098 brelse(bh);
1099 goto fail;
1100 }
1101
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001102 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 y++, slot++) {
1104 struct gfs2_quota_change qc;
1105 struct gfs2_quota_data *qd;
1106
1107 gfs2_quota_change_in(&qc, bh->b_data +
1108 sizeof(struct gfs2_meta_header) +
1109 y * sizeof(struct gfs2_quota_change));
1110 if (!qc.qc_change)
1111 continue;
1112
1113 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1114 qc.qc_id, &qd);
1115 if (error) {
1116 brelse(bh);
1117 goto fail;
1118 }
1119
1120 set_bit(QDF_CHANGE, &qd->qd_flags);
1121 qd->qd_change = qc.qc_change;
1122 qd->qd_slot = slot;
1123 qd->qd_slot_count = 1;
1124 qd->qd_last_touched = jiffies;
1125
1126 spin_lock(&sdp->sd_quota_spin);
1127 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1128 list_add(&qd->qd_list, &sdp->sd_quota_list);
1129 atomic_inc(&sdp->sd_quota_count);
1130 spin_unlock(&sdp->sd_quota_spin);
1131
1132 found++;
1133 }
1134
1135 brelse(bh);
1136 dblock++;
1137 extlen--;
1138 }
1139
1140 if (found)
1141 fs_info(sdp, "found %u quota changes\n", found);
1142
1143 return 0;
1144
Steven Whitehousea91ea692006-09-04 12:04:26 -04001145fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146 gfs2_quota_cleanup(sdp);
1147 return error;
1148}
1149
1150void gfs2_quota_scan(struct gfs2_sbd *sdp)
1151{
1152 struct gfs2_quota_data *qd, *safe;
1153 LIST_HEAD(dead);
1154
1155 spin_lock(&sdp->sd_quota_spin);
1156 list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
1157 if (!qd->qd_count &&
1158 time_after_eq(jiffies, qd->qd_last_touched +
1159 gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
1160 list_move(&qd->qd_list, &dead);
1161 gfs2_assert_warn(sdp,
1162 atomic_read(&sdp->sd_quota_count) > 0);
1163 atomic_dec(&sdp->sd_quota_count);
1164 }
1165 }
1166 spin_unlock(&sdp->sd_quota_spin);
1167
1168 while (!list_empty(&dead)) {
1169 qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
1170 list_del(&qd->qd_list);
1171
1172 gfs2_assert_warn(sdp, !qd->qd_change);
1173 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1174 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1175
1176 gfs2_lvb_unhold(qd->qd_gl);
1177 kfree(qd);
1178 }
1179}
1180
1181void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1182{
1183 struct list_head *head = &sdp->sd_quota_list;
1184 struct gfs2_quota_data *qd;
1185 unsigned int x;
1186
1187 spin_lock(&sdp->sd_quota_spin);
1188 while (!list_empty(head)) {
1189 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1190
1191 if (qd->qd_count > 1 ||
1192 (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
1193 list_move(&qd->qd_list, head);
1194 spin_unlock(&sdp->sd_quota_spin);
1195 schedule();
1196 spin_lock(&sdp->sd_quota_spin);
1197 continue;
1198 }
1199
1200 list_del(&qd->qd_list);
1201 atomic_dec(&sdp->sd_quota_count);
1202 spin_unlock(&sdp->sd_quota_spin);
1203
1204 if (!qd->qd_count) {
1205 gfs2_assert_warn(sdp, !qd->qd_change);
1206 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1207 } else
1208 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1209 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1210
1211 gfs2_lvb_unhold(qd->qd_gl);
1212 kfree(qd);
1213
1214 spin_lock(&sdp->sd_quota_spin);
1215 }
1216 spin_unlock(&sdp->sd_quota_spin);
1217
1218 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1219
1220 if (sdp->sd_quota_bitmap) {
1221 for (x = 0; x < sdp->sd_quota_chunks; x++)
1222 kfree(sdp->sd_quota_bitmap[x]);
1223 kfree(sdp->sd_quota_bitmap);
1224 }
1225}
1226