1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of operations over local quota file
7 #include <linux/slab.h>
8 #include <linux/quota.h>
9 #include <linux/quotaops.h>
10 #include <linux/module.h>
12 #include <cluster/masklog.h>
19 #include "buffer_head_io.h"
26 #include "ocfs2_trace.h"
28 /* Number of local quota structures per block */
29 static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
31 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
32 sizeof(struct ocfs2_local_disk_dqblk));
35 /* Number of blocks with entries in one chunk */
36 static inline unsigned int ol_chunk_blocks(struct super_block *sb)
38 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
39 OCFS2_QBLK_RESERVED_SPACE) << 3) /
40 ol_quota_entries_per_block(sb);
43 /* Number of entries in a chunk bitmap */
44 static unsigned int ol_chunk_entries(struct super_block *sb)
46 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
49 /* Offset of the chunk in quota file */
50 static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
52 /* 1 block for local quota file info, 1 block per chunk for chunk info */
53 return 1 + (ol_chunk_blocks(sb) + 1) * c;
56 static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
58 int epb = ol_quota_entries_per_block(sb);
60 return ol_quota_chunk_block(sb, c) + 1 + off / epb;
63 static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
65 int epb = ol_quota_entries_per_block(sb);
67 return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
70 /* Offset of the dquot structure in the quota file */
71 static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
73 return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
74 ol_dqblk_block_off(sb, c, off);
77 static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
79 return off & ((1 << sb->s_blocksize_bits) - 1);
82 /* Compute offset in the chunk of a structure with the given offset */
83 static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
85 int epb = ol_quota_entries_per_block(sb);
87 return ((off >> sb->s_blocksize_bits) -
88 ol_quota_chunk_block(sb, c) - 1) * epb
89 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
90 sizeof(struct ocfs2_local_disk_dqblk);
93 /* Write bufferhead into the fs */
94 static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
95 void (*modify)(struct buffer_head *, void *), void *private)
97 struct super_block *sb = inode->i_sb;
101 handle = ocfs2_start_trans(OCFS2_SB(sb),
102 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
103 if (IS_ERR(handle)) {
104 status = PTR_ERR(handle);
108 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
109 OCFS2_JOURNAL_ACCESS_WRITE);
112 ocfs2_commit_trans(OCFS2_SB(sb), handle);
118 ocfs2_journal_dirty(handle, bh);
120 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
129 * Read quota block from a given logical offset.
131 * This function acquires ip_alloc_sem and thus it must not be called with a
132 * transaction started.
134 static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
135 struct buffer_head **bh)
138 struct buffer_head *tmp = *bh;
140 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block)
141 return ocfs2_error(inode->i_sb,
142 "Quota file %llu is probably corrupted! Requested to read block %Lu but file has size only %Lu\n",
143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
144 (unsigned long long)v_block,
145 (unsigned long long)i_size_read(inode));
147 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
148 ocfs2_validate_quota_block);
152 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
159 /* Check whether we understand format of quota files */
160 static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
162 unsigned int lmagics[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
163 unsigned int lversions[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
164 unsigned int gmagics[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
165 unsigned int gversions[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
166 unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
167 GROUP_QUOTA_SYSTEM_INODE };
168 struct buffer_head *bh = NULL;
169 struct inode *linode = sb_dqopt(sb)->files[type];
170 struct inode *ginode = NULL;
171 struct ocfs2_disk_dqheader *dqhead;
174 /* First check whether we understand local quota file */
175 status = ocfs2_read_quota_block(linode, 0, &bh);
178 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
182 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
183 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
184 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
185 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
186 lmagics[type], type);
189 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
190 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
191 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
192 lversions[type], type);
198 /* Next check whether we understand global quota file */
199 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
202 mlog(ML_ERROR, "cannot get global quota file inode "
203 "(type=%d)\n", type);
206 /* Since the header is read only, we don't care about locking */
207 status = ocfs2_read_quota_block(ginode, 0, &bh);
210 mlog(ML_ERROR, "failed to read global quota file header "
211 "(type=%d)\n", type);
214 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
215 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
216 mlog(ML_ERROR, "global quota file magic does not match "
217 "(%u != %u), type=%d\n",
218 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
221 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
222 mlog(ML_ERROR, "global quota file version does not match "
223 "(%u != %u), type=%d\n",
224 le32_to_cpu(dqhead->dqh_version), gversions[type],
236 /* Release given list of quota file chunks */
237 static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
239 struct ocfs2_quota_chunk *pos, *next;
241 list_for_each_entry_safe(pos, next, head, qc_chunk) {
242 list_del(&pos->qc_chunk);
243 brelse(pos->qc_headerbh);
244 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
248 /* Load quota bitmaps into memory */
249 static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
250 struct ocfs2_local_disk_dqinfo *ldinfo,
251 struct list_head *head)
253 struct ocfs2_quota_chunk *newchunk;
256 INIT_LIST_HEAD(head);
257 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
258 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
260 ocfs2_release_local_quota_bitmaps(head);
263 newchunk->qc_num = i;
264 newchunk->qc_headerbh = NULL;
265 status = ocfs2_read_quota_block(inode,
266 ol_quota_chunk_block(inode->i_sb, i),
267 &newchunk->qc_headerbh);
270 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
271 ocfs2_release_local_quota_bitmaps(head);
274 list_add_tail(&newchunk->qc_chunk, head);
279 static void olq_update_info(struct buffer_head *bh, void *private)
281 struct mem_dqinfo *info = private;
282 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
283 struct ocfs2_local_disk_dqinfo *ldinfo;
285 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
286 OCFS2_LOCAL_INFO_OFF);
287 spin_lock(&dq_data_lock);
288 ldinfo->dqi_flags = cpu_to_le32(oinfo->dqi_flags);
289 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
290 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
291 spin_unlock(&dq_data_lock);
294 static int ocfs2_add_recovery_chunk(struct super_block *sb,
295 struct ocfs2_local_disk_chunk *dchunk,
297 struct list_head *head)
299 struct ocfs2_recovery_chunk *rc;
301 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
304 rc->rc_chunk = chunk;
305 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
306 if (!rc->rc_bitmap) {
310 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
311 (ol_chunk_entries(sb) + 7) >> 3);
312 list_add_tail(&rc->rc_list, head);
316 static void free_recovery_list(struct list_head *head)
318 struct ocfs2_recovery_chunk *next;
319 struct ocfs2_recovery_chunk *rchunk;
321 list_for_each_entry_safe(rchunk, next, head, rc_list) {
322 list_del(&rchunk->rc_list);
323 kfree(rchunk->rc_bitmap);
328 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
332 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
333 free_recovery_list(&(rec->r_list[type]));
337 /* Load entries in our quota file we have to recover*/
338 static int ocfs2_recovery_load_quota(struct inode *lqinode,
339 struct ocfs2_local_disk_dqinfo *ldinfo,
341 struct list_head *head)
343 struct super_block *sb = lqinode->i_sb;
344 struct buffer_head *hbh;
345 struct ocfs2_local_disk_chunk *dchunk;
346 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
349 for (i = 0; i < chunks; i++) {
351 status = ocfs2_read_quota_block(lqinode,
352 ol_quota_chunk_block(sb, i),
358 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
359 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
360 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
366 free_recovery_list(head);
370 static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
373 struct ocfs2_quota_recovery *rec;
375 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
378 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
379 INIT_LIST_HEAD(&(rec->r_list[type]));
383 /* Load information we need for quota recovery into memory */
384 struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
385 struct ocfs2_super *osb,
388 unsigned int feature[OCFS2_MAXQUOTAS] = {
389 OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
390 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
391 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
392 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
393 struct super_block *sb = osb->sb;
394 struct ocfs2_local_disk_dqinfo *ldinfo;
395 struct inode *lqinode;
396 struct buffer_head *bh;
399 struct ocfs2_quota_recovery *rec;
401 printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
402 "slot %u\n", osb->dev_str, slot_num);
404 rec = ocfs2_alloc_quota_recovery();
406 return ERR_PTR(-ENOMEM);
409 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
410 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
412 /* At this point, journal of the slot is already replayed so
413 * we can trust metadata and data of the quota file */
414 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
419 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
420 OCFS2_META_LOCK_RECOVERY);
425 /* Now read local header */
427 status = ocfs2_read_quota_block(lqinode, 0, &bh);
430 mlog(ML_ERROR, "failed to read quota file info header "
431 "(slot=%d type=%d)\n", slot_num, type);
434 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
435 OCFS2_LOCAL_INFO_OFF);
436 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
440 ocfs2_inode_unlock(lqinode, 1);
448 ocfs2_free_quota_recovery(rec);
449 rec = ERR_PTR(status);
454 /* Sync changes in local quota file into global quota file and
455 * reinitialize local quota file.
456 * The function expects local quota file to be already locked and
457 * s_umount locked in shared mode. */
458 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
460 struct ocfs2_quota_recovery *rec)
462 struct super_block *sb = lqinode->i_sb;
463 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
464 struct ocfs2_local_disk_chunk *dchunk;
465 struct ocfs2_local_disk_dqblk *dqblk;
468 struct buffer_head *hbh = NULL, *qbh = NULL;
471 struct ocfs2_recovery_chunk *rchunk, *next;
472 qsize_t spacechange, inodechange;
474 trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
476 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
477 chunk = rchunk->rc_chunk;
479 status = ocfs2_read_quota_block(lqinode,
480 ol_quota_chunk_block(sb, chunk),
486 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
487 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
489 status = ocfs2_read_quota_block(lqinode,
490 ol_dqblk_block(sb, chunk, bit),
496 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
497 ol_dqblk_block_off(sb, chunk, bit));
499 make_kqid(&init_user_ns, type,
500 le64_to_cpu(dqblk->dqb_id)));
502 status = PTR_ERR(dquot);
503 mlog(ML_ERROR, "Failed to get quota structure "
504 "for id %u, type %d. Cannot finish quota "
506 (unsigned)le64_to_cpu(dqblk->dqb_id),
510 status = ocfs2_lock_global_qf(oinfo, 1);
516 handle = ocfs2_start_trans(OCFS2_SB(sb),
517 OCFS2_QSYNC_CREDITS);
518 if (IS_ERR(handle)) {
519 status = PTR_ERR(handle);
523 down_write(&sb_dqopt(sb)->dqio_sem);
524 spin_lock(&dquot->dq_dqb_lock);
525 /* Add usage from quota entry into quota changes
526 * of our node. Auxiliary variables are important
527 * due to signedness */
528 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
529 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
530 dquot->dq_dqb.dqb_curspace += spacechange;
531 dquot->dq_dqb.dqb_curinodes += inodechange;
532 spin_unlock(&dquot->dq_dqb_lock);
533 /* We want to drop reference held by the crashed
534 * node. Since we have our own reference we know
535 * global structure actually won't be freed. */
536 status = ocfs2_global_release_dquot(dquot);
541 /* Release local quota file entry */
542 status = ocfs2_journal_access_dq(handle,
543 INODE_CACHE(lqinode),
544 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
550 WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
551 ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
552 le32_add_cpu(&dchunk->dqc_free, 1);
554 ocfs2_journal_dirty(handle, qbh);
556 up_write(&sb_dqopt(sb)->dqio_sem);
557 ocfs2_commit_trans(OCFS2_SB(sb), handle);
559 ocfs2_unlock_global_qf(oinfo, 1);
568 list_del(&rchunk->rc_list);
569 kfree(rchunk->rc_bitmap);
575 free_recovery_list(&(rec->r_list[type]));
581 /* Recover local quota files for given node different from us */
582 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
583 struct ocfs2_quota_recovery *rec,
586 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
587 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
588 struct super_block *sb = osb->sb;
589 struct ocfs2_local_disk_dqinfo *ldinfo;
590 struct buffer_head *bh;
594 struct inode *lqinode;
597 printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
598 "slot %u\n", osb->dev_str, slot_num);
600 down_read(&sb->s_umount);
601 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
602 if (list_empty(&(rec->r_list[type])))
604 trace_ocfs2_finish_quota_recovery(slot_num);
605 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
610 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
611 OCFS2_META_LOCK_NOQUEUE);
612 /* Someone else is holding the lock? Then he must be
613 * doing the recovery. Just skip the file... */
614 if (status == -EAGAIN) {
615 printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
616 "device (%s) for slot %d because quota file is "
617 "locked.\n", osb->dev_str, slot_num);
620 } else if (status < 0) {
624 /* Now read local header */
626 status = ocfs2_read_quota_block(lqinode, 0, &bh);
629 mlog(ML_ERROR, "failed to read quota file info header "
630 "(slot=%d type=%d)\n", slot_num, type);
633 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
634 OCFS2_LOCAL_INFO_OFF);
635 /* Is recovery still needed? */
636 flags = le32_to_cpu(ldinfo->dqi_flags);
637 if (!(flags & OLQF_CLEAN))
638 status = ocfs2_recover_local_quota_file(lqinode,
641 /* We don't want to mark file as clean when it is actually
643 if (slot_num == osb->slot_num)
645 /* Mark quota file as clean if we are recovering quota file of
646 * some other node. */
647 handle = ocfs2_start_trans(osb,
648 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
649 if (IS_ERR(handle)) {
650 status = PTR_ERR(handle);
654 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
656 OCFS2_JOURNAL_ACCESS_WRITE);
662 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
664 ocfs2_journal_dirty(handle, bh);
666 ocfs2_commit_trans(osb, handle);
670 ocfs2_inode_unlock(lqinode, 1);
677 up_read(&sb->s_umount);
682 /* Read information header from quota file */
683 static int ocfs2_local_read_info(struct super_block *sb, int type)
685 struct ocfs2_local_disk_dqinfo *ldinfo;
686 struct mem_dqinfo *info = sb_dqinfo(sb, type);
687 struct ocfs2_mem_dqinfo *oinfo;
688 struct inode *lqinode = sb_dqopt(sb)->files[type];
690 struct buffer_head *bh = NULL;
691 struct ocfs2_quota_recovery *rec;
694 info->dqi_max_spc_limit = 0x7fffffffffffffffLL;
695 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
696 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
698 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
702 info->dqi_priv = oinfo;
703 oinfo->dqi_type = type;
704 INIT_LIST_HEAD(&oinfo->dqi_chunk);
705 oinfo->dqi_rec = NULL;
706 oinfo->dqi_lqi_bh = NULL;
707 oinfo->dqi_libh = NULL;
709 status = ocfs2_global_read_info(sb, type);
713 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
720 /* Now read local header */
721 status = ocfs2_read_quota_block(lqinode, 0, &bh);
724 mlog(ML_ERROR, "failed to read quota file info header "
725 "(type=%d)\n", type);
728 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
729 OCFS2_LOCAL_INFO_OFF);
730 oinfo->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
731 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
732 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
733 oinfo->dqi_libh = bh;
735 /* We crashed when using local quota file? */
736 if (!(oinfo->dqi_flags & OLQF_CLEAN)) {
737 rec = OCFS2_SB(sb)->quota_rec;
739 rec = ocfs2_alloc_quota_recovery();
745 OCFS2_SB(sb)->quota_rec = rec;
748 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
756 status = ocfs2_load_local_quota_bitmaps(lqinode,
764 /* Now mark quota file as used */
765 oinfo->dqi_flags &= ~OLQF_CLEAN;
766 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
775 iput(oinfo->dqi_gqinode);
776 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
777 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
778 brelse(oinfo->dqi_lqi_bh);
780 ocfs2_inode_unlock(lqinode, 1);
781 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
788 /* Write local info to quota file */
789 static int ocfs2_local_write_info(struct super_block *sb, int type)
791 struct mem_dqinfo *info = sb_dqinfo(sb, type);
792 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
796 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
806 /* Release info from memory */
807 static int ocfs2_local_free_info(struct super_block *sb, int type)
809 struct mem_dqinfo *info = sb_dqinfo(sb, type);
810 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
811 struct ocfs2_quota_chunk *chunk;
812 struct ocfs2_local_disk_chunk *dchunk;
813 int mark_clean = 1, len;
816 iput(oinfo->dqi_gqinode);
817 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
818 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
819 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
820 dchunk = (struct ocfs2_local_disk_chunk *)
821 (chunk->qc_headerbh->b_data);
822 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
823 len = ol_chunk_entries(sb);
825 len = (oinfo->dqi_blocks -
826 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
827 * ol_quota_entries_per_block(sb);
829 /* Not all entries free? Bug! */
830 if (le32_to_cpu(dchunk->dqc_free) != len) {
831 mlog(ML_ERROR, "releasing quota file with used "
832 "entries (type=%d)\n", type);
836 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
839 * s_umount held in exclusive mode protects us against racing with
842 if (oinfo->dqi_rec) {
843 ocfs2_free_quota_recovery(oinfo->dqi_rec);
850 /* Mark local file as clean */
851 oinfo->dqi_flags |= OLQF_CLEAN;
852 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
859 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
860 brelse(oinfo->dqi_libh);
861 brelse(oinfo->dqi_lqi_bh);
866 static void olq_set_dquot(struct buffer_head *bh, void *private)
868 struct ocfs2_dquot *od = private;
869 struct ocfs2_local_disk_dqblk *dqblk;
870 struct super_block *sb = od->dq_dquot.dq_sb;
872 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
873 + ol_dqblk_block_offset(sb, od->dq_local_off));
875 dqblk->dqb_id = cpu_to_le64(from_kqid(&init_user_ns,
876 od->dq_dquot.dq_id));
877 spin_lock(&od->dq_dquot.dq_dqb_lock);
878 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
880 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
882 spin_unlock(&od->dq_dquot.dq_dqb_lock);
884 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
885 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
886 from_kqid(&init_user_ns, od->dq_dquot.dq_id));
889 /* Write dquot to local quota file */
890 int ocfs2_local_write_dquot(struct dquot *dquot)
892 struct super_block *sb = dquot->dq_sb;
893 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
894 struct buffer_head *bh;
895 struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_id.type];
898 status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
904 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
914 /* Find free entry in local quota file */
915 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
919 struct mem_dqinfo *info = sb_dqinfo(sb, type);
920 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
921 struct ocfs2_quota_chunk *chunk = NULL, *iter;
922 struct ocfs2_local_disk_chunk *dchunk;
925 list_for_each_entry(iter, &oinfo->dqi_chunk, qc_chunk) {
926 dchunk = (struct ocfs2_local_disk_chunk *)
927 iter->qc_headerbh->b_data;
928 if (le32_to_cpu(dchunk->dqc_free) > 0) {
936 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
937 len = ol_chunk_entries(sb);
939 len = (oinfo->dqi_blocks -
940 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
941 * ol_quota_entries_per_block(sb);
944 found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
947 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
948 " entries free (type=%d)\n", chunk->qc_num,
949 le32_to_cpu(dchunk->dqc_free), type);
950 return ERR_PTR(-EIO);
956 /* Add new chunk to the local quota file */
957 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
958 struct super_block *sb,
962 struct mem_dqinfo *info = sb_dqinfo(sb, type);
963 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
964 struct inode *lqinode = sb_dqopt(sb)->files[type];
965 struct ocfs2_quota_chunk *chunk = NULL;
966 struct ocfs2_local_disk_chunk *dchunk;
969 struct buffer_head *bh = NULL, *dbh = NULL;
972 /* We are protected by dqio_sem so no locking needed */
973 status = ocfs2_extend_no_holes(lqinode, NULL,
974 i_size_read(lqinode) + 2 * sb->s_blocksize,
975 i_size_read(lqinode));
980 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
981 i_size_read(lqinode) + 2 * sb->s_blocksize);
987 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
993 /* Local quota info and two new blocks we initialize */
994 handle = ocfs2_start_trans(OCFS2_SB(sb),
995 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
996 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
997 if (IS_ERR(handle)) {
998 status = PTR_ERR(handle);
1003 /* Initialize chunk header */
1004 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1005 &p_blkno, NULL, NULL);
1010 bh = sb_getblk(sb, p_blkno);
1016 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1017 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1018 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1019 OCFS2_JOURNAL_ACCESS_CREATE);
1025 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
1026 memset(dchunk->dqc_bitmap, 0,
1027 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1028 OCFS2_QBLK_RESERVED_SPACE);
1030 ocfs2_journal_dirty(handle, bh);
1032 /* Initialize new block with structures */
1033 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1034 &p_blkno, NULL, NULL);
1039 dbh = sb_getblk(sb, p_blkno);
1045 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
1046 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
1047 OCFS2_JOURNAL_ACCESS_CREATE);
1053 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1055 ocfs2_journal_dirty(handle, dbh);
1057 /* Update local quotafile info */
1058 oinfo->dqi_blocks += 2;
1059 oinfo->dqi_chunks++;
1060 status = ocfs2_local_write_info(sb, type);
1065 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1071 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1072 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1073 struct ocfs2_quota_chunk,
1074 qc_chunk)->qc_num + 1;
1075 chunk->qc_headerbh = bh;
1079 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1083 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1084 return ERR_PTR(status);
1087 /* Find free entry in local quota file */
1088 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1089 struct super_block *sb,
1093 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1094 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1095 struct ocfs2_quota_chunk *chunk;
1096 struct inode *lqinode = sb_dqopt(sb)->files[type];
1097 struct ocfs2_local_disk_chunk *dchunk;
1098 int epb = ol_quota_entries_per_block(sb);
1099 unsigned int chunk_blocks;
1100 struct buffer_head *bh;
1105 if (list_empty(&oinfo->dqi_chunk))
1106 return ocfs2_local_quota_add_chunk(sb, type, offset);
1107 /* Is the last chunk full? */
1108 chunk = list_entry(oinfo->dqi_chunk.prev,
1109 struct ocfs2_quota_chunk, qc_chunk);
1110 chunk_blocks = oinfo->dqi_blocks -
1111 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1112 if (ol_chunk_blocks(sb) == chunk_blocks)
1113 return ocfs2_local_quota_add_chunk(sb, type, offset);
1115 /* We are protected by dqio_sem so no locking needed */
1116 status = ocfs2_extend_no_holes(lqinode, NULL,
1117 i_size_read(lqinode) + sb->s_blocksize,
1118 i_size_read(lqinode));
1123 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1124 i_size_read(lqinode) + sb->s_blocksize);
1130 /* Get buffer from the just added block */
1131 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1132 &p_blkno, NULL, NULL);
1137 bh = sb_getblk(sb, p_blkno);
1143 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1145 /* Local quota info, chunk header and the new block we initialize */
1146 handle = ocfs2_start_trans(OCFS2_SB(sb),
1147 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1148 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1149 if (IS_ERR(handle)) {
1150 status = PTR_ERR(handle);
1154 /* Zero created block */
1155 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1156 OCFS2_JOURNAL_ACCESS_CREATE);
1162 memset(bh->b_data, 0, sb->s_blocksize);
1164 ocfs2_journal_dirty(handle, bh);
1166 /* Update chunk header */
1167 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1169 OCFS2_JOURNAL_ACCESS_WRITE);
1175 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1176 lock_buffer(chunk->qc_headerbh);
1177 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1178 unlock_buffer(chunk->qc_headerbh);
1179 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_unaligned(*offset, dchunk->dqc_bitmap);
1209 le32_add_cpu(&dchunk->dqc_free, -1);
1212 /* Create dquot in the local file for given id */
1213 int ocfs2_create_local_dquot(struct dquot *dquot)
1215 struct super_block *sb = dquot->dq_sb;
1216 int type = dquot->dq_id.type;
1217 struct inode *lqinode = sb_dqopt(sb)->files[type];
1218 struct ocfs2_quota_chunk *chunk;
1219 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1224 down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1225 chunk = ocfs2_find_free_entry(sb, type, &offset);
1227 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1228 if (IS_ERR(chunk)) {
1229 status = PTR_ERR(chunk);
1232 } else if (IS_ERR(chunk)) {
1233 status = PTR_ERR(chunk);
1236 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1237 od->dq_chunk = chunk;
1238 status = ocfs2_extent_map_get_blocks(lqinode,
1239 ol_dqblk_block(sb, chunk->qc_num, offset),
1240 &od->dq_local_phys_blk,
1244 /* Initialize dquot structure on disk */
1245 status = ocfs2_local_write_dquot(dquot);
1251 /* Mark structure as allocated */
1252 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1259 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1264 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1265 * already started a transaction and written all changes to global quota file
1267 int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
1270 int type = dquot->dq_id.type;
1271 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1272 struct super_block *sb = dquot->dq_sb;
1273 struct ocfs2_local_disk_chunk *dchunk;
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_unaligned(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);
1298 static const struct quota_format_ops ocfs2_format_ops = {
1299 .check_quota_file = ocfs2_local_check_quota_file,
1300 .read_file_info = ocfs2_local_read_info,
1301 .write_file_info = ocfs2_global_write_info,
1302 .free_file_info = ocfs2_local_free_info,
1305 struct quota_format_type ocfs2_quota_format = {
1306 .qf_fmt_id = QFMT_OCFS2,
1307 .qf_ops = &ocfs2_format_ops,
1308 .qf_owner = THIS_MODULE