4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/lvfs/quotafmt_test.c
36 * No redistribution or use is permitted outside of Sun Microsystems, Inc.
38 * Kernel module to test lustre administrative quotafile format APIs
39 * from the OBD setup function
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/errno.h>
46 #include <linux/kernel.h>
48 #include <lustre_quota.h>
49 #include <obd_class.h>
51 #include "lustre_quota_fmt.h"
53 char *test_quotafile[2] = { "usrquota_test", "grpquota_test" };
55 static int quotfmt_initialize(struct lustre_quota_info *lqi,
56 struct obd_device *tgt,
57 struct lvfs_run_ctxt *saved)
59 struct lustre_disk_dqheader dqhead;
60 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
61 static const uint quota_versions[] = LUSTRE_INITQVERSIONS_V2;
63 struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;
69 push_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);
71 for (i = 0; i < MAXQUOTAS; i++) {
73 char *name = test_quotafile[i];
74 int namelen = strlen(name);
76 /* remove the stale test quotafile */
77 mutex_lock_nested(&parent_inode->i_mutex, I_MUTEX_PARENT);
78 de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);
79 if (!IS_ERR(de) && de->d_inode)
80 ll_vfs_unlink(parent_inode, de,
81 tgt->obd_lvfs_ctxt.pwdmnt);
84 mutex_unlock(&parent_inode->i_mutex);
86 /* create quota file */
87 fp = filp_open(name, O_CREAT | O_EXCL, 0644);
90 CERROR("error creating test quotafile %s (rc = %d)\n",
94 lqi->qi_files[i] = fp;
96 /* write quotafile header */
97 dqhead.dqh_magic = cpu_to_le32(quota_magics[i]);
98 dqhead.dqh_version = cpu_to_le32(quota_versions[i]);
99 size = fp->f_op->write(fp, (char *)&dqhead,
100 sizeof(struct lustre_disk_dqheader),
102 if (size != sizeof(struct lustre_disk_dqheader)) {
103 CERROR("error writing quotafile header %s (rc = %d)\n",
113 static int quotfmt_finalize(struct lustre_quota_info *lqi,
114 struct obd_device *tgt, struct lvfs_run_ctxt *saved)
117 struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;
121 for (i = 0; i < MAXQUOTAS; i++) {
122 char *name = test_quotafile[i];
123 int namelen = strlen(name);
125 if (lqi->qi_files[i] == NULL)
128 /* close quota file */
129 filp_close(lqi->qi_files[i], 0);
131 /* unlink quota file */
132 mutex_lock_nested(&parent_inode->i_mutex, I_MUTEX_PARENT);
134 de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);
135 if (IS_ERR(de) || de->d_inode == NULL) {
136 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
137 CERROR("error lookup quotafile %s (rc = %d)\n",
142 rc = ll_vfs_unlink(parent_inode, de, tgt->obd_lvfs_ctxt.pwdmnt);
144 CERROR("error unlink quotafile %s (rc = %d)\n",
149 mutex_unlock(&parent_inode->i_mutex);
152 pop_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);
156 static int quotfmt_test_1(struct lustre_quota_info *lqi)
161 for (i = 0; i < MAXQUOTAS; i++) {
162 if (lustre_check_quota_file(lqi, i))
168 static void print_quota_info(struct lustre_quota_info *lqi)
171 struct lustre_mem_dqinfo *dqinfo;
174 for (i = 0; i < MAXQUOTAS; i++) {
175 dqinfo = &lqi->qi_info[i];
176 CDEBUG(D_INFO, "%s quota info:\n", i == USRQUOTA ? "user " : "group");
177 CDEBUG(D_INFO, "dqi_bgrace(%u) dqi_igrace(%u) dqi_flags(%lu) dqi_blocks(%u) "
178 "dqi_free_blk(%u) dqi_free_entry(%u)\n",
179 dqinfo->dqi_bgrace, dqinfo->dqi_igrace, dqinfo->dqi_flags,
180 dqinfo->dqi_blocks, dqinfo->dqi_free_blk,
181 dqinfo->dqi_free_entry);
186 static int quotfmt_test_2(struct lustre_quota_info *lqi)
191 for (i = 0; i < MAXQUOTAS; i++) {
192 struct lustre_mem_dqinfo dqinfo;
194 rc = lustre_init_quota_info(lqi, i);
196 CERROR("init quotainfo(%d) failed! (rc:%d)\n", i, rc);
199 memcpy(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo));
201 rc = lustre_read_quota_info(lqi, i);
203 CERROR("read quotainfo(%d) failed! (rc:%d)\n", i, rc);
207 if (memcmp(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo))) {
215 static struct lustre_dquot *get_rand_dquot(struct lustre_quota_info *lqi)
217 struct lustre_dquot *dquot;
220 OBD_ALLOC(dquot, sizeof(*dquot));
224 cfs_get_random_bytes(&rand, sizeof(rand));
228 dquot->dq_info = lqi;
229 dquot->dq_id = rand % 1000 + 1;
230 dquot->dq_type = rand % MAXQUOTAS;
232 dquot->dq_dqb.dqb_bhardlimit = rand;
233 dquot->dq_dqb.dqb_bsoftlimit = rand / 2;
234 dquot->dq_dqb.dqb_curspace = rand / 3;
235 dquot->dq_dqb.dqb_ihardlimit = rand;
236 dquot->dq_dqb.dqb_isoftlimit = rand / 2;
237 dquot->dq_dqb.dqb_curinodes = rand / 3;
238 dquot->dq_dqb.dqb_btime = jiffies;
239 dquot->dq_dqb.dqb_itime = jiffies;
244 static void put_rand_dquot(struct lustre_dquot *dquot)
246 OBD_FREE(dquot, sizeof(*dquot));
249 static int write_check_dquot(struct lustre_quota_info *lqi)
251 struct lustre_dquot *dquot;
252 struct lustre_mem_dqblk dqblk;
256 dquot = get_rand_dquot(lqi);
260 /* for already exists entry, we set the dq_off by read_dquot */
261 rc = lustre_read_dquot(dquot);
263 CERROR("read dquot failed! (rc:%d)\n", rc);
267 cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
268 /* for already exists entry, we rewrite it */
269 rc = lustre_commit_dquot(dquot);
271 CERROR("commit dquot failed! (rc:%d)\n", rc);
274 memcpy(&dqblk, &dquot->dq_dqb, sizeof(dqblk));
275 memset(&dquot->dq_dqb, 0, sizeof(dqblk));
277 rc = lustre_read_dquot(dquot);
279 CERROR("read dquot failed! (rc:%d)\n", rc);
283 if (memcmp(&dqblk, &dquot->dq_dqb, sizeof(dqblk))) {
288 put_rand_dquot(dquot);
292 static int quotfmt_test_3(struct lustre_quota_info *lqi)
294 struct lustre_dquot *dquot;
298 dquot = get_rand_dquot(lqi);
302 cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
303 /* write a new dquot */
304 rc = lustre_commit_dquot(dquot);
306 CERROR("commit dquot failed! (rc:%d)\n", rc);
310 memset(&dquot->dq_dqb, 0, sizeof(dquot->dq_dqb));
312 /* check if this dquot is on disk now */
313 rc = lustre_read_dquot(dquot);
315 CERROR("read dquot failed! (rc:%d)\n", rc);
318 if (!dquot->dq_off || cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags)) {
319 CERROR("the dquot isn't committed\n");
320 GOTO(out, rc = -EINVAL);
323 /* remove this dquot */
324 cfs_set_bit(DQ_FAKE_B, &dquot->dq_flags);
325 dquot->dq_dqb.dqb_curspace = 0;
326 dquot->dq_dqb.dqb_curinodes = 0;
327 rc = lustre_commit_dquot(dquot);
329 CERROR("remove dquot failed! (rc:%d)\n", rc);
333 /* check if the dquot is really removed */
334 cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
336 rc = lustre_read_dquot(dquot);
338 CERROR("read dquot failed! (rc:%d)\n", rc);
341 if (!cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags) || dquot->dq_off) {
342 CERROR("the dquot isn't removed!\n");
343 GOTO(out, rc = -EINVAL);
346 /* check if this dquot can be write again */
350 print_quota_info(lqi);
353 put_rand_dquot(dquot);
357 static int quotfmt_test_4(struct lustre_quota_info *lqi)
362 for (i = 0; i < 30000; i++) {
363 rc = write_check_dquot(lqi);
365 CERROR("write/check dquot failed at %d! (rc:%d)\n",
370 print_quota_info(lqi);
374 static int quotfmt_run_tests(struct obd_device *obd, struct obd_device *tgt)
376 struct lvfs_run_ctxt saved;
377 struct lustre_quota_info *lqi = NULL;
381 OBD_ALLOC(lqi, sizeof(*lqi));
383 CERROR("not enough memory\n");
387 CWARN("=== Initialize quotafile test\n");
388 rc = quotfmt_initialize(lqi, tgt, &saved);
392 CWARN("=== test 1: check quota header\n");
393 rc = quotfmt_test_1(lqi);
395 CERROR("check quota header failed! (rc:%d)\n", rc);
399 CWARN("=== test 2: write/read quota info\n");
400 rc = quotfmt_test_2(lqi);
402 CERROR("write/read quota info failed! (rc:%d)\n", rc);
406 CWARN("=== test 3: write/remove dquot\n");
407 rc = quotfmt_test_3(lqi);
409 CERROR("write/remove dquot failed! (rc:%d)\n", rc);
413 CWARN("=== test 4: write/read 30000 dquot\n");
414 rc = quotfmt_test_4(lqi);
416 CERROR("write/read 30000 dquot failed\n");
421 CWARN("=== Finalize quotafile test\n");
422 rc = quotfmt_finalize(lqi, tgt, &saved);
423 OBD_FREE(lqi, sizeof(*lqi));
427 static int quotfmt_test_cleanup(struct obd_device *obd)
430 lprocfs_obd_cleanup(obd);
434 static int quotfmt_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
436 struct lprocfs_static_vars lvars;
437 struct obd_device *tgt;
441 if (lcfg->lcfg_bufcount < 1) {
442 CERROR("requires a mds OBD name\n");
446 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
447 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
448 CERROR("target device not attached or not set up (%s)\n",
449 lustre_cfg_string(lcfg, 1));
453 rc = quotfmt_run_tests(obd, tgt);
455 quotfmt_test_cleanup(obd);
457 lprocfs_quotfmt_test_init_vars(&lvars);
458 lprocfs_obd_setup(obd, lvars.obd_vars);
463 static struct obd_ops quotfmt_obd_ops = {
464 .o_owner = THIS_MODULE,
465 .o_setup = quotfmt_test_setup,
466 .o_cleanup = quotfmt_test_cleanup,
470 static struct lprocfs_vars lprocfs_quotfmt_test_obd_vars[] = { {0} };
471 static struct lprocfs_vars lprocfs_quotfmt_test_module_vars[] = { {0} };
473 void lprocfs_quotfmt_test_init_vars(struct lprocfs_static_vars *lvars)
475 lvars->module_vars = lprocfs_quotfmt_test_module_vars;
476 lvars->obd_vars = lprocfs_quotfmt_test_obd_vars;
479 static int __init quotfmt_test_init(void)
481 struct lprocfs_static_vars lvars;
483 lprocfs_quotfmt_test_init_vars(&lvars);
484 return class_register_type("fmt_obd_ops, NULL, lvars.module_vars,
485 "quotfmt_test", NULL);
488 static void __exit quotfmt_test_exit(void)
490 class_unregister_type("quotfmt_test");
493 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
494 MODULE_DESCRIPTION("administrative quotafile test module");
495 MODULE_LICENSE("GPL");
497 module_init(quotfmt_test_init);
498 module_exit(quotfmt_test_exit);