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 status = ocfs2_journal_dirty(handle, bh);
125 ocfs2_commit_trans(OCFS2_SB(sb), handle);
128 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
136 /* Check whether we understand format of quota files */
137 static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
139 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
140 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
141 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
142 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
143 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
144 GROUP_QUOTA_SYSTEM_INODE };
145 struct buffer_head *bh = NULL;
146 struct inode *linode = sb_dqopt(sb)->files[type];
147 struct inode *ginode = NULL;
148 struct ocfs2_disk_dqheader *dqhead;
151 /* First check whether we understand local quota file */
152 status = ocfs2_read_quota_block(linode, 0, &bh);
155 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
159 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
160 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
161 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
162 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
163 lmagics[type], type);
166 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
167 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
168 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
169 lversions[type], type);
175 /* Next check whether we understand global quota file */
176 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
179 mlog(ML_ERROR, "cannot get global quota file inode "
180 "(type=%d)\n", type);
183 /* Since the header is read only, we don't care about locking */
184 status = ocfs2_read_quota_block(ginode, 0, &bh);
187 mlog(ML_ERROR, "failed to read global quota file header "
188 "(type=%d)\n", type);
191 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
192 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
193 mlog(ML_ERROR, "global quota file magic does not match "
194 "(%u != %u), type=%d\n",
195 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
198 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
199 mlog(ML_ERROR, "global quota file version does not match "
200 "(%u != %u), type=%d\n",
201 le32_to_cpu(dqhead->dqh_version), gversions[type],
213 /* Release given list of quota file chunks */
214 static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
216 struct ocfs2_quota_chunk *pos, *next;
218 list_for_each_entry_safe(pos, next, head, qc_chunk) {
219 list_del(&pos->qc_chunk);
220 brelse(pos->qc_headerbh);
221 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
225 /* Load quota bitmaps into memory */
226 static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
227 struct ocfs2_local_disk_dqinfo *ldinfo,
228 struct list_head *head)
230 struct ocfs2_quota_chunk *newchunk;
233 INIT_LIST_HEAD(head);
234 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
235 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
237 ocfs2_release_local_quota_bitmaps(head);
240 newchunk->qc_num = i;
241 newchunk->qc_headerbh = NULL;
242 status = ocfs2_read_quota_block(inode,
243 ol_quota_chunk_block(inode->i_sb, i),
244 &newchunk->qc_headerbh);
247 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
248 ocfs2_release_local_quota_bitmaps(head);
251 list_add_tail(&newchunk->qc_chunk, head);
256 static void olq_update_info(struct buffer_head *bh, void *private)
258 struct mem_dqinfo *info = private;
259 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
260 struct ocfs2_local_disk_dqinfo *ldinfo;
262 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
263 OCFS2_LOCAL_INFO_OFF);
264 spin_lock(&dq_data_lock);
265 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
266 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
267 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
268 spin_unlock(&dq_data_lock);
271 static int ocfs2_add_recovery_chunk(struct super_block *sb,
272 struct ocfs2_local_disk_chunk *dchunk,
274 struct list_head *head)
276 struct ocfs2_recovery_chunk *rc;
278 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
281 rc->rc_chunk = chunk;
282 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
283 if (!rc->rc_bitmap) {
287 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
288 (ol_chunk_entries(sb) + 7) >> 3);
289 list_add_tail(&rc->rc_list, head);
293 static void free_recovery_list(struct list_head *head)
295 struct ocfs2_recovery_chunk *next;
296 struct ocfs2_recovery_chunk *rchunk;
298 list_for_each_entry_safe(rchunk, next, head, rc_list) {
299 list_del(&rchunk->rc_list);
300 kfree(rchunk->rc_bitmap);
305 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
309 for (type = 0; type < MAXQUOTAS; type++)
310 free_recovery_list(&(rec->r_list[type]));
314 /* Load entries in our quota file we have to recover*/
315 static int ocfs2_recovery_load_quota(struct inode *lqinode,
316 struct ocfs2_local_disk_dqinfo *ldinfo,
318 struct list_head *head)
320 struct super_block *sb = lqinode->i_sb;
321 struct buffer_head *hbh;
322 struct ocfs2_local_disk_chunk *dchunk;
323 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
326 for (i = 0; i < chunks; i++) {
328 status = ocfs2_read_quota_block(lqinode,
329 ol_quota_chunk_block(sb, i),
335 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
336 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
337 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
343 free_recovery_list(head);
347 static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
350 struct ocfs2_quota_recovery *rec;
352 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
355 for (type = 0; type < MAXQUOTAS; type++)
356 INIT_LIST_HEAD(&(rec->r_list[type]));
360 /* Load information we need for quota recovery into memory */
361 struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
362 struct ocfs2_super *osb,
365 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
366 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
367 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
368 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
369 struct super_block *sb = osb->sb;
370 struct ocfs2_local_disk_dqinfo *ldinfo;
371 struct inode *lqinode;
372 struct buffer_head *bh;
375 struct ocfs2_quota_recovery *rec;
377 mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
378 rec = ocfs2_alloc_quota_recovery();
380 return ERR_PTR(-ENOMEM);
383 for (type = 0; type < MAXQUOTAS; type++) {
384 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
386 /* At this point, journal of the slot is already replayed so
387 * we can trust metadata and data of the quota file */
388 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
393 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
394 OCFS2_META_LOCK_RECOVERY);
399 /* Now read local header */
401 status = ocfs2_read_quota_block(lqinode, 0, &bh);
404 mlog(ML_ERROR, "failed to read quota file info header "
405 "(slot=%d type=%d)\n", slot_num, type);
408 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
409 OCFS2_LOCAL_INFO_OFF);
410 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
414 ocfs2_inode_unlock(lqinode, 1);
422 ocfs2_free_quota_recovery(rec);
423 rec = ERR_PTR(status);
428 /* Sync changes in local quota file into global quota file and
429 * reinitialize local quota file.
430 * The function expects local quota file to be already locked and
431 * dqonoff_mutex locked. */
432 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
434 struct ocfs2_quota_recovery *rec)
436 struct super_block *sb = lqinode->i_sb;
437 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
438 struct ocfs2_local_disk_chunk *dchunk;
439 struct ocfs2_local_disk_dqblk *dqblk;
442 struct buffer_head *hbh = NULL, *qbh = NULL;
445 struct ocfs2_recovery_chunk *rchunk, *next;
446 qsize_t spacechange, inodechange;
448 mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
450 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
451 chunk = rchunk->rc_chunk;
453 status = ocfs2_read_quota_block(lqinode,
454 ol_quota_chunk_block(sb, chunk),
460 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
461 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
463 status = ocfs2_read_quota_block(lqinode,
464 ol_dqblk_block(sb, chunk, bit),
470 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
471 ol_dqblk_block_off(sb, chunk, bit));
472 dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
475 mlog(ML_ERROR, "Failed to get quota structure "
476 "for id %u, type %d. Cannot finish quota "
478 (unsigned)le64_to_cpu(dqblk->dqb_id),
482 status = ocfs2_lock_global_qf(oinfo, 1);
488 handle = ocfs2_start_trans(OCFS2_SB(sb),
489 OCFS2_QSYNC_CREDITS);
490 if (IS_ERR(handle)) {
491 status = PTR_ERR(handle);
495 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
496 spin_lock(&dq_data_lock);
497 /* Add usage from quota entry into quota changes
498 * of our node. Auxiliary variables are important
499 * due to signedness */
500 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
501 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
502 dquot->dq_dqb.dqb_curspace += spacechange;
503 dquot->dq_dqb.dqb_curinodes += inodechange;
504 spin_unlock(&dq_data_lock);
505 /* We want to drop reference held by the crashed
506 * node. Since we have our own reference we know
507 * global structure actually won't be freed. */
508 status = ocfs2_global_release_dquot(dquot);
513 /* Release local quota file entry */
514 status = ocfs2_journal_access_dq(handle,
515 INODE_CACHE(lqinode),
516 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
522 WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
523 ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
524 le32_add_cpu(&dchunk->dqc_free, 1);
526 status = ocfs2_journal_dirty(handle, qbh);
530 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
531 ocfs2_commit_trans(OCFS2_SB(sb), handle);
533 ocfs2_unlock_global_qf(oinfo, 1);
542 list_del(&rchunk->rc_list);
543 kfree(rchunk->rc_bitmap);
549 free_recovery_list(&(rec->r_list[type]));
554 /* Recover local quota files for given node different from us */
555 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
556 struct ocfs2_quota_recovery *rec,
559 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
560 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
561 struct super_block *sb = osb->sb;
562 struct ocfs2_local_disk_dqinfo *ldinfo;
563 struct buffer_head *bh;
567 struct inode *lqinode;
570 mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
571 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
572 for (type = 0; type < MAXQUOTAS; type++) {
573 if (list_empty(&(rec->r_list[type])))
575 mlog(0, "Recovering quota in slot %d\n", slot_num);
576 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
581 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
582 OCFS2_META_LOCK_NOQUEUE);
583 /* Someone else is holding the lock? Then he must be
584 * doing the recovery. Just skip the file... */
585 if (status == -EAGAIN) {
586 mlog(ML_NOTICE, "skipping quota recovery for slot %d "
587 "because quota file is locked.\n", slot_num);
590 } else if (status < 0) {
594 /* Now read local header */
596 status = ocfs2_read_quota_block(lqinode, 0, &bh);
599 mlog(ML_ERROR, "failed to read quota file info header "
600 "(slot=%d type=%d)\n", slot_num, type);
603 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
604 OCFS2_LOCAL_INFO_OFF);
605 /* Is recovery still needed? */
606 flags = le32_to_cpu(ldinfo->dqi_flags);
607 if (!(flags & OLQF_CLEAN))
608 status = ocfs2_recover_local_quota_file(lqinode,
611 /* We don't want to mark file as clean when it is actually
613 if (slot_num == osb->slot_num)
615 /* Mark quota file as clean if we are recovering quota file of
616 * some other node. */
617 handle = ocfs2_start_trans(osb,
618 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
619 if (IS_ERR(handle)) {
620 status = PTR_ERR(handle);
624 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
626 OCFS2_JOURNAL_ACCESS_WRITE);
632 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
634 status = ocfs2_journal_dirty(handle, bh);
638 ocfs2_commit_trans(osb, handle);
642 ocfs2_inode_unlock(lqinode, 1);
649 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
654 /* Read information header from quota file */
655 static int ocfs2_local_read_info(struct super_block *sb, int type)
657 struct ocfs2_local_disk_dqinfo *ldinfo;
658 struct mem_dqinfo *info = sb_dqinfo(sb, type);
659 struct ocfs2_mem_dqinfo *oinfo;
660 struct inode *lqinode = sb_dqopt(sb)->files[type];
662 struct buffer_head *bh = NULL;
663 struct ocfs2_quota_recovery *rec;
666 /* We don't need the lock and we have to acquire quota file locks
667 * which will later depend on this lock */
668 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
669 info->dqi_maxblimit = 0x7fffffffffffffffLL;
670 info->dqi_maxilimit = 0x7fffffffffffffffLL;
671 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
673 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
677 info->dqi_priv = oinfo;
678 oinfo->dqi_type = type;
679 INIT_LIST_HEAD(&oinfo->dqi_chunk);
680 oinfo->dqi_rec = NULL;
681 oinfo->dqi_lqi_bh = NULL;
682 oinfo->dqi_ibh = NULL;
684 status = ocfs2_global_read_info(sb, type);
688 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
695 /* Now read local header */
696 status = ocfs2_read_quota_block(lqinode, 0, &bh);
699 mlog(ML_ERROR, "failed to read quota file info header "
700 "(type=%d)\n", type);
703 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
704 OCFS2_LOCAL_INFO_OFF);
705 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
706 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
707 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
710 /* We crashed when using local quota file? */
711 if (!(info->dqi_flags & OLQF_CLEAN)) {
712 rec = OCFS2_SB(sb)->quota_rec;
714 rec = ocfs2_alloc_quota_recovery();
720 OCFS2_SB(sb)->quota_rec = rec;
723 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
731 status = ocfs2_load_local_quota_bitmaps(lqinode,
739 /* Now mark quota file as used */
740 info->dqi_flags &= ~OLQF_CLEAN;
741 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
747 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
751 iput(oinfo->dqi_gqinode);
752 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
753 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
754 brelse(oinfo->dqi_lqi_bh);
756 ocfs2_inode_unlock(lqinode, 1);
757 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
761 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
765 /* Write local info to quota file */
766 static int ocfs2_local_write_info(struct super_block *sb, int type)
768 struct mem_dqinfo *info = sb_dqinfo(sb, type);
769 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
773 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
783 /* Release info from memory */
784 static int ocfs2_local_free_info(struct super_block *sb, int type)
786 struct mem_dqinfo *info = sb_dqinfo(sb, type);
787 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
788 struct ocfs2_quota_chunk *chunk;
789 struct ocfs2_local_disk_chunk *dchunk;
790 int mark_clean = 1, len;
793 /* At this point we know there are no more dquots and thus
794 * even if there's some sync in the pdflush queue, it won't
795 * find any dquots and return without doing anything */
796 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
797 iput(oinfo->dqi_gqinode);
798 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
799 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
800 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
801 dchunk = (struct ocfs2_local_disk_chunk *)
802 (chunk->qc_headerbh->b_data);
803 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
804 len = ol_chunk_entries(sb);
806 len = (oinfo->dqi_blocks -
807 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
808 * ol_quota_entries_per_block(sb);
810 /* Not all entries free? Bug! */
811 if (le32_to_cpu(dchunk->dqc_free) != len) {
812 mlog(ML_ERROR, "releasing quota file with used "
813 "entries (type=%d)\n", type);
817 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
819 /* dqonoff_mutex protects us against racing with recovery thread... */
820 if (oinfo->dqi_rec) {
821 ocfs2_free_quota_recovery(oinfo->dqi_rec);
828 /* Mark local file as clean */
829 info->dqi_flags |= OLQF_CLEAN;
830 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
840 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
841 brelse(oinfo->dqi_ibh);
842 brelse(oinfo->dqi_lqi_bh);
847 static void olq_set_dquot(struct buffer_head *bh, void *private)
849 struct ocfs2_dquot *od = private;
850 struct ocfs2_local_disk_dqblk *dqblk;
851 struct super_block *sb = od->dq_dquot.dq_sb;
853 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
854 + ol_dqblk_block_offset(sb, od->dq_local_off));
856 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
857 spin_lock(&dq_data_lock);
858 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
860 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
862 spin_unlock(&dq_data_lock);
863 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
864 od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
865 (long long)le64_to_cpu(dqblk->dqb_inodemod));
868 /* Write dquot to local quota file */
869 static int ocfs2_local_write_dquot(struct dquot *dquot)
871 struct super_block *sb = dquot->dq_sb;
872 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
873 struct buffer_head *bh = NULL;
876 status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
877 ol_dqblk_file_block(sb, od->dq_local_off),
883 status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
894 /* Find free entry in local quota file */
895 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
899 struct mem_dqinfo *info = sb_dqinfo(sb, type);
900 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
901 struct ocfs2_quota_chunk *chunk;
902 struct ocfs2_local_disk_chunk *dchunk;
905 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
906 dchunk = (struct ocfs2_local_disk_chunk *)
907 chunk->qc_headerbh->b_data;
908 if (le32_to_cpu(dchunk->dqc_free) > 0) {
916 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
917 len = ol_chunk_entries(sb);
919 len = (oinfo->dqi_blocks -
920 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
921 * ol_quota_entries_per_block(sb);
924 found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
927 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
928 " entries free (type=%d)\n", chunk->qc_num,
929 le32_to_cpu(dchunk->dqc_free), type);
930 return ERR_PTR(-EIO);
936 /* Add new chunk to the local quota file */
937 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
938 struct super_block *sb,
942 struct mem_dqinfo *info = sb_dqinfo(sb, type);
943 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
944 struct inode *lqinode = sb_dqopt(sb)->files[type];
945 struct ocfs2_quota_chunk *chunk = NULL;
946 struct ocfs2_local_disk_chunk *dchunk;
949 struct buffer_head *bh = NULL, *dbh = NULL;
952 /* We are protected by dqio_sem so no locking needed */
953 status = ocfs2_extend_no_holes(lqinode,
954 lqinode->i_size + 2 * sb->s_blocksize,
960 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
961 lqinode->i_size + 2 * sb->s_blocksize);
967 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
973 /* Local quota info and two new blocks we initialize */
974 handle = ocfs2_start_trans(OCFS2_SB(sb),
975 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
976 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
977 if (IS_ERR(handle)) {
978 status = PTR_ERR(handle);
983 /* Initialize chunk header */
984 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
985 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
986 &p_blkno, NULL, NULL);
987 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
992 bh = sb_getblk(sb, p_blkno);
998 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
999 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1000 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1001 OCFS2_JOURNAL_ACCESS_CREATE);
1007 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
1008 memset(dchunk->dqc_bitmap, 0,
1009 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1010 OCFS2_QBLK_RESERVED_SPACE);
1012 status = ocfs2_journal_dirty(handle, bh);
1018 /* Initialize new block with structures */
1019 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1020 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1021 &p_blkno, NULL, NULL);
1022 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1027 dbh = sb_getblk(sb, p_blkno);
1033 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
1034 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
1035 OCFS2_JOURNAL_ACCESS_CREATE);
1041 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1043 status = ocfs2_journal_dirty(handle, dbh);
1049 /* Update local quotafile info */
1050 oinfo->dqi_blocks += 2;
1051 oinfo->dqi_chunks++;
1052 status = ocfs2_local_write_info(sb, type);
1057 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1063 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1064 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1065 struct ocfs2_quota_chunk,
1066 qc_chunk)->qc_num + 1;
1067 chunk->qc_headerbh = bh;
1071 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1075 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1076 return ERR_PTR(status);
1079 /* Find free entry in local quota file */
1080 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1081 struct super_block *sb,
1085 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1086 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1087 struct ocfs2_quota_chunk *chunk;
1088 struct inode *lqinode = sb_dqopt(sb)->files[type];
1089 struct ocfs2_local_disk_chunk *dchunk;
1090 int epb = ol_quota_entries_per_block(sb);
1091 unsigned int chunk_blocks;
1092 struct buffer_head *bh;
1097 if (list_empty(&oinfo->dqi_chunk))
1098 return ocfs2_local_quota_add_chunk(sb, type, offset);
1099 /* Is the last chunk full? */
1100 chunk = list_entry(oinfo->dqi_chunk.prev,
1101 struct ocfs2_quota_chunk, qc_chunk);
1102 chunk_blocks = oinfo->dqi_blocks -
1103 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1104 if (ol_chunk_blocks(sb) == chunk_blocks)
1105 return ocfs2_local_quota_add_chunk(sb, type, offset);
1107 /* We are protected by dqio_sem so no locking needed */
1108 status = ocfs2_extend_no_holes(lqinode,
1109 lqinode->i_size + sb->s_blocksize,
1115 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1116 lqinode->i_size + sb->s_blocksize);
1122 /* Get buffer from the just added block */
1123 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1124 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1125 &p_blkno, NULL, NULL);
1126 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1131 bh = sb_getblk(sb, p_blkno);
1137 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1139 /* Local quota info, chunk header and the new block we initialize */
1140 handle = ocfs2_start_trans(OCFS2_SB(sb),
1141 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1142 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1143 if (IS_ERR(handle)) {
1144 status = PTR_ERR(handle);
1148 /* Zero created block */
1149 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1150 OCFS2_JOURNAL_ACCESS_CREATE);
1156 memset(bh->b_data, 0, sb->s_blocksize);
1158 status = ocfs2_journal_dirty(handle, bh);
1163 /* Update chunk header */
1164 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1166 OCFS2_JOURNAL_ACCESS_WRITE);
1172 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1173 lock_buffer(chunk->qc_headerbh);
1174 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1175 unlock_buffer(chunk->qc_headerbh);
1176 status = ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1181 /* Update file header */
1182 oinfo->dqi_blocks++;
1183 status = ocfs2_local_write_info(sb, type);
1189 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1194 *offset = chunk_blocks * epb;
1197 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1199 return ERR_PTR(status);
1202 static void olq_alloc_dquot(struct buffer_head *bh, void *private)
1204 int *offset = private;
1205 struct ocfs2_local_disk_chunk *dchunk;
1207 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1208 ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
1209 le32_add_cpu(&dchunk->dqc_free, -1);
1212 /* Create dquot in the local file for given id */
1213 static int ocfs2_create_local_dquot(struct dquot *dquot)
1215 struct super_block *sb = dquot->dq_sb;
1216 int type = dquot->dq_type;
1217 struct inode *lqinode = sb_dqopt(sb)->files[type];
1218 struct ocfs2_quota_chunk *chunk;
1219 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1223 chunk = ocfs2_find_free_entry(sb, type, &offset);
1225 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1227 return PTR_ERR(chunk);
1228 } else if (IS_ERR(chunk)) {
1229 return PTR_ERR(chunk);
1231 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1232 od->dq_chunk = chunk;
1234 /* Initialize dquot structure on disk */
1235 status = ocfs2_local_write_dquot(dquot);
1241 /* Mark structure as allocated */
1242 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1252 /* Create entry in local file for dquot, load data from the global file */
1253 static int ocfs2_local_read_dquot(struct dquot *dquot)
1257 mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
1259 status = ocfs2_global_read_dquot(dquot);
1265 /* Now create entry in the local quota file */
1266 status = ocfs2_create_local_dquot(dquot);
1278 /* Release dquot structure from local quota file. ocfs2_release_dquot() has
1279 * already started a transaction and obtained exclusive lock for global
1281 static int ocfs2_local_release_dquot(struct dquot *dquot)
1284 int type = dquot->dq_type;
1285 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1286 struct super_block *sb = dquot->dq_sb;
1287 struct ocfs2_local_disk_chunk *dchunk;
1289 handle_t *handle = journal_current_handle();
1292 /* First write all local changes to global file */
1293 status = ocfs2_global_release_dquot(dquot);
1299 status = ocfs2_journal_access_dq(handle,
1300 INODE_CACHE(sb_dqopt(sb)->files[type]),
1301 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1306 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1308 dchunk = (struct ocfs2_local_disk_chunk *)
1309 (od->dq_chunk->qc_headerbh->b_data);
1310 /* Mark structure as freed */
1311 lock_buffer(od->dq_chunk->qc_headerbh);
1312 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
1313 le32_add_cpu(&dchunk->dqc_free, 1);
1314 unlock_buffer(od->dq_chunk->qc_headerbh);
1315 status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1322 /* Clear the read bit so that next time someone uses this
1323 * dquot he reads fresh info from disk and allocates local
1324 * dquot structure */
1325 clear_bit(DQ_READ_B, &dquot->dq_flags);
1329 static const struct quota_format_ops ocfs2_format_ops = {
1330 .check_quota_file = ocfs2_local_check_quota_file,
1331 .read_file_info = ocfs2_local_read_info,
1332 .write_file_info = ocfs2_global_write_info,
1333 .free_file_info = ocfs2_local_free_info,
1334 .read_dqblk = ocfs2_local_read_dquot,
1335 .commit_dqblk = ocfs2_local_write_dquot,
1336 .release_dqblk = ocfs2_local_release_dquot,
1339 struct quota_format_type ocfs2_quota_format = {
1340 .qf_fmt_id = QFMT_OCFS2,
1341 .qf_ops = &ocfs2_format_ops,
1342 .qf_owner = THIS_MODULE