2 * Implementation of operations over local quota file
6 #include <linux/slab.h>
7 #include <linux/quota.h>
8 #include <linux/quotaops.h>
9 #include <linux/module.h>
11 #define MLOG_MASK_PREFIX ML_QUOTA
12 #include <cluster/masklog.h>
19 #include "buffer_head_io.h"
26 /* Number of local quota structures per block */
27 static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
29 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
30 sizeof(struct ocfs2_local_disk_dqblk));
33 /* Number of blocks with entries in one chunk */
34 static inline unsigned int ol_chunk_blocks(struct super_block *sb)
36 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
37 OCFS2_QBLK_RESERVED_SPACE) << 3) /
38 ol_quota_entries_per_block(sb);
41 /* Number of entries in a chunk bitmap */
42 static unsigned int ol_chunk_entries(struct super_block *sb)
44 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
47 /* Offset of the chunk in quota file */
48 static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
50 /* 1 block for local quota file info, 1 block per chunk for chunk info */
51 return 1 + (ol_chunk_blocks(sb) + 1) * c;
54 static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
56 int epb = ol_quota_entries_per_block(sb);
58 return ol_quota_chunk_block(sb, c) + 1 + off / epb;
61 static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
63 int epb = ol_quota_entries_per_block(sb);
65 return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
68 /* Offset of the dquot structure in the quota file */
69 static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
71 return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
72 ol_dqblk_block_off(sb, c, off);
75 /* Compute block number from given offset */
76 static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
78 return off >> sb->s_blocksize_bits;
81 static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
83 return off & ((1 << sb->s_blocksize_bits) - 1);
86 /* Compute offset in the chunk of a structure with the given offset */
87 static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
89 int epb = ol_quota_entries_per_block(sb);
91 return ((off >> sb->s_blocksize_bits) -
92 ol_quota_chunk_block(sb, c) - 1) * epb
93 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
94 sizeof(struct ocfs2_local_disk_dqblk);
97 /* Write bufferhead into the fs */
98 static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
99 void (*modify)(struct buffer_head *, void *), void *private)
101 struct super_block *sb = inode->i_sb;
105 handle = ocfs2_start_trans(OCFS2_SB(sb),
106 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
107 if (IS_ERR(handle)) {
108 status = PTR_ERR(handle);
112 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
113 OCFS2_JOURNAL_ACCESS_WRITE);
116 ocfs2_commit_trans(OCFS2_SB(sb), handle);
122 ocfs2_journal_dirty(handle, bh);
124 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
132 /* Check whether we understand format of quota files */
133 static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
135 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
136 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
137 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
138 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
139 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
140 GROUP_QUOTA_SYSTEM_INODE };
141 struct buffer_head *bh = NULL;
142 struct inode *linode = sb_dqopt(sb)->files[type];
143 struct inode *ginode = NULL;
144 struct ocfs2_disk_dqheader *dqhead;
147 /* First check whether we understand local quota file */
148 status = ocfs2_read_quota_block(linode, 0, &bh);
151 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
155 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
156 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
157 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
158 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
159 lmagics[type], type);
162 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
163 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
164 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
165 lversions[type], type);
171 /* Next check whether we understand global quota file */
172 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
175 mlog(ML_ERROR, "cannot get global quota file inode "
176 "(type=%d)\n", type);
179 /* Since the header is read only, we don't care about locking */
180 status = ocfs2_read_quota_block(ginode, 0, &bh);
183 mlog(ML_ERROR, "failed to read global quota file header "
184 "(type=%d)\n", type);
187 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
188 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
189 mlog(ML_ERROR, "global quota file magic does not match "
190 "(%u != %u), type=%d\n",
191 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
194 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
195 mlog(ML_ERROR, "global quota file version does not match "
196 "(%u != %u), type=%d\n",
197 le32_to_cpu(dqhead->dqh_version), gversions[type],
209 /* Release given list of quota file chunks */
210 static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
212 struct ocfs2_quota_chunk *pos, *next;
214 list_for_each_entry_safe(pos, next, head, qc_chunk) {
215 list_del(&pos->qc_chunk);
216 brelse(pos->qc_headerbh);
217 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
221 /* Load quota bitmaps into memory */
222 static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
223 struct ocfs2_local_disk_dqinfo *ldinfo,
224 struct list_head *head)
226 struct ocfs2_quota_chunk *newchunk;
229 INIT_LIST_HEAD(head);
230 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
231 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
233 ocfs2_release_local_quota_bitmaps(head);
236 newchunk->qc_num = i;
237 newchunk->qc_headerbh = NULL;
238 status = ocfs2_read_quota_block(inode,
239 ol_quota_chunk_block(inode->i_sb, i),
240 &newchunk->qc_headerbh);
243 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
244 ocfs2_release_local_quota_bitmaps(head);
247 list_add_tail(&newchunk->qc_chunk, head);
252 static void olq_update_info(struct buffer_head *bh, void *private)
254 struct mem_dqinfo *info = private;
255 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
256 struct ocfs2_local_disk_dqinfo *ldinfo;
258 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
259 OCFS2_LOCAL_INFO_OFF);
260 spin_lock(&dq_data_lock);
261 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
262 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
263 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
264 spin_unlock(&dq_data_lock);
267 static int ocfs2_add_recovery_chunk(struct super_block *sb,
268 struct ocfs2_local_disk_chunk *dchunk,
270 struct list_head *head)
272 struct ocfs2_recovery_chunk *rc;
274 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
277 rc->rc_chunk = chunk;
278 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
279 if (!rc->rc_bitmap) {
283 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
284 (ol_chunk_entries(sb) + 7) >> 3);
285 list_add_tail(&rc->rc_list, head);
289 static void free_recovery_list(struct list_head *head)
291 struct ocfs2_recovery_chunk *next;
292 struct ocfs2_recovery_chunk *rchunk;
294 list_for_each_entry_safe(rchunk, next, head, rc_list) {
295 list_del(&rchunk->rc_list);
296 kfree(rchunk->rc_bitmap);
301 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
305 for (type = 0; type < MAXQUOTAS; type++)
306 free_recovery_list(&(rec->r_list[type]));
310 /* Load entries in our quota file we have to recover*/
311 static int ocfs2_recovery_load_quota(struct inode *lqinode,
312 struct ocfs2_local_disk_dqinfo *ldinfo,
314 struct list_head *head)
316 struct super_block *sb = lqinode->i_sb;
317 struct buffer_head *hbh;
318 struct ocfs2_local_disk_chunk *dchunk;
319 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
322 for (i = 0; i < chunks; i++) {
324 status = ocfs2_read_quota_block(lqinode,
325 ol_quota_chunk_block(sb, i),
331 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
332 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
333 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
339 free_recovery_list(head);
343 static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
346 struct ocfs2_quota_recovery *rec;
348 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
351 for (type = 0; type < MAXQUOTAS; type++)
352 INIT_LIST_HEAD(&(rec->r_list[type]));
356 /* Load information we need for quota recovery into memory */
357 struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
358 struct ocfs2_super *osb,
361 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
362 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
363 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
364 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
365 struct super_block *sb = osb->sb;
366 struct ocfs2_local_disk_dqinfo *ldinfo;
367 struct inode *lqinode;
368 struct buffer_head *bh;
371 struct ocfs2_quota_recovery *rec;
373 mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
374 rec = ocfs2_alloc_quota_recovery();
376 return ERR_PTR(-ENOMEM);
379 for (type = 0; type < MAXQUOTAS; type++) {
380 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
382 /* At this point, journal of the slot is already replayed so
383 * we can trust metadata and data of the quota file */
384 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
389 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
390 OCFS2_META_LOCK_RECOVERY);
395 /* Now read local header */
397 status = ocfs2_read_quota_block(lqinode, 0, &bh);
400 mlog(ML_ERROR, "failed to read quota file info header "
401 "(slot=%d type=%d)\n", slot_num, type);
404 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
405 OCFS2_LOCAL_INFO_OFF);
406 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
410 ocfs2_inode_unlock(lqinode, 1);
418 ocfs2_free_quota_recovery(rec);
419 rec = ERR_PTR(status);
424 /* Sync changes in local quota file into global quota file and
425 * reinitialize local quota file.
426 * The function expects local quota file to be already locked and
427 * dqonoff_mutex locked. */
428 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
430 struct ocfs2_quota_recovery *rec)
432 struct super_block *sb = lqinode->i_sb;
433 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
434 struct ocfs2_local_disk_chunk *dchunk;
435 struct ocfs2_local_disk_dqblk *dqblk;
438 struct buffer_head *hbh = NULL, *qbh = NULL;
441 struct ocfs2_recovery_chunk *rchunk, *next;
442 qsize_t spacechange, inodechange;
444 mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
446 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
447 chunk = rchunk->rc_chunk;
449 status = ocfs2_read_quota_block(lqinode,
450 ol_quota_chunk_block(sb, chunk),
456 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
457 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
459 status = ocfs2_read_quota_block(lqinode,
460 ol_dqblk_block(sb, chunk, bit),
466 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
467 ol_dqblk_block_off(sb, chunk, bit));
468 dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
471 mlog(ML_ERROR, "Failed to get quota structure "
472 "for id %u, type %d. Cannot finish quota "
474 (unsigned)le64_to_cpu(dqblk->dqb_id),
478 status = ocfs2_lock_global_qf(oinfo, 1);
484 handle = ocfs2_start_trans(OCFS2_SB(sb),
485 OCFS2_QSYNC_CREDITS);
486 if (IS_ERR(handle)) {
487 status = PTR_ERR(handle);
491 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
492 spin_lock(&dq_data_lock);
493 /* Add usage from quota entry into quota changes
494 * of our node. Auxiliary variables are important
495 * due to signedness */
496 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
497 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
498 dquot->dq_dqb.dqb_curspace += spacechange;
499 dquot->dq_dqb.dqb_curinodes += inodechange;
500 spin_unlock(&dq_data_lock);
501 /* We want to drop reference held by the crashed
502 * node. Since we have our own reference we know
503 * global structure actually won't be freed. */
504 status = ocfs2_global_release_dquot(dquot);
509 /* Release local quota file entry */
510 status = ocfs2_journal_access_dq(handle,
511 INODE_CACHE(lqinode),
512 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
518 WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
519 ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
520 le32_add_cpu(&dchunk->dqc_free, 1);
522 ocfs2_journal_dirty(handle, qbh);
524 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
525 ocfs2_commit_trans(OCFS2_SB(sb), handle);
527 ocfs2_unlock_global_qf(oinfo, 1);
536 list_del(&rchunk->rc_list);
537 kfree(rchunk->rc_bitmap);
543 free_recovery_list(&(rec->r_list[type]));
548 /* Recover local quota files for given node different from us */
549 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
550 struct ocfs2_quota_recovery *rec,
553 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
554 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
555 struct super_block *sb = osb->sb;
556 struct ocfs2_local_disk_dqinfo *ldinfo;
557 struct buffer_head *bh;
561 struct inode *lqinode;
564 mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
565 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
566 for (type = 0; type < MAXQUOTAS; type++) {
567 if (list_empty(&(rec->r_list[type])))
569 mlog(0, "Recovering quota in slot %d\n", slot_num);
570 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
575 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
576 OCFS2_META_LOCK_NOQUEUE);
577 /* Someone else is holding the lock? Then he must be
578 * doing the recovery. Just skip the file... */
579 if (status == -EAGAIN) {
580 mlog(ML_NOTICE, "skipping quota recovery for slot %d "
581 "because quota file is locked.\n", slot_num);
584 } else if (status < 0) {
588 /* Now read local header */
590 status = ocfs2_read_quota_block(lqinode, 0, &bh);
593 mlog(ML_ERROR, "failed to read quota file info header "
594 "(slot=%d type=%d)\n", slot_num, type);
597 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
598 OCFS2_LOCAL_INFO_OFF);
599 /* Is recovery still needed? */
600 flags = le32_to_cpu(ldinfo->dqi_flags);
601 if (!(flags & OLQF_CLEAN))
602 status = ocfs2_recover_local_quota_file(lqinode,
605 /* We don't want to mark file as clean when it is actually
607 if (slot_num == osb->slot_num)
609 /* Mark quota file as clean if we are recovering quota file of
610 * some other node. */
611 handle = ocfs2_start_trans(osb,
612 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
613 if (IS_ERR(handle)) {
614 status = PTR_ERR(handle);
618 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
620 OCFS2_JOURNAL_ACCESS_WRITE);
626 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
628 ocfs2_journal_dirty(handle, bh);
630 ocfs2_commit_trans(osb, handle);
634 ocfs2_inode_unlock(lqinode, 1);
641 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
646 /* Read information header from quota file */
647 static int ocfs2_local_read_info(struct super_block *sb, int type)
649 struct ocfs2_local_disk_dqinfo *ldinfo;
650 struct mem_dqinfo *info = sb_dqinfo(sb, type);
651 struct ocfs2_mem_dqinfo *oinfo;
652 struct inode *lqinode = sb_dqopt(sb)->files[type];
654 struct buffer_head *bh = NULL;
655 struct ocfs2_quota_recovery *rec;
658 /* We don't need the lock and we have to acquire quota file locks
659 * which will later depend on this lock */
660 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
661 info->dqi_maxblimit = 0x7fffffffffffffffLL;
662 info->dqi_maxilimit = 0x7fffffffffffffffLL;
663 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
665 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
669 info->dqi_priv = oinfo;
670 oinfo->dqi_type = type;
671 INIT_LIST_HEAD(&oinfo->dqi_chunk);
672 oinfo->dqi_rec = NULL;
673 oinfo->dqi_lqi_bh = NULL;
674 oinfo->dqi_ibh = NULL;
676 status = ocfs2_global_read_info(sb, type);
680 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
687 /* Now read local header */
688 status = ocfs2_read_quota_block(lqinode, 0, &bh);
691 mlog(ML_ERROR, "failed to read quota file info header "
692 "(type=%d)\n", type);
695 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
696 OCFS2_LOCAL_INFO_OFF);
697 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
698 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
699 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
702 /* We crashed when using local quota file? */
703 if (!(info->dqi_flags & OLQF_CLEAN)) {
704 rec = OCFS2_SB(sb)->quota_rec;
706 rec = ocfs2_alloc_quota_recovery();
712 OCFS2_SB(sb)->quota_rec = rec;
715 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
723 status = ocfs2_load_local_quota_bitmaps(lqinode,
731 /* Now mark quota file as used */
732 info->dqi_flags &= ~OLQF_CLEAN;
733 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
739 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
743 iput(oinfo->dqi_gqinode);
744 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
745 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
746 brelse(oinfo->dqi_lqi_bh);
748 ocfs2_inode_unlock(lqinode, 1);
749 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
753 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
757 /* Write local info to quota file */
758 static int ocfs2_local_write_info(struct super_block *sb, int type)
760 struct mem_dqinfo *info = sb_dqinfo(sb, type);
761 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
765 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
775 /* Release info from memory */
776 static int ocfs2_local_free_info(struct super_block *sb, int type)
778 struct mem_dqinfo *info = sb_dqinfo(sb, type);
779 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
780 struct ocfs2_quota_chunk *chunk;
781 struct ocfs2_local_disk_chunk *dchunk;
782 int mark_clean = 1, len;
785 /* At this point we know there are no more dquots and thus
786 * even if there's some sync in the pdflush queue, it won't
787 * find any dquots and return without doing anything */
788 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
789 iput(oinfo->dqi_gqinode);
790 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
791 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
792 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
793 dchunk = (struct ocfs2_local_disk_chunk *)
794 (chunk->qc_headerbh->b_data);
795 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
796 len = ol_chunk_entries(sb);
798 len = (oinfo->dqi_blocks -
799 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
800 * ol_quota_entries_per_block(sb);
802 /* Not all entries free? Bug! */
803 if (le32_to_cpu(dchunk->dqc_free) != len) {
804 mlog(ML_ERROR, "releasing quota file with used "
805 "entries (type=%d)\n", type);
809 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
811 /* dqonoff_mutex protects us against racing with recovery thread... */
812 if (oinfo->dqi_rec) {
813 ocfs2_free_quota_recovery(oinfo->dqi_rec);
820 /* Mark local file as clean */
821 info->dqi_flags |= OLQF_CLEAN;
822 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
832 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
833 brelse(oinfo->dqi_ibh);
834 brelse(oinfo->dqi_lqi_bh);
839 static void olq_set_dquot(struct buffer_head *bh, void *private)
841 struct ocfs2_dquot *od = private;
842 struct ocfs2_local_disk_dqblk *dqblk;
843 struct super_block *sb = od->dq_dquot.dq_sb;
845 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
846 + ol_dqblk_block_offset(sb, od->dq_local_off));
848 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
849 spin_lock(&dq_data_lock);
850 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
852 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
854 spin_unlock(&dq_data_lock);
855 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
856 od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
857 (long long)le64_to_cpu(dqblk->dqb_inodemod));
860 /* Write dquot to local quota file */
861 static int ocfs2_local_write_dquot(struct dquot *dquot)
863 struct super_block *sb = dquot->dq_sb;
864 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
865 struct buffer_head *bh = NULL;
868 status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
869 ol_dqblk_file_block(sb, od->dq_local_off),
875 status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
886 /* Find free entry in local quota file */
887 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
891 struct mem_dqinfo *info = sb_dqinfo(sb, type);
892 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
893 struct ocfs2_quota_chunk *chunk;
894 struct ocfs2_local_disk_chunk *dchunk;
897 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
898 dchunk = (struct ocfs2_local_disk_chunk *)
899 chunk->qc_headerbh->b_data;
900 if (le32_to_cpu(dchunk->dqc_free) > 0) {
908 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
909 len = ol_chunk_entries(sb);
911 len = (oinfo->dqi_blocks -
912 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
913 * ol_quota_entries_per_block(sb);
916 found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
919 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
920 " entries free (type=%d)\n", chunk->qc_num,
921 le32_to_cpu(dchunk->dqc_free), type);
922 return ERR_PTR(-EIO);
928 /* Add new chunk to the local quota file */
929 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
930 struct super_block *sb,
934 struct mem_dqinfo *info = sb_dqinfo(sb, type);
935 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
936 struct inode *lqinode = sb_dqopt(sb)->files[type];
937 struct ocfs2_quota_chunk *chunk = NULL;
938 struct ocfs2_local_disk_chunk *dchunk;
941 struct buffer_head *bh = NULL, *dbh = NULL;
944 /* We are protected by dqio_sem so no locking needed */
945 status = ocfs2_extend_no_holes(lqinode,
946 lqinode->i_size + 2 * sb->s_blocksize,
952 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
953 lqinode->i_size + 2 * sb->s_blocksize);
959 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
965 /* Local quota info and two new blocks we initialize */
966 handle = ocfs2_start_trans(OCFS2_SB(sb),
967 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
968 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
969 if (IS_ERR(handle)) {
970 status = PTR_ERR(handle);
975 /* Initialize chunk header */
976 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
977 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
978 &p_blkno, NULL, NULL);
979 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
984 bh = sb_getblk(sb, p_blkno);
990 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
991 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
992 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
993 OCFS2_JOURNAL_ACCESS_CREATE);
999 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
1000 memset(dchunk->dqc_bitmap, 0,
1001 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1002 OCFS2_QBLK_RESERVED_SPACE);
1004 ocfs2_journal_dirty(handle, bh);
1006 /* Initialize new block with structures */
1007 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1008 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1009 &p_blkno, NULL, NULL);
1010 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1015 dbh = sb_getblk(sb, p_blkno);
1021 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
1022 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
1023 OCFS2_JOURNAL_ACCESS_CREATE);
1029 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1031 ocfs2_journal_dirty(handle, dbh);
1033 /* Update local quotafile info */
1034 oinfo->dqi_blocks += 2;
1035 oinfo->dqi_chunks++;
1036 status = ocfs2_local_write_info(sb, type);
1041 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1047 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1048 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1049 struct ocfs2_quota_chunk,
1050 qc_chunk)->qc_num + 1;
1051 chunk->qc_headerbh = bh;
1055 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1059 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1060 return ERR_PTR(status);
1063 /* Find free entry in local quota file */
1064 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1065 struct super_block *sb,
1069 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1070 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1071 struct ocfs2_quota_chunk *chunk;
1072 struct inode *lqinode = sb_dqopt(sb)->files[type];
1073 struct ocfs2_local_disk_chunk *dchunk;
1074 int epb = ol_quota_entries_per_block(sb);
1075 unsigned int chunk_blocks;
1076 struct buffer_head *bh;
1081 if (list_empty(&oinfo->dqi_chunk))
1082 return ocfs2_local_quota_add_chunk(sb, type, offset);
1083 /* Is the last chunk full? */
1084 chunk = list_entry(oinfo->dqi_chunk.prev,
1085 struct ocfs2_quota_chunk, qc_chunk);
1086 chunk_blocks = oinfo->dqi_blocks -
1087 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1088 if (ol_chunk_blocks(sb) == chunk_blocks)
1089 return ocfs2_local_quota_add_chunk(sb, type, offset);
1091 /* We are protected by dqio_sem so no locking needed */
1092 status = ocfs2_extend_no_holes(lqinode,
1093 lqinode->i_size + sb->s_blocksize,
1099 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1100 lqinode->i_size + sb->s_blocksize);
1106 /* Get buffer from the just added block */
1107 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1108 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1109 &p_blkno, NULL, NULL);
1110 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1115 bh = sb_getblk(sb, p_blkno);
1121 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1123 /* Local quota info, chunk header and the new block we initialize */
1124 handle = ocfs2_start_trans(OCFS2_SB(sb),
1125 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1126 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1127 if (IS_ERR(handle)) {
1128 status = PTR_ERR(handle);
1132 /* Zero created block */
1133 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1134 OCFS2_JOURNAL_ACCESS_CREATE);
1140 memset(bh->b_data, 0, sb->s_blocksize);
1142 ocfs2_journal_dirty(handle, bh);
1144 /* Update chunk header */
1145 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1147 OCFS2_JOURNAL_ACCESS_WRITE);
1153 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1154 lock_buffer(chunk->qc_headerbh);
1155 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1156 unlock_buffer(chunk->qc_headerbh);
1157 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1159 /* Update file header */
1160 oinfo->dqi_blocks++;
1161 status = ocfs2_local_write_info(sb, type);
1167 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1172 *offset = chunk_blocks * epb;
1175 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1177 return ERR_PTR(status);
1180 static void olq_alloc_dquot(struct buffer_head *bh, void *private)
1182 int *offset = private;
1183 struct ocfs2_local_disk_chunk *dchunk;
1185 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1186 ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
1187 le32_add_cpu(&dchunk->dqc_free, -1);
1190 /* Create dquot in the local file for given id */
1191 static int ocfs2_create_local_dquot(struct dquot *dquot)
1193 struct super_block *sb = dquot->dq_sb;
1194 int type = dquot->dq_type;
1195 struct inode *lqinode = sb_dqopt(sb)->files[type];
1196 struct ocfs2_quota_chunk *chunk;
1197 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1201 chunk = ocfs2_find_free_entry(sb, type, &offset);
1203 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1205 return PTR_ERR(chunk);
1206 } else if (IS_ERR(chunk)) {
1207 return PTR_ERR(chunk);
1209 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1210 od->dq_chunk = chunk;
1212 /* Initialize dquot structure on disk */
1213 status = ocfs2_local_write_dquot(dquot);
1219 /* Mark structure as allocated */
1220 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1230 /* Create entry in local file for dquot, load data from the global file */
1231 static int ocfs2_local_read_dquot(struct dquot *dquot)
1235 mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
1237 status = ocfs2_global_read_dquot(dquot);
1243 /* Now create entry in the local quota file */
1244 status = ocfs2_create_local_dquot(dquot);
1256 /* Release dquot structure from local quota file. ocfs2_release_dquot() has
1257 * already started a transaction and obtained exclusive lock for global
1259 static int ocfs2_local_release_dquot(struct dquot *dquot)
1262 int type = dquot->dq_type;
1263 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1264 struct super_block *sb = dquot->dq_sb;
1265 struct ocfs2_local_disk_chunk *dchunk;
1267 handle_t *handle = journal_current_handle();
1270 /* First write all local changes to global file */
1271 status = ocfs2_global_release_dquot(dquot);
1277 status = ocfs2_journal_access_dq(handle,
1278 INODE_CACHE(sb_dqopt(sb)->files[type]),
1279 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1284 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1286 dchunk = (struct ocfs2_local_disk_chunk *)
1287 (od->dq_chunk->qc_headerbh->b_data);
1288 /* Mark structure as freed */
1289 lock_buffer(od->dq_chunk->qc_headerbh);
1290 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
1291 le32_add_cpu(&dchunk->dqc_free, 1);
1292 unlock_buffer(od->dq_chunk->qc_headerbh);
1293 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1296 /* Clear the read bit so that next time someone uses this
1297 * dquot he reads fresh info from disk and allocates local
1298 * dquot structure */
1299 clear_bit(DQ_READ_B, &dquot->dq_flags);
1303 static const struct quota_format_ops ocfs2_format_ops = {
1304 .check_quota_file = ocfs2_local_check_quota_file,
1305 .read_file_info = ocfs2_local_read_info,
1306 .write_file_info = ocfs2_global_write_info,
1307 .free_file_info = ocfs2_local_free_info,
1308 .read_dqblk = ocfs2_local_read_dquot,
1309 .commit_dqblk = ocfs2_local_write_dquot,
1310 .release_dqblk = ocfs2_local_release_dquot,
1313 struct quota_format_type ocfs2_quota_format = {
1314 .qf_fmt_id = QFMT_OCFS2,
1315 .qf_ops = &ocfs2_format_ops,
1316 .qf_owner = THIS_MODULE