1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/quotafmt_test.c
38 * No redistribution or use is permitted outside of Sun Microsystems, Inc.
40 * Kernel module to test lustre administrative quotafile format APIs
41 * from the OBD setup function
45 # define EXPORT_SYMTAB
48 #include <linux/module.h>
49 #include <linux/init.h>
50 #include <linux/errno.h>
52 #include <linux/kernel.h>
53 #include <linux/random.h>
55 #include <lustre_quota.h>
56 #include <obd_class.h>
58 #include "lustre_quota_fmt.h"
60 #ifdef HAVE_QUOTA_SUPPORT
62 char *test_quotafile[2] = { "usrquota_test", "grpquota_test" };
64 static int quotfmt_initialize(struct lustre_quota_info *lqi,
65 struct obd_device *tgt,
66 struct lvfs_run_ctxt *saved)
68 struct lustre_disk_dqheader dqhead;
69 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
70 static const uint quota_versions[] = LUSTRE_INITQVERSIONS;
72 struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;
78 push_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);
80 for (i = 0; i < MAXQUOTAS; i++) {
82 char *name = test_quotafile[i];
83 int namelen = strlen(name);
85 /* remove the stale test quotafile */
86 LOCK_INODE_MUTEX(parent_inode);
87 de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);
88 if (!IS_ERR(de) && de->d_inode)
89 ll_vfs_unlink(parent_inode, de,
90 tgt->obd_lvfs_ctxt.pwdmnt);
93 UNLOCK_INODE_MUTEX(parent_inode);
95 /* create quota file */
96 fp = filp_open(name, O_CREAT | O_EXCL, 0644);
99 CERROR("error creating test quotafile %s (rc = %d)\n",
103 lqi->qi_files[i] = fp;
105 /* write quotafile header */
106 dqhead.dqh_magic = cpu_to_le32(quota_magics[i]);
107 dqhead.dqh_version = cpu_to_le32(quota_versions[i]);
108 size = fp->f_op->write(fp, (char *)&dqhead,
109 sizeof(struct lustre_disk_dqheader),
111 if (size != sizeof(struct lustre_disk_dqheader)) {
112 CERROR("error writing quotafile header %s (rc = %d)\n",
122 static int quotfmt_finalize(struct lustre_quota_info *lqi,
123 struct obd_device *tgt, struct lvfs_run_ctxt *saved)
126 struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;
130 for (i = 0; i < MAXQUOTAS; i++) {
131 char *name = test_quotafile[i];
132 int namelen = strlen(name);
134 if (lqi->qi_files[i] == NULL)
137 /* close quota file */
138 filp_close(lqi->qi_files[i], 0);
140 /* unlink quota file */
141 LOCK_INODE_MUTEX(parent_inode);
143 de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);
144 if (IS_ERR(de) || de->d_inode == NULL) {
145 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
146 CERROR("error lookup quotafile %s (rc = %d)\n",
151 rc = ll_vfs_unlink(parent_inode, de, tgt->obd_lvfs_ctxt.pwdmnt);
153 CERROR("error unlink quotafile %s (rc = %d)\n",
158 UNLOCK_INODE_MUTEX(parent_inode);
161 pop_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);
165 static int quotfmt_test_1(struct lustre_quota_info *lqi)
170 for (i = 0; i < MAXQUOTAS; i++) {
171 if (lustre_check_quota_file(lqi, i))
177 static void print_quota_info(struct lustre_quota_info *lqi)
180 struct lustre_mem_dqinfo *dqinfo;
183 for (i = 0; i < MAXQUOTAS; i++) {
184 dqinfo = &lqi->qi_info[i];
185 printk("%s quota info:\n", i == USRQUOTA ? "user " : "group");
187 ("dqi_bgrace(%u) dqi_igrace(%u) dqi_flags(%lu) dqi_blocks(%u) "
188 "dqi_free_blk(%u) dqi_free_entry(%u)\n",
189 dqinfo->dqi_bgrace, dqinfo->dqi_igrace, dqinfo->dqi_flags,
190 dqinfo->dqi_blocks, dqinfo->dqi_free_blk,
191 dqinfo->dqi_free_entry);
196 static int quotfmt_test_2(struct lustre_quota_info *lqi)
201 for (i = 0; i < MAXQUOTAS; i++) {
202 struct lustre_mem_dqinfo dqinfo;
204 rc = lustre_init_quota_info(lqi, i);
206 CERROR("init quotainfo(%d) failed! (rc:%d)\n", i, rc);
209 memcpy(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo));
211 rc = lustre_read_quota_info(lqi, i);
213 CERROR("read quotainfo(%d) failed! (rc:%d)\n", i, rc);
217 if (memcmp(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo))) {
225 static struct lustre_dquot *get_rand_dquot(struct lustre_quota_info *lqi)
227 struct lustre_dquot *dquot;
230 OBD_ALLOC(dquot, sizeof(*dquot));
234 get_random_bytes(&rand, sizeof(rand));
238 dquot->dq_info = lqi;
239 dquot->dq_id = rand % 1000 + 1;
240 dquot->dq_type = rand % MAXQUOTAS;
242 dquot->dq_dqb.dqb_bhardlimit = rand;
243 dquot->dq_dqb.dqb_bsoftlimit = rand / 2;
244 dquot->dq_dqb.dqb_curspace = rand / 3;
245 dquot->dq_dqb.dqb_ihardlimit = rand;
246 dquot->dq_dqb.dqb_isoftlimit = rand / 2;
247 dquot->dq_dqb.dqb_curinodes = rand / 3;
248 dquot->dq_dqb.dqb_btime = jiffies;
249 dquot->dq_dqb.dqb_itime = jiffies;
254 static void put_rand_dquot(struct lustre_dquot *dquot)
256 OBD_FREE(dquot, sizeof(*dquot));
259 static int write_check_dquot(struct lustre_quota_info *lqi)
261 struct lustre_dquot *dquot;
262 struct lustre_mem_dqblk dqblk;
266 dquot = get_rand_dquot(lqi);
270 /* for already exists entry, we set the dq_off by read_dquot */
271 rc = lustre_read_dquot(dquot);
273 CERROR("read dquot failed! (rc:%d)\n", rc);
277 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
278 /* for already exists entry, we rewrite it */
279 rc = lustre_commit_dquot(dquot);
281 CERROR("commit dquot failed! (rc:%d)\n", rc);
284 memcpy(&dqblk, &dquot->dq_dqb, sizeof(dqblk));
285 memset(&dquot->dq_dqb, 0, sizeof(dqblk));
287 rc = lustre_read_dquot(dquot);
289 CERROR("read dquot failed! (rc:%d)\n", rc);
293 if (memcmp(&dqblk, &dquot->dq_dqb, sizeof(dqblk))) {
298 put_rand_dquot(dquot);
302 static int quotfmt_test_3(struct lustre_quota_info *lqi)
304 struct lustre_dquot *dquot;
308 dquot = get_rand_dquot(lqi);
312 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
313 /* write a new dquot */
314 rc = lustre_commit_dquot(dquot);
316 CERROR("commit dquot failed! (rc:%d)\n", rc);
320 memset(&dquot->dq_dqb, 0, sizeof(dquot->dq_dqb));
322 /* check if this dquot is on disk now */
323 rc = lustre_read_dquot(dquot);
325 CERROR("read dquot failed! (rc:%d)\n", rc);
328 if (!dquot->dq_off || test_bit(DQ_FAKE_B, &dquot->dq_flags)) {
329 CERROR("the dquot isn't committed\n");
330 GOTO(out, rc = -EINVAL);
333 /* remove this dquot */
334 set_bit(DQ_FAKE_B, &dquot->dq_flags);
335 dquot->dq_dqb.dqb_curspace = 0;
336 dquot->dq_dqb.dqb_curinodes = 0;
337 rc = lustre_commit_dquot(dquot);
339 CERROR("remove dquot failed! (rc:%d)\n", rc);
343 /* check if the dquot is really removed */
344 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
346 rc = lustre_read_dquot(dquot);
348 CERROR("read dquot failed! (rc:%d)\n", rc);
351 if (!test_bit(DQ_FAKE_B, &dquot->dq_flags) || dquot->dq_off) {
352 CERROR("the dquot isn't removed!\n");
353 GOTO(out, rc = -EINVAL);
356 /* check if this dquot can be write again */
360 print_quota_info(lqi);
363 put_rand_dquot(dquot);
367 static int quotfmt_test_4(struct lustre_quota_info *lqi)
372 for (i = 0; i < 30000; i++) {
373 rc = write_check_dquot(lqi);
375 CERROR("write/check dquot failed at %d! (rc:%d)\n",
380 print_quota_info(lqi);
384 static int quotfmt_test_5(struct lustre_quota_info *lqi)
386 #ifndef KERNEL_SUPPORTS_QUOTA_READ
389 for (i = USRQUOTA; i < MAXQUOTAS && !rc; i++) {
390 struct list_head list;
391 struct dquot_id *dqid, *tmp;
393 INIT_LIST_HEAD(&list);
394 rc = lustre_get_qids(lqi->qi_files[i], NULL, i, &list);
396 CERROR("%s get all %ss (rc:%d):\n",
397 rc ? "error" : "success",
398 i == USRQUOTA ? "uid" : "gid", rc);
400 list_for_each_entry_safe(dqid, tmp, &list, di_link) {
401 list_del_init(&dqid->di_link);
403 printk("%d ", dqid->di_id);
410 CWARN("kernel supports quota_read OR kernel version >= 2.6.12, test skipped\n");
415 static int quotfmt_run_tests(struct obd_device *obd, struct obd_device *tgt)
417 struct lvfs_run_ctxt saved;
418 struct lustre_quota_info *lqi = NULL;
422 OBD_ALLOC(lqi, sizeof(*lqi));
424 CERROR("not enough memory\n");
428 CWARN("=== Initialize quotafile test\n");
429 rc = quotfmt_initialize(lqi, tgt, &saved);
433 CWARN("=== test 1: check quota header\n");
434 rc = quotfmt_test_1(lqi);
436 CERROR("check quota header failed! (rc:%d)\n", rc);
440 CWARN("=== test 2: write/read quota info\n");
441 rc = quotfmt_test_2(lqi);
443 CERROR("write/read quota info failed! (rc:%d)\n", rc);
447 CWARN("=== test 3: write/remove dquot\n");
448 rc = quotfmt_test_3(lqi);
450 CERROR("write/remove dquot failed! (rc:%d)\n", rc);
454 CWARN("=== test 4: write/read 30000 dquot\n");
455 rc = quotfmt_test_4(lqi);
457 CERROR("write/read 30000 dquot failed\n");
461 CWARN("=== test 5: walk through quota file to get all ids\n");
462 rc = quotfmt_test_5(lqi);
464 CERROR("walk through quota file failed\n");
469 CWARN("=== Finalize quotafile test\n");
470 rc = quotfmt_finalize(lqi, tgt, &saved);
471 OBD_FREE(lqi, sizeof(*lqi));
475 static int quotfmt_test_cleanup(struct obd_device *obd)
478 lprocfs_obd_cleanup(obd);
482 static int quotfmt_test_setup(struct obd_device *obd, obd_count len, void *buf)
484 struct lprocfs_static_vars lvars;
485 struct lustre_cfg *lcfg = buf;
486 struct obd_device *tgt;
490 if (lcfg->lcfg_bufcount < 1) {
491 CERROR("requires a mds OBD name\n");
495 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
496 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
497 CERROR("target device not attached or not set up (%s)\n",
498 lustre_cfg_string(lcfg, 1));
502 rc = quotfmt_run_tests(obd, tgt);
504 quotfmt_test_cleanup(obd);
506 lprocfs_quotfmt_test_init_vars(&lvars);
507 lprocfs_obd_setup(obd, lvars.obd_vars);
512 static struct obd_ops quotfmt_obd_ops = {
513 .o_owner = THIS_MODULE,
514 .o_setup = quotfmt_test_setup,
515 .o_cleanup = quotfmt_test_cleanup,
519 static struct lprocfs_vars lprocfs_quotfmt_test_obd_vars[] = { {0} };
520 static struct lprocfs_vars lprocfs_quotfmt_test_module_vars[] = { {0} };
522 void lprocfs_quotfmt_test_init_vars(struct lprocfs_static_vars *lvars)
524 lvars->module_vars = lprocfs_quotfmt_test_module_vars;
525 lvars->obd_vars = lprocfs_quotfmt_test_obd_vars;
528 static int __init quotfmt_test_init(void)
530 struct lprocfs_static_vars lvars;
532 lprocfs_quotfmt_test_init_vars(&lvars);
533 return class_register_type("fmt_obd_ops, lvars.module_vars,
537 static void __exit quotfmt_test_exit(void)
539 class_unregister_type("quotfmt_test");
542 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
543 MODULE_DESCRIPTION("administrative quotafile test module");
544 MODULE_LICENSE("GPL");
546 module_init(quotfmt_test_init);
547 module_exit(quotfmt_test_exit);
549 #endif /* HAVE_QUOTA_SUPPORT */