blob: e8ef0f80fb1185bd4d576d81d33079cf4d05d09d [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Peterson0d0868b2007-12-11 18:51:25 -06003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
10/*
11 * Quota change tags are associated with each transaction that allocates or
12 * deallocates space. Those changes are accumulated locally to each node (in a
13 * per-node file) and then are periodically synced to the quota file. This
14 * avoids the bottleneck of constantly touching the quota file, but introduces
15 * fuzziness in the current usage value of IDs that are being used on different
16 * nodes in the cluster simultaneously. So, it is possible for a user on
17 * multiple nodes to overrun their quota, but that overrun is controlable.
18 * Since quota tags are part of transactions, there is no need to a quota check
19 * program to be run on node crashes or anything like that.
20 *
21 * There are couple of knobs that let the administrator manage the quota
22 * fuzziness. "quota_quantum" sets the maximum time a quota change can be
23 * sitting on one node before being synced to the quota file. (The default is
24 * 60 seconds.) Another knob, "quota_scale" controls how quickly the frequency
25 * of quota file syncs increases as the user moves closer to their limit. The
26 * more frequent the syncs, the more accurate the quota enforcement, but that
27 * means that there is more contention between the nodes for the quota file.
28 * The default value is one. This sets the maximum theoretical quota overrun
29 * (with infinite node with infinite bandwidth) to twice the user's limit. (In
30 * practice, the maximum overrun you see should be much less.) A "quota_scale"
31 * number greater than one makes quota syncs more frequent and reduces the
32 * maximum overrun. Numbers less than one (but greater than zero) make quota
33 * syncs less frequent.
34 *
35 * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of
36 * the quota file, so it is not being constantly read.
37 */
38
39#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/completion.h>
43#include <linux/buffer_head.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000044#include <linux/sort.h>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000045#include <linux/fs.h>
Steven Whitehouse2e565bb2006-10-02 11:38:25 -040046#include <linux/bio.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050047#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020048#include <linux/lm_interface.h>
Steven Whitehouse37b2c832008-11-17 14:25:37 +000049#include <linux/kthread.h>
50#include <linux/freezer.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000051
52#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050053#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000054#include "bmap.h"
55#include "glock.h"
56#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000057#include "log.h"
58#include "meta_io.h"
59#include "quota.h"
60#include "rgrp.h"
61#include "super.h"
62#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000063#include "inode.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000064#include "ops_address.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050065#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000066
67#define QUOTA_USER 1
68#define QUOTA_GROUP 0
69
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010070struct gfs2_quota_host {
71 u64 qu_limit;
72 u64 qu_warn;
73 s64 qu_value;
Abhijith Das2d9a4bb2007-08-15 11:25:05 -050074 u32 qu_ll_next;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010075};
76
77struct gfs2_quota_change_host {
78 u64 qc_change;
79 u32 qc_flags; /* GFS2_QCF_... */
80 u32 qc_id;
81};
82
Abhijith Das0a7ab792009-01-07 16:03:37 -060083static LIST_HEAD(qd_lru_list);
84static atomic_t qd_lru_count = ATOMIC_INIT(0);
85static spinlock_t qd_lru_lock = SPIN_LOCK_UNLOCKED;
86
87int gfs2_shrink_qd_memory(int nr, gfp_t gfp_mask)
88{
89 struct gfs2_quota_data *qd;
90 struct gfs2_sbd *sdp;
91
92 if (nr == 0)
93 goto out;
94
95 if (!(gfp_mask & __GFP_FS))
96 return -1;
97
98 spin_lock(&qd_lru_lock);
99 while (nr && !list_empty(&qd_lru_list)) {
100 qd = list_entry(qd_lru_list.next,
101 struct gfs2_quota_data, qd_reclaim);
102 sdp = qd->qd_gl->gl_sbd;
103
104 /* Free from the filesystem-specific list */
105 list_del(&qd->qd_list);
106
Abhijith Das0a7ab792009-01-07 16:03:37 -0600107 gfs2_assert_warn(sdp, !qd->qd_change);
108 gfs2_assert_warn(sdp, !qd->qd_slot_count);
109 gfs2_assert_warn(sdp, !qd->qd_bh_count);
110
111 gfs2_lvb_unhold(qd->qd_gl);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600112 atomic_dec(&sdp->sd_quota_count);
113
114 /* Delete it from the common reclaim list */
115 list_del_init(&qd->qd_reclaim);
116 atomic_dec(&qd_lru_count);
117 spin_unlock(&qd_lru_lock);
118 kmem_cache_free(gfs2_quotad_cachep, qd);
119 spin_lock(&qd_lru_lock);
120 nr--;
121 }
122 spin_unlock(&qd_lru_lock);
123
124out:
125 return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
126}
127
Steven Whitehousecd915492006-09-04 12:49:07 -0400128static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129{
Steven Whitehousecd915492006-09-04 12:49:07 -0400130 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131
Steven Whitehousecd915492006-09-04 12:49:07 -0400132 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 offset *= sizeof(struct gfs2_quota);
134
135 return offset;
136}
137
Steven Whitehousecd915492006-09-04 12:49:07 -0400138static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139 struct gfs2_quota_data **qdp)
140{
141 struct gfs2_quota_data *qd;
142 int error;
143
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000144 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 if (!qd)
146 return -ENOMEM;
147
Abhijith Das0a7ab792009-01-07 16:03:37 -0600148 atomic_set(&qd->qd_count, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 qd->qd_id = id;
150 if (user)
151 set_bit(QDF_USER, &qd->qd_flags);
152 qd->qd_slot = -1;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600153 INIT_LIST_HEAD(&qd->qd_reclaim);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154
Steven Whitehousecd915492006-09-04 12:49:07 -0400155 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 &gfs2_quota_glops, CREATE, &qd->qd_gl);
157 if (error)
158 goto fail;
159
160 error = gfs2_lvb_hold(qd->qd_gl);
161 gfs2_glock_put(qd->qd_gl);
162 if (error)
163 goto fail;
164
165 *qdp = qd;
166
167 return 0;
168
Steven Whitehousea91ea692006-09-04 12:04:26 -0400169fail:
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000170 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 return error;
172}
173
Steven Whitehousecd915492006-09-04 12:49:07 -0400174static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 struct gfs2_quota_data **qdp)
176{
177 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
178 int error, found;
179
180 *qdp = NULL;
181
182 for (;;) {
183 found = 0;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600184 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
186 if (qd->qd_id == id &&
187 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
Abhijith Das0a7ab792009-01-07 16:03:37 -0600188 if (!atomic_read(&qd->qd_count) &&
189 !list_empty(&qd->qd_reclaim)) {
190 /* Remove it from reclaim list */
191 list_del_init(&qd->qd_reclaim);
192 atomic_dec(&qd_lru_count);
193 }
194 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 found = 1;
196 break;
197 }
198 }
199
200 if (!found)
201 qd = NULL;
202
203 if (!qd && new_qd) {
204 qd = new_qd;
205 list_add(&qd->qd_list, &sdp->sd_quota_list);
206 atomic_inc(&sdp->sd_quota_count);
207 new_qd = NULL;
208 }
209
Abhijith Das0a7ab792009-01-07 16:03:37 -0600210 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211
212 if (qd || !create) {
213 if (new_qd) {
214 gfs2_lvb_unhold(new_qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000215 kmem_cache_free(gfs2_quotad_cachep, new_qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 }
217 *qdp = qd;
218 return 0;
219 }
220
221 error = qd_alloc(sdp, user, id, &new_qd);
222 if (error)
223 return error;
224 }
225}
226
227static void qd_hold(struct gfs2_quota_data *qd)
228{
229 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600230 gfs2_assert(sdp, atomic_read(&qd->qd_count));
231 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232}
233
234static void qd_put(struct gfs2_quota_data *qd)
235{
Abhijith Das0a7ab792009-01-07 16:03:37 -0600236 if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
237 /* Add to the reclaim list */
238 list_add_tail(&qd->qd_reclaim, &qd_lru_list);
239 atomic_inc(&qd_lru_count);
240 spin_unlock(&qd_lru_lock);
241 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242}
243
244static int slot_get(struct gfs2_quota_data *qd)
245{
246 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
247 unsigned int c, o = 0, b;
248 unsigned char byte = 0;
249
Steven Whitehouse22077f52009-01-08 14:28:42 +0000250 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251
252 if (qd->qd_slot_count++) {
Steven Whitehouse22077f52009-01-08 14:28:42 +0000253 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 return 0;
255 }
256
257 for (c = 0; c < sdp->sd_quota_chunks; c++)
258 for (o = 0; o < PAGE_SIZE; o++) {
259 byte = sdp->sd_quota_bitmap[c][o];
260 if (byte != 0xFF)
261 goto found;
262 }
263
264 goto fail;
265
Steven Whitehousea91ea692006-09-04 12:04:26 -0400266found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267 for (b = 0; b < 8; b++)
268 if (!(byte & (1 << b)))
269 break;
270 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
271
272 if (qd->qd_slot >= sdp->sd_quota_slots)
273 goto fail;
274
275 sdp->sd_quota_bitmap[c][o] |= 1 << b;
276
Steven Whitehouse22077f52009-01-08 14:28:42 +0000277 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278
279 return 0;
280
Steven Whitehousea91ea692006-09-04 12:04:26 -0400281fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 qd->qd_slot_count--;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000283 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 return -ENOSPC;
285}
286
287static void slot_hold(struct gfs2_quota_data *qd)
288{
289 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
290
Steven Whitehouse22077f52009-01-08 14:28:42 +0000291 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 gfs2_assert(sdp, qd->qd_slot_count);
293 qd->qd_slot_count++;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000294 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295}
296
297static void slot_put(struct gfs2_quota_data *qd)
298{
299 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
300
Steven Whitehouse22077f52009-01-08 14:28:42 +0000301 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 gfs2_assert(sdp, qd->qd_slot_count);
303 if (!--qd->qd_slot_count) {
304 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
305 qd->qd_slot = -1;
306 }
Steven Whitehouse22077f52009-01-08 14:28:42 +0000307 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308}
309
310static int bh_get(struct gfs2_quota_data *qd)
311{
312 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400313 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 struct buffer_head *bh;
316 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400317 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318
Steven Whitehousef55ab262006-02-21 12:51:39 +0000319 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320
321 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000322 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 return 0;
324 }
325
326 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600327 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328
Steven Whitehouse23591252006-10-13 17:25:45 -0400329 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600330 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331 if (error)
332 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400333 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334 if (error)
335 goto fail;
336 error = -EIO;
337 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
338 goto fail_brelse;
339
340 qd->qd_bh = bh;
341 qd->qd_bh_qc = (struct gfs2_quota_change *)
342 (bh->b_data + sizeof(struct gfs2_meta_header) +
343 offset * sizeof(struct gfs2_quota_change));
344
Josef Whiter2e95b662007-02-20 00:03:29 -0500345 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346
347 return 0;
348
Steven Whitehousea91ea692006-09-04 12:04:26 -0400349fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400351fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000353 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 return error;
355}
356
357static void bh_put(struct gfs2_quota_data *qd)
358{
359 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
360
Steven Whitehousef55ab262006-02-21 12:51:39 +0000361 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 gfs2_assert(sdp, qd->qd_bh_count);
363 if (!--qd->qd_bh_count) {
364 brelse(qd->qd_bh);
365 qd->qd_bh = NULL;
366 qd->qd_bh_qc = NULL;
367 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000368 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369}
370
371static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
372{
373 struct gfs2_quota_data *qd = NULL;
374 int error;
375 int found = 0;
376
377 *qdp = NULL;
378
379 if (sdp->sd_vfs->s_flags & MS_RDONLY)
380 return 0;
381
Abhijith Das0a7ab792009-01-07 16:03:37 -0600382 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383
384 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
385 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
386 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
387 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
388 continue;
389
390 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
391
392 set_bit(QDF_LOCKED, &qd->qd_flags);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600393 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
394 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395 qd->qd_change_sync = qd->qd_change;
396 gfs2_assert_warn(sdp, qd->qd_slot_count);
397 qd->qd_slot_count++;
398 found = 1;
399
400 break;
401 }
402
403 if (!found)
404 qd = NULL;
405
Abhijith Das0a7ab792009-01-07 16:03:37 -0600406 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407
408 if (qd) {
409 gfs2_assert_warn(sdp, qd->qd_change_sync);
410 error = bh_get(qd);
411 if (error) {
412 clear_bit(QDF_LOCKED, &qd->qd_flags);
413 slot_put(qd);
414 qd_put(qd);
415 return error;
416 }
417 }
418
419 *qdp = qd;
420
421 return 0;
422}
423
424static int qd_trylock(struct gfs2_quota_data *qd)
425{
426 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
427
428 if (sdp->sd_vfs->s_flags & MS_RDONLY)
429 return 0;
430
Abhijith Das0a7ab792009-01-07 16:03:37 -0600431 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432
433 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
434 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
Abhijith Das0a7ab792009-01-07 16:03:37 -0600435 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 return 0;
437 }
438
439 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
440
441 set_bit(QDF_LOCKED, &qd->qd_flags);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600442 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
443 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 qd->qd_change_sync = qd->qd_change;
445 gfs2_assert_warn(sdp, qd->qd_slot_count);
446 qd->qd_slot_count++;
447
Abhijith Das0a7ab792009-01-07 16:03:37 -0600448 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
450 gfs2_assert_warn(sdp, qd->qd_change_sync);
451 if (bh_get(qd)) {
452 clear_bit(QDF_LOCKED, &qd->qd_flags);
453 slot_put(qd);
454 qd_put(qd);
455 return 0;
456 }
457
458 return 1;
459}
460
461static void qd_unlock(struct gfs2_quota_data *qd)
462{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500463 gfs2_assert_warn(qd->qd_gl->gl_sbd,
464 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465 clear_bit(QDF_LOCKED, &qd->qd_flags);
466 bh_put(qd);
467 slot_put(qd);
468 qd_put(qd);
469}
470
Steven Whitehousecd915492006-09-04 12:49:07 -0400471static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 struct gfs2_quota_data **qdp)
473{
474 int error;
475
476 error = qd_get(sdp, user, id, create, qdp);
477 if (error)
478 return error;
479
480 error = slot_get(*qdp);
481 if (error)
482 goto fail;
483
484 error = bh_get(*qdp);
485 if (error)
486 goto fail_slot;
487
488 return 0;
489
Steven Whitehousea91ea692006-09-04 12:04:26 -0400490fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400492fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 qd_put(*qdp);
494 return error;
495}
496
497static void qdsb_put(struct gfs2_quota_data *qd)
498{
499 bh_put(qd);
500 slot_put(qd);
501 qd_put(qd);
502}
503
Steven Whitehousecd915492006-09-04 12:49:07 -0400504int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400506 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000507 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 struct gfs2_quota_data **qd = al->al_qd;
509 int error;
510
511 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
512 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
513 return -EIO;
514
515 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
516 return 0;
517
Steven Whitehouse2933f922006-11-01 13:23:29 -0500518 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 if (error)
520 goto out;
521 al->al_qd_num++;
522 qd++;
523
Steven Whitehouse2933f922006-11-01 13:23:29 -0500524 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 if (error)
526 goto out;
527 al->al_qd_num++;
528 qd++;
529
Steven Whitehouse2933f922006-11-01 13:23:29 -0500530 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
532 if (error)
533 goto out;
534 al->al_qd_num++;
535 qd++;
536 }
537
Steven Whitehouse2933f922006-11-01 13:23:29 -0500538 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
540 if (error)
541 goto out;
542 al->al_qd_num++;
543 qd++;
544 }
545
Steven Whitehousea91ea692006-09-04 12:04:26 -0400546out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 if (error)
548 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 return error;
550}
551
552void gfs2_quota_unhold(struct gfs2_inode *ip)
553{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400554 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000555 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 unsigned int x;
557
558 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
559
560 for (x = 0; x < al->al_qd_num; x++) {
561 qdsb_put(al->al_qd[x]);
562 al->al_qd[x] = NULL;
563 }
564 al->al_qd_num = 0;
565}
566
567static int sort_qd(const void *a, const void *b)
568{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400569 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
570 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571
572 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
573 !test_bit(QDF_USER, &qd_b->qd_flags)) {
574 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400575 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400577 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400579 if (qd_a->qd_id < qd_b->qd_id)
580 return -1;
581 if (qd_a->qd_id > qd_b->qd_id)
582 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583
Steven Whitehouse48fac172006-09-05 15:17:12 -0400584 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585}
586
Steven Whitehousecd915492006-09-04 12:49:07 -0400587static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588{
589 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400590 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400592 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593
Steven Whitehousef55ab262006-02-21 12:51:39 +0000594 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000595 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596
597 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
598 qc->qc_change = 0;
599 qc->qc_flags = 0;
600 if (test_bit(QDF_USER, &qd->qd_flags))
601 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
602 qc->qc_id = cpu_to_be32(qd->qd_id);
603 }
604
Al Virob44b84d2006-10-14 10:46:30 -0400605 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 qc->qc_change = cpu_to_be64(x);
607
Steven Whitehouse22077f52009-01-08 14:28:42 +0000608 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 qd->qd_change = x;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000610 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611
612 if (!x) {
613 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
614 clear_bit(QDF_CHANGE, &qd->qd_flags);
615 qc->qc_flags = 0;
616 qc->qc_id = 0;
617 slot_put(qd);
618 qd_put(qd);
619 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
620 qd_hold(qd);
621 slot_hold(qd);
622 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400623
Steven Whitehousef55ab262006-02-21 12:51:39 +0000624 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625}
626
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100627static void gfs2_quota_in(struct gfs2_quota_host *qu, const void *buf)
628{
629 const struct gfs2_quota *str = buf;
630
631 qu->qu_limit = be64_to_cpu(str->qu_limit);
632 qu->qu_warn = be64_to_cpu(str->qu_warn);
633 qu->qu_value = be64_to_cpu(str->qu_value);
Abhijith Das2d9a4bb2007-08-15 11:25:05 -0500634 qu->qu_ll_next = be32_to_cpu(str->qu_ll_next);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100635}
636
637static void gfs2_quota_out(const struct gfs2_quota_host *qu, void *buf)
638{
639 struct gfs2_quota *str = buf;
640
641 str->qu_limit = cpu_to_be64(qu->qu_limit);
642 str->qu_warn = cpu_to_be64(qu->qu_warn);
643 str->qu_value = cpu_to_be64(qu->qu_value);
Abhijith Das2d9a4bb2007-08-15 11:25:05 -0500644 str->qu_ll_next = cpu_to_be32(qu->qu_ll_next);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100645 memset(&str->qu_reserved, 0, sizeof(str->qu_reserved));
646}
647
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000648/**
649 * gfs2_adjust_quota
650 *
651 * This function was mostly borrowed from gfs2_block_truncate_page which was
652 * in turn mostly borrowed from ext3
653 */
654static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousecd915492006-09-04 12:49:07 -0400655 s64 change, struct gfs2_quota_data *qd)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000656{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400657 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000658 struct address_space *mapping = inode->i_mapping;
659 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500660 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000661 unsigned blocksize, iblock, pos;
662 struct buffer_head *bh;
663 struct page *page;
664 void *kaddr;
Abhijith Das1990e912007-05-31 17:52:02 -0500665 char *ptr;
666 struct gfs2_quota_host qp;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400667 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000668 int err = -EIO;
669
Abhijith Das20b95bf2008-03-06 17:43:52 -0600670 if (gfs2_is_stuffed(ip))
Abhijith Das0fd53552007-08-14 15:34:58 -0500671 gfs2_unstuff_dinode(ip, NULL);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600672
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000673 page = grab_cache_page(mapping, index);
674 if (!page)
675 return -ENOMEM;
676
677 blocksize = inode->i_sb->s_blocksize;
678 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
679
680 if (!page_has_buffers(page))
681 create_empty_buffers(page, blocksize, 0);
682
683 bh = page_buffers(page);
684 pos = blocksize;
685 while (offset >= pos) {
686 bh = bh->b_this_page;
687 iblock++;
688 pos += blocksize;
689 }
690
691 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600692 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000693 if (!buffer_mapped(bh))
694 goto unlock;
695 }
696
697 if (PageUptodate(page))
698 set_buffer_uptodate(bh);
699
700 if (!buffer_uptodate(bh)) {
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400701 ll_rw_block(READ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000702 wait_on_buffer(bh);
703 if (!buffer_uptodate(bh))
704 goto unlock;
705 }
706
707 gfs2_trans_add_bh(ip->i_gl, bh, 0);
708
709 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse48fac172006-09-05 15:17:12 -0400710 ptr = kaddr + offset;
Abhijith Das1990e912007-05-31 17:52:02 -0500711 gfs2_quota_in(&qp, ptr);
712 qp.qu_value += change;
713 value = qp.qu_value;
714 gfs2_quota_out(&qp, ptr);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000715 flush_dcache_page(page);
716 kunmap_atomic(kaddr, KM_USER0);
717 err = 0;
718 qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000719 qd->qd_qb.qb_value = cpu_to_be64(value);
Abhijith Das2a87ab02007-05-16 17:02:19 -0500720 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC);
721 ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000722unlock:
723 unlock_page(page);
724 page_cache_release(page);
725 return err;
726}
727
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
729{
730 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400731 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 unsigned int data_blocks, ind_blocks;
733 struct gfs2_holder *ghs, i_gh;
734 unsigned int qx, x;
735 struct gfs2_quota_data *qd;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000736 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600737 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 struct gfs2_alloc *al = NULL;
739 int error;
740
741 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
742 &data_blocks, &ind_blocks);
743
Josef Bacik16c5f062008-04-09 09:33:41 -0400744 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 if (!ghs)
746 return -ENOMEM;
747
748 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
749 for (qx = 0; qx < num_qd; qx++) {
750 error = gfs2_glock_nq_init(qda[qx]->qd_gl,
751 LM_ST_EXCLUSIVE,
752 GL_NOCACHE, &ghs[qx]);
753 if (error)
754 goto out;
755 }
756
757 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
758 if (error)
759 goto out;
760
761 for (x = 0; x < num_qd; x++) {
762 int alloc_required;
763
764 offset = qd2offset(qda[x]);
765 error = gfs2_write_alloc_required(ip, offset,
766 sizeof(struct gfs2_quota),
767 &alloc_required);
768 if (error)
769 goto out_gunlock;
770 if (alloc_required)
771 nalloc++;
772 }
773
Abhijith Das20b95bf2008-03-06 17:43:52 -0600774 al = gfs2_alloc_get(ip);
775 if (!al) {
776 error = -ENOMEM;
777 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 }
Abhijith Das20b95bf2008-03-06 17:43:52 -0600779 /*
780 * 1 blk for unstuffing inode if stuffed. We add this extra
781 * block to the reservation unconditionally. If the inode
782 * doesn't need unstuffing, the block will be released to the
783 * rgrp since it won't be allocated during the transaction
784 */
785 al->al_requested = 1;
786 /* +1 in the end for block requested above for unstuffing */
787 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 1;
788
789 if (nalloc)
790 al->al_requested += nalloc * (data_blocks + ind_blocks);
791 error = gfs2_inplace_reserve(ip);
792 if (error)
793 goto out_alloc;
794
795 if (nalloc)
796 blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS;
797
798 error = gfs2_trans_begin(sdp, blocks, 0);
799 if (error)
800 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801
802 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803 qd = qda[x];
804 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000805 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500806 (struct gfs2_quota_data *)
Abhijith Das2a87ab02007-05-16 17:02:19 -0500807 qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000808 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 goto out_end_trans;
810
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 }
813
814 error = 0;
815
Steven Whitehousea91ea692006-09-04 12:04:26 -0400816out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400818out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600819 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400820out_alloc:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600821 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400822out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400824out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 while (qx--)
826 gfs2_glock_dq_uninit(&ghs[qx]);
827 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400828 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 return error;
830}
831
832static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
833 struct gfs2_holder *q_gh)
834{
835 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400836 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837 struct gfs2_holder i_gh;
Al Virob5bc9e82006-10-13 23:31:55 -0400838 struct gfs2_quota_host q;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 char buf[sizeof(struct gfs2_quota)];
840 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400841 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842
Steven Whitehousea91ea692006-09-04 12:04:26 -0400843restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
845 if (error)
846 return error;
847
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400848 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400850 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000851 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 gfs2_glock_dq_uninit(q_gh);
853 error = gfs2_glock_nq_init(qd->qd_gl,
Abhijith Das0a7ab792009-01-07 16:03:37 -0600854 LM_ST_EXCLUSIVE, GL_NOCACHE,
855 q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 if (error)
857 return error;
858
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400859 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860 if (error)
861 goto fail;
862
863 memset(buf, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000864 pos = qd2offset(qd);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100865 error = gfs2_internal_read(ip, NULL, buf, &pos,
866 sizeof(struct gfs2_quota));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 if (error < 0)
868 goto fail_gunlock;
869
870 gfs2_glock_dq_uninit(&i_gh);
871
872 gfs2_quota_in(&q, buf);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400873 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
874 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
875 qlvb->__pad = 0;
876 qlvb->qb_limit = cpu_to_be64(q.qu_limit);
877 qlvb->qb_warn = cpu_to_be64(q.qu_warn);
878 qlvb->qb_value = cpu_to_be64(q.qu_value);
879 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880
881 if (gfs2_glock_is_blocking(qd->qd_gl)) {
882 gfs2_glock_dq_uninit(q_gh);
883 force_refresh = 0;
884 goto restart;
885 }
886 }
887
888 return 0;
889
Steven Whitehousea91ea692006-09-04 12:04:26 -0400890fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400892fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 return error;
895}
896
Steven Whitehousecd915492006-09-04 12:49:07 -0400897int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400899 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000900 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 unsigned int x;
902 int error = 0;
903
904 gfs2_quota_hold(ip, uid, gid);
905
906 if (capable(CAP_SYS_RESOURCE) ||
907 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
908 return 0;
909
910 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
911 sort_qd, NULL);
912
913 for (x = 0; x < al->al_qd_num; x++) {
914 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
915 if (error)
916 break;
917 }
918
919 if (!error)
920 set_bit(GIF_QD_LOCKED, &ip->i_flags);
921 else {
922 while (x--)
923 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
924 gfs2_quota_unhold(ip);
925 }
926
927 return error;
928}
929
930static int need_sync(struct gfs2_quota_data *qd)
931{
932 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
933 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400934 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 unsigned int num, den;
936 int do_sync = 1;
937
938 if (!qd->qd_qb.qb_limit)
939 return 0;
940
Steven Whitehouse22077f52009-01-08 14:28:42 +0000941 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 value = qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000943 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944
945 spin_lock(&gt->gt_spin);
946 num = gt->gt_quota_scale_num;
947 den = gt->gt_quota_scale_den;
948 spin_unlock(&gt->gt_spin);
949
950 if (value < 0)
951 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400952 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
953 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 do_sync = 0;
955 else {
956 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +0100957 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400958 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -0400959 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 do_sync = 0;
961 }
962
963 return do_sync;
964}
965
966void gfs2_quota_unlock(struct gfs2_inode *ip)
967{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000968 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969 struct gfs2_quota_data *qda[4];
970 unsigned int count = 0;
971 unsigned int x;
972
973 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
974 goto out;
975
976 for (x = 0; x < al->al_qd_num; x++) {
977 struct gfs2_quota_data *qd;
978 int sync;
979
980 qd = al->al_qd[x];
981 sync = need_sync(qd);
982
983 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
984
985 if (sync && qd_trylock(qd))
986 qda[count++] = qd;
987 }
988
989 if (count) {
990 do_sync(count, qda);
991 for (x = 0; x < count; x++)
992 qd_unlock(qda[x]);
993 }
994
Steven Whitehousea91ea692006-09-04 12:04:26 -0400995out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 gfs2_quota_unhold(ip);
997}
998
999#define MAX_LINE 256
1000
1001static int print_message(struct gfs2_quota_data *qd, char *type)
1002{
1003 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004
Steven Whitehouse02630a12006-07-03 11:20:06 -04001005 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
1006 sdp->sd_fsname, type,
1007 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
1008 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009
1010 return 0;
1011}
1012
Steven Whitehousecd915492006-09-04 12:49:07 -04001013int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001015 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001016 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001018 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 unsigned int x;
1020 int error = 0;
1021
1022 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
1023 return 0;
1024
1025 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1026 return 0;
1027
1028 for (x = 0; x < al->al_qd_num; x++) {
1029 qd = al->al_qd[x];
1030
1031 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1032 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
1033 continue;
1034
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001035 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse22077f52009-01-08 14:28:42 +00001036 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 value += qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +00001038 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039
Steven Whitehousecd915492006-09-04 12:49:07 -04001040 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 +00001041 print_message(qd, "exceeded");
1042 error = -EDQUOT;
1043 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001044 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001045 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001047 gfs2_tune_get(sdp,
1048 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 error = print_message(qd, "warning");
1050 qd->qd_last_warn = jiffies;
1051 }
1052 }
1053
1054 return error;
1055}
1056
Steven Whitehousecd915492006-09-04 12:49:07 -04001057void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1058 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001060 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 struct gfs2_quota_data *qd;
1062 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001064 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001066 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067 return;
1068
1069 for (x = 0; x < al->al_qd_num; x++) {
1070 qd = al->al_qd[x];
1071
1072 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1073 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1074 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 }
1076 }
1077}
1078
1079int gfs2_quota_sync(struct gfs2_sbd *sdp)
1080{
1081 struct gfs2_quota_data **qda;
1082 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1083 unsigned int num_qd;
1084 unsigned int x;
1085 int error = 0;
1086
1087 sdp->sd_quota_sync_gen++;
1088
1089 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1090 if (!qda)
1091 return -ENOMEM;
1092
1093 do {
1094 num_qd = 0;
1095
1096 for (;;) {
1097 error = qd_fish(sdp, qda + num_qd);
1098 if (error || !qda[num_qd])
1099 break;
1100 if (++num_qd == max_qd)
1101 break;
1102 }
1103
1104 if (num_qd) {
1105 if (!error)
1106 error = do_sync(num_qd, qda);
1107 if (!error)
1108 for (x = 0; x < num_qd; x++)
1109 qda[x]->qd_sync_gen =
1110 sdp->sd_quota_sync_gen;
1111
1112 for (x = 0; x < num_qd; x++)
1113 qd_unlock(qda[x]);
1114 }
1115 } while (!error && num_qd == max_qd);
1116
1117 kfree(qda);
1118
1119 return error;
1120}
1121
Steven Whitehousecd915492006-09-04 12:49:07 -04001122int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123{
1124 struct gfs2_quota_data *qd;
1125 struct gfs2_holder q_gh;
1126 int error;
1127
1128 error = qd_get(sdp, user, id, CREATE, &qd);
1129 if (error)
1130 return error;
1131
1132 error = do_glock(qd, FORCE, &q_gh);
1133 if (!error)
1134 gfs2_glock_dq_uninit(&q_gh);
1135
1136 qd_put(qd);
1137
1138 return error;
1139}
1140
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001141static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1142{
1143 const struct gfs2_quota_change *str = buf;
1144
1145 qc->qc_change = be64_to_cpu(str->qc_change);
1146 qc->qc_flags = be32_to_cpu(str->qc_flags);
1147 qc->qc_id = be32_to_cpu(str->qc_id);
1148}
1149
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150int gfs2_quota_init(struct gfs2_sbd *sdp)
1151{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001152 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001153 unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 unsigned int x, slot = 0;
1155 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001156 u64 dblock;
1157 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 int error;
1159
Steven Whitehousec9e98882008-11-04 09:47:33 +00001160 if (!ip->i_disksize || ip->i_disksize > (64 << 20) ||
1161 ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 gfs2_consist_inode(ip);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001163 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 }
1165 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001166 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001167
1168 error = -ENOMEM;
1169
1170 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
Josef Bacik16c5f062008-04-09 09:33:41 -04001171 sizeof(unsigned char *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172 if (!sdp->sd_quota_bitmap)
1173 return error;
1174
1175 for (x = 0; x < sdp->sd_quota_chunks; x++) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001176 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 if (!sdp->sd_quota_bitmap[x])
1178 goto fail;
1179 }
1180
1181 for (x = 0; x < blocks; x++) {
1182 struct buffer_head *bh;
1183 unsigned int y;
1184
1185 if (!extlen) {
1186 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001187 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188 if (error)
1189 goto fail;
1190 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001191 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001192 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1193 if (!bh)
1194 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1196 brelse(bh);
1197 goto fail;
1198 }
1199
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001200 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 y++, slot++) {
Al Virob62f9632006-10-13 23:46:46 -04001202 struct gfs2_quota_change_host qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 struct gfs2_quota_data *qd;
1204
1205 gfs2_quota_change_in(&qc, bh->b_data +
1206 sizeof(struct gfs2_meta_header) +
1207 y * sizeof(struct gfs2_quota_change));
1208 if (!qc.qc_change)
1209 continue;
1210
1211 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1212 qc.qc_id, &qd);
1213 if (error) {
1214 brelse(bh);
1215 goto fail;
1216 }
1217
1218 set_bit(QDF_CHANGE, &qd->qd_flags);
1219 qd->qd_change = qc.qc_change;
1220 qd->qd_slot = slot;
1221 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222
Abhijith Das0a7ab792009-01-07 16:03:37 -06001223 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1225 list_add(&qd->qd_list, &sdp->sd_quota_list);
1226 atomic_inc(&sdp->sd_quota_count);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001227 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228
1229 found++;
1230 }
1231
1232 brelse(bh);
1233 dblock++;
1234 extlen--;
1235 }
1236
1237 if (found)
1238 fs_info(sdp, "found %u quota changes\n", found);
1239
1240 return 0;
1241
Steven Whitehousea91ea692006-09-04 12:04:26 -04001242fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243 gfs2_quota_cleanup(sdp);
1244 return error;
1245}
1246
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1248{
1249 struct list_head *head = &sdp->sd_quota_list;
1250 struct gfs2_quota_data *qd;
1251 unsigned int x;
1252
Abhijith Das0a7ab792009-01-07 16:03:37 -06001253 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 while (!list_empty(head)) {
1255 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1256
Abhijith Das0a7ab792009-01-07 16:03:37 -06001257 if (atomic_read(&qd->qd_count) > 1 ||
1258 (atomic_read(&qd->qd_count) &&
1259 !test_bit(QDF_CHANGE, &qd->qd_flags))) {
Abhijith Das0a7ab792009-01-07 16:03:37 -06001260 list_move(&qd->qd_list, head);
1261 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001262 schedule();
Abhijith Das0a7ab792009-01-07 16:03:37 -06001263 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 continue;
1265 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001266
Abhijith Das0a7ab792009-01-07 16:03:37 -06001267 list_del(&qd->qd_list);
1268 /* Also remove if this qd exists in the reclaim list */
1269 if (!list_empty(&qd->qd_reclaim)) {
1270 list_del_init(&qd->qd_reclaim);
1271 atomic_dec(&qd_lru_count);
1272 }
1273 atomic_dec(&sdp->sd_quota_count);
1274 spin_unlock(&qd_lru_lock);
1275
1276 if (!atomic_read(&qd->qd_count)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277 gfs2_assert_warn(sdp, !qd->qd_change);
1278 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1279 } else
1280 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1281 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1282
1283 gfs2_lvb_unhold(qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001284 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285
Abhijith Das0a7ab792009-01-07 16:03:37 -06001286 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287 }
Abhijith Das0a7ab792009-01-07 16:03:37 -06001288 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289
1290 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1291
1292 if (sdp->sd_quota_bitmap) {
1293 for (x = 0; x < sdp->sd_quota_chunks; x++)
1294 kfree(sdp->sd_quota_bitmap[x]);
1295 kfree(sdp->sd_quota_bitmap);
1296 }
1297}
1298
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001299static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1300{
1301 if (error == 0 || error == -EROFS)
1302 return;
1303 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1304 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1305}
1306
1307static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
1308 int (*fxn)(struct gfs2_sbd *sdp),
1309 unsigned long t, unsigned long *timeo,
1310 unsigned int *new_timeo)
1311{
1312 if (t >= *timeo) {
1313 int error = fxn(sdp);
1314 quotad_error(sdp, msg, error);
1315 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1316 } else {
1317 *timeo -= t;
1318 }
1319}
1320
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001321static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1322{
1323 struct gfs2_inode *ip;
1324
1325 while(1) {
1326 ip = NULL;
1327 spin_lock(&sdp->sd_trunc_lock);
1328 if (!list_empty(&sdp->sd_trunc_list)) {
1329 ip = list_entry(sdp->sd_trunc_list.next,
1330 struct gfs2_inode, i_trunc_list);
1331 list_del_init(&ip->i_trunc_list);
1332 }
1333 spin_unlock(&sdp->sd_trunc_lock);
1334 if (ip == NULL)
1335 return;
1336 gfs2_glock_finish_truncate(ip);
1337 }
1338}
1339
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001340/**
1341 * gfs2_quotad - Write cached quota changes into the quota file
1342 * @sdp: Pointer to GFS2 superblock
1343 *
1344 */
1345
1346int gfs2_quotad(void *data)
1347{
1348 struct gfs2_sbd *sdp = data;
1349 struct gfs2_tune *tune = &sdp->sd_tune;
1350 unsigned long statfs_timeo = 0;
1351 unsigned long quotad_timeo = 0;
1352 unsigned long t = 0;
1353 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001354 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001355
1356 while (!kthread_should_stop()) {
1357
1358 /* Update the master statfs file */
1359 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1360 &statfs_timeo, &tune->gt_statfs_quantum);
1361
1362 /* Update quota file */
1363 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1364 &quotad_timeo, &tune->gt_quota_quantum);
1365
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001366 /* Check for & recover partially truncated inodes */
1367 quotad_check_trunc_list(sdp);
1368
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001369 if (freezing(current))
1370 refrigerator();
1371 t = min(quotad_timeo, statfs_timeo);
1372
1373 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_UNINTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001374 spin_lock(&sdp->sd_trunc_lock);
1375 empty = list_empty(&sdp->sd_trunc_list);
1376 spin_unlock(&sdp->sd_trunc_lock);
1377 if (empty)
1378 t -= schedule_timeout(t);
1379 else
1380 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001381 finish_wait(&sdp->sd_quota_wait, &wait);
1382 }
1383
1384 return 0;
1385}
1386