| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <sys/param.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <sys/file.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <sys/time.h> |
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/wait.h> |
| 37 |
#include <sys/errno.h> |
| 38 |
#include <fcntl.h> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#define TOP_DIRS 2 |
| 60 |
#define BOT_DIRS 3 |
| 61 |
#define FILES_PER_DIR 1 |
| 62 |
#define FILE_CREATES 83 |
| 63 |
#define NREAD_LINK 583 |
| 64 |
#define BLOCKS_PER_FILE 250 |
| 65 |
#define FILE_LOOKUPS 4167 |
| 66 |
#define BYTES_PER_BLOCK 8192 |
| 67 |
#define TOT_DIRS TOP_DIRS * BOT_DIRS |
| 68 |
#define TOT_FILES TOT_DIRS * FILES_PER_DIR |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define TOT_FILEOPS TOT_FILES * 11 * BLOCKS_PER_FILE + \ |
| 85 |
(FILE_LOOKUPS + FILE_CREATES + NREAD_LINK) * TOT_FILES |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#define TOTAL_OPS TOT_DIRS * 2 + TOT_FILES * 2 + TOT_FILEOPS |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
int buf[BYTES_PER_BLOCK/sizeof(int)]; |
| 98 |
main(argc, argv) |
| 99 |
int argc; |
| 100 |
char *argv[]; |
| 101 |
{ |
| 102 |
char *path; |
| 103 |
char *lock; |
| 104 |
int lock_fd; |
| 105 |
struct timeval start_time, end_time; |
| 106 |
struct timezone dummy; |
| 107 |
int i, j, num = 0, pid, wait(); |
| 108 |
char s[BUFSIZ], t[BUFSIZ], u[BUFSIZ]; |
| 109 |
double total_time; |
| 110 |
union wait status; |
| 111 |
int error = 0; |
| 112 |
struct stat stat_buf; |
| 113 |
|
| 114 |
bzero((char *)buf, BYTES_PER_BLOCK); |
| 115 |
if (argc != 3) { |
| 116 |
(void)fprintf(stderr, "Usage: %s path_prefix lock_file_path\n", |
| 117 |
argv[0]); |
| 118 |
exit(-1); |
| 119 |
} |
| 120 |
|
| 121 |
path = argv[1]; |
| 122 |
lock = argv[2]; |
| 123 |
|
| 124 |
if (stat(path, &stat_buf) != 0) { |
| 125 |
perror("path_prefix"); |
| 126 |
exit(-1); |
| 127 |
} |
| 128 |
|
| 129 |
if ((stat_buf.st_mode & S_IFMT) != S_IFDIR || |
| 130 |
access(path, W_OK|X_OK) != 0) { |
| 131 |
(void)fprintf(stderr, |
| 132 |
"Path_prefix is not a directory with write permission.\n"); |
| 133 |
exit(-1); |
| 134 |
} |
| 135 |
|
| 136 |
if ((lock_fd = open(lock, O_RDWR)) < 0) { |
| 137 |
perror("lock_file_path"); |
| 138 |
exit(-1); |
| 139 |
} |
| 140 |
|
| 141 |
if (chdir(path) != 0) { |
| 142 |
perror("path_prefix"); |
| 143 |
exit(-1); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (lockf(lock_fd, F_LOCK, 0) != 0) { |
| 151 |
perror("lockf"); |
| 152 |
exit(-1); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if (lockf(lock_fd, F_ULOCK, 0) != 0) { |
| 160 |
perror("lockf"); |
| 161 |
exit(-1); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (gettimeofday(&start_time, &dummy) == -1) { |
| 168 |
perror("gettimeofday"); |
| 169 |
exit(-1); |
| 170 |
} |
| 171 |
|
| 172 |
for (i = 0; i < TOP_DIRS; i++) { |
| 173 |
(void)sprintf(u, "%8d", ++num); |
| 174 |
if (mkdir(u, 0777) != 0) { |
| 175 |
perror("mkdir"); |
| 176 |
exit(-1); |
| 177 |
} |
| 178 |
for (j = 0; j < BOT_DIRS; j++) { |
| 179 |
(void)sprintf(t, "%8d", ++num); |
| 180 |
(void)strcpy(s, u); |
| 181 |
(void)strcat(s, "/"); |
| 182 |
(void)strcat(s, t); |
| 183 |
if (mkdir(s, 0777) != 0) { |
| 184 |
perror("mkdir: subdir"); |
| 185 |
exit(-1); |
| 186 |
} |
| 187 |
if ((pid = fork()) == 0) |
| 188 |
do_child(s); |
| 189 |
else if (pid == -1) { |
| 190 |
perror("fork"); |
| 191 |
exit(-1); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
while(wait(&status) != -1) |
| 201 |
if (status.w_status != 0) |
| 202 |
error = 1; |
| 203 |
|
| 204 |
num = 0; |
| 205 |
for (i = 0; i < TOP_DIRS; i++) { |
| 206 |
(void)sprintf(u, "%8d", ++num); |
| 207 |
for (j = 0; j < BOT_DIRS; j++) { |
| 208 |
(void)sprintf(t, "%8d", ++num); |
| 209 |
(void)strcpy(s, u); |
| 210 |
(void)strcat(s, "/"); |
| 211 |
(void)strcat(s, t); |
| 212 |
if (rmdir(s) != 0) { |
| 213 |
perror("rmdir: subdir"); |
| 214 |
exit(-1); |
| 215 |
} |
| 216 |
} |
| 217 |
if (rmdir(u) != 0) { |
| 218 |
perror("rmdir"); |
| 219 |
exit(-1); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (error) |
| 224 |
exit(-1); |
| 225 |
|
| 226 |
if (gettimeofday(&end_time, &dummy) == -1) { |
| 227 |
perror("gettimeofday"); |
| 228 |
exit(-1); |
| 229 |
} |
| 230 |
|
| 231 |
total_time = (double)(end_time.tv_sec - start_time.tv_sec) + |
| 232 |
((double)(end_time.tv_usec - start_time.tv_usec)/1000000.0); |
| 233 |
|
| 234 |
(void)printf("Total NFSstones: %d\n", TOTAL_OPS); |
| 235 |
(void)printf("Total time = %f seconds, or %f NFSstones/second\n", total_time, |
| 236 |
((double) TOTAL_OPS)/total_time); |
| 237 |
exit(0); |
| 238 |
} |
| 239 |
|
| 240 |
do_child(dir) |
| 241 |
char *dir; |
| 242 |
{ |
| 243 |
int i, fd, cfd; |
| 244 |
long j; |
| 245 |
char s[BUFSIZ]; |
| 246 |
char b[BUFSIZ]; |
| 247 |
extern int errno; |
| 248 |
|
| 249 |
if (chdir(dir) != 0) { |
| 250 |
perror("Subdirectory"); |
| 251 |
exit(-1); |
| 252 |
} |
| 253 |
for (i = 0; i < FILES_PER_DIR; i++) { |
| 254 |
(void)sprintf(s, "%8d", i); |
| 255 |
if ((fd = open(s, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) { |
| 256 |
perror("open"); |
| 257 |
exit(-1); |
| 258 |
} |
| 259 |
|
| 260 |
seq_write(fd); |
| 261 |
seq_read(fd); |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if (symlink("/aaa/bbb/ccc/ddd/eee/fff", "Test_link") != 0) { |
| 268 |
perror("symlink"); |
| 269 |
if (errno != EEXIST) |
| 270 |
exit(-1); |
| 271 |
} |
| 272 |
|
| 273 |
for (j = 0; j < NREAD_LINK; j++) { |
| 274 |
if (readlink("Test_link", b, BUFSIZ) == -1) { |
| 275 |
perror("readlink"); |
| 276 |
exit(-1); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
nseq_read(fd); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
(void)unlink("Test_link"); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
for (j = 0; j < FILE_CREATES; j++) { |
| 293 |
if ((cfd = open("test_create", O_RDWR|O_CREAT|O_TRUNC, |
| 294 |
0666)) < 0) { |
| 295 |
perror("open"); |
| 296 |
exit(-1); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
(void)fsync(cfd); |
| 302 |
(void)close(cfd); |
| 303 |
} |
| 304 |
|
| 305 |
seq_read(fd); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
(void)unlink("test_create"); |
| 312 |
seq_read(fd); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
for (j = 0; j < FILE_LOOKUPS; j++) { |
| 318 |
char sb[BUFSIZ]; |
| 319 |
int pp = getpid(); |
| 320 |
|
| 321 |
(void)sprintf(sb, "xx%d%d%d", j,pp,i); |
| 322 |
(void)access(sb, F_OK); |
| 323 |
} |
| 324 |
|
| 325 |
seq_read(fd); |
| 326 |
nseq_read(fd); |
| 327 |
seq_read(fd); |
| 328 |
seq_read(fd); |
| 329 |
seq_read(fd); |
| 330 |
nseq_read(fd); |
| 331 |
|
| 332 |
(void)close(fd); |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if (rename(s, "Test_of_rename") != 0) { |
| 338 |
perror("rename"); |
| 339 |
exit(-1); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
if (unlink("Test_of_rename") != 0) { |
| 346 |
perror("unlink"); |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
if (errno != ENOENT) |
| 355 |
exit(-1); |
| 356 |
} |
| 357 |
sync(); |
| 358 |
} |
| 359 |
|
| 360 |
exit(0); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
seq_write(fd) |
| 367 |
int fd; |
| 368 |
{ |
| 369 |
long j; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
if (lseek(fd, 0L, L_SET) == -1) { |
| 376 |
perror("lseek"); |
| 377 |
exit(-1); |
| 378 |
} |
| 379 |
|
| 380 |
for (j = 0; j < BLOCKS_PER_FILE; j++) { |
| 381 |
buf[0] = j; |
| 382 |
if (write(fd,(char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) { |
| 383 |
perror("write"); |
| 384 |
exit(-1); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
(void)fsync(fd); |
| 389 |
} |
| 390 |
|
| 391 |
seq_read(fd) |
| 392 |
int fd; |
| 393 |
{ |
| 394 |
long j; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
if (lseek(fd, 0L, L_SET) == -1) { |
| 400 |
perror("lseek"); |
| 401 |
exit(-1); |
| 402 |
} |
| 403 |
|
| 404 |
for (j = 0; j < BLOCKS_PER_FILE; j++) { |
| 405 |
if (read(fd, (char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) { |
| 406 |
perror("read"); |
| 407 |
exit(-1); |
| 408 |
} |
| 409 |
|
| 410 |
if (buf[0] != j) { |
| 411 |
(void)fprintf(stderr, "Data corruption error\n"); |
| 412 |
exit(-1); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
nseq_read(fd) |
| 418 |
int fd; |
| 419 |
{ |
| 420 |
long j; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
for (j = 0; j < BLOCKS_PER_FILE/2; j++) { |
| 426 |
if (lseek(fd, j * BYTES_PER_BLOCK, L_SET) == -1) { |
| 427 |
perror("lseek"); |
| 428 |
exit(-1); |
| 429 |
} |
| 430 |
|
| 431 |
if (read(fd, (char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) { |
| 432 |
perror("read"); |
| 433 |
exit(-1); |
| 434 |
} |
| 435 |
|
| 436 |
if (buf[0] != j) { |
| 437 |
(void)fprintf(stderr, "Data corruption error\n"); |
| 438 |
exit(-1); |
| 439 |
} |
| 440 |
|
| 441 |
if (lseek(fd, (BLOCKS_PER_FILE - (j + 1)) * |
| 442 |
BYTES_PER_BLOCK, L_SET) == -1) { |
| 443 |
perror("lseek"); |
| 444 |
exit(-1); |
| 445 |
} |
| 446 |
|
| 447 |
if (read(fd, (char *)buf, BYTES_PER_BLOCK) != BYTES_PER_BLOCK) { |
| 448 |
perror("read"); |
| 449 |
exit(-1); |
| 450 |
} |
| 451 |
|
| 452 |
if (buf[0] != (BLOCKS_PER_FILE - (j + 1))) { |
| 453 |
(void)fprintf(stderr, "Data corruption error\n"); |
| 454 |
exit(-1); |
| 455 |
} |
| 456 |
} |
| 457 |
} |