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"
28 static struct workqueue_struct *ocfs2_quota_wq = NULL;
30 static void qsync_work_fn(struct work_struct *work);
32 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
34 struct ocfs2_global_disk_dqblk *d = dp;
35 struct mem_dqblk *m = &dquot->dq_dqb;
37 /* Update from disk only entries not set by the admin */
38 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
39 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
40 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
42 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
43 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
44 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
45 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
46 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
48 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
49 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
50 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
51 m->dqb_btime = le64_to_cpu(d->dqb_btime);
52 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
53 m->dqb_itime = le64_to_cpu(d->dqb_itime);
54 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
57 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
59 struct ocfs2_global_disk_dqblk *d = dp;
60 struct mem_dqblk *m = &dquot->dq_dqb;
62 d->dqb_id = cpu_to_le32(dquot->dq_id);
63 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
64 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
65 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
66 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
67 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
68 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
69 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
70 d->dqb_btime = cpu_to_le64(m->dqb_btime);
71 d->dqb_itime = cpu_to_le64(m->dqb_itime);
72 d->dqb_pad1 = d->dqb_pad2 = 0;
75 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
77 struct ocfs2_global_disk_dqblk *d = dp;
78 struct ocfs2_mem_dqinfo *oinfo =
79 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
81 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
83 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
86 struct qtree_fmt_operations ocfs2_global_ops = {
87 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
88 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
89 .is_id = ocfs2_global_is_id,
92 static int ocfs2_validate_quota_block(struct super_block *sb,
93 struct buffer_head *bh)
95 struct ocfs2_disk_dqtrailer *dqt =
96 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
98 mlog(0, "Validating quota block %llu\n",
99 (unsigned long long)bh->b_blocknr);
101 BUG_ON(!buffer_uptodate(bh));
104 * If the ecc fails, we return the error but otherwise
105 * leave the filesystem running. We know any error is
106 * local to this block.
108 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
111 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
112 struct buffer_head **bh)
115 struct buffer_head *tmp = *bh;
117 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
118 ocfs2_validate_quota_block);
122 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
129 static int ocfs2_get_quota_block(struct inode *inode, int block,
130 struct buffer_head **bh)
135 down_read(&OCFS2_I(inode)->ip_alloc_sem);
136 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
137 up_read(&OCFS2_I(inode)->ip_alloc_sem);
142 *bh = sb_getblk(inode->i_sb, pblock);
150 /* Read data from global quotafile - avoid pagecache and such because we cannot
151 * afford acquiring the locks... We use quota cluster lock to serialize
152 * operations. Caller is responsible for acquiring it. */
153 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
154 size_t len, loff_t off)
156 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
157 struct inode *gqinode = oinfo->dqi_gqinode;
158 loff_t i_size = i_size_read(gqinode);
159 int offset = off & (sb->s_blocksize - 1);
160 sector_t blk = off >> sb->s_blocksize_bits;
162 struct buffer_head *bh;
163 size_t toread, tocopy;
167 if (off + len > i_size)
171 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
173 err = ocfs2_read_quota_block(gqinode, blk, &bh);
178 memcpy(data, bh->b_data + offset, tocopy);
188 /* Write to quotafile (we know the transaction is already started and has
190 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
191 const char *data, size_t len, loff_t off)
193 struct mem_dqinfo *info = sb_dqinfo(sb, type);
194 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
195 struct inode *gqinode = oinfo->dqi_gqinode;
196 int offset = off & (sb->s_blocksize - 1);
197 sector_t blk = off >> sb->s_blocksize_bits;
198 int err = 0, new = 0, ja_type;
199 struct buffer_head *bh = NULL;
200 handle_t *handle = journal_current_handle();
203 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
204 "because transaction was not started.\n",
205 (unsigned long long)off, (unsigned long long)len);
208 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
210 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
213 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
214 if (gqinode->i_size < off + len) {
216 ocfs2_align_bytes_to_blocks(sb, off + len);
218 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
219 err = ocfs2_extend_no_holes(gqinode, rounded_end, off);
220 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
223 err = ocfs2_simple_size_update(gqinode,
230 /* Not rewriting whole block? */
231 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
233 err = ocfs2_read_quota_block(gqinode, blk, &bh);
234 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
236 err = ocfs2_get_quota_block(gqinode, blk, &bh);
237 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
245 memset(bh->b_data, 0, sb->s_blocksize);
246 memcpy(bh->b_data + offset, data, len);
247 flush_dcache_page(bh->b_page);
248 set_buffer_uptodate(bh);
250 ocfs2_set_buffer_uptodate(gqinode, bh);
251 err = ocfs2_journal_access_dq(handle, gqinode, bh, ja_type);
256 err = ocfs2_journal_dirty(handle, bh);
262 mutex_unlock(&gqinode->i_mutex);
266 gqinode->i_version++;
267 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
268 mutex_unlock(&gqinode->i_mutex);
272 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
275 struct buffer_head *bh = NULL;
277 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
280 spin_lock(&dq_data_lock);
281 if (!oinfo->dqi_gqi_count++)
282 oinfo->dqi_gqi_bh = bh;
284 WARN_ON(bh != oinfo->dqi_gqi_bh);
285 spin_unlock(&dq_data_lock);
289 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
291 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
292 brelse(oinfo->dqi_gqi_bh);
293 spin_lock(&dq_data_lock);
294 if (!--oinfo->dqi_gqi_count)
295 oinfo->dqi_gqi_bh = NULL;
296 spin_unlock(&dq_data_lock);
299 /* Read information header from global quota file */
300 int ocfs2_global_read_info(struct super_block *sb, int type)
302 struct inode *gqinode = NULL;
303 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
304 GROUP_QUOTA_SYSTEM_INODE };
305 struct ocfs2_global_disk_dqinfo dinfo;
306 struct mem_dqinfo *info = sb_dqinfo(sb, type);
307 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
312 /* Read global header */
313 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
316 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
321 oinfo->dqi_gi.dqi_sb = sb;
322 oinfo->dqi_gi.dqi_type = type;
323 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
324 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
325 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
326 oinfo->dqi_gqi_bh = NULL;
327 oinfo->dqi_gqi_count = 0;
328 oinfo->dqi_gqinode = gqinode;
329 status = ocfs2_lock_global_qf(oinfo, 0);
334 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
335 sizeof(struct ocfs2_global_disk_dqinfo),
336 OCFS2_GLOBAL_INFO_OFF);
337 ocfs2_unlock_global_qf(oinfo, 0);
338 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
339 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
346 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
347 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
348 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
349 oinfo->dqi_syncjiff = msecs_to_jiffies(oinfo->dqi_syncms);
350 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
351 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
352 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
353 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
354 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
355 OCFS2_QBLK_RESERVED_SPACE;
356 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
357 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
358 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
359 oinfo->dqi_syncjiff);
366 /* Write information to global quota file. Expects exlusive lock on quota
367 * file inode and quota info */
368 static int __ocfs2_global_write_info(struct super_block *sb, int type)
370 struct mem_dqinfo *info = sb_dqinfo(sb, type);
371 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
372 struct ocfs2_global_disk_dqinfo dinfo;
375 spin_lock(&dq_data_lock);
376 info->dqi_flags &= ~DQF_INFO_DIRTY;
377 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
378 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
379 spin_unlock(&dq_data_lock);
380 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
381 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
382 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
383 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
384 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
385 sizeof(struct ocfs2_global_disk_dqinfo),
386 OCFS2_GLOBAL_INFO_OFF);
387 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
388 mlog(ML_ERROR, "Cannot write global quota info structure\n");
396 int ocfs2_global_write_info(struct super_block *sb, int type)
399 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
401 err = ocfs2_qinfo_lock(info, 1);
404 err = __ocfs2_global_write_info(sb, type);
405 ocfs2_qinfo_unlock(info, 1);
409 /* Read in information from global quota file and acquire a reference to it.
410 * dquot_acquire() has already started the transaction and locked quota file */
411 int ocfs2_global_read_dquot(struct dquot *dquot)
413 int err, err2, ex = 0;
414 struct ocfs2_mem_dqinfo *info =
415 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
417 err = ocfs2_qinfo_lock(info, 0);
420 err = qtree_read_dquot(&info->dqi_gi, dquot);
423 OCFS2_DQUOT(dquot)->dq_use_count++;
424 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
425 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
426 if (!dquot->dq_off) { /* No real quota entry? */
427 /* Upgrade to exclusive lock for allocation */
428 ocfs2_qinfo_unlock(info, 0);
429 err = ocfs2_qinfo_lock(info, 1);
434 err = qtree_write_dquot(&info->dqi_gi, dquot);
435 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
436 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
442 ocfs2_qinfo_unlock(info, 1);
444 ocfs2_qinfo_unlock(info, 0);
451 /* Sync local information about quota modifications with global quota file.
452 * Caller must have started the transaction and obtained exclusive lock for
453 * global quota file inode */
454 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
457 struct super_block *sb = dquot->dq_sb;
458 int type = dquot->dq_type;
459 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
460 struct ocfs2_global_disk_dqblk dqblk;
461 s64 spacechange, inodechange;
462 time_t olditime, oldbtime;
464 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
465 sizeof(struct ocfs2_global_disk_dqblk),
467 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
469 mlog(ML_ERROR, "Short read from global quota file "
476 /* Update space and inode usage. Get also other information from
477 * global quota file so that we don't overwrite any changes there.
479 spin_lock(&dq_data_lock);
480 spacechange = dquot->dq_dqb.dqb_curspace -
481 OCFS2_DQUOT(dquot)->dq_origspace;
482 inodechange = dquot->dq_dqb.dqb_curinodes -
483 OCFS2_DQUOT(dquot)->dq_originodes;
484 olditime = dquot->dq_dqb.dqb_itime;
485 oldbtime = dquot->dq_dqb.dqb_btime;
486 ocfs2_global_disk2memdqb(dquot, &dqblk);
487 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
488 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
489 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
490 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
491 dquot->dq_dqb.dqb_curspace += spacechange;
492 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
493 dquot->dq_dqb.dqb_curinodes += inodechange;
494 /* Set properly space grace time... */
495 if (dquot->dq_dqb.dqb_bsoftlimit &&
496 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
497 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
499 if (dquot->dq_dqb.dqb_btime > 0)
500 dquot->dq_dqb.dqb_btime =
501 min(dquot->dq_dqb.dqb_btime, oldbtime);
503 dquot->dq_dqb.dqb_btime = oldbtime;
506 dquot->dq_dqb.dqb_btime = 0;
507 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
509 /* Set properly inode grace time... */
510 if (dquot->dq_dqb.dqb_isoftlimit &&
511 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
512 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
514 if (dquot->dq_dqb.dqb_itime > 0)
515 dquot->dq_dqb.dqb_itime =
516 min(dquot->dq_dqb.dqb_itime, olditime);
518 dquot->dq_dqb.dqb_itime = olditime;
521 dquot->dq_dqb.dqb_itime = 0;
522 clear_bit(DQ_INODES_B, &dquot->dq_flags);
524 /* All information is properly updated, clear the flags */
525 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
526 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
527 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
528 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
529 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
530 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
531 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
532 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
533 spin_unlock(&dq_data_lock);
534 err = ocfs2_qinfo_lock(info, freeing);
536 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
537 " (type=%d, id=%u)\n", dquot->dq_type,
538 (unsigned)dquot->dq_id);
542 OCFS2_DQUOT(dquot)->dq_use_count--;
543 err = qtree_write_dquot(&info->dqi_gi, dquot);
546 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
547 err = qtree_release_dquot(&info->dqi_gi, dquot);
548 if (info_dirty(sb_dqinfo(sb, type))) {
549 err2 = __ocfs2_global_write_info(sb, type);
555 ocfs2_qinfo_unlock(info, freeing);
563 * Functions for periodic syncing of dquots with global file
565 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
568 struct super_block *sb = dquot->dq_sb;
569 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
570 struct ocfs2_super *osb = OCFS2_SB(sb);
573 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
574 dquot->dq_type, type, sb->s_id);
575 if (type != dquot->dq_type)
577 status = ocfs2_lock_global_qf(oinfo, 1);
581 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
582 if (IS_ERR(handle)) {
583 status = PTR_ERR(handle);
587 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
588 status = ocfs2_sync_dquot(dquot);
589 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
592 /* We have to write local structure as well... */
593 dquot_mark_dquot_dirty(dquot);
594 status = dquot_commit(dquot);
597 ocfs2_commit_trans(osb, handle);
599 ocfs2_unlock_global_qf(oinfo, 1);
605 static void qsync_work_fn(struct work_struct *work)
607 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
608 struct ocfs2_mem_dqinfo,
610 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
612 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
613 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
614 oinfo->dqi_syncjiff);
618 * Wrappers for generic quota functions
621 static int ocfs2_write_dquot(struct dquot *dquot)
624 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
627 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
629 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
630 if (IS_ERR(handle)) {
631 status = PTR_ERR(handle);
635 status = dquot_commit(dquot);
636 ocfs2_commit_trans(osb, handle);
642 int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
644 struct ocfs2_mem_dqinfo *oinfo;
645 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
646 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
648 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
651 oinfo = sb_dqinfo(sb, type)->dqi_priv;
652 /* We modify tree, leaf block, global info, local chunk header,
653 * global and local inode */
654 return oinfo->dqi_gi.dqi_qtree_depth + 2 + 1 +
655 2 * OCFS2_INODE_UPDATE_CREDITS;
658 static int ocfs2_release_dquot(struct dquot *dquot)
661 struct ocfs2_mem_dqinfo *oinfo =
662 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
663 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
666 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
668 status = ocfs2_lock_global_qf(oinfo, 1);
671 handle = ocfs2_start_trans(osb,
672 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
673 if (IS_ERR(handle)) {
674 status = PTR_ERR(handle);
678 status = dquot_release(dquot);
679 ocfs2_commit_trans(osb, handle);
681 ocfs2_unlock_global_qf(oinfo, 1);
687 int ocfs2_calc_qinit_credits(struct super_block *sb, int type)
689 struct ocfs2_mem_dqinfo *oinfo;
690 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
691 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
692 struct ocfs2_dinode *lfe, *gfe;
694 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
697 oinfo = sb_dqinfo(sb, type)->dqi_priv;
698 gfe = (struct ocfs2_dinode *)oinfo->dqi_gqi_bh->b_data;
699 lfe = (struct ocfs2_dinode *)oinfo->dqi_lqi_bh->b_data;
700 /* We can extend local file + global file. In local file we
701 * can modify info, chunk header block and dquot block. In
702 * global file we can modify info, tree and leaf block */
703 return ocfs2_calc_extend_credits(sb, &lfe->id2.i_list, 0) +
704 ocfs2_calc_extend_credits(sb, &gfe->id2.i_list, 0) +
705 3 + oinfo->dqi_gi.dqi_qtree_depth + 2;
708 static int ocfs2_acquire_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);
717 /* We need an exclusive lock, because we're going to update use count
718 * and instantiate possibly new dquot structure */
719 status = ocfs2_lock_global_qf(oinfo, 1);
722 handle = ocfs2_start_trans(osb,
723 ocfs2_calc_qinit_credits(dquot->dq_sb, dquot->dq_type));
724 if (IS_ERR(handle)) {
725 status = PTR_ERR(handle);
729 status = dquot_acquire(dquot);
730 ocfs2_commit_trans(osb, handle);
732 ocfs2_unlock_global_qf(oinfo, 1);
738 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
740 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
741 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
742 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
743 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
744 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
745 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
748 struct super_block *sb = dquot->dq_sb;
749 int type = dquot->dq_type;
750 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
752 struct ocfs2_super *osb = OCFS2_SB(sb);
754 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
755 dquot_mark_dquot_dirty(dquot);
757 /* In case user set some limits, sync dquot immediately to global
758 * quota file so that information propagates quicker */
759 spin_lock(&dq_data_lock);
760 if (dquot->dq_flags & mask)
762 spin_unlock(&dq_data_lock);
763 /* This is a slight hack but we can't afford getting global quota
764 * lock if we already have a transaction started. */
765 if (!sync || journal_current_handle()) {
766 status = ocfs2_write_dquot(dquot);
769 status = ocfs2_lock_global_qf(oinfo, 1);
772 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
773 if (IS_ERR(handle)) {
774 status = PTR_ERR(handle);
778 status = ocfs2_sync_dquot(dquot);
783 /* Now write updated local dquot structure */
784 status = dquot_commit(dquot);
786 ocfs2_commit_trans(osb, handle);
788 ocfs2_unlock_global_qf(oinfo, 1);
794 /* This should happen only after set_dqinfo(). */
795 static int ocfs2_write_info(struct super_block *sb, int type)
799 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
803 status = ocfs2_lock_global_qf(oinfo, 1);
806 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
807 if (IS_ERR(handle)) {
808 status = PTR_ERR(handle);
812 status = dquot_commit_info(sb, type);
813 ocfs2_commit_trans(OCFS2_SB(sb), handle);
815 ocfs2_unlock_global_qf(oinfo, 1);
821 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
823 struct ocfs2_dquot *dquot =
824 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
828 return &dquot->dq_dquot;
831 static void ocfs2_destroy_dquot(struct dquot *dquot)
833 kmem_cache_free(ocfs2_dquot_cachep, dquot);
836 struct dquot_operations ocfs2_quota_operations = {
837 .initialize = dquot_initialize,
839 .alloc_space = dquot_alloc_space,
840 .alloc_inode = dquot_alloc_inode,
841 .free_space = dquot_free_space,
842 .free_inode = dquot_free_inode,
843 .transfer = dquot_transfer,
844 .write_dquot = ocfs2_write_dquot,
845 .acquire_dquot = ocfs2_acquire_dquot,
846 .release_dquot = ocfs2_release_dquot,
847 .mark_dirty = ocfs2_mark_dquot_dirty,
848 .write_info = ocfs2_write_info,
849 .alloc_dquot = ocfs2_alloc_dquot,
850 .destroy_dquot = ocfs2_destroy_dquot,
853 int ocfs2_quota_setup(void)
855 ocfs2_quota_wq = create_workqueue("o2quot");
861 void ocfs2_quota_shutdown(void)
863 if (ocfs2_quota_wq) {
864 flush_workqueue(ocfs2_quota_wq);
865 destroy_workqueue(ocfs2_quota_wq);
866 ocfs2_quota_wq = NULL;