]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-kobj.c
a0d4097d5943ba5edfc0ea4155a837f0a8567a1b
[mirror_spl-debian.git] / module / splat / splat-kobj.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Kobj Tests.
25 \*****************************************************************************/
26
27 #include <sys/kobj.h>
28 #include "splat-internal.h"
29
30 #define SPLAT_KOBJ_NAME "kobj"
31 #define SPLAT_KOBJ_DESC "Kernel Kobj Tests"
32
33 #define SPLAT_KOBJ_TEST1_ID 0x0a01
34 #define SPLAT_KOBJ_TEST1_NAME "open"
35 #define SPLAT_KOBJ_TEST1_DESC "Kobj Open/Close Test"
36
37 #define SPLAT_KOBJ_TEST2_ID 0x0a02
38 #define SPLAT_KOBJ_TEST2_NAME "size/read"
39 #define SPLAT_KOBJ_TEST2_DESC "Kobj Size/Read Test"
40
41 #define SPLAT_KOBJ_TEST_FILE "/etc/fstab"
42
43 static int
44 splat_kobj_test1(struct file *file, void *arg)
45 {
46 struct _buf *f;
47
48 f = kobj_open_file(SPLAT_KOBJ_TEST_FILE);
49 if (f == (struct _buf *)-1) {
50 splat_vprint(file, SPLAT_KOBJ_TEST1_NAME, "Failed to open "
51 "test file: %s\n", SPLAT_KOBJ_TEST_FILE);
52 return -ENOENT;
53 }
54
55 kobj_close_file(f);
56 splat_vprint(file, SPLAT_KOBJ_TEST1_NAME, "Successfully opened and "
57 "closed test file: %s\n", SPLAT_KOBJ_TEST_FILE);
58
59 return 0;
60 } /* splat_kobj_test1() */
61
62 static int
63 splat_kobj_test2(struct file *file, void *arg)
64 {
65 struct _buf *f;
66 char *buf;
67 uint64_t size;
68 int rc;
69
70 f = kobj_open_file(SPLAT_KOBJ_TEST_FILE);
71 if (f == (struct _buf *)-1) {
72 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Failed to open "
73 "test file: %s\n", SPLAT_KOBJ_TEST_FILE);
74 return -ENOENT;
75 }
76
77 rc = kobj_get_filesize(f, &size);
78 if (rc) {
79 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Failed stat of "
80 "test file: %s (%d)\n", SPLAT_KOBJ_TEST_FILE, rc);
81 goto out;
82 }
83
84 buf = kmalloc(size + 1, GFP_KERNEL);
85 if (!buf) {
86 rc = -ENOMEM;
87 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Failed to alloc "
88 "%lld bytes for tmp buffer (%d)\n",
89 (long long)size, rc);
90 goto out;
91 }
92
93 memset(buf, 0, size + 1);
94 rc = kobj_read_file(f, buf, size, 0);
95 if (rc < 0) {
96 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Failed read of "
97 "test file: %s (%d)\n", SPLAT_KOBJ_TEST_FILE, rc);
98 goto out2;
99 }
100
101 /* Validate we read as many bytes as expected based on the stat. This
102 * isn't a perfect test since we didn't create the file however it is
103 * pretty unlikely there are garbage characters in your /etc/fstab */
104 if (size != (uint64_t)strlen(buf)) {
105 rc = -EFBIG;
106 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Stat'ed size "
107 "(%lld) does not match number of bytes read "
108 "(%lld)\n", (long long)size,
109 (long long)strlen(buf));
110 goto out2;
111 }
112
113 rc = 0;
114 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "\n%s\n", buf);
115 splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Successfully stat'ed "
116 "and read expected number of bytes (%lld) from test "
117 "file: %s\n", (long long)size, SPLAT_KOBJ_TEST_FILE);
118 out2:
119 kfree(buf);
120 out:
121 kobj_close_file(f);
122
123 return rc;
124 } /* splat_kobj_test2() */
125
126 splat_subsystem_t *
127 splat_kobj_init(void)
128 {
129 splat_subsystem_t *sub;
130
131 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
132 if (sub == NULL)
133 return NULL;
134
135 memset(sub, 0, sizeof(*sub));
136 strncpy(sub->desc.name, SPLAT_KOBJ_NAME, SPLAT_NAME_SIZE);
137 strncpy(sub->desc.desc, SPLAT_KOBJ_DESC, SPLAT_DESC_SIZE);
138 INIT_LIST_HEAD(&sub->subsystem_list);
139 INIT_LIST_HEAD(&sub->test_list);
140 spin_lock_init(&sub->test_lock);
141 sub->desc.id = SPLAT_SUBSYSTEM_KOBJ;
142
143 SPLAT_TEST_INIT(sub, SPLAT_KOBJ_TEST1_NAME, SPLAT_KOBJ_TEST1_DESC,
144 SPLAT_KOBJ_TEST1_ID, splat_kobj_test1);
145 SPLAT_TEST_INIT(sub, SPLAT_KOBJ_TEST2_NAME, SPLAT_KOBJ_TEST2_DESC,
146 SPLAT_KOBJ_TEST2_ID, splat_kobj_test2);
147
148 return sub;
149 } /* splat_kobj_init() */
150
151 void
152 splat_kobj_fini(splat_subsystem_t *sub)
153 {
154 ASSERT(sub);
155
156 SPLAT_TEST_FINI(sub, SPLAT_KOBJ_TEST2_ID);
157 SPLAT_TEST_FINI(sub, SPLAT_KOBJ_TEST1_ID);
158
159 kfree(sub);
160 } /* splat_kobj_fini() */
161
162 int
163 splat_kobj_id(void)
164 {
165 return SPLAT_SUBSYSTEM_KOBJ;
166 } /* splat_kobj_id() */