2 * Implementation of operations over global quota file
4 #include <linux/spinlock.h>
6 #include <linux/slab.h>
7 #include <linux/quota.h>
8 #include <linux/quotaops.h>
9 #include <linux/dqblk_qtree.h>
10 #include <linux/jiffies.h>
11 #include <linux/writeback.h>
12 #include <linux/workqueue.h>
14 #define MLOG_MASK_PREFIX ML_QUOTA
15 #include <cluster/masklog.h>
20 #include "blockcheck.h"
30 static struct workqueue_struct *ocfs2_quota_wq = NULL;
32 static void qsync_work_fn(struct work_struct *work);
34 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
36 struct ocfs2_global_disk_dqblk *d = dp;
37 struct mem_dqblk *m = &dquot->dq_dqb;
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);
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);
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);
59 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
61 struct ocfs2_global_disk_dqblk *d = dp;
62 struct mem_dqblk *m = &dquot->dq_dqb;
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);
74 d->dqb_pad1 = d->dqb_pad2 = 0;
77 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
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;
83 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
85 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
88 struct 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,
94 static int ocfs2_validate_quota_block(struct super_block *sb,
95 struct buffer_head *bh)
97 struct ocfs2_disk_dqtrailer *dqt =
98 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
100 mlog(0, "Validating quota block %llu\n",
101 (unsigned long long)bh->b_blocknr);
103 BUG_ON(!buffer_uptodate(bh));
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.
110 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
113 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
114 struct buffer_head **bh)
117 struct buffer_head *tmp = *bh;
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));
128 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
129 ocfs2_validate_quota_block);
133 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
140 static int ocfs2_get_quota_block(struct inode *inode, int block,
141 struct buffer_head **bh)
146 down_read(&OCFS2_I(inode)->ip_alloc_sem);
147 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
148 up_read(&OCFS2_I(inode)->ip_alloc_sem);
153 *bh = sb_getblk(inode->i_sb, pblock);
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. */
164 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
165 size_t len, loff_t off)
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;
173 struct buffer_head *bh;
174 size_t toread, tocopy;
178 if (off + len > i_size)
182 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
184 err = ocfs2_read_quota_block(gqinode, blk, &bh);
189 memcpy(data, bh->b_data + offset, tocopy);
199 /* Write to quotafile (we know the transaction is already started and has
201 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
202 const char *data, size_t len, loff_t off)
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;
209 int err = 0, new = 0, ja_type;
210 struct buffer_head *bh = NULL;
211 handle_t *handle = journal_current_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);
219 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
221 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
224 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
225 if (gqinode->i_size < off + len) {
227 ocfs2_align_bytes_to_blocks(sb, off + len);
229 /* Space is already allocated in ocfs2_global_read_dquot() */
230 err = ocfs2_simple_size_update(gqinode,
237 /* Not rewriting whole block? */
238 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
240 err = ocfs2_read_quota_block(gqinode, blk, &bh);
241 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
243 err = ocfs2_get_quota_block(gqinode, blk, &bh);
244 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
252 memset(bh->b_data, 0, sb->s_blocksize);
253 memcpy(bh->b_data + offset, data, len);
254 flush_dcache_page(bh->b_page);
255 set_buffer_uptodate(bh);
257 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
258 err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
264 err = ocfs2_journal_dirty(handle, bh);
270 mutex_unlock(&gqinode->i_mutex);
274 gqinode->i_version++;
275 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
276 mutex_unlock(&gqinode->i_mutex);
280 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
283 struct buffer_head *bh = NULL;
285 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
288 spin_lock(&dq_data_lock);
289 if (!oinfo->dqi_gqi_count++)
290 oinfo->dqi_gqi_bh = bh;
292 WARN_ON(bh != oinfo->dqi_gqi_bh);
293 spin_unlock(&dq_data_lock);
297 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
299 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
300 brelse(oinfo->dqi_gqi_bh);
301 spin_lock(&dq_data_lock);
302 if (!--oinfo->dqi_gqi_count)
303 oinfo->dqi_gqi_bh = NULL;
304 spin_unlock(&dq_data_lock);
307 /* Read information header from global quota file */
308 int ocfs2_global_read_info(struct super_block *sb, int type)
310 struct inode *gqinode = NULL;
311 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
312 GROUP_QUOTA_SYSTEM_INODE };
313 struct ocfs2_global_disk_dqinfo dinfo;
314 struct mem_dqinfo *info = sb_dqinfo(sb, type);
315 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
320 /* Read global header */
321 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
324 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
329 oinfo->dqi_gi.dqi_sb = sb;
330 oinfo->dqi_gi.dqi_type = type;
331 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
332 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
333 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
334 oinfo->dqi_gqi_bh = NULL;
335 oinfo->dqi_gqi_count = 0;
336 oinfo->dqi_gqinode = gqinode;
337 status = ocfs2_lock_global_qf(oinfo, 0);
342 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
343 sizeof(struct ocfs2_global_disk_dqinfo),
344 OCFS2_GLOBAL_INFO_OFF);
345 ocfs2_unlock_global_qf(oinfo, 0);
346 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
347 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
354 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
355 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
356 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
357 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
358 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
359 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
360 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
361 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
362 OCFS2_QBLK_RESERVED_SPACE;
363 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
364 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
365 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
366 msecs_to_jiffies(oinfo->dqi_syncms));
373 /* Write information to global quota file. Expects exlusive lock on quota
374 * file inode and quota info */
375 static int __ocfs2_global_write_info(struct super_block *sb, int type)
377 struct mem_dqinfo *info = sb_dqinfo(sb, type);
378 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
379 struct ocfs2_global_disk_dqinfo dinfo;
382 spin_lock(&dq_data_lock);
383 info->dqi_flags &= ~DQF_INFO_DIRTY;
384 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
385 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
386 spin_unlock(&dq_data_lock);
387 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
388 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
389 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
390 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
391 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
392 sizeof(struct ocfs2_global_disk_dqinfo),
393 OCFS2_GLOBAL_INFO_OFF);
394 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
395 mlog(ML_ERROR, "Cannot write global quota info structure\n");
403 int ocfs2_global_write_info(struct super_block *sb, int type)
406 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
408 err = ocfs2_qinfo_lock(info, 1);
411 err = __ocfs2_global_write_info(sb, type);
412 ocfs2_qinfo_unlock(info, 1);
416 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
418 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
421 * We may need to allocate tree blocks and a leaf block but not the
424 return oinfo->dqi_gi.dqi_qtree_depth;
427 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
429 /* We modify all the allocated blocks, tree root, and info block */
430 return (ocfs2_global_qinit_alloc(sb, type) + 2) *
431 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
434 /* Read in information from global quota file and acquire a reference to it.
435 * dquot_acquire() has already started the transaction and locked quota file */
436 int ocfs2_global_read_dquot(struct dquot *dquot)
438 int err, err2, ex = 0;
439 struct super_block *sb = dquot->dq_sb;
440 int type = dquot->dq_type;
441 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
442 struct ocfs2_super *osb = OCFS2_SB(sb);
443 struct inode *gqinode = info->dqi_gqinode;
444 int need_alloc = ocfs2_global_qinit_alloc(sb, type);
445 handle_t *handle = NULL;
447 err = ocfs2_qinfo_lock(info, 0);
450 err = qtree_read_dquot(&info->dqi_gi, dquot);
453 OCFS2_DQUOT(dquot)->dq_use_count++;
454 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
455 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
456 ocfs2_qinfo_unlock(info, 0);
458 if (!dquot->dq_off) { /* No real quota entry? */
461 * Add blocks to quota file before we start a transaction since
462 * locking allocators ranks above a transaction start
464 WARN_ON(journal_current_handle());
465 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
466 err = ocfs2_extend_no_holes(gqinode,
467 gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
469 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
474 handle = ocfs2_start_trans(osb,
475 ocfs2_calc_global_qinit_credits(sb, type));
476 if (IS_ERR(handle)) {
477 err = PTR_ERR(handle);
480 err = ocfs2_qinfo_lock(info, ex);
483 err = qtree_write_dquot(&info->dqi_gi, dquot);
484 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
485 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
491 ocfs2_qinfo_unlock(info, 1);
493 ocfs2_qinfo_unlock(info, 0);
496 ocfs2_commit_trans(osb, handle);
503 /* Sync local information about quota modifications with global quota file.
504 * Caller must have started the transaction and obtained exclusive lock for
505 * global quota file inode */
506 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
509 struct super_block *sb = dquot->dq_sb;
510 int type = dquot->dq_type;
511 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
512 struct ocfs2_global_disk_dqblk dqblk;
513 s64 spacechange, inodechange;
514 time_t olditime, oldbtime;
516 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
517 sizeof(struct ocfs2_global_disk_dqblk),
519 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
521 mlog(ML_ERROR, "Short read from global quota file "
528 /* Update space and inode usage. Get also other information from
529 * global quota file so that we don't overwrite any changes there.
531 spin_lock(&dq_data_lock);
532 spacechange = dquot->dq_dqb.dqb_curspace -
533 OCFS2_DQUOT(dquot)->dq_origspace;
534 inodechange = dquot->dq_dqb.dqb_curinodes -
535 OCFS2_DQUOT(dquot)->dq_originodes;
536 olditime = dquot->dq_dqb.dqb_itime;
537 oldbtime = dquot->dq_dqb.dqb_btime;
538 ocfs2_global_disk2memdqb(dquot, &dqblk);
539 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
540 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
541 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
542 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
543 dquot->dq_dqb.dqb_curspace += spacechange;
544 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
545 dquot->dq_dqb.dqb_curinodes += inodechange;
546 /* Set properly space grace time... */
547 if (dquot->dq_dqb.dqb_bsoftlimit &&
548 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
549 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
551 if (dquot->dq_dqb.dqb_btime > 0)
552 dquot->dq_dqb.dqb_btime =
553 min(dquot->dq_dqb.dqb_btime, oldbtime);
555 dquot->dq_dqb.dqb_btime = oldbtime;
558 dquot->dq_dqb.dqb_btime = 0;
559 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
561 /* Set properly inode grace time... */
562 if (dquot->dq_dqb.dqb_isoftlimit &&
563 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
564 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
566 if (dquot->dq_dqb.dqb_itime > 0)
567 dquot->dq_dqb.dqb_itime =
568 min(dquot->dq_dqb.dqb_itime, olditime);
570 dquot->dq_dqb.dqb_itime = olditime;
573 dquot->dq_dqb.dqb_itime = 0;
574 clear_bit(DQ_INODES_B, &dquot->dq_flags);
576 /* All information is properly updated, clear the flags */
577 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
578 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
579 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
580 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
581 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
582 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
583 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
584 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
585 spin_unlock(&dq_data_lock);
586 err = ocfs2_qinfo_lock(info, freeing);
588 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
589 " (type=%d, id=%u)\n", dquot->dq_type,
590 (unsigned)dquot->dq_id);
594 OCFS2_DQUOT(dquot)->dq_use_count--;
595 err = qtree_write_dquot(&info->dqi_gi, dquot);
598 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
599 err = qtree_release_dquot(&info->dqi_gi, dquot);
600 if (info_dirty(sb_dqinfo(sb, type))) {
601 err2 = __ocfs2_global_write_info(sb, type);
607 ocfs2_qinfo_unlock(info, freeing);
615 * Functions for periodic syncing of dquots with global file
617 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
620 struct super_block *sb = dquot->dq_sb;
621 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
622 struct ocfs2_super *osb = OCFS2_SB(sb);
625 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
626 dquot->dq_type, type, sb->s_id);
627 if (type != dquot->dq_type)
629 status = ocfs2_lock_global_qf(oinfo, 1);
633 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
634 if (IS_ERR(handle)) {
635 status = PTR_ERR(handle);
639 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
640 status = ocfs2_sync_dquot(dquot);
641 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
644 /* We have to write local structure as well... */
645 dquot_mark_dquot_dirty(dquot);
646 status = dquot_commit(dquot);
649 ocfs2_commit_trans(osb, handle);
651 ocfs2_unlock_global_qf(oinfo, 1);
657 static void qsync_work_fn(struct work_struct *work)
659 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
660 struct ocfs2_mem_dqinfo,
662 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
664 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
665 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
666 msecs_to_jiffies(oinfo->dqi_syncms));
670 * Wrappers for generic quota functions
673 static int ocfs2_write_dquot(struct dquot *dquot)
676 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
679 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
681 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
682 if (IS_ERR(handle)) {
683 status = PTR_ERR(handle);
687 status = dquot_commit(dquot);
688 ocfs2_commit_trans(osb, handle);
694 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
696 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
698 * We modify tree, leaf block, global info, local chunk header,
699 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
700 * accounts for inode update
702 return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
703 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
704 OCFS2_QINFO_WRITE_CREDITS +
705 OCFS2_INODE_UPDATE_CREDITS;
708 static int ocfs2_release_dquot(struct dquot *dquot)
711 struct ocfs2_mem_dqinfo *oinfo =
712 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
713 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
716 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
718 status = ocfs2_lock_global_qf(oinfo, 1);
721 handle = ocfs2_start_trans(osb,
722 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
723 if (IS_ERR(handle)) {
724 status = PTR_ERR(handle);
728 status = dquot_release(dquot);
729 ocfs2_commit_trans(osb, handle);
731 ocfs2_unlock_global_qf(oinfo, 1);
737 static int ocfs2_acquire_dquot(struct dquot *dquot)
739 struct ocfs2_mem_dqinfo *oinfo =
740 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
743 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
744 /* We need an exclusive lock, because we're going to update use count
745 * and instantiate possibly new dquot structure */
746 status = ocfs2_lock_global_qf(oinfo, 1);
749 status = dquot_acquire(dquot);
750 ocfs2_unlock_global_qf(oinfo, 1);
756 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
758 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
759 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
760 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
761 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
762 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
763 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
766 struct super_block *sb = dquot->dq_sb;
767 int type = dquot->dq_type;
768 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
770 struct ocfs2_super *osb = OCFS2_SB(sb);
772 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
773 dquot_mark_dquot_dirty(dquot);
775 /* In case user set some limits, sync dquot immediately to global
776 * quota file so that information propagates quicker */
777 spin_lock(&dq_data_lock);
778 if (dquot->dq_flags & mask)
780 spin_unlock(&dq_data_lock);
781 /* This is a slight hack but we can't afford getting global quota
782 * lock if we already have a transaction started. */
783 if (!sync || journal_current_handle()) {
784 status = ocfs2_write_dquot(dquot);
787 status = ocfs2_lock_global_qf(oinfo, 1);
790 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
791 if (IS_ERR(handle)) {
792 status = PTR_ERR(handle);
796 status = ocfs2_sync_dquot(dquot);
801 /* Now write updated local dquot structure */
802 status = dquot_commit(dquot);
804 ocfs2_commit_trans(osb, handle);
806 ocfs2_unlock_global_qf(oinfo, 1);
812 /* This should happen only after set_dqinfo(). */
813 static int ocfs2_write_info(struct super_block *sb, int type)
817 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
821 status = ocfs2_lock_global_qf(oinfo, 1);
824 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
825 if (IS_ERR(handle)) {
826 status = PTR_ERR(handle);
830 status = dquot_commit_info(sb, type);
831 ocfs2_commit_trans(OCFS2_SB(sb), handle);
833 ocfs2_unlock_global_qf(oinfo, 1);
839 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
841 struct ocfs2_dquot *dquot =
842 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
846 return &dquot->dq_dquot;
849 static void ocfs2_destroy_dquot(struct dquot *dquot)
851 kmem_cache_free(ocfs2_dquot_cachep, dquot);
854 const struct dquot_operations ocfs2_quota_operations = {
855 .write_dquot = ocfs2_write_dquot,
856 .acquire_dquot = ocfs2_acquire_dquot,
857 .release_dquot = ocfs2_release_dquot,
858 .mark_dirty = ocfs2_mark_dquot_dirty,
859 .write_info = ocfs2_write_info,
860 .alloc_dquot = ocfs2_alloc_dquot,
861 .destroy_dquot = ocfs2_destroy_dquot,
864 int ocfs2_quota_setup(void)
866 ocfs2_quota_wq = create_workqueue("o2quot");
872 void ocfs2_quota_shutdown(void)
874 if (ocfs2_quota_wq) {
875 flush_workqueue(ocfs2_quota_wq);
876 destroy_workqueue(ocfs2_quota_wq);
877 ocfs2_quota_wq = NULL;