2 * Implementation of operations over local quota file
6 #include <linux/quota.h>
7 #include <linux/quotaops.h>
8 #include <linux/module.h>
10 #define MLOG_MASK_PREFIX ML_QUOTA
11 #include <cluster/masklog.h>
18 #include "buffer_head_io.h"
25 /* Number of local quota structures per block */
26 static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
28 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
29 sizeof(struct ocfs2_local_disk_dqblk));
32 /* Number of blocks with entries in one chunk */
33 static inline unsigned int ol_chunk_blocks(struct super_block *sb)
35 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
36 OCFS2_QBLK_RESERVED_SPACE) << 3) /
37 ol_quota_entries_per_block(sb);
40 /* Number of entries in a chunk bitmap */
41 static unsigned int ol_chunk_entries(struct super_block *sb)
43 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
46 /* Offset of the chunk in quota file */
47 static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
49 /* 1 block for local quota file info, 1 block per chunk for chunk info */
50 return 1 + (ol_chunk_blocks(sb) + 1) * c;
53 static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
55 int epb = ol_quota_entries_per_block(sb);
57 return ol_quota_chunk_block(sb, c) + 1 + off / epb;
60 static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
62 int epb = ol_quota_entries_per_block(sb);
64 return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
67 /* Offset of the dquot structure in the quota file */
68 static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
70 return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
71 ol_dqblk_block_off(sb, c, off);
74 /* Compute block number from given offset */
75 static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
77 return off >> sb->s_blocksize_bits;
80 static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
82 return off & ((1 << sb->s_blocksize_bits) - 1);
85 /* Compute offset in the chunk of a structure with the given offset */
86 static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
88 int epb = ol_quota_entries_per_block(sb);
90 return ((off >> sb->s_blocksize_bits) -
91 ol_quota_chunk_block(sb, c) - 1) * epb
92 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
93 sizeof(struct ocfs2_local_disk_dqblk);
96 /* Write bufferhead into the fs */
97 static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
98 void (*modify)(struct buffer_head *, void *), void *private)
100 struct super_block *sb = inode->i_sb;
104 handle = ocfs2_start_trans(OCFS2_SB(sb),
105 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
106 if (IS_ERR(handle)) {
107 status = PTR_ERR(handle);
111 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
112 OCFS2_JOURNAL_ACCESS_WRITE);
115 ocfs2_commit_trans(OCFS2_SB(sb), handle);
121 ocfs2_journal_dirty(handle, bh);
123 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
131 /* Check whether we understand format of quota files */
132 static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
134 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
135 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
136 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
137 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
138 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
139 GROUP_QUOTA_SYSTEM_INODE };
140 struct buffer_head *bh = NULL;
141 struct inode *linode = sb_dqopt(sb)->files[type];
142 struct inode *ginode = NULL;
143 struct ocfs2_disk_dqheader *dqhead;
146 /* First check whether we understand local quota file */
147 status = ocfs2_read_quota_block(linode, 0, &bh);
150 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
154 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
155 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
156 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
157 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
158 lmagics[type], type);
161 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
162 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
163 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
164 lversions[type], type);
170 /* Next check whether we understand global quota file */
171 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
174 mlog(ML_ERROR, "cannot get global quota file inode "
175 "(type=%d)\n", type);
178 /* Since the header is read only, we don't care about locking */
179 status = ocfs2_read_quota_block(ginode, 0, &bh);
182 mlog(ML_ERROR, "failed to read global quota file header "
183 "(type=%d)\n", type);
186 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
187 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
188 mlog(ML_ERROR, "global quota file magic does not match "
189 "(%u != %u), type=%d\n",
190 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
193 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
194 mlog(ML_ERROR, "global quota file version does not match "
195 "(%u != %u), type=%d\n",
196 le32_to_cpu(dqhead->dqh_version), gversions[type],
208 /* Release given list of quota file chunks */
209 static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
211 struct ocfs2_quota_chunk *pos, *next;
213 list_for_each_entry_safe(pos, next, head, qc_chunk) {
214 list_del(&pos->qc_chunk);
215 brelse(pos->qc_headerbh);
216 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
220 /* Load quota bitmaps into memory */
221 static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
222 struct ocfs2_local_disk_dqinfo *ldinfo,
223 struct list_head *head)
225 struct ocfs2_quota_chunk *newchunk;
228 INIT_LIST_HEAD(head);
229 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
230 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
232 ocfs2_release_local_quota_bitmaps(head);
235 newchunk->qc_num = i;
236 newchunk->qc_headerbh = NULL;
237 status = ocfs2_read_quota_block(inode,
238 ol_quota_chunk_block(inode->i_sb, i),
239 &newchunk->qc_headerbh);
242 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
243 ocfs2_release_local_quota_bitmaps(head);
246 list_add_tail(&newchunk->qc_chunk, head);
251 static void olq_update_info(struct buffer_head *bh, void *private)
253 struct mem_dqinfo *info = private;
254 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
255 struct ocfs2_local_disk_dqinfo *ldinfo;
257 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
258 OCFS2_LOCAL_INFO_OFF);
259 spin_lock(&dq_data_lock);
260 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
261 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
262 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
263 spin_unlock(&dq_data_lock);
266 static int ocfs2_add_recovery_chunk(struct super_block *sb,
267 struct ocfs2_local_disk_chunk *dchunk,
269 struct list_head *head)
271 struct ocfs2_recovery_chunk *rc;
273 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
276 rc->rc_chunk = chunk;
277 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
278 if (!rc->rc_bitmap) {
282 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
283 (ol_chunk_entries(sb) + 7) >> 3);
284 list_add_tail(&rc->rc_list, head);
288 static void free_recovery_list(struct list_head *head)
290 struct ocfs2_recovery_chunk *next;
291 struct ocfs2_recovery_chunk *rchunk;
293 list_for_each_entry_safe(rchunk, next, head, rc_list) {
294 list_del(&rchunk->rc_list);
295 kfree(rchunk->rc_bitmap);
300 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
304 for (type = 0; type < MAXQUOTAS; type++)
305 free_recovery_list(&(rec->r_list[type]));
309 /* Load entries in our quota file we have to recover*/
310 static int ocfs2_recovery_load_quota(struct inode *lqinode,
311 struct ocfs2_local_disk_dqinfo *ldinfo,
313 struct list_head *head)
315 struct super_block *sb = lqinode->i_sb;
316 struct buffer_head *hbh;
317 struct ocfs2_local_disk_chunk *dchunk;
318 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
321 for (i = 0; i < chunks; i++) {
323 status = ocfs2_read_quota_block(lqinode,
324 ol_quota_chunk_block(sb, i),
330 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
331 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
332 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
338 free_recovery_list(head);
342 static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
345 struct ocfs2_quota_recovery *rec;
347 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
350 for (type = 0; type < MAXQUOTAS; type++)
351 INIT_LIST_HEAD(&(rec->r_list[type]));
355 /* Load information we need for quota recovery into memory */
356 struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
357 struct ocfs2_super *osb,
360 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
361 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
362 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
363 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
364 struct super_block *sb = osb->sb;
365 struct ocfs2_local_disk_dqinfo *ldinfo;
366 struct inode *lqinode;
367 struct buffer_head *bh;
370 struct ocfs2_quota_recovery *rec;
372 mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
373 rec = ocfs2_alloc_quota_recovery();
375 return ERR_PTR(-ENOMEM);
378 for (type = 0; type < MAXQUOTAS; type++) {
379 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
381 /* At this point, journal of the slot is already replayed so
382 * we can trust metadata and data of the quota file */
383 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
388 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
389 OCFS2_META_LOCK_RECOVERY);
394 /* Now read local header */
396 status = ocfs2_read_quota_block(lqinode, 0, &bh);
399 mlog(ML_ERROR, "failed to read quota file info header "
400 "(slot=%d type=%d)\n", slot_num, type);
403 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
404 OCFS2_LOCAL_INFO_OFF);
405 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
409 ocfs2_inode_unlock(lqinode, 1);
417 ocfs2_free_quota_recovery(rec);
418 rec = ERR_PTR(status);
423 /* Sync changes in local quota file into global quota file and
424 * reinitialize local quota file.
425 * The function expects local quota file to be already locked and
426 * dqonoff_mutex locked. */
427 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
429 struct ocfs2_quota_recovery *rec)
431 struct super_block *sb = lqinode->i_sb;
432 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
433 struct ocfs2_local_disk_chunk *dchunk;
434 struct ocfs2_local_disk_dqblk *dqblk;
437 struct buffer_head *hbh = NULL, *qbh = NULL;
440 struct ocfs2_recovery_chunk *rchunk, *next;
441 qsize_t spacechange, inodechange;
443 mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
445 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
446 chunk = rchunk->rc_chunk;
448 status = ocfs2_read_quota_block(lqinode,
449 ol_quota_chunk_block(sb, chunk),
455 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
456 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
458 status = ocfs2_read_quota_block(lqinode,
459 ol_dqblk_block(sb, chunk, bit),
465 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
466 ol_dqblk_block_off(sb, chunk, bit));
467 dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
470 mlog(ML_ERROR, "Failed to get quota structure "
471 "for id %u, type %d. Cannot finish quota "
473 (unsigned)le64_to_cpu(dqblk->dqb_id),
477 status = ocfs2_lock_global_qf(oinfo, 1);
483 handle = ocfs2_start_trans(OCFS2_SB(sb),
484 OCFS2_QSYNC_CREDITS);
485 if (IS_ERR(handle)) {
486 status = PTR_ERR(handle);
490 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
491 spin_lock(&dq_data_lock);
492 /* Add usage from quota entry into quota changes
493 * of our node. Auxiliary variables are important
494 * due to signedness */
495 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
496 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
497 dquot->dq_dqb.dqb_curspace += spacechange;
498 dquot->dq_dqb.dqb_curinodes += inodechange;
499 spin_unlock(&dq_data_lock);
500 /* We want to drop reference held by the crashed
501 * node. Since we have our own reference we know
502 * global structure actually won't be freed. */
503 status = ocfs2_global_release_dquot(dquot);
508 /* Release local quota file entry */
509 status = ocfs2_journal_access_dq(handle,
510 INODE_CACHE(lqinode),
511 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
517 WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
518 ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
519 le32_add_cpu(&dchunk->dqc_free, 1);
521 ocfs2_journal_dirty(handle, qbh);
523 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
524 ocfs2_commit_trans(OCFS2_SB(sb), handle);
526 ocfs2_unlock_global_qf(oinfo, 1);
535 list_del(&rchunk->rc_list);
536 kfree(rchunk->rc_bitmap);
542 free_recovery_list(&(rec->r_list[type]));
547 /* Recover local quota files for given node different from us */
548 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
549 struct ocfs2_quota_recovery *rec,
552 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
553 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
554 struct super_block *sb = osb->sb;
555 struct ocfs2_local_disk_dqinfo *ldinfo;
556 struct buffer_head *bh;
560 struct inode *lqinode;
563 mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
564 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
565 for (type = 0; type < MAXQUOTAS; type++) {
566 if (list_empty(&(rec->r_list[type])))
568 mlog(0, "Recovering quota in slot %d\n", slot_num);
569 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
574 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
575 OCFS2_META_LOCK_NOQUEUE);
576 /* Someone else is holding the lock? Then he must be
577 * doing the recovery. Just skip the file... */
578 if (status == -EAGAIN) {
579 mlog(ML_NOTICE, "skipping quota recovery for slot %d "
580 "because quota file is locked.\n", slot_num);
583 } else if (status < 0) {
587 /* Now read local header */
589 status = ocfs2_read_quota_block(lqinode, 0, &bh);
592 mlog(ML_ERROR, "failed to read quota file info header "
593 "(slot=%d type=%d)\n", slot_num, type);
596 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
597 OCFS2_LOCAL_INFO_OFF);
598 /* Is recovery still needed? */
599 flags = le32_to_cpu(ldinfo->dqi_flags);
600 if (!(flags & OLQF_CLEAN))
601 status = ocfs2_recover_local_quota_file(lqinode,
604 /* We don't want to mark file as clean when it is actually
606 if (slot_num == osb->slot_num)
608 /* Mark quota file as clean if we are recovering quota file of
609 * some other node. */
610 handle = ocfs2_start_trans(osb,
611 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
612 if (IS_ERR(handle)) {
613 status = PTR_ERR(handle);
617 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
619 OCFS2_JOURNAL_ACCESS_WRITE);
625 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
627 ocfs2_journal_dirty(handle, bh);
629 ocfs2_commit_trans(osb, handle);
633 ocfs2_inode_unlock(lqinode, 1);
640 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
645 /* Read information header from quota file */
646 static int ocfs2_local_read_info(struct super_block *sb, int type)
648 struct ocfs2_local_disk_dqinfo *ldinfo;
649 struct mem_dqinfo *info = sb_dqinfo(sb, type);
650 struct ocfs2_mem_dqinfo *oinfo;
651 struct inode *lqinode = sb_dqopt(sb)->files[type];
653 struct buffer_head *bh = NULL;
654 struct ocfs2_quota_recovery *rec;
657 /* We don't need the lock and we have to acquire quota file locks
658 * which will later depend on this lock */
659 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
660 info->dqi_maxblimit = 0x7fffffffffffffffLL;
661 info->dqi_maxilimit = 0x7fffffffffffffffLL;
662 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
664 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
668 info->dqi_priv = oinfo;
669 oinfo->dqi_type = type;
670 INIT_LIST_HEAD(&oinfo->dqi_chunk);
671 oinfo->dqi_rec = NULL;
672 oinfo->dqi_lqi_bh = NULL;
673 oinfo->dqi_ibh = NULL;
675 status = ocfs2_global_read_info(sb, type);
679 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
686 /* Now read local header */
687 status = ocfs2_read_quota_block(lqinode, 0, &bh);
690 mlog(ML_ERROR, "failed to read quota file info header "
691 "(type=%d)\n", type);
694 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
695 OCFS2_LOCAL_INFO_OFF);
696 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
697 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
698 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
701 /* We crashed when using local quota file? */
702 if (!(info->dqi_flags & OLQF_CLEAN)) {
703 rec = OCFS2_SB(sb)->quota_rec;
705 rec = ocfs2_alloc_quota_recovery();
711 OCFS2_SB(sb)->quota_rec = rec;
714 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
722 status = ocfs2_load_local_quota_bitmaps(lqinode,
730 /* Now mark quota file as used */
731 info->dqi_flags &= ~OLQF_CLEAN;
732 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
738 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
742 iput(oinfo->dqi_gqinode);
743 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
744 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
745 brelse(oinfo->dqi_lqi_bh);
747 ocfs2_inode_unlock(lqinode, 1);
748 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
752 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
756 /* Write local info to quota file */
757 static int ocfs2_local_write_info(struct super_block *sb, int type)
759 struct mem_dqinfo *info = sb_dqinfo(sb, type);
760 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
764 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
774 /* Release info from memory */
775 static int ocfs2_local_free_info(struct super_block *sb, int type)
777 struct mem_dqinfo *info = sb_dqinfo(sb, type);
778 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
779 struct ocfs2_quota_chunk *chunk;
780 struct ocfs2_local_disk_chunk *dchunk;
781 int mark_clean = 1, len;
784 /* At this point we know there are no more dquots and thus
785 * even if there's some sync in the pdflush queue, it won't
786 * find any dquots and return without doing anything */
787 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
788 iput(oinfo->dqi_gqinode);
789 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
790 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
791 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
792 dchunk = (struct ocfs2_local_disk_chunk *)
793 (chunk->qc_headerbh->b_data);
794 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
795 len = ol_chunk_entries(sb);
797 len = (oinfo->dqi_blocks -
798 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
799 * ol_quota_entries_per_block(sb);
801 /* Not all entries free? Bug! */
802 if (le32_to_cpu(dchunk->dqc_free) != len) {
803 mlog(ML_ERROR, "releasing quota file with used "
804 "entries (type=%d)\n", type);
808 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
810 /* dqonoff_mutex protects us against racing with recovery thread... */
811 if (oinfo->dqi_rec) {
812 ocfs2_free_quota_recovery(oinfo->dqi_rec);
819 /* Mark local file as clean */
820 info->dqi_flags |= OLQF_CLEAN;
821 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
831 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
832 brelse(oinfo->dqi_ibh);
833 brelse(oinfo->dqi_lqi_bh);
838 static void olq_set_dquot(struct buffer_head *bh, void *private)
840 struct ocfs2_dquot *od = private;
841 struct ocfs2_local_disk_dqblk *dqblk;
842 struct super_block *sb = od->dq_dquot.dq_sb;
844 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
845 + ol_dqblk_block_offset(sb, od->dq_local_off));
847 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
848 spin_lock(&dq_data_lock);
849 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
851 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
853 spin_unlock(&dq_data_lock);
854 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
855 od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
856 (long long)le64_to_cpu(dqblk->dqb_inodemod));
859 /* Write dquot to local quota file */
860 static int ocfs2_local_write_dquot(struct dquot *dquot)
862 struct super_block *sb = dquot->dq_sb;
863 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
864 struct buffer_head *bh = NULL;
867 status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
868 ol_dqblk_file_block(sb, od->dq_local_off),
874 status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
885 /* Find free entry in local quota file */
886 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
890 struct mem_dqinfo *info = sb_dqinfo(sb, type);
891 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
892 struct ocfs2_quota_chunk *chunk;
893 struct ocfs2_local_disk_chunk *dchunk;
896 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
897 dchunk = (struct ocfs2_local_disk_chunk *)
898 chunk->qc_headerbh->b_data;
899 if (le32_to_cpu(dchunk->dqc_free) > 0) {
907 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
908 len = ol_chunk_entries(sb);
910 len = (oinfo->dqi_blocks -
911 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
912 * ol_quota_entries_per_block(sb);
915 found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
918 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
919 " entries free (type=%d)\n", chunk->qc_num,
920 le32_to_cpu(dchunk->dqc_free), type);
921 return ERR_PTR(-EIO);
927 /* Add new chunk to the local quota file */
928 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
929 struct super_block *sb,
933 struct mem_dqinfo *info = sb_dqinfo(sb, type);
934 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
935 struct inode *lqinode = sb_dqopt(sb)->files[type];
936 struct ocfs2_quota_chunk *chunk = NULL;
937 struct ocfs2_local_disk_chunk *dchunk;
940 struct buffer_head *bh = NULL, *dbh = NULL;
943 /* We are protected by dqio_sem so no locking needed */
944 status = ocfs2_extend_no_holes(lqinode,
945 lqinode->i_size + 2 * sb->s_blocksize,
951 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
952 lqinode->i_size + 2 * sb->s_blocksize);
958 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
964 /* Local quota info and two new blocks we initialize */
965 handle = ocfs2_start_trans(OCFS2_SB(sb),
966 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
967 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
968 if (IS_ERR(handle)) {
969 status = PTR_ERR(handle);
974 /* Initialize chunk header */
975 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
976 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
977 &p_blkno, NULL, NULL);
978 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
983 bh = sb_getblk(sb, p_blkno);
989 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
990 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
991 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
992 OCFS2_JOURNAL_ACCESS_CREATE);
998 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
999 memset(dchunk->dqc_bitmap, 0,
1000 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1001 OCFS2_QBLK_RESERVED_SPACE);
1003 ocfs2_journal_dirty(handle, bh);
1005 /* Initialize new block with structures */
1006 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1007 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1008 &p_blkno, NULL, NULL);
1009 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1014 dbh = sb_getblk(sb, p_blkno);
1020 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
1021 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
1022 OCFS2_JOURNAL_ACCESS_CREATE);
1028 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1030 ocfs2_journal_dirty(handle, dbh);
1032 /* Update local quotafile info */
1033 oinfo->dqi_blocks += 2;
1034 oinfo->dqi_chunks++;
1035 status = ocfs2_local_write_info(sb, type);
1040 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1046 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1047 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1048 struct ocfs2_quota_chunk,
1049 qc_chunk)->qc_num + 1;
1050 chunk->qc_headerbh = bh;
1054 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1058 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1059 return ERR_PTR(status);
1062 /* Find free entry in local quota file */
1063 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1064 struct super_block *sb,
1068 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1069 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1070 struct ocfs2_quota_chunk *chunk;
1071 struct inode *lqinode = sb_dqopt(sb)->files[type];
1072 struct ocfs2_local_disk_chunk *dchunk;
1073 int epb = ol_quota_entries_per_block(sb);
1074 unsigned int chunk_blocks;
1075 struct buffer_head *bh;
1080 if (list_empty(&oinfo->dqi_chunk))
1081 return ocfs2_local_quota_add_chunk(sb, type, offset);
1082 /* Is the last chunk full? */
1083 chunk = list_entry(oinfo->dqi_chunk.prev,
1084 struct ocfs2_quota_chunk, qc_chunk);
1085 chunk_blocks = oinfo->dqi_blocks -
1086 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1087 if (ol_chunk_blocks(sb) == chunk_blocks)
1088 return ocfs2_local_quota_add_chunk(sb, type, offset);
1090 /* We are protected by dqio_sem so no locking needed */
1091 status = ocfs2_extend_no_holes(lqinode,
1092 lqinode->i_size + sb->s_blocksize,
1098 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1099 lqinode->i_size + sb->s_blocksize);
1105 /* Get buffer from the just added block */
1106 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1107 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1108 &p_blkno, NULL, NULL);
1109 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
1114 bh = sb_getblk(sb, p_blkno);
1120 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1122 /* Local quota info, chunk header and the new block we initialize */
1123 handle = ocfs2_start_trans(OCFS2_SB(sb),
1124 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1125 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1126 if (IS_ERR(handle)) {
1127 status = PTR_ERR(handle);
1131 /* Zero created block */
1132 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1133 OCFS2_JOURNAL_ACCESS_CREATE);
1139 memset(bh->b_data, 0, sb->s_blocksize);
1141 ocfs2_journal_dirty(handle, bh);
1143 /* Update chunk header */
1144 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1146 OCFS2_JOURNAL_ACCESS_WRITE);
1152 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1153 lock_buffer(chunk->qc_headerbh);
1154 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1155 unlock_buffer(chunk->qc_headerbh);
1156 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1158 /* Update file header */
1159 oinfo->dqi_blocks++;
1160 status = ocfs2_local_write_info(sb, type);
1166 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1171 *offset = chunk_blocks * epb;
1174 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1176 return ERR_PTR(status);
1179 static void olq_alloc_dquot(struct buffer_head *bh, void *private)
1181 int *offset = private;
1182 struct ocfs2_local_disk_chunk *dchunk;
1184 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1185 ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
1186 le32_add_cpu(&dchunk->dqc_free, -1);
1189 /* Create dquot in the local file for given id */
1190 static int ocfs2_create_local_dquot(struct dquot *dquot)
1192 struct super_block *sb = dquot->dq_sb;
1193 int type = dquot->dq_type;
1194 struct inode *lqinode = sb_dqopt(sb)->files[type];
1195 struct ocfs2_quota_chunk *chunk;
1196 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1200 chunk = ocfs2_find_free_entry(sb, type, &offset);
1202 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1204 return PTR_ERR(chunk);
1205 } else if (IS_ERR(chunk)) {
1206 return PTR_ERR(chunk);
1208 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1209 od->dq_chunk = chunk;
1211 /* Initialize dquot structure on disk */
1212 status = ocfs2_local_write_dquot(dquot);
1218 /* Mark structure as allocated */
1219 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1229 /* Create entry in local file for dquot, load data from the global file */
1230 static int ocfs2_local_read_dquot(struct dquot *dquot)
1234 mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
1236 status = ocfs2_global_read_dquot(dquot);
1242 /* Now create entry in the local quota file */
1243 status = ocfs2_create_local_dquot(dquot);
1255 /* Release dquot structure from local quota file. ocfs2_release_dquot() has
1256 * already started a transaction and obtained exclusive lock for global
1258 static int ocfs2_local_release_dquot(struct dquot *dquot)
1261 int type = dquot->dq_type;
1262 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1263 struct super_block *sb = dquot->dq_sb;
1264 struct ocfs2_local_disk_chunk *dchunk;
1266 handle_t *handle = journal_current_handle();
1269 /* First write all local changes to global file */
1270 status = ocfs2_global_release_dquot(dquot);
1276 status = ocfs2_journal_access_dq(handle,
1277 INODE_CACHE(sb_dqopt(sb)->files[type]),
1278 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1283 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1285 dchunk = (struct ocfs2_local_disk_chunk *)
1286 (od->dq_chunk->qc_headerbh->b_data);
1287 /* Mark structure as freed */
1288 lock_buffer(od->dq_chunk->qc_headerbh);
1289 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
1290 le32_add_cpu(&dchunk->dqc_free, 1);
1291 unlock_buffer(od->dq_chunk->qc_headerbh);
1292 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1295 /* Clear the read bit so that next time someone uses this
1296 * dquot he reads fresh info from disk and allocates local
1297 * dquot structure */
1298 clear_bit(DQ_READ_B, &dquot->dq_flags);
1302 static const struct quota_format_ops ocfs2_format_ops = {
1303 .check_quota_file = ocfs2_local_check_quota_file,
1304 .read_file_info = ocfs2_local_read_info,
1305 .write_file_info = ocfs2_global_write_info,
1306 .free_file_info = ocfs2_local_free_info,
1307 .read_dqblk = ocfs2_local_read_dquot,
1308 .commit_dqblk = ocfs2_local_write_dquot,
1309 .release_dqblk = ocfs2_local_release_dquot,
1312 struct quota_format_type ocfs2_quota_format = {
1313 .qf_fmt_id = QFMT_OCFS2,
1314 .qf_ops = &ocfs2_format_ops,
1315 .qf_owner = THIS_MODULE