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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
|
/*
* HMC Drive FUSE Filesystem (FUSE.HMCDRVFS)
*
* Copyright IBM Corp. 2015, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <inttypes.h>
#include <langinfo.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "lib/util_libc.h"
#include "lib/zt_common.h"
#define HMCDRV_FUSE_LOGNAME "hmcdrvfs" /* log prefix */
#define HMCDRV_FUSE_LOGHEAD HMCDRV_FUSE_LOGNAME ": " /* log header */
#define HMCDRV_FUSE_FTPDEV "/dev/hmcdrv" /* DIAG/SCLP FTP device */
#define HMCDRV_FUSE_MAXPATH 192 /* max. path length (including EOS) */
#define HMCDRV_FUSE_MAXFTPLEN 3 /* max. length of FTP cmd (dir/nls/get) */
#define HMCDRV_FUSE_OFSPATH (HMCDRV_FUSE_MAXFTPLEN + 1) /* path in cmd */
#define HMCDRV_FUSE_MAXCMDLEN (HMCDRV_FUSE_MAXPATH + HMCDRV_FUSE_OFSPATH)
/* internal increase (margin/offset) of cache timeout with respect to FUSE
* options 'attr_timeout' and/or 'entry_timeout' (having default 1s)
*/
#define HMCDRV_FUSE_CACHE_TMOFS 30
/* size of file name/attributes cache (should be a prime number)
*/
#define HMCDRV_FUSE_CACHE_SIZE 2053
/* number of hash table lines inspected in a single garbage loop
*/
#define HMCDRV_FUSE_GARBAGE_MAX 128
/* max. size of FTP 'dir <path>' output chunk size
*/
#define HMCDRV_FUSE_DIRBUF_SIZE (64 * 1024)
/* max. string length in FTP 'dir <path>' output buffer
*/
#define HMCDRV_FUSE_DIRBUF_LEN (HMCDRV_FUSE_DIRBUF_SIZE - 1)
/* pointer to path (token) in FTP command string associated with file 'fp'
*/
#define HMCDRV_FUSE_PATH(fp) ((fp)->ftpcmd + HMCDRV_FUSE_OFSPATH)
/*
* DEBUG log macro (requires -DDEBUG, else empty)
*/
#ifdef DEBUG
#define HMCDRV_FUSE_DBGLOG(fmt, ...) syslog(LOG_ERR, (fmt), ##__VA_ARGS__)
#else
#define HMCDRV_FUSE_DBGLOG(fmt, ...) do {} while (0)
#endif /* DEBUG */
/*
* log output to 'stderr' and optionally (if from a fork'ed process/thread)
* to syslog
*/
#define HMCDRV_FUSE_LOG(sev, fmt, ...) \
hmcdrv_fuse_log((sev), HMCDRV_FUSE_LOGHEAD "%s: " fmt "\n", \
hmcdrv_fuse_logsevname[sev], ##__VA_ARGS__)
#define HMCDRV_FUSE_STR(x) #x
#define HMCDRV_FUSE_STRINGER(x) HMCDRV_FUSE_STR(x)
#define HMCDRV_FUSE_RELEASE HMCDRV_FUSE_STRINGER(S390_TOOLS_RELEASE)
#define HMCDRV_FUSE_OPT(t, p, v) { t, offsetof(struct hmcdrv_fuse_opt, p), v }
/*
* option key IDs (used on command line parsing)
*/
enum {
HMCDRV_FUSE_OPTKEY_VERSION, /* "-v", "--version" */
HMCDRV_FUSE_OPTKEY_HELP, /* "-h", "--help" */
HMCDRV_FUSE_OPTKEY_RO, /* read-only, e.g. "-o ro" or "-r" */
HMCDRV_FUSE_OPTKEY_RW, /* option associated with -wr (ignored) */
HMCDRV_FUSE_OPTKEY_NATM, /* option "noatime" */
HMCDRV_FUSE_OPTKEY_CTMO, /* cache timeout (entry/attr_timeout) */
HMCDRV_FUSE_OPTKEY_NSUP, /* unsupported options, e.g. "subdir" */
};
/*
* internal used FTP command IDs
*/
enum hmcdrv_fuse_cmdid {
HMCDRV_FUSE_CMDID_NLS = 0,
HMCDRV_FUSE_CMDID_DIR = 1,
HMCDRV_FUSE_CMDID_GET = 2,
HMCDRV_FUSE_CMDID_SIZE
};
/*
* options/parameters to lookup at start
*/
struct hmcdrv_fuse_opt {
unsigned int ro:1; /* option(s) "ro", "-r", etc. */
unsigned int noatime:1; /* option "noatime" */
char *hmctz; /* HMC timezone option "-o hmctz=TZ" */
char *hmclang; /* HMC locale option "-o hmclang=LANG" */
};
/*
* HMC drive FUSE context
*/
struct hmcdrv_fuse_context {
struct hmcdrv_fuse_opt opt; /* some saved startup options */
int fd; /* file descriptor of FTP device HMCDRV_FUSE_FTPDEV */
char *path; /* mount point path (root) */
struct stat st; /* mount point stat attributes */
time_t ctmo; /* cache timeout (derived from entry/attr_timeout) */
pthread_t tid; /* cache aging thread ID */
pthread_mutex_t mutex; /* cache access mutex */
pid_t pid; /* PID of main() */
char *abmon[12]; /* abbreviated month name of HMC locale */
int ablen[12]; /* length of each abbreviated month name */
};
/*
* SE/HMC drive FUSE file private data
*/
struct hmcdrv_fuse_file {
struct hmcdrv_fuse_file *next; /* collision list (equal hash) */
struct stat st; /* stat structure of this file */
char *symlnk; /* pointer to path name of symlink target (S_IFLNK) */
time_t timeout; /* cache timeout for this file */
size_t cmdlen; /* length of FTP command + path */
char ftpcmd[0]; /* FTP command + path (max HMCDRV_FUSE_MAXCMDLEN) */
};
/*
* all file attributes accumulated from interpreting tokens/fields of 'dir'
* command listing
*/
struct hmcdrv_fuse_attr {
struct stat *st; /* pointer to stat structure */
struct tm ftm; /* local time */
char *fname; /* (base-) name */
};
/*
* prototype (forward) declaration for hmcdrv_cache_dir()
*/
static struct hmcdrv_fuse_file *hmcdrv_file_get(const char *path);
/*
* log severity strings
*/
static const char *const hmcdrv_fuse_logsevname[] = {
"Emergency", /* (0) LOG_EMERG */
"Alert", /* (1) LOG_ALERT */
"Critical", /* (2) LOG_CRIT */
"Error", /* (3) LOG_ERR */
"Warning", /* (4) LOG_WARNING */
"Notice", /* (5) LOG_NOTICE */
"Info", /* (6) LOG_INFO */
"Debug" /* (7) DEBUG */
};
/*
* file attributes cache
*/
static struct hmcdrv_fuse_file *hmcdrv_fuse_cache[HMCDRV_FUSE_CACHE_SIZE];
/*
* context
*/
static struct hmcdrv_fuse_context hmcdrv_ctx = {
.fd = -1,
.ctmo = 1 + HMCDRV_FUSE_CACHE_TMOFS,
.mutex = PTHREAD_MUTEX_INITIALIZER,
};
/*
* log output from hmcdrvfs daemon
*/
static void hmcdrv_fuse_log(int sev, const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp); /* always to stderr */
va_end(argp);
if (hmcdrv_ctx.pid != getpid()) {
struct fuse_context *fctx = fuse_get_context();
if ((fctx != NULL) && (hmcdrv_ctx.pid != fctx->pid)) {
va_start(argp, fmt);
vsyslog(sev,
fmt + (sizeof(HMCDRV_FUSE_LOGHEAD) - 1),
argp);
va_end(argp);
}
}
}
/*
* calculate a hash value from a file path
*/
static unsigned int hmcdrv_hash_path(const char *path)
{
unsigned int hash = ~0;
int len = strlen(path);
while (len > 0) {
hash += (unsigned) *path;
++path;
--len;
}
hash %= HMCDRV_FUSE_CACHE_SIZE;
return hash;
}
/*
* restart of cache entry aging time
*/
static void hmcdrv_cache_trestart(struct hmcdrv_fuse_file *fp)
{
fp->timeout = time(NULL) + hmcdrv_ctx.ctmo;
}
/*
* update or free symlink information
*/
static void hmcdrv_cache_symlink(struct hmcdrv_fuse_file *fp,
const char *symlink)
{
if (fp->symlnk != NULL) {
free(fp->symlnk);
fp->symlnk = NULL;
}
if ((symlink != NULL) && (symlink[0] != '\0')) {
fp->symlnk = malloc(HMCDRV_FUSE_MAXPATH);
if (fp->symlnk != NULL) {
util_strlcpy(fp->symlnk, symlink, HMCDRV_FUSE_MAXPATH);
fp->symlnk[HMCDRV_FUSE_MAXPATH - 1] = '\0';
}
}
}
/*
* refresh the attributes of file/directory 'path'
* note: if there is no cache entry for this file, then create one
*/
static void hmcdrv_cache_refresh(const char *path, const struct stat *st,
const char *symlink)
{
struct hmcdrv_fuse_file **pbase; /* storage location of fp */
struct hmcdrv_fuse_file *fp;
unsigned int index;
int pathlen = strlen(path);
if ((pathlen == 0) || (pathlen >= HMCDRV_FUSE_MAXPATH)) {
HMCDRV_FUSE_LOG(LOG_WARNING,
"Invalid path length for '%s'", path);
return;
}
index = hmcdrv_hash_path(path);
pbase = &hmcdrv_fuse_cache[index];
fp = *pbase;
while (fp != NULL) {
if (strcmp(HMCDRV_FUSE_PATH(fp), path) == 0) {
fp->st = *st; /* update file info */
hmcdrv_cache_symlink(fp, symlink);
hmcdrv_cache_trestart(fp); /* restart aging */
return;
}
pbase = &fp->next;
fp = fp->next;
}
/* entry does not exist so far - create new one
*/
fp = malloc((offsetof(struct hmcdrv_fuse_file, ftpcmd) +
HMCDRV_FUSE_OFSPATH + 1) + pathlen);
if (fp == NULL) {
HMCDRV_FUSE_LOG(LOG_ERR, "Out of memory, %ld bytes",
(offsetof(struct hmcdrv_fuse_file, ftpcmd) +
HMCDRV_FUSE_OFSPATH + 1) + pathlen);
} else {
*pbase = fp;
fp->cmdlen = pathlen + HMCDRV_FUSE_OFSPATH;
fp->st = *st;
memcpy(HMCDRV_FUSE_PATH(fp), path, pathlen + 1);
fp->symlnk = NULL;
hmcdrv_cache_symlink(fp, symlink);
hmcdrv_cache_trestart(fp);
fp->next = NULL;
}
}
/*
* search for a file/directory path in cache
*/
static struct hmcdrv_fuse_file *hmcdrv_cache_find(const char *path)
{
unsigned index = hmcdrv_hash_path(path);
struct hmcdrv_fuse_file *fp = hmcdrv_fuse_cache[index];
while (fp != NULL) {
if (strcmp(HMCDRV_FUSE_PATH(fp), path) == 0) {
hmcdrv_cache_trestart(fp);
return fp;
}
fp = fp->next;
}
return NULL;
}
/*
* check for aging timeout of cache entry at index
*/
static void hmcdrv_cache_expire(unsigned index, time_t now)
{
struct hmcdrv_fuse_file **pbase; /* storage location of fp */
struct hmcdrv_fuse_file *fp, *next;
pbase = &hmcdrv_fuse_cache[index];
fp = *pbase;
/* iterate the collision list */
while (fp != NULL) {
next = fp->next;
if ((fp->timeout <= now) &&
(strcmp(HMCDRV_FUSE_PATH(fp), "/") != 0)) {
hmcdrv_cache_symlink(fp, NULL);
*pbase = next;
free(fp);
} else {
pbase = &fp->next;
}
fp = next;
}
}
/*
* cache expiry handler thread
*/
static void *hmcdrv_cache_aging(void *UNUSED(arg))
{
unsigned int cnt;
time_t now;
unsigned index = 0;
while (1) {
sleep(1);
pthread_mutex_lock(&hmcdrv_ctx.mutex);
now = time(NULL);
/* each second scan a number of cache entries
*/
for (cnt = 0; cnt < HMCDRV_FUSE_GARBAGE_MAX; ++cnt) {
hmcdrv_cache_expire(index, now);
if (++index == HMCDRV_FUSE_CACHE_SIZE)
index = 0;
}
pthread_mutex_unlock(&hmcdrv_ctx.mutex);
}
return NULL;
}
/*
* convert a FTP command ID into a string
*
* Note: If a command string is shorter than HMCDRV_FUSE_MAXFTPLEN then
* define it right-aligned (prefixed with spaces) in table 'cmdstr'.
*/
static void hmcdrv_ftp_str(enum hmcdrv_fuse_cmdid cmd, char *ftp)
{
static const char *cmdstr[HMCDRV_FUSE_CMDID_SIZE] = {
#if (HMCDRV_FUSE_MAXFTPLEN != 3)
#error The length of strings in cmdstr[] must match HMCDRV_FUSE_MAXFTPLEN
#endif
"nls", /* HMCDRV_FUSE_CMDID_NLS */
"dir", /* HMCDRV_FUSE_CMDID_DIR */
"get" /* HMCDRV_FUSE_CMDID_GET */
};
strcpy(ftp, cmdstr[cmd]);
ftp[HMCDRV_FUSE_MAXFTPLEN] = ' '; /* overwrite '\0' from strcpy() */
}
/*
* FTP command execution via kernel device
*/
static ssize_t hmcdrv_ftp_transfer(struct hmcdrv_fuse_file *fp, char *buf,
size_t len, off_t offset)
{
static off_t current_offset;
static char last_ftpcmd[HMCDRV_FUSE_MAXCMDLEN];
ssize_t retlen;
/*
* First check if this is a sequential read from the same file. If
* so skip repositioning the files seek pointer and emitting a new
* command.
*/
if ((offset != current_offset) ||
(strncmp(fp->ftpcmd, last_ftpcmd, HMCDRV_FUSE_MAXCMDLEN) != 0)) {
if ((lseek(hmcdrv_ctx.fd, offset, SEEK_END) < 0) ||
(write(hmcdrv_ctx.fd, fp->ftpcmd, fp->cmdlen) < 0)) {
last_ftpcmd[0] = '\0';
return -errno;
}
current_offset = offset;
}
retlen = read(hmcdrv_ctx.fd, buf, len);
if (retlen < 0) {
last_ftpcmd[0] = '\0';
return -errno;
}
current_offset += retlen;
util_strlcpy(last_ftpcmd, fp->ftpcmd, HMCDRV_FUSE_MAXCMDLEN);
return retlen;
}
/*
* FTP command assembly and execution
*/
static ssize_t hmcdrv_ftp_cmd(struct hmcdrv_fuse_file *fp,
enum hmcdrv_fuse_cmdid cmd,
char *buf, size_t len, off_t offset)
{
if (fp == NULL)
return -EBADF;
hmcdrv_ftp_str(cmd, fp->ftpcmd);
return hmcdrv_ftp_transfer(fp, buf, len, offset);
}
/*
* returns a file path (from internal file structure) to a buffer,
* with appending a slash (in case it is missing)
*/
static int hmcdrv_path_copy(struct hmcdrv_fuse_file *fp, char *dest)
{
char *src = HMCDRV_FUSE_PATH(fp);
int len = 0;
while ((len < (HMCDRV_FUSE_MAXPATH - 1)) &&
(*src != '\0')) {
*dest = *src;
++len;
++dest;
++src;
}
if ((len > 0) && (*(dest - 1) != '/')) {
*dest++ = '/';
++len;
}
*dest = '\0';
return len;
}
/*
* convert a string into into a unsigned number,
* returning 0 on success or -errno on error
*/
static int hmcdrv_parse_uint(const char *s, unsigned int *pval)
{
errno = 0;
*pval = strtoul(s, NULL, 10);
return -errno;
}
/*
* convert a string into into a signed number (with range check),
* returning 0 on success or -errno on error
*/
static int hmcdrv_parse_int(const char *s, int vmin, int vmax, int *pval)
{
errno = 0;
*pval = strtol(s, NULL, 10);
if (errno == 0) {
if ((*pval >= vmin) && (*pval <= vmax))
return 0;
errno = ERANGE;
}
return -errno;
}
/*
* convert an abbreviated month name (%b) into a number
*/
static int hmcdrv_parse_month(const char *s)
{
int i;
for (i = 0; i < 12; ++i) {
if (strncasecmp(hmcdrv_ctx.abmon[i], s,
hmcdrv_ctx.ablen[i]) == 0)
return i;
}
return -1;
}
/*
* return the file mode from a 'ls -l' like mode string
*/
static mode_t hmcdrv_parse_mode(const char *s)
{
int i;
mode_t type;
mode_t mode = 0;
switch (*s) {
case 'd': /* directory */
type = S_IFDIR;
break;
case 'b': /* block device */
type = S_IFBLK;
break;
case 'c': /* character device */
type = S_IFCHR;
break;
case 'l': /* symbolic link */
type = S_IFLNK;
break;
case 'p': /* FIFO */
type = S_IFIFO;
break;
case 's': /* socket */
type = S_IFSOCK;
break;
case '-':
type = S_IFREG;
break;
default:
return 0;
}
for (i = 0; i < 9; ++i) {
mode <<= 1;
switch (*++s) {
case 'r':
case 'w':
case 'x':
mode |= 1;
break;
case '-':
break;
default:
return 0;
}
}
mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
return mode | type;
}
/*
* convert a time string from format "%H:%M" into a 'struct tm' (without any
* local time conversion)
*/
static int hmcdrv_parse_time(const char *s, struct tm *t)
{
char *endptr;
int hour, minute;
errno = 0;
hour = strtol(s, &endptr, 10);
if ((errno == 0) && (endptr != s) &&
(hour >= 0) && (hour < 24)) {
if (*endptr == ':') {
s = ++endptr;
errno = 0;
minute = strtol(s, &endptr, 10);
if ((errno == 0) && (endptr != s) &&
(minute >= 0) && (minute < 60)) {
t->tm_hour = hour;
t->tm_min = minute;
return 0; /* success */
}
}
}
return -1;
}
/*
* interpret token depending on current field number and
* fill the attributes structure
*
* Return: 'field' incremented by 1 or INT_MAX on parse errors
*/
static int hmcdrv_parse_ntoken(int field, char *token,
struct hmcdrv_fuse_attr *attr)
{
switch (field) {
case 0: /* mode */
attr->st->st_mode = hmcdrv_parse_mode(token);
if (attr->st->st_mode == 0) /* error ? */
field = INT_MAX - 1; /* stop */
break;
case 1: /* number of hard links to this file */
errno = 0;
attr->st->st_nlink = strtoul(token, NULL, 10);
if (errno != 0)
field = INT_MAX - 1;
break;
case 2: /* user ID */
if (hmcdrv_parse_uint(token, &attr->st->st_uid) != 0) {
attr->st->st_uid = hmcdrv_ctx.st.st_uid;
HMCDRV_FUSE_LOG(LOG_WARNING,
"Could not parse UID (will use %u)",
attr->st->st_uid);
}
break;
case 3: /* group ID */
if (hmcdrv_parse_uint(token, &attr->st->st_gid) != 0) {
attr->st->st_gid = hmcdrv_ctx.st.st_gid;
HMCDRV_FUSE_LOG(LOG_WARNING,
"Could not parse GID (will use %u)",
attr->st->st_gid);
}
break;
case 4: /* file size */
errno = 0;
attr->st->st_size = strtol(token, NULL, 10);
if (errno != 0)
field = INT_MAX - 1;
break;
case 5: /* month of year [0,11] */
attr->ftm.tm_mon = hmcdrv_parse_month(token);
break;
case 6: /* day of month [1,31] */
if (hmcdrv_parse_int(token, 1, 31, &attr->ftm.tm_mday) != 0) {
field = INT_MAX - 1;
} else {
/*
* in case month is not in English language we've
* got an error in attr->ftm.tm_mon
*/
if (attr->ftm.tm_mon < 0) {
attr->ftm.tm_mday = 1; /* YYYY-01-01 */
attr->ftm.tm_mon = 0;
}
}
break;
case 7: /* %H:%M or %Y */
if (hmcdrv_parse_time(token, &attr->ftm) != 0) {
if (hmcdrv_parse_int(token, 1900, INT_MAX,
&attr->ftm.tm_year) != 0)
field = INT_MAX - 1;
else
attr->ftm.tm_year -= 1900;
}
break;
case 8: /* file name (rest of line) */
attr->fname = token;
break;
default:
break;
}
return ++field;
}
/*
* parse filename and attributes from 'dir' output (a single line)
*
* Note: Because HMC command 'dir' always uses the HMC server local time the
* current timezone should be set correctly, based on startup option
* "-o hmctz=TZ".
*
* Return: pointer to start of next line (skipping any whitespace)
* or pointer to '\0'
*/
static char *hmcdrv_parse_line(char *line, char *namebuf,
int bufsize, struct stat *st,
char *symlink)
{
int field = 0;
struct hmcdrv_fuse_attr attr = {.fname = NULL, .st = st};
memset(st, 0, sizeof(*st));
namebuf[0] = '\0';
/* set default values
*/
st->st_mtime = time(NULL); /* see next line: gmtime_r() */
gmtime_r(&st->st_mtime, &attr.ftm); /* tm_year default */
attr.ftm.tm_isdst = -1; /* mktime() should use "TZ" info */
attr.ftm.tm_hour = attr.ftm.tm_min = attr.ftm.tm_sec = 0;
while ((*line != '\0') && isspace(*line))
++line; /* skip leading whitespace (incl. newlines) */
while ((*line != '\0') && (*line != '\n') && (field < 9)) {
while ((*line != '\0') &&
(*line != '\n') &&
isspace(*line))
++line; /* skip spaces, but not newlines */
if ((*line != '\0') && (*line != '\n')) {
field = hmcdrv_parse_ntoken(field, line, &attr);
while ((*line != '\0') && (*line != '\n')) {
if (isspace(*line))
break;
if (field == 1 && isdigit(*line))
break;
++line; /* search end of field */
}
}
} /* while */
/* search end of line now (skips some fields in case of error)
*/
while ((*line != '\0') && (*line != '\n'))
++line;
/*
* If start of file name was found by parser, then indicate success
* returning the file attributes. But do this only if the end of
* line was really found, else this seems to be an incomplete line
* in current chunk (fragment).
*/
if ((attr.fname != NULL) && (*line == '\n')) {
char *arrow = NULL; /* " -> " pointer, when symlink */
*line = '\0'; /* temporary set EOS for strncpy() */
if (S_ISLNK(st->st_mode)) {
arrow = strstr(attr.fname, " -> ");
if (arrow != NULL)
*arrow = '\0';
}
util_strlcpy(namebuf, attr.fname, bufsize);
if (arrow == NULL) {
symlink[0] = '\0';
} else {
util_strlcpy(symlink, arrow + 4, HMCDRV_FUSE_MAXPATH);
*arrow = ' '; /* restore */
}
*line = '\n'; /* restore */
st->st_mtime = mktime(&attr.ftm); /* UTC from local time */
if (st->st_mtime == (time_t) -1)
st->st_mtime = 0;
st->st_atime = st->st_ctime = st->st_mtime;
/* In contrast to FUSE documentation the members below must
* be set in stat structure. For example 'du' uses them,
* because it shows the disk usage from used blocks and not
* the file size (unless option '--apparent-size' is given).
*/
st->st_blksize = 2048U; /* nearly all CD/DVDs have this */
st->st_blocks = (st->st_size + 511U) / 512U;
}
while ((*line != '\0') && isspace(*line))
++line; /* skip whitespace after EOL, incl. newlines */
return line;
}
/*
* performs a FTP 'dir', then scans/parses the output, fills the cache and
* (optional) calls the FUSE filler function for every file in 'dir' listing
*/
static int hmcdrv_cache_dir(const char *dir, fuse_fill_dir_t filler, void *buf)
{
char path[HMCDRV_FUSE_MAXPATH]; /* constructed path of a file */
char symlink[HMCDRV_FUSE_MAXPATH]; /* target of symlink (S_IFLNK) */
struct hmcdrv_fuse_file *fpdir; /* directory file pointer */
struct stat st; /* attributes of file */
char *dirbuf; /* 'dir' listing (chunk) buffer */
char *next; /* 'next line' pointer in 'dir' buffer */
char *line; /* current line start position in 'dir' buffer */
int dirlen; /* number of characters in 'dir' buffer */
int fraglen; /* fragment length in 'dir' buffer */
char *fname; /* buffer pointer for basename of file */
int len; /* maximum length of file basename (buffer space) */
off_t offset; /* device 'dir' position */
char *tzenv = NULL; /* "TZ" environment variable */
fpdir = hmcdrv_file_get(dir);
if (fpdir == NULL) {
HMCDRV_FUSE_LOG(LOG_ERR,
"Could not find directory '%s' in cache",
dir);
return -ENOENT;
}
dirbuf = malloc(HMCDRV_FUSE_DIRBUF_SIZE);
if (dirbuf == NULL) {
HMCDRV_FUSE_LOG(LOG_ERR,
"Out of memory, %d bytes",
HMCDRV_FUSE_DIRBUF_SIZE);
return -ENOMEM;
}
/* set HMC timezone (optional), else host timezone remains active -
* possibly different from HMC timezone and so resulting in wrong
* file modification times (cf. mktime() in hmcdrv_parse_line())
*/
if (hmcdrv_ctx.opt.hmctz != NULL) { /* "-o hmctz=TZ" present ? */
tzenv = getenv("TZ");
if (tzenv != NULL)
tzenv = strdup(tzenv);
if (setenv("TZ", hmcdrv_ctx.opt.hmctz, 1) != 0) {
HMCDRV_FUSE_LOG(LOG_ERR,
"Could not set HMC timezone '%s': %s",
hmcdrv_ctx.opt.hmctz,
strerror(errno));
if (tzenv != NULL) {
setenv("TZ", tzenv, 1);
free(tzenv);
}
free(dirbuf);
return -ENOMEM;
}
tzset();
}
len = hmcdrv_path_copy(fpdir, path); /* get directory path */
fname = path + len; /* start of basename in path buffer */
len = HMCDRV_FUSE_MAXPATH - len; /* space for basename */
HMCDRV_FUSE_DBGLOG("scanning '%s'...", path);
/* do the 'dir' chunks and parse all
*/
dirlen = hmcdrv_ftp_cmd(fpdir, HMCDRV_FUSE_CMDID_DIR,
dirbuf, HMCDRV_FUSE_DIRBUF_LEN, 0);
next = dirbuf; /* first line at start of buffer */
offset = dirlen;
while (dirlen > 0) {
next[dirlen] = '\0'; /* force end of string */
/* parse all files in this 'dir' listing chunk
*/
do {
line = next;
/* parse next line and retrieve the file name
*/
next = hmcdrv_parse_line(line, fname, len,
&st, symlink);
/* If parsing of 'dir' line was successful, then
* store file attributes and name in cache. Else
* skip this line because of incorrect syntax -
* possibly it is a fragment (at end of buffer) or
* this line does not hold any valid file info.
*/
if (*fname != '\0') {
hmcdrv_cache_refresh(path, &st, symlink);
if ((filler != NULL) &&
(filler(buf, fname, &st, 0) != 0))
filler = NULL; /* stop filling */
#ifdef DEBUG
strftime(symlink, sizeof(symlink),
"%c", gmtime(&st.st_mtime));
HMCDRV_FUSE_DBGLOG(" * %s (size=%zu, mode=%o, uid=%u, gid=%u, time=%s)",
fname,
st.st_size, st.st_mode,
st.st_uid, st.st_gid,
symlink);
#endif
}
} while (*next != '\0');
/* Try to read more bytes from the FTP device. But regard
* remaining chars from the last line, in case it could not
* completely parsed (in other words the line was a
* fragment, and has to be moved to start of buffer).
*/
if (*fname == '\0') { /* no valid filename? */
fraglen = next - line;
if (fraglen > 0)
memcpy(dirbuf, line, fraglen);
} else {
fraglen = 0;
}
dirlen = hmcdrv_ftp_cmd(fpdir, HMCDRV_FUSE_CMDID_DIR,
dirbuf + fraglen,
HMCDRV_FUSE_DIRBUF_LEN - fraglen,
offset);
if (dirlen > 0) { /* on success continue */
next = dirbuf;
offset += dirlen;
dirlen += fraglen;
}
}
free(dirbuf);
/* restore old host timezone
*/
if (hmcdrv_ctx.opt.hmctz != NULL) {
if (tzenv != NULL) {
putenv(tzenv);
free(tzenv);
} else {
unsetenv("TZ");
}
tzset();
}
return dirlen;
}
/*
* cache all files located in parent directory of 'dir',
* so also caching the attributes of 'dir' again
*
* Return: 0 on success, else a negative error code (-ENOENT if 'dir' is "/")
*/
static int hmcdrv_cache_parent(const char *dir)
{
char *tmp, *parent;
if (strcmp(dir, "/") == 0) /* root has no parent */
return -ENOENT;
tmp = strdup(dir); /* need a copy because of dirname() */
if (tmp == NULL) {
HMCDRV_FUSE_LOG(LOG_ERR, "Out of memory, %zd bytes",
strlen(dir) + 1);
return -ENOMEM;
}
parent = dirname(tmp);
HMCDRV_FUSE_DBGLOG("re-scanning parent '%s'...", parent);
hmcdrv_cache_dir(parent, NULL, NULL);
free(tmp);
return 0;
}
/*
* return the file attributes data pointer (or NULL on error)
*/
static struct hmcdrv_fuse_file *hmcdrv_file_get(const char *path)
{
struct hmcdrv_fuse_file *fp = hmcdrv_cache_find(path);
if (fp != NULL) /* path found in cache ? */
return fp;
/* in very, very rare cases we must scan the parent again,
* recursive up to the root directory
*/
if (hmcdrv_cache_parent(path) != 0)
return NULL;
/* because the mutex is taken by the caller, we MUST find this file
* now in cache (if not so then the HMC drive DVD may have changed
* without unmounting FUSE.HMCDRVFS)
*/
return hmcdrv_cache_find(path);
}
/*
* obtain file status information (attributes) on FUSE.HMCDRVFS filesystem
*
* Note: The most important function which FUSE calls (very often).
*/
static int hmcdrv_fuse_getattr(const char *path, struct stat *stbuf)
{
struct hmcdrv_fuse_file *fp;
int rc = 0;
pthread_mutex_lock(&hmcdrv_ctx.mutex);
fp = hmcdrv_file_get(path);
if (fp == NULL)
rc = -ENOENT;
else
*stbuf = fp->st;
pthread_mutex_unlock(&hmcdrv_ctx.mutex);
return rc;
}
/*
* get symlink target path
*/
static int hmcdrv_fuse_readlink(const char *path, char *buf, size_t size)
{
struct hmcdrv_fuse_file *fp;
int rc = 0;
pthread_mutex_lock(&hmcdrv_ctx.mutex);
fp = hmcdrv_file_get(path);
if (fp == NULL) {
rc = -ENOENT;
} else {
if (fp->symlnk == NULL) {
rc = -ENOMEM;
} else {
if (!S_ISLNK(fp->st.st_mode)) {
rc = -EINVAL;
} else {
util_strlcpy(buf, fp->symlnk, size);
}
}
}
pthread_mutex_unlock(&hmcdrv_ctx.mutex);
return rc;
}
/*
* open a directory on FUSE.HMCDRVFS filesystem
*/
static int hmcdrv_fuse_opendir(const char *UNUSED(path),
struct fuse_file_info *fi)
{
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES; /* all files are read-only */
return 0;
}
/*
* read a directory on FUSE.HMCDRVFS filesystem
*/
static int hmcdrv_fuse_readdir(const char *path, void *buf,
fuse_fill_dir_t filler, off_t UNUSED(offset),
struct fuse_file_info *UNUSED(fi))
{
int ret;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
pthread_mutex_lock(&hmcdrv_ctx.mutex);
ret = hmcdrv_cache_dir(path, filler, buf);
pthread_mutex_unlock(&hmcdrv_ctx.mutex);
return ret;
}
/*
* open a file on FUSE.HMCDRVFS filesystem
*/
static int hmcdrv_fuse_open(const char *UNUSED(path), struct fuse_file_info *fi)
{
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES; /* all files are read-only */
return 0;
}
/*
* read a file on FUSE.HMCDRVFS filesystem
*/
static int hmcdrv_fuse_read(const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *UNUSED(fi))
{
struct hmcdrv_fuse_file *fp;
int rc;
pthread_mutex_lock(&hmcdrv_ctx.mutex);
fp = hmcdrv_file_get(path);
if (fp == NULL)
rc = -ENOENT;
else
rc = hmcdrv_ftp_cmd(fp, HMCDRV_FUSE_CMDID_GET,
buf, size, offset);
pthread_mutex_unlock(&hmcdrv_ctx.mutex);
return rc;
}
/*
* initialize FUSE.HMCDRVFS filesystem
*
* Note: calls fuse_exit() on errors
*
* Return: value to be passed in the private_data field of fuse_context to
* all file operations and as a parameter to the destroy() method
*/
static void *hmcdrv_fuse_init(struct fuse_conn_info *UNUSED(conn))
{
pthread_mutexattr_t attr;
memset(hmcdrv_fuse_cache, 0, sizeof(hmcdrv_fuse_cache));
openlog(HMCDRV_FUSE_LOGNAME, LOG_PID, LOG_DAEMON);
if (pthread_mutexattr_init(&attr) != 0)
goto err_out;
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
hmcdrv_ctx.fd = open(HMCDRV_FUSE_FTPDEV, O_RDWR);
if (hmcdrv_ctx.fd < 0)
goto err_dev;
if (pthread_mutex_init(&hmcdrv_ctx.mutex, &attr) != 0)
goto err_mutex;
hmcdrv_cache_refresh("/", &hmcdrv_ctx.st, NULL); /* never expires */
if (pthread_create(&hmcdrv_ctx.tid, NULL,
hmcdrv_cache_aging, NULL) == 0) {
pthread_mutexattr_destroy(&attr);
return &hmcdrv_ctx.tid;
}
err_mutex:
close(hmcdrv_ctx.fd);
err_dev:
pthread_mutexattr_destroy(&attr);
err_out:
HMCDRV_FUSE_LOG(LOG_ERR, "Initialization failed: %s",
strerror(errno));
closelog();
fuse_exit(fuse_get_context()->fuse);
return NULL;
}
/*
* clean up FUSE.HMCDRVFS filesystem
*/
static void hmcdrv_fuse_exit(void *arg)
{
struct hmcdrv_fuse_file *fp, *next;
int i;
if (arg != NULL)
pthread_cancel(*(pthread_t *) arg);
pthread_mutex_lock(&hmcdrv_ctx.mutex);
for (i = 0; i < HMCDRV_FUSE_CACHE_SIZE; ++i) {
fp = hmcdrv_fuse_cache[i];
while (fp != NULL) {
next = fp->next;
hmcdrv_cache_symlink(fp, NULL);
free(fp);
fp = next;
}
}
memset(hmcdrv_fuse_cache, 0, sizeof(hmcdrv_fuse_cache));
pthread_mutex_destroy(&hmcdrv_ctx.mutex);
closelog();
if (hmcdrv_ctx.fd >= 0)
close(hmcdrv_ctx.fd);
}
/*
* FUSE.HMCDRVFS internal main function
*/
static int hmcdrv_fuse_main(struct fuse_args *args)
{
static struct fuse_operations hmcdrv_fuse_op = {
.init = hmcdrv_fuse_init,
.destroy = hmcdrv_fuse_exit,
.getattr = hmcdrv_fuse_getattr,
.opendir = hmcdrv_fuse_opendir,
.readdir = hmcdrv_fuse_readdir,
.open = hmcdrv_fuse_open,
.read = hmcdrv_fuse_read,
.readlink = hmcdrv_fuse_readlink
};
#if FUSE_VERSION >= 26
return fuse_main(args->argc, args->argv, &hmcdrv_fuse_op, NULL);
#else
return fuse_main(args->argc, args->argv, &hmcdrv_fuse_op);
#endif
}
/*
* usage output
*/
static void hmcdrv_fuse_usage(const char *progname)
{
fprintf(stdout,
"Usage: %s MOUNTPOINT [OPTIONS]\n\n"
"Use the %s command to read files from a HMC drive DVD.\n"
"\n"
"General options:\n"
" -o opt,[opt...] Mount options\n"
" -h --help Print help, then exit\n"
" -v --version Print version, then exit\n"
"\n"
"Specific options:\n"
" -o hmclang=LANG HMC speaks language LANG (see locale(1))\n"
" -o hmctz=TZ HMC is in timezone TZ (see tzset(3))\n"
"\n"
"Attention:\n"
" The following general and FUSE specific mount options will\n"
" be ignored (most do not make sense on a read-only media):\n"
" --rw, -w, -o rw, -o atime, -o max_write=N, -o big_writes,\n"
" -o atomic_o_trunc, -o hard_remove, -o negative_timeout=T,\n"
" -o use_ino, -o readdir_ino, -o subdir=DIR\n"
"\n",
progname, progname);
}
/*
* process the "-o hmclang=LANG" command line option (if there is one) and
* prepare abbreviated month names (%b) in context structure
*
* Return: 0 on success, -1 on error
*/
static int hmcdrv_optproc_lang(void)
{
static const nl_item nl_abmon[12] = {
ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6,
ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12};
unsigned int i;
char *lc = NULL; /* current LC_TIME locale */
if (hmcdrv_ctx.opt.hmclang != NULL) {
HMCDRV_FUSE_DBGLOG("option \"-o hmclang=%s\" detected",
hmcdrv_ctx.opt.hmclang);
lc = setlocale(LC_TIME, NULL);
if (setlocale(LC_TIME, hmcdrv_ctx.opt.hmclang) == NULL) {
setlocale(LC_TIME, lc);
return -1;
}
}
for (i = 0; i < (sizeof(nl_abmon) / sizeof(nl_abmon[0])); ++i) {
hmcdrv_ctx.abmon[i] = nl_langinfo(nl_abmon[i]);
hmcdrv_ctx.ablen[i] = strlen(hmcdrv_ctx.abmon[i]);
}
if (lc)
setlocale(LC_TIME, lc); /* restore LC_TIME locale */
return 0;
}
/*
* option parsing function
*/
static int hmcdrv_fuse_optproc(void *data, const char *arg,
int key, struct fuse_args *outargs)
{
double tmo; /* timeout T in "-o entry/attr_timeout=T" */
mode_t mask; /* umask() of caller */
(void)outargs;
switch (key) {
case HMCDRV_FUSE_OPTKEY_NSUP:
HMCDRV_FUSE_LOG(LOG_WARNING,
"FUSE option \"%s\" ignored (unsupported)",
arg);
return 0; /* remove this option(s) */
case HMCDRV_FUSE_OPTKEY_RW: /* write options (unsupported) */
HMCDRV_FUSE_LOG(LOG_WARNING,
"FUSE option \"%s\" ignored (r/o filesystem)",
arg);
return 0; /* remove this option(s) */
case HMCDRV_FUSE_OPTKEY_RO: /* "-o ro" or "-r" */
((struct hmcdrv_fuse_opt *)data)->ro = 1;
return 1;
case HMCDRV_FUSE_OPTKEY_NATM: /* "-o noatime" */
((struct hmcdrv_fuse_opt *)data)->noatime = 1;
return 1;
case HMCDRV_FUSE_OPTKEY_CTMO: /* "-o entry/attr_timeout=T" */
if (sscanf(arg, "%*[^=]=%lf", &tmo) == 1) {
if (tmo < 1.0)
tmo = 1.0;
if (hmcdrv_ctx.ctmo <
(HMCDRV_FUSE_CACHE_TMOFS + (time_t)tmo))
hmcdrv_ctx.ctmo =
HMCDRV_FUSE_CACHE_TMOFS + (time_t)tmo;
HMCDRV_FUSE_DBGLOG("option \"%s\" detected (cache timeout now is %" PRIdMAX " sec.)",
arg, (intmax_t) hmcdrv_ctx.ctmo);
}
return 1;
case HMCDRV_FUSE_OPTKEY_VERSION:
fprintf(stdout, HMCDRV_FUSE_LOGHEAD
"HMC drive DVD file system, version %s\n"
"Copyright IBM Corp. 2015, 2017\n",
HMCDRV_FUSE_RELEASE);
exit(EXIT_SUCCESS);
case HMCDRV_FUSE_OPTKEY_HELP:
hmcdrv_fuse_usage(outargs->argv[0]);
/*
* Usage output needs to go to stdout to be consistent with
* coding guidelines. FUSE versions before 3.0.0 print help
* output to stderr. Redirect stderr to stdout here to enforce
* consistent behavior.
*/
fflush(stderr);
dup2(STDOUT_FILENO, STDERR_FILENO);
fuse_opt_add_arg(outargs, "-ho");
hmcdrv_fuse_main(outargs);
exit(EXIT_SUCCESS);
case FUSE_OPT_KEY_NONOPT: /* normally the mount point */
mask = umask(0);
umask(mask);
hmcdrv_ctx.st.st_uid = getgid();
hmcdrv_ctx.st.st_gid = getuid();
hmcdrv_ctx.st.st_ino = 0;
hmcdrv_ctx.st.st_size = 0; /* unknown */
hmcdrv_ctx.st.st_blksize = 2048U; /* DVD sector size */
hmcdrv_ctx.st.st_nlink = 2; /* minimum */
hmcdrv_ctx.st.st_mtime = time(NULL);
hmcdrv_ctx.st.st_atime = hmcdrv_ctx.st.st_mtime;
hmcdrv_ctx.st.st_ctime = hmcdrv_ctx.st.st_mtime;
hmcdrv_ctx.st.st_mode =
(S_IFDIR |
S_IXUSR | S_IRUSR |
S_IXGRP | S_IRGRP |
S_IXOTH | S_IROTH) &
~(mask | (S_IWUSR | S_IWGRP | S_IWOTH));
HMCDRV_FUSE_DBGLOG("mount point is %s (uid = %u, gid = %u)",
arg,
hmcdrv_ctx.st.st_uid,
hmcdrv_ctx.st.st_gid);
return 1;
default:
HMCDRV_FUSE_DBGLOG("option \"%s\" passed to FUSE", arg);
return 1; /* pass all other options to fuse_main() */
}
}
/*
* FUSE.HMCDRVFS entry function
*/
int main(int argc, char *argv[])
{
static struct fuse_opt lookup_opt[] = {
HMCDRV_FUSE_OPT("hmctz=%s", hmctz, 0),
HMCDRV_FUSE_OPT("hmclang=%s", hmclang, 0),
FUSE_OPT_KEY("ro", HMCDRV_FUSE_OPTKEY_RO),
FUSE_OPT_KEY("-r", HMCDRV_FUSE_OPTKEY_RO),
FUSE_OPT_KEY("--read-only", HMCDRV_FUSE_OPTKEY_RO),
FUSE_OPT_KEY("noatime", HMCDRV_FUSE_OPTKEY_NATM),
FUSE_OPT_KEY("entry_timeout=%lf", HMCDRV_FUSE_OPTKEY_CTMO),
FUSE_OPT_KEY("attr_timeout=%lf", HMCDRV_FUSE_OPTKEY_CTMO),
/* unsupported (ignored) */
FUSE_OPT_KEY("subdir=%s", HMCDRV_FUSE_OPTKEY_NSUP),
FUSE_OPT_KEY("use_ino", HMCDRV_FUSE_OPTKEY_NSUP),
FUSE_OPT_KEY("readdir_ino", HMCDRV_FUSE_OPTKEY_NSUP),
/* to be ignored on read-only filesystem */
FUSE_OPT_KEY("rw", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("-w", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("--rw", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("atime", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("max_write=%u", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("big_writes", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("hard_remove", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("atomic_o_trunc", HMCDRV_FUSE_OPTKEY_RW),
FUSE_OPT_KEY("negative_timeout=%lf", HMCDRV_FUSE_OPTKEY_RW),
/* options, that exit immediately */
FUSE_OPT_KEY("-v", HMCDRV_FUSE_OPTKEY_VERSION),
FUSE_OPT_KEY("--version", HMCDRV_FUSE_OPTKEY_VERSION),
FUSE_OPT_KEY("-h", HMCDRV_FUSE_OPTKEY_HELP),
FUSE_OPT_KEY("--help", HMCDRV_FUSE_OPTKEY_HELP),
FUSE_OPT_END
};
int ret;
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
memset(&hmcdrv_ctx.opt, 0, sizeof(hmcdrv_ctx.opt));
hmcdrv_ctx.pid = getpid();
fuse_opt_parse(&args, &hmcdrv_ctx.opt, lookup_opt,
hmcdrv_fuse_optproc);
/* the following options are required on a read-only media */
if (!hmcdrv_ctx.opt.ro)
fuse_opt_add_arg(&args, "-oro");
if (!hmcdrv_ctx.opt.noatime)
fuse_opt_add_arg(&args, "-onoatime");
if (hmcdrv_optproc_lang() != 0) {
HMCDRV_FUSE_LOG(LOG_ERR,
"Unknown HMC language in '-o hmclang=%s'",
hmcdrv_ctx.opt.hmclang);
exit(EXIT_FAILURE);
}
/* notice that there is no way to check the timezone parameter for
* correct syntax, not in glibc as well as POSIX 1003.1
*/
if (hmcdrv_ctx.opt.hmctz != NULL)
HMCDRV_FUSE_DBGLOG("option \"-o hmctz=%s\" detected",
hmcdrv_ctx.opt.hmctz);
ret = hmcdrv_fuse_main(&args);
fuse_opt_free_args(&args);
return ret;
}
|