Whamcloud - gitweb
unload a third time for lnet
[fs/lustre-release.git] / lustre / quota / quotacheck_test.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2005 Cluster File Systems, Inc.
5  *   Author: Lai Siyao <lsy@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org/
8  *
9  *   No redistribution or use is permitted outside of Cluster File Systems, Inc.
10  *
11  * A kernel module which tests the fsfilt quotacheck API from the OBD setup function.
12  */
13
14 #ifndef EXPORT_SYMTAB
15 # define EXPORT_SYMTAB
16 #endif
17 #define DEBUG_SUBSYSTEM S_CLASS
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/fs.h>
22 #include <linux/jbd.h>
23 #include <linux/slab.h>
24 #include <linux/pagemap.h>
25 #include <linux/quotaops.h>
26 #include <linux/ext3_fs.h>
27 #include <linux/ext3_jbd.h>
28 #include <linux/version.h>
29 #include <linux/bitops.h>
30
31 #include <obd_class.h>
32 #include <lustre_fsfilt.h>
33 #include <lustre_mds.h>
34 #include <obd_ost.h>
35
36 char *test_quotafile[] = {"aquotacheck.user", "aquotacheck.group"};
37
38 static inline struct ext3_group_desc *
39 get_group_desc(struct super_block *sb, int group)
40 {
41         unsigned long desc_block, desc;
42         struct ext3_group_desc *gdp;
43
44         desc_block = group / EXT3_DESC_PER_BLOCK(sb);
45         desc = group % EXT3_DESC_PER_BLOCK(sb);
46         gdp = (struct ext3_group_desc *)
47               EXT3_SB(sb)->s_group_desc[desc_block]->b_data;
48
49         return gdp + desc;
50 }
51
52 static inline struct buffer_head *
53 read_inode_bitmap(struct super_block *sb, unsigned long group)
54 {
55         struct ext3_group_desc *desc;
56         struct buffer_head *bh;
57
58         desc = get_group_desc(sb, group);
59         bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
60
61         return bh;
62 }
63
64 static inline struct inode *ext3_iget_inuse(struct super_block *sb,
65                                      struct buffer_head *bitmap_bh,
66                                      int index, unsigned long ino)
67 {
68         struct inode *inode = NULL;
69
70         if (ext3_test_bit(index, bitmap_bh->b_data)) {
71                 CERROR("i: %d, ino: %lu\n", index, ino);
72                 ll_sleep(1);
73                 inode = iget(sb, ino);
74         }
75
76         return inode;
77 }
78
79 static void print_inode(struct inode *inode)
80 {
81         loff_t size = 0;
82
83         if (S_ISDIR(inode->i_mode) ||
84             S_ISREG(inode->i_mode) ||
85             S_ISLNK(inode->i_mode))
86                 size = inode_get_bytes(inode);
87
88          CERROR("%lu: uid: %u, size: %llu, blocks: %lu, real size: %llu\n",
89                inode->i_ino, inode->i_uid, inode->i_size, inode->i_blocks, size);
90 }
91
92 /* Test quotaon */
93 static int quotacheck_test_1(struct obd_device *obd, struct super_block *sb)
94 {
95         struct ext3_sb_info *sbi = EXT3_SB(sb);
96         struct buffer_head *bitmap_bh = NULL;
97         struct inode *inode;
98         unsigned long ino;
99         int i, group;
100         ENTRY;
101
102         for (group = 0; group < sbi->s_groups_count; group++) {
103                 ino = group * sbi->s_inodes_per_group + 1;
104                 brelse(bitmap_bh);
105                 bitmap_bh = read_inode_bitmap(sb, group);
106
107                 if (group == 0)
108                         CERROR("groups_count: %lu, inodes_per_group: %lu, first_ino: %u, inodes_count: %u\n",
109                                sbi->s_groups_count, sbi->s_inodes_per_group,
110                                sbi->s_first_ino, le32_to_cpu(sbi->s_es->s_inodes_count));
111
112                 for (i = 0; i < sbi->s_inodes_per_group; i++, ino++) {
113                         if (ino < sbi->s_first_ino)
114                                 continue;
115                         if (ino > le32_to_cpu(sbi->s_es->s_inodes_count)) {
116                                 CERROR("bad inode number: %lu > s_inodes_count\n", ino);
117                                 brelse(bitmap_bh);
118                                 RETURN(-E2BIG);
119                         }
120                         inode = ext3_iget_inuse(sb, bitmap_bh, i, ino);
121                         if (inode)
122                                 print_inode(inode);
123                         iput(inode);
124                 }
125         }
126         brelse(bitmap_bh);
127
128         RETURN(0);
129 }
130
131 /* -------------------------------------------------------------------------
132  * Tests above, boring obd functions below
133  * ------------------------------------------------------------------------- */
134 static int quotacheck_run_tests(struct obd_device *obd, struct obd_device *tgt)
135 {
136         int rc;
137         ENTRY;
138
139         if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDS_NAME) &&
140             !strcmp(tgt->obd_type->typ_name, "obdfilter")) {
141                 CERROR("TARGET OBD should be mds or ost\n");
142                 RETURN(-EINVAL);
143         }
144
145         rc = quotacheck_test_1(tgt, tgt->u.obt.obt_sb);
146
147         return rc;
148 }
149
150 static int quotacheck_test_cleanup(struct obd_device *obd)
151 {
152         lprocfs_obd_cleanup(obd);
153         return 0;
154 }
155
156 static int quotacheck_test_setup(struct obd_device *obd, obd_count len, void *buf)
157 {
158         struct lprocfs_static_vars lvars;
159         struct lustre_cfg *lcfg = buf;
160         struct obd_device *tgt;
161         int rc;
162         ENTRY;
163
164         if (lcfg->lcfg_bufcount < 1) {
165                 CERROR("requires a mds OBD name\n");
166                 RETURN(-EINVAL);
167         }
168
169         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
170         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
171                 CERROR("target device not attached or not set up (%s)\n",
172                        lustre_cfg_string(lcfg, 1));
173                 RETURN(-EINVAL);
174         }
175
176         rc = quotacheck_run_tests(obd, tgt);
177         if (rc)
178                 quotacheck_test_cleanup(obd);
179
180         lprocfs_init_vars(quotacheck_test, &lvars);
181         lprocfs_obd_setup(obd, lvars.obd_vars);
182
183         RETURN(rc);
184 }
185
186 static struct obd_ops quotacheck_obd_ops = {
187         .o_owner       = THIS_MODULE,
188         .o_setup       = quotacheck_test_setup,
189         .o_cleanup     = quotacheck_test_cleanup,
190 };
191
192 #ifdef LPROCFS
193 static struct lprocfs_vars lprocfs_obd_vars[] = { {0} };
194 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
195 LPROCFS_INIT_VARS(quotacheck_test, lprocfs_module_vars, lprocfs_obd_vars)
196 #endif
197
198 static int __init quotacheck_test_init(void)
199 {
200         struct lprocfs_static_vars lvars;
201
202         lprocfs_init_vars(quotacheck_test, &lvars);
203         return class_register_type(&quotacheck_obd_ops, lvars.module_vars,
204                                    "quotacheck_test");
205 }
206
207 static void __exit quotacheck_test_exit(void)
208 {
209         class_unregister_type("quotacheck_test");
210 }
211
212 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
213 MODULE_DESCRIPTION("quotacheck test module");
214 MODULE_LICENSE("GPL");
215
216 module_init(quotacheck_test_init);
217 module_exit(quotacheck_test_exit);