Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
0d0868bd | 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
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. | |
1e72c0f7 | 18 | * Since quota tags are part of transactions, there is no need for a quota check |
b3b94faa DT |
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> | |
1495f230 | 41 | #include <linux/mm.h> |
b3b94faa DT |
42 | #include <linux/spinlock.h> |
43 | #include <linux/completion.h> | |
44 | #include <linux/buffer_head.h> | |
b3b94faa | 45 | #include <linux/sort.h> |
18ec7d5c | 46 | #include <linux/fs.h> |
2e565bb6 | 47 | #include <linux/bio.h> |
5c676f6d | 48 | #include <linux/gfs2_ondisk.h> |
37b2c837 SW |
49 | #include <linux/kthread.h> |
50 | #include <linux/freezer.h> | |
2ec46505 | 51 | #include <linux/quota.h> |
1d371b5e | 52 | #include <linux/dqblk_xfs.h> |
b3b94faa DT |
53 | |
54 | #include "gfs2.h" | |
5c676f6d | 55 | #include "incore.h" |
b3b94faa DT |
56 | #include "bmap.h" |
57 | #include "glock.h" | |
58 | #include "glops.h" | |
b3b94faa DT |
59 | #include "log.h" |
60 | #include "meta_io.h" | |
61 | #include "quota.h" | |
62 | #include "rgrp.h" | |
63 | #include "super.h" | |
64 | #include "trans.h" | |
18ec7d5c | 65 | #include "inode.h" |
5c676f6d | 66 | #include "util.h" |
b3b94faa | 67 | |
bb8d8a6f SW |
68 | struct gfs2_quota_change_host { |
69 | u64 qc_change; | |
70 | u32 qc_flags; /* GFS2_QCF_... */ | |
e08d8d7f | 71 | struct kqid qc_id; |
bb8d8a6f SW |
72 | }; |
73 | ||
0a7ab79c AD |
74 | static LIST_HEAD(qd_lru_list); |
75 | static atomic_t qd_lru_count = ATOMIC_INIT(0); | |
1328df72 | 76 | static DEFINE_SPINLOCK(qd_lru_lock); |
0a7ab79c | 77 | |
1ab6c499 DC |
78 | unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink, |
79 | struct shrink_control *sc) | |
0a7ab79c AD |
80 | { |
81 | struct gfs2_quota_data *qd; | |
82 | struct gfs2_sbd *sdp; | |
1495f230 | 83 | int nr_to_scan = sc->nr_to_scan; |
1ab6c499 | 84 | long freed = 0; |
0a7ab79c | 85 | |
1495f230 | 86 | if (!(sc->gfp_mask & __GFP_FS)) |
1ab6c499 | 87 | return SHRINK_STOP; |
0a7ab79c AD |
88 | |
89 | spin_lock(&qd_lru_lock); | |
1495f230 | 90 | while (nr_to_scan && !list_empty(&qd_lru_list)) { |
0a7ab79c AD |
91 | qd = list_entry(qd_lru_list.next, |
92 | struct gfs2_quota_data, qd_reclaim); | |
93 | sdp = qd->qd_gl->gl_sbd; | |
94 | ||
95 | /* Free from the filesystem-specific list */ | |
96 | list_del(&qd->qd_list); | |
97 | ||
0a7ab79c AD |
98 | gfs2_assert_warn(sdp, !qd->qd_change); |
99 | gfs2_assert_warn(sdp, !qd->qd_slot_count); | |
100 | gfs2_assert_warn(sdp, !qd->qd_bh_count); | |
101 | ||
f057f6cd | 102 | gfs2_glock_put(qd->qd_gl); |
0a7ab79c AD |
103 | atomic_dec(&sdp->sd_quota_count); |
104 | ||
105 | /* Delete it from the common reclaim list */ | |
106 | list_del_init(&qd->qd_reclaim); | |
107 | atomic_dec(&qd_lru_count); | |
108 | spin_unlock(&qd_lru_lock); | |
109 | kmem_cache_free(gfs2_quotad_cachep, qd); | |
110 | spin_lock(&qd_lru_lock); | |
1495f230 | 111 | nr_to_scan--; |
1ab6c499 | 112 | freed++; |
0a7ab79c AD |
113 | } |
114 | spin_unlock(&qd_lru_lock); | |
1ab6c499 DC |
115 | return freed; |
116 | } | |
0a7ab79c | 117 | |
1ab6c499 DC |
118 | unsigned long gfs2_qd_shrink_count(struct shrinker *shrink, |
119 | struct shrink_control *sc) | |
120 | { | |
55f841ce | 121 | return vfs_pressure_ratio(atomic_read(&qd_lru_count)); |
0a7ab79c AD |
122 | } |
123 | ||
2f6c9896 EB |
124 | static u64 qd2index(struct gfs2_quota_data *qd) |
125 | { | |
05e0a60d EB |
126 | struct kqid qid = qd->qd_id; |
127 | return (2 * (u64)from_kqid(&init_user_ns, qid)) + | |
37f71577 | 128 | ((qid.type == USRQUOTA) ? 0 : 1); |
2f6c9896 EB |
129 | } |
130 | ||
cd915493 | 131 | static u64 qd2offset(struct gfs2_quota_data *qd) |
b3b94faa | 132 | { |
cd915493 | 133 | u64 offset; |
b3b94faa | 134 | |
2f6c9896 | 135 | offset = qd2index(qd); |
b3b94faa DT |
136 | offset *= sizeof(struct gfs2_quota); |
137 | ||
138 | return offset; | |
139 | } | |
140 | ||
05e0a60d | 141 | static int qd_alloc(struct gfs2_sbd *sdp, struct kqid qid, |
b3b94faa DT |
142 | struct gfs2_quota_data **qdp) |
143 | { | |
144 | struct gfs2_quota_data *qd; | |
145 | int error; | |
146 | ||
37b2c837 | 147 | qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS); |
b3b94faa DT |
148 | if (!qd) |
149 | return -ENOMEM; | |
150 | ||
0a7ab79c | 151 | atomic_set(&qd->qd_count, 1); |
05e0a60d | 152 | qd->qd_id = qid; |
b3b94faa | 153 | qd->qd_slot = -1; |
0a7ab79c | 154 | INIT_LIST_HEAD(&qd->qd_reclaim); |
b3b94faa | 155 | |
2f6c9896 | 156 | error = gfs2_glock_get(sdp, qd2index(qd), |
b3b94faa DT |
157 | &gfs2_quota_glops, CREATE, &qd->qd_gl); |
158 | if (error) | |
159 | goto fail; | |
160 | ||
b3b94faa DT |
161 | *qdp = qd; |
162 | ||
163 | return 0; | |
164 | ||
a91ea69f | 165 | fail: |
37b2c837 | 166 | kmem_cache_free(gfs2_quotad_cachep, qd); |
b3b94faa DT |
167 | return error; |
168 | } | |
169 | ||
05e0a60d | 170 | static int qd_get(struct gfs2_sbd *sdp, struct kqid qid, |
b3b94faa DT |
171 | struct gfs2_quota_data **qdp) |
172 | { | |
173 | struct gfs2_quota_data *qd = NULL, *new_qd = NULL; | |
174 | int error, found; | |
175 | ||
176 | *qdp = NULL; | |
177 | ||
178 | for (;;) { | |
179 | found = 0; | |
0a7ab79c | 180 | spin_lock(&qd_lru_lock); |
b3b94faa | 181 | list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { |
05e0a60d | 182 | if (qid_eq(qd->qd_id, qid)) { |
0a7ab79c AD |
183 | if (!atomic_read(&qd->qd_count) && |
184 | !list_empty(&qd->qd_reclaim)) { | |
185 | /* Remove it from reclaim list */ | |
186 | list_del_init(&qd->qd_reclaim); | |
187 | atomic_dec(&qd_lru_count); | |
188 | } | |
189 | atomic_inc(&qd->qd_count); | |
b3b94faa DT |
190 | found = 1; |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
195 | if (!found) | |
196 | qd = NULL; | |
197 | ||
198 | if (!qd && new_qd) { | |
199 | qd = new_qd; | |
200 | list_add(&qd->qd_list, &sdp->sd_quota_list); | |
201 | atomic_inc(&sdp->sd_quota_count); | |
202 | new_qd = NULL; | |
203 | } | |
204 | ||
0a7ab79c | 205 | spin_unlock(&qd_lru_lock); |
b3b94faa | 206 | |
6a6ada81 | 207 | if (qd) { |
b3b94faa | 208 | if (new_qd) { |
f057f6cd | 209 | gfs2_glock_put(new_qd->qd_gl); |
37b2c837 | 210 | kmem_cache_free(gfs2_quotad_cachep, new_qd); |
b3b94faa DT |
211 | } |
212 | *qdp = qd; | |
213 | return 0; | |
214 | } | |
215 | ||
05e0a60d | 216 | error = qd_alloc(sdp, qid, &new_qd); |
b3b94faa DT |
217 | if (error) |
218 | return error; | |
219 | } | |
220 | } | |
221 | ||
222 | static void qd_hold(struct gfs2_quota_data *qd) | |
223 | { | |
224 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
0a7ab79c AD |
225 | gfs2_assert(sdp, atomic_read(&qd->qd_count)); |
226 | atomic_inc(&qd->qd_count); | |
b3b94faa DT |
227 | } |
228 | ||
229 | static void qd_put(struct gfs2_quota_data *qd) | |
230 | { | |
0a7ab79c AD |
231 | if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) { |
232 | /* Add to the reclaim list */ | |
233 | list_add_tail(&qd->qd_reclaim, &qd_lru_list); | |
234 | atomic_inc(&qd_lru_count); | |
235 | spin_unlock(&qd_lru_lock); | |
236 | } | |
b3b94faa DT |
237 | } |
238 | ||
239 | static int slot_get(struct gfs2_quota_data *qd) | |
240 | { | |
241 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
242 | unsigned int c, o = 0, b; | |
243 | unsigned char byte = 0; | |
244 | ||
22077f57 | 245 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
246 | |
247 | if (qd->qd_slot_count++) { | |
22077f57 | 248 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
249 | return 0; |
250 | } | |
251 | ||
252 | for (c = 0; c < sdp->sd_quota_chunks; c++) | |
253 | for (o = 0; o < PAGE_SIZE; o++) { | |
254 | byte = sdp->sd_quota_bitmap[c][o]; | |
255 | if (byte != 0xFF) | |
256 | goto found; | |
257 | } | |
258 | ||
259 | goto fail; | |
260 | ||
a91ea69f | 261 | found: |
b3b94faa DT |
262 | for (b = 0; b < 8; b++) |
263 | if (!(byte & (1 << b))) | |
264 | break; | |
265 | qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b; | |
266 | ||
267 | if (qd->qd_slot >= sdp->sd_quota_slots) | |
268 | goto fail; | |
269 | ||
270 | sdp->sd_quota_bitmap[c][o] |= 1 << b; | |
271 | ||
22077f57 | 272 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
273 | |
274 | return 0; | |
275 | ||
a91ea69f | 276 | fail: |
b3b94faa | 277 | qd->qd_slot_count--; |
22077f57 | 278 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
279 | return -ENOSPC; |
280 | } | |
281 | ||
282 | static void slot_hold(struct gfs2_quota_data *qd) | |
283 | { | |
284 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
285 | ||
22077f57 | 286 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
287 | gfs2_assert(sdp, qd->qd_slot_count); |
288 | qd->qd_slot_count++; | |
22077f57 | 289 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
290 | } |
291 | ||
26e43a15 SW |
292 | static void gfs2_icbit_munge(struct gfs2_sbd *sdp, unsigned char **bitmap, |
293 | unsigned int bit, int new_value) | |
294 | { | |
295 | unsigned int c, o, b = bit; | |
296 | int old_value; | |
297 | ||
298 | c = b / (8 * PAGE_SIZE); | |
299 | b %= 8 * PAGE_SIZE; | |
300 | o = b / 8; | |
301 | b %= 8; | |
302 | ||
303 | old_value = (bitmap[c][o] & (1 << b)); | |
304 | gfs2_assert_withdraw(sdp, !old_value != !new_value); | |
305 | ||
306 | if (new_value) | |
307 | bitmap[c][o] |= 1 << b; | |
308 | else | |
309 | bitmap[c][o] &= ~(1 << b); | |
310 | } | |
311 | ||
b3b94faa DT |
312 | static void slot_put(struct gfs2_quota_data *qd) |
313 | { | |
314 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
315 | ||
22077f57 | 316 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
317 | gfs2_assert(sdp, qd->qd_slot_count); |
318 | if (!--qd->qd_slot_count) { | |
319 | gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0); | |
320 | qd->qd_slot = -1; | |
321 | } | |
22077f57 | 322 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
323 | } |
324 | ||
325 | static int bh_get(struct gfs2_quota_data *qd) | |
326 | { | |
327 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
feaa7bba | 328 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
b3b94faa | 329 | unsigned int block, offset; |
b3b94faa DT |
330 | struct buffer_head *bh; |
331 | int error; | |
23591256 | 332 | struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 }; |
b3b94faa | 333 | |
f55ab26a | 334 | mutex_lock(&sdp->sd_quota_mutex); |
b3b94faa DT |
335 | |
336 | if (qd->qd_bh_count++) { | |
f55ab26a | 337 | mutex_unlock(&sdp->sd_quota_mutex); |
b3b94faa DT |
338 | return 0; |
339 | } | |
340 | ||
341 | block = qd->qd_slot / sdp->sd_qc_per_block; | |
0d0868bd | 342 | offset = qd->qd_slot % sdp->sd_qc_per_block; |
b3b94faa | 343 | |
23591256 | 344 | bh_map.b_size = 1 << ip->i_inode.i_blkbits; |
e9e1ef2b | 345 | error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0); |
b3b94faa DT |
346 | if (error) |
347 | goto fail; | |
7276b3b0 | 348 | error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh); |
b3b94faa DT |
349 | if (error) |
350 | goto fail; | |
351 | error = -EIO; | |
352 | if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) | |
353 | goto fail_brelse; | |
354 | ||
355 | qd->qd_bh = bh; | |
356 | qd->qd_bh_qc = (struct gfs2_quota_change *) | |
357 | (bh->b_data + sizeof(struct gfs2_meta_header) + | |
358 | offset * sizeof(struct gfs2_quota_change)); | |
359 | ||
2e95b665 | 360 | mutex_unlock(&sdp->sd_quota_mutex); |
b3b94faa DT |
361 | |
362 | return 0; | |
363 | ||
a91ea69f | 364 | fail_brelse: |
b3b94faa | 365 | brelse(bh); |
a91ea69f | 366 | fail: |
b3b94faa | 367 | qd->qd_bh_count--; |
f55ab26a | 368 | mutex_unlock(&sdp->sd_quota_mutex); |
b3b94faa DT |
369 | return error; |
370 | } | |
371 | ||
372 | static void bh_put(struct gfs2_quota_data *qd) | |
373 | { | |
374 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
375 | ||
f55ab26a | 376 | mutex_lock(&sdp->sd_quota_mutex); |
b3b94faa DT |
377 | gfs2_assert(sdp, qd->qd_bh_count); |
378 | if (!--qd->qd_bh_count) { | |
379 | brelse(qd->qd_bh); | |
380 | qd->qd_bh = NULL; | |
381 | qd->qd_bh_qc = NULL; | |
382 | } | |
f55ab26a | 383 | mutex_unlock(&sdp->sd_quota_mutex); |
b3b94faa DT |
384 | } |
385 | ||
386 | static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp) | |
387 | { | |
388 | struct gfs2_quota_data *qd = NULL; | |
389 | int error; | |
390 | int found = 0; | |
391 | ||
392 | *qdp = NULL; | |
393 | ||
394 | if (sdp->sd_vfs->s_flags & MS_RDONLY) | |
395 | return 0; | |
396 | ||
0a7ab79c | 397 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
398 | |
399 | list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { | |
400 | if (test_bit(QDF_LOCKED, &qd->qd_flags) || | |
401 | !test_bit(QDF_CHANGE, &qd->qd_flags) || | |
402 | qd->qd_sync_gen >= sdp->sd_quota_sync_gen) | |
403 | continue; | |
404 | ||
405 | list_move_tail(&qd->qd_list, &sdp->sd_quota_list); | |
406 | ||
407 | set_bit(QDF_LOCKED, &qd->qd_flags); | |
0a7ab79c AD |
408 | gfs2_assert_warn(sdp, atomic_read(&qd->qd_count)); |
409 | atomic_inc(&qd->qd_count); | |
b3b94faa DT |
410 | qd->qd_change_sync = qd->qd_change; |
411 | gfs2_assert_warn(sdp, qd->qd_slot_count); | |
412 | qd->qd_slot_count++; | |
413 | found = 1; | |
414 | ||
415 | break; | |
416 | } | |
417 | ||
418 | if (!found) | |
419 | qd = NULL; | |
420 | ||
0a7ab79c | 421 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
422 | |
423 | if (qd) { | |
424 | gfs2_assert_warn(sdp, qd->qd_change_sync); | |
425 | error = bh_get(qd); | |
426 | if (error) { | |
427 | clear_bit(QDF_LOCKED, &qd->qd_flags); | |
428 | slot_put(qd); | |
429 | qd_put(qd); | |
430 | return error; | |
431 | } | |
432 | } | |
433 | ||
434 | *qdp = qd; | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
439 | static int qd_trylock(struct gfs2_quota_data *qd) | |
440 | { | |
441 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
442 | ||
443 | if (sdp->sd_vfs->s_flags & MS_RDONLY) | |
444 | return 0; | |
445 | ||
0a7ab79c | 446 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
447 | |
448 | if (test_bit(QDF_LOCKED, &qd->qd_flags) || | |
449 | !test_bit(QDF_CHANGE, &qd->qd_flags)) { | |
0a7ab79c | 450 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
451 | return 0; |
452 | } | |
453 | ||
454 | list_move_tail(&qd->qd_list, &sdp->sd_quota_list); | |
455 | ||
456 | set_bit(QDF_LOCKED, &qd->qd_flags); | |
0a7ab79c AD |
457 | gfs2_assert_warn(sdp, atomic_read(&qd->qd_count)); |
458 | atomic_inc(&qd->qd_count); | |
b3b94faa DT |
459 | qd->qd_change_sync = qd->qd_change; |
460 | gfs2_assert_warn(sdp, qd->qd_slot_count); | |
461 | qd->qd_slot_count++; | |
462 | ||
0a7ab79c | 463 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
464 | |
465 | gfs2_assert_warn(sdp, qd->qd_change_sync); | |
466 | if (bh_get(qd)) { | |
467 | clear_bit(QDF_LOCKED, &qd->qd_flags); | |
468 | slot_put(qd); | |
469 | qd_put(qd); | |
470 | return 0; | |
471 | } | |
472 | ||
473 | return 1; | |
474 | } | |
475 | ||
476 | static void qd_unlock(struct gfs2_quota_data *qd) | |
477 | { | |
568f4c96 SW |
478 | gfs2_assert_warn(qd->qd_gl->gl_sbd, |
479 | test_bit(QDF_LOCKED, &qd->qd_flags)); | |
b3b94faa DT |
480 | clear_bit(QDF_LOCKED, &qd->qd_flags); |
481 | bh_put(qd); | |
482 | slot_put(qd); | |
483 | qd_put(qd); | |
484 | } | |
485 | ||
b59c8b6f | 486 | static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid, |
b3b94faa DT |
487 | struct gfs2_quota_data **qdp) |
488 | { | |
489 | int error; | |
490 | ||
05e0a60d | 491 | error = qd_get(sdp, qid, qdp); |
b3b94faa DT |
492 | if (error) |
493 | return error; | |
494 | ||
495 | error = slot_get(*qdp); | |
496 | if (error) | |
497 | goto fail; | |
498 | ||
499 | error = bh_get(*qdp); | |
500 | if (error) | |
501 | goto fail_slot; | |
502 | ||
503 | return 0; | |
504 | ||
a91ea69f | 505 | fail_slot: |
b3b94faa | 506 | slot_put(*qdp); |
a91ea69f | 507 | fail: |
b3b94faa DT |
508 | qd_put(*qdp); |
509 | return error; | |
510 | } | |
511 | ||
512 | static void qdsb_put(struct gfs2_quota_data *qd) | |
513 | { | |
514 | bh_put(qd); | |
515 | slot_put(qd); | |
516 | qd_put(qd); | |
517 | } | |
518 | ||
7c06b5d6 | 519 | int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) |
b3b94faa | 520 | { |
feaa7bba | 521 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
5407e242 | 522 | struct gfs2_quota_data **qd; |
b3b94faa DT |
523 | int error; |
524 | ||
aaaf68c5 AP |
525 | if (ip->i_res == NULL) { |
526 | error = gfs2_rs_alloc(ip); | |
527 | if (error) | |
528 | return error; | |
529 | } | |
5407e242 BP |
530 | |
531 | qd = ip->i_res->rs_qa_qd; | |
532 | ||
533 | if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) || | |
b3b94faa DT |
534 | gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) |
535 | return -EIO; | |
536 | ||
537 | if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) | |
538 | return 0; | |
539 | ||
b59c8b6f | 540 | error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd); |
b3b94faa DT |
541 | if (error) |
542 | goto out; | |
5407e242 | 543 | ip->i_res->rs_qa_qd_num++; |
b3b94faa DT |
544 | qd++; |
545 | ||
b59c8b6f | 546 | error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd); |
b3b94faa DT |
547 | if (error) |
548 | goto out; | |
5407e242 | 549 | ip->i_res->rs_qa_qd_num++; |
b3b94faa DT |
550 | qd++; |
551 | ||
6b24c0d2 EB |
552 | if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) && |
553 | !uid_eq(uid, ip->i_inode.i_uid)) { | |
b59c8b6f | 554 | error = qdsb_get(sdp, make_kqid_uid(uid), qd); |
b3b94faa DT |
555 | if (error) |
556 | goto out; | |
5407e242 | 557 | ip->i_res->rs_qa_qd_num++; |
b3b94faa DT |
558 | qd++; |
559 | } | |
560 | ||
6b24c0d2 EB |
561 | if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) && |
562 | !gid_eq(gid, ip->i_inode.i_gid)) { | |
b59c8b6f | 563 | error = qdsb_get(sdp, make_kqid_gid(gid), qd); |
b3b94faa DT |
564 | if (error) |
565 | goto out; | |
5407e242 | 566 | ip->i_res->rs_qa_qd_num++; |
b3b94faa DT |
567 | qd++; |
568 | } | |
569 | ||
a91ea69f | 570 | out: |
b3b94faa DT |
571 | if (error) |
572 | gfs2_quota_unhold(ip); | |
b3b94faa DT |
573 | return error; |
574 | } | |
575 | ||
576 | void gfs2_quota_unhold(struct gfs2_inode *ip) | |
577 | { | |
feaa7bba | 578 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
579 | unsigned int x; |
580 | ||
5407e242 BP |
581 | if (ip->i_res == NULL) |
582 | return; | |
b3b94faa DT |
583 | gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)); |
584 | ||
5407e242 BP |
585 | for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) { |
586 | qdsb_put(ip->i_res->rs_qa_qd[x]); | |
587 | ip->i_res->rs_qa_qd[x] = NULL; | |
b3b94faa | 588 | } |
5407e242 | 589 | ip->i_res->rs_qa_qd_num = 0; |
b3b94faa DT |
590 | } |
591 | ||
592 | static int sort_qd(const void *a, const void *b) | |
593 | { | |
48fac179 SW |
594 | const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a; |
595 | const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b; | |
b3b94faa | 596 | |
05e0a60d | 597 | if (qid_lt(qd_a->qd_id, qd_b->qd_id)) |
48fac179 | 598 | return -1; |
05e0a60d | 599 | if (qid_lt(qd_b->qd_id, qd_a->qd_id)) |
48fac179 | 600 | return 1; |
48fac179 | 601 | return 0; |
b3b94faa DT |
602 | } |
603 | ||
cd915493 | 604 | static void do_qc(struct gfs2_quota_data *qd, s64 change) |
b3b94faa DT |
605 | { |
606 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
feaa7bba | 607 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
b3b94faa | 608 | struct gfs2_quota_change *qc = qd->qd_bh_qc; |
cd915493 | 609 | s64 x; |
b3b94faa | 610 | |
f55ab26a | 611 | mutex_lock(&sdp->sd_quota_mutex); |
350a9b0a | 612 | gfs2_trans_add_meta(ip->i_gl, qd->qd_bh); |
b3b94faa DT |
613 | |
614 | if (!test_bit(QDF_CHANGE, &qd->qd_flags)) { | |
615 | qc->qc_change = 0; | |
616 | qc->qc_flags = 0; | |
05e0a60d | 617 | if (qd->qd_id.type == USRQUOTA) |
b3b94faa | 618 | qc->qc_flags = cpu_to_be32(GFS2_QCF_USER); |
05e0a60d | 619 | qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id)); |
b3b94faa DT |
620 | } |
621 | ||
b44b84d7 | 622 | x = be64_to_cpu(qc->qc_change) + change; |
b3b94faa DT |
623 | qc->qc_change = cpu_to_be64(x); |
624 | ||
22077f57 | 625 | spin_lock(&qd_lru_lock); |
b3b94faa | 626 | qd->qd_change = x; |
22077f57 | 627 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
628 | |
629 | if (!x) { | |
630 | gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags)); | |
631 | clear_bit(QDF_CHANGE, &qd->qd_flags); | |
632 | qc->qc_flags = 0; | |
633 | qc->qc_id = 0; | |
634 | slot_put(qd); | |
635 | qd_put(qd); | |
636 | } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) { | |
637 | qd_hold(qd); | |
638 | slot_hold(qd); | |
639 | } | |
907b9bce | 640 | |
f55ab26a | 641 | mutex_unlock(&sdp->sd_quota_mutex); |
b3b94faa DT |
642 | } |
643 | ||
18ec7d5c | 644 | /** |
1e72c0f7 SW |
645 | * gfs2_adjust_quota - adjust record of current block usage |
646 | * @ip: The quota inode | |
647 | * @loc: Offset of the entry in the quota file | |
e285c100 | 648 | * @change: The amount of usage change to record |
1e72c0f7 | 649 | * @qd: The quota data |
e285c100 | 650 | * @fdq: The updated limits to record |
18ec7d5c SW |
651 | * |
652 | * This function was mostly borrowed from gfs2_block_truncate_page which was | |
653 | * in turn mostly borrowed from ext3 | |
1e72c0f7 SW |
654 | * |
655 | * Returns: 0 or -ve on error | |
18ec7d5c | 656 | */ |
1e72c0f7 | 657 | |
18ec7d5c | 658 | static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc, |
e285c100 SW |
659 | s64 change, struct gfs2_quota_data *qd, |
660 | struct fs_disk_quota *fdq) | |
18ec7d5c | 661 | { |
feaa7bba | 662 | struct inode *inode = &ip->i_inode; |
14870b45 | 663 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
18ec7d5c SW |
664 | struct address_space *mapping = inode->i_mapping; |
665 | unsigned long index = loc >> PAGE_CACHE_SHIFT; | |
1990e917 | 666 | unsigned offset = loc & (PAGE_CACHE_SIZE - 1); |
18ec7d5c | 667 | unsigned blocksize, iblock, pos; |
ab9bbda0 | 668 | struct buffer_head *bh; |
18ec7d5c | 669 | struct page *page; |
7e619bc3 AD |
670 | void *kaddr, *ptr; |
671 | struct gfs2_quota q, *qp; | |
672 | int err, nbytes; | |
e285c100 | 673 | u64 size; |
18ec7d5c | 674 | |
891a8e93 SW |
675 | if (gfs2_is_stuffed(ip)) { |
676 | err = gfs2_unstuff_dinode(ip, NULL); | |
677 | if (err) | |
678 | return err; | |
679 | } | |
7e619bc3 AD |
680 | |
681 | memset(&q, 0, sizeof(struct gfs2_quota)); | |
4306629e | 682 | err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q)); |
7e619bc3 AD |
683 | if (err < 0) |
684 | return err; | |
685 | ||
686 | err = -EIO; | |
687 | qp = &q; | |
688 | qp->qu_value = be64_to_cpu(qp->qu_value); | |
689 | qp->qu_value += change; | |
690 | qp->qu_value = cpu_to_be64(qp->qu_value); | |
691 | qd->qd_qb.qb_value = qp->qu_value; | |
692 | if (fdq) { | |
693 | if (fdq->d_fieldmask & FS_DQ_BSOFT) { | |
14870b45 | 694 | qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift); |
7e619bc3 AD |
695 | qd->qd_qb.qb_warn = qp->qu_warn; |
696 | } | |
697 | if (fdq->d_fieldmask & FS_DQ_BHARD) { | |
14870b45 | 698 | qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift); |
7e619bc3 AD |
699 | qd->qd_qb.qb_limit = qp->qu_limit; |
700 | } | |
802ec9b6 AD |
701 | if (fdq->d_fieldmask & FS_DQ_BCOUNT) { |
702 | qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift); | |
703 | qd->qd_qb.qb_value = qp->qu_value; | |
704 | } | |
7e619bc3 AD |
705 | } |
706 | ||
707 | /* Write the quota into the quota file on disk */ | |
708 | ptr = qp; | |
709 | nbytes = sizeof(struct gfs2_quota); | |
710 | get_a_page: | |
220cca2a | 711 | page = find_or_create_page(mapping, index, GFP_NOFS); |
18ec7d5c SW |
712 | if (!page) |
713 | return -ENOMEM; | |
714 | ||
715 | blocksize = inode->i_sb->s_blocksize; | |
716 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | |
717 | ||
718 | if (!page_has_buffers(page)) | |
719 | create_empty_buffers(page, blocksize, 0); | |
720 | ||
721 | bh = page_buffers(page); | |
722 | pos = blocksize; | |
723 | while (offset >= pos) { | |
724 | bh = bh->b_this_page; | |
725 | iblock++; | |
726 | pos += blocksize; | |
727 | } | |
728 | ||
729 | if (!buffer_mapped(bh)) { | |
e9e1ef2b | 730 | gfs2_block_map(inode, iblock, bh, 1); |
18ec7d5c | 731 | if (!buffer_mapped(bh)) |
7e619bc3 AD |
732 | goto unlock_out; |
733 | /* If it's a newly allocated disk block for quota, zero it */ | |
8b421601 AD |
734 | if (buffer_new(bh)) |
735 | zero_user(page, pos - blocksize, bh->b_size); | |
18ec7d5c SW |
736 | } |
737 | ||
738 | if (PageUptodate(page)) | |
739 | set_buffer_uptodate(bh); | |
740 | ||
741 | if (!buffer_uptodate(bh)) { | |
20ed0535 | 742 | ll_rw_block(READ | REQ_META, 1, &bh); |
18ec7d5c SW |
743 | wait_on_buffer(bh); |
744 | if (!buffer_uptodate(bh)) | |
7e619bc3 | 745 | goto unlock_out; |
18ec7d5c SW |
746 | } |
747 | ||
37f71577 | 748 | gfs2_trans_add_data(ip->i_gl, bh); |
18ec7d5c | 749 | |
d9349285 | 750 | kaddr = kmap_atomic(page); |
7e619bc3 AD |
751 | if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE) |
752 | nbytes = PAGE_CACHE_SIZE - offset; | |
753 | memcpy(kaddr + offset, ptr, nbytes); | |
18ec7d5c | 754 | flush_dcache_page(page); |
d9349285 | 755 | kunmap_atomic(kaddr); |
7e619bc3 AD |
756 | unlock_page(page); |
757 | page_cache_release(page); | |
758 | ||
759 | /* If quota straddles page boundary, we need to update the rest of the | |
760 | * quota at the beginning of the next page */ | |
8b421601 | 761 | if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) { |
7e619bc3 AD |
762 | ptr = ptr + nbytes; |
763 | nbytes = sizeof(struct gfs2_quota) - nbytes; | |
764 | offset = 0; | |
765 | index++; | |
766 | goto get_a_page; | |
767 | } | |
e285c100 | 768 | |
e285c100 | 769 | size = loc + sizeof(struct gfs2_quota); |
a2e0f799 | 770 | if (size > inode->i_size) |
e285c100 | 771 | i_size_write(inode, size); |
e285c100 | 772 | inode->i_mtime = inode->i_atime = CURRENT_TIME; |
e285c100 | 773 | mark_inode_dirty(inode); |
500242ac | 774 | return 0; |
ab9bbda0 | 775 | |
7e619bc3 | 776 | unlock_out: |
18ec7d5c SW |
777 | unlock_page(page); |
778 | page_cache_release(page); | |
779 | return err; | |
780 | } | |
781 | ||
b3b94faa DT |
782 | static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda) |
783 | { | |
784 | struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd; | |
feaa7bba | 785 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); |
7b9cff46 | 786 | struct gfs2_alloc_parms ap = { .aflags = 0, }; |
b3b94faa DT |
787 | unsigned int data_blocks, ind_blocks; |
788 | struct gfs2_holder *ghs, i_gh; | |
789 | unsigned int qx, x; | |
790 | struct gfs2_quota_data *qd; | |
71f890f7 | 791 | unsigned reserved; |
f42faf4f | 792 | loff_t offset; |
20b95bf2 | 793 | unsigned int nalloc = 0, blocks; |
b3b94faa DT |
794 | int error; |
795 | ||
0a305e49 BP |
796 | error = gfs2_rs_alloc(ip); |
797 | if (error) | |
798 | return error; | |
799 | ||
b3b94faa DT |
800 | gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), |
801 | &data_blocks, &ind_blocks); | |
802 | ||
16c5f06f | 803 | ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS); |
b3b94faa DT |
804 | if (!ghs) |
805 | return -ENOMEM; | |
806 | ||
807 | sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL); | |
56aa72d0 | 808 | mutex_lock(&ip->i_inode.i_mutex); |
b3b94faa | 809 | for (qx = 0; qx < num_qd; qx++) { |
1e72c0f7 | 810 | error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE, |
b3b94faa DT |
811 | GL_NOCACHE, &ghs[qx]); |
812 | if (error) | |
813 | goto out; | |
814 | } | |
815 | ||
816 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); | |
817 | if (error) | |
818 | goto out; | |
819 | ||
820 | for (x = 0; x < num_qd; x++) { | |
b3b94faa | 821 | offset = qd2offset(qda[x]); |
461cb419 BP |
822 | if (gfs2_write_alloc_required(ip, offset, |
823 | sizeof(struct gfs2_quota))) | |
b3b94faa DT |
824 | nalloc++; |
825 | } | |
826 | ||
20b95bf2 AD |
827 | /* |
828 | * 1 blk for unstuffing inode if stuffed. We add this extra | |
829 | * block to the reservation unconditionally. If the inode | |
830 | * doesn't need unstuffing, the block will be released to the | |
831 | * rgrp since it won't be allocated during the transaction | |
832 | */ | |
7e619bc3 AD |
833 | /* +3 in the end for unstuffing block, inode size update block |
834 | * and another block in case quota straddles page boundary and | |
835 | * two blocks need to be updated instead of 1 */ | |
836 | blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3; | |
b3b94faa | 837 | |
71f890f7 | 838 | reserved = 1 + (nalloc * (data_blocks + ind_blocks)); |
7b9cff46 SW |
839 | ap.target = reserved; |
840 | error = gfs2_inplace_reserve(ip, &ap); | |
20b95bf2 AD |
841 | if (error) |
842 | goto out_alloc; | |
b3b94faa | 843 | |
20b95bf2 | 844 | if (nalloc) |
71f890f7 | 845 | blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS; |
20b95bf2 AD |
846 | |
847 | error = gfs2_trans_begin(sdp, blocks, 0); | |
848 | if (error) | |
849 | goto out_ipres; | |
b3b94faa DT |
850 | |
851 | for (x = 0; x < num_qd; x++) { | |
b3b94faa DT |
852 | qd = qda[x]; |
853 | offset = qd2offset(qd); | |
e285c100 | 854 | error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL); |
18ec7d5c | 855 | if (error) |
b3b94faa | 856 | goto out_end_trans; |
b3b94faa DT |
857 | |
858 | do_qc(qd, -qd->qd_change_sync); | |
662e3a55 | 859 | set_bit(QDF_REFRESH, &qd->qd_flags); |
b3b94faa DT |
860 | } |
861 | ||
862 | error = 0; | |
863 | ||
a91ea69f | 864 | out_end_trans: |
b3b94faa | 865 | gfs2_trans_end(sdp); |
a91ea69f | 866 | out_ipres: |
20b95bf2 | 867 | gfs2_inplace_release(ip); |
a91ea69f | 868 | out_alloc: |
b3b94faa | 869 | gfs2_glock_dq_uninit(&i_gh); |
a91ea69f | 870 | out: |
b3b94faa DT |
871 | while (qx--) |
872 | gfs2_glock_dq_uninit(&ghs[qx]); | |
e285c100 | 873 | mutex_unlock(&ip->i_inode.i_mutex); |
b3b94faa | 874 | kfree(ghs); |
b09e593d | 875 | gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl); |
b3b94faa DT |
876 | return error; |
877 | } | |
878 | ||
e285c100 SW |
879 | static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd) |
880 | { | |
881 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); | |
882 | struct gfs2_quota q; | |
883 | struct gfs2_quota_lvb *qlvb; | |
884 | loff_t pos; | |
885 | int error; | |
886 | ||
887 | memset(&q, 0, sizeof(struct gfs2_quota)); | |
888 | pos = qd2offset(qd); | |
4306629e | 889 | error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q)); |
e285c100 SW |
890 | if (error < 0) |
891 | return error; | |
892 | ||
4e2f8849 | 893 | qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; |
e285c100 SW |
894 | qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC); |
895 | qlvb->__pad = 0; | |
896 | qlvb->qb_limit = q.qu_limit; | |
897 | qlvb->qb_warn = q.qu_warn; | |
898 | qlvb->qb_value = q.qu_value; | |
899 | qd->qd_qb = *qlvb; | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
b3b94faa DT |
904 | static int do_glock(struct gfs2_quota_data *qd, int force_refresh, |
905 | struct gfs2_holder *q_gh) | |
906 | { | |
907 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
feaa7bba | 908 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); |
b3b94faa | 909 | struct gfs2_holder i_gh; |
b3b94faa DT |
910 | int error; |
911 | ||
a91ea69f | 912 | restart: |
b3b94faa DT |
913 | error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh); |
914 | if (error) | |
915 | return error; | |
916 | ||
4e2f8849 | 917 | qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; |
b3b94faa | 918 | |
e9fc2aa0 | 919 | if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) { |
b3b94faa | 920 | gfs2_glock_dq_uninit(q_gh); |
91094d0f SW |
921 | error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, |
922 | GL_NOCACHE, q_gh); | |
b3b94faa DT |
923 | if (error) |
924 | return error; | |
925 | ||
e9fc2aa0 | 926 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); |
b3b94faa DT |
927 | if (error) |
928 | goto fail; | |
929 | ||
e285c100 SW |
930 | error = update_qd(sdp, qd); |
931 | if (error) | |
1e72c0f7 | 932 | goto fail_gunlock; |
b3b94faa | 933 | |
e285c100 | 934 | gfs2_glock_dq_uninit(&i_gh); |
91094d0f SW |
935 | gfs2_glock_dq_uninit(q_gh); |
936 | force_refresh = 0; | |
937 | goto restart; | |
b3b94faa DT |
938 | } |
939 | ||
940 | return 0; | |
941 | ||
a91ea69f | 942 | fail_gunlock: |
b3b94faa | 943 | gfs2_glock_dq_uninit(&i_gh); |
a91ea69f | 944 | fail: |
b3b94faa | 945 | gfs2_glock_dq_uninit(q_gh); |
b3b94faa DT |
946 | return error; |
947 | } | |
948 | ||
7c06b5d6 | 949 | int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) |
b3b94faa | 950 | { |
feaa7bba | 951 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
662e3a55 | 952 | struct gfs2_quota_data *qd; |
b3b94faa DT |
953 | unsigned int x; |
954 | int error = 0; | |
955 | ||
891a8e93 SW |
956 | error = gfs2_quota_hold(ip, uid, gid); |
957 | if (error) | |
958 | return error; | |
b3b94faa DT |
959 | |
960 | if (capable(CAP_SYS_RESOURCE) || | |
961 | sdp->sd_args.ar_quota != GFS2_QUOTA_ON) | |
962 | return 0; | |
963 | ||
5407e242 BP |
964 | sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num, |
965 | sizeof(struct gfs2_quota_data *), sort_qd, NULL); | |
b3b94faa | 966 | |
5407e242 | 967 | for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) { |
662e3a55 | 968 | int force = NO_FORCE; |
5407e242 | 969 | qd = ip->i_res->rs_qa_qd[x]; |
662e3a55 AD |
970 | if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags)) |
971 | force = FORCE; | |
5407e242 | 972 | error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]); |
b3b94faa DT |
973 | if (error) |
974 | break; | |
975 | } | |
976 | ||
977 | if (!error) | |
978 | set_bit(GIF_QD_LOCKED, &ip->i_flags); | |
979 | else { | |
980 | while (x--) | |
5407e242 | 981 | gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]); |
b3b94faa DT |
982 | gfs2_quota_unhold(ip); |
983 | } | |
984 | ||
985 | return error; | |
986 | } | |
987 | ||
988 | static int need_sync(struct gfs2_quota_data *qd) | |
989 | { | |
990 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
991 | struct gfs2_tune *gt = &sdp->sd_tune; | |
cd915493 | 992 | s64 value; |
b3b94faa DT |
993 | unsigned int num, den; |
994 | int do_sync = 1; | |
995 | ||
996 | if (!qd->qd_qb.qb_limit) | |
997 | return 0; | |
998 | ||
22077f57 | 999 | spin_lock(&qd_lru_lock); |
b3b94faa | 1000 | value = qd->qd_change; |
22077f57 | 1001 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
1002 | |
1003 | spin_lock(>->gt_spin); | |
1004 | num = gt->gt_quota_scale_num; | |
1005 | den = gt->gt_quota_scale_den; | |
1006 | spin_unlock(>->gt_spin); | |
1007 | ||
1008 | if (value < 0) | |
1009 | do_sync = 0; | |
e9fc2aa0 SW |
1010 | else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >= |
1011 | (s64)be64_to_cpu(qd->qd_qb.qb_limit)) | |
b3b94faa DT |
1012 | do_sync = 0; |
1013 | else { | |
1014 | value *= gfs2_jindex_size(sdp) * num; | |
4abaca17 | 1015 | value = div_s64(value, den); |
e9fc2aa0 | 1016 | value += (s64)be64_to_cpu(qd->qd_qb.qb_value); |
cd915493 | 1017 | if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit)) |
b3b94faa DT |
1018 | do_sync = 0; |
1019 | } | |
1020 | ||
1021 | return do_sync; | |
1022 | } | |
1023 | ||
1024 | void gfs2_quota_unlock(struct gfs2_inode *ip) | |
1025 | { | |
b3b94faa DT |
1026 | struct gfs2_quota_data *qda[4]; |
1027 | unsigned int count = 0; | |
1028 | unsigned int x; | |
1029 | ||
1030 | if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags)) | |
1031 | goto out; | |
1032 | ||
5407e242 | 1033 | for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) { |
b3b94faa DT |
1034 | struct gfs2_quota_data *qd; |
1035 | int sync; | |
1036 | ||
5407e242 | 1037 | qd = ip->i_res->rs_qa_qd[x]; |
b3b94faa DT |
1038 | sync = need_sync(qd); |
1039 | ||
5407e242 | 1040 | gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]); |
b3b94faa DT |
1041 | |
1042 | if (sync && qd_trylock(qd)) | |
1043 | qda[count++] = qd; | |
1044 | } | |
1045 | ||
1046 | if (count) { | |
1047 | do_sync(count, qda); | |
1048 | for (x = 0; x < count; x++) | |
1049 | qd_unlock(qda[x]); | |
1050 | } | |
1051 | ||
a91ea69f | 1052 | out: |
b3b94faa DT |
1053 | gfs2_quota_unhold(ip); |
1054 | } | |
1055 | ||
1056 | #define MAX_LINE 256 | |
1057 | ||
1058 | static int print_message(struct gfs2_quota_data *qd, char *type) | |
1059 | { | |
1060 | struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd; | |
b3b94faa | 1061 | |
2ec46505 | 1062 | printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n", |
02630a12 | 1063 | sdp->sd_fsname, type, |
05e0a60d EB |
1064 | (qd->qd_id.type == USRQUOTA) ? "user" : "group", |
1065 | from_kqid(&init_user_ns, qd->qd_id)); | |
b3b94faa DT |
1066 | |
1067 | return 0; | |
1068 | } | |
1069 | ||
7c06b5d6 | 1070 | int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) |
b3b94faa | 1071 | { |
feaa7bba | 1072 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 1073 | struct gfs2_quota_data *qd; |
cd915493 | 1074 | s64 value; |
b3b94faa DT |
1075 | unsigned int x; |
1076 | int error = 0; | |
1077 | ||
1078 | if (!test_bit(GIF_QD_LOCKED, &ip->i_flags)) | |
1079 | return 0; | |
1080 | ||
1081 | if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON) | |
1082 | return 0; | |
1083 | ||
5407e242 BP |
1084 | for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) { |
1085 | qd = ip->i_res->rs_qa_qd[x]; | |
b3b94faa | 1086 | |
05e0a60d EB |
1087 | if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) || |
1088 | qid_eq(qd->qd_id, make_kqid_gid(gid)))) | |
b3b94faa DT |
1089 | continue; |
1090 | ||
e9fc2aa0 | 1091 | value = (s64)be64_to_cpu(qd->qd_qb.qb_value); |
22077f57 | 1092 | spin_lock(&qd_lru_lock); |
b3b94faa | 1093 | value += qd->qd_change; |
22077f57 | 1094 | spin_unlock(&qd_lru_lock); |
b3b94faa | 1095 | |
cd915493 | 1096 | if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) { |
b3b94faa | 1097 | print_message(qd, "exceeded"); |
05e0a60d | 1098 | quota_send_warning(qd->qd_id, |
2ec46505 SW |
1099 | sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN); |
1100 | ||
b3b94faa DT |
1101 | error = -EDQUOT; |
1102 | break; | |
e9fc2aa0 | 1103 | } else if (be64_to_cpu(qd->qd_qb.qb_warn) && |
cd915493 | 1104 | (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value && |
b3b94faa | 1105 | time_after_eq(jiffies, qd->qd_last_warn + |
568f4c96 SW |
1106 | gfs2_tune_get(sdp, |
1107 | gt_quota_warn_period) * HZ)) { | |
05e0a60d | 1108 | quota_send_warning(qd->qd_id, |
2ec46505 | 1109 | sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN); |
b3b94faa DT |
1110 | error = print_message(qd, "warning"); |
1111 | qd->qd_last_warn = jiffies; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | return error; | |
1116 | } | |
1117 | ||
cd915493 | 1118 | void gfs2_quota_change(struct gfs2_inode *ip, s64 change, |
7c06b5d6 | 1119 | kuid_t uid, kgid_t gid) |
b3b94faa | 1120 | { |
b3b94faa DT |
1121 | struct gfs2_quota_data *qd; |
1122 | unsigned int x; | |
b3b94faa | 1123 | |
feaa7bba | 1124 | if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change)) |
b3b94faa | 1125 | return; |
383f01fb | 1126 | if (ip->i_diskflags & GFS2_DIF_SYSTEM) |
b3b94faa DT |
1127 | return; |
1128 | ||
5407e242 BP |
1129 | for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) { |
1130 | qd = ip->i_res->rs_qa_qd[x]; | |
b3b94faa | 1131 | |
05e0a60d EB |
1132 | if (qid_eq(qd->qd_id, make_kqid_uid(uid)) || |
1133 | qid_eq(qd->qd_id, make_kqid_gid(gid))) { | |
b3b94faa | 1134 | do_qc(qd, change); |
b3b94faa DT |
1135 | } |
1136 | } | |
1137 | } | |
1138 | ||
ceed1723 | 1139 | int gfs2_quota_sync(struct super_block *sb, int type) |
b3b94faa | 1140 | { |
8c42d637 | 1141 | struct gfs2_sbd *sdp = sb->s_fs_info; |
b3b94faa | 1142 | struct gfs2_quota_data **qda; |
bef292a7 | 1143 | unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder); |
b3b94faa DT |
1144 | unsigned int num_qd; |
1145 | unsigned int x; | |
1146 | int error = 0; | |
1147 | ||
1148 | sdp->sd_quota_sync_gen++; | |
1149 | ||
1150 | qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL); | |
1151 | if (!qda) | |
1152 | return -ENOMEM; | |
1153 | ||
1154 | do { | |
1155 | num_qd = 0; | |
1156 | ||
1157 | for (;;) { | |
1158 | error = qd_fish(sdp, qda + num_qd); | |
1159 | if (error || !qda[num_qd]) | |
1160 | break; | |
1161 | if (++num_qd == max_qd) | |
1162 | break; | |
1163 | } | |
1164 | ||
1165 | if (num_qd) { | |
1166 | if (!error) | |
1167 | error = do_sync(num_qd, qda); | |
1168 | if (!error) | |
1169 | for (x = 0; x < num_qd; x++) | |
1170 | qda[x]->qd_sync_gen = | |
1171 | sdp->sd_quota_sync_gen; | |
1172 | ||
1173 | for (x = 0; x < num_qd; x++) | |
1174 | qd_unlock(qda[x]); | |
1175 | } | |
1176 | } while (!error && num_qd == max_qd); | |
1177 | ||
1178 | kfree(qda); | |
1179 | ||
1180 | return error; | |
1181 | } | |
1182 | ||
ed87dabc | 1183 | int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid) |
b3b94faa DT |
1184 | { |
1185 | struct gfs2_quota_data *qd; | |
1186 | struct gfs2_holder q_gh; | |
1187 | int error; | |
1188 | ||
05e0a60d | 1189 | error = qd_get(sdp, qid, &qd); |
b3b94faa DT |
1190 | if (error) |
1191 | return error; | |
1192 | ||
1193 | error = do_glock(qd, FORCE, &q_gh); | |
1194 | if (!error) | |
1195 | gfs2_glock_dq_uninit(&q_gh); | |
1196 | ||
1197 | qd_put(qd); | |
b3b94faa DT |
1198 | return error; |
1199 | } | |
1200 | ||
bb8d8a6f SW |
1201 | static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf) |
1202 | { | |
1203 | const struct gfs2_quota_change *str = buf; | |
1204 | ||
1205 | qc->qc_change = be64_to_cpu(str->qc_change); | |
1206 | qc->qc_flags = be32_to_cpu(str->qc_flags); | |
e08d8d7f EB |
1207 | qc->qc_id = make_kqid(&init_user_ns, |
1208 | (qc->qc_flags & GFS2_QCF_USER)?USRQUOTA:GRPQUOTA, | |
1209 | be32_to_cpu(str->qc_id)); | |
bb8d8a6f SW |
1210 | } |
1211 | ||
b3b94faa DT |
1212 | int gfs2_quota_init(struct gfs2_sbd *sdp) |
1213 | { | |
feaa7bba | 1214 | struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); |
a2e0f799 SW |
1215 | u64 size = i_size_read(sdp->sd_qc_inode); |
1216 | unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift; | |
b3b94faa DT |
1217 | unsigned int x, slot = 0; |
1218 | unsigned int found = 0; | |
cd915493 SW |
1219 | u64 dblock; |
1220 | u32 extlen = 0; | |
b3b94faa DT |
1221 | int error; |
1222 | ||
a2e0f799 | 1223 | if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20)) |
907b9bce | 1224 | return -EIO; |
a2e0f799 | 1225 | |
b3b94faa | 1226 | sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block; |
5c676f6d | 1227 | sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE); |
b3b94faa DT |
1228 | |
1229 | error = -ENOMEM; | |
1230 | ||
1231 | sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks, | |
16c5f06f | 1232 | sizeof(unsigned char *), GFP_NOFS); |
b3b94faa DT |
1233 | if (!sdp->sd_quota_bitmap) |
1234 | return error; | |
1235 | ||
1236 | for (x = 0; x < sdp->sd_quota_chunks; x++) { | |
16c5f06f | 1237 | sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS); |
b3b94faa DT |
1238 | if (!sdp->sd_quota_bitmap[x]) |
1239 | goto fail; | |
1240 | } | |
1241 | ||
1242 | for (x = 0; x < blocks; x++) { | |
1243 | struct buffer_head *bh; | |
1244 | unsigned int y; | |
1245 | ||
1246 | if (!extlen) { | |
1247 | int new = 0; | |
feaa7bba | 1248 | error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen); |
b3b94faa DT |
1249 | if (error) |
1250 | goto fail; | |
1251 | } | |
b3b94faa | 1252 | error = -EIO; |
7276b3b0 SW |
1253 | bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); |
1254 | if (!bh) | |
1255 | goto fail; | |
b3b94faa DT |
1256 | if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) { |
1257 | brelse(bh); | |
1258 | goto fail; | |
1259 | } | |
1260 | ||
7276b3b0 | 1261 | for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots; |
b3b94faa | 1262 | y++, slot++) { |
b62f963e | 1263 | struct gfs2_quota_change_host qc; |
b3b94faa DT |
1264 | struct gfs2_quota_data *qd; |
1265 | ||
1266 | gfs2_quota_change_in(&qc, bh->b_data + | |
1267 | sizeof(struct gfs2_meta_header) + | |
1268 | y * sizeof(struct gfs2_quota_change)); | |
1269 | if (!qc.qc_change) | |
1270 | continue; | |
1271 | ||
05e0a60d | 1272 | error = qd_alloc(sdp, qc.qc_id, &qd); |
b3b94faa DT |
1273 | if (error) { |
1274 | brelse(bh); | |
1275 | goto fail; | |
1276 | } | |
1277 | ||
1278 | set_bit(QDF_CHANGE, &qd->qd_flags); | |
1279 | qd->qd_change = qc.qc_change; | |
1280 | qd->qd_slot = slot; | |
1281 | qd->qd_slot_count = 1; | |
b3b94faa | 1282 | |
0a7ab79c | 1283 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
1284 | gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1); |
1285 | list_add(&qd->qd_list, &sdp->sd_quota_list); | |
1286 | atomic_inc(&sdp->sd_quota_count); | |
0a7ab79c | 1287 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
1288 | |
1289 | found++; | |
1290 | } | |
1291 | ||
1292 | brelse(bh); | |
1293 | dblock++; | |
1294 | extlen--; | |
1295 | } | |
1296 | ||
1297 | if (found) | |
1298 | fs_info(sdp, "found %u quota changes\n", found); | |
1299 | ||
1300 | return 0; | |
1301 | ||
a91ea69f | 1302 | fail: |
b3b94faa DT |
1303 | gfs2_quota_cleanup(sdp); |
1304 | return error; | |
1305 | } | |
1306 | ||
b3b94faa DT |
1307 | void gfs2_quota_cleanup(struct gfs2_sbd *sdp) |
1308 | { | |
1309 | struct list_head *head = &sdp->sd_quota_list; | |
1310 | struct gfs2_quota_data *qd; | |
1311 | unsigned int x; | |
1312 | ||
0a7ab79c | 1313 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
1314 | while (!list_empty(head)) { |
1315 | qd = list_entry(head->prev, struct gfs2_quota_data, qd_list); | |
1316 | ||
0a7ab79c AD |
1317 | if (atomic_read(&qd->qd_count) > 1 || |
1318 | (atomic_read(&qd->qd_count) && | |
1319 | !test_bit(QDF_CHANGE, &qd->qd_flags))) { | |
0a7ab79c AD |
1320 | list_move(&qd->qd_list, head); |
1321 | spin_unlock(&qd_lru_lock); | |
b3b94faa | 1322 | schedule(); |
0a7ab79c | 1323 | spin_lock(&qd_lru_lock); |
b3b94faa DT |
1324 | continue; |
1325 | } | |
1326 | ||
1327 | list_del(&qd->qd_list); | |
0a7ab79c AD |
1328 | /* Also remove if this qd exists in the reclaim list */ |
1329 | if (!list_empty(&qd->qd_reclaim)) { | |
1330 | list_del_init(&qd->qd_reclaim); | |
1331 | atomic_dec(&qd_lru_count); | |
1332 | } | |
b3b94faa | 1333 | atomic_dec(&sdp->sd_quota_count); |
0a7ab79c | 1334 | spin_unlock(&qd_lru_lock); |
b3b94faa | 1335 | |
0a7ab79c | 1336 | if (!atomic_read(&qd->qd_count)) { |
b3b94faa DT |
1337 | gfs2_assert_warn(sdp, !qd->qd_change); |
1338 | gfs2_assert_warn(sdp, !qd->qd_slot_count); | |
1339 | } else | |
1340 | gfs2_assert_warn(sdp, qd->qd_slot_count == 1); | |
1341 | gfs2_assert_warn(sdp, !qd->qd_bh_count); | |
1342 | ||
f057f6cd | 1343 | gfs2_glock_put(qd->qd_gl); |
37b2c837 | 1344 | kmem_cache_free(gfs2_quotad_cachep, qd); |
b3b94faa | 1345 | |
0a7ab79c | 1346 | spin_lock(&qd_lru_lock); |
b3b94faa | 1347 | } |
0a7ab79c | 1348 | spin_unlock(&qd_lru_lock); |
b3b94faa DT |
1349 | |
1350 | gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count)); | |
1351 | ||
1352 | if (sdp->sd_quota_bitmap) { | |
1353 | for (x = 0; x < sdp->sd_quota_chunks; x++) | |
1354 | kfree(sdp->sd_quota_bitmap[x]); | |
1355 | kfree(sdp->sd_quota_bitmap); | |
1356 | } | |
1357 | } | |
1358 | ||
37b2c837 SW |
1359 | static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error) |
1360 | { | |
1361 | if (error == 0 || error == -EROFS) | |
1362 | return; | |
1363 | if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) | |
1364 | fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error); | |
1365 | } | |
1366 | ||
1367 | static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg, | |
8c42d637 | 1368 | int (*fxn)(struct super_block *sb, int type), |
37b2c837 SW |
1369 | unsigned long t, unsigned long *timeo, |
1370 | unsigned int *new_timeo) | |
1371 | { | |
1372 | if (t >= *timeo) { | |
8c42d637 | 1373 | int error = fxn(sdp->sd_vfs, 0); |
37b2c837 SW |
1374 | quotad_error(sdp, msg, error); |
1375 | *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ; | |
1376 | } else { | |
1377 | *timeo -= t; | |
1378 | } | |
1379 | } | |
1380 | ||
813e0c46 SW |
1381 | static void quotad_check_trunc_list(struct gfs2_sbd *sdp) |
1382 | { | |
1383 | struct gfs2_inode *ip; | |
1384 | ||
1385 | while(1) { | |
1386 | ip = NULL; | |
1387 | spin_lock(&sdp->sd_trunc_lock); | |
1388 | if (!list_empty(&sdp->sd_trunc_list)) { | |
1389 | ip = list_entry(sdp->sd_trunc_list.next, | |
1390 | struct gfs2_inode, i_trunc_list); | |
1391 | list_del_init(&ip->i_trunc_list); | |
1392 | } | |
1393 | spin_unlock(&sdp->sd_trunc_lock); | |
1394 | if (ip == NULL) | |
1395 | return; | |
1396 | gfs2_glock_finish_truncate(ip); | |
1397 | } | |
1398 | } | |
1399 | ||
3d3c10f2 BM |
1400 | void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) { |
1401 | if (!sdp->sd_statfs_force_sync) { | |
1402 | sdp->sd_statfs_force_sync = 1; | |
1403 | wake_up(&sdp->sd_quota_wait); | |
1404 | } | |
1405 | } | |
1406 | ||
1407 | ||
37b2c837 SW |
1408 | /** |
1409 | * gfs2_quotad - Write cached quota changes into the quota file | |
1410 | * @sdp: Pointer to GFS2 superblock | |
1411 | * | |
1412 | */ | |
1413 | ||
1414 | int gfs2_quotad(void *data) | |
1415 | { | |
1416 | struct gfs2_sbd *sdp = data; | |
1417 | struct gfs2_tune *tune = &sdp->sd_tune; | |
1418 | unsigned long statfs_timeo = 0; | |
1419 | unsigned long quotad_timeo = 0; | |
1420 | unsigned long t = 0; | |
1421 | DEFINE_WAIT(wait); | |
813e0c46 | 1422 | int empty; |
37b2c837 SW |
1423 | |
1424 | while (!kthread_should_stop()) { | |
1425 | ||
1426 | /* Update the master statfs file */ | |
3d3c10f2 BM |
1427 | if (sdp->sd_statfs_force_sync) { |
1428 | int error = gfs2_statfs_sync(sdp->sd_vfs, 0); | |
1429 | quotad_error(sdp, "statfs", error); | |
1430 | statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ; | |
1431 | } | |
1432 | else | |
1433 | quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t, | |
1434 | &statfs_timeo, | |
1435 | &tune->gt_statfs_quantum); | |
37b2c837 SW |
1436 | |
1437 | /* Update quota file */ | |
edd2e9ac | 1438 | quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t, |
37b2c837 SW |
1439 | "ad_timeo, &tune->gt_quota_quantum); |
1440 | ||
813e0c46 SW |
1441 | /* Check for & recover partially truncated inodes */ |
1442 | quotad_check_trunc_list(sdp); | |
1443 | ||
a0acae0e TH |
1444 | try_to_freeze(); |
1445 | ||
37b2c837 SW |
1446 | t = min(quotad_timeo, statfs_timeo); |
1447 | ||
7fa5d20d | 1448 | prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE); |
813e0c46 SW |
1449 | spin_lock(&sdp->sd_trunc_lock); |
1450 | empty = list_empty(&sdp->sd_trunc_list); | |
1451 | spin_unlock(&sdp->sd_trunc_lock); | |
3d3c10f2 | 1452 | if (empty && !sdp->sd_statfs_force_sync) |
813e0c46 SW |
1453 | t -= schedule_timeout(t); |
1454 | else | |
1455 | t = 0; | |
37b2c837 SW |
1456 | finish_wait(&sdp->sd_quota_wait, &wait); |
1457 | } | |
1458 | ||
1459 | return 0; | |
1460 | } | |
1461 | ||
1d371b5e SW |
1462 | static int gfs2_quota_get_xstate(struct super_block *sb, |
1463 | struct fs_quota_stat *fqs) | |
1464 | { | |
1465 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
1466 | ||
1467 | memset(fqs, 0, sizeof(struct fs_quota_stat)); | |
1468 | fqs->qs_version = FS_QSTAT_VERSION; | |
ad6bb90f CH |
1469 | |
1470 | switch (sdp->sd_args.ar_quota) { | |
1471 | case GFS2_QUOTA_ON: | |
ade7ce31 | 1472 | fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD); |
ad6bb90f CH |
1473 | /*FALLTHRU*/ |
1474 | case GFS2_QUOTA_ACCOUNT: | |
ade7ce31 | 1475 | fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT); |
ad6bb90f CH |
1476 | break; |
1477 | case GFS2_QUOTA_OFF: | |
1478 | break; | |
1479 | } | |
1480 | ||
1d371b5e SW |
1481 | if (sdp->sd_quota_inode) { |
1482 | fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr; | |
1483 | fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks; | |
1484 | } | |
1485 | fqs->qs_uquota.qfs_nextents = 1; /* unsupported */ | |
1486 | fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */ | |
1487 | fqs->qs_incoredqs = atomic_read(&qd_lru_count); | |
1488 | return 0; | |
1489 | } | |
1490 | ||
74a8a103 | 1491 | static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid, |
b9b2dd36 | 1492 | struct fs_disk_quota *fdq) |
113d6b3c SW |
1493 | { |
1494 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
1495 | struct gfs2_quota_lvb *qlvb; | |
1496 | struct gfs2_quota_data *qd; | |
1497 | struct gfs2_holder q_gh; | |
1498 | int error; | |
1499 | ||
1500 | memset(fdq, 0, sizeof(struct fs_disk_quota)); | |
1501 | ||
1502 | if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) | |
1503 | return -ESRCH; /* Crazy XFS error code */ | |
1504 | ||
236c64e4 EB |
1505 | if ((qid.type != USRQUOTA) && |
1506 | (qid.type != GRPQUOTA)) | |
113d6b3c SW |
1507 | return -EINVAL; |
1508 | ||
05e0a60d | 1509 | error = qd_get(sdp, qid, &qd); |
113d6b3c SW |
1510 | if (error) |
1511 | return error; | |
1512 | error = do_glock(qd, FORCE, &q_gh); | |
1513 | if (error) | |
1514 | goto out; | |
1515 | ||
4e2f8849 | 1516 | qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; |
113d6b3c | 1517 | fdq->d_version = FS_DQUOT_VERSION; |
236c64e4 | 1518 | fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA; |
558e8528 | 1519 | fdq->d_id = from_kqid_munged(current_user_ns(), qid); |
14870b45 AD |
1520 | fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift; |
1521 | fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift; | |
1522 | fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift; | |
113d6b3c SW |
1523 | |
1524 | gfs2_glock_dq_uninit(&q_gh); | |
1525 | out: | |
1526 | qd_put(qd); | |
1527 | return error; | |
1528 | } | |
1529 | ||
e285c100 | 1530 | /* GFS2 only supports a subset of the XFS fields */ |
802ec9b6 | 1531 | #define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT) |
e285c100 | 1532 | |
74a8a103 | 1533 | static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid, |
c472b432 | 1534 | struct fs_disk_quota *fdq) |
e285c100 SW |
1535 | { |
1536 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
1537 | struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); | |
1538 | struct gfs2_quota_data *qd; | |
1539 | struct gfs2_holder q_gh, i_gh; | |
1540 | unsigned int data_blocks, ind_blocks; | |
1541 | unsigned int blocks = 0; | |
1542 | int alloc_required; | |
e285c100 SW |
1543 | loff_t offset; |
1544 | int error; | |
1545 | ||
1546 | if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) | |
1547 | return -ESRCH; /* Crazy XFS error code */ | |
1548 | ||
236c64e4 EB |
1549 | if ((qid.type != USRQUOTA) && |
1550 | (qid.type != GRPQUOTA)) | |
e285c100 | 1551 | return -EINVAL; |
e285c100 SW |
1552 | |
1553 | if (fdq->d_fieldmask & ~GFS2_FIELDMASK) | |
1554 | return -EINVAL; | |
e285c100 | 1555 | |
05e0a60d | 1556 | error = qd_get(sdp, qid, &qd); |
e285c100 SW |
1557 | if (error) |
1558 | return error; | |
1559 | ||
0a305e49 BP |
1560 | error = gfs2_rs_alloc(ip); |
1561 | if (error) | |
1562 | goto out_put; | |
1563 | ||
e285c100 SW |
1564 | mutex_lock(&ip->i_inode.i_mutex); |
1565 | error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh); | |
1566 | if (error) | |
0a305e49 | 1567 | goto out_unlockput; |
e285c100 SW |
1568 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
1569 | if (error) | |
1570 | goto out_q; | |
1571 | ||
1572 | /* Check for existing entry, if none then alloc new blocks */ | |
1573 | error = update_qd(sdp, qd); | |
1574 | if (error) | |
1575 | goto out_i; | |
1576 | ||
1577 | /* If nothing has changed, this is a no-op */ | |
1578 | if ((fdq->d_fieldmask & FS_DQ_BSOFT) && | |
14870b45 | 1579 | ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn))) |
e285c100 | 1580 | fdq->d_fieldmask ^= FS_DQ_BSOFT; |
802ec9b6 | 1581 | |
e285c100 | 1582 | if ((fdq->d_fieldmask & FS_DQ_BHARD) && |
14870b45 | 1583 | ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit))) |
e285c100 | 1584 | fdq->d_fieldmask ^= FS_DQ_BHARD; |
802ec9b6 AD |
1585 | |
1586 | if ((fdq->d_fieldmask & FS_DQ_BCOUNT) && | |
1587 | ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value))) | |
1588 | fdq->d_fieldmask ^= FS_DQ_BCOUNT; | |
1589 | ||
e285c100 SW |
1590 | if (fdq->d_fieldmask == 0) |
1591 | goto out_i; | |
1592 | ||
1593 | offset = qd2offset(qd); | |
461cb419 | 1594 | alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota)); |
e79a46a0 AD |
1595 | if (gfs2_is_stuffed(ip)) |
1596 | alloc_required = 1; | |
e285c100 | 1597 | if (alloc_required) { |
7b9cff46 | 1598 | struct gfs2_alloc_parms ap = { .aflags = 0, }; |
e285c100 SW |
1599 | gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), |
1600 | &data_blocks, &ind_blocks); | |
564e12b1 | 1601 | blocks = 1 + data_blocks + ind_blocks; |
7b9cff46 SW |
1602 | ap.target = blocks; |
1603 | error = gfs2_inplace_reserve(ip, &ap); | |
e285c100 | 1604 | if (error) |
564e12b1 | 1605 | goto out_i; |
71f890f7 | 1606 | blocks += gfs2_rg_blocks(ip, blocks); |
e285c100 SW |
1607 | } |
1608 | ||
e79a46a0 AD |
1609 | /* Some quotas span block boundaries and can update two blocks, |
1610 | adding an extra block to the transaction to handle such quotas */ | |
1611 | error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0); | |
e285c100 SW |
1612 | if (error) |
1613 | goto out_release; | |
1614 | ||
1615 | /* Apply changes */ | |
1616 | error = gfs2_adjust_quota(ip, offset, 0, qd, fdq); | |
1617 | ||
1618 | gfs2_trans_end(sdp); | |
1619 | out_release: | |
564e12b1 | 1620 | if (alloc_required) |
e285c100 | 1621 | gfs2_inplace_release(ip); |
e285c100 SW |
1622 | out_i: |
1623 | gfs2_glock_dq_uninit(&i_gh); | |
1624 | out_q: | |
1625 | gfs2_glock_dq_uninit(&q_gh); | |
0a305e49 | 1626 | out_unlockput: |
e285c100 | 1627 | mutex_unlock(&ip->i_inode.i_mutex); |
0a305e49 | 1628 | out_put: |
e285c100 SW |
1629 | qd_put(qd); |
1630 | return error; | |
1631 | } | |
1632 | ||
cc632e7f SW |
1633 | const struct quotactl_ops gfs2_quotactl_ops = { |
1634 | .quota_sync = gfs2_quota_sync, | |
1d371b5e | 1635 | .get_xstate = gfs2_quota_get_xstate, |
b9b2dd36 | 1636 | .get_dqblk = gfs2_get_dqblk, |
c472b432 | 1637 | .set_dqblk = gfs2_set_dqblk, |
cc632e7f | 1638 | }; |