Whamcloud - gitweb
LU-82 Remove useless clio locks
[fs/lustre-release.git] / lustre / lvfs / quotafmt_test.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lvfs/quotafmt_test.c
37  *
38  * No redistribution or use is permitted outside of Sun Microsystems, Inc.
39  *
40  * Kernel module to test lustre administrative quotafile format APIs
41  * from the OBD setup function
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47
48 #include <linux/module.h>
49 #include <linux/init.h>
50 #include <linux/errno.h>
51 #include <linux/fs.h>
52 #include <linux/kernel.h>
53
54 #include <lustre_quota.h>
55 #include <obd_class.h>
56
57 #include "lustre_quota_fmt.h"
58
59 #ifdef HAVE_QUOTA_SUPPORT
60
61 char *test_quotafile[2] = { "usrquota_test", "grpquota_test" };
62
63 static int quotfmt_initialize(struct lustre_quota_info *lqi,
64                               struct obd_device *tgt,
65                               struct lvfs_run_ctxt *saved)
66 {
67         struct lustre_disk_dqheader dqhead;
68         static const uint quota_magics[] = LUSTRE_INITQMAGICS;
69         static const uint quota_versions[] = LUSTRE_INITQVERSIONS_V2;
70         struct file *fp;
71         struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;
72         size_t size;
73         struct dentry *de;
74         int i, rc = 0;
75         ENTRY;
76
77         push_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);
78
79         for (i = 0; i < MAXQUOTAS; i++) {
80                 loff_t offset = 0;
81                 char *name = test_quotafile[i];
82                 int namelen = strlen(name);
83
84                 /* remove the stale test quotafile */
85                 LOCK_INODE_MUTEX_PARENT(parent_inode);
86                 de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);
87                 if (!IS_ERR(de) && de->d_inode)
88                         ll_vfs_unlink(parent_inode, de,
89                                       tgt->obd_lvfs_ctxt.pwdmnt);
90                 if (!IS_ERR(de))
91                         dput(de);
92                 UNLOCK_INODE_MUTEX(parent_inode);
93
94                 /* create quota file */
95                 fp = filp_open(name, O_CREAT | O_EXCL, 0644);
96                 if (IS_ERR(fp)) {
97                         rc = PTR_ERR(fp);
98                         CERROR("error creating test quotafile %s (rc = %d)\n",
99                                name, rc);
100                         break;
101                 }
102                 lqi->qi_files[i] = fp;
103
104                 /* write quotafile header */
105                 dqhead.dqh_magic = cpu_to_le32(quota_magics[i]);
106                 dqhead.dqh_version = cpu_to_le32(quota_versions[i]);
107                 size = fp->f_op->write(fp, (char *)&dqhead,
108                                        sizeof(struct lustre_disk_dqheader),
109                                        &offset);
110                 if (size != sizeof(struct lustre_disk_dqheader)) {
111                         CERROR("error writing quotafile header %s (rc = %d)\n",
112                                name, rc);
113                         rc = size;
114                         break;
115                 }
116         }
117
118         RETURN(rc);
119 }
120
121 static int quotfmt_finalize(struct lustre_quota_info *lqi,
122                             struct obd_device *tgt, struct lvfs_run_ctxt *saved)
123 {
124         struct dentry *de;
125         struct inode *parent_inode = tgt->obd_lvfs_ctxt.pwd->d_inode;
126         int i, rc = 0;
127         ENTRY;
128
129         for (i = 0; i < MAXQUOTAS; i++) {
130                 char *name = test_quotafile[i];
131                 int namelen = strlen(name);
132
133                 if (lqi->qi_files[i] == NULL)
134                         continue;
135
136                 /* close quota file */
137                 filp_close(lqi->qi_files[i], 0);
138
139                 /* unlink quota file */
140                 LOCK_INODE_MUTEX_PARENT(parent_inode);
141
142                 de = lookup_one_len(name, tgt->obd_lvfs_ctxt.pwd, namelen);
143                 if (IS_ERR(de) || de->d_inode == NULL) {
144                         rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
145                         CERROR("error lookup quotafile %s (rc = %d)\n",
146                                name, rc);
147                         goto dput;
148                 }
149
150                 rc = ll_vfs_unlink(parent_inode, de, tgt->obd_lvfs_ctxt.pwdmnt);
151                 if (rc)
152                         CERROR("error unlink quotafile %s (rc = %d)\n",
153                                name, rc);
154               dput:
155                 if (!IS_ERR(de))
156                         dput(de);
157                 UNLOCK_INODE_MUTEX(parent_inode);
158         }
159
160         pop_ctxt(saved, &tgt->obd_lvfs_ctxt, NULL);
161         RETURN(rc);
162 }
163
164 static int quotfmt_test_1(struct lustre_quota_info *lqi)
165 {
166         int i;
167         ENTRY;
168
169         for (i = 0; i < MAXQUOTAS; i++) {
170                 if (lustre_check_quota_file(lqi, i))
171                         RETURN(-EINVAL);
172         }
173         RETURN(0);
174 }
175
176 static void print_quota_info(struct lustre_quota_info *lqi)
177 {
178 #if 0
179         struct lustre_mem_dqinfo *dqinfo;
180         int i;
181
182         for (i = 0; i < MAXQUOTAS; i++) {
183                 dqinfo = &lqi->qi_info[i];
184                 CDEBUG(D_INFO, "%s quota info:\n", i == USRQUOTA ? "user " : "group");
185                 CDEBUG(D_INFO, "dqi_bgrace(%u) dqi_igrace(%u) dqi_flags(%lu) dqi_blocks(%u) "
186                        "dqi_free_blk(%u) dqi_free_entry(%u)\n",
187                        dqinfo->dqi_bgrace, dqinfo->dqi_igrace, dqinfo->dqi_flags,
188                        dqinfo->dqi_blocks, dqinfo->dqi_free_blk,
189                        dqinfo->dqi_free_entry);
190         }
191 #endif
192 }
193
194 static int quotfmt_test_2(struct lustre_quota_info *lqi)
195 {
196         int i, rc = 0;
197         ENTRY;
198
199         for (i = 0; i < MAXQUOTAS; i++) {
200                 struct lustre_mem_dqinfo dqinfo;
201
202                 rc = lustre_init_quota_info(lqi, i);
203                 if (rc) {
204                         CERROR("init quotainfo(%d) failed! (rc:%d)\n", i, rc);
205                         break;
206                 }
207                 memcpy(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo));
208
209                 rc = lustre_read_quota_info(lqi, i);
210                 if (rc) {
211                         CERROR("read quotainfo(%d) failed! (rc:%d)\n", i, rc);
212                         break;
213                 }
214
215                 if (memcmp(&dqinfo, &lqi->qi_info[i], sizeof(dqinfo))) {
216                         rc = -EINVAL;
217                         break;
218                 }
219         }
220         RETURN(rc);
221 }
222
223 static struct lustre_dquot *get_rand_dquot(struct lustre_quota_info *lqi)
224 {
225         struct lustre_dquot *dquot;
226         unsigned int rand;
227
228         OBD_ALLOC(dquot, sizeof(*dquot));
229         if (dquot == NULL)
230                 return NULL;
231
232         cfs_get_random_bytes(&rand, sizeof(rand));
233         if (!rand)
234                 rand = 1000;
235
236         dquot->dq_info = lqi;
237         dquot->dq_id = rand % 1000 + 1;
238         dquot->dq_type = rand % MAXQUOTAS;
239
240         dquot->dq_dqb.dqb_bhardlimit = rand;
241         dquot->dq_dqb.dqb_bsoftlimit = rand / 2;
242         dquot->dq_dqb.dqb_curspace = rand / 3;
243         dquot->dq_dqb.dqb_ihardlimit = rand;
244         dquot->dq_dqb.dqb_isoftlimit = rand / 2;
245         dquot->dq_dqb.dqb_curinodes = rand / 3;
246         dquot->dq_dqb.dqb_btime = jiffies;
247         dquot->dq_dqb.dqb_itime = jiffies;
248
249         return dquot;
250 }
251
252 static void put_rand_dquot(struct lustre_dquot *dquot)
253 {
254         OBD_FREE(dquot, sizeof(*dquot));
255 }
256
257 static int write_check_dquot(struct lustre_quota_info *lqi)
258 {
259         struct lustre_dquot *dquot;
260         struct lustre_mem_dqblk dqblk;
261         int rc = 0;
262         ENTRY;
263
264         dquot = get_rand_dquot(lqi);
265         if (dquot == NULL)
266                 RETURN(-ENOMEM);
267
268         /* for already exists entry, we set the dq_off by read_dquot */
269         rc = lustre_read_dquot(dquot);
270         if (rc) {
271                 CERROR("read dquot failed! (rc:%d)\n", rc);
272                 GOTO(out, rc);
273         }
274
275         cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
276         /* for already exists entry, we rewrite it */
277         rc = lustre_commit_dquot(dquot);
278         if (rc) {
279                 CERROR("commit dquot failed! (rc:%d)\n", rc);
280                 GOTO(out, rc);
281         }
282         memcpy(&dqblk, &dquot->dq_dqb, sizeof(dqblk));
283         memset(&dquot->dq_dqb, 0, sizeof(dqblk));
284
285         rc = lustre_read_dquot(dquot);
286         if (rc) {
287                 CERROR("read dquot failed! (rc:%d)\n", rc);
288                 GOTO(out, rc);
289         }
290
291         if (memcmp(&dqblk, &dquot->dq_dqb, sizeof(dqblk))) {
292                 rc = -EINVAL;
293                 GOTO(out, rc);
294         }
295       out:
296         put_rand_dquot(dquot);
297         RETURN(rc);
298 }
299
300 static int quotfmt_test_3(struct lustre_quota_info *lqi)
301 {
302         struct lustre_dquot *dquot;
303         int i = 0, rc = 0;
304         ENTRY;
305
306         dquot = get_rand_dquot(lqi);
307         if (dquot == NULL)
308                 RETURN(-ENOMEM);
309       repeat:
310         cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
311         /* write a new dquot */
312         rc = lustre_commit_dquot(dquot);
313         if (rc) {
314                 CERROR("commit dquot failed! (rc:%d)\n", rc);
315                 GOTO(out, rc);
316         }
317         dquot->dq_off = 0;
318         memset(&dquot->dq_dqb, 0, sizeof(dquot->dq_dqb));
319
320         /* check if this dquot is on disk now */
321         rc = lustre_read_dquot(dquot);
322         if (rc) {
323                 CERROR("read dquot failed! (rc:%d)\n", rc);
324                 GOTO(out, rc);
325         }
326         if (!dquot->dq_off || cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags)) {
327                 CERROR("the dquot isn't committed\n");
328                 GOTO(out, rc = -EINVAL);
329         }
330
331         /* remove this dquot */
332         cfs_set_bit(DQ_FAKE_B, &dquot->dq_flags);
333         dquot->dq_dqb.dqb_curspace = 0;
334         dquot->dq_dqb.dqb_curinodes = 0;
335         rc = lustre_commit_dquot(dquot);
336         if (rc) {
337                 CERROR("remove dquot failed! (rc:%d)\n", rc);
338                 GOTO(out, rc);
339         }
340
341         /* check if the dquot is really removed */
342         cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
343         dquot->dq_off = 0;
344         rc = lustre_read_dquot(dquot);
345         if (rc) {
346                 CERROR("read dquot failed! (rc:%d)\n", rc);
347                 GOTO(out, rc);
348         }
349         if (!cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags) || dquot->dq_off) {
350                 CERROR("the dquot isn't removed!\n");
351                 GOTO(out, rc = -EINVAL);
352         }
353
354         /* check if this dquot can be write again */
355         if (++i < 2)
356                 goto repeat;
357
358         print_quota_info(lqi);
359
360       out:
361         put_rand_dquot(dquot);
362         RETURN(rc);
363 }
364
365 static int quotfmt_test_4(struct lustre_quota_info *lqi)
366 {
367         int i, rc = 0;
368         ENTRY;
369
370         for (i = 0; i < 30000; i++) {
371                 rc = write_check_dquot(lqi);
372                 if (rc) {
373                         CERROR("write/check dquot failed at %d! (rc:%d)\n",
374                                i, rc);
375                         break;
376                 }
377         }
378         print_quota_info(lqi);
379         RETURN(rc);
380 }
381
382 static int quotfmt_test_5(struct lustre_quota_info *lqi)
383 {
384 #ifndef KERNEL_SUPPORTS_QUOTA_READ
385         int i, rc = 0;
386
387         for (i = USRQUOTA; i < MAXQUOTAS && !rc; i++) {
388                 cfs_list_t list;
389                 struct dquot_id *dqid, *tmp;
390
391                 CFS_INIT_LIST_HEAD(&list);
392                 rc = lustre_get_qids(lqi->qi_files[i], NULL, i, &list);
393                 if (rc) {
394                         CERROR("%s get all %ss (rc:%d):\n",
395                                rc ? "error" : "success",
396                                i == USRQUOTA ? "uid" : "gid", rc);
397                 }
398                 cfs_list_for_each_entry_safe(dqid, tmp, &list, di_link) {
399                         cfs_list_del_init(&dqid->di_link);
400                         if (rc == 0)
401                                 CDEBUG(D_INFO, "%d ", dqid->di_id);
402                         kfree(dqid);
403                 }
404                 CDEBUG(D_INFO, "\n");
405         }
406         return rc;
407 #else
408         CWARN("kernel supports quota_read OR kernel version >= 2.6.12, test skipped\n");
409         return 0;
410 #endif
411 }
412
413 static int quotfmt_run_tests(struct obd_device *obd, struct obd_device *tgt)
414 {
415         struct lvfs_run_ctxt saved;
416         struct lustre_quota_info *lqi = NULL;
417         int rc = 0;
418         ENTRY;
419
420         OBD_ALLOC(lqi, sizeof(*lqi));
421         if (lqi == NULL) {
422                 CERROR("not enough memory\n");
423                 RETURN(-ENOMEM);
424         }
425
426         CWARN("=== Initialize quotafile test\n");
427         rc = quotfmt_initialize(lqi, tgt, &saved);
428         if (rc)
429                 GOTO(out, rc);
430
431         CWARN("=== test  1: check quota header\n");
432         rc = quotfmt_test_1(lqi);
433         if (rc) {
434                 CERROR("check quota header failed! (rc:%d)\n", rc);
435                 GOTO(out, rc);
436         }
437
438         CWARN("=== test  2: write/read quota info\n");
439         rc = quotfmt_test_2(lqi);
440         if (rc) {
441                 CERROR("write/read quota info failed! (rc:%d)\n", rc);
442                 GOTO(out, rc);
443         }
444
445         CWARN("=== test  3: write/remove dquot\n");
446         rc = quotfmt_test_3(lqi);
447         if (rc) {
448                 CERROR("write/remove dquot failed! (rc:%d)\n", rc);
449                 GOTO(out, rc);
450         }
451
452         CWARN("=== test  4: write/read 30000 dquot\n");
453         rc = quotfmt_test_4(lqi);
454         if (rc) {
455                 CERROR("write/read 30000 dquot failed\n");
456                 GOTO(out, rc);
457         }
458
459         CWARN("=== test 5: walk through quota file to get all ids\n");
460         rc = quotfmt_test_5(lqi);
461         if (rc) {
462                 CERROR("walk through quota file failed\n");
463                 GOTO(out, rc);
464         }
465
466       out:
467         CWARN("=== Finalize quotafile test\n");
468         rc = quotfmt_finalize(lqi, tgt, &saved);
469         OBD_FREE(lqi, sizeof(*lqi));
470         RETURN(rc);
471 }
472
473 static int quotfmt_test_cleanup(struct obd_device *obd)
474 {
475         ENTRY;
476         lprocfs_obd_cleanup(obd);
477         RETURN(0);
478 }
479
480 static int quotfmt_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
481 {
482         struct lprocfs_static_vars lvars;
483         struct obd_device *tgt;
484         int rc;
485         ENTRY;
486
487         if (lcfg->lcfg_bufcount < 1) {
488                 CERROR("requires a mds OBD name\n");
489                 RETURN(-EINVAL);
490         }
491
492         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
493         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
494                 CERROR("target device not attached or not set up (%s)\n",
495                        lustre_cfg_string(lcfg, 1));
496                 RETURN(-EINVAL);
497         }
498
499         rc = quotfmt_run_tests(obd, tgt);
500         if (rc)
501                 quotfmt_test_cleanup(obd);
502
503         lprocfs_quotfmt_test_init_vars(&lvars);
504         lprocfs_obd_setup(obd, lvars.obd_vars);
505
506         RETURN(rc);
507 }
508
509 static struct obd_ops quotfmt_obd_ops = {
510         .o_owner = THIS_MODULE,
511         .o_setup = quotfmt_test_setup,
512         .o_cleanup = quotfmt_test_cleanup,
513 };
514
515 #ifdef LPROCFS
516 static struct lprocfs_vars lprocfs_quotfmt_test_obd_vars[] = { {0} };
517 static struct lprocfs_vars lprocfs_quotfmt_test_module_vars[] = { {0} };
518
519 void lprocfs_quotfmt_test_init_vars(struct lprocfs_static_vars *lvars)
520 {
521     lvars->module_vars  = lprocfs_quotfmt_test_module_vars;
522     lvars->obd_vars     = lprocfs_quotfmt_test_obd_vars;
523 }
524 #endif
525 static int __init quotfmt_test_init(void)
526 {
527         struct lprocfs_static_vars lvars;
528
529         lprocfs_quotfmt_test_init_vars(&lvars);
530         return class_register_type(&quotfmt_obd_ops, NULL, lvars.module_vars,
531                                    "quotfmt_test", NULL);
532 }
533
534 static void __exit quotfmt_test_exit(void)
535 {
536         class_unregister_type("quotfmt_test");
537 }
538
539 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
540 MODULE_DESCRIPTION("administrative quotafile test module");
541 MODULE_LICENSE("GPL");
542
543 module_init(quotfmt_test_init);
544 module_exit(quotfmt_test_exit);
545
546 #endif /* HAVE_QUOTA_SUPPORT */