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"
28 #include "buffer_head_io.h"
31 static struct workqueue_struct *ocfs2_quota_wq = NULL;
33 static void qsync_work_fn(struct work_struct *work);
35 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
37 struct ocfs2_global_disk_dqblk *d = dp;
38 struct mem_dqblk *m = &dquot->dq_dqb;
40 /* Update from disk only entries not set by the admin */
41 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
42 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
43 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
45 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
46 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
47 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
48 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
49 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
51 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
52 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
53 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
54 m->dqb_btime = le64_to_cpu(d->dqb_btime);
55 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
56 m->dqb_itime = le64_to_cpu(d->dqb_itime);
57 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
60 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
62 struct ocfs2_global_disk_dqblk *d = dp;
63 struct mem_dqblk *m = &dquot->dq_dqb;
65 d->dqb_id = cpu_to_le32(dquot->dq_id);
66 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
67 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
68 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
69 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
70 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
71 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
72 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
73 d->dqb_btime = cpu_to_le64(m->dqb_btime);
74 d->dqb_itime = cpu_to_le64(m->dqb_itime);
75 d->dqb_pad1 = d->dqb_pad2 = 0;
78 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
80 struct ocfs2_global_disk_dqblk *d = dp;
81 struct ocfs2_mem_dqinfo *oinfo =
82 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
84 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
86 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
89 struct qtree_fmt_operations ocfs2_global_ops = {
90 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
91 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
92 .is_id = ocfs2_global_is_id,
95 static int ocfs2_validate_quota_block(struct super_block *sb,
96 struct buffer_head *bh)
98 struct ocfs2_disk_dqtrailer *dqt =
99 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
101 mlog(0, "Validating quota block %llu\n",
102 (unsigned long long)bh->b_blocknr);
104 BUG_ON(!buffer_uptodate(bh));
107 * If the ecc fails, we return the error but otherwise
108 * leave the filesystem running. We know any error is
109 * local to this block.
111 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
114 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
115 struct buffer_head **bh)
118 struct buffer_head *tmp = *bh;
120 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
121 ocfs2_error(inode->i_sb,
122 "Quota file %llu is probably corrupted! Requested "
123 "to read block %Lu but file has size only %Lu\n",
124 (unsigned long long)OCFS2_I(inode)->ip_blkno,
125 (unsigned long long)v_block,
126 (unsigned long long)i_size_read(inode));
129 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
130 ocfs2_validate_quota_block);
134 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
141 int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
142 struct buffer_head **bhp)
147 rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
148 ocfs2_validate_quota_block);
154 static int ocfs2_get_quota_block(struct inode *inode, int block,
155 struct buffer_head **bh)
160 down_read(&OCFS2_I(inode)->ip_alloc_sem);
161 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
162 up_read(&OCFS2_I(inode)->ip_alloc_sem);
167 *bh = sb_getblk(inode->i_sb, pblock);
175 /* Read data from global quotafile - avoid pagecache and such because we cannot
176 * afford acquiring the locks... We use quota cluster lock to serialize
177 * operations. Caller is responsible for acquiring it. */
178 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
179 size_t len, loff_t off)
181 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
182 struct inode *gqinode = oinfo->dqi_gqinode;
183 loff_t i_size = i_size_read(gqinode);
184 int offset = off & (sb->s_blocksize - 1);
185 sector_t blk = off >> sb->s_blocksize_bits;
187 struct buffer_head *bh;
188 size_t toread, tocopy;
192 if (off + len > i_size)
196 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
198 err = ocfs2_read_quota_block(gqinode, blk, &bh);
203 memcpy(data, bh->b_data + offset, tocopy);
213 /* Write to quotafile (we know the transaction is already started and has
215 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
216 const char *data, size_t len, loff_t off)
218 struct mem_dqinfo *info = sb_dqinfo(sb, type);
219 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
220 struct inode *gqinode = oinfo->dqi_gqinode;
221 int offset = off & (sb->s_blocksize - 1);
222 sector_t blk = off >> sb->s_blocksize_bits;
223 int err = 0, new = 0, ja_type;
224 struct buffer_head *bh = NULL;
225 handle_t *handle = journal_current_handle();
228 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
229 "because transaction was not started.\n",
230 (unsigned long long)off, (unsigned long long)len);
233 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
235 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
238 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
239 if (gqinode->i_size < off + len) {
241 ocfs2_align_bytes_to_blocks(sb, off + len);
243 /* Space is already allocated in ocfs2_global_read_dquot() */
244 err = ocfs2_simple_size_update(gqinode,
251 /* Not rewriting whole block? */
252 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
254 err = ocfs2_read_quota_block(gqinode, blk, &bh);
255 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
257 err = ocfs2_get_quota_block(gqinode, blk, &bh);
258 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
266 memset(bh->b_data, 0, sb->s_blocksize);
267 memcpy(bh->b_data + offset, data, len);
268 flush_dcache_page(bh->b_page);
269 set_buffer_uptodate(bh);
271 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
272 err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
278 ocfs2_journal_dirty(handle, bh);
282 mutex_unlock(&gqinode->i_mutex);
286 gqinode->i_version++;
287 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
288 mutex_unlock(&gqinode->i_mutex);
292 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
295 struct buffer_head *bh = NULL;
297 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
300 spin_lock(&dq_data_lock);
301 if (!oinfo->dqi_gqi_count++)
302 oinfo->dqi_gqi_bh = bh;
304 WARN_ON(bh != oinfo->dqi_gqi_bh);
305 spin_unlock(&dq_data_lock);
309 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
311 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
312 brelse(oinfo->dqi_gqi_bh);
313 spin_lock(&dq_data_lock);
314 if (!--oinfo->dqi_gqi_count)
315 oinfo->dqi_gqi_bh = NULL;
316 spin_unlock(&dq_data_lock);
319 /* Read information header from global quota file */
320 int ocfs2_global_read_info(struct super_block *sb, int type)
322 struct inode *gqinode = NULL;
323 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
324 GROUP_QUOTA_SYSTEM_INODE };
325 struct ocfs2_global_disk_dqinfo dinfo;
326 struct mem_dqinfo *info = sb_dqinfo(sb, type);
327 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
332 /* Read global header */
333 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
336 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
341 oinfo->dqi_gi.dqi_sb = sb;
342 oinfo->dqi_gi.dqi_type = type;
343 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
344 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
345 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
346 oinfo->dqi_gqi_bh = NULL;
347 oinfo->dqi_gqi_count = 0;
348 oinfo->dqi_gqinode = gqinode;
349 status = ocfs2_lock_global_qf(oinfo, 0);
354 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
355 sizeof(struct ocfs2_global_disk_dqinfo),
356 OCFS2_GLOBAL_INFO_OFF);
357 ocfs2_unlock_global_qf(oinfo, 0);
358 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
359 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
366 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
367 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
368 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
369 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
370 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
371 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
372 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
373 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
374 OCFS2_QBLK_RESERVED_SPACE;
375 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
376 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
377 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
378 msecs_to_jiffies(oinfo->dqi_syncms));
385 /* Write information to global quota file. Expects exlusive lock on quota
386 * file inode and quota info */
387 static int __ocfs2_global_write_info(struct super_block *sb, int type)
389 struct mem_dqinfo *info = sb_dqinfo(sb, type);
390 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
391 struct ocfs2_global_disk_dqinfo dinfo;
394 spin_lock(&dq_data_lock);
395 info->dqi_flags &= ~DQF_INFO_DIRTY;
396 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
397 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
398 spin_unlock(&dq_data_lock);
399 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
400 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
401 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
402 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
403 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
404 sizeof(struct ocfs2_global_disk_dqinfo),
405 OCFS2_GLOBAL_INFO_OFF);
406 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
407 mlog(ML_ERROR, "Cannot write global quota info structure\n");
415 int ocfs2_global_write_info(struct super_block *sb, int type)
418 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
420 err = ocfs2_qinfo_lock(info, 1);
423 err = __ocfs2_global_write_info(sb, type);
424 ocfs2_qinfo_unlock(info, 1);
428 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
430 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
433 * We may need to allocate tree blocks and a leaf block but not the
436 return oinfo->dqi_gi.dqi_qtree_depth;
439 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
441 /* We modify all the allocated blocks, tree root, and info block */
442 return (ocfs2_global_qinit_alloc(sb, type) + 2) *
443 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
446 /* Read in information from global quota file and acquire a reference to it.
447 * dquot_acquire() has already started the transaction and locked quota file */
448 int ocfs2_global_read_dquot(struct dquot *dquot)
450 int err, err2, ex = 0;
451 struct super_block *sb = dquot->dq_sb;
452 int type = dquot->dq_type;
453 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
454 struct ocfs2_super *osb = OCFS2_SB(sb);
455 struct inode *gqinode = info->dqi_gqinode;
456 int need_alloc = ocfs2_global_qinit_alloc(sb, type);
457 handle_t *handle = NULL;
459 err = ocfs2_qinfo_lock(info, 0);
462 err = qtree_read_dquot(&info->dqi_gi, dquot);
465 OCFS2_DQUOT(dquot)->dq_use_count++;
466 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
467 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
468 ocfs2_qinfo_unlock(info, 0);
470 if (!dquot->dq_off) { /* No real quota entry? */
473 * Add blocks to quota file before we start a transaction since
474 * locking allocators ranks above a transaction start
476 WARN_ON(journal_current_handle());
477 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
478 err = ocfs2_extend_no_holes(gqinode,
479 gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
481 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
486 handle = ocfs2_start_trans(osb,
487 ocfs2_calc_global_qinit_credits(sb, type));
488 if (IS_ERR(handle)) {
489 err = PTR_ERR(handle);
492 err = ocfs2_qinfo_lock(info, ex);
495 err = qtree_write_dquot(&info->dqi_gi, dquot);
496 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
497 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
503 ocfs2_qinfo_unlock(info, 1);
505 ocfs2_qinfo_unlock(info, 0);
508 ocfs2_commit_trans(osb, handle);
515 /* Sync local information about quota modifications with global quota file.
516 * Caller must have started the transaction and obtained exclusive lock for
517 * global quota file inode */
518 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
521 struct super_block *sb = dquot->dq_sb;
522 int type = dquot->dq_type;
523 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
524 struct ocfs2_global_disk_dqblk dqblk;
525 s64 spacechange, inodechange;
526 time_t olditime, oldbtime;
528 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
529 sizeof(struct ocfs2_global_disk_dqblk),
531 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
533 mlog(ML_ERROR, "Short read from global quota file "
540 /* Update space and inode usage. Get also other information from
541 * global quota file so that we don't overwrite any changes there.
543 spin_lock(&dq_data_lock);
544 spacechange = dquot->dq_dqb.dqb_curspace -
545 OCFS2_DQUOT(dquot)->dq_origspace;
546 inodechange = dquot->dq_dqb.dqb_curinodes -
547 OCFS2_DQUOT(dquot)->dq_originodes;
548 olditime = dquot->dq_dqb.dqb_itime;
549 oldbtime = dquot->dq_dqb.dqb_btime;
550 ocfs2_global_disk2memdqb(dquot, &dqblk);
551 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
552 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
553 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
554 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
555 dquot->dq_dqb.dqb_curspace += spacechange;
556 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
557 dquot->dq_dqb.dqb_curinodes += inodechange;
558 /* Set properly space grace time... */
559 if (dquot->dq_dqb.dqb_bsoftlimit &&
560 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
561 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
563 if (dquot->dq_dqb.dqb_btime > 0)
564 dquot->dq_dqb.dqb_btime =
565 min(dquot->dq_dqb.dqb_btime, oldbtime);
567 dquot->dq_dqb.dqb_btime = oldbtime;
570 dquot->dq_dqb.dqb_btime = 0;
571 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
573 /* Set properly inode grace time... */
574 if (dquot->dq_dqb.dqb_isoftlimit &&
575 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
576 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
578 if (dquot->dq_dqb.dqb_itime > 0)
579 dquot->dq_dqb.dqb_itime =
580 min(dquot->dq_dqb.dqb_itime, olditime);
582 dquot->dq_dqb.dqb_itime = olditime;
585 dquot->dq_dqb.dqb_itime = 0;
586 clear_bit(DQ_INODES_B, &dquot->dq_flags);
588 /* All information is properly updated, clear the flags */
589 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
590 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
591 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
592 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
593 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
594 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
595 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
596 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
597 spin_unlock(&dq_data_lock);
598 err = ocfs2_qinfo_lock(info, freeing);
600 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
601 " (type=%d, id=%u)\n", dquot->dq_type,
602 (unsigned)dquot->dq_id);
606 OCFS2_DQUOT(dquot)->dq_use_count--;
607 err = qtree_write_dquot(&info->dqi_gi, dquot);
610 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
611 err = qtree_release_dquot(&info->dqi_gi, dquot);
612 if (info_dirty(sb_dqinfo(sb, type))) {
613 err2 = __ocfs2_global_write_info(sb, type);
619 ocfs2_qinfo_unlock(info, freeing);
627 * Functions for periodic syncing of dquots with global file
629 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
632 struct super_block *sb = dquot->dq_sb;
633 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
634 struct ocfs2_super *osb = OCFS2_SB(sb);
637 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
638 dquot->dq_type, type, sb->s_id);
639 if (type != dquot->dq_type)
641 status = ocfs2_lock_global_qf(oinfo, 1);
645 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
646 if (IS_ERR(handle)) {
647 status = PTR_ERR(handle);
651 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
652 status = ocfs2_sync_dquot(dquot);
653 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
656 /* We have to write local structure as well... */
657 dquot_mark_dquot_dirty(dquot);
658 status = dquot_commit(dquot);
661 ocfs2_commit_trans(osb, handle);
663 ocfs2_unlock_global_qf(oinfo, 1);
669 static void qsync_work_fn(struct work_struct *work)
671 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
672 struct ocfs2_mem_dqinfo,
674 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
676 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
677 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
678 msecs_to_jiffies(oinfo->dqi_syncms));
682 * Wrappers for generic quota functions
685 static int ocfs2_write_dquot(struct dquot *dquot)
688 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
691 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
693 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
694 if (IS_ERR(handle)) {
695 status = PTR_ERR(handle);
699 status = dquot_commit(dquot);
700 ocfs2_commit_trans(osb, handle);
706 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
708 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
710 * We modify tree, leaf block, global info, local chunk header,
711 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
712 * accounts for inode update
714 return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
715 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
716 OCFS2_QINFO_WRITE_CREDITS +
717 OCFS2_INODE_UPDATE_CREDITS;
720 static int ocfs2_release_dquot(struct dquot *dquot)
723 struct ocfs2_mem_dqinfo *oinfo =
724 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
725 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
728 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
730 status = ocfs2_lock_global_qf(oinfo, 1);
733 handle = ocfs2_start_trans(osb,
734 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
735 if (IS_ERR(handle)) {
736 status = PTR_ERR(handle);
740 status = dquot_release(dquot);
741 ocfs2_commit_trans(osb, handle);
743 ocfs2_unlock_global_qf(oinfo, 1);
749 static int ocfs2_acquire_dquot(struct dquot *dquot)
751 struct ocfs2_mem_dqinfo *oinfo =
752 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
755 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
756 /* We need an exclusive lock, because we're going to update use count
757 * and instantiate possibly new dquot structure */
758 status = ocfs2_lock_global_qf(oinfo, 1);
761 status = dquot_acquire(dquot);
762 ocfs2_unlock_global_qf(oinfo, 1);
768 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
770 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
771 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
772 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
773 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
774 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
775 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
778 struct super_block *sb = dquot->dq_sb;
779 int type = dquot->dq_type;
780 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
782 struct ocfs2_super *osb = OCFS2_SB(sb);
784 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
785 dquot_mark_dquot_dirty(dquot);
787 /* In case user set some limits, sync dquot immediately to global
788 * quota file so that information propagates quicker */
789 spin_lock(&dq_data_lock);
790 if (dquot->dq_flags & mask)
792 spin_unlock(&dq_data_lock);
793 /* This is a slight hack but we can't afford getting global quota
794 * lock if we already have a transaction started. */
795 if (!sync || journal_current_handle()) {
796 status = ocfs2_write_dquot(dquot);
799 status = ocfs2_lock_global_qf(oinfo, 1);
802 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
803 if (IS_ERR(handle)) {
804 status = PTR_ERR(handle);
808 status = ocfs2_sync_dquot(dquot);
813 /* Now write updated local dquot structure */
814 status = dquot_commit(dquot);
816 ocfs2_commit_trans(osb, handle);
818 ocfs2_unlock_global_qf(oinfo, 1);
824 /* This should happen only after set_dqinfo(). */
825 static int ocfs2_write_info(struct super_block *sb, int type)
829 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
833 status = ocfs2_lock_global_qf(oinfo, 1);
836 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
837 if (IS_ERR(handle)) {
838 status = PTR_ERR(handle);
842 status = dquot_commit_info(sb, type);
843 ocfs2_commit_trans(OCFS2_SB(sb), handle);
845 ocfs2_unlock_global_qf(oinfo, 1);
851 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
853 struct ocfs2_dquot *dquot =
854 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
858 return &dquot->dq_dquot;
861 static void ocfs2_destroy_dquot(struct dquot *dquot)
863 kmem_cache_free(ocfs2_dquot_cachep, dquot);
866 const struct dquot_operations ocfs2_quota_operations = {
867 .write_dquot = ocfs2_write_dquot,
868 .acquire_dquot = ocfs2_acquire_dquot,
869 .release_dquot = ocfs2_release_dquot,
870 .mark_dirty = ocfs2_mark_dquot_dirty,
871 .write_info = ocfs2_write_info,
872 .alloc_dquot = ocfs2_alloc_dquot,
873 .destroy_dquot = ocfs2_destroy_dquot,
876 int ocfs2_quota_setup(void)
878 ocfs2_quota_wq = create_workqueue("o2quot");
884 void ocfs2_quota_shutdown(void)
886 if (ocfs2_quota_wq) {
887 flush_workqueue(ocfs2_quota_wq);
888 destroy_workqueue(ocfs2_quota_wq);
889 ocfs2_quota_wq = NULL;