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, ja_type;
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);
210 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
212 bh = ocfs2_get_quota_block(gqinode, blk, &err);
213 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
221 memset(bh->b_data, 0, sb->s_blocksize);
222 memcpy(bh->b_data + offset, data, len);
223 flush_dcache_page(bh->b_page);
224 set_buffer_uptodate(bh);
226 ocfs2_set_buffer_uptodate(gqinode, bh);
227 err = ocfs2_journal_access(handle, gqinode, bh, ja_type);
232 err = ocfs2_journal_dirty(handle, bh);
238 mutex_unlock(&gqinode->i_mutex);
242 gqinode->i_version++;
243 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
244 mutex_unlock(&gqinode->i_mutex);
248 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
251 struct buffer_head *bh = NULL;
253 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
256 spin_lock(&dq_data_lock);
257 if (!oinfo->dqi_gqi_count++)
258 oinfo->dqi_gqi_bh = bh;
260 WARN_ON(bh != oinfo->dqi_gqi_bh);
261 spin_unlock(&dq_data_lock);
265 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
267 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
268 brelse(oinfo->dqi_gqi_bh);
269 spin_lock(&dq_data_lock);
270 if (!--oinfo->dqi_gqi_count)
271 oinfo->dqi_gqi_bh = NULL;
272 spin_unlock(&dq_data_lock);
275 /* Read information header from global quota file */
276 int ocfs2_global_read_info(struct super_block *sb, int type)
278 struct inode *gqinode = NULL;
279 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
280 GROUP_QUOTA_SYSTEM_INODE };
281 struct ocfs2_global_disk_dqinfo dinfo;
282 struct mem_dqinfo *info = sb_dqinfo(sb, type);
283 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
288 /* Read global header */
289 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
292 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
297 oinfo->dqi_gi.dqi_sb = sb;
298 oinfo->dqi_gi.dqi_type = type;
299 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
300 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
301 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
302 oinfo->dqi_gqi_bh = NULL;
303 oinfo->dqi_gqi_count = 0;
304 oinfo->dqi_gqinode = gqinode;
305 status = ocfs2_lock_global_qf(oinfo, 0);
310 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
311 sizeof(struct ocfs2_global_disk_dqinfo),
312 OCFS2_GLOBAL_INFO_OFF);
313 ocfs2_unlock_global_qf(oinfo, 0);
314 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
315 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
322 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
323 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
324 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
325 oinfo->dqi_syncjiff = msecs_to_jiffies(oinfo->dqi_syncms);
326 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
327 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
328 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
329 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
330 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
331 OCFS2_QBLK_RESERVED_SPACE;
332 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
333 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
334 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
335 oinfo->dqi_syncjiff);
342 /* Write information to global quota file. Expects exlusive lock on quota
343 * file inode and quota info */
344 static int __ocfs2_global_write_info(struct super_block *sb, int type)
346 struct mem_dqinfo *info = sb_dqinfo(sb, type);
347 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
348 struct ocfs2_global_disk_dqinfo dinfo;
351 spin_lock(&dq_data_lock);
352 info->dqi_flags &= ~DQF_INFO_DIRTY;
353 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
354 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
355 spin_unlock(&dq_data_lock);
356 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
357 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
358 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
359 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
360 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
361 sizeof(struct ocfs2_global_disk_dqinfo),
362 OCFS2_GLOBAL_INFO_OFF);
363 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
364 mlog(ML_ERROR, "Cannot write global quota info structure\n");
372 int ocfs2_global_write_info(struct super_block *sb, int type)
375 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
377 err = ocfs2_qinfo_lock(info, 1);
380 err = __ocfs2_global_write_info(sb, type);
381 ocfs2_qinfo_unlock(info, 1);
385 /* Read in information from global quota file and acquire a reference to it.
386 * dquot_acquire() has already started the transaction and locked quota file */
387 int ocfs2_global_read_dquot(struct dquot *dquot)
389 int err, err2, ex = 0;
390 struct ocfs2_mem_dqinfo *info =
391 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
393 err = ocfs2_qinfo_lock(info, 0);
396 err = qtree_read_dquot(&info->dqi_gi, dquot);
399 OCFS2_DQUOT(dquot)->dq_use_count++;
400 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
401 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
402 if (!dquot->dq_off) { /* No real quota entry? */
403 /* Upgrade to exclusive lock for allocation */
404 err = ocfs2_qinfo_lock(info, 1);
409 err = qtree_write_dquot(&info->dqi_gi, dquot);
410 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
411 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
417 ocfs2_qinfo_unlock(info, 1);
418 ocfs2_qinfo_unlock(info, 0);
425 /* Sync local information about quota modifications with global quota file.
426 * Caller must have started the transaction and obtained exclusive lock for
427 * global quota file inode */
428 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
431 struct super_block *sb = dquot->dq_sb;
432 int type = dquot->dq_type;
433 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
434 struct ocfs2_global_disk_dqblk dqblk;
435 s64 spacechange, inodechange;
436 time_t olditime, oldbtime;
438 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
439 sizeof(struct ocfs2_global_disk_dqblk),
441 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
443 mlog(ML_ERROR, "Short read from global quota file "
450 /* Update space and inode usage. Get also other information from
451 * global quota file so that we don't overwrite any changes there.
453 spin_lock(&dq_data_lock);
454 spacechange = dquot->dq_dqb.dqb_curspace -
455 OCFS2_DQUOT(dquot)->dq_origspace;
456 inodechange = dquot->dq_dqb.dqb_curinodes -
457 OCFS2_DQUOT(dquot)->dq_originodes;
458 olditime = dquot->dq_dqb.dqb_itime;
459 oldbtime = dquot->dq_dqb.dqb_btime;
460 ocfs2_global_disk2memdqb(dquot, &dqblk);
461 mlog(0, "Syncing global dquot %d space %lld+%lld, inodes %lld+%lld\n",
462 dquot->dq_id, dquot->dq_dqb.dqb_curspace, spacechange,
463 dquot->dq_dqb.dqb_curinodes, inodechange);
464 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
465 dquot->dq_dqb.dqb_curspace += spacechange;
466 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
467 dquot->dq_dqb.dqb_curinodes += inodechange;
468 /* Set properly space grace time... */
469 if (dquot->dq_dqb.dqb_bsoftlimit &&
470 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
471 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
473 if (dquot->dq_dqb.dqb_btime > 0)
474 dquot->dq_dqb.dqb_btime =
475 min(dquot->dq_dqb.dqb_btime, oldbtime);
477 dquot->dq_dqb.dqb_btime = oldbtime;
480 dquot->dq_dqb.dqb_btime = 0;
481 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
483 /* Set properly inode grace time... */
484 if (dquot->dq_dqb.dqb_isoftlimit &&
485 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
486 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
488 if (dquot->dq_dqb.dqb_itime > 0)
489 dquot->dq_dqb.dqb_itime =
490 min(dquot->dq_dqb.dqb_itime, olditime);
492 dquot->dq_dqb.dqb_itime = olditime;
495 dquot->dq_dqb.dqb_itime = 0;
496 clear_bit(DQ_INODES_B, &dquot->dq_flags);
498 /* All information is properly updated, clear the flags */
499 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
500 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
501 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
502 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
503 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
504 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
505 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
506 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
507 spin_unlock(&dq_data_lock);
508 err = ocfs2_qinfo_lock(info, freeing);
510 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
511 " (type=%d, id=%u)\n", dquot->dq_type,
512 (unsigned)dquot->dq_id);
516 OCFS2_DQUOT(dquot)->dq_use_count--;
517 err = qtree_write_dquot(&info->dqi_gi, dquot);
520 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
521 err = qtree_release_dquot(&info->dqi_gi, dquot);
522 if (info_dirty(sb_dqinfo(sb, type))) {
523 err2 = __ocfs2_global_write_info(sb, type);
529 ocfs2_qinfo_unlock(info, freeing);
537 * Functions for periodic syncing of dquots with global file
539 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
542 struct super_block *sb = dquot->dq_sb;
543 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
544 struct ocfs2_super *osb = OCFS2_SB(sb);
547 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
548 dquot->dq_type, type, sb->s_id);
549 if (type != dquot->dq_type)
551 status = ocfs2_lock_global_qf(oinfo, 1);
555 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
556 if (IS_ERR(handle)) {
557 status = PTR_ERR(handle);
561 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
562 status = ocfs2_sync_dquot(dquot);
563 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
566 /* We have to write local structure as well... */
567 dquot_mark_dquot_dirty(dquot);
568 status = dquot_commit(dquot);
571 ocfs2_commit_trans(osb, handle);
573 ocfs2_unlock_global_qf(oinfo, 1);
579 static void qsync_work_fn(struct work_struct *work)
581 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
582 struct ocfs2_mem_dqinfo,
584 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
586 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
587 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
588 oinfo->dqi_syncjiff);
592 * Wrappers for generic quota functions
595 static int ocfs2_write_dquot(struct dquot *dquot)
598 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
601 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
603 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
604 if (IS_ERR(handle)) {
605 status = PTR_ERR(handle);
609 status = dquot_commit(dquot);
610 ocfs2_commit_trans(osb, handle);
616 int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
618 struct ocfs2_mem_dqinfo *oinfo;
619 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
620 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
622 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
625 oinfo = sb_dqinfo(sb, type)->dqi_priv;
626 /* We modify tree, leaf block, global info, local chunk header,
627 * global and local inode */
628 return oinfo->dqi_gi.dqi_qtree_depth + 2 + 1 +
629 2 * OCFS2_INODE_UPDATE_CREDITS;
632 static int ocfs2_release_dquot(struct dquot *dquot)
635 struct ocfs2_mem_dqinfo *oinfo =
636 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
637 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
640 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
642 status = ocfs2_lock_global_qf(oinfo, 1);
645 handle = ocfs2_start_trans(osb,
646 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
647 if (IS_ERR(handle)) {
648 status = PTR_ERR(handle);
652 status = dquot_release(dquot);
653 ocfs2_commit_trans(osb, handle);
655 ocfs2_unlock_global_qf(oinfo, 1);
661 int ocfs2_calc_qinit_credits(struct super_block *sb, int type)
663 struct ocfs2_mem_dqinfo *oinfo;
664 int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
665 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
666 struct ocfs2_dinode *lfe, *gfe;
668 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
671 oinfo = sb_dqinfo(sb, type)->dqi_priv;
672 gfe = (struct ocfs2_dinode *)oinfo->dqi_gqi_bh->b_data;
673 lfe = (struct ocfs2_dinode *)oinfo->dqi_lqi_bh->b_data;
674 /* We can extend local file + global file. In local file we
675 * can modify info, chunk header block and dquot block. In
676 * global file we can modify info, tree and leaf block */
677 return ocfs2_calc_extend_credits(sb, &lfe->id2.i_list, 0) +
678 ocfs2_calc_extend_credits(sb, &gfe->id2.i_list, 0) +
679 3 + oinfo->dqi_gi.dqi_qtree_depth + 2;
682 static int ocfs2_acquire_dquot(struct dquot *dquot)
685 struct ocfs2_mem_dqinfo *oinfo =
686 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
687 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
690 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
691 /* We need an exclusive lock, because we're going to update use count
692 * and instantiate possibly new dquot structure */
693 status = ocfs2_lock_global_qf(oinfo, 1);
696 handle = ocfs2_start_trans(osb,
697 ocfs2_calc_qinit_credits(dquot->dq_sb, dquot->dq_type));
698 if (IS_ERR(handle)) {
699 status = PTR_ERR(handle);
703 status = dquot_acquire(dquot);
704 ocfs2_commit_trans(osb, handle);
706 ocfs2_unlock_global_qf(oinfo, 1);
712 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
714 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
715 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
716 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
717 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
718 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
719 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
722 struct super_block *sb = dquot->dq_sb;
723 int type = dquot->dq_type;
724 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
726 struct ocfs2_super *osb = OCFS2_SB(sb);
728 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
729 dquot_mark_dquot_dirty(dquot);
731 /* In case user set some limits, sync dquot immediately to global
732 * quota file so that information propagates quicker */
733 spin_lock(&dq_data_lock);
734 if (dquot->dq_flags & mask)
736 spin_unlock(&dq_data_lock);
738 status = ocfs2_write_dquot(dquot);
741 status = ocfs2_lock_global_qf(oinfo, 1);
744 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
745 if (IS_ERR(handle)) {
746 status = PTR_ERR(handle);
750 status = ocfs2_sync_dquot(dquot);
755 /* Now write updated local dquot structure */
756 status = dquot_commit(dquot);
758 ocfs2_commit_trans(osb, handle);
760 ocfs2_unlock_global_qf(oinfo, 1);
766 /* This should happen only after set_dqinfo(). */
767 static int ocfs2_write_info(struct super_block *sb, int type)
771 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
775 status = ocfs2_lock_global_qf(oinfo, 1);
778 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
779 if (IS_ERR(handle)) {
780 status = PTR_ERR(handle);
784 status = dquot_commit_info(sb, type);
785 ocfs2_commit_trans(OCFS2_SB(sb), handle);
787 ocfs2_unlock_global_qf(oinfo, 1);
793 /* This is difficult. We have to lock quota inode and start transaction
794 * in this function but we don't want to take the penalty of exlusive
795 * quota file lock when we are just going to use cached structures. So
796 * we just take read lock check whether we have dquot cached and if so,
797 * we don't have to take the write lock... */
798 static int ocfs2_dquot_initialize(struct inode *inode, int type)
800 handle_t *handle = NULL;
802 struct super_block *sb = inode->i_sb;
803 struct ocfs2_mem_dqinfo *oinfo;
810 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
811 if (type != -1 && cnt != type)
813 if (!sb_has_quota_active(sb, cnt))
815 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
816 status = ocfs2_lock_global_qf(oinfo, 0);
819 /* This is just a performance optimization not a reliable test.
820 * Since we hold an inode lock, noone can actually release
821 * the structure until we are finished with initialization. */
822 if (inode->i_dquot[cnt] != NODQUOT) {
823 ocfs2_unlock_global_qf(oinfo, 0);
826 /* When we have inode lock, we know that no dquot_release() can
827 * run and thus we can safely check whether we need to
828 * read+modify global file to get quota information or whether
829 * our node already has it. */
832 else if (cnt == GRPQUOTA)
836 /* Obtain exclusion from quota off... */
837 down_write(&sb_dqopt(sb)->dqptr_sem);
838 exclusive = !dquot_is_cached(sb, id, cnt);
839 up_write(&sb_dqopt(sb)->dqptr_sem);
841 status = ocfs2_lock_global_qf(oinfo, 1);
847 handle = ocfs2_start_trans(OCFS2_SB(sb),
848 ocfs2_calc_qinit_credits(sb, cnt));
849 if (IS_ERR(handle)) {
850 status = PTR_ERR(handle);
855 dquot_initialize(inode, cnt);
857 ocfs2_commit_trans(OCFS2_SB(sb), handle);
858 ocfs2_unlock_global_qf(oinfo, 1);
860 ocfs2_unlock_global_qf(oinfo, 0);
866 ocfs2_unlock_global_qf(oinfo, 1);
867 ocfs2_unlock_global_qf(oinfo, 0);
873 static int ocfs2_dquot_drop_slow(struct inode *inode)
877 int got_lock[MAXQUOTAS] = {0, 0};
879 struct super_block *sb = inode->i_sb;
880 struct ocfs2_mem_dqinfo *oinfo;
882 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
883 if (!sb_has_quota_active(sb, cnt))
885 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
886 status = ocfs2_lock_global_qf(oinfo, 1);
891 handle = ocfs2_start_trans(OCFS2_SB(sb),
892 ocfs2_calc_qinit_credits(sb, USRQUOTA) +
893 ocfs2_calc_qinit_credits(sb, GRPQUOTA));
894 if (IS_ERR(handle)) {
895 status = PTR_ERR(handle);
900 ocfs2_commit_trans(OCFS2_SB(sb), handle);
902 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
904 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
905 ocfs2_unlock_global_qf(oinfo, 1);
910 /* See the comment before ocfs2_dquot_initialize. */
911 static int ocfs2_dquot_drop(struct inode *inode)
914 struct super_block *sb = inode->i_sb;
915 struct ocfs2_mem_dqinfo *oinfo;
918 int got_lock[MAXQUOTAS] = {0, 0};
921 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
922 if (!sb_has_quota_active(sb, cnt))
924 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
925 status = ocfs2_lock_global_qf(oinfo, 0);
930 /* Lock against anyone releasing references so that when when we check
931 * we know we are not going to be last ones to release dquot */
932 down_write(&sb_dqopt(sb)->dqptr_sem);
933 /* Urgh, this is a terrible hack :( */
934 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
935 if (inode->i_dquot[cnt] != NODQUOT &&
936 atomic_read(&inode->i_dquot[cnt]->dq_count) > 1) {
942 dquot_drop_locked(inode);
943 up_write(&sb_dqopt(sb)->dqptr_sem);
945 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
947 oinfo = sb_dqinfo(sb, cnt)->dqi_priv;
948 ocfs2_unlock_global_qf(oinfo, 0);
950 /* In case we bailed out because we had to do expensive locking
953 status = ocfs2_dquot_drop_slow(inode);
958 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
960 struct ocfs2_dquot *dquot =
961 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
965 return &dquot->dq_dquot;
968 static void ocfs2_destroy_dquot(struct dquot *dquot)
970 kmem_cache_free(ocfs2_dquot_cachep, dquot);
973 struct dquot_operations ocfs2_quota_operations = {
974 .initialize = ocfs2_dquot_initialize,
975 .drop = ocfs2_dquot_drop,
976 .alloc_space = dquot_alloc_space,
977 .alloc_inode = dquot_alloc_inode,
978 .free_space = dquot_free_space,
979 .free_inode = dquot_free_inode,
980 .transfer = dquot_transfer,
981 .write_dquot = ocfs2_write_dquot,
982 .acquire_dquot = ocfs2_acquire_dquot,
983 .release_dquot = ocfs2_release_dquot,
984 .mark_dirty = ocfs2_mark_dquot_dirty,
985 .write_info = ocfs2_write_info,
986 .alloc_dquot = ocfs2_alloc_dquot,
987 .destroy_dquot = ocfs2_destroy_dquot,
990 int ocfs2_quota_setup(void)
992 ocfs2_quota_wq = create_workqueue("o2quot");
998 void ocfs2_quota_shutdown(void)
1000 if (ocfs2_quota_wq) {
1001 flush_workqueue(ocfs2_quota_wq);
1002 destroy_workqueue(ocfs2_quota_wq);
1003 ocfs2_quota_wq = NULL;