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 /* Compute block number from given offset */
77 static inline unsigned int ol_dqblk_file_block(struct super_block
*sb
, loff_t off
)
79 return off
>> sb
->s_blocksize_bits
;
82 static inline unsigned int ol_dqblk_block_offset(struct super_block
*sb
, loff_t off
)
84 return off
& ((1 << sb
->s_blocksize_bits
) - 1);
87 /* Compute offset in the chunk of a structure with the given offset */
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
);
92 return ((off
>> sb
->s_blocksize_bits
) -
93 ol_quota_chunk_block(sb
, c
) - 1) * epb
94 + ((unsigned int)(off
& ((1 << sb
->s_blocksize_bits
) - 1))) /
95 sizeof(struct ocfs2_local_disk_dqblk
);
98 /* Write bufferhead into the fs */
99 static int ocfs2_modify_bh(struct inode
*inode
, struct buffer_head
*bh
,
100 void (*modify
)(struct buffer_head
*, void *), void *private)
102 struct super_block
*sb
= inode
->i_sb
;
106 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
107 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
);
108 if (IS_ERR(handle
)) {
109 status
= PTR_ERR(handle
);
113 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(inode
), bh
,
114 OCFS2_JOURNAL_ACCESS_WRITE
);
117 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
123 ocfs2_journal_dirty(handle
, bh
);
125 status
= ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
134 * Read quota block from a given logical offset.
136 * This function acquires ip_alloc_sem and thus it must not be called with a
137 * transaction started.
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
) {
146 ocfs2_error(inode
->i_sb
,
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
));
154 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, 0,
155 ocfs2_validate_quota_block
);
159 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
166 /* Check whether we understand format of quota files */
167 static int ocfs2_local_check_quota_file(struct super_block
*sb
, int type
)
169 unsigned int lmagics
[MAXQUOTAS
] = OCFS2_LOCAL_QMAGICS
;
170 unsigned int lversions
[MAXQUOTAS
] = OCFS2_LOCAL_QVERSIONS
;
171 unsigned int gmagics
[MAXQUOTAS
] = OCFS2_GLOBAL_QMAGICS
;
172 unsigned int gversions
[MAXQUOTAS
] = OCFS2_GLOBAL_QVERSIONS
;
173 unsigned int ino
[MAXQUOTAS
] = { USER_QUOTA_SYSTEM_INODE
,
174 GROUP_QUOTA_SYSTEM_INODE
};
175 struct buffer_head
*bh
= NULL
;
176 struct inode
*linode
= sb_dqopt(sb
)->files
[type
];
177 struct inode
*ginode
= NULL
;
178 struct ocfs2_disk_dqheader
*dqhead
;
181 /* First check whether we understand local quota file */
182 status
= ocfs2_read_quota_block(linode
, 0, &bh
);
185 mlog(ML_ERROR
, "failed to read quota file header (type=%d)\n",
189 dqhead
= (struct ocfs2_disk_dqheader
*)(bh
->b_data
);
190 if (le32_to_cpu(dqhead
->dqh_magic
) != lmagics
[type
]) {
191 mlog(ML_ERROR
, "quota file magic does not match (%u != %u),"
192 " type=%d\n", le32_to_cpu(dqhead
->dqh_magic
),
193 lmagics
[type
], type
);
196 if (le32_to_cpu(dqhead
->dqh_version
) != lversions
[type
]) {
197 mlog(ML_ERROR
, "quota file version does not match (%u != %u),"
198 " type=%d\n", le32_to_cpu(dqhead
->dqh_version
),
199 lversions
[type
], type
);
205 /* Next check whether we understand global quota file */
206 ginode
= ocfs2_get_system_file_inode(OCFS2_SB(sb
), ino
[type
],
209 mlog(ML_ERROR
, "cannot get global quota file inode "
210 "(type=%d)\n", type
);
213 /* Since the header is read only, we don't care about locking */
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
);
221 dqhead
= (struct ocfs2_disk_dqheader
*)(bh
->b_data
);
222 if (le32_to_cpu(dqhead
->dqh_magic
) != gmagics
[type
]) {
223 mlog(ML_ERROR
, "global quota file magic does not match "
224 "(%u != %u), type=%d\n",
225 le32_to_cpu(dqhead
->dqh_magic
), gmagics
[type
], type
);
228 if (le32_to_cpu(dqhead
->dqh_version
) != gversions
[type
]) {
229 mlog(ML_ERROR
, "global quota file version does not match "
230 "(%u != %u), type=%d\n",
231 le32_to_cpu(dqhead
->dqh_version
), gversions
[type
],
243 /* Release given list of quota file chunks */
244 static void ocfs2_release_local_quota_bitmaps(struct list_head
*head
)
246 struct ocfs2_quota_chunk
*pos
, *next
;
248 list_for_each_entry_safe(pos
, next
, head
, qc_chunk
) {
249 list_del(&pos
->qc_chunk
);
250 brelse(pos
->qc_headerbh
);
251 kmem_cache_free(ocfs2_qf_chunk_cachep
, pos
);
255 /* Load quota bitmaps into memory */
256 static int ocfs2_load_local_quota_bitmaps(struct inode
*inode
,
257 struct ocfs2_local_disk_dqinfo
*ldinfo
,
258 struct list_head
*head
)
260 struct ocfs2_quota_chunk
*newchunk
;
263 INIT_LIST_HEAD(head
);
264 for (i
= 0; i
< le32_to_cpu(ldinfo
->dqi_chunks
); i
++) {
265 newchunk
= kmem_cache_alloc(ocfs2_qf_chunk_cachep
, GFP_NOFS
);
267 ocfs2_release_local_quota_bitmaps(head
);
270 newchunk
->qc_num
= i
;
271 newchunk
->qc_headerbh
= NULL
;
272 status
= ocfs2_read_quota_block(inode
,
273 ol_quota_chunk_block(inode
->i_sb
, i
),
274 &newchunk
->qc_headerbh
);
277 kmem_cache_free(ocfs2_qf_chunk_cachep
, newchunk
);
278 ocfs2_release_local_quota_bitmaps(head
);
281 list_add_tail(&newchunk
->qc_chunk
, head
);
286 static void olq_update_info(struct buffer_head
*bh
, void *private)
288 struct mem_dqinfo
*info
= private;
289 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
290 struct ocfs2_local_disk_dqinfo
*ldinfo
;
292 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
293 OCFS2_LOCAL_INFO_OFF
);
294 spin_lock(&dq_data_lock
);
295 ldinfo
->dqi_flags
= cpu_to_le32(info
->dqi_flags
& DQF_MASK
);
296 ldinfo
->dqi_chunks
= cpu_to_le32(oinfo
->dqi_chunks
);
297 ldinfo
->dqi_blocks
= cpu_to_le32(oinfo
->dqi_blocks
);
298 spin_unlock(&dq_data_lock
);
301 static int ocfs2_add_recovery_chunk(struct super_block
*sb
,
302 struct ocfs2_local_disk_chunk
*dchunk
,
304 struct list_head
*head
)
306 struct ocfs2_recovery_chunk
*rc
;
308 rc
= kmalloc(sizeof(struct ocfs2_recovery_chunk
), GFP_NOFS
);
311 rc
->rc_chunk
= chunk
;
312 rc
->rc_bitmap
= kmalloc(sb
->s_blocksize
, GFP_NOFS
);
313 if (!rc
->rc_bitmap
) {
317 memcpy(rc
->rc_bitmap
, dchunk
->dqc_bitmap
,
318 (ol_chunk_entries(sb
) + 7) >> 3);
319 list_add_tail(&rc
->rc_list
, head
);
323 static void free_recovery_list(struct list_head
*head
)
325 struct ocfs2_recovery_chunk
*next
;
326 struct ocfs2_recovery_chunk
*rchunk
;
328 list_for_each_entry_safe(rchunk
, next
, head
, rc_list
) {
329 list_del(&rchunk
->rc_list
);
330 kfree(rchunk
->rc_bitmap
);
335 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery
*rec
)
339 for (type
= 0; type
< MAXQUOTAS
; type
++)
340 free_recovery_list(&(rec
->r_list
[type
]));
344 /* Load entries in our quota file we have to recover*/
345 static int ocfs2_recovery_load_quota(struct inode
*lqinode
,
346 struct ocfs2_local_disk_dqinfo
*ldinfo
,
348 struct list_head
*head
)
350 struct super_block
*sb
= lqinode
->i_sb
;
351 struct buffer_head
*hbh
;
352 struct ocfs2_local_disk_chunk
*dchunk
;
353 int i
, chunks
= le32_to_cpu(ldinfo
->dqi_chunks
);
356 for (i
= 0; i
< chunks
; i
++) {
358 status
= ocfs2_read_quota_block(lqinode
,
359 ol_quota_chunk_block(sb
, i
),
365 dchunk
= (struct ocfs2_local_disk_chunk
*)hbh
->b_data
;
366 if (le32_to_cpu(dchunk
->dqc_free
) < ol_chunk_entries(sb
))
367 status
= ocfs2_add_recovery_chunk(sb
, dchunk
, i
, head
);
373 free_recovery_list(head
);
377 static struct ocfs2_quota_recovery
*ocfs2_alloc_quota_recovery(void)
380 struct ocfs2_quota_recovery
*rec
;
382 rec
= kmalloc(sizeof(struct ocfs2_quota_recovery
), GFP_NOFS
);
385 for (type
= 0; type
< MAXQUOTAS
; type
++)
386 INIT_LIST_HEAD(&(rec
->r_list
[type
]));
390 /* Load information we need for quota recovery into memory */
391 struct ocfs2_quota_recovery
*ocfs2_begin_quota_recovery(
392 struct ocfs2_super
*osb
,
395 unsigned int feature
[MAXQUOTAS
] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA
,
396 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA
};
397 unsigned int ino
[MAXQUOTAS
] = { LOCAL_USER_QUOTA_SYSTEM_INODE
,
398 LOCAL_GROUP_QUOTA_SYSTEM_INODE
};
399 struct super_block
*sb
= osb
->sb
;
400 struct ocfs2_local_disk_dqinfo
*ldinfo
;
401 struct inode
*lqinode
;
402 struct buffer_head
*bh
;
405 struct ocfs2_quota_recovery
*rec
;
407 printk(KERN_NOTICE
"ocfs2: Beginning quota recovery on device (%s) for "
408 "slot %u\n", osb
->dev_str
, slot_num
);
410 rec
= ocfs2_alloc_quota_recovery();
412 return ERR_PTR(-ENOMEM
);
415 for (type
= 0; type
< MAXQUOTAS
; type
++) {
416 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb
, feature
[type
]))
418 /* At this point, journal of the slot is already replayed so
419 * we can trust metadata and data of the quota file */
420 lqinode
= ocfs2_get_system_file_inode(osb
, ino
[type
], slot_num
);
425 status
= ocfs2_inode_lock_full(lqinode
, NULL
, 1,
426 OCFS2_META_LOCK_RECOVERY
);
431 /* Now read local header */
433 status
= ocfs2_read_quota_block(lqinode
, 0, &bh
);
436 mlog(ML_ERROR
, "failed to read quota file info header "
437 "(slot=%d type=%d)\n", slot_num
, type
);
440 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
441 OCFS2_LOCAL_INFO_OFF
);
442 status
= ocfs2_recovery_load_quota(lqinode
, ldinfo
, type
,
446 ocfs2_inode_unlock(lqinode
, 1);
454 ocfs2_free_quota_recovery(rec
);
455 rec
= ERR_PTR(status
);
460 /* Sync changes in local quota file into global quota file and
461 * reinitialize local quota file.
462 * The function expects local quota file to be already locked and
463 * dqonoff_mutex locked. */
464 static int ocfs2_recover_local_quota_file(struct inode
*lqinode
,
466 struct ocfs2_quota_recovery
*rec
)
468 struct super_block
*sb
= lqinode
->i_sb
;
469 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
470 struct ocfs2_local_disk_chunk
*dchunk
;
471 struct ocfs2_local_disk_dqblk
*dqblk
;
474 struct buffer_head
*hbh
= NULL
, *qbh
= NULL
;
477 struct ocfs2_recovery_chunk
*rchunk
, *next
;
478 qsize_t spacechange
, inodechange
;
480 trace_ocfs2_recover_local_quota_file((unsigned long)lqinode
->i_ino
, type
);
482 list_for_each_entry_safe(rchunk
, next
, &(rec
->r_list
[type
]), rc_list
) {
483 chunk
= rchunk
->rc_chunk
;
485 status
= ocfs2_read_quota_block(lqinode
,
486 ol_quota_chunk_block(sb
, chunk
),
492 dchunk
= (struct ocfs2_local_disk_chunk
*)hbh
->b_data
;
493 for_each_set_bit(bit
, rchunk
->rc_bitmap
, ol_chunk_entries(sb
)) {
495 status
= ocfs2_read_quota_block(lqinode
,
496 ol_dqblk_block(sb
, chunk
, bit
),
502 dqblk
= (struct ocfs2_local_disk_dqblk
*)(qbh
->b_data
+
503 ol_dqblk_block_off(sb
, chunk
, bit
));
505 make_kqid(&init_user_ns
, type
,
506 le64_to_cpu(dqblk
->dqb_id
)));
509 mlog(ML_ERROR
, "Failed to get quota structure "
510 "for id %u, type %d. Cannot finish quota "
512 (unsigned)le64_to_cpu(dqblk
->dqb_id
),
516 status
= ocfs2_lock_global_qf(oinfo
, 1);
522 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
523 OCFS2_QSYNC_CREDITS
);
524 if (IS_ERR(handle
)) {
525 status
= PTR_ERR(handle
);
529 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
530 spin_lock(&dq_data_lock
);
531 /* Add usage from quota entry into quota changes
532 * of our node. Auxiliary variables are important
533 * due to signedness */
534 spacechange
= le64_to_cpu(dqblk
->dqb_spacemod
);
535 inodechange
= le64_to_cpu(dqblk
->dqb_inodemod
);
536 dquot
->dq_dqb
.dqb_curspace
+= spacechange
;
537 dquot
->dq_dqb
.dqb_curinodes
+= inodechange
;
538 spin_unlock(&dq_data_lock
);
539 /* We want to drop reference held by the crashed
540 * node. Since we have our own reference we know
541 * global structure actually won't be freed. */
542 status
= ocfs2_global_release_dquot(dquot
);
547 /* Release local quota file entry */
548 status
= ocfs2_journal_access_dq(handle
,
549 INODE_CACHE(lqinode
),
550 qbh
, OCFS2_JOURNAL_ACCESS_WRITE
);
556 WARN_ON(!ocfs2_test_bit_unaligned(bit
, dchunk
->dqc_bitmap
));
557 ocfs2_clear_bit_unaligned(bit
, dchunk
->dqc_bitmap
);
558 le32_add_cpu(&dchunk
->dqc_free
, 1);
560 ocfs2_journal_dirty(handle
, qbh
);
562 mutex_unlock(&sb_dqopt(sb
)->dqio_mutex
);
563 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
565 ocfs2_unlock_global_qf(oinfo
, 1);
574 list_del(&rchunk
->rc_list
);
575 kfree(rchunk
->rc_bitmap
);
581 free_recovery_list(&(rec
->r_list
[type
]));
587 /* Recover local quota files for given node different from us */
588 int ocfs2_finish_quota_recovery(struct ocfs2_super
*osb
,
589 struct ocfs2_quota_recovery
*rec
,
592 unsigned int ino
[MAXQUOTAS
] = { LOCAL_USER_QUOTA_SYSTEM_INODE
,
593 LOCAL_GROUP_QUOTA_SYSTEM_INODE
};
594 struct super_block
*sb
= osb
->sb
;
595 struct ocfs2_local_disk_dqinfo
*ldinfo
;
596 struct buffer_head
*bh
;
600 struct inode
*lqinode
;
603 printk(KERN_NOTICE
"ocfs2: Finishing quota recovery on device (%s) for "
604 "slot %u\n", osb
->dev_str
, slot_num
);
606 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
607 for (type
= 0; type
< MAXQUOTAS
; type
++) {
608 if (list_empty(&(rec
->r_list
[type
])))
610 trace_ocfs2_finish_quota_recovery(slot_num
);
611 lqinode
= ocfs2_get_system_file_inode(osb
, ino
[type
], slot_num
);
616 status
= ocfs2_inode_lock_full(lqinode
, NULL
, 1,
617 OCFS2_META_LOCK_NOQUEUE
);
618 /* Someone else is holding the lock? Then he must be
619 * doing the recovery. Just skip the file... */
620 if (status
== -EAGAIN
) {
621 printk(KERN_NOTICE
"ocfs2: Skipping quota recovery on "
622 "device (%s) for slot %d because quota file is "
623 "locked.\n", osb
->dev_str
, slot_num
);
626 } else if (status
< 0) {
630 /* Now read local header */
632 status
= ocfs2_read_quota_block(lqinode
, 0, &bh
);
635 mlog(ML_ERROR
, "failed to read quota file info header "
636 "(slot=%d type=%d)\n", slot_num
, type
);
639 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
640 OCFS2_LOCAL_INFO_OFF
);
641 /* Is recovery still needed? */
642 flags
= le32_to_cpu(ldinfo
->dqi_flags
);
643 if (!(flags
& OLQF_CLEAN
))
644 status
= ocfs2_recover_local_quota_file(lqinode
,
647 /* We don't want to mark file as clean when it is actually
649 if (slot_num
== osb
->slot_num
)
651 /* Mark quota file as clean if we are recovering quota file of
652 * some other node. */
653 handle
= ocfs2_start_trans(osb
,
654 OCFS2_LOCAL_QINFO_WRITE_CREDITS
);
655 if (IS_ERR(handle
)) {
656 status
= PTR_ERR(handle
);
660 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
),
662 OCFS2_JOURNAL_ACCESS_WRITE
);
668 ldinfo
->dqi_flags
= cpu_to_le32(flags
| OLQF_CLEAN
);
670 ocfs2_journal_dirty(handle
, bh
);
672 ocfs2_commit_trans(osb
, handle
);
676 ocfs2_inode_unlock(lqinode
, 1);
683 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
688 /* Read information header from quota file */
689 static int ocfs2_local_read_info(struct super_block
*sb
, int type
)
691 struct ocfs2_local_disk_dqinfo
*ldinfo
;
692 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
693 struct ocfs2_mem_dqinfo
*oinfo
;
694 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
696 struct buffer_head
*bh
= NULL
;
697 struct ocfs2_quota_recovery
*rec
;
700 /* We don't need the lock and we have to acquire quota file locks
701 * which will later depend on this lock */
702 mutex_unlock(&sb_dqopt(sb
)->dqio_mutex
);
703 info
->dqi_maxblimit
= 0x7fffffffffffffffLL
;
704 info
->dqi_maxilimit
= 0x7fffffffffffffffLL
;
705 oinfo
= kmalloc(sizeof(struct ocfs2_mem_dqinfo
), GFP_NOFS
);
707 mlog(ML_ERROR
, "failed to allocate memory for ocfs2 quota"
711 info
->dqi_priv
= oinfo
;
712 oinfo
->dqi_type
= type
;
713 INIT_LIST_HEAD(&oinfo
->dqi_chunk
);
714 oinfo
->dqi_rec
= NULL
;
715 oinfo
->dqi_lqi_bh
= NULL
;
716 oinfo
->dqi_libh
= NULL
;
718 status
= ocfs2_global_read_info(sb
, type
);
722 status
= ocfs2_inode_lock(lqinode
, &oinfo
->dqi_lqi_bh
, 1);
729 /* Now read local header */
730 status
= ocfs2_read_quota_block(lqinode
, 0, &bh
);
733 mlog(ML_ERROR
, "failed to read quota file info header "
734 "(type=%d)\n", type
);
737 ldinfo
= (struct ocfs2_local_disk_dqinfo
*)(bh
->b_data
+
738 OCFS2_LOCAL_INFO_OFF
);
739 info
->dqi_flags
= le32_to_cpu(ldinfo
->dqi_flags
);
740 oinfo
->dqi_chunks
= le32_to_cpu(ldinfo
->dqi_chunks
);
741 oinfo
->dqi_blocks
= le32_to_cpu(ldinfo
->dqi_blocks
);
742 oinfo
->dqi_libh
= bh
;
744 /* We crashed when using local quota file? */
745 if (!(info
->dqi_flags
& OLQF_CLEAN
)) {
746 rec
= OCFS2_SB(sb
)->quota_rec
;
748 rec
= ocfs2_alloc_quota_recovery();
754 OCFS2_SB(sb
)->quota_rec
= rec
;
757 status
= ocfs2_recovery_load_quota(lqinode
, ldinfo
, type
,
765 status
= ocfs2_load_local_quota_bitmaps(lqinode
,
773 /* Now mark quota file as used */
774 info
->dqi_flags
&= ~OLQF_CLEAN
;
775 status
= ocfs2_modify_bh(lqinode
, bh
, olq_update_info
, info
);
781 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
785 iput(oinfo
->dqi_gqinode
);
786 ocfs2_simple_drop_lockres(OCFS2_SB(sb
), &oinfo
->dqi_gqlock
);
787 ocfs2_lock_res_free(&oinfo
->dqi_gqlock
);
788 brelse(oinfo
->dqi_lqi_bh
);
790 ocfs2_inode_unlock(lqinode
, 1);
791 ocfs2_release_local_quota_bitmaps(&oinfo
->dqi_chunk
);
795 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
799 /* Write local info to quota file */
800 static int ocfs2_local_write_info(struct super_block
*sb
, int type
)
802 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
803 struct buffer_head
*bh
= ((struct ocfs2_mem_dqinfo
*)info
->dqi_priv
)
807 status
= ocfs2_modify_bh(sb_dqopt(sb
)->files
[type
], bh
, olq_update_info
,
817 /* Release info from memory */
818 static int ocfs2_local_free_info(struct super_block
*sb
, int type
)
820 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
821 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
822 struct ocfs2_quota_chunk
*chunk
;
823 struct ocfs2_local_disk_chunk
*dchunk
;
824 int mark_clean
= 1, len
;
827 iput(oinfo
->dqi_gqinode
);
828 ocfs2_simple_drop_lockres(OCFS2_SB(sb
), &oinfo
->dqi_gqlock
);
829 ocfs2_lock_res_free(&oinfo
->dqi_gqlock
);
830 list_for_each_entry(chunk
, &oinfo
->dqi_chunk
, qc_chunk
) {
831 dchunk
= (struct ocfs2_local_disk_chunk
*)
832 (chunk
->qc_headerbh
->b_data
);
833 if (chunk
->qc_num
< oinfo
->dqi_chunks
- 1) {
834 len
= ol_chunk_entries(sb
);
836 len
= (oinfo
->dqi_blocks
-
837 ol_quota_chunk_block(sb
, chunk
->qc_num
) - 1)
838 * ol_quota_entries_per_block(sb
);
840 /* Not all entries free? Bug! */
841 if (le32_to_cpu(dchunk
->dqc_free
) != len
) {
842 mlog(ML_ERROR
, "releasing quota file with used "
843 "entries (type=%d)\n", type
);
847 ocfs2_release_local_quota_bitmaps(&oinfo
->dqi_chunk
);
849 /* dqonoff_mutex protects us against racing with recovery thread... */
850 if (oinfo
->dqi_rec
) {
851 ocfs2_free_quota_recovery(oinfo
->dqi_rec
);
858 /* Mark local file as clean */
859 info
->dqi_flags
|= OLQF_CLEAN
;
860 status
= ocfs2_modify_bh(sb_dqopt(sb
)->files
[type
],
870 ocfs2_inode_unlock(sb_dqopt(sb
)->files
[type
], 1);
871 brelse(oinfo
->dqi_libh
);
872 brelse(oinfo
->dqi_lqi_bh
);
877 static void olq_set_dquot(struct buffer_head
*bh
, void *private)
879 struct ocfs2_dquot
*od
= private;
880 struct ocfs2_local_disk_dqblk
*dqblk
;
881 struct super_block
*sb
= od
->dq_dquot
.dq_sb
;
883 dqblk
= (struct ocfs2_local_disk_dqblk
*)(bh
->b_data
884 + ol_dqblk_block_offset(sb
, od
->dq_local_off
));
886 dqblk
->dqb_id
= cpu_to_le64(from_kqid(&init_user_ns
,
887 od
->dq_dquot
.dq_id
));
888 spin_lock(&dq_data_lock
);
889 dqblk
->dqb_spacemod
= cpu_to_le64(od
->dq_dquot
.dq_dqb
.dqb_curspace
-
891 dqblk
->dqb_inodemod
= cpu_to_le64(od
->dq_dquot
.dq_dqb
.dqb_curinodes
-
893 spin_unlock(&dq_data_lock
);
895 (unsigned long long)le64_to_cpu(dqblk
->dqb_spacemod
),
896 (unsigned long long)le64_to_cpu(dqblk
->dqb_inodemod
),
897 from_kqid(&init_user_ns
, od
->dq_dquot
.dq_id
));
900 /* Write dquot to local quota file */
901 int ocfs2_local_write_dquot(struct dquot
*dquot
)
903 struct super_block
*sb
= dquot
->dq_sb
;
904 struct ocfs2_dquot
*od
= OCFS2_DQUOT(dquot
);
905 struct buffer_head
*bh
;
906 struct inode
*lqinode
= sb_dqopt(sb
)->files
[dquot
->dq_id
.type
];
909 status
= ocfs2_read_quota_phys_block(lqinode
, od
->dq_local_phys_blk
,
915 status
= ocfs2_modify_bh(lqinode
, bh
, olq_set_dquot
, od
);
925 /* Find free entry in local quota file */
926 static struct ocfs2_quota_chunk
*ocfs2_find_free_entry(struct super_block
*sb
,
930 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
931 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
932 struct ocfs2_quota_chunk
*chunk
;
933 struct ocfs2_local_disk_chunk
*dchunk
;
936 list_for_each_entry(chunk
, &oinfo
->dqi_chunk
, qc_chunk
) {
937 dchunk
= (struct ocfs2_local_disk_chunk
*)
938 chunk
->qc_headerbh
->b_data
;
939 if (le32_to_cpu(dchunk
->dqc_free
) > 0) {
947 if (chunk
->qc_num
< oinfo
->dqi_chunks
- 1) {
948 len
= ol_chunk_entries(sb
);
950 len
= (oinfo
->dqi_blocks
-
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
,
960 le32_to_cpu(dchunk
->dqc_free
), type
);
961 return ERR_PTR(-EIO
);
967 /* Add new chunk to the local quota file */
968 static struct ocfs2_quota_chunk
*ocfs2_local_quota_add_chunk(
969 struct super_block
*sb
,
973 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
974 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
975 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
976 struct ocfs2_quota_chunk
*chunk
= NULL
;
977 struct ocfs2_local_disk_chunk
*dchunk
;
980 struct buffer_head
*bh
= NULL
, *dbh
= NULL
;
983 /* We are protected by dqio_sem so no locking needed */
984 status
= ocfs2_extend_no_holes(lqinode
, NULL
,
985 lqinode
->i_size
+ 2 * sb
->s_blocksize
,
991 status
= ocfs2_simple_size_update(lqinode
, oinfo
->dqi_lqi_bh
,
992 lqinode
->i_size
+ 2 * sb
->s_blocksize
);
998 chunk
= kmem_cache_alloc(ocfs2_qf_chunk_cachep
, GFP_NOFS
);
1004 /* Local quota info and two new blocks we initialize */
1005 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
1006 OCFS2_LOCAL_QINFO_WRITE_CREDITS
+
1007 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
);
1008 if (IS_ERR(handle
)) {
1009 status
= PTR_ERR(handle
);
1014 /* Initialize chunk header */
1015 status
= ocfs2_extent_map_get_blocks(lqinode
, oinfo
->dqi_blocks
,
1016 &p_blkno
, NULL
, NULL
);
1021 bh
= sb_getblk(sb
, p_blkno
);
1027 dchunk
= (struct ocfs2_local_disk_chunk
*)bh
->b_data
;
1028 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode
), bh
);
1029 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
), bh
,
1030 OCFS2_JOURNAL_ACCESS_CREATE
);
1036 dchunk
->dqc_free
= cpu_to_le32(ol_quota_entries_per_block(sb
));
1037 memset(dchunk
->dqc_bitmap
, 0,
1038 sb
->s_blocksize
- sizeof(struct ocfs2_local_disk_chunk
) -
1039 OCFS2_QBLK_RESERVED_SPACE
);
1041 ocfs2_journal_dirty(handle
, bh
);
1043 /* Initialize new block with structures */
1044 status
= ocfs2_extent_map_get_blocks(lqinode
, oinfo
->dqi_blocks
+ 1,
1045 &p_blkno
, NULL
, NULL
);
1050 dbh
= sb_getblk(sb
, p_blkno
);
1056 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode
), dbh
);
1057 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
), dbh
,
1058 OCFS2_JOURNAL_ACCESS_CREATE
);
1064 memset(dbh
->b_data
, 0, sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
);
1066 ocfs2_journal_dirty(handle
, dbh
);
1068 /* Update local quotafile info */
1069 oinfo
->dqi_blocks
+= 2;
1070 oinfo
->dqi_chunks
++;
1071 status
= ocfs2_local_write_info(sb
, type
);
1076 status
= ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1082 list_add_tail(&chunk
->qc_chunk
, &oinfo
->dqi_chunk
);
1083 chunk
->qc_num
= list_entry(chunk
->qc_chunk
.prev
,
1084 struct ocfs2_quota_chunk
,
1085 qc_chunk
)->qc_num
+ 1;
1086 chunk
->qc_headerbh
= bh
;
1090 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1094 kmem_cache_free(ocfs2_qf_chunk_cachep
, chunk
);
1095 return ERR_PTR(status
);
1098 /* Find free entry in local quota file */
1099 static struct ocfs2_quota_chunk
*ocfs2_extend_local_quota_file(
1100 struct super_block
*sb
,
1104 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
1105 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
1106 struct ocfs2_quota_chunk
*chunk
;
1107 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
1108 struct ocfs2_local_disk_chunk
*dchunk
;
1109 int epb
= ol_quota_entries_per_block(sb
);
1110 unsigned int chunk_blocks
;
1111 struct buffer_head
*bh
;
1116 if (list_empty(&oinfo
->dqi_chunk
))
1117 return ocfs2_local_quota_add_chunk(sb
, type
, offset
);
1118 /* Is the last chunk full? */
1119 chunk
= list_entry(oinfo
->dqi_chunk
.prev
,
1120 struct ocfs2_quota_chunk
, qc_chunk
);
1121 chunk_blocks
= oinfo
->dqi_blocks
-
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
);
1126 /* We are protected by dqio_sem so no locking needed */
1127 status
= ocfs2_extend_no_holes(lqinode
, NULL
,
1128 lqinode
->i_size
+ sb
->s_blocksize
,
1134 status
= ocfs2_simple_size_update(lqinode
, oinfo
->dqi_lqi_bh
,
1135 lqinode
->i_size
+ sb
->s_blocksize
);
1141 /* Get buffer from the just added block */
1142 status
= ocfs2_extent_map_get_blocks(lqinode
, oinfo
->dqi_blocks
,
1143 &p_blkno
, NULL
, NULL
);
1148 bh
= sb_getblk(sb
, p_blkno
);
1154 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode
), bh
);
1156 /* Local quota info, chunk header and the new block we initialize */
1157 handle
= ocfs2_start_trans(OCFS2_SB(sb
),
1158 OCFS2_LOCAL_QINFO_WRITE_CREDITS
+
1159 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
);
1160 if (IS_ERR(handle
)) {
1161 status
= PTR_ERR(handle
);
1165 /* Zero created block */
1166 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
), bh
,
1167 OCFS2_JOURNAL_ACCESS_CREATE
);
1173 memset(bh
->b_data
, 0, sb
->s_blocksize
);
1175 ocfs2_journal_dirty(handle
, bh
);
1177 /* Update chunk header */
1178 status
= ocfs2_journal_access_dq(handle
, INODE_CACHE(lqinode
),
1180 OCFS2_JOURNAL_ACCESS_WRITE
);
1186 dchunk
= (struct ocfs2_local_disk_chunk
*)chunk
->qc_headerbh
->b_data
;
1187 lock_buffer(chunk
->qc_headerbh
);
1188 le32_add_cpu(&dchunk
->dqc_free
, ol_quota_entries_per_block(sb
));
1189 unlock_buffer(chunk
->qc_headerbh
);
1190 ocfs2_journal_dirty(handle
, chunk
->qc_headerbh
);
1192 /* Update file header */
1193 oinfo
->dqi_blocks
++;
1194 status
= ocfs2_local_write_info(sb
, type
);
1200 status
= ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1205 *offset
= chunk_blocks
* epb
;
1208 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
1210 return ERR_PTR(status
);
1213 static void olq_alloc_dquot(struct buffer_head
*bh
, void *private)
1215 int *offset
= private;
1216 struct ocfs2_local_disk_chunk
*dchunk
;
1218 dchunk
= (struct ocfs2_local_disk_chunk
*)bh
->b_data
;
1219 ocfs2_set_bit_unaligned(*offset
, dchunk
->dqc_bitmap
);
1220 le32_add_cpu(&dchunk
->dqc_free
, -1);
1223 /* Create dquot in the local file for given id */
1224 int ocfs2_create_local_dquot(struct dquot
*dquot
)
1226 struct super_block
*sb
= dquot
->dq_sb
;
1227 int type
= dquot
->dq_id
.type
;
1228 struct inode
*lqinode
= sb_dqopt(sb
)->files
[type
];
1229 struct ocfs2_quota_chunk
*chunk
;
1230 struct ocfs2_dquot
*od
= OCFS2_DQUOT(dquot
);
1235 down_write(&OCFS2_I(lqinode
)->ip_alloc_sem
);
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
);
1247 od
->dq_local_off
= ol_dqblk_off(sb
, chunk
->qc_num
, offset
);
1248 od
->dq_chunk
= chunk
;
1249 status
= ocfs2_extent_map_get_blocks(lqinode
,
1250 ol_dqblk_block(sb
, chunk
->qc_num
, offset
),
1251 &od
->dq_local_phys_blk
,
1255 /* Initialize dquot structure on disk */
1256 status
= ocfs2_local_write_dquot(dquot
);
1262 /* Mark structure as allocated */
1263 status
= ocfs2_modify_bh(lqinode
, chunk
->qc_headerbh
, olq_alloc_dquot
,
1270 up_write(&OCFS2_I(lqinode
)->ip_alloc_sem
);
1275 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1276 * already started a transaction and written all changes to global quota file
1278 int ocfs2_local_release_dquot(handle_t
*handle
, struct dquot
*dquot
)
1281 int type
= dquot
->dq_id
.type
;
1282 struct ocfs2_dquot
*od
= OCFS2_DQUOT(dquot
);
1283 struct super_block
*sb
= dquot
->dq_sb
;
1284 struct ocfs2_local_disk_chunk
*dchunk
;
1287 status
= ocfs2_journal_access_dq(handle
,
1288 INODE_CACHE(sb_dqopt(sb
)->files
[type
]),
1289 od
->dq_chunk
->qc_headerbh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1294 offset
= ol_dqblk_chunk_off(sb
, od
->dq_chunk
->qc_num
,
1296 dchunk
= (struct ocfs2_local_disk_chunk
*)
1297 (od
->dq_chunk
->qc_headerbh
->b_data
);
1298 /* Mark structure as freed */
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);
1302 unlock_buffer(od
->dq_chunk
->qc_headerbh
);
1303 ocfs2_journal_dirty(handle
, od
->dq_chunk
->qc_headerbh
);
1306 /* Clear the read bit so that next time someone uses this
1307 * dquot he reads fresh info from disk and allocates local
1308 * dquot structure */
1309 clear_bit(DQ_READ_B
, &dquot
->dq_flags
);
1313 static const struct quota_format_ops ocfs2_format_ops
= {
1314 .check_quota_file
= ocfs2_local_check_quota_file
,
1315 .read_file_info
= ocfs2_local_read_info
,
1316 .write_file_info
= ocfs2_global_write_info
,
1317 .free_file_info
= ocfs2_local_free_info
,
1320 struct quota_format_type ocfs2_quota_format
= {
1321 .qf_fmt_id
= QFMT_OCFS2
,
1322 .qf_ops
= &ocfs2_format_ops
,
1323 .qf_owner
= THIS_MODULE