whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | /* |
| 33 | * |
| 34 | * Lib i/o |
| 35 | * |
| 36 | * This file contains several functions to doing reads and writes. |
| 37 | * It was written so that a single function could be called in a test |
| 38 | * program and only a io type field value would have to change to |
| 39 | * do different types of io. There is even a couple of functions that |
| 40 | * will allow you to parse a string to determine the iotype. |
| 41 | * |
| 42 | * This file contains functions for writing/reading to/from open files |
| 43 | * Prototypes: |
| 44 | * |
| 45 | * Functions declared in this module - see individual function code for |
| 46 | * usage comments: |
| 47 | * |
| 48 | * int stride_bounds(int offset, int stride, int nstrides, |
| 49 | * int bytes_per_stride, int *min, int *max); |
| 50 | |
| 51 | * int lio_write_buffer(int fd, int method, char *buffer, int size, |
| 52 | * char **errmsg, long wrd); |
| 53 | * int lio_read_buffer(int fd, int method, char *buffer, int size, |
| 54 | * char **errmsg, long wrd); |
| 55 | * |
| 56 | * #ifdef CRAY |
| 57 | * int lio_wait4asyncio(int method, int fd, struct iosw **statptr) |
| 58 | * int lio_check_asyncio(char *io_type, int size, struct iosw *status) |
| 59 | * #endif |
| 60 | * #ifdef sgi |
| 61 | * int lio_wait4asyncio(int method, int fd, aiocb_t *aiocbp) |
| 62 | * int lio_check_asyncio(char *io_type, int size, aiocb_t *aiocbp, int method) |
| 63 | * #endif |
| 64 | * |
| 65 | * int lio_parse_io_arg1(char *string) |
| 66 | * void lio_help1(char *prefix); |
| 67 | * |
| 68 | * int lio_parse_io_arg2(char *string, char **badtoken) |
| 69 | * void lio_help2(char *prefix); |
| 70 | * |
| 71 | * int lio_set_debug(int level); |
| 72 | * |
| 73 | * char Lio_SysCall[]; |
| 74 | * struct lio_info_type Lio_info1[]; |
| 75 | * struct lio_info_type Lio_info2[]; |
| 76 | * |
| 77 | * Author : Richard Logan |
| 78 | * |
| 79 | */ |
| 80 | |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 81 | #ifdef __linux__ |
Garrett Cooper | 9ef7f2d | 2011-04-08 22:44:06 -0700 | [diff] [blame] | 82 | #ifndef _GNU_SOURCE |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 83 | #define _GNU_SOURCE |
Garrett Cooper | 9ef7f2d | 2011-04-08 22:44:06 -0700 | [diff] [blame] | 84 | #endif |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 85 | #define _LARGEFILE64_SOURCE |
| 86 | #endif |
Mike Frysinger | 28606c1 | 2010-08-17 17:22:45 -0400 | [diff] [blame] | 87 | #include "config.h" |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 88 | #include <stdio.h> |
| 89 | #include <ctype.h> |
| 90 | #include <fcntl.h> |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 91 | #include <unistd.h> |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 92 | #include <sys/types.h> |
| 93 | #include <sys/stat.h> |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 94 | #include <sys/time.h> |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 95 | #include <sys/param.h> |
| 96 | #include <errno.h> |
| 97 | #include <sys/types.h> |
| 98 | #include <sys/file.h> |
| 99 | #include <signal.h> |
subrata_modak | 94d177a | 2008-12-22 07:33:02 +0000 | [diff] [blame] | 100 | #include <stdint.h> |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 101 | #ifdef CRAY |
| 102 | #include <sys/secparm.h> |
| 103 | #include <sys/iosw.h> |
| 104 | #include <sys/listio.h> |
| 105 | #else |
| 106 | /* for linux or sgi */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | #include <sys/uio.h> /* readv(2)/writev(2) */ |
| 108 | #include <string.h> /* bzero */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 109 | #endif |
mridge | f7298c7 | 2005-12-05 19:14:37 +0000 | [diff] [blame] | 110 | #if defined(__linux__) || defined(__sun) || defined(__hpux) || defined(_AIX) |
robbiew | 906676d | 2005-08-02 16:57:19 +0000 | [diff] [blame] | 111 | #if !defined(UCLINUX) && !defined(__UCLIBC__) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 112 | #include <aio.h> |
| 113 | #endif |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 115 | #include <stdlib.h> /* atoi, abs */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 116 | |
| 117 | #include "tlibio.h" /* defines LIO* marcos */ |
Garrett Cooper | 9ef7f2d | 2011-04-08 22:44:06 -0700 | [diff] [blame] | 118 | #include "random_range.h" |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 119 | |
| 120 | #ifndef PATH_MAX |
| 121 | #define PATH_MAX MAXPATHLEN |
| 122 | #endif |
| 123 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | #if 0 /* disabled until it's needed -- roehrich 6/11/97 */ |
| 125 | #define BUG1_workaround 1 /* Work around a condition where aio_return gives |
| 126 | * a value of zero but there is no errno followup |
| 127 | * and the read/write operation actually did its |
| 128 | * job. spr/pv 705244 |
| 129 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 132 | static void lio_async_signal_handler(); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 133 | #ifdef sgi |
| 134 | static void lio_async_callback_handler(); |
| 135 | #endif |
| 136 | |
| 137 | /* |
| 138 | * Define the structure as used in lio_parse_arg1 and lio_help1 |
| 139 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | struct lio_info_type Lio_info1[] = { |
| 141 | {"s", LIO_IO_SYNC, "sync i/o"}, |
| 142 | {"p", LIO_IO_ASYNC | LIO_WAIT_SIGACTIVE, |
| 143 | "async i/o using a loop to wait for a signal"}, |
| 144 | {"b", LIO_IO_ASYNC | LIO_WAIT_SIGPAUSE, "async i/o using pause"}, |
| 145 | {"a", LIO_IO_ASYNC | LIO_WAIT_RECALL, |
| 146 | "async i/o using recall/aio_suspend"}, |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 147 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | {"r", |
| 149 | LIO_RANDOM | LIO_IO_TYPES | LIO_WAIT_TYPES, |
| 150 | "random sync i/o types and wait methods"}, |
| 151 | {"R", |
| 152 | LIO_RANDOM | LIO_IO_ATYPES | LIO_WAIT_ATYPES, |
| 153 | "random i/o types and wait methods"}, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 154 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | {"r", |
| 156 | LIO_RANDOM | LIO_IO_TYPES | LIO_WAIT_TYPES, |
| 157 | "random i/o types and wait methods"}, |
| 158 | {"R", |
| 159 | LIO_RANDOM | LIO_IO_TYPES | LIO_WAIT_TYPES, |
| 160 | "random i/o types and wait methods"}, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 161 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | {"l", LIO_IO_SLISTIO | LIO_WAIT_RECALL, "single stride sync listio"}, |
| 163 | {"L", LIO_IO_ALISTIO | LIO_WAIT_RECALL, |
| 164 | "single stride async listio using recall"}, |
| 165 | {"X", LIO_IO_ALISTIO | LIO_WAIT_SIGPAUSE, |
| 166 | "single stride async listio using pause"}, |
| 167 | {"v", LIO_IO_SYNCV, "single buffer sync readv/writev"}, |
| 168 | {"P", LIO_IO_SYNCP, "sync pread/pwrite"}, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /* |
| 172 | * Define the structure used by lio_parse_arg2 and lio_help2 |
| 173 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | struct lio_info_type Lio_info2[] = { |
| 175 | {"sync", LIO_IO_SYNC, "sync i/o (read/write)"}, |
| 176 | {"async", LIO_IO_ASYNC, "async i/o (reada/writea/aio_read/aio_write)"}, |
| 177 | {"slistio", LIO_IO_SLISTIO, "single stride sync listio"}, |
| 178 | {"alistio", LIO_IO_ALISTIO, "single stride async listio"}, |
| 179 | {"syncv", LIO_IO_SYNCV, "single buffer sync readv/writev"}, |
| 180 | {"syncp", LIO_IO_SYNCP, "pread/pwrite"}, |
| 181 | {"active", LIO_WAIT_ACTIVE, "spin on status/control values"}, |
| 182 | {"recall", LIO_WAIT_RECALL, |
| 183 | "use recall(2)/aio_suspend(3) to wait for i/o to complete"}, |
| 184 | {"sigactive", LIO_WAIT_SIGACTIVE, "spin waiting for signal"}, |
| 185 | {"sigpause", LIO_WAIT_SIGPAUSE, "call pause(2) to wait for signal"}, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 186 | /* nowait is a touchy thing, it's an accident that this implementation worked at all. 6/27/97 roehrich */ |
| 187 | /* { "nowait", LIO_WAIT_NONE, "do not wait for async io to complete" },*/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | {"random", LIO_RANDOM, "set random bit"}, |
| 189 | {"randomall", |
| 190 | LIO_RANDOM | LIO_IO_TYPES | LIO_WAIT_TYPES, |
| 191 | "all random i/o types and wait methods (except nowait)"}, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | char Lio_SysCall[PATH_MAX]; /* string containing last i/o system call */ |
| 195 | |
| 196 | static volatile int Received_signal = 0; /* number of signals received */ |
| 197 | static volatile int Rec_signal; |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 198 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 199 | static volatile int Received_callback = 0; /* number of callbacks received */ |
| 200 | static volatile int Rec_callback; |
| 201 | #endif |
| 202 | static char Errormsg[500]; |
| 203 | static int Debug_level = 0; |
| 204 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 205 | /*********************************************************************** |
| 206 | * stride_bounds() |
| 207 | * |
| 208 | * Determine the bounds of a strided request, normalized to offset. Returns |
| 209 | * the number of bytes needed to satisfy the request, and optionally sets |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 210 | * *min and *max to the mininum and maximum bytes referenced, normalized |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 211 | * around offset. |
| 212 | * |
| 213 | * Returns -1 on error - the only possible error conditions are illegal values |
| 214 | * for nstrides and/or bytes_per_stride - both parameters must be >= 0. |
| 215 | * |
| 216 | * (maule, 11/16/95) |
| 217 | ***********************************************************************/ |
| 218 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 219 | int stride_bounds(offset, stride, nstrides, bytes_per_stride, min, max) |
| 220 | int offset; |
| 221 | int stride; |
| 222 | int nstrides; |
| 223 | int bytes_per_stride; |
| 224 | int *min; |
| 225 | int *max; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 226 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 227 | int nbytes, min_byte, max_byte; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * sanity checks ... |
| 231 | */ |
| 232 | |
| 233 | if (nstrides < 0 || bytes_per_stride < 0) { |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | if (stride == 0) { |
| 238 | stride = bytes_per_stride; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Determine the # of bytes needed to satisfy the request. This |
| 243 | * value, along with the offset argument, determines the min and max |
| 244 | * bytes referenced. |
| 245 | */ |
| 246 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | nbytes = abs(stride) * (nstrides - 1) + bytes_per_stride; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 248 | |
| 249 | if (stride < 0) { |
| 250 | max_byte = offset + bytes_per_stride - 1; |
| 251 | min_byte = max_byte - nbytes + 1; |
| 252 | } else { |
| 253 | min_byte = offset; |
| 254 | max_byte = min_byte + nbytes - 1; |
| 255 | } |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 256 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 257 | if (min != NULL) { |
| 258 | *min = min_byte; |
| 259 | } |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 260 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 261 | if (max != NULL) { |
| 262 | *max = max_byte; |
| 263 | } |
| 264 | |
| 265 | return nbytes; |
| 266 | } |
| 267 | |
| 268 | /*********************************************************************** |
| 269 | * This function will allow someone to set the debug level. |
| 270 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 271 | int lio_set_debug(level) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 272 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 273 | int old; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 274 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 275 | old = Debug_level; |
| 276 | Debug_level = level; |
| 277 | return old; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /*********************************************************************** |
| 281 | * This function will parse a string and return desired io-method. |
| 282 | * Only the first character of the string is used. |
| 283 | * |
| 284 | * This function does not provide for meaningful option arguments, |
| 285 | * but it supports current growfiles/btlk interface. |
| 286 | * |
| 287 | * (rrl 04/96) |
| 288 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 289 | int lio_parse_io_arg1(char *string) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 290 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 291 | unsigned int ind; |
| 292 | int found = 0; |
| 293 | int mask = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 294 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 295 | /* |
| 296 | * Determine if token is a valid string. |
| 297 | */ |
| 298 | for (ind = 0; ind < sizeof(Lio_info1) / sizeof(struct lio_info_type); |
| 299 | ind++) { |
| 300 | if (strcmp(string, Lio_info1[ind].token) == 0) { |
| 301 | mask |= Lio_info1[ind].bits; |
| 302 | found = 1; |
| 303 | break; |
| 304 | } |
| 305 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 306 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 307 | if (found == 0) { |
| 308 | return -1; |
| 309 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 310 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 311 | return mask; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 312 | |
| 313 | } |
| 314 | |
| 315 | /*********************************************************************** |
| 316 | * This function will print a help message describing the characters |
| 317 | * that can be parsed by lio_parse_io_arg1(). |
| 318 | * They will be printed one per line. |
| 319 | * (rrl 04/96) |
| 320 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 321 | void lio_help1(char *prefix) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 322 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 323 | unsigned int ind; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 324 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 325 | for (ind = 0; ind < sizeof(Lio_info1) / sizeof(struct lio_info_type); |
| 326 | ind++) { |
| 327 | printf("%s %s : %s\n", prefix, Lio_info1[ind].token, |
| 328 | Lio_info1[ind].desc); |
| 329 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 330 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 331 | return; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /*********************************************************************** |
| 335 | * This function will parse a string and return the desired io-method. |
| 336 | * This function will take a comma separated list of io type and wait |
| 337 | * method tokens as defined in Lio_info2[]. If a token does not match |
| 338 | * any of the tokens in Lio_info2[], it will be coverted to a number. |
| 339 | * If it was a number, those bits are also set. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 340 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 341 | * (rrl 04/96) |
| 342 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 343 | int lio_parse_io_arg2(char *string, char **badtoken) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 344 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 345 | char *token = string; |
| 346 | char *cc = token; |
| 347 | char savecc; |
| 348 | int found; |
| 349 | int mask = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 350 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 351 | int tmp; |
| 352 | unsigned int ind; |
| 353 | char chr; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 354 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 355 | if (token == NULL) |
| 356 | return -1; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 357 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 358 | for (;;) { |
| 359 | for (; ((*cc != ',') && (*cc != '\0')); cc++) ; |
| 360 | savecc = *cc; |
| 361 | *cc = '\0'; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 362 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 363 | found = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 364 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 365 | /* |
| 366 | * Determine if token is a valid string or number and if |
| 367 | * so, add the bits to the mask. |
| 368 | */ |
| 369 | for (ind = 0; |
| 370 | ind < sizeof(Lio_info2) / sizeof(struct lio_info_type); |
| 371 | ind++) { |
| 372 | if (strcmp(token, Lio_info2[ind].token) == 0) { |
| 373 | mask |= Lio_info2[ind].bits; |
| 374 | found = 1; |
| 375 | break; |
| 376 | } |
| 377 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 378 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 379 | /* |
| 380 | * If token does not match one of the defined tokens, determine |
| 381 | * if it is a number, if so, add the bits. |
| 382 | */ |
| 383 | if (!found) { |
| 384 | if (sscanf(token, "%i%c", &tmp, &chr) == 1) { |
| 385 | mask |= tmp; |
| 386 | found = 1; |
| 387 | } |
| 388 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 389 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 390 | *cc = savecc; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 391 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 392 | if (!found) { /* token is not valid */ |
| 393 | if (badtoken != NULL) |
| 394 | *badtoken = token; |
| 395 | return (-1); |
| 396 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 397 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 398 | if (savecc == '\0') |
| 399 | break; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 400 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 401 | token = ++cc; |
| 402 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 403 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 404 | return mask; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /*********************************************************************** |
| 408 | * This function will print a help message describing the tokens |
| 409 | * that can be parsed by lio_parse_io_arg2(). |
| 410 | * It will print them one per line. |
| 411 | * |
| 412 | * (rrl 04/96) |
| 413 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 414 | void lio_help2(char *prefix) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 415 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 416 | unsigned int ind; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 417 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 418 | for (ind = 0; ind < sizeof(Lio_info2) / sizeof(struct lio_info_type); |
| 419 | ind++) { |
| 420 | printf("%s %s : %s\n", prefix, Lio_info2[ind].token, |
| 421 | Lio_info2[ind].desc); |
| 422 | } |
| 423 | return; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 424 | } |
| 425 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 426 | /*********************************************************************** |
| 427 | * This is an internal signal handler. |
| 428 | * If the handler is called, it will increment the Received_signal |
| 429 | * global variable. |
| 430 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 431 | static void lio_async_signal_handler(int sig) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 432 | { |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 433 | if (Debug_level) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 434 | printf |
| 435 | ("DEBUG %s/%d: received signal %d, a signal caught %d times\n", |
| 436 | __FILE__, __LINE__, sig, Received_signal + 1); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 437 | |
| 438 | Received_signal++; |
| 439 | |
| 440 | return; |
| 441 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 442 | |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 443 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 444 | /*********************************************************************** |
| 445 | * This is an internal callback handler. |
| 446 | * If the handler is called, it will increment the Received_callback |
| 447 | * global variable. |
| 448 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 449 | static void lio_async_callback_handler(sigval_t sigval) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 450 | { |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 451 | if (Debug_level) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 452 | printf |
| 453 | ("DEBUG %s/%d: received callback, nbytes=%ld, a callback called %d times\n", |
| 454 | __FILE__, __LINE__, (long)sigval.sival_int, |
| 455 | Received_callback + 1); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 456 | |
| 457 | Received_callback++; |
| 458 | |
| 459 | return; |
| 460 | } |
| 461 | #endif /* sgi */ |
| 462 | |
| 463 | /*********************************************************************** |
| 464 | * lio_random_methods |
| 465 | * This function will randomly choose an io type and wait method |
| 466 | * from set of io types and wait methods. Since this information |
| 467 | * is stored in a bitmask, it randomly chooses an io type from |
| 468 | * the io type bits specified and does the same for wait methods. |
| 469 | * |
| 470 | * Return Value |
| 471 | * This function will return a value with all non choosen io type |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 472 | * and wait method bits cleared. The LIO_RANDOM bit is also |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 473 | * cleared. All other bits are left unchanged. |
| 474 | * |
| 475 | * (rrl 04/96) |
| 476 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 477 | int lio_random_methods(long curr_mask) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 478 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 479 | int mask = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 480 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 481 | /* remove random select, io type, and wait method bits from curr_mask */ |
| 482 | mask = curr_mask & (~(LIO_IO_TYPES | LIO_WAIT_TYPES | LIO_RANDOM)); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 483 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 484 | /* randomly select io type from specified io types */ |
| 485 | mask = mask | random_bit(curr_mask & LIO_IO_TYPES); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 486 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 487 | /* randomly select wait methods from specified wait methods */ |
| 488 | mask = mask | random_bit(curr_mask & LIO_WAIT_TYPES); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 489 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 490 | return mask; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 491 | } |
| 492 | |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 493 | static void wait4sync_io(int fd, int read) |
| 494 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 495 | fd_set s; |
| 496 | FD_ZERO(&s); |
| 497 | FD_SET(fd, &s); |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 498 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 499 | select(fd + 1, read ? &s : NULL, read ? NULL : &s, NULL, NULL); |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 500 | } |
| 501 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 502 | /*********************************************************************** |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 503 | * Generic write function |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 504 | * This function can be used to do a write using write(2), writea(2), |
| 505 | * aio_write(3), writev(2), pwrite(2), |
| 506 | * or single stride listio(2)/lio_listio(3). |
| 507 | * By setting the desired bits in the method |
| 508 | * bitmask, the caller can control the type of write and the wait method |
| 509 | * that will be used. If no io type bits are set, write will be used. |
| 510 | * |
| 511 | * If async io was attempted and no wait method bits are set then the |
| 512 | * wait method is: recall(2) for writea(2) and listio(2); aio_suspend(3) for |
| 513 | * aio_write(3) and lio_listio(3). |
| 514 | * |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 515 | * If multiple wait methods are specified, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 516 | * only one wait method will be used. The order is predetermined. |
| 517 | * |
| 518 | * If the call specifies a signal and one of the two signal wait methods, |
| 519 | * a signal handler for the signal is set. This will reset an already |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 520 | * set handler for this signal. |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 521 | * |
| 522 | * If the LIO_RANDOM method bit is set, this function will randomly |
| 523 | * choose a io type and wait method from bits in the method argument. |
| 524 | * |
| 525 | * If an error is encountered, an error message will be generated |
| 526 | * in a internal static buffer. If errmsg is not NULL, it will |
| 527 | * be updated to point to the static buffer, allowing the caller |
| 528 | * to print the error message. |
| 529 | * |
| 530 | * Return Value |
| 531 | * If a system call fails, -errno is returned. |
| 532 | * If LIO_WAIT_NONE bit is set, the return value is the return value |
| 533 | * of the system call. |
| 534 | * If the io did not fail, the amount of data written is returned. |
| 535 | * If the size the system call say was written is different |
| 536 | * then what was asked to be written, errmsg is updated for |
| 537 | * this error condition. The return value is still the amount |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 538 | * the system call says was written. |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 539 | * |
| 540 | * (rrl 04/96) |
| 541 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 542 | int lio_write_buffer(fd, method, buffer, size, sig, errmsg, wrd) |
| 543 | int fd; /* open file descriptor */ |
| 544 | int method; /* contains io type and wait method bitmask */ |
| 545 | char *buffer; /* pointer to buffer */ |
| 546 | int size; /* the size of the io */ |
| 547 | int sig; /* signal to use if async io */ |
| 548 | char **errmsg; /* char pointer that will be updated to point to err message */ |
| 549 | long wrd; /* to allow future features, use zero for now */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 550 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 551 | int ret = 0; /* syscall return or used to get random method */ |
| 552 | char *io_type; /* Holds string of type of io */ |
| 553 | int omethod = method; |
| 554 | int listio_cmd; /* Holds the listio/lio_listio cmd */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 555 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 556 | struct listreq request; /* Used when a listio is wanted */ |
| 557 | struct iosw status, *statptr[1]; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 558 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 559 | /* for linux or sgi */ |
| 560 | struct iovec iov; /* iovec for writev(2) */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 561 | #endif |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 562 | #if defined (sgi) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 563 | aiocb_t aiocbp; /* POSIX aio control block */ |
| 564 | aiocb_t *aiolist[1]; /* list of aio control blocks for lio_listio */ |
| 565 | off64_t poffset; /* pwrite(2) offset */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 566 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 567 | #if defined(__linux__) && !defined(__UCLIBC__) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 568 | struct aiocb aiocbp; /* POSIX aio control block */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 569 | struct aiocb *aiolist[1]; /* list of aio control blocks for lio_listio */ |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 570 | off64_t poffset; /* pwrite(2) offset */ |
| 571 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 572 | /* |
| 573 | * If LIO_RANDOM bit specified, get new method randomly. |
| 574 | */ |
| 575 | if (method & LIO_RANDOM) { |
| 576 | if (Debug_level > 3) |
| 577 | printf("DEBUG %s/%d: method mask to choose from: %#o\n", |
| 578 | __FILE__, __LINE__, method); |
| 579 | method = lio_random_methods(method); |
| 580 | if (Debug_level > 2) |
| 581 | printf("DEBUG %s/%d: random chosen method %#o\n", |
| 582 | __FILE__, __LINE__, method); |
| 583 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 584 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 585 | if (errmsg != NULL) |
| 586 | *errmsg = Errormsg; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 587 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 588 | Rec_signal = Received_signal; /* get the current number of signals received */ |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 589 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 590 | Rec_callback = Received_callback; /* get the current number of callbacks received */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 591 | #endif |
| 592 | |
| 593 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 594 | memset(&status, 0x00, sizeof(struct iosw)); |
| 595 | memset(&request, 0x00, sizeof(struct listreq)); |
| 596 | statptr[0] = &status; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 597 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 598 | /* for linux or sgi */ |
| 599 | memset(&iov, 0x00, sizeof(struct iovec)); |
| 600 | iov.iov_base = buffer; |
| 601 | iov.iov_len = size; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 602 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 603 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 604 | #if defined(sgi) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 605 | memset(&aiocbp, 0x00, sizeof(aiocb_t)); |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 606 | #else |
| 607 | memset(&aiocbp, 0x00, sizeof(struct aiocb)); |
| 608 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 609 | aiocbp.aio_fildes = fd; |
| 610 | aiocbp.aio_nbytes = size; |
| 611 | aiocbp.aio_buf = buffer; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 612 | /* aiocbp.aio_offset = lseek( fd, 0, SEEK_CUR ); -- set below */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 613 | aiocbp.aio_sigevent.sigev_notify = SIGEV_NONE; |
| 614 | aiocbp.aio_sigevent.sigev_signo = 0; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 615 | #ifdef sgi |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 616 | aiocbp.aio_sigevent.sigev_func = NULL; |
| 617 | aiocbp.aio_sigevent.sigev_value.sival_int = 0; |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 618 | #elif defined(__linux__) && !defined(__UCLIBC__) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 619 | aiocbp.aio_sigevent.sigev_notify_function = NULL; |
| 620 | aiocbp.aio_sigevent.sigev_notify_attributes = 0; |
| 621 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 622 | aiolist[0] = &aiocbp; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 623 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 624 | if ((ret = lseek(fd, 0, SEEK_CUR)) == -1) { |
| 625 | ret = 0; |
| 626 | /* If there is an error and it is not ESPIPE then kick out the error. |
| 627 | * If the fd is a fifo then we have to make sure that |
| 628 | * lio_random_methods() didn't select pwrite/pread; if it did then |
| 629 | * switch to write/read. |
| 630 | */ |
| 631 | if (errno == ESPIPE) { |
| 632 | if (method & LIO_IO_SYNCP) { |
| 633 | if (omethod & LIO_RANDOM) { |
| 634 | method &= ~LIO_IO_SYNCP; |
| 635 | method |= LIO_IO_SYNC; |
| 636 | if (Debug_level > 2) |
| 637 | printf |
| 638 | ("DEBUG %s/%d: random chosen method switched to %#o for fifo\n", |
| 639 | __FILE__, __LINE__, |
| 640 | method); |
| 641 | } else if (Debug_level) { |
| 642 | printf |
| 643 | ("DEBUG %s/%d: pwrite will fail when it writes to a fifo\n", |
| 644 | __FILE__, __LINE__); |
| 645 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 646 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 647 | /* else: let it ride */ |
| 648 | } else { |
| 649 | sprintf(Errormsg, |
| 650 | "%s/%d lseek(fd=%d,0,SEEK_CUR) failed, errno=%d %s", |
| 651 | __FILE__, __LINE__, fd, errno, strerror(errno)); |
| 652 | return -errno; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 653 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 654 | } |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 655 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 656 | poffset = (off64_t) ret; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 657 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 658 | aiocbp.aio_offset = ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 659 | |
| 660 | #endif |
| 661 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 662 | /* |
| 663 | * If the LIO_USE_SIGNAL bit is not set, only use the signal |
| 664 | * if the LIO_WAIT_SIGPAUSE or the LIO_WAIT_SIGACTIVE bits are bit. |
| 665 | * Otherwise there is not necessary a signal handler to trap |
| 666 | * the signal. |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 667 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 668 | if (sig && !(method & LIO_USE_SIGNAL) && !(method & LIO_WAIT_SIGTYPES)) { |
| 669 | |
| 670 | sig = 0; /* ignore signal parameter */ |
| 671 | } |
| 672 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
| 673 | if (sig && (method & LIO_WAIT_CBTYPES)) |
| 674 | sig = 0; /* ignore signal parameter */ |
| 675 | #endif |
| 676 | |
| 677 | /* |
| 678 | * only setup signal hander if sig was specified and |
| 679 | * a sig wait method was specified. |
| 680 | * Doing this will change the handler for this signal. The |
| 681 | * old signal handler will not be restored. |
| 682 | *** restoring the signal handler could be added *** |
| 683 | */ |
| 684 | |
| 685 | if (sig && (method & LIO_WAIT_SIGTYPES)) { |
| 686 | #ifdef CRAY |
| 687 | sigctl(SCTL_REG, sig, lio_async_signal_handler); |
| 688 | #endif |
| 689 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
| 690 | aiocbp.aio_sigevent.sigev_notify = SIGEV_SIGNAL; |
| 691 | aiocbp.aio_sigevent.sigev_signo = sig; |
| 692 | sigset(sig, lio_async_signal_handler); |
| 693 | #endif /* sgi */ |
| 694 | } |
| 695 | #if defined(sgi) |
| 696 | else if (method & LIO_WAIT_CBTYPES) { |
| 697 | /* sival_int just has to be something that I can use |
| 698 | * to identify the callback, and "size" happens to be handy... |
| 699 | */ |
| 700 | aiocbp.aio_sigevent.sigev_notify = SIGEV_CALLBACK; |
| 701 | aiocbp.aio_sigevent.sigev_func = lio_async_callback_handler; |
| 702 | aiocbp.aio_sigevent.sigev_value.sival_int = size; |
| 703 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 704 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 705 | #if defined(__linux__) && !defined(__UCLIBC__) |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 706 | else if (method & LIO_WAIT_CBTYPES) { |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 707 | /* sival_int just has to be something that I can use |
| 708 | * to identify the callback, and "size" happens to be handy... |
| 709 | */ |
| 710 | aiocbp.aio_sigevent.sigev_notify = SIGEV_THREAD; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 711 | aiocbp.aio_sigevent.sigev_notify_function = |
| 712 | lio_async_callback_handler; |
| 713 | aiocbp.aio_sigevent.sigev_notify_attributes = |
| 714 | (void *)(uintptr_t) size; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 715 | } |
| 716 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 717 | /* |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 718 | * Determine the system call that will be called and produce |
| 719 | * the string of the system call and place it in Lio_SysCall. |
| 720 | * Also update the io_type char pointer to give brief description |
| 721 | * of system call. Execute the system call and check for |
| 722 | * system call failure. If sync i/o, return the number of |
| 723 | * bytes written/read. |
| 724 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 725 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 726 | if ((method & LIO_IO_SYNC) |
| 727 | || (method & (LIO_IO_TYPES | LIO_IO_ATYPES)) == 0) { |
| 728 | /* |
| 729 | * write(2) is used if LIO_IO_SYNC bit is set or not none |
| 730 | * of the LIO_IO_TYPES bits are set (default). |
| 731 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 732 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 733 | sprintf(Lio_SysCall, "write(%d, buf, %d)", fd, size); |
| 734 | io_type = "write"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 735 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 736 | if (Debug_level) { |
| 737 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 738 | Lio_SysCall); |
| 739 | } |
| 740 | while (1) { |
| 741 | if (((ret = write(fd, buffer, size)) == -1) |
| 742 | && errno != EAGAIN && errno != EINTR) { |
| 743 | sprintf(Errormsg, |
| 744 | "%s/%d write(%d, buf, %d) ret:-1, errno=%d %s", |
| 745 | __FILE__, __LINE__, fd, size, errno, |
| 746 | strerror(errno)); |
| 747 | return -errno; |
| 748 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 749 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 750 | if (ret != -1) { |
| 751 | if (ret != size) { |
| 752 | sprintf(Errormsg, |
| 753 | "%s/%d write(%d, buf, %d) returned=%d", |
| 754 | __FILE__, __LINE__, |
| 755 | fd, size, ret); |
| 756 | size -= ret; |
| 757 | buffer += ret; |
| 758 | } else { |
| 759 | if (Debug_level > 1) |
| 760 | printf |
| 761 | ("DEBUG %s/%d: write completed without error (ret %d)\n", |
| 762 | __FILE__, __LINE__, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 763 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | } |
| 767 | wait4sync_io(fd, 0); |
| 768 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 769 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 770 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 771 | |
| 772 | else if (method & LIO_IO_ASYNC) { |
| 773 | #ifdef CRAY |
| 774 | sprintf(Lio_SysCall, |
| 775 | "writea(%d, buf, %d, &status, %d)", fd, size, sig); |
| 776 | io_type = "writea"; |
| 777 | |
| 778 | if (Debug_level) { |
| 779 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 780 | Lio_SysCall); |
| 781 | } |
| 782 | |
| 783 | sigoff(); |
| 784 | if ((ret = writea(fd, buffer, size, &status, sig)) == -1) { |
| 785 | sprintf(Errormsg, |
| 786 | "%s/%d writea(%d, buf, %d, &stat, %d) ret:-1, errno=%d %s", |
| 787 | __FILE__, __LINE__, |
| 788 | fd, size, sig, errno, strerror(errno)); |
| 789 | sigon(); |
| 790 | return -errno; |
| 791 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 792 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 793 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 794 | sprintf(Lio_SysCall, |
| 795 | "aio_write(fildes=%d, buf, nbytes=%d, signo=%d)", fd, |
| 796 | size, sig); |
| 797 | io_type = "aio_write"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 798 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 799 | if (Debug_level) { |
| 800 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 801 | Lio_SysCall); |
| 802 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 803 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 804 | if (sig) |
| 805 | sighold(sig); |
| 806 | if ((ret = aio_write(&aiocbp)) == -1) { |
| 807 | sprintf(Errormsg, |
| 808 | "%s/%d aio_write(fildes=%d, buf, nbytes=%d, signo=%d) ret:-1, errno=%d %s", |
| 809 | __FILE__, __LINE__, |
| 810 | fd, size, sig, errno, strerror(errno)); |
| 811 | if (sig) |
| 812 | sigrelse(sig); |
| 813 | return -errno; |
| 814 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 815 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 816 | } |
| 817 | /* LIO_IO_ASYNC */ |
| 818 | else if (method & LIO_IO_SLISTIO) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 819 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 820 | request.li_opcode = LO_WRITE; |
| 821 | request.li_fildes = fd; |
| 822 | request.li_buf = buffer; |
| 823 | request.li_nbyte = size; |
| 824 | request.li_status = &status; |
| 825 | request.li_signo = sig; |
| 826 | request.li_nstride = 0; |
| 827 | request.li_filstride = 0; |
| 828 | request.li_memstride = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 829 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 830 | listio_cmd = LC_WAIT; |
| 831 | io_type = "listio(2) sync write"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 832 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 833 | sprintf(Lio_SysCall, |
| 834 | "listio(LC_WAIT, &req, 1) LO_WRITE, fd:%d, nbyte:%d", |
| 835 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 836 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 837 | if (Debug_level) { |
| 838 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 839 | Lio_SysCall); |
| 840 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 841 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 842 | sigoff(); |
| 843 | if (listio(listio_cmd, &request, 1) == -1) { |
| 844 | sprintf(Errormsg, |
| 845 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 846 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 847 | errno, strerror(errno)); |
| 848 | sigon(); |
| 849 | return -errno; |
| 850 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 851 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 852 | if (Debug_level > 1) |
| 853 | printf("DEBUG %s/%d: %s did not return -1\n", |
| 854 | __FILE__, __LINE__, Lio_SysCall); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 855 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 856 | ret = lio_check_asyncio(io_type, size, &status); |
| 857 | return ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 858 | |
| 859 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 860 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 861 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 862 | aiocbp.aio_lio_opcode = LIO_WRITE; |
| 863 | listio_cmd = LIO_WAIT; |
| 864 | io_type = "lio_listio(3) sync write"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 865 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 866 | sprintf(Lio_SysCall, |
| 867 | "lio_listio(LIO_WAIT, aiolist, 1, NULL) LIO_WRITE, fd:%d, nbyte:%d, sig:%d", |
| 868 | fd, size, sig); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 869 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 870 | if (Debug_level) { |
| 871 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 872 | Lio_SysCall); |
| 873 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 874 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 875 | if (sig) |
| 876 | sighold(sig); |
| 877 | if (lio_listio(listio_cmd, aiolist, 1, NULL) == -1) { |
| 878 | sprintf(Errormsg, |
| 879 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 880 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 881 | errno, strerror(errno)); |
| 882 | if (sig) |
| 883 | sigrelse(sig); |
| 884 | return -errno; |
| 885 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 886 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 887 | if (Debug_level > 1) |
| 888 | printf("DEBUG %s/%d: %s did not return -1\n", |
| 889 | __FILE__, __LINE__, Lio_SysCall); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 890 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 891 | ret = lio_check_asyncio(io_type, size, &aiocbp, method); |
| 892 | return ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 893 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 894 | } |
| 895 | /* LIO_IO_SLISTIO */ |
| 896 | else if (method & LIO_IO_ALISTIO) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 897 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 898 | request.li_opcode = LO_WRITE; |
| 899 | request.li_fildes = fd; |
| 900 | request.li_buf = buffer; |
| 901 | request.li_nbyte = size; |
| 902 | request.li_status = &status; |
| 903 | request.li_signo = sig; |
| 904 | request.li_nstride = 0; |
| 905 | request.li_filstride = 0; |
| 906 | request.li_memstride = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 907 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 908 | listio_cmd = LC_START; |
| 909 | io_type = "listio(2) async write"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 910 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 911 | sprintf(Lio_SysCall, |
| 912 | "listio(LC_START, &req, 1) LO_WRITE, fd:%d, nbyte:%d", |
| 913 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 914 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 915 | if (Debug_level) { |
| 916 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 917 | Lio_SysCall); |
| 918 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 919 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 920 | sigoff(); |
| 921 | if (listio(listio_cmd, &request, 1) == -1) { |
| 922 | sprintf(Errormsg, |
| 923 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 924 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 925 | errno, strerror(errno)); |
| 926 | sigon(); |
| 927 | return -errno; |
| 928 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 929 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 930 | #if defined (sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 931 | aiocbp.aio_lio_opcode = LIO_WRITE; |
| 932 | listio_cmd = LIO_NOWAIT; |
| 933 | io_type = "lio_listio(3) async write"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 934 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 935 | sprintf(Lio_SysCall, |
| 936 | "lio_listio(LIO_NOWAIT, aiolist, 1, NULL) LIO_WRITE, fd:%d, nbyte:%d", |
| 937 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 938 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 939 | if (Debug_level) { |
| 940 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 941 | Lio_SysCall); |
| 942 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 943 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 944 | if (sig) |
| 945 | sighold(sig); |
| 946 | if (lio_listio(listio_cmd, aiolist, 1, NULL) == -1) { |
| 947 | sprintf(Errormsg, |
| 948 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 949 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 950 | errno, strerror(errno)); |
| 951 | if (sig) |
| 952 | sigrelse(sig); |
| 953 | return -errno; |
| 954 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 955 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 956 | } |
| 957 | /* LIO_IO_ALISTIO */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 958 | #ifndef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 959 | else if (method & LIO_IO_SYNCV) { |
| 960 | io_type = "writev(2)"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 961 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 962 | sprintf(Lio_SysCall, "writev(%d, &iov, 1) nbyte:%d", fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 963 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 964 | if (Debug_level) { |
| 965 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 966 | Lio_SysCall); |
| 967 | } |
| 968 | if ((ret = writev(fd, &iov, 1)) == -1) { |
| 969 | sprintf(Errormsg, |
| 970 | "%s/%d writev(%d, iov, 1) nbyte:%d ret:-1, errno=%d %s", |
| 971 | __FILE__, __LINE__, fd, size, errno, |
| 972 | strerror(errno)); |
| 973 | return -errno; |
| 974 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 975 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 976 | if (ret != size) { |
| 977 | sprintf(Errormsg, |
| 978 | "%s/%d writev(%d, iov, 1) nbyte:%d returned=%d", |
| 979 | __FILE__, __LINE__, fd, size, ret); |
| 980 | } else if (Debug_level > 1) |
| 981 | printf |
| 982 | ("DEBUG %s/%d: writev completed without error (ret %d)\n", |
| 983 | __FILE__, __LINE__, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 984 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 985 | return ret; |
| 986 | } /* LIO_IO_SYNCV */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 987 | #endif |
| 988 | |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 989 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 990 | else if (method & LIO_IO_SYNCP) { |
| 991 | io_type = "pwrite(2)"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 992 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 993 | sprintf(Lio_SysCall, |
| 994 | "pwrite(%d, buf, %d, %lld)", fd, size, |
| 995 | (long long)poffset); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 996 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 997 | if (Debug_level) { |
| 998 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 999 | Lio_SysCall); |
| 1000 | } |
| 1001 | if ((ret = pwrite(fd, buffer, size, poffset)) == -1) { |
| 1002 | sprintf(Errormsg, |
| 1003 | "%s/%d pwrite(%d, buf, %d, %lld) ret:-1, errno=%d %s", |
| 1004 | __FILE__, __LINE__, fd, size, |
| 1005 | (long long)poffset, errno, strerror(errno)); |
| 1006 | return -errno; |
| 1007 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1008 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1009 | if (ret != size) { |
| 1010 | sprintf(Errormsg, |
| 1011 | "%s/%d pwrite(%d, buf, %d, %lld) returned=%d", |
| 1012 | __FILE__, __LINE__, |
| 1013 | fd, size, (long long)poffset, ret); |
| 1014 | } else if (Debug_level > 1) |
| 1015 | printf |
| 1016 | ("DEBUG %s/%d: pwrite completed without error (ret %d)\n", |
| 1017 | __FILE__, __LINE__, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1018 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1019 | return ret; |
| 1020 | } /* LIO_IO_SYNCP */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1021 | #endif |
| 1022 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1023 | else { |
| 1024 | printf("DEBUG %s/%d: No I/O method chosen\n", __FILE__, |
| 1025 | __LINE__); |
| 1026 | return -1; |
| 1027 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1028 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1029 | /* |
| 1030 | * wait for async io to complete. |
| 1031 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1032 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1033 | ret = lio_wait4asyncio(method, fd, statptr); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1034 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1035 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1036 | ret = lio_wait4asyncio(method, fd, &aiocbp); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1037 | #endif |
| 1038 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1039 | /* |
| 1040 | * If there was an error waiting for async i/o to complete, |
| 1041 | * return the error value (errno) to the caller. |
| 1042 | * Note: Errormsg should already have been updated. |
| 1043 | */ |
| 1044 | if (ret < 0) { |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * If i/o was not waited for (may not have been completed at this time), |
| 1050 | * return the size that was requested. |
| 1051 | */ |
| 1052 | if (ret == 1) |
| 1053 | return size; |
| 1054 | |
| 1055 | /* |
| 1056 | * check that async io was successful. |
| 1057 | * Note: if the there was an system call failure, -errno |
| 1058 | * was returned and Errormsg should already have been updated. |
| 1059 | * If amount i/o was different than size, Errormsg should already |
| 1060 | * have been updated but the actual i/o size if returned. |
| 1061 | */ |
| 1062 | |
| 1063 | #ifdef CRAY |
| 1064 | ret = lio_check_asyncio(io_type, size, &status); |
| 1065 | #endif |
| 1066 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
| 1067 | ret = lio_check_asyncio(io_type, size, &aiocbp, method); |
| 1068 | #endif |
| 1069 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1070 | return ret; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1071 | } /* end of lio_write_buffer */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1072 | |
| 1073 | /*********************************************************************** |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 1074 | * Generic read function |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1075 | * This function can be used to do a read using read(2), reada(2), |
| 1076 | * aio_read(3), readv(2), pread(2), |
| 1077 | * or single stride listio(2)/lio_listio(3). |
| 1078 | * By setting the desired bits in the method |
| 1079 | * bitmask, the caller can control the type of read and the wait method |
| 1080 | * that will be used. If no io type bits are set, read will be used. |
| 1081 | * |
| 1082 | * If async io was attempted and no wait method bits are set then the |
| 1083 | * wait method is: recall(2) for reada(2) and listio(2); aio_suspend(3) for |
| 1084 | * aio_read(3) and lio_listio(3). |
| 1085 | * |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 1086 | * If multiple wait methods are specified, |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1087 | * only one wait method will be used. The order is predetermined. |
| 1088 | * |
| 1089 | * If the call specifies a signal and one of the two signal wait methods, |
| 1090 | * a signal handler for the signal is set. This will reset an already |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 1091 | * set handler for this signal. |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1092 | * |
| 1093 | * If the LIO_RANDOM method bit is set, this function will randomly |
| 1094 | * choose a io type and wait method from bits in the method argument. |
| 1095 | * |
| 1096 | * If an error is encountered, an error message will be generated |
| 1097 | * in a internal static buffer. If errmsg is not NULL, it will |
| 1098 | * be updated to point to the static buffer, allowing the caller |
| 1099 | * to print the error message. |
| 1100 | * |
| 1101 | * Return Value |
| 1102 | * If a system call fails, -errno is returned. |
| 1103 | * If LIO_WAIT_NONE bit is set, the return value is the return value |
| 1104 | * of the system call. |
| 1105 | * If the io did not fail, the amount of data written is returned. |
| 1106 | * If the size the system call say was written is different |
| 1107 | * then what was asked to be written, errmsg is updated for |
| 1108 | * this error condition. The return value is still the amount |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 1109 | * the system call says was written. |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1110 | * |
| 1111 | * (rrl 04/96) |
| 1112 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1113 | int lio_read_buffer(fd, method, buffer, size, sig, errmsg, wrd) |
| 1114 | int fd; /* open file descriptor */ |
| 1115 | int method; /* contains io type and wait method bitmask */ |
| 1116 | char *buffer; /* pointer to buffer */ |
| 1117 | int size; /* the size of the io */ |
| 1118 | int sig; /* signal to use if async io */ |
| 1119 | char **errmsg; /* char pointer that will be updated to point to err message */ |
| 1120 | long wrd; /* to allow future features, use zero for now */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1121 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1122 | int ret = 0; /* syscall return or used to get random method */ |
| 1123 | char *io_type; /* Holds string of type of io */ |
| 1124 | int listio_cmd; /* Holds the listio/lio_listio cmd */ |
| 1125 | int omethod = method; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1126 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1127 | struct listreq request; /* Used when a listio is wanted */ |
| 1128 | struct iosw status, *statptr[1]; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1129 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1130 | /* for linux or sgi */ |
| 1131 | struct iovec iov; /* iovec for readv(2) */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1132 | #endif |
| 1133 | #ifdef sgi |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1134 | aiocb_t aiocbp; /* POSIX aio control block */ |
| 1135 | aiocb_t *aiolist[1]; /* list of aio control blocks for lio_listio */ |
| 1136 | off64_t poffset; /* pread(2) offset */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1137 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1138 | #if defined (__linux__) && !defined(__UCLIBC__) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1139 | struct aiocb aiocbp; /* POSIX aio control block */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1140 | struct aiocb *aiolist[1]; /* list of aio control blocks for lio_listio */ |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1141 | off64_t poffset; /* pread(2) offset */ |
| 1142 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1143 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1144 | /* |
| 1145 | * If LIO_RANDOM bit specified, get new method randomly. |
| 1146 | */ |
| 1147 | if (method & LIO_RANDOM) { |
| 1148 | if (Debug_level > 3) |
| 1149 | printf("DEBUG %s/%d: method mask to choose from: %#o\n", |
| 1150 | __FILE__, __LINE__, method); |
| 1151 | method = lio_random_methods(method); |
| 1152 | if (Debug_level > 2) |
| 1153 | printf("DEBUG %s/%d: random chosen method %#o\n", |
| 1154 | __FILE__, __LINE__, method); |
| 1155 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1156 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1157 | if (errmsg != NULL) |
| 1158 | *errmsg = Errormsg; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1159 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1160 | Rec_signal = Received_signal; /* get the current number of signals received */ |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1161 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1162 | Rec_callback = Received_callback; /* get the current number of callbacks received */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1163 | #endif |
| 1164 | |
| 1165 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1166 | memset(&status, 0x00, sizeof(struct iosw)); |
| 1167 | memset(&request, 0x00, sizeof(struct listreq)); |
| 1168 | statptr[0] = &status; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1169 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1170 | /* for linux or sgi */ |
| 1171 | memset(&iov, 0x00, sizeof(struct iovec)); |
| 1172 | iov.iov_base = buffer; |
| 1173 | iov.iov_len = size; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1174 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1175 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1176 | #if defined(sgi) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1177 | memset(&aiocbp, 0x00, sizeof(aiocb_t)); |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1178 | #else |
| 1179 | memset(&aiocbp, 0x00, sizeof(struct aiocb)); |
| 1180 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1181 | aiocbp.aio_fildes = fd; |
| 1182 | aiocbp.aio_nbytes = size; |
| 1183 | aiocbp.aio_buf = buffer; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1184 | /* aiocbp.aio_offset = lseek( fd, 0, SEEK_CUR ); -- set below */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1185 | aiocbp.aio_sigevent.sigev_notify = SIGEV_NONE; |
| 1186 | aiocbp.aio_sigevent.sigev_signo = 0; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1187 | #ifdef sgi |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1188 | aiocbp.aio_sigevent.sigev_func = NULL; |
| 1189 | aiocbp.aio_sigevent.sigev_value.sival_int = 0; |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1190 | #elif defined(__linux__) && !defined(__UCLIBC__) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1191 | aiocbp.aio_sigevent.sigev_notify_function = NULL; |
| 1192 | aiocbp.aio_sigevent.sigev_notify_attributes = 0; |
| 1193 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1194 | aiolist[0] = &aiocbp; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1195 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1196 | if ((ret = lseek(fd, 0, SEEK_CUR)) == -1) { |
| 1197 | ret = 0; |
| 1198 | /* If there is an error and it is not ESPIPE then kick out the error. |
| 1199 | * If the fd is a fifo then we have to make sure that |
| 1200 | * lio_random_methods() didn't select pwrite/pread; if it did then |
| 1201 | * switch to write/read. |
| 1202 | */ |
| 1203 | if (errno == ESPIPE) { |
| 1204 | if (method & LIO_IO_SYNCP) { |
| 1205 | if (omethod & LIO_RANDOM) { |
| 1206 | method &= ~LIO_IO_SYNCP; |
| 1207 | method |= LIO_IO_SYNC; |
| 1208 | if (Debug_level > 2) |
| 1209 | printf |
| 1210 | ("DEBUG %s/%d: random chosen method switched to %#o for fifo\n", |
| 1211 | __FILE__, __LINE__, |
| 1212 | method); |
| 1213 | } else if (Debug_level) { |
| 1214 | printf |
| 1215 | ("DEBUG %s/%d: pread will fail when it reads from a fifo\n", |
| 1216 | __FILE__, __LINE__); |
| 1217 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1218 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1219 | /* else: let it ride */ |
| 1220 | } else { |
| 1221 | sprintf(Errormsg, |
| 1222 | "%s/%d lseek(fd=%d,0,SEEK_CUR) failed, errno=%d %s", |
| 1223 | __FILE__, __LINE__, fd, errno, strerror(errno)); |
| 1224 | return -errno; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1225 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1226 | } |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1227 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1228 | poffset = (off64_t) ret; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1229 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1230 | aiocbp.aio_offset = ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1231 | |
| 1232 | #endif |
| 1233 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1234 | /* |
| 1235 | * If the LIO_USE_SIGNAL bit is not set, only use the signal |
| 1236 | * if the LIO_WAIT_SIGPAUSE or the LIO_WAIT_SIGACTIVE bits are set. |
| 1237 | * Otherwise there is not necessarily a signal handler to trap |
| 1238 | * the signal. |
| 1239 | */ |
| 1240 | if (sig && !(method & LIO_USE_SIGNAL) && !(method & LIO_WAIT_SIGTYPES)) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1241 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1242 | sig = 0; /* ignore signal parameter */ |
| 1243 | } |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1244 | #if defined(sgi) || (defined(__linux__)&& !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1245 | if (sig && (method & LIO_WAIT_CBTYPES)) |
| 1246 | sig = 0; /* ignore signal parameter */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1247 | #endif |
| 1248 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1249 | /* |
| 1250 | * only setup signal hander if sig was specified and |
| 1251 | * a sig wait method was specified. |
| 1252 | * Doing this will change the handler for this signal. The |
| 1253 | * old signal handler will not be restored. |
| 1254 | *** restoring the signal handler could be added *** |
| 1255 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1256 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1257 | if (sig && (method & LIO_WAIT_SIGTYPES)) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1258 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1259 | sigctl(SCTL_REG, sig, lio_async_signal_handler); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1260 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1261 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1262 | aiocbp.aio_sigevent.sigev_notify = SIGEV_SIGNAL; |
| 1263 | aiocbp.aio_sigevent.sigev_signo = sig; |
| 1264 | sigset(sig, lio_async_signal_handler); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1265 | #endif /* CRAY */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1266 | } |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1267 | #if defined(sgi) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1268 | else if (method & LIO_WAIT_CBTYPES) { |
| 1269 | aiocbp.aio_sigevent.sigev_notify = SIGEV_CALLBACK; |
| 1270 | aiocbp.aio_sigevent.sigev_func = lio_async_callback_handler; |
| 1271 | /* sival_int just has to be something that I can use |
| 1272 | * to identify the callback, and "size" happens to be handy... |
| 1273 | */ |
| 1274 | aiocbp.aio_sigevent.sigev_value.sival_int = size; |
| 1275 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1276 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1277 | #if defined(__linux__) && !defined(__UCLIBC__) |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 1278 | else if (method & LIO_WAIT_CBTYPES) { |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1279 | aiocbp.aio_sigevent.sigev_notify = SIGEV_THREAD; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1280 | aiocbp.aio_sigevent.sigev_notify_function = |
| 1281 | lio_async_callback_handler; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1282 | /* sival_int just has to be something that I can use |
| 1283 | * to identify the callback, and "size" happens to be handy... |
| 1284 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1285 | aiocbp.aio_sigevent.sigev_notify_attributes = |
| 1286 | (void *)(uintptr_t) size; |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1287 | } |
| 1288 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1289 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1290 | /* |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1291 | * Determine the system call that will be called and produce |
| 1292 | * the string of the system call and place it in Lio_SysCall. |
| 1293 | * Also update the io_type char pointer to give brief description |
| 1294 | * of system call. Execute the system call and check for |
| 1295 | * system call failure. If sync i/o, return the number of |
| 1296 | * bytes written/read. |
| 1297 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1298 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1299 | if ((method & LIO_IO_SYNC) |
| 1300 | || (method & (LIO_IO_TYPES | LIO_IO_ATYPES)) == 0) { |
| 1301 | /* |
| 1302 | * read(2) is used if LIO_IO_SYNC bit is set or not none |
| 1303 | * of the LIO_IO_TYPES bits are set (default). |
| 1304 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1305 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1306 | sprintf(Lio_SysCall, "read(%d, buf, %d)", fd, size); |
| 1307 | io_type = "read"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1308 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1309 | if (Debug_level) { |
| 1310 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1311 | Lio_SysCall); |
| 1312 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1313 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1314 | while (1) { |
| 1315 | if (((ret = read(fd, buffer, size)) == -1) |
| 1316 | && errno != EINTR && errno != EAGAIN) { |
| 1317 | sprintf(Errormsg, |
| 1318 | "%s/%d read(%d, buf, %d) ret:-1, errno=%d %s", |
| 1319 | __FILE__, __LINE__, fd, size, errno, |
| 1320 | strerror(errno)); |
| 1321 | return -errno; |
| 1322 | } |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1323 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1324 | if (ret == 0) |
| 1325 | return 0; |
| 1326 | if (ret != -1) { |
| 1327 | if (ret != size) { |
| 1328 | sprintf(Errormsg, |
| 1329 | "%s/%d read(%d, buf, %d) returned=%d", |
| 1330 | __FILE__, __LINE__, |
| 1331 | fd, size, ret); |
| 1332 | size -= ret; |
| 1333 | buffer += ret; |
| 1334 | } else { |
| 1335 | if (Debug_level > 1) |
| 1336 | printf |
| 1337 | ("DEBUG %s/%d: read completed without error (ret %d)\n", |
| 1338 | __FILE__, __LINE__, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1339 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1340 | return ret; |
| 1341 | } |
| 1342 | } |
| 1343 | wait4sync_io(fd, 1); |
| 1344 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1345 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1346 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1347 | |
| 1348 | else if (method & LIO_IO_ASYNC) { |
| 1349 | #ifdef CRAY |
| 1350 | sprintf(Lio_SysCall, |
| 1351 | "reada(%d, buf, %d, &status, %d)", fd, size, sig); |
| 1352 | io_type = "reada"; |
| 1353 | |
| 1354 | if (Debug_level) { |
| 1355 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1356 | Lio_SysCall); |
| 1357 | } |
| 1358 | |
| 1359 | sigoff(); |
| 1360 | if ((ret = reada(fd, buffer, size, &status, sig)) == -1) { |
| 1361 | sprintf(Errormsg, |
| 1362 | "%s/%d reada(%d, buf, %d, &stat, %d) ret:-1, errno=%d %s", |
| 1363 | __FILE__, __LINE__, |
| 1364 | fd, size, sig, errno, strerror(errno)); |
| 1365 | sigon(); |
| 1366 | return -errno; |
| 1367 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1368 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1369 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1370 | sprintf(Lio_SysCall, |
| 1371 | "aio_read(fildes=%d, buf, nbytes=%d, signo=%d)", fd, |
| 1372 | size, sig); |
| 1373 | io_type = "aio_read"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1374 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1375 | if (Debug_level) { |
| 1376 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1377 | Lio_SysCall); |
| 1378 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1379 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1380 | if (sig) |
| 1381 | sighold(sig); |
| 1382 | if ((ret = aio_read(&aiocbp)) == -1) { |
| 1383 | sprintf(Errormsg, |
| 1384 | "%s/%d aio_read(fildes=%d, buf, nbytes=%d, signo=%d) ret:-1, errno=%d %s", |
| 1385 | __FILE__, __LINE__, |
| 1386 | fd, size, sig, errno, strerror(errno)); |
| 1387 | if (sig) |
| 1388 | sigrelse(sig); |
| 1389 | return -errno; |
| 1390 | } |
| 1391 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1392 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1393 | /* LIO_IO_ASYNC */ |
| 1394 | else if (method & LIO_IO_SLISTIO) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1395 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1396 | request.li_opcode = LO_READ; |
| 1397 | request.li_fildes = fd; |
| 1398 | request.li_buf = buffer; |
| 1399 | request.li_nbyte = size; |
| 1400 | request.li_status = &status; |
| 1401 | request.li_signo = sig; |
| 1402 | request.li_nstride = 0; |
| 1403 | request.li_filstride = 0; |
| 1404 | request.li_memstride = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1405 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1406 | listio_cmd = LC_WAIT; |
| 1407 | io_type = "listio(2) sync read"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1408 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1409 | sprintf(Lio_SysCall, |
| 1410 | "listio(LC_WAIT, &req, 1) LO_READ, fd:%d, nbyte:%d", |
| 1411 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1412 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1413 | if (Debug_level) { |
| 1414 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1415 | Lio_SysCall); |
| 1416 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1417 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1418 | sigoff(); |
| 1419 | if (listio(listio_cmd, &request, 1) == -1) { |
| 1420 | sprintf(Errormsg, |
| 1421 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 1422 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 1423 | errno, strerror(errno)); |
| 1424 | sigon(); |
| 1425 | return -errno; |
| 1426 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1427 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1428 | if (Debug_level > 1) |
| 1429 | printf("DEBUG %s/%d: %s did not return -1\n", |
| 1430 | __FILE__, __LINE__, Lio_SysCall); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1431 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1432 | ret = lio_check_asyncio(io_type, size, &status); |
| 1433 | return ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1434 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1435 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1436 | aiocbp.aio_lio_opcode = LIO_READ; |
| 1437 | listio_cmd = LIO_WAIT; |
| 1438 | io_type = "lio_listio(3) sync read"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1439 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1440 | sprintf(Lio_SysCall, |
| 1441 | "lio_listio(LIO_WAIT, aiolist, 1, NULL) LIO_READ, fd:%d, nbyte:%d", |
| 1442 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1443 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1444 | if (Debug_level) { |
| 1445 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1446 | Lio_SysCall); |
| 1447 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1448 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1449 | if (sig) |
| 1450 | sighold(sig); |
| 1451 | if (lio_listio(listio_cmd, aiolist, 1, NULL) == -1) { |
| 1452 | sprintf(Errormsg, |
| 1453 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 1454 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 1455 | errno, strerror(errno)); |
| 1456 | if (sig) |
| 1457 | sigrelse(sig); |
| 1458 | return -errno; |
| 1459 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1460 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1461 | if (Debug_level > 1) |
| 1462 | printf("DEBUG %s/%d: %s did not return -1\n", |
| 1463 | __FILE__, __LINE__, Lio_SysCall); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1464 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1465 | ret = lio_check_asyncio(io_type, size, &aiocbp, method); |
| 1466 | return ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1467 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1468 | } |
| 1469 | /* LIO_IO_SLISTIO */ |
| 1470 | else if (method & LIO_IO_ALISTIO) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1471 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1472 | request.li_opcode = LO_READ; |
| 1473 | request.li_fildes = fd; |
| 1474 | request.li_buf = buffer; |
| 1475 | request.li_nbyte = size; |
| 1476 | request.li_status = &status; |
| 1477 | request.li_signo = sig; |
| 1478 | request.li_nstride = 0; |
| 1479 | request.li_filstride = 0; |
| 1480 | request.li_memstride = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1481 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1482 | listio_cmd = LC_START; |
| 1483 | io_type = "listio(2) async read"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1484 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1485 | sprintf(Lio_SysCall, |
| 1486 | "listio(LC_START, &req, 1) LO_READ, fd:%d, nbyte:%d", |
| 1487 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1488 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1489 | if (Debug_level) { |
| 1490 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1491 | Lio_SysCall); |
| 1492 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1493 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1494 | sigoff(); |
| 1495 | if (listio(listio_cmd, &request, 1) == -1) { |
| 1496 | sprintf(Errormsg, |
| 1497 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 1498 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 1499 | errno, strerror(errno)); |
| 1500 | sigon(); |
| 1501 | return -errno; |
| 1502 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1503 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1504 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1505 | aiocbp.aio_lio_opcode = LIO_READ; |
| 1506 | listio_cmd = LIO_NOWAIT; |
| 1507 | io_type = "lio_listio(3) async read"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1508 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1509 | sprintf(Lio_SysCall, |
| 1510 | "lio_listio(LIO_NOWAIT, aiolist, 1, NULL) LIO_READ, fd:%d, nbyte:%d", |
| 1511 | fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1512 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1513 | if (Debug_level) { |
| 1514 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1515 | Lio_SysCall); |
| 1516 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1517 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1518 | if (sig) |
| 1519 | sighold(sig); |
| 1520 | if (lio_listio(listio_cmd, aiolist, 1, NULL) == -1) { |
| 1521 | sprintf(Errormsg, |
| 1522 | "%s/%d %s failed, fd:%d, nbyte:%d errno=%d %s", |
| 1523 | __FILE__, __LINE__, Lio_SysCall, fd, size, |
| 1524 | errno, strerror(errno)); |
| 1525 | if (sig) |
| 1526 | sigrelse(sig); |
| 1527 | return -errno; |
| 1528 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1529 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1530 | } |
| 1531 | /* LIO_IO_ALISTIO */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1532 | #ifndef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1533 | else if (method & LIO_IO_SYNCV) { |
| 1534 | io_type = "readv(2)"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1535 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1536 | sprintf(Lio_SysCall, "readv(%d, &iov, 1) nbyte:%d", fd, size); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1537 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1538 | if (Debug_level) { |
| 1539 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1540 | Lio_SysCall); |
| 1541 | } |
| 1542 | if ((ret = readv(fd, &iov, 1)) == -1) { |
| 1543 | sprintf(Errormsg, |
| 1544 | "%s/%d readv(%d, iov, 1) nbyte:%d ret:-1, errno=%d %s", |
| 1545 | __FILE__, __LINE__, fd, size, errno, |
| 1546 | strerror(errno)); |
| 1547 | return -errno; |
| 1548 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1549 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1550 | if (ret != size) { |
| 1551 | sprintf(Errormsg, |
| 1552 | "%s/%d readv(%d, iov, 1) nbyte:%d returned=%d", |
| 1553 | __FILE__, __LINE__, fd, size, ret); |
| 1554 | } else if (Debug_level > 1) |
| 1555 | printf |
| 1556 | ("DEBUG %s/%d: readv completed without error (ret %d)\n", |
| 1557 | __FILE__, __LINE__, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1558 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1559 | return ret; |
| 1560 | } /* LIO_IO_SYNCV */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1561 | #endif |
| 1562 | |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1563 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1564 | else if (method & LIO_IO_SYNCP) { |
| 1565 | io_type = "pread(2)"; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1566 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1567 | sprintf(Lio_SysCall, |
| 1568 | "pread(%d, buf, %d, %lld)", fd, size, |
| 1569 | (long long)poffset); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1570 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1571 | if (Debug_level) { |
| 1572 | printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, |
| 1573 | Lio_SysCall); |
| 1574 | } |
| 1575 | if ((ret = pread(fd, buffer, size, poffset)) == -1) { |
| 1576 | sprintf(Errormsg, |
| 1577 | "%s/%d pread(%d, buf, %d, %lld) ret:-1, errno=%d %s", |
| 1578 | __FILE__, __LINE__, fd, size, |
| 1579 | (long long)poffset, errno, strerror(errno)); |
| 1580 | return -errno; |
| 1581 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1582 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1583 | if (ret != size) { |
| 1584 | sprintf(Errormsg, |
| 1585 | "%s/%d pread(%d, buf, %d, %lld) returned=%d", |
| 1586 | __FILE__, __LINE__, |
| 1587 | fd, size, (long long)poffset, ret); |
| 1588 | } else if (Debug_level > 1) |
| 1589 | printf |
| 1590 | ("DEBUG %s/%d: pread completed without error (ret %d)\n", |
| 1591 | __FILE__, __LINE__, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1592 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1593 | return ret; |
| 1594 | } /* LIO_IO_SYNCP */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1595 | #endif |
| 1596 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1597 | else { |
| 1598 | printf("DEBUG %s/%d: No I/O method chosen\n", __FILE__, |
| 1599 | __LINE__); |
| 1600 | return -1; |
| 1601 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1602 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1603 | /* |
| 1604 | * wait for async io to complete. |
| 1605 | * Note: Sync io should have returned prior to getting here. |
| 1606 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1607 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1608 | ret = lio_wait4asyncio(method, fd, statptr); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1609 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1610 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1611 | ret = lio_wait4asyncio(method, fd, &aiocbp); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1612 | #endif |
| 1613 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1614 | /* |
| 1615 | * If there was an error waiting for async i/o to complete, |
| 1616 | * return the error value (errno) to the caller. |
| 1617 | * Note: Errormsg should already have been updated. |
| 1618 | */ |
| 1619 | if (ret < 0) { |
| 1620 | return ret; |
| 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * If i/o was not waited for (may not have been completed at this time), |
| 1625 | * return the size that was requested. |
| 1626 | */ |
| 1627 | if (ret == 1) |
| 1628 | return size; |
| 1629 | |
| 1630 | /* |
| 1631 | * check that async io was successful. |
| 1632 | * Note: if the there was an system call failure, -errno |
| 1633 | * was returned and Errormsg should already have been updated. |
| 1634 | * If amount i/o was different than size, Errormsg should already |
| 1635 | * have been updated but the actual i/o size if returned. |
| 1636 | */ |
| 1637 | |
| 1638 | #ifdef CRAY |
| 1639 | ret = lio_check_asyncio(io_type, size, &status); |
| 1640 | #endif |
| 1641 | #if defined(sgi) || (defined(__linux__) && !defined(__UCLIBC__)) |
| 1642 | ret = lio_check_asyncio(io_type, size, &aiocbp, method); |
| 1643 | #endif |
| 1644 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1645 | return ret; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1646 | } /* end of lio_read_buffer */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1647 | |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1648 | #if !defined(__sun) && !defined(__hpux) && !defined(_AIX) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1649 | /*********************************************************************** |
| 1650 | * This function will check that async io was successful. |
| 1651 | * It can also be used to check sync listio since it uses the |
| 1652 | * same method. |
| 1653 | * |
| 1654 | * Return Values |
| 1655 | * If status.sw_error is set, -status.sw_error is returned. |
| 1656 | * Otherwise sw_count's field value is returned. |
| 1657 | * |
| 1658 | * (rrl 04/96) |
| 1659 | ***********************************************************************/ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1660 | #ifdef CRAY |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1661 | int lio_check_asyncio(char *io_type, int size, struct iosw *status) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1662 | #elif defined(sgi) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1663 | int lio_check_asyncio(char *io_type, int size, aiocb_t * aiocbp, int method) |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1664 | #elif defined(__linux__) && !defined(__UCLIBC__) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1665 | int lio_check_asyncio(char *io_type, int size, struct aiocb *aiocbp, int method) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1666 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1667 | int ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1668 | |
| 1669 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1670 | if (status->sw_error) { |
| 1671 | sprintf(Errormsg, |
| 1672 | "%s/%d %s, sw_error set = %d %s, sw_count = %d", |
| 1673 | __FILE__, __LINE__, io_type, |
| 1674 | status->sw_error, strerror(status->sw_error), |
| 1675 | status->sw_count); |
| 1676 | return -status->sw_error; |
| 1677 | } else if (status->sw_count != size) { |
| 1678 | sprintf(Errormsg, |
| 1679 | "%s/%d %s, sw_count not as expected(%d), but actual:%d", |
| 1680 | __FILE__, __LINE__, io_type, size, status->sw_count); |
| 1681 | } else if (Debug_level > 1) { |
| 1682 | printf |
| 1683 | ("DEBUG %s/%d: %s completed without error (sw_error == 0, sw_count == %d)\n", |
| 1684 | __FILE__, __LINE__, io_type, status->sw_count); |
| 1685 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1686 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1687 | return status->sw_count; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1688 | |
| 1689 | #else |
| 1690 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1691 | int cnt = 1; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1692 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1693 | /* The I/O may have been synchronous with signal completion. It doesn't |
| 1694 | * make sense, but the combination could be generated. Release the |
| 1695 | * completion signal here otherwise it'll hang around and bite us |
| 1696 | * later. |
| 1697 | */ |
| 1698 | if (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL) |
| 1699 | sigrelse(aiocbp->aio_sigevent.sigev_signo); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1700 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1701 | ret = aio_error(aiocbp); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1702 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1703 | while (ret == EINPROGRESS) { |
| 1704 | ret = aio_error(aiocbp); |
| 1705 | ++cnt; |
| 1706 | } |
| 1707 | if (cnt > 1) { |
| 1708 | sprintf(Errormsg, |
| 1709 | "%s/%d %s, aio_error had to loop on EINPROGRESS, cnt=%d; random method %#o; sigev_notify=%s", |
| 1710 | __FILE__, __LINE__, io_type, cnt, method, |
| 1711 | (aiocbp->aio_sigevent.sigev_notify == |
| 1712 | SIGEV_SIGNAL ? "signal" : aiocbp->aio_sigevent. |
| 1713 | sigev_notify == SIGEV_NONE ? "none" : |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 1714 | #ifdef SIGEV_CALLBACK |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1715 | aiocbp->aio_sigevent.sigev_notify == |
| 1716 | SIGEV_CALLBACK ? "callback" : |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 1717 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1718 | aiocbp->aio_sigevent.sigev_notify == |
| 1719 | SIGEV_THREAD ? "thread" : "unknown")); |
| 1720 | return -ret; |
| 1721 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1722 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1723 | if (ret != 0) { |
| 1724 | sprintf(Errormsg, |
| 1725 | "%s/%d %s, aio_error = %d %s; random method %#o", |
| 1726 | __FILE__, __LINE__, io_type, |
| 1727 | ret, strerror(ret), method); |
| 1728 | return -ret; |
| 1729 | } |
| 1730 | ret = aio_return(aiocbp); |
| 1731 | if (ret != size) { |
| 1732 | sprintf(Errormsg, |
| 1733 | "%s/%d %s, aio_return not as expected(%d), but actual:%d", |
| 1734 | __FILE__, __LINE__, io_type, size, ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1735 | |
| 1736 | #ifdef BUG1_workaround |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1737 | if (ret == 0) { |
| 1738 | ret = size; |
| 1739 | if (Debug_level > 1) { |
| 1740 | printf |
| 1741 | ("WARN %s/%d: %s completed with bug1_workaround (aio_error == 0, aio_return now == %d)\n", |
| 1742 | __FILE__, __LINE__, io_type, ret); |
| 1743 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1744 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1745 | #endif /* BUG1_workaround */ |
| 1746 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1747 | } else if (Debug_level > 1) { |
| 1748 | printf |
| 1749 | ("DEBUG %s/%d: %s completed without error (aio_error == 0, aio_return == %d)\n", |
| 1750 | __FILE__, __LINE__, io_type, ret); |
| 1751 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1752 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1753 | return ret; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1754 | |
| 1755 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1756 | } /* end of lio_check_asyncio */ |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1757 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1758 | |
| 1759 | /*********************************************************************** |
| 1760 | * |
| 1761 | * This function will wait for async io to complete. |
| 1762 | * If multiple wait methods are specified, the order is predetermined |
| 1763 | * to LIO_WAIT_RECALL, |
| 1764 | * LIO_WAIT_ACTIVE, LIO_WAIT_SIGPAUSE, LIO_WAIT_SIGACTIVE, |
| 1765 | * then LIO_WAIT_NONE. |
| 1766 | * |
| 1767 | * If no wait method was specified the default wait method is: recall(2) |
| 1768 | * or aio_suspend(3), as appropriate. |
| 1769 | * |
| 1770 | * Return Values |
| 1771 | * <0: errno of failed recall |
| 1772 | * 0 : async io was completed |
| 1773 | * 1 : async was not waited for, io may not have completed. |
| 1774 | * |
| 1775 | * (rrl 04/96) |
| 1776 | ***********************************************************************/ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1777 | #ifdef CRAY |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1778 | int lio_wait4asyncio(int method, int fd, struct iosw **statptr) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1779 | #elif defined(sgi) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1780 | int lio_wait4asyncio(int method, int fd, aiocb_t * aiocbp) |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1781 | #elif defined(__linux__) && !defined(__UCLIBC__) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1782 | int lio_wait4asyncio(int method, int fd, struct aiocb *aiocbp) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1783 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1784 | int cnt; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1785 | #ifdef sgi |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1786 | int ret; |
| 1787 | const aiocb_t *aioary[1]; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1788 | #endif |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1789 | #if defined(__linux__)&& !defined(__UCLIBC__) |
subrata_modak | ea37be8 | 2008-12-11 13:17:49 +0000 | [diff] [blame] | 1790 | int ret; |
| 1791 | const struct aiocb *aioary[1]; |
| 1792 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1793 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1794 | if ((method & LIO_WAIT_RECALL) |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1795 | #if defined(sgi) || (defined(__linux__)&& !defined(__UCLIBC__)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1796 | || (method & LIO_WAIT_CBSUSPEND) |
| 1797 | || (method & LIO_WAIT_SIGSUSPEND) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1798 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1799 | || ((method & LIO_WAIT_TYPES) == 0)) { |
| 1800 | /* |
| 1801 | * If method has LIO_WAIT_RECALL bit set or method does |
| 1802 | * not have any wait method bits set (default), use recall/aio_suspend. |
| 1803 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1804 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1805 | if (Debug_level > 2) |
| 1806 | printf("DEBUG %s/%d: wait method : recall\n", __FILE__, |
| 1807 | __LINE__); |
| 1808 | sigon(); |
| 1809 | if (recall(fd, 1, statptr)) { |
| 1810 | sprintf(Errormsg, |
| 1811 | "%s/%d recall(%d, 1, stat) failed, errno:%d %s", |
| 1812 | __FILE__, __LINE__, fd, errno, strerror(errno)); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1813 | return -errno; |
| 1814 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1815 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1816 | if (Debug_level > 2) |
| 1817 | printf |
| 1818 | ("DEBUG %s/%d: wait method : aio_suspend, sigev_notify=%s\n", |
| 1819 | __FILE__, __LINE__, |
| 1820 | (aiocbp->aio_sigevent.sigev_notify == |
| 1821 | SIGEV_SIGNAL ? "signal" : aiocbp->aio_sigevent. |
| 1822 | sigev_notify == SIGEV_NONE ? "none" : |
| 1823 | #ifdef SIGEV_CALLBACK |
| 1824 | aiocbp->aio_sigevent.sigev_notify == |
| 1825 | SIGEV_CALLBACK ? "callback" : |
| 1826 | #endif |
| 1827 | aiocbp->aio_sigevent.sigev_notify == |
| 1828 | SIGEV_THREAD ? "thread" : "unknown")); |
| 1829 | |
| 1830 | aioary[0] = aiocbp; |
| 1831 | ret = aio_suspend(aioary, 1, NULL); |
| 1832 | if ((ret == -1) && (errno == EINTR)) { |
| 1833 | if (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL) { |
| 1834 | if (Debug_level > 2) { |
| 1835 | printf |
| 1836 | ("DEBUG %s/%d: aio_suspend received EINTR, sigev_notify=SIGEV_SIGNAL -- ok\n", |
| 1837 | __FILE__, __LINE__); |
| 1838 | } |
| 1839 | } else { |
| 1840 | sprintf(Errormsg, |
| 1841 | "%s/%d aio_suspend received EINTR, sigev_notify=%s, not ok\n", |
| 1842 | __FILE__, __LINE__, |
| 1843 | (aiocbp->aio_sigevent.sigev_notify == |
| 1844 | SIGEV_SIGNAL ? "signal" : aiocbp-> |
| 1845 | aio_sigevent.sigev_notify == |
| 1846 | SIGEV_NONE ? "none" : |
| 1847 | #ifdef SIGEV_CALLBACK |
| 1848 | aiocbp->aio_sigevent.sigev_notify == |
| 1849 | SIGEV_CALLBACK ? "callback" : |
| 1850 | #endif |
| 1851 | aiocbp->aio_sigevent.sigev_notify == |
| 1852 | SIGEV_THREAD ? "thread" : "unknown")); |
| 1853 | return -errno; |
| 1854 | } |
| 1855 | } else if (ret) { |
| 1856 | sprintf(Errormsg, |
| 1857 | "%s/%d aio_suspend(fildes=%d, aioary, 1, NULL) failed, errno:%d %s", |
| 1858 | __FILE__, __LINE__, fd, errno, strerror(errno)); |
| 1859 | return -errno; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1860 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1861 | #endif |
| 1862 | |
| 1863 | } else if (method & LIO_WAIT_ACTIVE) { |
| 1864 | if (Debug_level > 2) |
| 1865 | printf("DEBUG %s/%d: wait method : active\n", __FILE__, |
| 1866 | __LINE__); |
| 1867 | #ifdef CRAY |
| 1868 | sigon(); |
| 1869 | /* |
| 1870 | * loop until sw_flag, sw_count or sw_error field elements |
| 1871 | * change to non-zero. |
| 1872 | */ |
| 1873 | cnt = 0; |
| 1874 | while ((*statptr)->sw_flag == 0 && |
| 1875 | (*statptr)->sw_count == 0 && (*statptr)->sw_error == 0) { |
| 1876 | cnt++; |
| 1877 | } |
| 1878 | #else |
| 1879 | /* loop while aio_error() returns EINPROGRESS */ |
| 1880 | cnt = 0; |
| 1881 | while (1) { |
| 1882 | ret = aio_error(aiocbp); |
| 1883 | if ((ret == 0) || (ret != EINPROGRESS)) { |
| 1884 | break; |
| 1885 | } |
| 1886 | ++cnt; |
| 1887 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1888 | |
| 1889 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1890 | if (Debug_level > 5 && cnt && (cnt % 50) == 0) |
| 1891 | printf("DEBUG %s/%d: wait active cnt = %d\n", |
| 1892 | __FILE__, __LINE__, cnt); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1893 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1894 | } else if (method & LIO_WAIT_SIGPAUSE) { |
| 1895 | if (Debug_level > 2) |
| 1896 | printf("DEBUG %s/%d: wait method : sigpause\n", |
| 1897 | __FILE__, __LINE__); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1898 | #ifdef sgi |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1899 | /* note: don't do the sigon() for CRAY in this case. why? -- roehrich 6/11/97 */ |
| 1900 | if (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL) |
| 1901 | sigrelse(aiocbp->aio_sigevent.sigev_signo); |
| 1902 | else { |
| 1903 | printf("DEBUG %s/%d: sigev_notify != SIGEV_SIGNAL\n", |
| 1904 | __FILE__, __LINE__); |
| 1905 | return -1; |
| 1906 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1907 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1908 | pause(); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1909 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1910 | } else if (method & LIO_WAIT_SIGACTIVE) { |
| 1911 | if (Debug_level > 2) |
| 1912 | printf("DEBUG %s/%d: wait method : sigactive\n", |
| 1913 | __FILE__, __LINE__); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1914 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1915 | sigon(); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1916 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1917 | if (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL) |
| 1918 | sigrelse(aiocbp->aio_sigevent.sigev_signo); |
| 1919 | else { |
| 1920 | printf("DEBUG %s/%d: sigev_notify != SIGEV_SIGNAL\n", |
| 1921 | __FILE__, __LINE__); |
| 1922 | return -1; |
| 1923 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1924 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1925 | /* loop waiting for signal */ |
| 1926 | while (Received_signal == Rec_signal) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1927 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1928 | sigon(); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1929 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1930 | sigrelse(aiocbp->aio_sigevent.sigev_signo); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1931 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1932 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1933 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1934 | } else if (method & LIO_WAIT_NONE) { |
| 1935 | if (Debug_level > 2) |
| 1936 | printf("DEBUG %s/%d: wait method : none\n", __FILE__, |
| 1937 | __LINE__); |
| 1938 | /* It's broken because the aiocb/iosw is an automatic variable in |
| 1939 | * lio_{read,write}_buffer, so when the function returns and the |
| 1940 | * I/O completes there will be nowhere to write the I/O status. |
| 1941 | * It doesn't cause a problem on unicos--probably because of some |
| 1942 | * compiler quirk, or an accident. It causes POSIX async I/O |
| 1943 | * to core dump some threads. spr/pv 705909. 6/27/97 roehrich |
| 1944 | */ |
| 1945 | sprintf(Errormsg, |
| 1946 | "%s/%d LIO_WAIT_NONE was selected (this is broken)\n", |
| 1947 | __FILE__, __LINE__); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1948 | #ifdef CRAY |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1949 | sigon(); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1950 | #endif |
| 1951 | /* return 1;*/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1952 | return -1; |
| 1953 | } else { |
| 1954 | if (Debug_level > 2) |
| 1955 | printf("DEBUG %s/%d: no wait method was chosen\n", |
| 1956 | __FILE__, __LINE__); |
| 1957 | return -1; |
| 1958 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1959 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1960 | return 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1961 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1962 | } /* end of lio_wait4asyncio */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1963 | |
| 1964 | #endif /* ifndef linux */ |
subrata_modak | 26e07d5 | 2009-02-26 06:33:39 +0000 | [diff] [blame] | 1965 | #endif |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1966 | |
| 1967 | #if UNIT_TEST |
| 1968 | /*********************************************************************** |
| 1969 | * The following code is provided as unit test. |
| 1970 | * Just define add "-DUNIT_TEST=1" to the cc line. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 1971 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1972 | * (rrl 04/96) |
| 1973 | ***********************************************************************/ |
| 1974 | struct unit_info_t { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1975 | int method; |
| 1976 | int sig; |
| 1977 | char *str; |
| 1978 | } Unit_info[] = { |
| 1979 | { |
| 1980 | LIO_IO_SYNC, 0, "sync io"}, { |
| 1981 | LIO_IO_SYNCV, 0, "sync readv/writev"}, { |
| 1982 | LIO_IO_SYNCP, 0, "sync pread/pwrite"}, { |
| 1983 | LIO_IO_ASYNC, 0, "async io, def wait"}, { |
| 1984 | LIO_IO_SLISTIO, 0, "sync listio"}, { |
| 1985 | LIO_IO_ALISTIO, 0, "async listio, def wait"}, { |
| 1986 | LIO_IO_ASYNC | LIO_WAIT_ACTIVE, 0, "async active"}, { |
| 1987 | LIO_IO_ASYNC | LIO_WAIT_RECALL, 0, "async recall/suspend"}, { |
| 1988 | LIO_IO_ASYNC | LIO_WAIT_SIGPAUSE, SIGUSR1, "async sigpause"}, { |
| 1989 | LIO_IO_ASYNC | LIO_WAIT_SIGACTIVE, SIGUSR1, "async sigactive"}, { |
| 1990 | LIO_IO_ALISTIO | LIO_WAIT_ACTIVE, 0, "async listio active"}, { |
| 1991 | LIO_IO_ALISTIO | LIO_WAIT_RECALL, 0, "async listio recall"}, { |
| 1992 | LIO_IO_ALISTIO | LIO_WAIT_SIGACTIVE, SIGUSR1, "async listio sigactive"}, |
| 1993 | { |
| 1994 | LIO_IO_ALISTIO | LIO_WAIT_SIGPAUSE, SIGUSR1, "async listio sigpause"}, |
| 1995 | { |
| 1996 | LIO_IO_ASYNC, SIGUSR2, "async io, def wait, sigusr2"}, { |
| 1997 | LIO_IO_ALISTIO, SIGUSR2, "async listio, def wait, sigusr2"},}; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1998 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1999 | int main(argc, argv) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2000 | int argc; |
| 2001 | char **argv; |
| 2002 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2003 | extern char *optarg; |
| 2004 | extern int optind; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2005 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2006 | int fd; |
| 2007 | char *err; |
| 2008 | char buffer[4096]; |
| 2009 | int size = 4096; |
| 2010 | int ret; |
| 2011 | int ind; |
| 2012 | int iter = 3; |
| 2013 | int method; |
| 2014 | int exit_status = 0; |
| 2015 | int c; |
| 2016 | int i; |
| 2017 | char *symbols = NULL; |
| 2018 | int die_on_err = 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2019 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2020 | while ((c = getopt(argc, argv, "s:di:")) != -1) { |
| 2021 | switch (c) { |
| 2022 | case 's': |
| 2023 | symbols = optarg; |
| 2024 | break; |
| 2025 | case 'd': |
| 2026 | ++die_on_err; |
| 2027 | break; |
| 2028 | case 'i': |
| 2029 | iter = atoi(optarg); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2034 | if ((fd = |
| 2035 | open("unit_test_file", O_CREAT | O_RDWR | O_TRUNC, 0777)) == -1) { |
| 2036 | perror |
| 2037 | ("open(unit_test_file, O_CREAT|O_RDWR|O_TRUNC, 0777) failed"); |
| 2038 | exit(1); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2041 | Debug_level = 9; |
| 2042 | |
| 2043 | if (symbols != NULL) { |
| 2044 | if ((method = lio_parse_io_arg2(symbols, &err)) == -1) { |
| 2045 | printf |
| 2046 | ("lio_parse_io_arg2(%s, &err) failed, bad token starting at %s\n", |
| 2047 | symbols, err); |
| 2048 | if (die_on_err) |
| 2049 | exit(1); |
| 2050 | } else |
| 2051 | printf("lio_parse_io_arg2(%s, &err) returned %#o\n", |
| 2052 | symbols, method); |
| 2053 | |
| 2054 | exit_status = 0; |
| 2055 | for (ind = 0; ind < iter; ind++) { |
| 2056 | memset(buffer, 'A', 4096); |
| 2057 | if (lseek(fd, 0, 0) == -1) { |
| 2058 | printf("lseek(fd,0,0), %d, failed, errno %d\n", |
| 2059 | __LINE__, errno); |
| 2060 | ++exit_status; |
| 2061 | } |
| 2062 | if ((ret = lio_write_buffer(fd, method, buffer, |
| 2063 | size, SIGUSR1, &err, |
| 2064 | 0)) != size) { |
| 2065 | printf |
| 2066 | ("lio_write_buffer returned -1, err = %s\n", |
| 2067 | err); |
| 2068 | } else |
| 2069 | printf("lio_write_buffer returned %d\n", ret); |
| 2070 | |
| 2071 | memset(buffer, 'B', 4096); |
| 2072 | if (lseek(fd, 0, 0) == -1) { |
| 2073 | printf("lseek(fd,0,0), %d, failed, errno %d\n", |
| 2074 | __LINE__, errno); |
| 2075 | ++exit_status; |
| 2076 | } |
| 2077 | if ((ret = lio_read_buffer(fd, method, buffer, |
| 2078 | size, SIGUSR2, &err, |
| 2079 | 0)) != size) { |
| 2080 | printf |
| 2081 | ("lio_read_buffer returned -1, err = %s\n", |
| 2082 | err); |
| 2083 | } else |
| 2084 | printf("lio_read_buffer returned %d\n", ret); |
| 2085 | |
| 2086 | for (i = 0; i < 4096; ++i) { |
| 2087 | if (buffer[i] != 'A') { |
| 2088 | printf(" buffer[%d] = %d\n", i, |
| 2089 | buffer[i]); |
| 2090 | ++exit_status; |
| 2091 | break; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | if (exit_status) |
| 2096 | exit(exit_status); |
| 2097 | |
| 2098 | } |
| 2099 | |
| 2100 | unlink("unit_test_file"); |
| 2101 | exit(0); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2104 | for (ind = 0; ind < sizeof(Unit_info) / sizeof(struct unit_info_t); |
| 2105 | ind++) { |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2106 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2107 | printf("\n********* write %s ***************\n", |
| 2108 | Unit_info[ind].str); |
| 2109 | if (lseek(fd, 0, 0) == -1) { |
| 2110 | printf("lseek(fd,0,0), %d, failed, errno %d\n", |
| 2111 | __LINE__, errno); |
| 2112 | ++exit_status; |
| 2113 | } |
| 2114 | |
| 2115 | memset(buffer, 'A', 4096); |
| 2116 | if ((ret = lio_write_buffer(fd, Unit_info[ind].method, buffer, |
| 2117 | size, Unit_info[ind].sig, &err, |
| 2118 | 0)) != size) { |
| 2119 | printf |
| 2120 | (">>>>> lio_write_buffer(fd,0%x,buffer,%d,%d,err,0) returned -1,\n err = %s\n", |
| 2121 | Unit_info[ind].method, size, Unit_info[ind].sig, |
| 2122 | err); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2123 | ++exit_status; |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 2124 | if (die_on_err) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2125 | exit(exit_status); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2126 | } else { |
| 2127 | printf("lio_write_buffer returned %d\n", ret); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2128 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2129 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2130 | printf("\n********* read %s ***************\n", |
| 2131 | Unit_info[ind].str); |
| 2132 | if (lseek(fd, 0, 0) == -1) { |
| 2133 | printf("lseek(fd,0,0), %d, failed, errno %d\n", |
| 2134 | __LINE__, errno); |
| 2135 | ++exit_status; |
| 2136 | } |
| 2137 | memset(buffer, 'B', 4096); |
| 2138 | if ((ret = lio_read_buffer(fd, Unit_info[ind].method, buffer, |
| 2139 | size, Unit_info[ind].sig, &err, |
| 2140 | 0)) != size) { |
| 2141 | printf |
| 2142 | (">>>>> lio_read_buffer(fd,0%x,buffer,%d,%d,err,0) returned -1,\n err = %s\n", |
| 2143 | Unit_info[ind].method, size, Unit_info[ind].sig, |
| 2144 | err); |
| 2145 | ++exit_status; |
| 2146 | if (die_on_err) |
| 2147 | exit(exit_status); |
| 2148 | } else { |
| 2149 | printf("lio_read_buffer returned %d\n", ret); |
| 2150 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2151 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2152 | for (i = 0; i < 4096; ++i) { |
| 2153 | if (buffer[i] != 'A') { |
| 2154 | printf(" buffer[%d] = %d\n", i, buffer[i]); |
| 2155 | ++exit_status; |
| 2156 | if (die_on_err) |
| 2157 | exit(exit_status); |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2161 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2162 | fflush(stdout); |
| 2163 | fflush(stderr); |
| 2164 | sleep(1); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2165 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | unlink("unit_test_file"); |
| 2169 | |
| 2170 | exit(exit_status); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 2171 | } |
Garrett Cooper | 9ef7f2d | 2011-04-08 22:44:06 -0700 | [diff] [blame] | 2172 | #endif |