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 #include <cluster/masklog.h>
18 #include "buffer_head_io.h"
25 #include "ocfs2_trace.h"
27 /* Number of local quota structures per block */
28 static inline unsigned int ol_quota_entries_per_block(struct super_block
*sb
)
30 return ((sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
) /
31 sizeof(struct ocfs2_local_disk_dqblk
));
34 /* Number of blocks with entries in one chunk */
35 static inline unsigned int ol_chunk_blocks(struct super_block
*sb
)
37 return ((sb
->s_blocksize
- sizeof(struct ocfs2_local_disk_chunk
) -
38 OCFS2_QBLK_RESERVED_SPACE
) << 3) /
39 ol_quota_entries_per_block(sb
);
42 /* Number of entries in a chunk bitmap */
43 static unsigned int ol_chunk_entries(struct super_block
*sb
)
45 return ol_chunk_blocks(sb
) * ol_quota_entries_per_block(sb
);
48 /* Offset of the chunk in quota file */
49 static unsigned int ol_quota_chunk_block(struct super_block
*sb
, int c
)
51 /* 1 block for local quota file info, 1 block per chunk for chunk info */
52 return 1 + (ol_chunk_blocks(sb
) + 1) * c
;
55 static unsigned int ol_dqblk_block(struct super_block
*sb
, int c
, int off
)
57 int epb
= ol_quota_entries_per_block(sb
);
59 return ol_quota_chunk_block(sb
, c
) + 1 + off
/ epb
;
62 static unsigned int ol_dqblk_block_off(struct super_block
*sb
, int c
, int off
)
64 int epb
= ol_quota_entries_per_block(sb
);
66 return (off
% epb
) * sizeof(struct ocfs2_local_disk_dqblk
);
69 /* Offset of the dquot structure in the quota file */
70 static loff_t
ol_dqblk_off(struct super_block
*sb
, int c
, int off
)
72 return (ol_dqblk_block(sb
, c
, off
) << sb
->s_blocksize_bits
) +
73 ol_dqblk_block_off(sb
, c
, off
);
76 static inline unsigned int ol_dqblk_block_offset(struct super_block
*sb
, loff_t off
)
78 return off
& ((1 << sb
->s_blocksize_bits
) - 1);
81 /* Compute offset in the chunk of a structure with the given offset */
82 static int ol_dqblk_chunk_off(struct super_block
*sb
, int c
, loff_t off
)
84 int epb
= ol_quota_entries_per_block(sb
);
86 return ((off
>> sb
->s_blocksize_bits
) -
87 ol_quota_chunk_block(sb
, c
) - 1) * epb
88 + ((unsigned int)(off
& ((1 << sb
->s_blocksize_bits
) - 1))) /
89 sizeof(struct ocfs2_local_disk_dqblk
);
92 /* Write bufferhead into the fs */
93 static int ocfs2_modify_bh(struct inode
*inode
, struct buffer_head
*bh
,
94 void (*modify
)(struct buffer_head
*, void *), void *private)
96 struct super_block
*sb
= inode
->i_sb
;
100 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
101 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
);
102 if (IS_ERR(handle
)) {
103 status
= PTR_ERR(handle
);
107 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(inode
), bh
,
108 OCFS2_JOURNAL_ACCESS_WRITE
);
111 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
117 ocfs2_journal_dirty(handle
, bh
);
119 status
= ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
128 * Read quota block from a given logical offset.
130 * This function acquires ip_alloc_sem and thus it must not be called with a
131 * transaction started.
133 static int ocfs2_read_quota_block(struct inode
*inode
, u64 v_block
,
134 struct buffer_head
**bh
)
137 struct buffer_head
*tmp
= *bh
;
139 if (i_size_read(inode
) >> inode
->i_sb
->s_blocksize_bits
<= v_block
) {
140 ocfs2_error(inode
->i_sb
,
141 "Quota file %llu is probably corrupted! Requested "
142 "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
));
148 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, 0,
149 ocfs2_validate_quota_block
);
153 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
160 /* Check whether we understand format of quota files */
161 static int ocfs2_local_check_quota_file(struct super_block
*sb
, int type
)
163 unsigned int lmagics
[OCFS2_MAXQUOTAS
] = OCFS2_LOCAL_QMAGICS
;
164 unsigned int lversions
[OCFS2_MAXQUOTAS
] = OCFS2_LOCAL_QVERSIONS
;
165 unsigned int gmagics
[OCFS2_MAXQUOTAS
] = OCFS2_GLOBAL_QMAGICS
;
166 unsigned int gversions
[OCFS2_MAXQUOTAS
] = OCFS2_GLOBAL_QVERSIONS
;
167 unsigned int ino
[OCFS2_MAXQUOTAS
] = { USER_QUOTA_SYSTEM_INODE
,
168 GROUP_QUOTA_SYSTEM_INODE
};
169 struct buffer_head
*bh
= NULL
;
170 struct inode
*linode
= sb_dqopt(sb
)->files
[type
];
171 struct inode
*ginode
= NULL
;
172 struct ocfs2_disk_dqheader
*dqhead
;
175 /* First check whether we understand local quota file */
176 status
= ocfs2_read_quota_block(linode
, 0, &bh
);
179 mlog(ML_ERROR
, "failed to read quota file header (type=%d)\n",
183 dqhead
= (struct ocfs2_disk_dqheader
*)(bh
->b_data
);
184 if (le32_to_cpu(dqhead
->dqh_magic
) != lmagics
[type
]) {
185 mlog(ML_ERROR
, "quota file magic does not match (%u != %u),"
186 " type=%d\n", le32_to_cpu(dqhead
->dqh_magic
),
187 lmagics
[type
], type
);
190 if (le32_to_cpu(dqhead
->dqh_version
) != lversions
[type
]) {
191 mlog(ML_ERROR
, "quota file version does not match (%u != %u),"
192 " type=%d\n", le32_to_cpu(dqhead
->dqh_version
),
193 lversions
[type
], type
);
199 /* Next check whether we understand global quota file */
200 ginode
= ocfs2_get_system_file_inode(OCFS2_SB(sb
), ino
[type
],
203 mlog(ML_ERROR
, "cannot get global quota file inode "
204 "(type=%d)\n", type
);
207 /* Since the header is read only, we don't care about locking */
208 status
= ocfs2_read_quota_block(ginode
, 0, &bh
);
211 mlog(ML_ERROR
, "failed to read global quota file header "
212 "(type=%d)\n", type
);
215 dqhead
= (struct ocfs2_disk_dqheader
*)(bh
->b_data
);
216 if (le32_to_cpu(dqhead
->dqh_magic
) != gmagics
[type
]) {
217 mlog(ML_ERROR
, "global quota file magic does not match "
218 "(%u != %u), type=%d\n",
219 le32_to_cpu(dqhead
->dqh_magic
), gmagics
[type
], type
);
222 if (le32_to_cpu(dqhead
->dqh_version
) != gversions
[type
]) {
223 mlog(ML_ERROR
, "global quota file version does not match "
224 "(%u != %u), type=%d\n",
225 le32_to_cpu(dqhead
->dqh_version
), gversions
[type
],
237 /* Release given list of quota file chunks */
238 static void ocfs2_release_local_quota_bitmaps(struct list_head
*head
)
240 struct ocfs2_quota_chunk
*pos
, *next
;
242 list_for_each_entry_safe(pos
, next
, head
, qc_chunk
) {
243 list_del(&pos
->qc_chunk
);
244 brelse(pos
->qc_headerbh
);
245 kmem_cache_free(ocfs2_qf_chunk_cachep
, pos
);
249 /* Load quota bitmaps into memory */
250 static int ocfs2_load_local_quota_bitmaps(struct inode
*inode
,
251 struct ocfs2_local_disk_dqinfo
*ldinfo
,
252 struct list_head
*head
)
254 struct ocfs2_quota_chunk
*newchunk
;
257 INIT_LIST_HEAD(head
);
258 for (i
= 0; i
< le32_to_cpu(ldinfo
->dqi_chunks
); i
++) {
259 newchunk
= kmem_cache_alloc(ocfs2_qf_chunk_cachep
, GFP_NOFS
);
261 ocfs2_release_local_quota_bitmaps(head
);
264 newchunk
->qc_num
= i
;
265 newchunk
->qc_headerbh
= NULL
;
266 status
= ocfs2_read_quota_block(inode
,
267 ol_quota_chunk_block(inode
->i_sb
, i
),
268 &newchunk
->qc_headerbh
);
271 kmem_cache_free(ocfs2_qf_chunk_cachep
, newchunk
);
272 ocfs2_release_local_quota_bitmaps(head
);
275 list_add_tail(&newchunk
->qc_chunk
, head
);
280 static void olq_update_info(struct buffer_head
*bh
, void *private)
282 struct mem_dqinfo
*info
= private;
283 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
284 struct ocfs2_local_disk_dqinfo
*ldinfo
;
286 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
287 OCFS2_LOCAL_INFO_OFF
);
288 spin_lock(&dq_data_lock
);
289 ldinfo
->dqi_flags
= cpu_to_le32(oinfo
->dqi_flags
);
290 ldinfo
->dqi_chunks
= cpu_to_le32(oinfo
->dqi_chunks
);
291 ldinfo
->dqi_blocks
= cpu_to_le32(oinfo
->dqi_blocks
);
292 spin_unlock(&dq_data_lock
);
295 static int ocfs2_add_recovery_chunk(struct super_block
*sb
,
296 struct ocfs2_local_disk_chunk
*dchunk
,
298 struct list_head
*head
)
300 struct ocfs2_recovery_chunk
*rc
;
302 rc
= kmalloc(sizeof(struct ocfs2_recovery_chunk
), GFP_NOFS
);
305 rc
->rc_chunk
= chunk
;
306 rc
->rc_bitmap
= kmalloc(sb
->s_blocksize
, GFP_NOFS
);
307 if (!rc
->rc_bitmap
) {
311 memcpy(rc
->rc_bitmap
, dchunk
->dqc_bitmap
,
312 (ol_chunk_entries(sb
) + 7) >> 3);
313 list_add_tail(&rc
->rc_list
, head
);
317 static void free_recovery_list(struct list_head
*head
)
319 struct ocfs2_recovery_chunk
*next
;
320 struct ocfs2_recovery_chunk
*rchunk
;
322 list_for_each_entry_safe(rchunk
, next
, head
, rc_list
) {
323 list_del(&rchunk
->rc_list
);
324 kfree(rchunk
->rc_bitmap
);
329 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery
*rec
)
333 for (type
= 0; type
< OCFS2_MAXQUOTAS
; type
++)
334 free_recovery_list(&(rec
->r_list
[type
]));
338 /* Load entries in our quota file we have to recover*/
339 static int ocfs2_recovery_load_quota(struct inode
*lqinode
,
340 struct ocfs2_local_disk_dqinfo
*ldinfo
,
342 struct list_head
*head
)
344 struct super_block
*sb
= lqinode
->i_sb
;
345 struct buffer_head
*hbh
;
346 struct ocfs2_local_disk_chunk
*dchunk
;
347 int i
, chunks
= le32_to_cpu(ldinfo
->dqi_chunks
);
350 for (i
= 0; i
< chunks
; i
++) {
352 status
= ocfs2_read_quota_block(lqinode
,
353 ol_quota_chunk_block(sb
, i
),
359 dchunk
= (struct ocfs2_local_disk_chunk
*)hbh
->b_data
;
360 if (le32_to_cpu(dchunk
->dqc_free
) < ol_chunk_entries(sb
))
361 status
= ocfs2_add_recovery_chunk(sb
, dchunk
, i
, head
);
367 free_recovery_list(head
);
371 static struct ocfs2_quota_recovery
*ocfs2_alloc_quota_recovery(void)
374 struct ocfs2_quota_recovery
*rec
;
376 rec
= kmalloc(sizeof(struct ocfs2_quota_recovery
), GFP_NOFS
);
379 for (type
= 0; type
< OCFS2_MAXQUOTAS
; type
++)
380 INIT_LIST_HEAD(&(rec
->r_list
[type
]));
384 /* Load information we need for quota recovery into memory */
385 struct ocfs2_quota_recovery
*ocfs2_begin_quota_recovery(
386 struct ocfs2_super
*osb
,
389 unsigned int feature
[OCFS2_MAXQUOTAS
] = {
390 OCFS2_FEATURE_RO_COMPAT_USRQUOTA
,
391 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA
};
392 unsigned int ino
[OCFS2_MAXQUOTAS
] = { LOCAL_USER_QUOTA_SYSTEM_INODE
,
393 LOCAL_GROUP_QUOTA_SYSTEM_INODE
};
394 struct super_block
*sb
= osb
->sb
;
395 struct ocfs2_local_disk_dqinfo
*ldinfo
;
396 struct inode
*lqinode
;
397 struct buffer_head
*bh
;
400 struct ocfs2_quota_recovery
*rec
;
402 printk(KERN_NOTICE
"ocfs2: Beginning quota recovery on device (%s) for "
403 "slot %u\n", osb
->dev_str
, slot_num
);
405 rec
= ocfs2_alloc_quota_recovery();
407 return ERR_PTR(-ENOMEM
);
410 for (type
= 0; type
< OCFS2_MAXQUOTAS
; type
++) {
411 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb
, feature
[type
]))
413 /* At this point, journal of the slot is already replayed so
414 * we can trust metadata and data of the quota file */
415 lqinode
= ocfs2_get_system_file_inode(osb
, ino
[type
], slot_num
);
420 status
= ocfs2_inode_lock_full(lqinode
, NULL
, 1,
421 OCFS2_META_LOCK_RECOVERY
);
426 /* Now read local header */
428 status
= ocfs2_read_quota_block(lqinode
, 0, &bh
);
431 mlog(ML_ERROR
, "failed to read quota file info header "
432 "(slot=%d type=%d)\n", slot_num
, type
);
435 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
436 OCFS2_LOCAL_INFO_OFF
);
437 status
= ocfs2_recovery_load_quota(lqinode
, ldinfo
, type
,
441 ocfs2_inode_unlock(lqinode
, 1);
449 ocfs2_free_quota_recovery(rec
);
450 rec
= ERR_PTR(status
);
455 /* Sync changes in local quota file into global quota file and
456 * reinitialize local quota file.
457 * The function expects local quota file to be already locked and
458 * dqonoff_mutex locked. */
459 static int ocfs2_recover_local_quota_file(struct inode
*lqinode
,
461 struct ocfs2_quota_recovery
*rec
)
463 struct super_block
*sb
= lqinode
->i_sb
;
464 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
465 struct ocfs2_local_disk_chunk
*dchunk
;
466 struct ocfs2_local_disk_dqblk
*dqblk
;
469 struct buffer_head
*hbh
= NULL
, *qbh
= NULL
;
472 struct ocfs2_recovery_chunk
*rchunk
, *next
;
473 qsize_t spacechange
, inodechange
;
475 trace_ocfs2_recover_local_quota_file((unsigned long)lqinode
->i_ino
, type
);
477 list_for_each_entry_safe(rchunk
, next
, &(rec
->r_list
[type
]), rc_list
) {
478 chunk
= rchunk
->rc_chunk
;
480 status
= ocfs2_read_quota_block(lqinode
,
481 ol_quota_chunk_block(sb
, chunk
),
487 dchunk
= (struct ocfs2_local_disk_chunk
*)hbh
->b_data
;
488 for_each_set_bit(bit
, rchunk
->rc_bitmap
, ol_chunk_entries(sb
)) {
490 status
= ocfs2_read_quota_block(lqinode
,
491 ol_dqblk_block(sb
, chunk
, bit
),
497 dqblk
= (struct ocfs2_local_disk_dqblk
*)(qbh
->b_data
+
498 ol_dqblk_block_off(sb
, chunk
, bit
));
500 make_kqid(&init_user_ns
, type
,
501 le64_to_cpu(dqblk
->dqb_id
)));
504 mlog(ML_ERROR
, "Failed to get quota structure "
505 "for id %u, type %d. Cannot finish quota "
507 (unsigned)le64_to_cpu(dqblk
->dqb_id
),
511 status
= ocfs2_lock_global_qf(oinfo
, 1);
517 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
518 OCFS2_QSYNC_CREDITS
);
519 if (IS_ERR(handle
)) {
520 status
= PTR_ERR(handle
);
524 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
525 spin_lock(&dq_data_lock
);
526 /* Add usage from quota entry into quota changes
527 * of our node. Auxiliary variables are important
528 * due to signedness */
529 spacechange
= le64_to_cpu(dqblk
->dqb_spacemod
);
530 inodechange
= le64_to_cpu(dqblk
->dqb_inodemod
);
531 dquot
->dq_dqb
.dqb_curspace
+= spacechange
;
532 dquot
->dq_dqb
.dqb_curinodes
+= inodechange
;
533 spin_unlock(&dq_data_lock
);
534 /* We want to drop reference held by the crashed
535 * node. Since we have our own reference we know
536 * global structure actually won't be freed. */
537 status
= ocfs2_global_release_dquot(dquot
);
542 /* Release local quota file entry */
543 status
= ocfs2_journal_access_dq(handle
,
544 INODE_CACHE(lqinode
),
545 qbh
, OCFS2_JOURNAL_ACCESS_WRITE
);
551 WARN_ON(!ocfs2_test_bit_unaligned(bit
, dchunk
->dqc_bitmap
));
552 ocfs2_clear_bit_unaligned(bit
, dchunk
->dqc_bitmap
);
553 le32_add_cpu(&dchunk
->dqc_free
, 1);
555 ocfs2_journal_dirty(handle
, qbh
);
557 mutex_unlock(&sb_dqopt(sb
)->dqio_mutex
);
558 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
560 ocfs2_unlock_global_qf(oinfo
, 1);
569 list_del(&rchunk
->rc_list
);
570 kfree(rchunk
->rc_bitmap
);
576 free_recovery_list(&(rec
->r_list
[type
]));
582 /* Recover local quota files for given node different from us */
583 int ocfs2_finish_quota_recovery(struct ocfs2_super
*osb
,
584 struct ocfs2_quota_recovery
*rec
,
587 unsigned int ino
[OCFS2_MAXQUOTAS
] = { LOCAL_USER_QUOTA_SYSTEM_INODE
,
588 LOCAL_GROUP_QUOTA_SYSTEM_INODE
};
589 struct super_block
*sb
= osb
->sb
;
590 struct ocfs2_local_disk_dqinfo
*ldinfo
;
591 struct buffer_head
*bh
;
595 struct inode
*lqinode
;
598 printk(KERN_NOTICE
"ocfs2: Finishing quota recovery on device (%s) for "
599 "slot %u\n", osb
->dev_str
, slot_num
);
601 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
602 for (type
= 0; type
< OCFS2_MAXQUOTAS
; type
++) {
603 if (list_empty(&(rec
->r_list
[type
])))
605 trace_ocfs2_finish_quota_recovery(slot_num
);
606 lqinode
= ocfs2_get_system_file_inode(osb
, ino
[type
], slot_num
);
611 status
= ocfs2_inode_lock_full(lqinode
, NULL
, 1,
612 OCFS2_META_LOCK_NOQUEUE
);
613 /* Someone else is holding the lock? Then he must be
614 * doing the recovery. Just skip the file... */
615 if (status
== -EAGAIN
) {
616 printk(KERN_NOTICE
"ocfs2: Skipping quota recovery on "
617 "device (%s) for slot %d because quota file is "
618 "locked.\n", osb
->dev_str
, slot_num
);
621 } else if (status
< 0) {
625 /* Now read local header */
627 status
= ocfs2_read_quota_block(lqinode
, 0, &bh
);
630 mlog(ML_ERROR
, "failed to read quota file info header "
631 "(slot=%d type=%d)\n", slot_num
, type
);
634 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
635 OCFS2_LOCAL_INFO_OFF
);
636 /* Is recovery still needed? */
637 flags
= le32_to_cpu(ldinfo
->dqi_flags
);
638 if (!(flags
& OLQF_CLEAN
))
639 status
= ocfs2_recover_local_quota_file(lqinode
,
642 /* We don't want to mark file as clean when it is actually
644 if (slot_num
== osb
->slot_num
)
646 /* Mark quota file as clean if we are recovering quota file of
647 * some other node. */
648 handle
= ocfs2_start_trans(osb
,
649 OCFS2_LOCAL_QINFO_WRITE_CREDITS
);
650 if (IS_ERR(handle
)) {
651 status
= PTR_ERR(handle
);
655 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
),
657 OCFS2_JOURNAL_ACCESS_WRITE
);
663 ldinfo
->dqi_flags
= cpu_to_le32(flags
| OLQF_CLEAN
);
665 ocfs2_journal_dirty(handle
, bh
);
667 ocfs2_commit_trans(osb
, handle
);
671 ocfs2_inode_unlock(lqinode
, 1);
678 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
683 /* Read information header from quota file */
684 static int ocfs2_local_read_info(struct super_block
*sb
, int type
)
686 struct ocfs2_local_disk_dqinfo
*ldinfo
;
687 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
688 struct ocfs2_mem_dqinfo
*oinfo
;
689 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
691 struct buffer_head
*bh
= NULL
;
692 struct ocfs2_quota_recovery
*rec
;
695 /* We don't need the lock and we have to acquire quota file locks
696 * which will later depend on this lock */
697 mutex_unlock(&sb_dqopt(sb
)->dqio_mutex
);
698 info
->dqi_max_spc_limit
= 0x7fffffffffffffffLL
;
699 info
->dqi_max_ino_limit
= 0x7fffffffffffffffLL
;
700 oinfo
= kmalloc(sizeof(struct ocfs2_mem_dqinfo
), GFP_NOFS
);
702 mlog(ML_ERROR
, "failed to allocate memory for ocfs2 quota"
706 info
->dqi_priv
= oinfo
;
707 oinfo
->dqi_type
= type
;
708 INIT_LIST_HEAD(&oinfo
->dqi_chunk
);
709 oinfo
->dqi_rec
= NULL
;
710 oinfo
->dqi_lqi_bh
= NULL
;
711 oinfo
->dqi_libh
= NULL
;
713 status
= ocfs2_global_read_info(sb
, type
);
717 status
= ocfs2_inode_lock(lqinode
, &oinfo
->dqi_lqi_bh
, 1);
724 /* Now read local header */
725 status
= ocfs2_read_quota_block(lqinode
, 0, &bh
);
728 mlog(ML_ERROR
, "failed to read quota file info header "
729 "(type=%d)\n", type
);
732 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
733 OCFS2_LOCAL_INFO_OFF
);
734 oinfo
->dqi_flags
= le32_to_cpu(ldinfo
->dqi_flags
);
735 oinfo
->dqi_chunks
= le32_to_cpu(ldinfo
->dqi_chunks
);
736 oinfo
->dqi_blocks
= le32_to_cpu(ldinfo
->dqi_blocks
);
737 oinfo
->dqi_libh
= bh
;
739 /* We crashed when using local quota file? */
740 if (!(oinfo
->dqi_flags
& OLQF_CLEAN
)) {
741 rec
= OCFS2_SB(sb
)->quota_rec
;
743 rec
= ocfs2_alloc_quota_recovery();
749 OCFS2_SB(sb
)->quota_rec
= rec
;
752 status
= ocfs2_recovery_load_quota(lqinode
, ldinfo
, type
,
760 status
= ocfs2_load_local_quota_bitmaps(lqinode
,
768 /* Now mark quota file as used */
769 oinfo
->dqi_flags
&= ~OLQF_CLEAN
;
770 status
= ocfs2_modify_bh(lqinode
, bh
, olq_update_info
, info
);
776 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
780 iput(oinfo
->dqi_gqinode
);
781 ocfs2_simple_drop_lockres(OCFS2_SB(sb
), &oinfo
->dqi_gqlock
);
782 ocfs2_lock_res_free(&oinfo
->dqi_gqlock
);
783 brelse(oinfo
->dqi_lqi_bh
);
785 ocfs2_inode_unlock(lqinode
, 1);
786 ocfs2_release_local_quota_bitmaps(&oinfo
->dqi_chunk
);
790 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
794 /* Write local info to quota file */
795 static int ocfs2_local_write_info(struct super_block
*sb
, int type
)
797 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
798 struct buffer_head
*bh
= ((struct ocfs2_mem_dqinfo
*)info
->dqi_priv
)
802 status
= ocfs2_modify_bh(sb_dqopt(sb
)->files
[type
], bh
, olq_update_info
,
812 /* Release info from memory */
813 static int ocfs2_local_free_info(struct super_block
*sb
, int type
)
815 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
816 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
817 struct ocfs2_quota_chunk
*chunk
;
818 struct ocfs2_local_disk_chunk
*dchunk
;
819 int mark_clean
= 1, len
;
822 iput(oinfo
->dqi_gqinode
);
823 ocfs2_simple_drop_lockres(OCFS2_SB(sb
), &oinfo
->dqi_gqlock
);
824 ocfs2_lock_res_free(&oinfo
->dqi_gqlock
);
825 list_for_each_entry(chunk
, &oinfo
->dqi_chunk
, qc_chunk
) {
826 dchunk
= (struct ocfs2_local_disk_chunk
*)
827 (chunk
->qc_headerbh
->b_data
);
828 if (chunk
->qc_num
< oinfo
->dqi_chunks
- 1) {
829 len
= ol_chunk_entries(sb
);
831 len
= (oinfo
->dqi_blocks
-
832 ol_quota_chunk_block(sb
, chunk
->qc_num
) - 1)
833 * ol_quota_entries_per_block(sb
);
835 /* Not all entries free? Bug! */
836 if (le32_to_cpu(dchunk
->dqc_free
) != len
) {
837 mlog(ML_ERROR
, "releasing quota file with used "
838 "entries (type=%d)\n", type
);
842 ocfs2_release_local_quota_bitmaps(&oinfo
->dqi_chunk
);
844 /* dqonoff_mutex protects us against racing with recovery thread... */
845 if (oinfo
->dqi_rec
) {
846 ocfs2_free_quota_recovery(oinfo
->dqi_rec
);
853 /* Mark local file as clean */
854 oinfo
->dqi_flags
|= OLQF_CLEAN
;
855 status
= ocfs2_modify_bh(sb_dqopt(sb
)->files
[type
],
865 ocfs2_inode_unlock(sb_dqopt(sb
)->files
[type
], 1);
866 brelse(oinfo
->dqi_libh
);
867 brelse(oinfo
->dqi_lqi_bh
);
872 static void olq_set_dquot(struct buffer_head
*bh
, void *private)
874 struct ocfs2_dquot
*od
= private;
875 struct ocfs2_local_disk_dqblk
*dqblk
;
876 struct super_block
*sb
= od
->dq_dquot
.dq_sb
;
878 dqblk
= (struct ocfs2_local_disk_dqblk
*)(bh
->b_data
879 + ol_dqblk_block_offset(sb
, od
->dq_local_off
));
881 dqblk
->dqb_id
= cpu_to_le64(from_kqid(&init_user_ns
,
882 od
->dq_dquot
.dq_id
));
883 spin_lock(&dq_data_lock
);
884 dqblk
->dqb_spacemod
= cpu_to_le64(od
->dq_dquot
.dq_dqb
.dqb_curspace
-
886 dqblk
->dqb_inodemod
= cpu_to_le64(od
->dq_dquot
.dq_dqb
.dqb_curinodes
-
888 spin_unlock(&dq_data_lock
);
890 (unsigned long long)le64_to_cpu(dqblk
->dqb_spacemod
),
891 (unsigned long long)le64_to_cpu(dqblk
->dqb_inodemod
),
892 from_kqid(&init_user_ns
, od
->dq_dquot
.dq_id
));
895 /* Write dquot to local quota file */
896 int ocfs2_local_write_dquot(struct dquot
*dquot
)
898 struct super_block
*sb
= dquot
->dq_sb
;
899 struct ocfs2_dquot
*od
= OCFS2_DQUOT(dquot
);
900 struct buffer_head
*bh
;
901 struct inode
*lqinode
= sb_dqopt(sb
)->files
[dquot
->dq_id
.type
];
904 status
= ocfs2_read_quota_phys_block(lqinode
, od
->dq_local_phys_blk
,
910 status
= ocfs2_modify_bh(lqinode
, bh
, olq_set_dquot
, od
);
920 /* Find free entry in local quota file */
921 static struct ocfs2_quota_chunk
*ocfs2_find_free_entry(struct super_block
*sb
,
925 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
926 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
927 struct ocfs2_quota_chunk
*chunk
;
928 struct ocfs2_local_disk_chunk
*dchunk
;
931 list_for_each_entry(chunk
, &oinfo
->dqi_chunk
, qc_chunk
) {
932 dchunk
= (struct ocfs2_local_disk_chunk
*)
933 chunk
->qc_headerbh
->b_data
;
934 if (le32_to_cpu(dchunk
->dqc_free
) > 0) {
942 if (chunk
->qc_num
< oinfo
->dqi_chunks
- 1) {
943 len
= ol_chunk_entries(sb
);
945 len
= (oinfo
->dqi_blocks
-
946 ol_quota_chunk_block(sb
, chunk
->qc_num
) - 1)
947 * ol_quota_entries_per_block(sb
);
950 found
= ocfs2_find_next_zero_bit_unaligned(dchunk
->dqc_bitmap
, len
, 0);
953 mlog(ML_ERROR
, "Did not find empty entry in chunk %d with %u"
954 " entries free (type=%d)\n", chunk
->qc_num
,
955 le32_to_cpu(dchunk
->dqc_free
), type
);
956 return ERR_PTR(-EIO
);
962 /* Add new chunk to the local quota file */
963 static struct ocfs2_quota_chunk
*ocfs2_local_quota_add_chunk(
964 struct super_block
*sb
,
968 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
969 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
970 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
971 struct ocfs2_quota_chunk
*chunk
= NULL
;
972 struct ocfs2_local_disk_chunk
*dchunk
;
975 struct buffer_head
*bh
= NULL
, *dbh
= NULL
;
978 /* We are protected by dqio_sem so no locking needed */
979 status
= ocfs2_extend_no_holes(lqinode
, NULL
,
980 i_size_read(lqinode
) + 2 * sb
->s_blocksize
,
981 i_size_read(lqinode
));
986 status
= ocfs2_simple_size_update(lqinode
, oinfo
->dqi_lqi_bh
,
987 i_size_read(lqinode
) + 2 * sb
->s_blocksize
);
993 chunk
= kmem_cache_alloc(ocfs2_qf_chunk_cachep
, GFP_NOFS
);
999 /* Local quota info and two new blocks we initialize */
1000 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
1001 OCFS2_LOCAL_QINFO_WRITE_CREDITS
+
1002 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
);
1003 if (IS_ERR(handle
)) {
1004 status
= PTR_ERR(handle
);
1009 /* Initialize chunk header */
1010 status
= ocfs2_extent_map_get_blocks(lqinode
, oinfo
->dqi_blocks
,
1011 &p_blkno
, NULL
, NULL
);
1016 bh
= sb_getblk(sb
, p_blkno
);
1022 dchunk
= (struct ocfs2_local_disk_chunk
*)bh
->b_data
;
1023 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode
), bh
);
1024 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
), bh
,
1025 OCFS2_JOURNAL_ACCESS_CREATE
);
1031 dchunk
->dqc_free
= cpu_to_le32(ol_quota_entries_per_block(sb
));
1032 memset(dchunk
->dqc_bitmap
, 0,
1033 sb
->s_blocksize
- sizeof(struct ocfs2_local_disk_chunk
) -
1034 OCFS2_QBLK_RESERVED_SPACE
);
1036 ocfs2_journal_dirty(handle
, bh
);
1038 /* Initialize new block with structures */
1039 status
= ocfs2_extent_map_get_blocks(lqinode
, oinfo
->dqi_blocks
+ 1,
1040 &p_blkno
, NULL
, NULL
);
1045 dbh
= sb_getblk(sb
, p_blkno
);
1051 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode
), dbh
);
1052 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
), dbh
,
1053 OCFS2_JOURNAL_ACCESS_CREATE
);
1059 memset(dbh
->b_data
, 0, sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
);
1061 ocfs2_journal_dirty(handle
, dbh
);
1063 /* Update local quotafile info */
1064 oinfo
->dqi_blocks
+= 2;
1065 oinfo
->dqi_chunks
++;
1066 status
= ocfs2_local_write_info(sb
, type
);
1071 status
= ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1077 list_add_tail(&chunk
->qc_chunk
, &oinfo
->dqi_chunk
);
1078 chunk
->qc_num
= list_entry(chunk
->qc_chunk
.prev
,
1079 struct ocfs2_quota_chunk
,
1080 qc_chunk
)->qc_num
+ 1;
1081 chunk
->qc_headerbh
= bh
;
1085 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1089 kmem_cache_free(ocfs2_qf_chunk_cachep
, chunk
);
1090 return ERR_PTR(status
);
1093 /* Find free entry in local quota file */
1094 static struct ocfs2_quota_chunk
*ocfs2_extend_local_quota_file(
1095 struct super_block
*sb
,
1099 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
1100 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
1101 struct ocfs2_quota_chunk
*chunk
;
1102 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
1103 struct ocfs2_local_disk_chunk
*dchunk
;
1104 int epb
= ol_quota_entries_per_block(sb
);
1105 unsigned int chunk_blocks
;
1106 struct buffer_head
*bh
;
1111 if (list_empty(&oinfo
->dqi_chunk
))
1112 return ocfs2_local_quota_add_chunk(sb
, type
, offset
);
1113 /* Is the last chunk full? */
1114 chunk
= list_entry(oinfo
->dqi_chunk
.prev
,
1115 struct ocfs2_quota_chunk
, qc_chunk
);
1116 chunk_blocks
= oinfo
->dqi_blocks
-
1117 ol_quota_chunk_block(sb
, chunk
->qc_num
) - 1;
1118 if (ol_chunk_blocks(sb
) == chunk_blocks
)
1119 return ocfs2_local_quota_add_chunk(sb
, type
, offset
);
1121 /* We are protected by dqio_sem so no locking needed */
1122 status
= ocfs2_extend_no_holes(lqinode
, NULL
,
1123 i_size_read(lqinode
) + sb
->s_blocksize
,
1124 i_size_read(lqinode
));
1129 status
= ocfs2_simple_size_update(lqinode
, oinfo
->dqi_lqi_bh
,
1130 i_size_read(lqinode
) + sb
->s_blocksize
);
1136 /* Get buffer from the just added block */
1137 status
= ocfs2_extent_map_get_blocks(lqinode
, oinfo
->dqi_blocks
,
1138 &p_blkno
, NULL
, NULL
);
1143 bh
= sb_getblk(sb
, p_blkno
);
1149 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode
), bh
);
1151 /* Local quota info, chunk header and the new block we initialize */
1152 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
1153 OCFS2_LOCAL_QINFO_WRITE_CREDITS
+
1154 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
);
1155 if (IS_ERR(handle
)) {
1156 status
= PTR_ERR(handle
);
1160 /* Zero created block */
1161 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
), bh
,
1162 OCFS2_JOURNAL_ACCESS_CREATE
);
1168 memset(bh
->b_data
, 0, sb
->s_blocksize
);
1170 ocfs2_journal_dirty(handle
, bh
);
1172 /* Update chunk header */
1173 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
),
1175 OCFS2_JOURNAL_ACCESS_WRITE
);
1181 dchunk
= (struct ocfs2_local_disk_chunk
*)chunk
->qc_headerbh
->b_data
;
1182 lock_buffer(chunk
->qc_headerbh
);
1183 le32_add_cpu(&dchunk
->dqc_free
, ol_quota_entries_per_block(sb
));
1184 unlock_buffer(chunk
->qc_headerbh
);
1185 ocfs2_journal_dirty(handle
, chunk
->qc_headerbh
);
1187 /* Update file header */
1188 oinfo
->dqi_blocks
++;
1189 status
= ocfs2_local_write_info(sb
, type
);
1195 status
= ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1200 *offset
= chunk_blocks
* epb
;
1203 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1205 return ERR_PTR(status
);
1208 static void olq_alloc_dquot(struct buffer_head
*bh
, void *private)
1210 int *offset
= private;
1211 struct ocfs2_local_disk_chunk
*dchunk
;
1213 dchunk
= (struct ocfs2_local_disk_chunk
*)bh
->b_data
;
1214 ocfs2_set_bit_unaligned(*offset
, dchunk
->dqc_bitmap
);
1215 le32_add_cpu(&dchunk
->dqc_free
, -1);
1218 /* Create dquot in the local file for given id */
1219 int ocfs2_create_local_dquot(struct dquot
*dquot
)
1221 struct super_block
*sb
= dquot
->dq_sb
;
1222 int type
= dquot
->dq_id
.type
;
1223 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
1224 struct ocfs2_quota_chunk
*chunk
;
1225 struct ocfs2_dquot
*od
= OCFS2_DQUOT(dquot
);
1230 down_write(&OCFS2_I(lqinode
)->ip_alloc_sem
);
1231 chunk
= ocfs2_find_free_entry(sb
, type
, &offset
);
1233 chunk
= ocfs2_extend_local_quota_file(sb
, type
, &offset
);
1234 if (IS_ERR(chunk
)) {
1235 status
= PTR_ERR(chunk
);
1238 } else if (IS_ERR(chunk
)) {
1239 status
= PTR_ERR(chunk
);
1242 od
->dq_local_off
= ol_dqblk_off(sb
, chunk
->qc_num
, offset
);
1243 od
->dq_chunk
= chunk
;
1244 status
= ocfs2_extent_map_get_blocks(lqinode
,
1245 ol_dqblk_block(sb
, chunk
->qc_num
, offset
),
1246 &od
->dq_local_phys_blk
,
1250 /* Initialize dquot structure on disk */
1251 status
= ocfs2_local_write_dquot(dquot
);
1257 /* Mark structure as allocated */
1258 status
= ocfs2_modify_bh(lqinode
, chunk
->qc_headerbh
, olq_alloc_dquot
,
1265 up_write(&OCFS2_I(lqinode
)->ip_alloc_sem
);
1270 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1271 * already started a transaction and written all changes to global quota file
1273 int ocfs2_local_release_dquot(handle_t
*handle
, struct dquot
*dquot
)
1276 int type
= dquot
->dq_id
.type
;
1277 struct ocfs2_dquot
*od
= OCFS2_DQUOT(dquot
);
1278 struct super_block
*sb
= dquot
->dq_sb
;
1279 struct ocfs2_local_disk_chunk
*dchunk
;
1282 status
= ocfs2_journal_access_dq(handle
,
1283 INODE_CACHE(sb_dqopt(sb
)->files
[type
]),
1284 od
->dq_chunk
->qc_headerbh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1289 offset
= ol_dqblk_chunk_off(sb
, od
->dq_chunk
->qc_num
,
1291 dchunk
= (struct ocfs2_local_disk_chunk
*)
1292 (od
->dq_chunk
->qc_headerbh
->b_data
);
1293 /* Mark structure as freed */
1294 lock_buffer(od
->dq_chunk
->qc_headerbh
);
1295 ocfs2_clear_bit_unaligned(offset
, dchunk
->dqc_bitmap
);
1296 le32_add_cpu(&dchunk
->dqc_free
, 1);
1297 unlock_buffer(od
->dq_chunk
->qc_headerbh
);
1298 ocfs2_journal_dirty(handle
, od
->dq_chunk
->qc_headerbh
);
1304 static const struct quota_format_ops ocfs2_format_ops
= {
1305 .check_quota_file
= ocfs2_local_check_quota_file
,
1306 .read_file_info
= ocfs2_local_read_info
,
1307 .write_file_info
= ocfs2_global_write_info
,
1308 .free_file_info
= ocfs2_local_free_info
,
1311 struct quota_format_type ocfs2_quota_format
= {
1312 .qf_fmt_id
= QFMT_OCFS2
,
1313 .qf_ops
= &ocfs2_format_ops
,
1314 .qf_owner
= THIS_MODULE