2 * Implementation of operations over global quota file
4 #include <linux/spinlock.h>
6 #include <linux/quota.h>
7 #include <linux/quotaops.h>
8 #include <linux/dqblk_qtree.h>
9 #include <linux/jiffies.h>
10 #include <linux/writeback.h>
11 #include <linux/workqueue.h>
13 #define MLOG_MASK_PREFIX ML_QUOTA
14 #include <cluster/masklog.h>
19 #include "blockcheck.h"
29 static struct workqueue_struct *ocfs2_quota_wq = NULL;
31 static void qsync_work_fn(struct work_struct *work);
33 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
35 struct ocfs2_global_disk_dqblk *d = dp;
36 struct mem_dqblk *m = &dquot->dq_dqb;
38 /* Update from disk only entries not set by the admin */
39 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
40 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
41 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
43 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
44 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
45 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
46 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
47 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
49 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
50 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
51 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
52 m->dqb_btime = le64_to_cpu(d->dqb_btime);
53 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
54 m->dqb_itime = le64_to_cpu(d->dqb_itime);
55 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
58 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
60 struct ocfs2_global_disk_dqblk *d = dp;
61 struct mem_dqblk *m = &dquot->dq_dqb;
63 d->dqb_id = cpu_to_le32(dquot->dq_id);
64 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
65 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
66 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
67 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
68 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
69 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
70 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
71 d->dqb_btime = cpu_to_le64(m->dqb_btime);
72 d->dqb_itime = cpu_to_le64(m->dqb_itime);
73 d->dqb_pad1 = d->dqb_pad2 = 0;
76 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
78 struct ocfs2_global_disk_dqblk *d = dp;
79 struct ocfs2_mem_dqinfo *oinfo =
80 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
82 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
84 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
87 struct qtree_fmt_operations ocfs2_global_ops = {
88 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
89 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
90 .is_id = ocfs2_global_is_id,
93 static int ocfs2_validate_quota_block(struct super_block *sb,
94 struct buffer_head *bh)
96 struct ocfs2_disk_dqtrailer *dqt =
97 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
99 mlog(0, "Validating quota block %llu\n",
100 (unsigned long long)bh->b_blocknr);
102 BUG_ON(!buffer_uptodate(bh));
105 * If the ecc fails, we return the error but otherwise
106 * leave the filesystem running. We know any error is
107 * local to this block.
109 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
112 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
113 struct buffer_head **bh)
116 struct buffer_head *tmp = *bh;
118 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
119 ocfs2_error(inode->i_sb,
120 "Quota file %llu is probably corrupted! Requested "
121 "to read block %Lu but file has size only %Lu\n",
122 (unsigned long long)OCFS2_I(inode)->ip_blkno,
123 (unsigned long long)v_block,
124 (unsigned long long)i_size_read(inode));
127 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
128 ocfs2_validate_quota_block);
132 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
139 static int ocfs2_get_quota_block(struct inode *inode, int block,
140 struct buffer_head **bh)
145 down_read(&OCFS2_I(inode)->ip_alloc_sem);
146 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
147 up_read(&OCFS2_I(inode)->ip_alloc_sem);
152 *bh = sb_getblk(inode->i_sb, pblock);
160 /* Read data from global quotafile - avoid pagecache and such because we cannot
161 * afford acquiring the locks... We use quota cluster lock to serialize
162 * operations. Caller is responsible for acquiring it. */
163 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
164 size_t len, loff_t off)
166 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
167 struct inode *gqinode = oinfo->dqi_gqinode;
168 loff_t i_size = i_size_read(gqinode);
169 int offset = off & (sb->s_blocksize - 1);
170 sector_t blk = off >> sb->s_blocksize_bits;
172 struct buffer_head *bh;
173 size_t toread, tocopy;
177 if (off + len > i_size)
181 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
183 err = ocfs2_read_quota_block(gqinode, blk, &bh);
188 memcpy(data, bh->b_data + offset, tocopy);
198 /* Write to quotafile (we know the transaction is already started and has
200 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
201 const char *data, size_t len, loff_t off)
203 struct mem_dqinfo *info = sb_dqinfo(sb, type);
204 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
205 struct inode *gqinode = oinfo->dqi_gqinode;
206 int offset = off & (sb->s_blocksize - 1);
207 sector_t blk = off >> sb->s_blocksize_bits;
208 int err = 0, new = 0, ja_type;
209 struct buffer_head *bh = NULL;
210 handle_t *handle = journal_current_handle();
213 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
214 "because transaction was not started.\n",
215 (unsigned long long)off, (unsigned long long)len);
218 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
220 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
223 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
224 if (gqinode->i_size < off + len) {
226 ocfs2_align_bytes_to_blocks(sb, off + len);
228 /* Space is already allocated in ocfs2_global_read_dquot() */
229 err = ocfs2_simple_size_update(gqinode,
236 /* Not rewriting whole block? */
237 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
239 err = ocfs2_read_quota_block(gqinode, blk, &bh);
240 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
242 err = ocfs2_get_quota_block(gqinode, blk, &bh);
243 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
251 memset(bh->b_data, 0, sb->s_blocksize);
252 memcpy(bh->b_data + offset, data, len);
253 flush_dcache_page(bh->b_page);
254 set_buffer_uptodate(bh);
256 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
257 err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
263 ocfs2_journal_dirty(handle, bh);
267 mutex_unlock(&gqinode->i_mutex);
271 gqinode->i_version++;
272 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
273 mutex_unlock(&gqinode->i_mutex);
277 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
280 struct buffer_head *bh = NULL;
282 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
285 spin_lock(&dq_data_lock);
286 if (!oinfo->dqi_gqi_count++)
287 oinfo->dqi_gqi_bh = bh;
289 WARN_ON(bh != oinfo->dqi_gqi_bh);
290 spin_unlock(&dq_data_lock);
294 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
296 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
297 brelse(oinfo->dqi_gqi_bh);
298 spin_lock(&dq_data_lock);
299 if (!--oinfo->dqi_gqi_count)
300 oinfo->dqi_gqi_bh = NULL;
301 spin_unlock(&dq_data_lock);
304 /* Read information header from global quota file */
305 int ocfs2_global_read_info(struct super_block *sb, int type)
307 struct inode *gqinode = NULL;
308 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
309 GROUP_QUOTA_SYSTEM_INODE };
310 struct ocfs2_global_disk_dqinfo dinfo;
311 struct mem_dqinfo *info = sb_dqinfo(sb, type);
312 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
317 /* Read global header */
318 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
321 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
326 oinfo->dqi_gi.dqi_sb = sb;
327 oinfo->dqi_gi.dqi_type = type;
328 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
329 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
330 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
331 oinfo->dqi_gqi_bh = NULL;
332 oinfo->dqi_gqi_count = 0;
333 oinfo->dqi_gqinode = gqinode;
334 status = ocfs2_lock_global_qf(oinfo, 0);
339 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
340 sizeof(struct ocfs2_global_disk_dqinfo),
341 OCFS2_GLOBAL_INFO_OFF);
342 ocfs2_unlock_global_qf(oinfo, 0);
343 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
344 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
351 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
352 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
353 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
354 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
355 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
356 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
357 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
358 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
359 OCFS2_QBLK_RESERVED_SPACE;
360 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
361 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
362 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
363 msecs_to_jiffies(oinfo->dqi_syncms));
370 /* Write information to global quota file. Expects exlusive lock on quota
371 * file inode and quota info */
372 static int __ocfs2_global_write_info(struct super_block *sb, int type)
374 struct mem_dqinfo *info = sb_dqinfo(sb, type);
375 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
376 struct ocfs2_global_disk_dqinfo dinfo;
379 spin_lock(&dq_data_lock);
380 info->dqi_flags &= ~DQF_INFO_DIRTY;
381 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
382 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
383 spin_unlock(&dq_data_lock);
384 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
385 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
386 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
387 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
388 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
389 sizeof(struct ocfs2_global_disk_dqinfo),
390 OCFS2_GLOBAL_INFO_OFF);
391 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
392 mlog(ML_ERROR, "Cannot write global quota info structure\n");
400 int ocfs2_global_write_info(struct super_block *sb, int type)
403 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
405 err = ocfs2_qinfo_lock(info, 1);
408 err = __ocfs2_global_write_info(sb, type);
409 ocfs2_qinfo_unlock(info, 1);
413 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
415 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
418 * We may need to allocate tree blocks and a leaf block but not the
421 return oinfo->dqi_gi.dqi_qtree_depth;
424 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
426 /* We modify all the allocated blocks, tree root, and info block */
427 return (ocfs2_global_qinit_alloc(sb, type) + 2) *
428 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
431 /* Read in information from global quota file and acquire a reference to it.
432 * dquot_acquire() has already started the transaction and locked quota file */
433 int ocfs2_global_read_dquot(struct dquot *dquot)
435 int err, err2, ex = 0;
436 struct super_block *sb = dquot->dq_sb;
437 int type = dquot->dq_type;
438 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
439 struct ocfs2_super *osb = OCFS2_SB(sb);
440 struct inode *gqinode = info->dqi_gqinode;
441 int need_alloc = ocfs2_global_qinit_alloc(sb, type);
442 handle_t *handle = NULL;
444 err = ocfs2_qinfo_lock(info, 0);
447 err = qtree_read_dquot(&info->dqi_gi, dquot);
450 OCFS2_DQUOT(dquot)->dq_use_count++;
451 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
452 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
453 ocfs2_qinfo_unlock(info, 0);
455 if (!dquot->dq_off) { /* No real quota entry? */
458 * Add blocks to quota file before we start a transaction since
459 * locking allocators ranks above a transaction start
461 WARN_ON(journal_current_handle());
462 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
463 err = ocfs2_extend_no_holes(gqinode,
464 gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
466 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
471 handle = ocfs2_start_trans(osb,
472 ocfs2_calc_global_qinit_credits(sb, type));
473 if (IS_ERR(handle)) {
474 err = PTR_ERR(handle);
477 err = ocfs2_qinfo_lock(info, ex);
480 err = qtree_write_dquot(&info->dqi_gi, dquot);
481 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
482 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
488 ocfs2_qinfo_unlock(info, 1);
490 ocfs2_qinfo_unlock(info, 0);
493 ocfs2_commit_trans(osb, handle);
500 /* Sync local information about quota modifications with global quota file.
501 * Caller must have started the transaction and obtained exclusive lock for
502 * global quota file inode */
503 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
506 struct super_block *sb = dquot->dq_sb;
507 int type = dquot->dq_type;
508 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
509 struct ocfs2_global_disk_dqblk dqblk;
510 s64 spacechange, inodechange;
511 time_t olditime, oldbtime;
513 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
514 sizeof(struct ocfs2_global_disk_dqblk),
516 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
518 mlog(ML_ERROR, "Short read from global quota file "
525 /* Update space and inode usage. Get also other information from
526 * global quota file so that we don't overwrite any changes there.
528 spin_lock(&dq_data_lock);
529 spacechange = dquot->dq_dqb.dqb_curspace -
530 OCFS2_DQUOT(dquot)->dq_origspace;
531 inodechange = dquot->dq_dqb.dqb_curinodes -
532 OCFS2_DQUOT(dquot)->dq_originodes;
533 olditime = dquot->dq_dqb.dqb_itime;
534 oldbtime = dquot->dq_dqb.dqb_btime;
535 ocfs2_global_disk2memdqb(dquot, &dqblk);
536 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
537 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
538 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
539 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
540 dquot->dq_dqb.dqb_curspace += spacechange;
541 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
542 dquot->dq_dqb.dqb_curinodes += inodechange;
543 /* Set properly space grace time... */
544 if (dquot->dq_dqb.dqb_bsoftlimit &&
545 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
546 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
548 if (dquot->dq_dqb.dqb_btime > 0)
549 dquot->dq_dqb.dqb_btime =
550 min(dquot->dq_dqb.dqb_btime, oldbtime);
552 dquot->dq_dqb.dqb_btime = oldbtime;
555 dquot->dq_dqb.dqb_btime = 0;
556 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
558 /* Set properly inode grace time... */
559 if (dquot->dq_dqb.dqb_isoftlimit &&
560 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
561 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
563 if (dquot->dq_dqb.dqb_itime > 0)
564 dquot->dq_dqb.dqb_itime =
565 min(dquot->dq_dqb.dqb_itime, olditime);
567 dquot->dq_dqb.dqb_itime = olditime;
570 dquot->dq_dqb.dqb_itime = 0;
571 clear_bit(DQ_INODES_B, &dquot->dq_flags);
573 /* All information is properly updated, clear the flags */
574 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
575 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
576 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
577 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
578 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
579 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
580 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
581 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
582 spin_unlock(&dq_data_lock);
583 err = ocfs2_qinfo_lock(info, freeing);
585 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
586 " (type=%d, id=%u)\n", dquot->dq_type,
587 (unsigned)dquot->dq_id);
591 OCFS2_DQUOT(dquot)->dq_use_count--;
592 err = qtree_write_dquot(&info->dqi_gi, dquot);
595 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
596 err = qtree_release_dquot(&info->dqi_gi, dquot);
597 if (info_dirty(sb_dqinfo(sb, type))) {
598 err2 = __ocfs2_global_write_info(sb, type);
604 ocfs2_qinfo_unlock(info, freeing);
612 * Functions for periodic syncing of dquots with global file
614 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
617 struct super_block *sb = dquot->dq_sb;
618 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
619 struct ocfs2_super *osb = OCFS2_SB(sb);
622 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
623 dquot->dq_type, type, sb->s_id);
624 if (type != dquot->dq_type)
626 status = ocfs2_lock_global_qf(oinfo, 1);
630 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
631 if (IS_ERR(handle)) {
632 status = PTR_ERR(handle);
636 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
637 status = ocfs2_sync_dquot(dquot);
638 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
641 /* We have to write local structure as well... */
642 dquot_mark_dquot_dirty(dquot);
643 status = dquot_commit(dquot);
646 ocfs2_commit_trans(osb, handle);
648 ocfs2_unlock_global_qf(oinfo, 1);
654 static void qsync_work_fn(struct work_struct *work)
656 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
657 struct ocfs2_mem_dqinfo,
659 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
661 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
662 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
663 msecs_to_jiffies(oinfo->dqi_syncms));
667 * Wrappers for generic quota functions
670 static int ocfs2_write_dquot(struct dquot *dquot)
673 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
676 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
678 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
679 if (IS_ERR(handle)) {
680 status = PTR_ERR(handle);
684 status = dquot_commit(dquot);
685 ocfs2_commit_trans(osb, handle);
691 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
693 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
695 * We modify tree, leaf block, global info, local chunk header,
696 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
697 * accounts for inode update
699 return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
700 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
701 OCFS2_QINFO_WRITE_CREDITS +
702 OCFS2_INODE_UPDATE_CREDITS;
705 static int ocfs2_release_dquot(struct dquot *dquot)
708 struct ocfs2_mem_dqinfo *oinfo =
709 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
710 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
713 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
715 status = ocfs2_lock_global_qf(oinfo, 1);
718 handle = ocfs2_start_trans(osb,
719 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
720 if (IS_ERR(handle)) {
721 status = PTR_ERR(handle);
725 status = dquot_release(dquot);
726 ocfs2_commit_trans(osb, handle);
728 ocfs2_unlock_global_qf(oinfo, 1);
734 static int ocfs2_acquire_dquot(struct dquot *dquot)
736 struct ocfs2_mem_dqinfo *oinfo =
737 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
740 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
741 /* We need an exclusive lock, because we're going to update use count
742 * and instantiate possibly new dquot structure */
743 status = ocfs2_lock_global_qf(oinfo, 1);
746 status = dquot_acquire(dquot);
747 ocfs2_unlock_global_qf(oinfo, 1);
753 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
755 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
756 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
757 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
758 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
759 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
760 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
763 struct super_block *sb = dquot->dq_sb;
764 int type = dquot->dq_type;
765 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
767 struct ocfs2_super *osb = OCFS2_SB(sb);
769 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
770 dquot_mark_dquot_dirty(dquot);
772 /* In case user set some limits, sync dquot immediately to global
773 * quota file so that information propagates quicker */
774 spin_lock(&dq_data_lock);
775 if (dquot->dq_flags & mask)
777 spin_unlock(&dq_data_lock);
778 /* This is a slight hack but we can't afford getting global quota
779 * lock if we already have a transaction started. */
780 if (!sync || journal_current_handle()) {
781 status = ocfs2_write_dquot(dquot);
784 status = ocfs2_lock_global_qf(oinfo, 1);
787 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
788 if (IS_ERR(handle)) {
789 status = PTR_ERR(handle);
793 status = ocfs2_sync_dquot(dquot);
798 /* Now write updated local dquot structure */
799 status = dquot_commit(dquot);
801 ocfs2_commit_trans(osb, handle);
803 ocfs2_unlock_global_qf(oinfo, 1);
809 /* This should happen only after set_dqinfo(). */
810 static int ocfs2_write_info(struct super_block *sb, int type)
814 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
818 status = ocfs2_lock_global_qf(oinfo, 1);
821 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
822 if (IS_ERR(handle)) {
823 status = PTR_ERR(handle);
827 status = dquot_commit_info(sb, type);
828 ocfs2_commit_trans(OCFS2_SB(sb), handle);
830 ocfs2_unlock_global_qf(oinfo, 1);
836 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
838 struct ocfs2_dquot *dquot =
839 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
843 return &dquot->dq_dquot;
846 static void ocfs2_destroy_dquot(struct dquot *dquot)
848 kmem_cache_free(ocfs2_dquot_cachep, dquot);
851 const struct dquot_operations ocfs2_quota_operations = {
852 .write_dquot = ocfs2_write_dquot,
853 .acquire_dquot = ocfs2_acquire_dquot,
854 .release_dquot = ocfs2_release_dquot,
855 .mark_dirty = ocfs2_mark_dquot_dirty,
856 .write_info = ocfs2_write_info,
857 .alloc_dquot = ocfs2_alloc_dquot,
858 .destroy_dquot = ocfs2_destroy_dquot,
861 int ocfs2_quota_setup(void)
863 ocfs2_quota_wq = create_workqueue("o2quot");
869 void ocfs2_quota_shutdown(void)
871 if (ocfs2_quota_wq) {
872 flush_workqueue(ocfs2_quota_wq);
873 destroy_workqueue(ocfs2_quota_wq);
874 ocfs2_quota_wq = NULL;