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>
27 static struct workqueue_struct *ocfs2_quota_wq = NULL;
29 static void qsync_work_fn(struct work_struct *work);
31 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
33 struct ocfs2_global_disk_dqblk *d = dp;
34 struct mem_dqblk *m = &dquot->dq_dqb;
36 /* Update from disk only entries not set by the admin */
37 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
38 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
39 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
41 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
42 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
43 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
44 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
45 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
47 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
48 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
49 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
50 m->dqb_btime = le64_to_cpu(d->dqb_btime);
51 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
52 m->dqb_itime = le64_to_cpu(d->dqb_itime);
53 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
56 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
58 struct ocfs2_global_disk_dqblk *d = dp;
59 struct mem_dqblk *m = &dquot->dq_dqb;
61 d->dqb_id = cpu_to_le32(dquot->dq_id);
62 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
63 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
64 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
65 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
66 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
67 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
68 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
69 d->dqb_btime = cpu_to_le64(m->dqb_btime);
70 d->dqb_itime = cpu_to_le64(m->dqb_itime);
73 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
75 struct ocfs2_global_disk_dqblk *d = dp;
76 struct ocfs2_mem_dqinfo *oinfo =
77 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
79 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
81 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
84 struct qtree_fmt_operations ocfs2_global_ops = {
85 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
86 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
87 .is_id = ocfs2_global_is_id,
90 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
91 struct buffer_head **bh)
94 struct buffer_head *tmp = *bh;
96 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0, NULL);
100 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
107 static int ocfs2_get_quota_block(struct inode *inode, int block,
108 struct buffer_head **bh)
113 down_read(&OCFS2_I(inode)->ip_alloc_sem);
114 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
115 up_read(&OCFS2_I(inode)->ip_alloc_sem);
120 *bh = sb_getblk(inode->i_sb, pblock);
128 /* Read data from global quotafile - avoid pagecache and such because we cannot
129 * afford acquiring the locks... We use quota cluster lock to serialize
130 * operations. Caller is responsible for acquiring it. */
131 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
132 size_t len, loff_t off)
134 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
135 struct inode *gqinode = oinfo->dqi_gqinode;
136 loff_t i_size = i_size_read(gqinode);
137 int offset = off & (sb->s_blocksize - 1);
138 sector_t blk = off >> sb->s_blocksize_bits;
140 struct buffer_head *bh;
141 size_t toread, tocopy;
145 if (off + len > i_size)
149 tocopy = min((size_t)(sb->s_blocksize - offset), toread);
151 err = ocfs2_read_quota_block(gqinode, blk, &bh);
156 memcpy(data, bh->b_data + offset, tocopy);
166 /* Write to quotafile (we know the transaction is already started and has
168 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
169 const char *data, size_t len, loff_t off)
171 struct mem_dqinfo *info = sb_dqinfo(sb, type);
172 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
173 struct inode *gqinode = oinfo->dqi_gqinode;
174 int offset = off & (sb->s_blocksize - 1);
175 sector_t blk = off >> sb->s_blocksize_bits;
176 int err = 0, new = 0, ja_type;
177 struct buffer_head *bh = NULL;
178 handle_t *handle = journal_current_handle();
181 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
182 "because transaction was not started.\n",
183 (unsigned long long)off, (unsigned long long)len);
186 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
188 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
191 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
192 if (gqinode->i_size < off + len) {
193 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
194 err = ocfs2_extend_no_holes(gqinode, off + len, off);
195 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
198 err = ocfs2_simple_size_update(gqinode,
205 /* Not rewriting whole block? */
206 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
208 err = ocfs2_read_quota_block(gqinode, blk, &bh);
209 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
211 err = ocfs2_get_quota_block(gqinode, blk, &bh);
212 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
220 memset(bh->b_data, 0, sb->s_blocksize);
221 memcpy(bh->b_data + offset, data, len);
222 flush_dcache_page(bh->b_page);
223 set_buffer_uptodate(bh);
225 ocfs2_set_buffer_uptodate(gqinode, bh);
226 err = ocfs2_journal_access(handle, gqinode, bh, ja_type);
231 err = ocfs2_journal_dirty(handle, bh);
237 mutex_unlock(&gqinode->i_mutex);
241 gqinode->i_version++;
242 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
243 mutex_unlock(&gqinode->i_mutex);
247 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
250 struct buffer_head *bh = NULL;
252 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
255 spin_lock(&dq_data_lock);
256 if (!oinfo->dqi_gqi_count++)
257 oinfo->dqi_gqi_bh = bh;
259 WARN_ON(bh != oinfo->dqi_gqi_bh);
260 spin_unlock(&dq_data_lock);
264 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
266 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
267 brelse(oinfo->dqi_gqi_bh);
268 spin_lock(&dq_data_lock);
269 if (!--oinfo->dqi_gqi_count)
270 oinfo->dqi_gqi_bh = NULL;
271 spin_unlock(&dq_data_lock);
274 /* Read information header from global quota file */
275 int ocfs2_global_read_info(struct super_block *sb, int type)
277 struct inode *gqinode = NULL;
278 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
279 GROUP_QUOTA_SYSTEM_INODE };
280 struct ocfs2_global_disk_dqinfo dinfo;
281 struct mem_dqinfo *info = sb_dqinfo(sb, type);
282 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
287 /* Read global header */
288 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
291 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
296 oinfo->dqi_gi.dqi_sb = sb;
297 oinfo->dqi_gi.dqi_type = type;
298 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
299 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
300 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
301 oinfo->dqi_gqi_bh = NULL;
302 oinfo->dqi_gqi_count = 0;
303 oinfo->dqi_gqinode = gqinode;
304 status = ocfs2_lock_global_qf(oinfo, 0);
309 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
310 sizeof(struct ocfs2_global_disk_dqinfo),
311 OCFS2_GLOBAL_INFO_OFF);
312 ocfs2_unlock_global_qf(oinfo, 0);
313 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
314 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
321 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
322 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
323 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
324 oinfo->dqi_syncjiff = msecs_to_jiffies(oinfo->dqi_syncms);
325 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
326 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
327 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
328 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
329 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
330 OCFS2_QBLK_RESERVED_SPACE;
331 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
332 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
333 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
334 oinfo->dqi_syncjiff);
341 /* Write information to global quota file. Expects exlusive lock on quota
342 * file inode and quota info */
343 static int __ocfs2_global_write_info(struct super_block *sb, int type)
345 struct mem_dqinfo *info = sb_dqinfo(sb, type);
346 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
347 struct ocfs2_global_disk_dqinfo dinfo;
350 spin_lock(&dq_data_lock);
351 info->dqi_flags &= ~DQF_INFO_DIRTY;
352 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
353 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
354 spin_unlock(&dq_data_lock);
355 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
356 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
357 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
358 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
359 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
360 sizeof(struct ocfs2_global_disk_dqinfo),
361 OCFS2_GLOBAL_INFO_OFF);
362 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
363 mlog(ML_ERROR, "Cannot write global quota info structure\n");
371 int ocfs2_global_write_info(struct super_block *sb, int type)
374 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
376 err = ocfs2_qinfo_lock(info, 1);
379 err = __ocfs2_global_write_info(sb, type);
380 ocfs2_qinfo_unlock(info, 1);
384 /* Read in information from global quota file and acquire a reference to it.
385 * dquot_acquire() has already started the transaction and locked quota file */
386 int ocfs2_global_read_dquot(struct dquot *dquot)
388 int err, err2, ex = 0;
389 struct ocfs2_mem_dqinfo *info =
390 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
392 err = ocfs2_qinfo_lock(info, 0);
395 err = qtree_read_dquot(&info->dqi_gi, dquot);
398 OCFS2_DQUOT(dquot)->dq_use_count++;
399 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
400 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
401 if (!dquot->dq_off) { /* No real quota entry? */
402 /* Upgrade to exclusive lock for allocation */
403 err = ocfs2_qinfo_lock(info, 1);
408 err = qtree_write_dquot(&info->dqi_gi, dquot);
409 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
410 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
416 ocfs2_qinfo_unlock(info, 1);
417 ocfs2_qinfo_unlock(info, 0);
424 /* Sync local information about quota modifications with global quota file.
425 * Caller must have started the transaction and obtained exclusive lock for
426 * global quota file inode */
427 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
430 struct super_block *sb = dquot->dq_sb;
431 int type = dquot->dq_type;
432 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
433 struct ocfs2_global_disk_dqblk dqblk;
434 s64 spacechange, inodechange;
435 time_t olditime, oldbtime;
437 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
438 sizeof(struct ocfs2_global_disk_dqblk),
440 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
442 mlog(ML_ERROR, "Short read from global quota file "
449 /* Update space and inode usage. Get also other information from
450 * global quota file so that we don't overwrite any changes there.
452 spin_lock(&dq_data_lock);
453 spacechange = dquot->dq_dqb.dqb_curspace -
454 OCFS2_DQUOT(dquot)->dq_origspace;
455 inodechange = dquot->dq_dqb.dqb_curinodes -
456 OCFS2_DQUOT(dquot)->dq_originodes;
457 olditime = dquot->dq_dqb.dqb_itime;
458 oldbtime = dquot->dq_dqb.dqb_btime;
459 ocfs2_global_disk2memdqb(dquot, &dqblk);
460 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
461 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
462 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
463 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
464 dquot->dq_dqb.dqb_curspace += spacechange;
465 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
466 dquot->dq_dqb.dqb_curinodes += inodechange;
467 /* Set properly space grace time... */
468 if (dquot->dq_dqb.dqb_bsoftlimit &&
469 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
470 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
472 if (dquot->dq_dqb.dqb_btime > 0)
473 dquot->dq_dqb.dqb_btime =
474 min(dquot->dq_dqb.dqb_btime, oldbtime);
476 dquot->dq_dqb.dqb_btime = oldbtime;
479 dquot->dq_dqb.dqb_btime = 0;
480 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
482 /* Set properly inode grace time... */
483 if (dquot->dq_dqb.dqb_isoftlimit &&
484 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
485 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
487 if (dquot->dq_dqb.dqb_itime > 0)
488 dquot->dq_dqb.dqb_itime =
489 min(dquot->dq_dqb.dqb_itime, olditime);
491 dquot->dq_dqb.dqb_itime = olditime;
494 dquot->dq_dqb.dqb_itime = 0;
495 clear_bit(DQ_INODES_B, &dquot->dq_flags);
497 /* All information is properly updated, clear the flags */
498 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
499 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
500 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
501 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
502 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
503 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
504 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
505 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
506 spin_unlock(&dq_data_lock);
507 err = ocfs2_qinfo_lock(info, freeing);
509 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
510 " (type=%d, id=%u)\n", dquot->dq_type,
511 (unsigned)dquot->dq_id);
515 OCFS2_DQUOT(dquot)->dq_use_count--;
516 err = qtree_write_dquot(&info->dqi_gi, dquot);
519 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
520 err = qtree_release_dquot(&info->dqi_gi, dquot);
521 if (info_dirty(sb_dqinfo(sb, type))) {
522 err2 = __ocfs2_global_write_info(sb, type);
528 ocfs2_qinfo_unlock(info, freeing);
536 * Functions for periodic syncing of dquots with global file
538 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
541 struct super_block *sb = dquot->dq_sb;
542 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
543 struct ocfs2_super *osb = OCFS2_SB(sb);
546 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
547 dquot->dq_type, type, sb->s_id);
548 if (type != dquot->dq_type)
550 status = ocfs2_lock_global_qf(oinfo, 1);
554 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
555 if (IS_ERR(handle)) {
556 status = PTR_ERR(handle);
560 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
561 status = ocfs2_sync_dquot(dquot);
562 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
565 /* We have to write local structure as well... */
566 dquot_mark_dquot_dirty(dquot);
567 status = dquot_commit(dquot);
570 ocfs2_commit_trans(osb, handle);
572 ocfs2_unlock_global_qf(oinfo, 1);
578 static void qsync_work_fn(struct work_struct *work)
580 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
581 struct ocfs2_mem_dqinfo,
583 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
585 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
586 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
587 oinfo->dqi_syncjiff);
591 * Wrappers for generic quota functions
594 static int ocfs2_write_dquot(struct dquot *dquot)
597 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
600 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
602 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
603 if (IS_ERR(handle)) {
604 status = PTR_ERR(handle);
608 status = dquot_commit(dquot);
609 ocfs2_commit_trans(osb, handle);
615 int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
617 struct ocfs2_mem_dqinfo *oinfo;
618 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
619 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
621 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
624 oinfo = sb_dqinfo(sb, type)->dqi_priv;
625 /* We modify tree, leaf block, global info, local chunk header,
626 * global and local inode */
627 return oinfo->dqi_gi.dqi_qtree_depth + 2 + 1 +
628 2 * OCFS2_INODE_UPDATE_CREDITS;
631 static int ocfs2_release_dquot(struct dquot *dquot)
634 struct ocfs2_mem_dqinfo *oinfo =
635 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
636 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
639 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
641 status = ocfs2_lock_global_qf(oinfo, 1);
644 handle = ocfs2_start_trans(osb,
645 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
646 if (IS_ERR(handle)) {
647 status = PTR_ERR(handle);
651 status = dquot_release(dquot);
652 ocfs2_commit_trans(osb, handle);
654 ocfs2_unlock_global_qf(oinfo, 1);
660 int ocfs2_calc_qinit_credits(struct super_block *sb, int type)
662 struct ocfs2_mem_dqinfo *oinfo;
663 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
664 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
665 struct ocfs2_dinode *lfe, *gfe;
667 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
670 oinfo = sb_dqinfo(sb, type)->dqi_priv;
671 gfe = (struct ocfs2_dinode *)oinfo->dqi_gqi_bh->b_data;
672 lfe = (struct ocfs2_dinode *)oinfo->dqi_lqi_bh->b_data;
673 /* We can extend local file + global file. In local file we
674 * can modify info, chunk header block and dquot block. In
675 * global file we can modify info, tree and leaf block */
676 return ocfs2_calc_extend_credits(sb, &lfe->id2.i_list, 0) +
677 ocfs2_calc_extend_credits(sb, &gfe->id2.i_list, 0) +
678 3 + oinfo->dqi_gi.dqi_qtree_depth + 2;
681 static int ocfs2_acquire_dquot(struct dquot *dquot)
684 struct ocfs2_mem_dqinfo *oinfo =
685 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
686 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
689 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
690 /* We need an exclusive lock, because we're going to update use count
691 * and instantiate possibly new dquot structure */
692 status = ocfs2_lock_global_qf(oinfo, 1);
695 handle = ocfs2_start_trans(osb,
696 ocfs2_calc_qinit_credits(dquot->dq_sb, dquot->dq_type));
697 if (IS_ERR(handle)) {
698 status = PTR_ERR(handle);
702 status = dquot_acquire(dquot);
703 ocfs2_commit_trans(osb, handle);
705 ocfs2_unlock_global_qf(oinfo, 1);
711 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
713 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
714 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
715 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
716 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
717 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
718 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
721 struct super_block *sb = dquot->dq_sb;
722 int type = dquot->dq_type;
723 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
725 struct ocfs2_super *osb = OCFS2_SB(sb);
727 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
728 dquot_mark_dquot_dirty(dquot);
730 /* In case user set some limits, sync dquot immediately to global
731 * quota file so that information propagates quicker */
732 spin_lock(&dq_data_lock);
733 if (dquot->dq_flags & mask)
735 spin_unlock(&dq_data_lock);
737 status = ocfs2_write_dquot(dquot);
740 status = ocfs2_lock_global_qf(oinfo, 1);
743 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
744 if (IS_ERR(handle)) {
745 status = PTR_ERR(handle);
749 status = ocfs2_sync_dquot(dquot);
754 /* Now write updated local dquot structure */
755 status = dquot_commit(dquot);
757 ocfs2_commit_trans(osb, handle);
759 ocfs2_unlock_global_qf(oinfo, 1);
765 /* This should happen only after set_dqinfo(). */
766 static int ocfs2_write_info(struct super_block *sb, int type)
770 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
774 status = ocfs2_lock_global_qf(oinfo, 1);
777 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
778 if (IS_ERR(handle)) {
779 status = PTR_ERR(handle);
783 status = dquot_commit_info(sb, type);
784 ocfs2_commit_trans(OCFS2_SB(sb), handle);
786 ocfs2_unlock_global_qf(oinfo, 1);
792 /* This is difficult. We have to lock quota inode and start transaction
793 * in this function but we don't want to take the penalty of exlusive
794 * quota file lock when we are just going to use cached structures. So
795 * we just take read lock check whether we have dquot cached and if so,
796 * we don't have to take the write lock... */
797 static int ocfs2_dquot_initialize(struct inode *inode, int type)
799 handle_t *handle = NULL;
801 struct super_block *sb = inode->i_sb;
802 struct ocfs2_mem_dqinfo *oinfo;
809 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
810 if (type != -1 && cnt != type)
812 if (!sb_has_quota_active(sb, cnt))
814 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
815 status = ocfs2_lock_global_qf(oinfo, 0);
818 /* This is just a performance optimization not a reliable test.
819 * Since we hold an inode lock, noone can actually release
820 * the structure until we are finished with initialization. */
821 if (inode->i_dquot[cnt] != NODQUOT) {
822 ocfs2_unlock_global_qf(oinfo, 0);
825 /* When we have inode lock, we know that no dquot_release() can
826 * run and thus we can safely check whether we need to
827 * read+modify global file to get quota information or whether
828 * our node already has it. */
831 else if (cnt == GRPQUOTA)
835 /* Obtain exclusion from quota off... */
836 down_write(&sb_dqopt(sb)->dqptr_sem);
837 exclusive = !dquot_is_cached(sb, id, cnt);
838 up_write(&sb_dqopt(sb)->dqptr_sem);
840 status = ocfs2_lock_global_qf(oinfo, 1);
846 handle = ocfs2_start_trans(OCFS2_SB(sb),
847 ocfs2_calc_qinit_credits(sb, cnt));
848 if (IS_ERR(handle)) {
849 status = PTR_ERR(handle);
854 dquot_initialize(inode, cnt);
856 ocfs2_commit_trans(OCFS2_SB(sb), handle);
857 ocfs2_unlock_global_qf(oinfo, 1);
859 ocfs2_unlock_global_qf(oinfo, 0);
865 ocfs2_unlock_global_qf(oinfo, 1);
866 ocfs2_unlock_global_qf(oinfo, 0);
872 static int ocfs2_dquot_drop_slow(struct inode *inode)
876 int got_lock[MAXQUOTAS] = {0, 0};
878 struct super_block *sb = inode->i_sb;
879 struct ocfs2_mem_dqinfo *oinfo;
881 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
882 if (!sb_has_quota_active(sb, cnt))
884 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
885 status = ocfs2_lock_global_qf(oinfo, 1);
890 handle = ocfs2_start_trans(OCFS2_SB(sb),
891 ocfs2_calc_qinit_credits(sb, USRQUOTA) +
892 ocfs2_calc_qinit_credits(sb, GRPQUOTA));
893 if (IS_ERR(handle)) {
894 status = PTR_ERR(handle);
899 ocfs2_commit_trans(OCFS2_SB(sb), handle);
901 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
903 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
904 ocfs2_unlock_global_qf(oinfo, 1);
909 /* See the comment before ocfs2_dquot_initialize. */
910 static int ocfs2_dquot_drop(struct inode *inode)
913 struct super_block *sb = inode->i_sb;
914 struct ocfs2_mem_dqinfo *oinfo;
917 int got_lock[MAXQUOTAS] = {0, 0};
920 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
921 if (!sb_has_quota_active(sb, cnt))
923 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
924 status = ocfs2_lock_global_qf(oinfo, 0);
929 /* Lock against anyone releasing references so that when when we check
930 * we know we are not going to be last ones to release dquot */
931 down_write(&sb_dqopt(sb)->dqptr_sem);
932 /* Urgh, this is a terrible hack :( */
933 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
934 if (inode->i_dquot[cnt] != NODQUOT &&
935 atomic_read(&inode->i_dquot[cnt]->dq_count) > 1) {
941 dquot_drop_locked(inode);
942 up_write(&sb_dqopt(sb)->dqptr_sem);
944 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
946 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
947 ocfs2_unlock_global_qf(oinfo, 0);
949 /* In case we bailed out because we had to do expensive locking
952 status = ocfs2_dquot_drop_slow(inode);
957 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
959 struct ocfs2_dquot *dquot =
960 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
964 return &dquot->dq_dquot;
967 static void ocfs2_destroy_dquot(struct dquot *dquot)
969 kmem_cache_free(ocfs2_dquot_cachep, dquot);
972 struct dquot_operations ocfs2_quota_operations = {
973 .initialize = ocfs2_dquot_initialize,
974 .drop = ocfs2_dquot_drop,
975 .alloc_space = dquot_alloc_space,
976 .alloc_inode = dquot_alloc_inode,
977 .free_space = dquot_free_space,
978 .free_inode = dquot_free_inode,
979 .transfer = dquot_transfer,
980 .write_dquot = ocfs2_write_dquot,
981 .acquire_dquot = ocfs2_acquire_dquot,
982 .release_dquot = ocfs2_release_dquot,
983 .mark_dirty = ocfs2_mark_dquot_dirty,
984 .write_info = ocfs2_write_info,
985 .alloc_dquot = ocfs2_alloc_dquot,
986 .destroy_dquot = ocfs2_destroy_dquot,
989 int ocfs2_quota_setup(void)
991 ocfs2_quota_wq = create_workqueue("o2quot");
997 void ocfs2_quota_shutdown(void)
999 if (ocfs2_quota_wq) {
1000 flush_workqueue(ocfs2_quota_wq);
1001 destroy_workqueue(ocfs2_quota_wq);
1002 ocfs2_quota_wq = NULL;