]>
Commit | Line | Data |
---|---|---|
715f6251 | 1 | /* |
2 | * This file is part of the SPL: Solaris Porting Layer. | |
3 | * | |
4 | * Copyright (c) 2008 Lawrence Livermore National Security, LLC. | |
5 | * Produced at Lawrence Livermore National Laboratory | |
6 | * Written by: | |
7 | * Brian Behlendorf <behlendorf1@llnl.gov>, | |
8 | * Herb Wartens <wartens2@llnl.gov>, | |
9 | * Jim Garlick <garlick@llnl.gov> | |
10 | * UCRL-CODE-235197 | |
11 | * | |
12 | * This is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
25 | */ | |
26 | ||
9490c148 | 27 | #include "splat-internal.h" |
28 | ||
4b171585 | 29 | #define SPLAT_SUBSYSTEM_KOBJ 0x0a00 |
9490c148 | 30 | #define SPLAT_KOBJ_NAME "kobj" |
5d86345d | 31 | #define SPLAT_KOBJ_DESC "Kernel Kobj Tests" |
9490c148 | 32 | |
4b171585 | 33 | #define SPLAT_KOBJ_TEST1_ID 0x0a01 |
34 | #define SPLAT_KOBJ_TEST1_NAME "open" | |
5d86345d | 35 | #define SPLAT_KOBJ_TEST1_DESC "Kobj Open/Close Test" |
9490c148 | 36 | |
4b171585 | 37 | #define SPLAT_KOBJ_TEST2_ID 0x0a02 |
38 | #define SPLAT_KOBJ_TEST2_NAME "size/read" | |
5d86345d | 39 | #define SPLAT_KOBJ_TEST2_DESC "Kobj Size/Read Test" |
9490c148 | 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 | ||
e4f1d29f | 84 | buf = kmalloc(size + 1, GFP_KERNEL); |
9490c148 | 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", size, rc); | |
89 | goto out; | |
90 | } | |
91 | ||
e4f1d29f | 92 | memset(buf, 0, size + 1); |
9490c148 | 93 | rc = kobj_read_file(f, buf, size, 0); |
94 | if (rc < 0) { | |
95 | splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Failed read of " | |
96 | "test file: %s (%d)\n", SPLAT_KOBJ_TEST_FILE, rc); | |
97 | goto out2; | |
98 | } | |
99 | ||
100 | /* Validate we read as many bytes as expected based on the stat. This | |
101 | * isn't a perfect test since we didn't create the file however it is | |
102 | * pretty unlikely there are garbage characters in your /etc/fstab */ | |
103 | if (size != (uint64_t)strlen(buf)) { | |
e4f1d29f | 104 | rc = -EFBIG; |
9490c148 | 105 | splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Stat'ed size " |
106 | "(%lld) does not match number of bytes read " | |
107 | "(%lld)\n", size, (uint64_t)strlen(buf)); | |
108 | goto out2; | |
109 | } | |
110 | ||
111 | rc = 0; | |
112 | splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "\n%s\n", buf); | |
113 | splat_vprint(file, SPLAT_KOBJ_TEST2_NAME, "Successfully stat'ed " | |
114 | "and read expected number of bytes (%lld) from test " | |
115 | "file: %s\n", size, SPLAT_KOBJ_TEST_FILE); | |
116 | out2: | |
117 | kfree(buf); | |
118 | out: | |
119 | kobj_close_file(f); | |
120 | ||
121 | return rc; | |
122 | } /* splat_kobj_test2() */ | |
123 | ||
124 | splat_subsystem_t * | |
125 | splat_kobj_init(void) | |
126 | { | |
127 | splat_subsystem_t *sub; | |
128 | ||
129 | sub = kmalloc(sizeof(*sub), GFP_KERNEL); | |
130 | if (sub == NULL) | |
131 | return NULL; | |
132 | ||
133 | memset(sub, 0, sizeof(*sub)); | |
134 | strncpy(sub->desc.name, SPLAT_KOBJ_NAME, SPLAT_NAME_SIZE); | |
135 | strncpy(sub->desc.desc, SPLAT_KOBJ_DESC, SPLAT_DESC_SIZE); | |
136 | INIT_LIST_HEAD(&sub->subsystem_list); | |
137 | INIT_LIST_HEAD(&sub->test_list); | |
138 | spin_lock_init(&sub->test_lock); | |
139 | sub->desc.id = SPLAT_SUBSYSTEM_KOBJ; | |
140 | ||
141 | SPLAT_TEST_INIT(sub, SPLAT_KOBJ_TEST1_NAME, SPLAT_KOBJ_TEST1_DESC, | |
142 | SPLAT_KOBJ_TEST1_ID, splat_kobj_test1); | |
143 | SPLAT_TEST_INIT(sub, SPLAT_KOBJ_TEST2_NAME, SPLAT_KOBJ_TEST2_DESC, | |
144 | SPLAT_KOBJ_TEST2_ID, splat_kobj_test2); | |
145 | ||
146 | return sub; | |
147 | } /* splat_kobj_init() */ | |
148 | ||
149 | void | |
150 | splat_kobj_fini(splat_subsystem_t *sub) | |
151 | { | |
152 | ASSERT(sub); | |
153 | ||
154 | SPLAT_TEST_FINI(sub, SPLAT_KOBJ_TEST2_ID); | |
155 | SPLAT_TEST_FINI(sub, SPLAT_KOBJ_TEST1_ID); | |
156 | ||
157 | kfree(sub); | |
158 | } /* splat_kobj_fini() */ | |
159 | ||
160 | int | |
161 | splat_kobj_id(void) | |
162 | { | |
163 | return SPLAT_SUBSYSTEM_KOBJ; | |
164 | } /* splat_kobj_id() */ |