]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ocfs2/quota_global.c
quota: Refactor dquot_transfer code so that OCFS2 can pass in its references
[mirror_ubuntu-bionic-kernel.git] / fs / ocfs2 / quota_global.c
CommitLineData
9e33d69f
JK
1/*
2 * Implementation of operations over global quota file
3 */
171bf93c 4#include <linux/spinlock.h>
9e33d69f 5#include <linux/fs.h>
5a0e3ad6 6#include <linux/slab.h>
9e33d69f
JK
7#include <linux/quota.h>
8#include <linux/quotaops.h>
9#include <linux/dqblk_qtree.h>
171bf93c
MF
10#include <linux/jiffies.h>
11#include <linux/writeback.h>
12#include <linux/workqueue.h>
9e33d69f
JK
13
14#define MLOG_MASK_PREFIX ML_QUOTA
15#include <cluster/masklog.h>
16
17#include "ocfs2_fs.h"
18#include "ocfs2.h"
19#include "alloc.h"
d6b32bbb 20#include "blockcheck.h"
9e33d69f
JK
21#include "inode.h"
22#include "journal.h"
23#include "file.h"
24#include "sysfile.h"
25#include "dlmglue.h"
26#include "uptodate.h"
ada50827 27#include "super.h"
9e33d69f
JK
28#include "quota.h"
29
171bf93c
MF
30static struct workqueue_struct *ocfs2_quota_wq = NULL;
31
32static void qsync_work_fn(struct work_struct *work);
33
9e33d69f
JK
34static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
35{
36 struct ocfs2_global_disk_dqblk *d = dp;
37 struct mem_dqblk *m = &dquot->dq_dqb;
38
39 /* Update from disk only entries not set by the admin */
40 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
41 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
42 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
43 }
44 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
45 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
46 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
47 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
48 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
49 }
50 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
51 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
52 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
53 m->dqb_btime = le64_to_cpu(d->dqb_btime);
54 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
55 m->dqb_itime = le64_to_cpu(d->dqb_itime);
56 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
57}
58
59static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
60{
61 struct ocfs2_global_disk_dqblk *d = dp;
62 struct mem_dqblk *m = &dquot->dq_dqb;
63
64 d->dqb_id = cpu_to_le32(dquot->dq_id);
65 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
66 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
67 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
68 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
69 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
70 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
71 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
72 d->dqb_btime = cpu_to_le64(m->dqb_btime);
73 d->dqb_itime = cpu_to_le64(m->dqb_itime);
7669f54c 74 d->dqb_pad1 = d->dqb_pad2 = 0;
9e33d69f
JK
75}
76
77static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
78{
79 struct ocfs2_global_disk_dqblk *d = dp;
80 struct ocfs2_mem_dqinfo *oinfo =
81 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
82
83 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
84 return 0;
85 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
86}
87
88struct qtree_fmt_operations ocfs2_global_ops = {
89 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
90 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
91 .is_id = ocfs2_global_is_id,
92};
93
684ef278
JB
94static int ocfs2_validate_quota_block(struct super_block *sb,
95 struct buffer_head *bh)
96{
d6b32bbb
JB
97 struct ocfs2_disk_dqtrailer *dqt =
98 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
684ef278
JB
99
100 mlog(0, "Validating quota block %llu\n",
101 (unsigned long long)bh->b_blocknr);
102
d6b32bbb
JB
103 BUG_ON(!buffer_uptodate(bh));
104
105 /*
106 * If the ecc fails, we return the error but otherwise
107 * leave the filesystem running. We know any error is
108 * local to this block.
109 */
110 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
684ef278
JB
111}
112
85eb8b73
JB
113int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
114 struct buffer_head **bh)
9e33d69f 115{
85eb8b73
JB
116 int rc = 0;
117 struct buffer_head *tmp = *bh;
9e33d69f 118
ada50827
JK
119 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
120 ocfs2_error(inode->i_sb,
121 "Quota file %llu is probably corrupted! Requested "
122 "to read block %Lu but file has size only %Lu\n",
123 (unsigned long long)OCFS2_I(inode)->ip_blkno,
124 (unsigned long long)v_block,
125 (unsigned long long)i_size_read(inode));
126 return -EIO;
127 }
684ef278
JB
128 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
129 ocfs2_validate_quota_block);
85eb8b73
JB
130 if (rc)
131 mlog_errno(rc);
132
133 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
134 if (!rc && !*bh)
135 *bh = tmp;
9e33d69f 136
85eb8b73 137 return rc;
9e33d69f
JK
138}
139
53a36046
JK
140static int ocfs2_get_quota_block(struct inode *inode, int block,
141 struct buffer_head **bh)
9e33d69f
JK
142{
143 u64 pblock, pcount;
53a36046 144 int err;
9e33d69f
JK
145
146 down_read(&OCFS2_I(inode)->ip_alloc_sem);
53a36046 147 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
9e33d69f 148 up_read(&OCFS2_I(inode)->ip_alloc_sem);
53a36046
JK
149 if (err) {
150 mlog_errno(err);
151 return err;
9e33d69f 152 }
53a36046
JK
153 *bh = sb_getblk(inode->i_sb, pblock);
154 if (!*bh) {
155 err = -EIO;
156 mlog_errno(err);
9e33d69f 157 }
a419aef8 158 return err;
9e33d69f
JK
159}
160
161/* Read data from global quotafile - avoid pagecache and such because we cannot
162 * afford acquiring the locks... We use quota cluster lock to serialize
163 * operations. Caller is responsible for acquiring it. */
164ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
165 size_t len, loff_t off)
166{
167 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
168 struct inode *gqinode = oinfo->dqi_gqinode;
169 loff_t i_size = i_size_read(gqinode);
170 int offset = off & (sb->s_blocksize - 1);
171 sector_t blk = off >> sb->s_blocksize_bits;
172 int err = 0;
173 struct buffer_head *bh;
174 size_t toread, tocopy;
175
176 if (off > i_size)
177 return 0;
178 if (off + len > i_size)
179 len = i_size - off;
180 toread = len;
181 while (toread > 0) {
dad7d975 182 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
85eb8b73
JB
183 bh = NULL;
184 err = ocfs2_read_quota_block(gqinode, blk, &bh);
185 if (err) {
9e33d69f
JK
186 mlog_errno(err);
187 return err;
188 }
189 memcpy(data, bh->b_data + offset, tocopy);
190 brelse(bh);
191 offset = 0;
192 toread -= tocopy;
193 data += tocopy;
194 blk++;
195 }
196 return len;
197}
198
199/* Write to quotafile (we know the transaction is already started and has
200 * enough credits) */
201ssize_t ocfs2_quota_write(struct super_block *sb, int type,
202 const char *data, size_t len, loff_t off)
203{
204 struct mem_dqinfo *info = sb_dqinfo(sb, type);
205 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
206 struct inode *gqinode = oinfo->dqi_gqinode;
207 int offset = off & (sb->s_blocksize - 1);
208 sector_t blk = off >> sb->s_blocksize_bits;
af09e51b 209 int err = 0, new = 0, ja_type;
85eb8b73 210 struct buffer_head *bh = NULL;
9e33d69f
JK
211 handle_t *handle = journal_current_handle();
212
213 if (!handle) {
214 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
215 "because transaction was not started.\n",
216 (unsigned long long)off, (unsigned long long)len);
217 return -EIO;
218 }
219 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
220 WARN_ON(1);
221 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
222 }
223
224 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
225 if (gqinode->i_size < off + len) {
b57ac2c4
JK
226 loff_t rounded_end =
227 ocfs2_align_bytes_to_blocks(sb, off + len);
228
b409d7a0 229 /* Space is already allocated in ocfs2_global_read_dquot() */
9e33d69f
JK
230 err = ocfs2_simple_size_update(gqinode,
231 oinfo->dqi_gqi_bh,
b57ac2c4 232 rounded_end);
9e33d69f
JK
233 if (err < 0)
234 goto out;
235 new = 1;
236 }
237 /* Not rewriting whole block? */
238 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
239 !new) {
85eb8b73 240 err = ocfs2_read_quota_block(gqinode, blk, &bh);
af09e51b 241 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
9e33d69f 242 } else {
53a36046 243 err = ocfs2_get_quota_block(gqinode, blk, &bh);
af09e51b 244 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
9e33d69f 245 }
af09e51b
JK
246 if (err) {
247 mlog_errno(err);
e9956fae 248 goto out;
9e33d69f
JK
249 }
250 lock_buffer(bh);
251 if (new)
252 memset(bh->b_data, 0, sb->s_blocksize);
253 memcpy(bh->b_data + offset, data, len);
254 flush_dcache_page(bh->b_page);
af09e51b 255 set_buffer_uptodate(bh);
9e33d69f 256 unlock_buffer(bh);
8cb471e8 257 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
0cf2f763
JB
258 err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
259 ja_type);
af09e51b
JK
260 if (err < 0) {
261 brelse(bh);
262 goto out;
263 }
ec20cec7 264 ocfs2_journal_dirty(handle, bh);
9e33d69f 265 brelse(bh);
9e33d69f
JK
266out:
267 if (err) {
268 mutex_unlock(&gqinode->i_mutex);
269 mlog_errno(err);
270 return err;
271 }
272 gqinode->i_version++;
273 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
274 mutex_unlock(&gqinode->i_mutex);
275 return len;
276}
277
278int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
279{
280 int status;
281 struct buffer_head *bh = NULL;
282
283 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
284 if (status < 0)
285 return status;
286 spin_lock(&dq_data_lock);
287 if (!oinfo->dqi_gqi_count++)
288 oinfo->dqi_gqi_bh = bh;
289 else
290 WARN_ON(bh != oinfo->dqi_gqi_bh);
291 spin_unlock(&dq_data_lock);
292 return 0;
293}
294
295void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
296{
297 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
298 brelse(oinfo->dqi_gqi_bh);
299 spin_lock(&dq_data_lock);
300 if (!--oinfo->dqi_gqi_count)
301 oinfo->dqi_gqi_bh = NULL;
302 spin_unlock(&dq_data_lock);
303}
304
305/* Read information header from global quota file */
306int ocfs2_global_read_info(struct super_block *sb, int type)
307{
308 struct inode *gqinode = NULL;
309 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
310 GROUP_QUOTA_SYSTEM_INODE };
311 struct ocfs2_global_disk_dqinfo dinfo;
312 struct mem_dqinfo *info = sb_dqinfo(sb, type);
313 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
314 int status;
315
316 mlog_entry_void();
317
318 /* Read global header */
319 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
320 OCFS2_INVALID_SLOT);
321 if (!gqinode) {
322 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
323 type);
324 status = -EINVAL;
325 goto out_err;
326 }
327 oinfo->dqi_gi.dqi_sb = sb;
328 oinfo->dqi_gi.dqi_type = type;
329 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
330 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
331 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
332 oinfo->dqi_gqi_bh = NULL;
333 oinfo->dqi_gqi_count = 0;
334 oinfo->dqi_gqinode = gqinode;
335 status = ocfs2_lock_global_qf(oinfo, 0);
336 if (status < 0) {
337 mlog_errno(status);
338 goto out_err;
339 }
340 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
341 sizeof(struct ocfs2_global_disk_dqinfo),
342 OCFS2_GLOBAL_INFO_OFF);
343 ocfs2_unlock_global_qf(oinfo, 0);
344 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
345 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
346 status);
347 if (status >= 0)
348 status = -EIO;
349 mlog_errno(status);
350 goto out_err;
351 }
352 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
353 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
354 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
355 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
356 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
357 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
358 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
359 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
360 OCFS2_QBLK_RESERVED_SPACE;
361 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
171bf93c
MF
362 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
363 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
4539f1df 364 msecs_to_jiffies(oinfo->dqi_syncms));
171bf93c 365
9e33d69f
JK
366out_err:
367 mlog_exit(status);
368 return status;
369}
370
371/* Write information to global quota file. Expects exlusive lock on quota
372 * file inode and quota info */
373static int __ocfs2_global_write_info(struct super_block *sb, int type)
374{
375 struct mem_dqinfo *info = sb_dqinfo(sb, type);
376 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
377 struct ocfs2_global_disk_dqinfo dinfo;
378 ssize_t size;
379
380 spin_lock(&dq_data_lock);
381 info->dqi_flags &= ~DQF_INFO_DIRTY;
382 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
383 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
384 spin_unlock(&dq_data_lock);
385 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
386 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
387 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
388 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
389 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
390 sizeof(struct ocfs2_global_disk_dqinfo),
391 OCFS2_GLOBAL_INFO_OFF);
392 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
393 mlog(ML_ERROR, "Cannot write global quota info structure\n");
394 if (size >= 0)
395 size = -EIO;
396 return size;
397 }
398 return 0;
399}
400
401int ocfs2_global_write_info(struct super_block *sb, int type)
402{
403 int err;
404 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
405
406 err = ocfs2_qinfo_lock(info, 1);
407 if (err < 0)
408 return err;
409 err = __ocfs2_global_write_info(sb, type);
410 ocfs2_qinfo_unlock(info, 1);
411 return err;
412}
413
b409d7a0
JK
414static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
415{
416 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
417
418 /*
419 * We may need to allocate tree blocks and a leaf block but not the
420 * root block
421 */
422 return oinfo->dqi_gi.dqi_qtree_depth;
423}
424
425static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
426{
427 /* We modify all the allocated blocks, tree root, and info block */
428 return (ocfs2_global_qinit_alloc(sb, type) + 2) *
429 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
430}
431
9e33d69f
JK
432/* Read in information from global quota file and acquire a reference to it.
433 * dquot_acquire() has already started the transaction and locked quota file */
434int ocfs2_global_read_dquot(struct dquot *dquot)
435{
436 int err, err2, ex = 0;
b409d7a0
JK
437 struct super_block *sb = dquot->dq_sb;
438 int type = dquot->dq_type;
439 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
440 struct ocfs2_super *osb = OCFS2_SB(sb);
441 struct inode *gqinode = info->dqi_gqinode;
442 int need_alloc = ocfs2_global_qinit_alloc(sb, type);
443 handle_t *handle = NULL;
9e33d69f
JK
444
445 err = ocfs2_qinfo_lock(info, 0);
446 if (err < 0)
447 goto out;
448 err = qtree_read_dquot(&info->dqi_gi, dquot);
449 if (err < 0)
450 goto out_qlock;
451 OCFS2_DQUOT(dquot)->dq_use_count++;
452 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
453 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
b409d7a0
JK
454 ocfs2_qinfo_unlock(info, 0);
455
9e33d69f 456 if (!dquot->dq_off) { /* No real quota entry? */
9e33d69f 457 ex = 1;
b409d7a0
JK
458 /*
459 * Add blocks to quota file before we start a transaction since
460 * locking allocators ranks above a transaction start
461 */
462 WARN_ON(journal_current_handle());
463 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
464 err = ocfs2_extend_no_holes(gqinode,
465 gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
466 gqinode->i_size);
467 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
468 if (err < 0)
469 goto out;
9e33d69f 470 }
b409d7a0
JK
471
472 handle = ocfs2_start_trans(osb,
473 ocfs2_calc_global_qinit_credits(sb, type));
474 if (IS_ERR(handle)) {
475 err = PTR_ERR(handle);
476 goto out;
477 }
478 err = ocfs2_qinfo_lock(info, ex);
479 if (err < 0)
480 goto out_trans;
9e33d69f
JK
481 err = qtree_write_dquot(&info->dqi_gi, dquot);
482 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
483 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
484 if (!err)
485 err = err2;
486 }
487out_qlock:
488 if (ex)
489 ocfs2_qinfo_unlock(info, 1);
4e8a3019
JK
490 else
491 ocfs2_qinfo_unlock(info, 0);
b409d7a0
JK
492out_trans:
493 if (handle)
494 ocfs2_commit_trans(osb, handle);
9e33d69f
JK
495out:
496 if (err < 0)
497 mlog_errno(err);
498 return err;
499}
500
501/* Sync local information about quota modifications with global quota file.
502 * Caller must have started the transaction and obtained exclusive lock for
503 * global quota file inode */
504int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
505{
506 int err, err2;
507 struct super_block *sb = dquot->dq_sb;
508 int type = dquot->dq_type;
509 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
510 struct ocfs2_global_disk_dqblk dqblk;
511 s64 spacechange, inodechange;
512 time_t olditime, oldbtime;
513
514 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
515 sizeof(struct ocfs2_global_disk_dqblk),
516 dquot->dq_off);
517 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
518 if (err >= 0) {
519 mlog(ML_ERROR, "Short read from global quota file "
520 "(%u read)\n", err);
521 err = -EIO;
522 }
523 goto out;
524 }
525
526 /* Update space and inode usage. Get also other information from
527 * global quota file so that we don't overwrite any changes there.
528 * We are */
529 spin_lock(&dq_data_lock);
530 spacechange = dquot->dq_dqb.dqb_curspace -
531 OCFS2_DQUOT(dquot)->dq_origspace;
532 inodechange = dquot->dq_dqb.dqb_curinodes -
533 OCFS2_DQUOT(dquot)->dq_originodes;
534 olditime = dquot->dq_dqb.dqb_itime;
535 oldbtime = dquot->dq_dqb.dqb_btime;
536 ocfs2_global_disk2memdqb(dquot, &dqblk);
9a2f3866
JK
537 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
538 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
539 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
9e33d69f
JK
540 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
541 dquot->dq_dqb.dqb_curspace += spacechange;
542 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
543 dquot->dq_dqb.dqb_curinodes += inodechange;
544 /* Set properly space grace time... */
545 if (dquot->dq_dqb.dqb_bsoftlimit &&
546 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
547 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
548 oldbtime > 0) {
549 if (dquot->dq_dqb.dqb_btime > 0)
550 dquot->dq_dqb.dqb_btime =
551 min(dquot->dq_dqb.dqb_btime, oldbtime);
552 else
553 dquot->dq_dqb.dqb_btime = oldbtime;
554 }
555 } else {
556 dquot->dq_dqb.dqb_btime = 0;
557 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
558 }
559 /* Set properly inode grace time... */
560 if (dquot->dq_dqb.dqb_isoftlimit &&
561 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
562 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
563 olditime > 0) {
564 if (dquot->dq_dqb.dqb_itime > 0)
565 dquot->dq_dqb.dqb_itime =
566 min(dquot->dq_dqb.dqb_itime, olditime);
567 else
568 dquot->dq_dqb.dqb_itime = olditime;
569 }
570 } else {
571 dquot->dq_dqb.dqb_itime = 0;
572 clear_bit(DQ_INODES_B, &dquot->dq_flags);
573 }
574 /* All information is properly updated, clear the flags */
575 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
576 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
577 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
578 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
579 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
580 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
581 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
582 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
583 spin_unlock(&dq_data_lock);
584 err = ocfs2_qinfo_lock(info, freeing);
585 if (err < 0) {
586 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
587 " (type=%d, id=%u)\n", dquot->dq_type,
588 (unsigned)dquot->dq_id);
589 goto out;
590 }
591 if (freeing)
592 OCFS2_DQUOT(dquot)->dq_use_count--;
593 err = qtree_write_dquot(&info->dqi_gi, dquot);
594 if (err < 0)
595 goto out_qlock;
596 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
597 err = qtree_release_dquot(&info->dqi_gi, dquot);
598 if (info_dirty(sb_dqinfo(sb, type))) {
599 err2 = __ocfs2_global_write_info(sb, type);
600 if (!err)
601 err = err2;
602 }
603 }
604out_qlock:
605 ocfs2_qinfo_unlock(info, freeing);
606out:
607 if (err < 0)
608 mlog_errno(err);
609 return err;
610}
611
171bf93c
MF
612/*
613 * Functions for periodic syncing of dquots with global file
614 */
615static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
616{
617 handle_t *handle;
618 struct super_block *sb = dquot->dq_sb;
619 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
620 struct ocfs2_super *osb = OCFS2_SB(sb);
621 int status = 0;
622
623 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
624 dquot->dq_type, type, sb->s_id);
625 if (type != dquot->dq_type)
626 goto out;
627 status = ocfs2_lock_global_qf(oinfo, 1);
628 if (status < 0)
629 goto out;
630
631 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
632 if (IS_ERR(handle)) {
633 status = PTR_ERR(handle);
634 mlog_errno(status);
635 goto out_ilock;
636 }
637 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
638 status = ocfs2_sync_dquot(dquot);
639 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
640 if (status < 0)
641 mlog_errno(status);
642 /* We have to write local structure as well... */
643 dquot_mark_dquot_dirty(dquot);
644 status = dquot_commit(dquot);
645 if (status < 0)
646 mlog_errno(status);
647 ocfs2_commit_trans(osb, handle);
648out_ilock:
649 ocfs2_unlock_global_qf(oinfo, 1);
650out:
651 mlog_exit(status);
652 return status;
653}
654
655static void qsync_work_fn(struct work_struct *work)
656{
657 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
658 struct ocfs2_mem_dqinfo,
659 dqi_sync_work.work);
660 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
661
662 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
663 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
4539f1df 664 msecs_to_jiffies(oinfo->dqi_syncms));
171bf93c
MF
665}
666
9e33d69f
JK
667/*
668 * Wrappers for generic quota functions
669 */
670
671static int ocfs2_write_dquot(struct dquot *dquot)
672{
673 handle_t *handle;
674 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
675 int status = 0;
676
677 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
678
679 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
680 if (IS_ERR(handle)) {
681 status = PTR_ERR(handle);
682 mlog_errno(status);
683 goto out;
684 }
685 status = dquot_commit(dquot);
686 ocfs2_commit_trans(osb, handle);
687out:
688 mlog_exit(status);
689 return status;
690}
691
b409d7a0 692static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
9e33d69f 693{
b409d7a0 694 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0584974a
JK
695 /*
696 * We modify tree, leaf block, global info, local chunk header,
697 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
698 * accounts for inode update
699 */
b409d7a0
JK
700 return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
701 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
0584974a 702 OCFS2_QINFO_WRITE_CREDITS +
0584974a 703 OCFS2_INODE_UPDATE_CREDITS;
9e33d69f
JK
704}
705
706static int ocfs2_release_dquot(struct dquot *dquot)
707{
708 handle_t *handle;
709 struct ocfs2_mem_dqinfo *oinfo =
710 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
711 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
712 int status = 0;
713
714 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
715
716 status = ocfs2_lock_global_qf(oinfo, 1);
717 if (status < 0)
718 goto out;
719 handle = ocfs2_start_trans(osb,
720 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
721 if (IS_ERR(handle)) {
722 status = PTR_ERR(handle);
723 mlog_errno(status);
724 goto out_ilock;
725 }
726 status = dquot_release(dquot);
727 ocfs2_commit_trans(osb, handle);
728out_ilock:
729 ocfs2_unlock_global_qf(oinfo, 1);
730out:
731 mlog_exit(status);
732 return status;
733}
734
9e33d69f
JK
735static int ocfs2_acquire_dquot(struct dquot *dquot)
736{
9e33d69f
JK
737 struct ocfs2_mem_dqinfo *oinfo =
738 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
9e33d69f
JK
739 int status = 0;
740
741 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
742 /* We need an exclusive lock, because we're going to update use count
743 * and instantiate possibly new dquot structure */
744 status = ocfs2_lock_global_qf(oinfo, 1);
745 if (status < 0)
746 goto out;
9e33d69f 747 status = dquot_acquire(dquot);
9e33d69f
JK
748 ocfs2_unlock_global_qf(oinfo, 1);
749out:
750 mlog_exit(status);
751 return status;
752}
753
754static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
755{
756 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
757 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
758 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
759 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
760 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
761 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
762 int sync = 0;
763 int status;
764 struct super_block *sb = dquot->dq_sb;
765 int type = dquot->dq_type;
766 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
767 handle_t *handle;
768 struct ocfs2_super *osb = OCFS2_SB(sb);
769
770 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
771 dquot_mark_dquot_dirty(dquot);
772
773 /* In case user set some limits, sync dquot immediately to global
774 * quota file so that information propagates quicker */
775 spin_lock(&dq_data_lock);
776 if (dquot->dq_flags & mask)
777 sync = 1;
778 spin_unlock(&dq_data_lock);
f8afead7
JK
779 /* This is a slight hack but we can't afford getting global quota
780 * lock if we already have a transaction started. */
781 if (!sync || journal_current_handle()) {
9e33d69f
JK
782 status = ocfs2_write_dquot(dquot);
783 goto out;
784 }
785 status = ocfs2_lock_global_qf(oinfo, 1);
786 if (status < 0)
787 goto out;
788 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
789 if (IS_ERR(handle)) {
790 status = PTR_ERR(handle);
791 mlog_errno(status);
792 goto out_ilock;
793 }
794 status = ocfs2_sync_dquot(dquot);
795 if (status < 0) {
796 mlog_errno(status);
797 goto out_trans;
798 }
799 /* Now write updated local dquot structure */
800 status = dquot_commit(dquot);
801out_trans:
802 ocfs2_commit_trans(osb, handle);
803out_ilock:
804 ocfs2_unlock_global_qf(oinfo, 1);
805out:
806 mlog_exit(status);
807 return status;
808}
809
810/* This should happen only after set_dqinfo(). */
811static int ocfs2_write_info(struct super_block *sb, int type)
812{
813 handle_t *handle;
814 int status = 0;
815 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
816
817 mlog_entry_void();
818
819 status = ocfs2_lock_global_qf(oinfo, 1);
820 if (status < 0)
821 goto out;
822 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
823 if (IS_ERR(handle)) {
824 status = PTR_ERR(handle);
825 mlog_errno(status);
826 goto out_ilock;
827 }
828 status = dquot_commit_info(sb, type);
829 ocfs2_commit_trans(OCFS2_SB(sb), handle);
830out_ilock:
831 ocfs2_unlock_global_qf(oinfo, 1);
832out:
833 mlog_exit(status);
834 return status;
835}
836
9e33d69f
JK
837static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
838{
839 struct ocfs2_dquot *dquot =
840 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
841
842 if (!dquot)
843 return NULL;
844 return &dquot->dq_dquot;
845}
846
847static void ocfs2_destroy_dquot(struct dquot *dquot)
848{
849 kmem_cache_free(ocfs2_dquot_cachep, dquot);
850}
851
61e225dc 852const struct dquot_operations ocfs2_quota_operations = {
9e33d69f
JK
853 .write_dquot = ocfs2_write_dquot,
854 .acquire_dquot = ocfs2_acquire_dquot,
855 .release_dquot = ocfs2_release_dquot,
856 .mark_dirty = ocfs2_mark_dquot_dirty,
857 .write_info = ocfs2_write_info,
858 .alloc_dquot = ocfs2_alloc_dquot,
859 .destroy_dquot = ocfs2_destroy_dquot,
860};
171bf93c
MF
861
862int ocfs2_quota_setup(void)
863{
864 ocfs2_quota_wq = create_workqueue("o2quot");
865 if (!ocfs2_quota_wq)
866 return -ENOMEM;
867 return 0;
868}
869
870void ocfs2_quota_shutdown(void)
871{
872 if (ocfs2_quota_wq) {
873 flush_workqueue(ocfs2_quota_wq);
874 destroy_workqueue(ocfs2_quota_wq);
875 ocfs2_quota_wq = NULL;
876 }
877}