quotafmt_test.c

来自「lustre 1.6.5 source code」· C语言 代码 · 共 509 行 · 第 1/2 页

C
509
字号
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: * *   No redistribution or use is permitted outside of Cluster File Systems, Inc. * * Kernel module to test lustre administrative quotafile format APIs * from the OBD setup function */#ifndef EXPORT_SYMTAB# define EXPORT_SYMTAB#endif#include <linux/module.h>#include <linux/init.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/random.h>#include <lustre_quota.h>#include <obd_class.h>#include "lustre_quota_fmt.h"char *test_quotafile[2] = { "usrquota_test", "grpquota_test" };static int quotfmt_initialize(struct lustre_quota_info *lqi,                              struct obd_device *tgt,                              struct lvfs_run_ctxt *saved){        struct lustre_disk_dqheader dqhead;        static const uint quota_magics[] = LUSTRE_INITQMAGICS;        static const uint quota_versions[] = LUSTRE_INITQVERSIONS;        struct file *fp;        struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;        size_t size;        struct dentry *de;        int i, rc = 0;        ENTRY;        push_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);        for (i = 0; i < MAXQUOTAS; i++) {                loff_t offset = 0;                char *name = test_quotafile[i];                int namelen = strlen(name);                /* remove the stale test quotafile */                LOCK_INODE_MUTEX(parent_inode);                de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);                if (!IS_ERR(de) && de->d_inode)                        vfs_unlink(parent_inode, de);                if (!IS_ERR(de))                        dput(de);                UNLOCK_INODE_MUTEX(parent_inode);                /* create quota file */                fp = filp_open(name, O_CREAT | O_EXCL, 0644);                if (IS_ERR(fp)) {                        rc = PTR_ERR(fp);                        CERROR("error creating test quotafile %s (rc = %d)\n",                               name, rc);                        break;                }                lqi->qi_files[i] = fp;                /* write quotafile header */                dqhead.dqh_magic = cpu_to_le32(quota_magics[i]);                dqhead.dqh_version = cpu_to_le32(quota_versions[i]);                size = fp->f_op->write(fp, (char *)&dqhead,                                       sizeof(struct lustre_disk_dqheader),                                       &offset);                if (size != sizeof(struct lustre_disk_dqheader)) {                        CERROR("error writing quotafile header %s (rc = %d)\n",                               name, rc);                        rc = size;                        break;                }        }        RETURN(rc);}static int quotfmt_finalize(struct lustre_quota_info *lqi,                            struct obd_device *tgt, struct lvfs_run_ctxt *saved){        struct dentry *de;        struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;        int i, rc = 0;        ENTRY;        for (i = 0; i < MAXQUOTAS; i++) {                char *name = test_quotafile[i];                int namelen = strlen(name);                if (lqi->qi_files[i] == NULL)                        continue;                /* close quota file */                filp_close(lqi->qi_files[i], 0);                /* unlink quota file */                LOCK_INODE_MUTEX(parent_inode);                de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);                if (IS_ERR(de) || de->d_inode == NULL) {                        rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;                        CERROR("error lookup quotafile %s (rc = %d)\n",                               name, rc);                        goto dput;                }                rc = vfs_unlink(parent_inode, de);                if (rc)                        CERROR("error unlink quotafile %s (rc = %d)\n",                               name, rc);              dput:                if (!IS_ERR(de))                        dput(de);                UNLOCK_INODE_MUTEX(parent_inode);        }        pop_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);        RETURN(rc);}static int quotfmt_test_1(struct lustre_quota_info *lqi){        int i;        ENTRY;        for (i = 0; i < MAXQUOTAS; i++) {                if (lustre_check_quota_file(lqi, i))                        RETURN(-EINVAL);        }        RETURN(0);}static void print_quota_info(struct lustre_quota_info *lqi){#if 0        struct lustre_mem_dqinfo *dqinfo;        int i;        for (i = 0; i < MAXQUOTAS; i++) {                dqinfo = &lqi->qi_info[i];                printk("%s quota info:\n", i == USRQUOTA ? "user " : "group");                printk                    ("dqi_bgrace(%u) dqi_igrace(%u) dqi_flags(%lu) dqi_blocks(%u) "                     "dqi_free_blk(%u) dqi_free_entry(%u)\n",                     dqinfo->dqi_bgrace, dqinfo->dqi_igrace, dqinfo->dqi_flags,                     dqinfo->dqi_blocks, dqinfo->dqi_free_blk,                     dqinfo->dqi_free_entry);        }#endif}static int quotfmt_test_2(struct lustre_quota_info *lqi){        int i, rc = 0;        ENTRY;        for (i = 0; i < MAXQUOTAS; i++) {                struct lustre_mem_dqinfo dqinfo;                rc = lustre_init_quota_info(lqi, i);                if (rc) {                        CERROR("init quotainfo(%d) failed! (rc:%d)\n", i, rc);                        break;                }                memcpy(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo));                rc = lustre_read_quota_info(lqi, i);                if (rc) {                        CERROR("read quotainfo(%d) failed! (rc:%d)\n", i, rc);                        break;                }                if (memcmp(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo))) {                        rc = -EINVAL;                        break;                }        }        RETURN(rc);}static struct lustre_dquot *get_rand_dquot(struct lustre_quota_info *lqi){        struct lustre_dquot *dquot;        unsigned int rand;        OBD_ALLOC(dquot, sizeof(*dquot));        if (dquot == NULL)                return NULL;        get_random_bytes(&rand, sizeof(rand));        if (!rand)                rand = 1000;        dquot->dq_info = lqi;        dquot->dq_id = rand % 1000 + 1;        dquot->dq_type = rand % MAXQUOTAS;        dquot->dq_dqb.dqb_bhardlimit = rand;        dquot->dq_dqb.dqb_bsoftlimit = rand / 2;        dquot->dq_dqb.dqb_curspace = rand / 3;        dquot->dq_dqb.dqb_ihardlimit = rand;        dquot->dq_dqb.dqb_isoftlimit = rand / 2;        dquot->dq_dqb.dqb_curinodes = rand / 3;        dquot->dq_dqb.dqb_btime = jiffies;        dquot->dq_dqb.dqb_itime = jiffies;        return dquot;}static void put_rand_dquot(struct lustre_dquot *dquot){        OBD_FREE(dquot, sizeof(*dquot));}static int write_check_dquot(struct lustre_quota_info *lqi){        struct lustre_dquot *dquot;        struct lustre_mem_dqblk dqblk;        int rc = 0;        ENTRY;        dquot = get_rand_dquot(lqi);        if (dquot == NULL)                RETURN(-ENOMEM);        /* for already exists entry, we set the dq_off by read_dquot */        rc = lustre_read_dquot(dquot);        if (rc) {                CERROR("read dquot failed! (rc:%d)\n", rc);                GOTO(out, rc);        }        clear_bit(DQ_FAKE_B, &dquot->dq_flags);        /* for already exists entry, we rewrite it */        rc = lustre_commit_dquot(dquot);        if (rc) {                CERROR("commit dquot failed! (rc:%d)\n", rc);                GOTO(out, rc);        }        memcpy(&dqblk, &dquot->dq_dqb, sizeof(dqblk));        memset(&dquot->dq_dqb, 0, sizeof(dqblk));        rc = lustre_read_dquot(dquot);        if (rc) {                CERROR("read dquot failed! (rc:%d)\n", rc);                GOTO(out, rc);        }        if (memcmp(&dqblk, &dquot->dq_dqb, sizeof(dqblk))) {                rc = -EINVAL;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?