blob: 33e369f108b38f3d6767c714b49f91d2761335df [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.
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +010018 * Since quota tags are part of transactions, there is no need for a quota check
David Teiglandb3b94fa2006-01-16 16:50:04 +000019 * 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>
Steven Whitehouse37b2c832008-11-17 14:25:37 +000048#include <linux/kthread.h>
49#include <linux/freezer.h>
Steven Whitehouse1d371b52009-09-11 15:57:27 +010050#include <linux/dqblk_xfs.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 Whitehouse5c676f62006-02-27 17:23:27 -050064#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
66#define QUOTA_USER 1
67#define QUOTA_GROUP 0
68
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010069struct gfs2_quota_change_host {
70 u64 qc_change;
71 u32 qc_flags; /* GFS2_QCF_... */
72 u32 qc_id;
73};
74
Abhijith Das0a7ab792009-01-07 16:03:37 -060075static LIST_HEAD(qd_lru_list);
76static atomic_t qd_lru_count = ATOMIC_INIT(0);
Xu Gang1328df72009-04-14 14:54:14 +080077static DEFINE_SPINLOCK(qd_lru_lock);
Abhijith Das0a7ab792009-01-07 16:03:37 -060078
79int gfs2_shrink_qd_memory(int nr, gfp_t gfp_mask)
80{
81 struct gfs2_quota_data *qd;
82 struct gfs2_sbd *sdp;
83
84 if (nr == 0)
85 goto out;
86
87 if (!(gfp_mask & __GFP_FS))
88 return -1;
89
90 spin_lock(&qd_lru_lock);
91 while (nr && !list_empty(&qd_lru_list)) {
92 qd = list_entry(qd_lru_list.next,
93 struct gfs2_quota_data, qd_reclaim);
94 sdp = qd->qd_gl->gl_sbd;
95
96 /* Free from the filesystem-specific list */
97 list_del(&qd->qd_list);
98
Abhijith Das0a7ab792009-01-07 16:03:37 -060099 gfs2_assert_warn(sdp, !qd->qd_change);
100 gfs2_assert_warn(sdp, !qd->qd_slot_count);
101 gfs2_assert_warn(sdp, !qd->qd_bh_count);
102
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000103 gfs2_glock_put(qd->qd_gl);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600104 atomic_dec(&sdp->sd_quota_count);
105
106 /* Delete it from the common reclaim list */
107 list_del_init(&qd->qd_reclaim);
108 atomic_dec(&qd_lru_count);
109 spin_unlock(&qd_lru_lock);
110 kmem_cache_free(gfs2_quotad_cachep, qd);
111 spin_lock(&qd_lru_lock);
112 nr--;
113 }
114 spin_unlock(&qd_lru_lock);
115
116out:
117 return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
118}
119
Steven Whitehousecd915492006-09-04 12:49:07 -0400120static u64 qd2offset(struct gfs2_quota_data *qd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121{
Steven Whitehousecd915492006-09-04 12:49:07 -0400122 u64 offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123
Steven Whitehousecd915492006-09-04 12:49:07 -0400124 offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 offset *= sizeof(struct gfs2_quota);
126
127 return offset;
128}
129
Steven Whitehousecd915492006-09-04 12:49:07 -0400130static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 struct gfs2_quota_data **qdp)
132{
133 struct gfs2_quota_data *qd;
134 int error;
135
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000136 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 if (!qd)
138 return -ENOMEM;
139
Abhijith Das0a7ab792009-01-07 16:03:37 -0600140 atomic_set(&qd->qd_count, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 qd->qd_id = id;
142 if (user)
143 set_bit(QDF_USER, &qd->qd_flags);
144 qd->qd_slot = -1;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600145 INIT_LIST_HEAD(&qd->qd_reclaim);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146
Steven Whitehousecd915492006-09-04 12:49:07 -0400147 error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 &gfs2_quota_glops, CREATE, &qd->qd_gl);
149 if (error)
150 goto fail;
151
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 *qdp = qd;
153
154 return 0;
155
Steven Whitehousea91ea692006-09-04 12:04:26 -0400156fail:
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000157 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 return error;
159}
160
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100161static int qd_get(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 struct gfs2_quota_data **qdp)
163{
164 struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
165 int error, found;
166
167 *qdp = NULL;
168
169 for (;;) {
170 found = 0;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600171 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
173 if (qd->qd_id == id &&
174 !test_bit(QDF_USER, &qd->qd_flags) == !user) {
Abhijith Das0a7ab792009-01-07 16:03:37 -0600175 if (!atomic_read(&qd->qd_count) &&
176 !list_empty(&qd->qd_reclaim)) {
177 /* Remove it from reclaim list */
178 list_del_init(&qd->qd_reclaim);
179 atomic_dec(&qd_lru_count);
180 }
181 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 found = 1;
183 break;
184 }
185 }
186
187 if (!found)
188 qd = NULL;
189
190 if (!qd && new_qd) {
191 qd = new_qd;
192 list_add(&qd->qd_list, &sdp->sd_quota_list);
193 atomic_inc(&sdp->sd_quota_count);
194 new_qd = NULL;
195 }
196
Abhijith Das0a7ab792009-01-07 16:03:37 -0600197 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100199 if (qd) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200 if (new_qd) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000201 gfs2_glock_put(new_qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000202 kmem_cache_free(gfs2_quotad_cachep, new_qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 }
204 *qdp = qd;
205 return 0;
206 }
207
208 error = qd_alloc(sdp, user, id, &new_qd);
209 if (error)
210 return error;
211 }
212}
213
214static void qd_hold(struct gfs2_quota_data *qd)
215{
216 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Abhijith Das0a7ab792009-01-07 16:03:37 -0600217 gfs2_assert(sdp, atomic_read(&qd->qd_count));
218 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219}
220
221static void qd_put(struct gfs2_quota_data *qd)
222{
Abhijith Das0a7ab792009-01-07 16:03:37 -0600223 if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
224 /* Add to the reclaim list */
225 list_add_tail(&qd->qd_reclaim, &qd_lru_list);
226 atomic_inc(&qd_lru_count);
227 spin_unlock(&qd_lru_lock);
228 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229}
230
231static int slot_get(struct gfs2_quota_data *qd)
232{
233 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
234 unsigned int c, o = 0, b;
235 unsigned char byte = 0;
236
Steven Whitehouse22077f52009-01-08 14:28:42 +0000237 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238
239 if (qd->qd_slot_count++) {
Steven Whitehouse22077f52009-01-08 14:28:42 +0000240 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 return 0;
242 }
243
244 for (c = 0; c < sdp->sd_quota_chunks; c++)
245 for (o = 0; o < PAGE_SIZE; o++) {
246 byte = sdp->sd_quota_bitmap[c][o];
247 if (byte != 0xFF)
248 goto found;
249 }
250
251 goto fail;
252
Steven Whitehousea91ea692006-09-04 12:04:26 -0400253found:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 for (b = 0; b < 8; b++)
255 if (!(byte & (1 << b)))
256 break;
257 qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
258
259 if (qd->qd_slot >= sdp->sd_quota_slots)
260 goto fail;
261
262 sdp->sd_quota_bitmap[c][o] |= 1 << b;
263
Steven Whitehouse22077f52009-01-08 14:28:42 +0000264 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265
266 return 0;
267
Steven Whitehousea91ea692006-09-04 12:04:26 -0400268fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 qd->qd_slot_count--;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000270 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 return -ENOSPC;
272}
273
274static void slot_hold(struct gfs2_quota_data *qd)
275{
276 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
277
Steven Whitehouse22077f52009-01-08 14:28:42 +0000278 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 gfs2_assert(sdp, qd->qd_slot_count);
280 qd->qd_slot_count++;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000281 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282}
283
284static void slot_put(struct gfs2_quota_data *qd)
285{
286 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
287
Steven Whitehouse22077f52009-01-08 14:28:42 +0000288 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 gfs2_assert(sdp, qd->qd_slot_count);
290 if (!--qd->qd_slot_count) {
291 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
292 qd->qd_slot = -1;
293 }
Steven Whitehouse22077f52009-01-08 14:28:42 +0000294 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295}
296
297static int bh_get(struct gfs2_quota_data *qd)
298{
299 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400300 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 unsigned int block, offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 struct buffer_head *bh;
303 int error;
Steven Whitehouse23591252006-10-13 17:25:45 -0400304 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305
Steven Whitehousef55ab262006-02-21 12:51:39 +0000306 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307
308 if (qd->qd_bh_count++) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000309 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 return 0;
311 }
312
313 block = qd->qd_slot / sdp->sd_qc_per_block;
Bob Peterson0d0868b2007-12-11 18:51:25 -0600314 offset = qd->qd_slot % sdp->sd_qc_per_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315
Steven Whitehouse23591252006-10-13 17:25:45 -0400316 bh_map.b_size = 1 << ip->i_inode.i_blkbits;
Bob Petersone9e1ef22007-12-10 14:13:27 -0600317 error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 if (error)
319 goto fail;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400320 error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 if (error)
322 goto fail;
323 error = -EIO;
324 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
325 goto fail_brelse;
326
327 qd->qd_bh = bh;
328 qd->qd_bh_qc = (struct gfs2_quota_change *)
329 (bh->b_data + sizeof(struct gfs2_meta_header) +
330 offset * sizeof(struct gfs2_quota_change));
331
Josef Whiter2e95b662007-02-20 00:03:29 -0500332 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333
334 return 0;
335
Steven Whitehousea91ea692006-09-04 12:04:26 -0400336fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 brelse(bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400338fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 qd->qd_bh_count--;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000340 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 return error;
342}
343
344static void bh_put(struct gfs2_quota_data *qd)
345{
346 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
347
Steven Whitehousef55ab262006-02-21 12:51:39 +0000348 mutex_lock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349 gfs2_assert(sdp, qd->qd_bh_count);
350 if (!--qd->qd_bh_count) {
351 brelse(qd->qd_bh);
352 qd->qd_bh = NULL;
353 qd->qd_bh_qc = NULL;
354 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000355 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356}
357
358static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
359{
360 struct gfs2_quota_data *qd = NULL;
361 int error;
362 int found = 0;
363
364 *qdp = NULL;
365
366 if (sdp->sd_vfs->s_flags & MS_RDONLY)
367 return 0;
368
Abhijith Das0a7ab792009-01-07 16:03:37 -0600369 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370
371 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
372 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
373 !test_bit(QDF_CHANGE, &qd->qd_flags) ||
374 qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
375 continue;
376
377 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
378
379 set_bit(QDF_LOCKED, &qd->qd_flags);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600380 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
381 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382 qd->qd_change_sync = qd->qd_change;
383 gfs2_assert_warn(sdp, qd->qd_slot_count);
384 qd->qd_slot_count++;
385 found = 1;
386
387 break;
388 }
389
390 if (!found)
391 qd = NULL;
392
Abhijith Das0a7ab792009-01-07 16:03:37 -0600393 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394
395 if (qd) {
396 gfs2_assert_warn(sdp, qd->qd_change_sync);
397 error = bh_get(qd);
398 if (error) {
399 clear_bit(QDF_LOCKED, &qd->qd_flags);
400 slot_put(qd);
401 qd_put(qd);
402 return error;
403 }
404 }
405
406 *qdp = qd;
407
408 return 0;
409}
410
411static int qd_trylock(struct gfs2_quota_data *qd)
412{
413 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
414
415 if (sdp->sd_vfs->s_flags & MS_RDONLY)
416 return 0;
417
Abhijith Das0a7ab792009-01-07 16:03:37 -0600418 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419
420 if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
421 !test_bit(QDF_CHANGE, &qd->qd_flags)) {
Abhijith Das0a7ab792009-01-07 16:03:37 -0600422 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 return 0;
424 }
425
426 list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
427
428 set_bit(QDF_LOCKED, &qd->qd_flags);
Abhijith Das0a7ab792009-01-07 16:03:37 -0600429 gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
430 atomic_inc(&qd->qd_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 qd->qd_change_sync = qd->qd_change;
432 gfs2_assert_warn(sdp, qd->qd_slot_count);
433 qd->qd_slot_count++;
434
Abhijith Das0a7ab792009-01-07 16:03:37 -0600435 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436
437 gfs2_assert_warn(sdp, qd->qd_change_sync);
438 if (bh_get(qd)) {
439 clear_bit(QDF_LOCKED, &qd->qd_flags);
440 slot_put(qd);
441 qd_put(qd);
442 return 0;
443 }
444
445 return 1;
446}
447
448static void qd_unlock(struct gfs2_quota_data *qd)
449{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500450 gfs2_assert_warn(qd->qd_gl->gl_sbd,
451 test_bit(QDF_LOCKED, &qd->qd_flags));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452 clear_bit(QDF_LOCKED, &qd->qd_flags);
453 bh_put(qd);
454 slot_put(qd);
455 qd_put(qd);
456}
457
Steven Whitehouse33a82522009-09-15 16:25:40 +0100458static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 struct gfs2_quota_data **qdp)
460{
461 int error;
462
Steven Whitehouse6a6ada82009-09-15 16:30:38 +0100463 error = qd_get(sdp, user, id, qdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 if (error)
465 return error;
466
467 error = slot_get(*qdp);
468 if (error)
469 goto fail;
470
471 error = bh_get(*qdp);
472 if (error)
473 goto fail_slot;
474
475 return 0;
476
Steven Whitehousea91ea692006-09-04 12:04:26 -0400477fail_slot:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478 slot_put(*qdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400479fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 qd_put(*qdp);
481 return error;
482}
483
484static void qdsb_put(struct gfs2_quota_data *qd)
485{
486 bh_put(qd);
487 slot_put(qd);
488 qd_put(qd);
489}
490
Steven Whitehousecd915492006-09-04 12:49:07 -0400491int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400493 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000494 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 struct gfs2_quota_data **qd = al->al_qd;
496 int error;
497
498 if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
499 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
500 return -EIO;
501
502 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
503 return 0;
504
Steven Whitehouse33a82522009-09-15 16:25:40 +0100505 error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506 if (error)
507 goto out;
508 al->al_qd_num++;
509 qd++;
510
Steven Whitehouse33a82522009-09-15 16:25:40 +0100511 error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 if (error)
513 goto out;
514 al->al_qd_num++;
515 qd++;
516
Steven Whitehouse2933f922006-11-01 13:23:29 -0500517 if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
Steven Whitehouse33a82522009-09-15 16:25:40 +0100518 error = qdsb_get(sdp, QUOTA_USER, uid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 if (error)
520 goto out;
521 al->al_qd_num++;
522 qd++;
523 }
524
Steven Whitehouse2933f922006-11-01 13:23:29 -0500525 if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
Steven Whitehouse33a82522009-09-15 16:25:40 +0100526 error = qdsb_get(sdp, QUOTA_GROUP, gid, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 if (error)
528 goto out;
529 al->al_qd_num++;
530 qd++;
531 }
532
Steven Whitehousea91ea692006-09-04 12:04:26 -0400533out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 if (error)
535 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 return error;
537}
538
539void gfs2_quota_unhold(struct gfs2_inode *ip)
540{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400541 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000542 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 unsigned int x;
544
545 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
546
547 for (x = 0; x < al->al_qd_num; x++) {
548 qdsb_put(al->al_qd[x]);
549 al->al_qd[x] = NULL;
550 }
551 al->al_qd_num = 0;
552}
553
554static int sort_qd(const void *a, const void *b)
555{
Steven Whitehouse48fac172006-09-05 15:17:12 -0400556 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
557 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558
559 if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
560 !test_bit(QDF_USER, &qd_b->qd_flags)) {
561 if (test_bit(QDF_USER, &qd_a->qd_flags))
Steven Whitehouse48fac172006-09-05 15:17:12 -0400562 return -1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 else
Steven Whitehouse48fac172006-09-05 15:17:12 -0400564 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 }
Steven Whitehouse48fac172006-09-05 15:17:12 -0400566 if (qd_a->qd_id < qd_b->qd_id)
567 return -1;
568 if (qd_a->qd_id > qd_b->qd_id)
569 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570
Steven Whitehouse48fac172006-09-05 15:17:12 -0400571 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572}
573
Steven Whitehousecd915492006-09-04 12:49:07 -0400574static void do_qc(struct gfs2_quota_data *qd, s64 change)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575{
576 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400577 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 struct gfs2_quota_change *qc = qd->qd_bh_qc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400579 s64 x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580
Steven Whitehousef55ab262006-02-21 12:51:39 +0000581 mutex_lock(&sdp->sd_quota_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000582 gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583
584 if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
585 qc->qc_change = 0;
586 qc->qc_flags = 0;
587 if (test_bit(QDF_USER, &qd->qd_flags))
588 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
589 qc->qc_id = cpu_to_be32(qd->qd_id);
590 }
591
Al Virob44b84d2006-10-14 10:46:30 -0400592 x = be64_to_cpu(qc->qc_change) + change;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 qc->qc_change = cpu_to_be64(x);
594
Steven Whitehouse22077f52009-01-08 14:28:42 +0000595 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 qd->qd_change = x;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000597 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598
599 if (!x) {
600 gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
601 clear_bit(QDF_CHANGE, &qd->qd_flags);
602 qc->qc_flags = 0;
603 qc->qc_id = 0;
604 slot_put(qd);
605 qd_put(qd);
606 } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
607 qd_hold(qd);
608 slot_hold(qd);
609 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400610
Steven Whitehousef55ab262006-02-21 12:51:39 +0000611 mutex_unlock(&sdp->sd_quota_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612}
613
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000614/**
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100615 * gfs2_adjust_quota - adjust record of current block usage
616 * @ip: The quota inode
617 * @loc: Offset of the entry in the quota file
618 * @change: The amount of change to record
619 * @qd: The quota data
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000620 *
621 * This function was mostly borrowed from gfs2_block_truncate_page which was
622 * in turn mostly borrowed from ext3
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100623 *
624 * Returns: 0 or -ve on error
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000625 */
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100626
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000627static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
Steven Whitehousecd915492006-09-04 12:49:07 -0400628 s64 change, struct gfs2_quota_data *qd)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000629{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400630 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000631 struct address_space *mapping = inode->i_mapping;
632 unsigned long index = loc >> PAGE_CACHE_SHIFT;
Abhijith Das1990e912007-05-31 17:52:02 -0500633 unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000634 unsigned blocksize, iblock, pos;
635 struct buffer_head *bh;
636 struct page *page;
637 void *kaddr;
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100638 struct gfs2_quota *qp;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400639 s64 value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000640 int err = -EIO;
641
Abhijith Das20b95bf2008-03-06 17:43:52 -0600642 if (gfs2_is_stuffed(ip))
Abhijith Das0fd53552007-08-14 15:34:58 -0500643 gfs2_unstuff_dinode(ip, NULL);
Abhijith Das20b95bf2008-03-06 17:43:52 -0600644
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000645 page = grab_cache_page(mapping, index);
646 if (!page)
647 return -ENOMEM;
648
649 blocksize = inode->i_sb->s_blocksize;
650 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
651
652 if (!page_has_buffers(page))
653 create_empty_buffers(page, blocksize, 0);
654
655 bh = page_buffers(page);
656 pos = blocksize;
657 while (offset >= pos) {
658 bh = bh->b_this_page;
659 iblock++;
660 pos += blocksize;
661 }
662
663 if (!buffer_mapped(bh)) {
Bob Petersone9e1ef22007-12-10 14:13:27 -0600664 gfs2_block_map(inode, iblock, bh, 1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000665 if (!buffer_mapped(bh))
666 goto unlock;
667 }
668
669 if (PageUptodate(page))
670 set_buffer_uptodate(bh);
671
672 if (!buffer_uptodate(bh)) {
Steven Whitehouse2e565bb2006-10-02 11:38:25 -0400673 ll_rw_block(READ_META, 1, &bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000674 wait_on_buffer(bh);
675 if (!buffer_uptodate(bh))
676 goto unlock;
677 }
678
679 gfs2_trans_add_bh(ip->i_gl, bh, 0);
680
681 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100682 qp = kaddr + offset;
683 value = (s64)be64_to_cpu(qp->qu_value) + change;
684 qp->qu_value = cpu_to_be64(value);
685 qd->qd_qb.qb_value = qp->qu_value;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000686 flush_dcache_page(page);
687 kunmap_atomic(kaddr, KM_USER0);
688 err = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000689unlock:
690 unlock_page(page);
691 page_cache_release(page);
692 return err;
693}
694
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
696{
697 struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400698 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699 unsigned int data_blocks, ind_blocks;
700 struct gfs2_holder *ghs, i_gh;
701 unsigned int qx, x;
702 struct gfs2_quota_data *qd;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000703 loff_t offset;
Abhijith Das20b95bf2008-03-06 17:43:52 -0600704 unsigned int nalloc = 0, blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 struct gfs2_alloc *al = NULL;
706 int error;
707
708 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
709 &data_blocks, &ind_blocks);
710
Josef Bacik16c5f062008-04-09 09:33:41 -0400711 ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 if (!ghs)
713 return -ENOMEM;
714
715 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
716 for (qx = 0; qx < num_qd; qx++) {
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100717 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 GL_NOCACHE, &ghs[qx]);
719 if (error)
720 goto out;
721 }
722
723 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
724 if (error)
725 goto out;
726
727 for (x = 0; x < num_qd; x++) {
728 int alloc_required;
729
730 offset = qd2offset(qda[x]);
731 error = gfs2_write_alloc_required(ip, offset,
732 sizeof(struct gfs2_quota),
733 &alloc_required);
734 if (error)
735 goto out_gunlock;
736 if (alloc_required)
737 nalloc++;
738 }
739
Abhijith Das20b95bf2008-03-06 17:43:52 -0600740 al = gfs2_alloc_get(ip);
741 if (!al) {
742 error = -ENOMEM;
743 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 }
Abhijith Das20b95bf2008-03-06 17:43:52 -0600745 /*
746 * 1 blk for unstuffing inode if stuffed. We add this extra
747 * block to the reservation unconditionally. If the inode
748 * doesn't need unstuffing, the block will be released to the
749 * rgrp since it won't be allocated during the transaction
750 */
751 al->al_requested = 1;
752 /* +1 in the end for block requested above for unstuffing */
753 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 1;
754
755 if (nalloc)
756 al->al_requested += nalloc * (data_blocks + ind_blocks);
757 error = gfs2_inplace_reserve(ip);
758 if (error)
759 goto out_alloc;
760
761 if (nalloc)
762 blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS;
763
764 error = gfs2_trans_begin(sdp, blocks, 0);
765 if (error)
766 goto out_ipres;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767
768 for (x = 0; x < num_qd; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 qd = qda[x];
770 offset = qd2offset(qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000771 error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100772 (struct gfs2_quota_data *)qd);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000773 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 goto out_end_trans;
775
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 do_qc(qd, -qd->qd_change_sync);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 }
778
779 error = 0;
780
Steven Whitehousea91ea692006-09-04 12:04:26 -0400781out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400783out_ipres:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600784 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400785out_alloc:
Abhijith Das20b95bf2008-03-06 17:43:52 -0600786 gfs2_alloc_put(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400787out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400789out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 while (qx--)
791 gfs2_glock_dq_uninit(&ghs[qx]);
792 kfree(ghs);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400793 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 return error;
795}
796
797static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
798 struct gfs2_holder *q_gh)
799{
800 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400801 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 struct gfs2_holder i_gh;
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100803 struct gfs2_quota q;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 int error;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400805 struct gfs2_quota_lvb *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806
Steven Whitehousea91ea692006-09-04 12:04:26 -0400807restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
809 if (error)
810 return error;
811
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400812 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400814 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000815 loff_t pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816 gfs2_glock_dq_uninit(q_gh);
Steven Whitehouse91094d02009-09-11 15:21:56 +0100817 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
818 GL_NOCACHE, q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819 if (error)
820 return error;
821
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400822 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 if (error)
824 goto fail;
825
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100826 memset(&q, 0, sizeof(struct gfs2_quota));
Steven Whitehousef42faf42006-01-30 18:34:10 +0000827 pos = qd2offset(qd);
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100828 error = gfs2_internal_read(ip, NULL, (char *)&q, &pos, sizeof(q));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 if (error < 0)
830 goto fail_gunlock;
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100831 if ((error < sizeof(q)) && force_refresh) {
832 error = -ENOENT;
833 goto fail_gunlock;
834 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835 gfs2_glock_dq_uninit(&i_gh);
836
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400837 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
838 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
839 qlvb->__pad = 0;
Steven Whitehouse1e72c0f2009-09-15 20:42:56 +0100840 qlvb->qb_limit = q.qu_limit;
841 qlvb->qb_warn = q.qu_warn;
842 qlvb->qb_value = q.qu_value;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400843 qd->qd_qb = *qlvb;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844
Steven Whitehouse91094d02009-09-11 15:21:56 +0100845 gfs2_glock_dq_uninit(q_gh);
846 force_refresh = 0;
847 goto restart;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 }
849
850 return 0;
851
Steven Whitehousea91ea692006-09-04 12:04:26 -0400852fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400854fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 gfs2_glock_dq_uninit(q_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 return error;
857}
858
Steven Whitehousecd915492006-09-04 12:49:07 -0400859int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400861 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000862 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000863 unsigned int x;
864 int error = 0;
865
866 gfs2_quota_hold(ip, uid, gid);
867
868 if (capable(CAP_SYS_RESOURCE) ||
869 sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
870 return 0;
871
872 sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
873 sort_qd, NULL);
874
875 for (x = 0; x < al->al_qd_num; x++) {
876 error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
877 if (error)
878 break;
879 }
880
881 if (!error)
882 set_bit(GIF_QD_LOCKED, &ip->i_flags);
883 else {
884 while (x--)
885 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
886 gfs2_quota_unhold(ip);
887 }
888
889 return error;
890}
891
892static int need_sync(struct gfs2_quota_data *qd)
893{
894 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
895 struct gfs2_tune *gt = &sdp->sd_tune;
Steven Whitehousecd915492006-09-04 12:49:07 -0400896 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 unsigned int num, den;
898 int do_sync = 1;
899
900 if (!qd->qd_qb.qb_limit)
901 return 0;
902
Steven Whitehouse22077f52009-01-08 14:28:42 +0000903 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904 value = qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +0000905 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906
907 spin_lock(&gt->gt_spin);
908 num = gt->gt_quota_scale_num;
909 den = gt->gt_quota_scale_den;
910 spin_unlock(&gt->gt_spin);
911
912 if (value < 0)
913 do_sync = 0;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400914 else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
915 (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916 do_sync = 0;
917 else {
918 value *= gfs2_jindex_size(sdp) * num;
David Howells4abaca172008-07-11 14:39:56 +0100919 value = div_s64(value, den);
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400920 value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehousecd915492006-09-04 12:49:07 -0400921 if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 do_sync = 0;
923 }
924
925 return do_sync;
926}
927
928void gfs2_quota_unlock(struct gfs2_inode *ip)
929{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000930 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 struct gfs2_quota_data *qda[4];
932 unsigned int count = 0;
933 unsigned int x;
934
935 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
936 goto out;
937
938 for (x = 0; x < al->al_qd_num; x++) {
939 struct gfs2_quota_data *qd;
940 int sync;
941
942 qd = al->al_qd[x];
943 sync = need_sync(qd);
944
945 gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
946
947 if (sync && qd_trylock(qd))
948 qda[count++] = qd;
949 }
950
951 if (count) {
952 do_sync(count, qda);
953 for (x = 0; x < count; x++)
954 qd_unlock(qda[x]);
955 }
956
Steven Whitehousea91ea692006-09-04 12:04:26 -0400957out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 gfs2_quota_unhold(ip);
959}
960
961#define MAX_LINE 256
962
963static int print_message(struct gfs2_quota_data *qd, char *type)
964{
965 struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966
Steven Whitehouse02630a12006-07-03 11:20:06 -0400967 printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
968 sdp->sd_fsname, type,
969 (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
970 qd->qd_id);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971
972 return 0;
973}
974
Steven Whitehousecd915492006-09-04 12:49:07 -0400975int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400977 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000978 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 struct gfs2_quota_data *qd;
Steven Whitehousecd915492006-09-04 12:49:07 -0400980 s64 value;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 unsigned int x;
982 int error = 0;
983
984 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
985 return 0;
986
987 if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
988 return 0;
989
990 for (x = 0; x < al->al_qd_num; x++) {
991 qd = al->al_qd[x];
992
993 if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
994 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
995 continue;
996
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -0400997 value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
Steven Whitehouse22077f52009-01-08 14:28:42 +0000998 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 value += qd->qd_change;
Steven Whitehouse22077f52009-01-08 14:28:42 +00001000 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001
Steven Whitehousecd915492006-09-04 12:49:07 -04001002 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 +00001003 print_message(qd, "exceeded");
1004 error = -EDQUOT;
1005 break;
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04001006 } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
Steven Whitehousecd915492006-09-04 12:49:07 -04001007 (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 time_after_eq(jiffies, qd->qd_last_warn +
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001009 gfs2_tune_get(sdp,
1010 gt_quota_warn_period) * HZ)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 error = print_message(qd, "warning");
1012 qd->qd_last_warn = jiffies;
1013 }
1014 }
1015
1016 return error;
1017}
1018
Steven Whitehousecd915492006-09-04 12:49:07 -04001019void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
1020 u32 uid, u32 gid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001022 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023 struct gfs2_quota_data *qd;
1024 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001026 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 return;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001028 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 return;
1030
1031 for (x = 0; x < al->al_qd_num; x++) {
1032 qd = al->al_qd[x];
1033
1034 if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
1035 (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
1036 do_qc(qd, change);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 }
1038 }
1039}
1040
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001041int gfs2_quota_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042{
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001043 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 struct gfs2_quota_data **qda;
1045 unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
1046 unsigned int num_qd;
1047 unsigned int x;
1048 int error = 0;
1049
1050 sdp->sd_quota_sync_gen++;
1051
1052 qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
1053 if (!qda)
1054 return -ENOMEM;
1055
1056 do {
1057 num_qd = 0;
1058
1059 for (;;) {
1060 error = qd_fish(sdp, qda + num_qd);
1061 if (error || !qda[num_qd])
1062 break;
1063 if (++num_qd == max_qd)
1064 break;
1065 }
1066
1067 if (num_qd) {
1068 if (!error)
1069 error = do_sync(num_qd, qda);
1070 if (!error)
1071 for (x = 0; x < num_qd; x++)
1072 qda[x]->qd_sync_gen =
1073 sdp->sd_quota_sync_gen;
1074
1075 for (x = 0; x < num_qd; x++)
1076 qd_unlock(qda[x]);
1077 }
1078 } while (!error && num_qd == max_qd);
1079
1080 kfree(qda);
1081
1082 return error;
1083}
1084
Steven Whitehousecd915492006-09-04 12:49:07 -04001085int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001086{
1087 struct gfs2_quota_data *qd;
1088 struct gfs2_holder q_gh;
1089 int error;
1090
Steven Whitehouse6a6ada82009-09-15 16:30:38 +01001091 error = qd_get(sdp, user, id, &qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 if (error)
1093 return error;
1094
1095 error = do_glock(qd, FORCE, &q_gh);
1096 if (!error)
1097 gfs2_glock_dq_uninit(&q_gh);
1098
1099 qd_put(qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 return error;
1101}
1102
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001103static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
1104{
1105 const struct gfs2_quota_change *str = buf;
1106
1107 qc->qc_change = be64_to_cpu(str->qc_change);
1108 qc->qc_flags = be32_to_cpu(str->qc_flags);
1109 qc->qc_id = be32_to_cpu(str->qc_id);
1110}
1111
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112int gfs2_quota_init(struct gfs2_sbd *sdp)
1113{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001114 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001115 unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116 unsigned int x, slot = 0;
1117 unsigned int found = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001118 u64 dblock;
1119 u32 extlen = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 int error;
1121
Steven Whitehousec9e98882008-11-04 09:47:33 +00001122 if (!ip->i_disksize || ip->i_disksize > (64 << 20) ||
1123 ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001124 gfs2_consist_inode(ip);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001125 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 }
1127 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001128 sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129
1130 error = -ENOMEM;
1131
1132 sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
Josef Bacik16c5f062008-04-09 09:33:41 -04001133 sizeof(unsigned char *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 if (!sdp->sd_quota_bitmap)
1135 return error;
1136
1137 for (x = 0; x < sdp->sd_quota_chunks; x++) {
Josef Bacik16c5f062008-04-09 09:33:41 -04001138 sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 if (!sdp->sd_quota_bitmap[x])
1140 goto fail;
1141 }
1142
1143 for (x = 0; x < blocks; x++) {
1144 struct buffer_head *bh;
1145 unsigned int y;
1146
1147 if (!extlen) {
1148 int new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001149 error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150 if (error)
1151 goto fail;
1152 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153 error = -EIO;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001154 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
1155 if (!bh)
1156 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
1158 brelse(bh);
1159 goto fail;
1160 }
1161
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001162 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163 y++, slot++) {
Al Virob62f9632006-10-13 23:46:46 -04001164 struct gfs2_quota_change_host qc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 struct gfs2_quota_data *qd;
1166
1167 gfs2_quota_change_in(&qc, bh->b_data +
1168 sizeof(struct gfs2_meta_header) +
1169 y * sizeof(struct gfs2_quota_change));
1170 if (!qc.qc_change)
1171 continue;
1172
1173 error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
1174 qc.qc_id, &qd);
1175 if (error) {
1176 brelse(bh);
1177 goto fail;
1178 }
1179
1180 set_bit(QDF_CHANGE, &qd->qd_flags);
1181 qd->qd_change = qc.qc_change;
1182 qd->qd_slot = slot;
1183 qd->qd_slot_count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184
Abhijith Das0a7ab792009-01-07 16:03:37 -06001185 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
1187 list_add(&qd->qd_list, &sdp->sd_quota_list);
1188 atomic_inc(&sdp->sd_quota_count);
Abhijith Das0a7ab792009-01-07 16:03:37 -06001189 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190
1191 found++;
1192 }
1193
1194 brelse(bh);
1195 dblock++;
1196 extlen--;
1197 }
1198
1199 if (found)
1200 fs_info(sdp, "found %u quota changes\n", found);
1201
1202 return 0;
1203
Steven Whitehousea91ea692006-09-04 12:04:26 -04001204fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 gfs2_quota_cleanup(sdp);
1206 return error;
1207}
1208
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
1210{
1211 struct list_head *head = &sdp->sd_quota_list;
1212 struct gfs2_quota_data *qd;
1213 unsigned int x;
1214
Abhijith Das0a7ab792009-01-07 16:03:37 -06001215 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216 while (!list_empty(head)) {
1217 qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1218
Abhijith Das0a7ab792009-01-07 16:03:37 -06001219 if (atomic_read(&qd->qd_count) > 1 ||
1220 (atomic_read(&qd->qd_count) &&
1221 !test_bit(QDF_CHANGE, &qd->qd_flags))) {
Abhijith Das0a7ab792009-01-07 16:03:37 -06001222 list_move(&qd->qd_list, head);
1223 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 schedule();
Abhijith Das0a7ab792009-01-07 16:03:37 -06001225 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001226 continue;
1227 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228
Abhijith Das0a7ab792009-01-07 16:03:37 -06001229 list_del(&qd->qd_list);
1230 /* Also remove if this qd exists in the reclaim list */
1231 if (!list_empty(&qd->qd_reclaim)) {
1232 list_del_init(&qd->qd_reclaim);
1233 atomic_dec(&qd_lru_count);
1234 }
1235 atomic_dec(&sdp->sd_quota_count);
1236 spin_unlock(&qd_lru_lock);
1237
1238 if (!atomic_read(&qd->qd_count)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001239 gfs2_assert_warn(sdp, !qd->qd_change);
1240 gfs2_assert_warn(sdp, !qd->qd_slot_count);
1241 } else
1242 gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
1243 gfs2_assert_warn(sdp, !qd->qd_bh_count);
1244
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001245 gfs2_glock_put(qd->qd_gl);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001246 kmem_cache_free(gfs2_quotad_cachep, qd);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247
Abhijith Das0a7ab792009-01-07 16:03:37 -06001248 spin_lock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 }
Abhijith Das0a7ab792009-01-07 16:03:37 -06001250 spin_unlock(&qd_lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001251
1252 gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
1253
1254 if (sdp->sd_quota_bitmap) {
1255 for (x = 0; x < sdp->sd_quota_chunks; x++)
1256 kfree(sdp->sd_quota_bitmap[x]);
1257 kfree(sdp->sd_quota_bitmap);
1258 }
1259}
1260
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001261static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
1262{
1263 if (error == 0 || error == -EROFS)
1264 return;
1265 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
1266 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1267}
1268
1269static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001270 int (*fxn)(struct super_block *sb, int type),
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001271 unsigned long t, unsigned long *timeo,
1272 unsigned int *new_timeo)
1273{
1274 if (t >= *timeo) {
Steven Whitehouse8c42d632009-09-11 14:36:44 +01001275 int error = fxn(sdp->sd_vfs, 0);
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001276 quotad_error(sdp, msg, error);
1277 *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
1278 } else {
1279 *timeo -= t;
1280 }
1281}
1282
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001283static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
1284{
1285 struct gfs2_inode *ip;
1286
1287 while(1) {
1288 ip = NULL;
1289 spin_lock(&sdp->sd_trunc_lock);
1290 if (!list_empty(&sdp->sd_trunc_list)) {
1291 ip = list_entry(sdp->sd_trunc_list.next,
1292 struct gfs2_inode, i_trunc_list);
1293 list_del_init(&ip->i_trunc_list);
1294 }
1295 spin_unlock(&sdp->sd_trunc_lock);
1296 if (ip == NULL)
1297 return;
1298 gfs2_glock_finish_truncate(ip);
1299 }
1300}
1301
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001302/**
1303 * gfs2_quotad - Write cached quota changes into the quota file
1304 * @sdp: Pointer to GFS2 superblock
1305 *
1306 */
1307
1308int gfs2_quotad(void *data)
1309{
1310 struct gfs2_sbd *sdp = data;
1311 struct gfs2_tune *tune = &sdp->sd_tune;
1312 unsigned long statfs_timeo = 0;
1313 unsigned long quotad_timeo = 0;
1314 unsigned long t = 0;
1315 DEFINE_WAIT(wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001316 int empty;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001317
1318 while (!kthread_should_stop()) {
1319
1320 /* Update the master statfs file */
1321 quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
1322 &statfs_timeo, &tune->gt_statfs_quantum);
1323
1324 /* Update quota file */
1325 quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
1326 &quotad_timeo, &tune->gt_quota_quantum);
1327
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001328 /* Check for & recover partially truncated inodes */
1329 quotad_check_trunc_list(sdp);
1330
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001331 if (freezing(current))
1332 refrigerator();
1333 t = min(quotad_timeo, statfs_timeo);
1334
Steven Whitehouse7fa5d202009-03-31 15:49:08 +01001335 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001336 spin_lock(&sdp->sd_trunc_lock);
1337 empty = list_empty(&sdp->sd_trunc_list);
1338 spin_unlock(&sdp->sd_trunc_lock);
1339 if (empty)
1340 t -= schedule_timeout(t);
1341 else
1342 t = 0;
Steven Whitehouse37b2c832008-11-17 14:25:37 +00001343 finish_wait(&sdp->sd_quota_wait, &wait);
1344 }
1345
1346 return 0;
1347}
1348
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001349static int gfs2_quota_get_xstate(struct super_block *sb,
1350 struct fs_quota_stat *fqs)
1351{
1352 struct gfs2_sbd *sdp = sb->s_fs_info;
1353
1354 memset(fqs, 0, sizeof(struct fs_quota_stat));
1355 fqs->qs_version = FS_QSTAT_VERSION;
1356 if (sdp->sd_args.ar_quota == GFS2_QUOTA_ON)
1357 fqs->qs_flags = (XFS_QUOTA_UDQ_ENFD | XFS_QUOTA_GDQ_ENFD);
1358 else if (sdp->sd_args.ar_quota == GFS2_QUOTA_ACCOUNT)
1359 fqs->qs_flags = (XFS_QUOTA_UDQ_ACCT | XFS_QUOTA_GDQ_ACCT);
1360 if (sdp->sd_quota_inode) {
1361 fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
1362 fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
1363 }
1364 fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
1365 fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
1366 fqs->qs_incoredqs = atomic_read(&qd_lru_count);
1367 return 0;
1368}
1369
Steven Whitehousecc632e72009-09-15 09:59:02 +01001370const struct quotactl_ops gfs2_quotactl_ops = {
1371 .quota_sync = gfs2_quota_sync,
Steven Whitehouse1d371b52009-09-11 15:57:27 +01001372 .get_xstate = gfs2_quota_get_xstate,
Steven Whitehousecc632e72009-09-15 09:59:02 +01001373};
1374