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 struct buffer_head *ocfs2_get_quota_block(struct inode *inode,
111 struct buffer_head *bh;
113 down_read(&OCFS2_I(inode)->ip_alloc_sem);
114 *err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount,
116 up_read(&OCFS2_I(inode)->ip_alloc_sem);
121 bh = sb_getblk(inode->i_sb, pblock);
129 /* Read data from global quotafile - avoid pagecache and such because we cannot
130 * afford acquiring the locks... We use quota cluster lock to serialize
131 * operations. Caller is responsible for acquiring it. */
132 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
133 size_t len, loff_t off)
135 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
136 struct inode *gqinode = oinfo->dqi_gqinode;
137 loff_t i_size = i_size_read(gqinode);
138 int offset = off & (sb->s_blocksize - 1);
139 sector_t blk = off >> sb->s_blocksize_bits;
141 struct buffer_head *bh;
142 size_t toread, tocopy;
146 if (off + len > i_size)
150 tocopy = min((size_t)(sb->s_blocksize - offset), toread);
152 err = ocfs2_read_quota_block(gqinode, blk, &bh);
157 memcpy(data, bh->b_data + offset, tocopy);
167 /* Write to quotafile (we know the transaction is already started and has
169 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
170 const char *data, size_t len, loff_t off)
172 struct mem_dqinfo *info = sb_dqinfo(sb, type);
173 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
174 struct inode *gqinode = oinfo->dqi_gqinode;
175 int offset = off & (sb->s_blocksize - 1);
176 sector_t blk = off >> sb->s_blocksize_bits;
177 int err = 0, new = 0;
178 struct buffer_head *bh = NULL;
179 handle_t *handle = journal_current_handle();
182 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
183 "because transaction was not started.\n",
184 (unsigned long long)off, (unsigned long long)len);
187 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
189 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
192 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
193 if (gqinode->i_size < off + len) {
194 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
195 err = ocfs2_extend_no_holes(gqinode, off + len, off);
196 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
199 err = ocfs2_simple_size_update(gqinode,
206 /* Not rewriting whole block? */
207 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
209 err = ocfs2_read_quota_block(gqinode, blk, &bh);
214 err = ocfs2_journal_access(handle, gqinode, bh,
215 OCFS2_JOURNAL_ACCESS_WRITE);
217 bh = ocfs2_get_quota_block(gqinode, blk, &err);
222 err = ocfs2_journal_access(handle, gqinode, bh,
223 OCFS2_JOURNAL_ACCESS_CREATE);
231 memset(bh->b_data, 0, sb->s_blocksize);
232 memcpy(bh->b_data + offset, data, len);
233 flush_dcache_page(bh->b_page);
235 ocfs2_set_buffer_uptodate(gqinode, bh);
236 err = ocfs2_journal_dirty(handle, bh);
242 mutex_unlock(&gqinode->i_mutex);
246 gqinode->i_version++;
247 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
248 mutex_unlock(&gqinode->i_mutex);
252 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
255 struct buffer_head *bh = NULL;
257 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
260 spin_lock(&dq_data_lock);
261 if (!oinfo->dqi_gqi_count++)
262 oinfo->dqi_gqi_bh = bh;
264 WARN_ON(bh != oinfo->dqi_gqi_bh);
265 spin_unlock(&dq_data_lock);
269 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
271 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
272 brelse(oinfo->dqi_gqi_bh);
273 spin_lock(&dq_data_lock);
274 if (!--oinfo->dqi_gqi_count)
275 oinfo->dqi_gqi_bh = NULL;
276 spin_unlock(&dq_data_lock);
279 /* Read information header from global quota file */
280 int ocfs2_global_read_info(struct super_block *sb, int type)
282 struct inode *gqinode = NULL;
283 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
284 GROUP_QUOTA_SYSTEM_INODE };
285 struct ocfs2_global_disk_dqinfo dinfo;
286 struct mem_dqinfo *info = sb_dqinfo(sb, type);
287 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
292 /* Read global header */
293 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
296 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
301 oinfo->dqi_gi.dqi_sb = sb;
302 oinfo->dqi_gi.dqi_type = type;
303 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
304 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
305 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
306 oinfo->dqi_gqi_bh = NULL;
307 oinfo->dqi_gqi_count = 0;
308 oinfo->dqi_gqinode = gqinode;
309 status = ocfs2_lock_global_qf(oinfo, 0);
314 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
315 sizeof(struct ocfs2_global_disk_dqinfo),
316 OCFS2_GLOBAL_INFO_OFF);
317 ocfs2_unlock_global_qf(oinfo, 0);
318 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
319 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
326 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
327 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
328 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
329 oinfo->dqi_syncjiff = msecs_to_jiffies(oinfo->dqi_syncms);
330 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
331 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
332 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
333 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
334 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
335 OCFS2_QBLK_RESERVED_SPACE;
336 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
337 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
338 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
339 oinfo->dqi_syncjiff);
346 /* Write information to global quota file. Expects exlusive lock on quota
347 * file inode and quota info */
348 static int __ocfs2_global_write_info(struct super_block *sb, int type)
350 struct mem_dqinfo *info = sb_dqinfo(sb, type);
351 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
352 struct ocfs2_global_disk_dqinfo dinfo;
355 spin_lock(&dq_data_lock);
356 info->dqi_flags &= ~DQF_INFO_DIRTY;
357 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
358 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
359 spin_unlock(&dq_data_lock);
360 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
361 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
362 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
363 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
364 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
365 sizeof(struct ocfs2_global_disk_dqinfo),
366 OCFS2_GLOBAL_INFO_OFF);
367 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
368 mlog(ML_ERROR, "Cannot write global quota info structure\n");
376 int ocfs2_global_write_info(struct super_block *sb, int type)
379 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
381 err = ocfs2_qinfo_lock(info, 1);
384 err = __ocfs2_global_write_info(sb, type);
385 ocfs2_qinfo_unlock(info, 1);
389 /* Read in information from global quota file and acquire a reference to it.
390 * dquot_acquire() has already started the transaction and locked quota file */
391 int ocfs2_global_read_dquot(struct dquot *dquot)
393 int err, err2, ex = 0;
394 struct ocfs2_mem_dqinfo *info =
395 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
397 err = ocfs2_qinfo_lock(info, 0);
400 err = qtree_read_dquot(&info->dqi_gi, dquot);
403 OCFS2_DQUOT(dquot)->dq_use_count++;
404 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
405 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
406 if (!dquot->dq_off) { /* No real quota entry? */
407 /* Upgrade to exclusive lock for allocation */
408 err = ocfs2_qinfo_lock(info, 1);
413 err = qtree_write_dquot(&info->dqi_gi, dquot);
414 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
415 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
421 ocfs2_qinfo_unlock(info, 1);
422 ocfs2_qinfo_unlock(info, 0);
429 /* Sync local information about quota modifications with global quota file.
430 * Caller must have started the transaction and obtained exclusive lock for
431 * global quota file inode */
432 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
435 struct super_block *sb = dquot->dq_sb;
436 int type = dquot->dq_type;
437 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
438 struct ocfs2_global_disk_dqblk dqblk;
439 s64 spacechange, inodechange;
440 time_t olditime, oldbtime;
442 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
443 sizeof(struct ocfs2_global_disk_dqblk),
445 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
447 mlog(ML_ERROR, "Short read from global quota file "
454 /* Update space and inode usage. Get also other information from
455 * global quota file so that we don't overwrite any changes there.
457 spin_lock(&dq_data_lock);
458 spacechange = dquot->dq_dqb.dqb_curspace -
459 OCFS2_DQUOT(dquot)->dq_origspace;
460 inodechange = dquot->dq_dqb.dqb_curinodes -
461 OCFS2_DQUOT(dquot)->dq_originodes;
462 olditime = dquot->dq_dqb.dqb_itime;
463 oldbtime = dquot->dq_dqb.dqb_btime;
464 ocfs2_global_disk2memdqb(dquot, &dqblk);
465 mlog(0, "Syncing global dquot %d space %lld+%lld, inodes %lld+%lld\n",
466 dquot->dq_id, dquot->dq_dqb.dqb_curspace, spacechange,
467 dquot->dq_dqb.dqb_curinodes, inodechange);
468 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
469 dquot->dq_dqb.dqb_curspace += spacechange;
470 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
471 dquot->dq_dqb.dqb_curinodes += inodechange;
472 /* Set properly space grace time... */
473 if (dquot->dq_dqb.dqb_bsoftlimit &&
474 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
475 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
477 if (dquot->dq_dqb.dqb_btime > 0)
478 dquot->dq_dqb.dqb_btime =
479 min(dquot->dq_dqb.dqb_btime, oldbtime);
481 dquot->dq_dqb.dqb_btime = oldbtime;
484 dquot->dq_dqb.dqb_btime = 0;
485 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
487 /* Set properly inode grace time... */
488 if (dquot->dq_dqb.dqb_isoftlimit &&
489 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
490 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
492 if (dquot->dq_dqb.dqb_itime > 0)
493 dquot->dq_dqb.dqb_itime =
494 min(dquot->dq_dqb.dqb_itime, olditime);
496 dquot->dq_dqb.dqb_itime = olditime;
499 dquot->dq_dqb.dqb_itime = 0;
500 clear_bit(DQ_INODES_B, &dquot->dq_flags);
502 /* All information is properly updated, clear the flags */
503 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
504 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
505 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
506 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
507 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
508 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
509 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
510 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
511 spin_unlock(&dq_data_lock);
512 err = ocfs2_qinfo_lock(info, freeing);
514 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
515 " (type=%d, id=%u)\n", dquot->dq_type,
516 (unsigned)dquot->dq_id);
520 OCFS2_DQUOT(dquot)->dq_use_count--;
521 err = qtree_write_dquot(&info->dqi_gi, dquot);
524 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
525 err = qtree_release_dquot(&info->dqi_gi, dquot);
526 if (info_dirty(sb_dqinfo(sb, type))) {
527 err2 = __ocfs2_global_write_info(sb, type);
533 ocfs2_qinfo_unlock(info, freeing);
541 * Functions for periodic syncing of dquots with global file
543 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
546 struct super_block *sb = dquot->dq_sb;
547 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
548 struct ocfs2_super *osb = OCFS2_SB(sb);
551 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
552 dquot->dq_type, type, sb->s_id);
553 if (type != dquot->dq_type)
555 status = ocfs2_lock_global_qf(oinfo, 1);
559 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
560 if (IS_ERR(handle)) {
561 status = PTR_ERR(handle);
565 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
566 status = ocfs2_sync_dquot(dquot);
567 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
570 /* We have to write local structure as well... */
571 dquot_mark_dquot_dirty(dquot);
572 status = dquot_commit(dquot);
575 ocfs2_commit_trans(osb, handle);
577 ocfs2_unlock_global_qf(oinfo, 1);
583 static void qsync_work_fn(struct work_struct *work)
585 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
586 struct ocfs2_mem_dqinfo,
588 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
590 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
591 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
592 oinfo->dqi_syncjiff);
596 * Wrappers for generic quota functions
599 static int ocfs2_write_dquot(struct dquot *dquot)
602 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
605 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
607 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
608 if (IS_ERR(handle)) {
609 status = PTR_ERR(handle);
613 status = dquot_commit(dquot);
614 ocfs2_commit_trans(osb, handle);
620 int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
622 struct ocfs2_mem_dqinfo *oinfo;
623 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
624 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
626 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
629 oinfo = sb_dqinfo(sb, type)->dqi_priv;
630 /* We modify tree, leaf block, global info, local chunk header,
631 * global and local inode */
632 return oinfo->dqi_gi.dqi_qtree_depth + 2 + 1 +
633 2 * OCFS2_INODE_UPDATE_CREDITS;
636 static int ocfs2_release_dquot(struct dquot *dquot)
639 struct ocfs2_mem_dqinfo *oinfo =
640 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
641 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
644 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
646 status = ocfs2_lock_global_qf(oinfo, 1);
649 handle = ocfs2_start_trans(osb,
650 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
651 if (IS_ERR(handle)) {
652 status = PTR_ERR(handle);
656 status = dquot_release(dquot);
657 ocfs2_commit_trans(osb, handle);
659 ocfs2_unlock_global_qf(oinfo, 1);
665 int ocfs2_calc_qinit_credits(struct super_block *sb, int type)
667 struct ocfs2_mem_dqinfo *oinfo;
668 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
669 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
670 struct ocfs2_dinode *lfe, *gfe;
672 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
675 oinfo = sb_dqinfo(sb, type)->dqi_priv;
676 gfe = (struct ocfs2_dinode *)oinfo->dqi_gqi_bh->b_data;
677 lfe = (struct ocfs2_dinode *)oinfo->dqi_lqi_bh->b_data;
678 /* We can extend local file + global file. In local file we
679 * can modify info, chunk header block and dquot block. In
680 * global file we can modify info, tree and leaf block */
681 return ocfs2_calc_extend_credits(sb, &lfe->id2.i_list, 0) +
682 ocfs2_calc_extend_credits(sb, &gfe->id2.i_list, 0) +
683 3 + oinfo->dqi_gi.dqi_qtree_depth + 2;
686 static int ocfs2_acquire_dquot(struct dquot *dquot)
689 struct ocfs2_mem_dqinfo *oinfo =
690 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
691 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
694 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
695 /* We need an exclusive lock, because we're going to update use count
696 * and instantiate possibly new dquot structure */
697 status = ocfs2_lock_global_qf(oinfo, 1);
700 handle = ocfs2_start_trans(osb,
701 ocfs2_calc_qinit_credits(dquot->dq_sb, dquot->dq_type));
702 if (IS_ERR(handle)) {
703 status = PTR_ERR(handle);
707 status = dquot_acquire(dquot);
708 ocfs2_commit_trans(osb, handle);
710 ocfs2_unlock_global_qf(oinfo, 1);
716 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
718 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
719 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
720 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
721 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
722 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
723 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
726 struct super_block *sb = dquot->dq_sb;
727 int type = dquot->dq_type;
728 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
730 struct ocfs2_super *osb = OCFS2_SB(sb);
732 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
733 dquot_mark_dquot_dirty(dquot);
735 /* In case user set some limits, sync dquot immediately to global
736 * quota file so that information propagates quicker */
737 spin_lock(&dq_data_lock);
738 if (dquot->dq_flags & mask)
740 spin_unlock(&dq_data_lock);
742 status = ocfs2_write_dquot(dquot);
745 status = ocfs2_lock_global_qf(oinfo, 1);
748 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
749 if (IS_ERR(handle)) {
750 status = PTR_ERR(handle);
754 status = ocfs2_sync_dquot(dquot);
759 /* Now write updated local dquot structure */
760 status = dquot_commit(dquot);
762 ocfs2_commit_trans(osb, handle);
764 ocfs2_unlock_global_qf(oinfo, 1);
770 /* This should happen only after set_dqinfo(). */
771 static int ocfs2_write_info(struct super_block *sb, int type)
775 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
779 status = ocfs2_lock_global_qf(oinfo, 1);
782 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
783 if (IS_ERR(handle)) {
784 status = PTR_ERR(handle);
788 status = dquot_commit_info(sb, type);
789 ocfs2_commit_trans(OCFS2_SB(sb), handle);
791 ocfs2_unlock_global_qf(oinfo, 1);
797 /* This is difficult. We have to lock quota inode and start transaction
798 * in this function but we don't want to take the penalty of exlusive
799 * quota file lock when we are just going to use cached structures. So
800 * we just take read lock check whether we have dquot cached and if so,
801 * we don't have to take the write lock... */
802 static int ocfs2_dquot_initialize(struct inode *inode, int type)
804 handle_t *handle = NULL;
806 struct super_block *sb = inode->i_sb;
807 struct ocfs2_mem_dqinfo *oinfo;
814 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
815 if (type != -1 && cnt != type)
817 if (!sb_has_quota_active(sb, cnt))
819 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
820 status = ocfs2_lock_global_qf(oinfo, 0);
823 /* This is just a performance optimization not a reliable test.
824 * Since we hold an inode lock, noone can actually release
825 * the structure until we are finished with initialization. */
826 if (inode->i_dquot[cnt] != NODQUOT) {
827 ocfs2_unlock_global_qf(oinfo, 0);
830 /* When we have inode lock, we know that no dquot_release() can
831 * run and thus we can safely check whether we need to
832 * read+modify global file to get quota information or whether
833 * our node already has it. */
836 else if (cnt == GRPQUOTA)
840 /* Obtain exclusion from quota off... */
841 down_write(&sb_dqopt(sb)->dqptr_sem);
842 exclusive = !dquot_is_cached(sb, id, cnt);
843 up_write(&sb_dqopt(sb)->dqptr_sem);
845 status = ocfs2_lock_global_qf(oinfo, 1);
851 handle = ocfs2_start_trans(OCFS2_SB(sb),
852 ocfs2_calc_qinit_credits(sb, cnt));
853 if (IS_ERR(handle)) {
854 status = PTR_ERR(handle);
859 dquot_initialize(inode, cnt);
861 ocfs2_commit_trans(OCFS2_SB(sb), handle);
862 ocfs2_unlock_global_qf(oinfo, 1);
864 ocfs2_unlock_global_qf(oinfo, 0);
870 ocfs2_unlock_global_qf(oinfo, 1);
871 ocfs2_unlock_global_qf(oinfo, 0);
877 static int ocfs2_dquot_drop_slow(struct inode *inode)
881 int got_lock[MAXQUOTAS] = {0, 0};
883 struct super_block *sb = inode->i_sb;
884 struct ocfs2_mem_dqinfo *oinfo;
886 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
887 if (!sb_has_quota_active(sb, cnt))
889 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
890 status = ocfs2_lock_global_qf(oinfo, 1);
895 handle = ocfs2_start_trans(OCFS2_SB(sb),
896 ocfs2_calc_qinit_credits(sb, USRQUOTA) +
897 ocfs2_calc_qinit_credits(sb, GRPQUOTA));
898 if (IS_ERR(handle)) {
899 status = PTR_ERR(handle);
904 ocfs2_commit_trans(OCFS2_SB(sb), handle);
906 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
908 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
909 ocfs2_unlock_global_qf(oinfo, 1);
914 /* See the comment before ocfs2_dquot_initialize. */
915 static int ocfs2_dquot_drop(struct inode *inode)
918 struct super_block *sb = inode->i_sb;
919 struct ocfs2_mem_dqinfo *oinfo;
922 int got_lock[MAXQUOTAS] = {0, 0};
925 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
926 if (!sb_has_quota_active(sb, cnt))
928 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
929 status = ocfs2_lock_global_qf(oinfo, 0);
934 /* Lock against anyone releasing references so that when when we check
935 * we know we are not going to be last ones to release dquot */
936 down_write(&sb_dqopt(sb)->dqptr_sem);
937 /* Urgh, this is a terrible hack :( */
938 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
939 if (inode->i_dquot[cnt] != NODQUOT &&
940 atomic_read(&inode->i_dquot[cnt]->dq_count) > 1) {
946 dquot_drop_locked(inode);
947 up_write(&sb_dqopt(sb)->dqptr_sem);
949 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
951 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
952 ocfs2_unlock_global_qf(oinfo, 0);
954 /* In case we bailed out because we had to do expensive locking
957 status = ocfs2_dquot_drop_slow(inode);
962 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
964 struct ocfs2_dquot *dquot =
965 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
969 return &dquot->dq_dquot;
972 static void ocfs2_destroy_dquot(struct dquot *dquot)
974 kmem_cache_free(ocfs2_dquot_cachep, dquot);
977 struct dquot_operations ocfs2_quota_operations = {
978 .initialize = ocfs2_dquot_initialize,
979 .drop = ocfs2_dquot_drop,
980 .alloc_space = dquot_alloc_space,
981 .alloc_inode = dquot_alloc_inode,
982 .free_space = dquot_free_space,
983 .free_inode = dquot_free_inode,
984 .transfer = dquot_transfer,
985 .write_dquot = ocfs2_write_dquot,
986 .acquire_dquot = ocfs2_acquire_dquot,
987 .release_dquot = ocfs2_release_dquot,
988 .mark_dirty = ocfs2_mark_dquot_dirty,
989 .write_info = ocfs2_write_info,
990 .alloc_dquot = ocfs2_alloc_dquot,
991 .destroy_dquot = ocfs2_destroy_dquot,
994 int ocfs2_quota_setup(void)
996 ocfs2_quota_wq = create_workqueue("o2quot");
1002 void ocfs2_quota_shutdown(void)
1004 if (ocfs2_quota_wq) {
1005 flush_workqueue(ocfs2_quota_wq);
1006 destroy_workqueue(ocfs2_quota_wq);
1007 ocfs2_quota_wq = NULL;