blob: fd224af50db6334b6af41f1db782c61786d45e31 [file] [log] [blame]
mridge061ba162003-12-03 17:06:14 +00001/*
Alexey Kodanev69d3b322013-10-22 16:16:42 +04002 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
mridge3889d932006-01-09 19:17:16 +00004 *
Alexey Kodanev69d3b322013-10-22 16:16:42 +04005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
mridge3889d932006-01-09 19:17:16 +00009 *
Alexey Kodanev69d3b322013-10-22 16:16:42 +040010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
mridge3889d932006-01-09 19:17:16 +000014 *
Alexey Kodanev69d3b322013-10-22 16:16:42 +040015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
mridge3889d932006-01-09 19:17:16 +000018 *
subrata_modak4bb656a2009-02-26 12:02:09 +000019 * This is the main of your user space test program,
20 * which will open the correct kernel bioule, find the
21 * file descriptor value and use that value to make
mridge061ba162003-12-03 17:06:14 +000022 * ioctl calls to the system
23 *
subrata_modak4bb656a2009-02-26 12:02:09 +000024 * Use the ki_generic and other ki_testname functions
mridge061ba162003-12-03 17:06:14 +000025 * to abstract the calls from the main
26 *
mridge061ba162003-12-03 17:06:14 +000027 * FILE : user_tbio.c
28 * USAGE : kernel_space:./load_tbio.sh
29 * user_space :./test_bio
30 *
31 * DESCRIPTION : The bioule will test block i/o layer for kernel 2.5
32 * REQUIREMENTS:
33 * 1) glibc 2.1.91 or above.
34 *
35 * HISTORY :
36 * 11/19/2003 Kai Zhao (ltcd3@cn.ibm.com)
37 *
38 * CODE COVERAGE: 74.9% - fs/bio.c (Total Coverage)
39 *
40 */
41
mridge061ba162003-12-03 17:06:14 +000042#define _GNU_SOURCE
43#include <stdio.h>
44#include <stdlib.h>
45#include <sys/stat.h>
46#include <sys/ioctl.h>
47#include <fcntl.h>
48#include <errno.h>
49#include <sys/types.h>
50#include <linux/kernel.h>
Alexey Kodanev69d3b322013-10-22 16:16:42 +040051#include <unistd.h>
52#include <string.h>
mridge061ba162003-12-03 17:06:14 +000053
Alexey Kodanev69d3b322013-10-22 16:16:42 +040054#include "test.h"
Alexey Kodanev69d3b322013-10-22 16:16:42 +040055#include "safe_macros.h"
56#include "tst_module.h"
mridge061ba162003-12-03 17:06:14 +000057
Alexey Kodanev69d3b322013-10-22 16:16:42 +040058#include "../tbio_kernel/tbio.h"
mridge061ba162003-12-03 17:06:14 +000059
Alexey Kodanev69d3b322013-10-22 16:16:42 +040060char *TCID = TBIO_DEVICE_NAME;
mridge061ba162003-12-03 17:06:14 +000061
Alexey Kodanev69d3b322013-10-22 16:16:42 +040062static const char module_name[] = "ltp_tbio.ko";
63static int module_loaded;
64static int tbio_fd = -1;
mridge061ba162003-12-03 17:06:14 +000065
Alexey Kodanev69d3b322013-10-22 16:16:42 +040066void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080067{
mridge061ba162003-12-03 17:06:14 +000068
69 if (tbio_fd != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080070 close(tbio_fd);
mridge061ba162003-12-03 17:06:14 +000071 tbio_fd = -1;
72 }
Garrett Cooper2c282152010-12-16 00:55:50 -080073
Alexey Kodanev69d3b322013-10-22 16:16:42 +040074 if (module_loaded)
75 tst_module_unload(NULL, module_name);
76
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +030077 if (unlink(DEVICE_NAME) && (errno != ENOENT))
78 tst_brkm(TBROK | TERRNO, NULL, "unlink failed");
mridge061ba162003-12-03 17:06:14 +000079}
80
Alexey Kodanev69d3b322013-10-22 16:16:42 +040081
82void setup(void)
83{
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +030084 dev_t devt;
85 struct stat st;
86 unsigned int i, valid_node_created;
87
Alexey Kodanev69d3b322013-10-22 16:16:42 +040088 tst_require_root(NULL);
89
90 if (tst_kvercmp(2, 6, 0) < 0) {
91 tst_brkm(TCONF, NULL,
92 "Test must be run with kernel 2.6 or newer");
93 }
94
95 tst_module_load(cleanup, module_name, NULL);
96 module_loaded = 1;
97
Alexey Kodanev69d3b322013-10-22 16:16:42 +040098 SAFE_FILE_SCANF(cleanup, "/sys/class/block/tbio/dev",
99 "%d:0", &TBIO_MAJOR);
100
101 devt = makedev(TBIO_MAJOR, 0);
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +0300102
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400103 /*
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +0300104 * Wait until udev creates the device node.
105 * If the node is not created or invalid, create it manually.
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400106 */
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +0300107 valid_node_created = 0;
108 for (i = 0; i < 50; i++) {
109 if (stat(DEVICE_NAME, &st)) {
110 if (errno != ENOENT)
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400111 tst_brkm(TBROK | TERRNO, cleanup,
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +0300112 "stat() failed");
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400113 } else {
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +0300114 if ((st.st_mode & S_IFBLK) && (st.st_rdev == devt)) {
115 valid_node_created = 1;
116 break;
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400117 }
118 }
Stanislav Kholmanskikh5e61dbe2015-01-15 13:39:44 +0300119
120 usleep(100000);
121 }
122
123 if (!valid_node_created) {
124 tst_resm(TINFO,
125 "The device file was not created by udev, "
126 "proceeding with manual creation");
127
128 if (unlink(DEVICE_NAME) && (errno != ENOENT))
129 tst_brkm(TBROK | TERRNO, cleanup, "unlink() failed");
130 if (mknod(DEVICE_NAME, S_IFBLK | S_IRUSR | S_IWUSR |
131 S_IRGRP | S_IWGRP, devt))
132 tst_brkm(TBROK | TERRNO, cleanup, "mknod() failed");
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400133 }
134
135 tbio_fd = open(DEVICE_NAME, O_RDWR);
136 if (tbio_fd < 0) {
137 tst_brkm(TBROK | TERRNO, cleanup, "open of %s failed",
138 DEVICE_NAME);
139 }
140
141 tst_resm(TINFO, "Device opened successfully ");
142}
143
144
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145int tbio_to_dev(int fd, int flag)
mridge061ba162003-12-03 17:06:14 +0000146{
147 int rc;
148 tbio_interface_t bif;
subrata_modakbdbaec52009-02-26 12:14:51 +0000149
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 memset(&bif, 0, sizeof(tbio_interface_t));
151 rc = posix_memalign(&bif.data, 512, 1024);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800152 if (rc) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400153 tst_resm(TINFO, "posix_memalign failed");
mridge061ba162003-12-03 17:06:14 +0000154 return -1;
155 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000156
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 strcpy(bif.data, "User space data");
mridge061ba162003-12-03 17:06:14 +0000158 bif.data_len = 1024;
159 bif.direction = TBIO_TO_DEV;
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400160 bif.cmd = SAFE_MALLOC(cleanup, 6);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800161 if (bif.cmd == NULL) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400162 tst_resm(TINFO, "malloc cmd space failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 free(bif.data);
mridge061ba162003-12-03 17:06:14 +0000164 return -1;
165 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800166 strcpy(bif.cmd, "WRITE");
mridge061ba162003-12-03 17:06:14 +0000167 bif.cmd_len = 6;
subrata_modakbdbaec52009-02-26 12:14:51 +0000168
mridge061ba162003-12-03 17:06:14 +0000169 rc = ioctl(fd, flag, &bif);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800170 if (rc) {
mridge061ba162003-12-03 17:06:14 +0000171 free(bif.data);
172 free(bif.cmd);
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400173 tst_resm(TINFO, "Ioctl error for TBIO_TO_DEV");
mridge061ba162003-12-03 17:06:14 +0000174 return rc;
175 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000176
mridge061ba162003-12-03 17:06:14 +0000177 free(bif.data);
178 free(bif.cmd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000179
mridge061ba162003-12-03 17:06:14 +0000180 return 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000181
mridge061ba162003-12-03 17:06:14 +0000182}
183
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184int tbio_from_dev(int fd, int flag)
mridge061ba162003-12-03 17:06:14 +0000185{
186 int rc;
187 tbio_interface_t bif;
subrata_modakbdbaec52009-02-26 12:14:51 +0000188
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 memset(&bif, 0, sizeof(tbio_interface_t));
190 rc = posix_memalign(&bif.data, 512, 1024);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800191 if (rc) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400192 tst_resm(TINFO, "posix_memalign failed");
mridge061ba162003-12-03 17:06:14 +0000193 return -1;
194 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000195
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 memset(bif.data, 0, 1024);
subrata_modakbdbaec52009-02-26 12:14:51 +0000197
mridge061ba162003-12-03 17:06:14 +0000198 bif.data_len = 1024;
199 bif.direction = TBIO_FROM_DEV;
Stanislav Kholmanskikh49a41ae2014-12-19 15:49:01 +0300200 bif.cmd = SAFE_MALLOC(cleanup, 5);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800201 if (bif.cmd == NULL) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400202 tst_resm(TINFO, "malloc cmd space failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 free(bif.data);
mridge061ba162003-12-03 17:06:14 +0000204 return -1;
205 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800206 strcpy(bif.cmd, "READ");
Stanislav Kholmanskikh49a41ae2014-12-19 15:49:01 +0300207 bif.cmd_len = 5;
subrata_modakbdbaec52009-02-26 12:14:51 +0000208
mridge061ba162003-12-03 17:06:14 +0000209 rc = ioctl(fd, flag, &bif);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800210 if (rc) {
mridge061ba162003-12-03 17:06:14 +0000211 free(bif.data);
212 free(bif.cmd);
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400213 tst_resm(TINFO, "Ioctl error for TBIO_TO_DEV");
mridge061ba162003-12-03 17:06:14 +0000214 return rc;
215 }
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400216
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 if (strcmp(bif.data, "User space data")) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400218 tst_resm(TINFO, "TBIO_FROM_DEV failed");
mridge061ba162003-12-03 17:06:14 +0000219 free(bif.data);
220 free(bif.cmd);
221 return -1;
222 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000223
mridge061ba162003-12-03 17:06:14 +0000224 free(bif.data);
225 free(bif.cmd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000226
mridge061ba162003-12-03 17:06:14 +0000227 return 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000228
mridge061ba162003-12-03 17:06:14 +0000229}
230
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231int tbio_split_to_dev(int fd, int flag)
mridge061ba162003-12-03 17:06:14 +0000232{
233 int rc;
234 tbio_interface_t bif;
subrata_modakbdbaec52009-02-26 12:14:51 +0000235
Wanlong Gao354ebb42012-12-07 10:10:04 +0800236 memset(&bif, 0, sizeof(tbio_interface_t));
237 rc = posix_memalign(&bif.data, 512, 2048);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800238 if (rc) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400239 tst_resm(TINFO, "posix_memalign failed");
mridge061ba162003-12-03 17:06:14 +0000240 return -1;
241 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000242
Wanlong Gao354ebb42012-12-07 10:10:04 +0800243 strcpy(bif.data, "User space data");
mridge061ba162003-12-03 17:06:14 +0000244 bif.data_len = 2048;
245 bif.direction = TBIO_TO_DEV;
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400246 bif.cmd = SAFE_MALLOC(cleanup, 6);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800247 if (bif.cmd == NULL) {
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400248 tst_resm(TINFO, "malloc cmd space failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800249 free(bif.data);
mridge061ba162003-12-03 17:06:14 +0000250 return -1;
251 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800252 strcpy(bif.cmd, "WRITE");
mridge061ba162003-12-03 17:06:14 +0000253 bif.cmd_len = 6;
subrata_modakbdbaec52009-02-26 12:14:51 +0000254
mridge061ba162003-12-03 17:06:14 +0000255 rc = ioctl(fd, flag, &bif);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800256 if (rc) {
mridge061ba162003-12-03 17:06:14 +0000257 free(bif.data);
258 free(bif.cmd);
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400259 tst_resm(TINFO, "Ioctl error for TBIO_TO_DEV");
mridge061ba162003-12-03 17:06:14 +0000260 return rc;
261 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000262
mridge061ba162003-12-03 17:06:14 +0000263 free(bif.data);
264 free(bif.cmd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000265
mridge061ba162003-12-03 17:06:14 +0000266 return 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000267
mridge061ba162003-12-03 17:06:14 +0000268}
269
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400270int ki_generic(int fd, int flag)
mridge061ba162003-12-03 17:06:14 +0000271{
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400272 tbio_interface_t bif;
subrata_modakbdbaec52009-02-26 12:14:51 +0000273
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400274 int rc = ioctl(fd, flag, &bif);
275 if (rc)
276 tst_resm(TINFO | TERRNO, "ioctl error");
277
278 return rc;
279}
280
281
282int main(void)
283{
284 setup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800285
Wanlong Gao354ebb42012-12-07 10:10:04 +0800286 if (ki_generic(tbio_fd, LTP_TBIO_ALLOC))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400287 tst_resm(TFAIL, "failed on LTP_TBIO_ALLOC test");
mridge061ba162003-12-03 17:06:14 +0000288 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400289 tst_resm(TPASS, "success on LTP_TBIO_ALLOC test");
subrata_modakbdbaec52009-02-26 12:14:51 +0000290
Wanlong Gao354ebb42012-12-07 10:10:04 +0800291 if (ki_generic(tbio_fd, LTP_TBIO_CLONE))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400292 tst_resm(TFAIL, "failed on LTP_TBIO_CLONE test");
mridge061ba162003-12-03 17:06:14 +0000293 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400294 tst_resm(TPASS, "success on LTP_TBIO_CLONE test");
subrata_modakbdbaec52009-02-26 12:14:51 +0000295
Wanlong Gao354ebb42012-12-07 10:10:04 +0800296 if (ki_generic(tbio_fd, LTP_TBIO_GET_NR_VECS))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400297 tst_resm(TFAIL, "failed on LTP_TBIO_GET_NR_VECS test");
mridge061ba162003-12-03 17:06:14 +0000298 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400299 tst_resm(TPASS, "success on LTP_TBIO_GET_NR_VECS test");
subrata_modakbdbaec52009-02-26 12:14:51 +0000300
Wanlong Gao354ebb42012-12-07 10:10:04 +0800301 if (ki_generic(tbio_fd, LTP_TBIO_ADD_PAGE))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400302 tst_resm(TFAIL, "failed on LTP_TBIO_ADD_PAGE test");
mridge061ba162003-12-03 17:06:14 +0000303 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400304 tst_resm(TPASS, "success on LTP_TBIO_ADD_PAGE test");
subrata_modakbdbaec52009-02-26 12:14:51 +0000305
Wanlong Gao354ebb42012-12-07 10:10:04 +0800306 if (tbio_split_to_dev(tbio_fd, LTP_TBIO_SPLIT))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400307 tst_resm(TFAIL, "failed on LTP_TBIO_SPLIT:write to dev");
mridge061ba162003-12-03 17:06:14 +0000308 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400309 tst_resm(TPASS, "success on LTP_TBIO_SPLIT:write to dev");
subrata_modakbdbaec52009-02-26 12:14:51 +0000310
Wanlong Gao354ebb42012-12-07 10:10:04 +0800311 if (tbio_to_dev(tbio_fd, LTP_TBIO_DO_IO))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400312 tst_resm(TFAIL, "failed on LTP_TBIO_DO_IO:write to dev");
mridge061ba162003-12-03 17:06:14 +0000313 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400314 tst_resm(TPASS, "success on LTP_TBIO_DO_IO:write to dev");
subrata_modakbdbaec52009-02-26 12:14:51 +0000315
Wanlong Gao354ebb42012-12-07 10:10:04 +0800316 if (tbio_from_dev(tbio_fd, LTP_TBIO_DO_IO))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400317 tst_resm(TFAIL, "failed on LTP_TBIO_DO_IO:read from dev");
mridge061ba162003-12-03 17:06:14 +0000318 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400319 tst_resm(TPASS, "success on LTP_TBIO_DO_IO:read from dev");
subrata_modakbdbaec52009-02-26 12:14:51 +0000320
Wanlong Gao354ebb42012-12-07 10:10:04 +0800321 if (ki_generic(tbio_fd, LTP_TBIO_PUT))
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400322 tst_resm(TFAIL, "failed on LTP_TBIO_PUT test");
mridge061ba162003-12-03 17:06:14 +0000323 else
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400324 tst_resm(TPASS, "success on LTP_TBIO_PUT test");
subrata_modakbdbaec52009-02-26 12:14:51 +0000325
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400326 cleanup();
mridge061ba162003-12-03 17:06:14 +0000327
Alexey Kodanev69d3b322013-10-22 16:16:42 +0400328 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700329}