/*
* Implementation of operations over local quota file
*/
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/quota.h>
#include <linux/quotaops.h>
#include <linux/module.h>
#include <cluster/masklog.h>
#include "ocfs2_fs.h"
#include "ocfs2.h"
#include "inode.h"
#include "alloc.h"
#include "file.h"
#include "buffer_head_io.h"
#include "journal.h"
#include "sysfile.h"
#include "dlmglue.h"
#include "quota.h"
#include "uptodate.h"
#include "super.h"
#include "ocfs2_trace.h"
/* Number of local quota structures per block */
static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
{
return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
sizeof(struct ocfs2_local_disk_dqblk));
}
/* Number of blocks with entries in one chunk */
static inline unsigned int ol_chunk_blocks(struct super_block *sb)
{
return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
OCFS2_QBLK_RESERVED_SPACE) << 3) /
ol_quota_entries_per_block(sb);
}
/* Number of entries in a chunk bitmap */
static unsigned int ol_chunk_entries(struct super_block *sb)
{
return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
}
/* Offset of the chunk in quota file */
static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
{
/* 1 block for local quota file info, 1 block per chunk for chunk info */
return 1 + (ol_chunk_blocks(sb) + 1) * c;
}
static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
{
int epb = ol_quota_entries_per_block(sb);
return ol_quota_chunk_block(sb, c) + 1 + off / epb;
}
static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
{
int epb = ol_quota_entries_per_block(sb);
return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
}
/* Offset of the dquot structure in the quota file */
static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
{
return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
ol_dqblk_block_off(sb, c, off);
}
/* Compute block number from given offset */
static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
{
return off >> sb->s_blocksize_bits;
}
static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
{
return off & ((1 << sb->s_blocksize_bits) - 1);
}
/* Compute offset in the chunk of a structure with the given offset */
static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
{
int epb = ol_quota_entries_per_block(sb);
return ((off >> sb->s_blocksize_bits) -
ol_quota_chunk_block(sb, c) - 1) * epb
+ ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
sizeof(struct ocfs2_local_disk_dqblk);
}
/* Write bufferhead into the fs */
static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
void (*modify)(struct buffer_head *, void *), void *private)
{
struct super_block *sb = inode->i_sb;
handle_t *handle;
int status;
handle = ocfs2_start_trans(OCFS2_SB(sb),
OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
if (IS_ERR(handle)) {
status = PTR_ERR(handle);
mlog_errno(status);
return status;
}
status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
OCFS2_JOURNAL_ACCESS_WRITE);
if (status < 0) {
mlog_errno(status);
ocfs2_commit_trans(OCFS2_SB(sb), handle);
return status;
}
lock_buffer(bh);
modify(bh, private);
unlock_buffer(bh);
ocfs2_journal_dirty(handle, bh);
status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
if (status < 0) {
mlog_errno(status);
return status;
}
return 0;
}
/*
* Read quota block from a given logical offset.
*
* This function acquires ip_alloc_sem and thus it must not be called with a
* transaction started.
*/
static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
struct buffer_head **bh)
{
int rc = 0;
struct buffer_head *tmp = *bh;
if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
ocfs2_error(inode->i_sb,
"Quota file %llu is probably corrupted! Requested "
"to read block %Lu but file has size only %Lu\n",
(unsigned long long)OCFS2_I(inode)->ip_blkno,
(unsigned long long)v_block,
(unsigned long long)i_size_read(inode));
return -EIO;
}
rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
ocfs2_validate_quota_block);
if (rc)
mlog_errno(rc);
/* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
if (!rc && !*bh)
*bh = tmp;
return rc;
}
/* Check whether we understand format of quota files */
static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
{
unsigned int lmagics[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
unsigned int lversions[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
unsigned int gmagics[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
unsigned int gversions[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
GROUP_QUOTA_SYSTEM_INODE };
struct buffer_head *bh = NULL;
struct inode *linode = sb_dqopt(sb)->files[type];
struct inode *ginode = NULL;
struct ocfs2_disk_dqheader *dqhead;
int status, ret = 0;
/* First check whether we understand local quota file */
status = ocfs2_read_quota_block(linode, 0, &bh);
if (status) {
mlog_errno(status);
mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
type);
goto out_err;
}
dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
" type=%d\n", le32_to_cpu(dqhead->dqh_magic),
lmagics[type], type);
goto out_err;
}
if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
mlog(ML_ERROR, "quota file version does not match (%u != %u),"
" type=%d\n", le32_to_cpu(dqhead->dqh_version),
lversions[type], type);
goto out_err;
}
brelse(bh);
bh = NULL;
/* Next check whether we understand global quota file */
ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
OCFS2_INVALID_SLOT);
if (!ginode) {
mlog(ML_ERROR, "cannot get global quota file inode "
"(type=%d)\n", type);
goto out_err;
}
/* Since the header is read only, we don't care about locking */
status = ocfs2_read_quota_block(ginode, 0, &bh);
if (status) {
mlog_errno(status);
mlog(ML_ERROR, "failed to read global quota file header "
"(type=%d)\n", type);
goto out_err;
}
dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
mlog(ML_ERROR, "global quota file magic does not match "
"(%u != %u), type=%d\n",
le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
goto out_err;
}
if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
mlog(ML_ERROR, "global quota file version does not match "
"(%u != %u), type=%d\n",
le32_to_cpu(dqhead->dqh_version), gversions[type],
type);
goto out_err;
}
ret = 1;
out_err:
brelse(bh);
iput(ginode);
return ret;
}
/* Release given list of quota file chunks */
static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
{
struct ocfs2_quota_chunk *pos, *next;
list_for_each_entry_safe(pos, next, head, qc_chunk) {
list_del(&pos->qc_chunk);
brelse(pos->qc_headerbh);
kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
}
}
/* Load quota bitmaps into memory */
static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
struct ocfs2_local_disk_dqinfo *ldinfo,
struct list_head *head)
{
struct ocfs2_quota_chunk *newchunk;
int i, status;
INIT_LIST_HEAD(head);
for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
if (!newchunk) {
ocfs2_release_local_quota_bitmaps(head);
return -ENOMEM;
}
newchunk->qc_num = i;
newchunk->qc_headerbh = NULL;
status = ocfs2_read_quota_block(inode,
ol_quota_chunk_block(inode->i_sb, i),
&newchunk->qc_headerbh);
if (status) {
mlog_errno(status);
kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
ocfs