6 #include <linux/slab.h>
7 #include <linux/quota.h>
9 #include <linux/module.h>
28 static inline unsigned int ol_quota_entries_per_block(
struct super_block *
sb)
35 static inline unsigned int ol_chunk_blocks(
struct super_block *sb)
39 ol_quota_entries_per_block(sb);
43 static unsigned int ol_chunk_entries(
struct super_block *sb)
45 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
49 static unsigned int ol_quota_chunk_block(
struct super_block *sb,
int c)
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);
70 static loff_t ol_dqblk_off(
struct super_block *sb,
int c,
int off)
73 ol_dqblk_block_off(sb, c, off);
77 static inline unsigned int ol_dqblk_file_block(
struct super_block *sb, loff_t off)
82 static inline unsigned int ol_dqblk_block_offset(
struct super_block *sb, loff_t off)
88 static int ol_dqblk_chunk_off(
struct super_block *sb,
int c, loff_t off)
90 int epb = ol_quota_entries_per_block(sb);
93 ol_quota_chunk_block(sb, c) - 1) * epb
99 static int ocfs2_modify_bh(
struct inode *
inode,
struct buffer_head *bh,
100 void (*modify)(
struct buffer_head *,
void *),
void *
private)
108 if (IS_ERR(handle)) {
109 status = PTR_ERR(handle);
139 static int ocfs2_read_quota_block(
struct inode *inode,
u64 v_block,
140 struct buffer_head **bh)
143 struct buffer_head *
tmp = *bh;
145 if (i_size_read(inode) >> inode->
i_sb->s_blocksize_bits <= v_block) {
147 "Quota file %llu is probably corrupted! Requested "
148 "to read block %Lu but file has size only %Lu\n",
149 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
150 (
unsigned long long)v_block,
151 (
unsigned long long)i_size_read(inode));
167 static int ocfs2_local_check_quota_file(
struct super_block *sb,
int type)
175 struct buffer_head *bh =
NULL;
176 struct inode *linode = sb_dqopt(sb)->files[
type];
177 struct inode *ginode =
NULL;
182 status = ocfs2_read_quota_block(linode, 0, &bh);
185 mlog(
ML_ERROR,
"failed to read quota file header (type=%d)\n",
191 mlog(
ML_ERROR,
"quota file magic does not match (%u != %u),"
193 lmagics[type], type);
197 mlog(
ML_ERROR,
"quota file version does not match (%u != %u),"
199 lversions[type], type);
210 "(type=%d)\n", type);
214 status = ocfs2_read_quota_block(ginode, 0, &bh);
217 mlog(
ML_ERROR,
"failed to read global quota file header "
218 "(type=%d)\n", type);
223 mlog(
ML_ERROR,
"global quota file magic does not match "
224 "(%u != %u), type=%d\n",
229 mlog(
ML_ERROR,
"global quota file version does not match "
230 "(%u != %u), type=%d\n",
244 static void ocfs2_release_local_quota_bitmaps(
struct list_head *
head)
256 static int ocfs2_load_local_quota_bitmaps(
struct inode *inode,
263 INIT_LIST_HEAD(head);
267 ocfs2_release_local_quota_bitmaps(head);
272 status = ocfs2_read_quota_block(inode,
273 ol_quota_chunk_block(inode->
i_sb, i),
278 ocfs2_release_local_quota_bitmaps(head);
286 static void olq_update_info(
struct buffer_head *bh,
void *
private)
294 spin_lock(&dq_data_lock);
298 spin_unlock(&dq_data_lock);
301 static int ocfs2_add_recovery_chunk(
struct super_block *sb,
318 (ol_chunk_entries(sb) + 7) >> 3);
323 static void free_recovery_list(
struct list_head *head)
340 free_recovery_list(&(rec->
r_list[type]));
345 static int ocfs2_recovery_load_quota(
struct inode *lqinode,
351 struct buffer_head *hbh;
356 for (i = 0; i < chunks; i++) {
358 status = ocfs2_read_quota_block(lqinode,
359 ol_quota_chunk_block(sb, i),
367 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
373 free_recovery_list(head);
386 INIT_LIST_HEAD(&(rec->
r_list[type]));
401 struct inode *lqinode;
402 struct buffer_head *bh;
408 "slot %u\n", osb->
dev_str, slot_num);
410 rec = ocfs2_alloc_quota_recovery();
415 for (type = 0; type <
MAXQUOTAS; type++) {
433 status = ocfs2_read_quota_block(lqinode, 0, &bh);
437 "(slot=%d type=%d)\n", slot_num, type);
442 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
455 rec = ERR_PTR(status);
464 static int ocfs2_recover_local_quota_file(
struct inode *lqinode,
474 struct buffer_head *hbh =
NULL, *qbh =
NULL;
478 qsize_t spacechange, inodechange;
480 trace_ocfs2_recover_local_quota_file((
unsigned long)lqinode->
i_ino, type);
485 status = ocfs2_read_quota_block(lqinode,
486 ol_quota_chunk_block(sb, chunk),
495 status = ocfs2_read_quota_block(lqinode,
496 ol_dqblk_block(sb, chunk, bit),
503 ol_dqblk_block_off(sb, chunk, bit));
510 "for id %u, type %d. Cannot finish quota "
524 if (IS_ERR(handle)) {
525 status = PTR_ERR(handle);
530 spin_lock(&dq_data_lock);
536 dquot->
dq_dqb.dqb_curspace += spacechange;
537 dquot->
dq_dqb.dqb_curinodes += inodechange;
538 spin_unlock(&dq_data_lock);
542 status = ocfs2_global_release_dquot(dquot);
549 INODE_CACHE(lqinode),
557 ocfs2_clear_bit_unaligned(bit, dchunk->
dqc_bitmap);
581 free_recovery_list(&(rec->
r_list[type]));
596 struct buffer_head *bh;
600 struct inode *lqinode;
604 "slot %u\n", osb->
dev_str, slot_num);
607 for (type = 0; type <
MAXQUOTAS; type++) {
608 if (list_empty(&(rec->
r_list[type])))
610 trace_ocfs2_finish_quota_recovery(slot_num);
622 "device (%s) for slot %d because quota file is "
623 "locked.\n", osb->
dev_str, slot_num);
626 }
else if (status < 0) {
632 status = ocfs2_read_quota_block(lqinode, 0, &bh);
636 "(slot=%d type=%d)\n", slot_num, type);
644 status = ocfs2_recover_local_quota_file(lqinode,
655 if (IS_ERR(handle)) {
656 status = PTR_ERR(handle);
689 static int ocfs2_local_read_info(
struct super_block *sb,
int type)
692 struct mem_dqinfo *info = sb_dqinfo(sb, type);
694 struct inode *lqinode = sb_dqopt(sb)->files[
type];
696 struct buffer_head *bh =
NULL;
707 mlog(
ML_ERROR,
"failed to allocate memory for ocfs2 quota"
730 status = ocfs2_read_quota_block(lqinode, 0, &bh);
734 "(type=%d)\n", type);
748 rec = ocfs2_alloc_quota_recovery();
757 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
765 status = ocfs2_load_local_quota_bitmaps(lqinode,
775 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
791 ocfs2_release_local_quota_bitmaps(&oinfo->
dqi_chunk);
800 static int ocfs2_local_write_info(
struct super_block *sb,
int type)
802 struct mem_dqinfo *info = sb_dqinfo(sb, type);
807 status = ocfs2_modify_bh(sb_dqopt(sb)->
files[type], bh, olq_update_info,
818 static int ocfs2_local_free_info(
struct super_block *sb,
int type)
820 struct mem_dqinfo *info = sb_dqinfo(sb, type);
824 int mark_clean = 1,
len;
834 len = ol_chunk_entries(sb);
837 ol_quota_chunk_block(sb, chunk->
qc_num) - 1)
838 * ol_quota_entries_per_block(sb);
843 "entries (type=%d)\n", type);
847 ocfs2_release_local_quota_bitmaps(&oinfo->
dqi_chunk);
860 status = ocfs2_modify_bh(sb_dqopt(sb)->
files[type],
877 static void olq_set_dquot(
struct buffer_head *bh,
void *
private)
888 spin_lock(&dq_data_lock);
893 spin_unlock(&dq_data_lock);
905 struct buffer_head *bh;
906 struct inode *lqinode = sb_dqopt(sb)->files[dquot->
dq_id.type];
915 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
930 struct mem_dqinfo *info = sb_dqinfo(sb, type);
948 len = ol_chunk_entries(sb);
951 ol_quota_chunk_block(sb, chunk->
qc_num) - 1)
952 * ol_quota_entries_per_block(sb);
955 found = ocfs2_find_next_zero_bit_unaligned(dchunk->
dqc_bitmap, len, 0);
958 mlog(
ML_ERROR,
"Did not find empty entry in chunk %d with %u"
959 " entries free (type=%d)\n", chunk->
qc_num,
961 return ERR_PTR(-
EIO);
973 struct mem_dqinfo *info = sb_dqinfo(sb, type);
975 struct inode *lqinode = sb_dqopt(sb)->files[
type];
980 struct buffer_head *bh =
NULL, *dbh =
NULL;
1008 if (IS_ERR(handle)) {
1009 status = PTR_ERR(handle);
1021 bh = sb_getblk(sb, p_blkno);
1050 dbh = sb_getblk(sb, p_blkno);
1071 status = ocfs2_local_write_info(sb, type);
1085 qc_chunk)->qc_num + 1;
1095 return ERR_PTR(status);
1104 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1107 struct inode *lqinode = sb_dqopt(sb)->files[
type];
1109 int epb = ol_quota_entries_per_block(sb);
1110 unsigned int chunk_blocks;
1111 struct buffer_head *bh;
1117 return ocfs2_local_quota_add_chunk(sb, type, offset);
1122 ol_quota_chunk_block(sb, chunk->
qc_num) - 1;
1123 if (ol_chunk_blocks(sb) == chunk_blocks)
1124 return ocfs2_local_quota_add_chunk(sb, type, offset);
1148 bh = sb_getblk(sb, p_blkno);
1160 if (IS_ERR(handle)) {
1161 status = PTR_ERR(handle);
1188 le32_add_cpu(&dchunk->
dqc_free, ol_quota_entries_per_block(sb));
1194 status = ocfs2_local_write_info(sb, type);
1205 *offset = chunk_blocks * epb;
1210 return ERR_PTR(status);
1213 static void olq_alloc_dquot(
struct buffer_head *bh,
void *
private)
1215 int *offset =
private;
1219 ocfs2_set_bit_unaligned(*offset, dchunk->
dqc_bitmap);
1220 le32_add_cpu(&dchunk->
dqc_free, -1);
1227 int type = dquot->
dq_id.type;
1228 struct inode *lqinode = sb_dqopt(sb)->files[
type];
1236 chunk = ocfs2_find_free_entry(sb, type, &offset);
1238 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1239 if (IS_ERR(chunk)) {
1240 status = PTR_ERR(chunk);
1243 }
else if (IS_ERR(chunk)) {
1244 status = PTR_ERR(chunk);
1250 ol_dqblk_block(sb, chunk->
qc_num, offset),
1263 status = ocfs2_modify_bh(lqinode, chunk->
qc_headerbh, olq_alloc_dquot,
1270 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1281 int type = dquot->
dq_id.type;
1288 INODE_CACHE(sb_dqopt(sb)->
files[type]),
1294 offset = ol_dqblk_chunk_off(sb, od->
dq_chunk->qc_num,
1297 (od->
dq_chunk->qc_headerbh->b_data);
1299 lock_buffer(od->
dq_chunk->qc_headerbh);
1300 ocfs2_clear_bit_unaligned(offset, dchunk->
dqc_bitmap);
1301 le32_add_cpu(&dchunk->
dqc_free, 1);
1314 .check_quota_file = ocfs2_local_check_quota_file,
1315 .read_file_info = ocfs2_local_read_info,
1317 .free_file_info = ocfs2_local_free_info,
1322 .qf_ops = &ocfs2_format_ops,