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 1585 1586 1587 1588 1589 1590 1591 1592 1593
|
/*
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Further, this software is distributed without any warranty that it is
* free of the rightful claim of any third person regarding infringement
* or the like. Any license provided herein, whether implied or
* otherwise, applies only to this software file. Patent licenses, if
* any, provided herein do not apply to combinations of this program with
* other software, or any other product whatsoever.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston MA 02111-1307, USA.
*
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
* Mountain View, CA 94043, or:
*
* http://www.sgi.com
*
* For further information regarding this notice, see:
*
* http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
*/
/*
* fsr - file system reorganizer
*
* fsr [-d] [-v] [-n] [-s] [-g] [-t mins] [-f leftf] [-m mtab]
* fsr [-d] [-v] [-n] [-s] [-g] xfsdev | dir | file ...
*
* If invoked in the first form fsr does the following: starting with the
* dev/inum specified in /etc/fsrlast this reorgs each reg file in each file
* system found in /etc/mtab. After 2 hours of this we record the current
* dev/inum in /etc/fsrlast. If there is no /etc/fsrlast fsr starts at the
* top of /etc/mtab.
*
* -g print to syslog (default if stdout not a tty)
* -m mtab use something other than /etc/mtab
* -t time how long to run
* -f leftoff use this instead of /etc/fsrlast
*
* -v verbose. more -v's more verbose
* -d debug. print even more
* -n do nothing. only interesting with -v. Not
* effective with in mtab mode.
* -s print statistics only.
* -p passes Number of passes before terminating global re-org.
*/
#include <libxfs.h>
#include <jdm.h>
#include <fcntl.h>
#include <errno.h>
#include <malloc.h>
#include <mntent.h>
#include <syslog.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/vfs.h>
#include <sys/statvfs.h>
#include <attributes.h>
#include <xfs_dfrag.h>
int vflag;
int gflag;
static int Mflag;
/* static int nflag; */
int dflag = 0;
/* static int sflag; */
int argv_blksz_dio;
extern int max_ext_size;
static int npasses = 10;
static int startpass = 0;
struct getbmap *outmap = NULL;
int outmap_size = 0;
int RealUid;
int tmp_agi;
static __int64_t minimumfree = 2048;
#define MNTTYPE_XFS "xfs"
#define SMBUFSZ 1024
#define ROOT 0
#define NULLFD -1
#define GRABSZ 64
#define TARGETRANGE 10
#define V_NONE 0
#define V_OVERVIEW 1
#define V_ALL 2
#define BUFFER_SIZE (1<<16)
#define BUFFER_MAX (1<<24)
#define min(x, y) ((x) < (y) ? (x) : (y))
static time_t howlong = 7200; /* default seconds of reorganizing */
static char *leftofffile = "/var/tmp/.fsrlast_xfs";/* where we left off last */
static char *mtab = MOUNTED;
static char *progname;
static time_t endtime;
static time_t starttime;
static xfs_ino_t leftoffino = 0;
static int pagesize;
void usage(int ret);
static int fsrfile(char *fname, xfs_ino_t ino);
static int fsrfile_common( char *fname, char *tname, char *mnt,
int fd, xfs_bstat_t *statp);
static int packfile(char *fname, char *tname, int fd,
xfs_bstat_t *statp, int flag);
static void fsrdir(char *dirname);
static int fsrfs(char *mntdir, xfs_ino_t ino, int targetrange);
static void initallfs(char *mtab);
static void fsrallfs(int howlong, char *leftofffile);
static void fsrall_cleanup(int timeout);
static int getnextents(int);
int xfsrtextsize(int fd);
int xfs_getrt(int fd, struct statvfs64 *sfbp);
char * gettmpname(char *fname);
char * getparent(char *fname);
int fsrprintf(const char *fmt, ...);
int read_fd_bmap(int, xfs_bstat_t *, int *);
int cmp(const void *, const void *);
static void tmp_init(char *mnt);
static char * tmp_next(char *mnt);
static void tmp_close(char *mnt);
int xfs_getgeom(int , xfs_fsop_geom_t * );
static int getmntany (FILE *, struct mntent *, struct mntent *);
xfs_fsop_geom_t fsgeom; /* geometry of active mounted system */
#define NMOUNT 64
static int numfs;
typedef struct fsdesc {
char *dev;
char *mnt;
int npass;
} fsdesc_t;
fsdesc_t *fs, *fsbase, *fsend;
int fsbufsize = 10; /* A starting value */
int nfrags = 0; /* Debug option: Coerse into specific number
* of extents */
int openopts = O_CREAT|O_EXCL|O_RDWR|O_DIRECT;
int
xfs_fsgeometry(int fd, xfs_fsop_geom_t *geom)
{
return ioctl(fd, XFS_IOC_FSGEOMETRY, geom);
}
int
xfs_bulkstat_single(int fd, xfs_ino_t *lastip, xfs_bstat_t *ubuffer)
{
xfs_fsop_bulkreq_t bulkreq;
bulkreq.lastip = lastip;
bulkreq.icount = 1;
bulkreq.ubuffer = ubuffer;
bulkreq.ocount = NULL;
return ioctl(fd, XFS_IOC_FSBULKSTAT_SINGLE, &bulkreq);
}
int
xfs_bulkstat(int fd, xfs_ino_t *lastip, int icount,
xfs_bstat_t *ubuffer, size_t *ocount)
{
xfs_fsop_bulkreq_t bulkreq;
bulkreq.lastip = lastip;
bulkreq.icount = icount;
bulkreq.ubuffer = ubuffer;
bulkreq.ocount = (__s32 *)ocount;
return ioctl(fd, XFS_IOC_FSBULKSTAT, &bulkreq);
}
int
xfs_swapext(int fd, xfs_swapext_t *sx)
{
return ioctl(fd, XFS_IOC_SWAPEXT, sx);
}
int
xfs_fscounts(int fd, xfs_fsop_counts_t *counts)
{
return ioctl(fd, XFS_IOC_FSCOUNTS, counts);
}
void
aborter(int unused)
{
fsrall_cleanup(1);
exit(1);
}
int
main(int argc, char **argv)
{
struct stat sb, sb2;
char *argname;
char *cp;
int c;
struct mntent mntpref;
register struct mntent *mntp;
struct mntent mntent;
register FILE *mtabp;
setlinebuf(stdout);
progname = basename(argv[0]);
gflag = ! isatty(0);
while ((c = getopt(argc, argv, "C:p:e:MgsdnvTt:f:m:b:N:FV")) != -1 )
switch (c) {
case 'M':
Mflag = 1;
break;
case 'g':
gflag = 1;
break;
case 'n':
/* nflag = 1; */
break;
case 'v':
++vflag;
break;
case 'd':
dflag = 1;
break;
case 's': /* frag stats only */
/* sflag = 1; */
fprintf(stderr, "%s: Stats not yet supported for XFS\n",
progname);
usage(1);
break;
case 't':
howlong = atoi(optarg);
break;
case 'f':
leftofffile = optarg;
break;
case 'm':
mtab = optarg;
break;
case 'b':
argv_blksz_dio = atoi(optarg);
break;
case 'p':
npasses = atoi(optarg);
break;
case 'C':
/* Testing opt: coerses frag count in result */
if (getenv("FSRXFSTEST") != NULL) {
nfrags = atoi(optarg);
openopts |= O_SYNC;
}
break;
case 'V':
printf("%s version %s\n", progname, VERSION);
exit(0);
default:
usage(1);
}
if (vflag)
setbuf(stdout, NULL);
starttime = time(0);
/* Save the caller's real uid */
RealUid = getuid();
pagesize = getpagesize();
if (optind < argc) {
for (; optind < argc; optind++) {
argname = argv[optind];
mntp = NULL;
if (lstat(argname, &sb) < 0) {
fprintf(stderr, "%s: could not stat: %s: %s\n",
progname, argname, strerror(errno));
continue;
}
if (S_ISLNK(sb.st_mode) && stat(argname, &sb2) == 0 &&
(S_ISBLK(sb2.st_mode) || S_ISCHR(sb2.st_mode)))
sb = sb2;
if (S_ISBLK(sb.st_mode) || (S_ISDIR(sb.st_mode))) {
if ((mtabp = setmntent(mtab, "r")) == NULL) {
fprintf(stderr,
"%s: failed reading mtab: %s\n",
progname, mtab);
exit(1);
}
bzero(&mntpref, sizeof(mntpref));
if (S_ISDIR(sb.st_mode))
mntpref.mnt_dir = argname;
else
mntpref.mnt_fsname = argname;
if ((getmntany(mtabp, &mntent, &mntpref) == 0)
&&
(strcmp(mntent.mnt_type, MNTTYPE_XFS) == 0))
{
mntp = &mntent;
if (S_ISBLK(sb.st_mode)) {
cp = mntp->mnt_dir;
if (cp == NULL ||
stat(cp, &sb2) < 0) {
fprintf(stderr,
"%s: could not stat: %s: %s\n",
progname, argname,
strerror(errno));
continue;
}
sb = sb2;
argname = cp;
}
}
}
if (mntp != NULL) {
fsrfs(mntp->mnt_dir, 0, 100);
} else if (S_ISCHR(sb.st_mode)) {
fprintf(stderr,
"%s: char special not supported: %s\n",
progname, argname);
exit(1);
} else if (S_ISDIR(sb.st_mode) || S_ISREG(sb.st_mode)) {
struct statfs fs;
statfs(argname, &fs);
if (fs.f_type != XFS_SB_MAGIC) {
fprintf(stderr,
"%s: cannot defragment: %s: Not XFS\n",
progname, argname);
continue;
}
if (S_ISDIR(sb.st_mode))
fsrdir(argname);
else
fsrfile(argname, sb.st_ino);
} else {
printf(
"%s: not fsys dev, dir, or reg file, ignoring\n",
argname);
}
}
} else {
initallfs(mtab);
fsrallfs(howlong, leftofffile);
}
return 0;
}
void
usage(int ret)
{
fprintf(stderr, "Usage: %s [xfsfile] ...\n", progname);
exit(ret);
}
/*
* initallfs -- read the mount table and set up an internal form
*/
static void
initallfs(char *mtab)
{
FILE *fp;
struct mntent *mp;
int mi;
char *cp;
struct stat sb;
fp = setmntent(mtab, "r");
if (fp == NULL) {
fsrprintf("could not open mtab file: %s\n", mtab);
exit(1);
}
/* malloc a number of descriptors, increased later if needed */
if ((fsbase = (fsdesc_t *)malloc(fsbufsize * sizeof(fsdesc_t))) == NULL) {
fsrprintf("out of memory: %s\n", strerror(errno));
exit(1);
}
fsend = (fsbase + fsbufsize - 1);
/* find all rw xfs file systems */
mi = 0;
fs = fsbase;
while ((mp = getmntent(fp))) {
int rw = 0;
if (strcmp(mp->mnt_type, MNTTYPE_XFS ) != 0 ||
stat(mp->mnt_fsname, &sb) == -1 ||
!S_ISBLK(sb.st_mode))
continue;
cp = strtok(mp->mnt_opts,",");
do {
if (strcmp("rw", cp) == 0)
rw++;
} while ((cp = strtok(NULL, ",")) != NULL);
if (rw == 0) {
if (dflag)
fsrprintf("Skipping %s: not mounted rw\n",
mp->mnt_fsname);
continue;
}
if (mi == fsbufsize) {
fsbufsize += NMOUNT;
if ((fsbase = (fsdesc_t *)realloc((char *)fsbase,
fsbufsize * sizeof(fsdesc_t))) == NULL) {
fsrprintf("out of memory: %s\n", strerror(errno));
exit(1);
}
if (!fsbase) {
fsrprintf("out of memory on realloc: %s\n",
strerror(errno));
exit(1);
}
fs = (fsbase + mi); /* Needed ? */
}
fs->dev = strdup(mp->mnt_fsname);
fs->mnt = strdup(mp->mnt_dir);
if (fs->mnt == NULL || fs->mnt == NULL) {
fsrprintf("strdup(%s) failed\n",mp->mnt_fsname);
exit(1);
}
mi++;
fs++;
}
numfs = mi;
fsend = (fsbase + numfs);
endmntent(fp);
if (numfs == 0) {
fsrprintf("no rw xfs file systems in mtab: %s\n", mtab);
exit(0);
}
if (vflag || dflag) {
fsrprintf("Found %d mounted, writable, XFS filesystems\n",
numfs);
if (dflag)
for (fs = fsbase; fs < fsend; fs++)
fsrprintf("\t%-30.30s%-30.30s\n", fs->dev, fs->mnt);
}
}
static void
fsrallfs(int howlong, char *leftofffile)
{
int fd;
int error;
int found = 0;
char *fsname;
char buf[SMBUFSZ];
int mdonly = Mflag;
char *ptr;
xfs_ino_t startino = 0;
fsdesc_t *fsp;
struct stat64 sb, sb2;
fsrprintf("xfs_fsr -m %s -t %d -f %s ...\n", mtab, howlong, leftofffile);
endtime = starttime + howlong;
fs = fsbase;
/* where'd we leave off last time? */
if (lstat64(leftofffile, &sb) == 0) {
if ( (fd = open(leftofffile, O_RDONLY)) == -1 ) {
fsrprintf("%s: open failed\n", leftofffile);
}
else if ( fstat64(fd, &sb2) == 0) {
/*
* Verify that lstat & fstat point to the
* same regular file (no links/no quick spoofs)
*/
if ( (sb.st_dev != sb2.st_dev) ||
(sb.st_ino != sb2.st_ino) ||
((sb.st_mode & S_IFMT) != S_IFREG) ||
((sb2.st_mode & S_IFMT) != S_IFREG) ||
(sb2.st_uid != ROOT) ||
(sb2.st_nlink != 1)
)
{
fsrprintf( "Can't use %s: mode=0%o own=%d"
" nlink=%d\n",
leftofffile, sb.st_mode,
sb.st_uid, sb.st_nlink);
close(fd);
fd = NULLFD;
}
}
else {
close(fd);
fd = NULLFD;
}
}
else {
fd = NULLFD;
}
if (fd != NULLFD) {
if (read(fd, buf, SMBUFSZ) == -1) {
fs = fsbase;
fsrprintf("could not read %s, starting with %s\n",
leftofffile, *fs->dev);
} else {
for (fs = fsbase; fs < fsend; fs++) {
fsname = fs->dev;
if ((strncmp(buf,fsname,strlen(fsname)) == 0)
&& buf[strlen(fsname)] == ' ') {
found = 1;
break;
}
}
if (! found)
fs = fsbase;
ptr = strchr(buf, ' ');
if (ptr) {
startpass = atoi(++ptr);
ptr = strchr(ptr, ' ');
if (ptr) {
startino = strtoull(++ptr,
(char **)NULL,
10);
}
}
if (startpass < 0)
startpass = 0;
/* Init pass counts */
for (fsp = fsbase; fsp < fs; fsp++) {
fsp->npass = startpass + 1;
}
for (fsp = fs; fsp <= fsend; fsp++) {
fsp->npass = startpass;
}
}
close(fd);
}
if (vflag) {
fsrprintf("START: pass=%d ino=%llu %s %s\n",
fs->npass, (unsigned long long)startino,
fs->dev, fs->mnt);
}
signal(SIGABRT, aborter);
signal(SIGHUP, aborter);
signal(SIGINT, aborter);
signal(SIGQUIT, aborter);
signal(SIGTERM, aborter);
/* reorg for 'howlong' -- checked in 'fsrfs' */
while (endtime > time(0)) {
pid_t pid;
if (fs == fsend)
fs = fsbase;
if (fs->npass == npasses) {
fsrprintf("Completed all %d passes\n", npasses);
break;
}
if (npasses > 1 && !fs->npass)
Mflag = 1;
else
Mflag = mdonly;
pid = fork();
switch(pid) {
case -1:
fsrprintf("couldn't fork sub process:");
exit(1);
break;
case 0:
error = fsrfs(fs->mnt, startino, TARGETRANGE);
exit (error);
break;
default:
wait(&error);
close(fd);
if (WIFEXITED(error) && WEXITSTATUS(error) == 1) {
/* child timed out & did fsrall_cleanup */
exit(0);
}
break;
}
startino = 0; /* reset after the first time through */
fs->npass++;
fs++;
}
fsrall_cleanup(endtime <= time(0));
}
/*
* fsrall_cleanup -- close files, print next starting location, etc.
*/
static void
fsrall_cleanup(int timeout)
{
int fd;
int ret;
char buf[SMBUFSZ];
/* record where we left off */
unlink(leftofffile);
fd = open(leftofffile, O_WRONLY|O_CREAT|O_EXCL, 0644);
if (fd == -1)
fsrprintf("open(%s) failed: %s\n",
leftofffile, strerror(errno));
else {
if (timeout) {
ret = sprintf(buf, "%s %d %llu\n", fs->dev,
fs->npass, (unsigned long long)leftoffino);
if (write(fd, buf, ret) < strlen(buf))
fsrprintf("write(%s) failed: %s\n", leftofffile,
strerror(errno));
close(fd);
}
}
if (timeout)
fsrprintf("xfs_fsr startpass %d, endpass %d, time %d seconds\n",
startpass, fs->npass, time(0) - endtime + howlong);
}
/*
* fsrfs -- reorganize a file system
*/
static int
fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
{
int fsfd, fd;
int count = 0;
int ret;
size_t buflenout;
xfs_bstat_t buf[GRABSZ];
char fname[64];
char *tname;
jdm_fshandle_t *fshandlep;
xfs_ino_t lastino = startino;
fsrprintf("%s startino=%llu\n", mntdir, (unsigned long long)startino);
fshandlep = jdm_getfshandle( mntdir );
if ( ! fshandlep ) {
fsrprintf("unable to get handle: %s: %s\n",
mntdir, strerror( errno ));
return -1;
}
if ((fsfd = open(mntdir, O_RDONLY)) < 0) {
fsrprintf("unable to open: %s: %s\n",
mntdir, strerror( errno ));
return -1;
}
if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
fsrprintf("Skipping %s: could not get XFS geom\n",
mntdir);
return -1;
}
tmp_init(mntdir);
while (! xfs_bulkstat(fsfd, &lastino, GRABSZ, &buf[0], &buflenout)) {
xfs_bstat_t *p;
xfs_bstat_t *endp;
if (buflenout == 0 )
goto out0;
/* Each loop through, defrag targetrange percent of the files */
count = (buflenout * targetrange) / 100;
qsort((char *)buf, buflenout, sizeof(struct xfs_bstat), cmp);
for ( p = buf, endp = (buf + buflenout); p < endp ; p++ ) {
/* Do some obvious checks now */
if (((p->bs_mode & S_IFMT) != S_IFREG) ||
(p->bs_extents < 2))
continue;
if((fd = jdm_open(fshandlep, p, O_RDWR)) < 0) {
/* This probably means the file was
* removed while in progress of handling
* it. Just quietly ignore this file.
*/
if (dflag)
fsrprintf("could not open: ino %llu\n",
p->bs_ino);
continue;
}
/* Don't know the pathname, so make up something */
sprintf(fname, "ino=%llu", (unsigned long long)p->bs_ino);
/* Get a tmp file name */
tname = tmp_next(mntdir);
ret = fsrfile_common(fname, tname, mntdir, fd, p);
leftoffino = p->bs_ino;
close(fd);
if (ret == 0) {
if (--count <= 0)
break;
}
}
if (endtime && endtime < time(0)) {
tmp_close(mntdir);
close(fsfd);
fsrall_cleanup(1);
exit(1);
}
}
out0:
tmp_close(mntdir);
close(fsfd);
return 0;
}
/*
* To compare bstat structs for qsort.
*/
int
cmp(const void *s1, const void *s2)
{
return( ((xfs_bstat_t *)s2)->bs_extents -
((xfs_bstat_t *)s1)->bs_extents);
}
/*
* reorganize by directory hierarchy.
* Stay in dev (a restriction based on structure of this program -- either
* call efs_{n,u}mount() around each file, something smarter or this)
*/
static void
fsrdir(char *dirname)
{
fsrprintf("%s: Directory defragmentation not supported\n", dirname);
}
/*
* Sets up the defragmentation of a file based on the
* filepath. It collects the bstat information, does
* an open on the file and passes this all to fsrfile_common.
*/
static int
fsrfile(char *fname, xfs_ino_t ino)
{
xfs_bstat_t statbuf;
jdm_fshandle_t *fshandlep;
int fd, fsfd;
int error = 0;
char *tname;
fshandlep = jdm_getfshandle(getparent (fname) );
if (! fshandlep) {
fsrprintf(
"unable to construct sys handle for %s: %s\n",
fname, strerror(errno));
return -1;
}
/*
* Need to open something on the same filesystem as the
* file. Open the parent.
*/
fsfd = open(getparent(fname), O_RDONLY);
if (fsfd < 0) {
fsrprintf(
"unable to open sys handle for %s: %s\n",
fname, strerror(errno));
return -1;
}
if ((xfs_bulkstat_single(fsfd, &ino, &statbuf)) < 0) {
fsrprintf(
"unable to get bstat on %s: %s\n",
fname, strerror(errno));
close(fsfd);
return -1;
}
fd = jdm_open( fshandlep, &statbuf, O_RDWR);
if (fd < 0) {
fsrprintf(
"unable to open handle %s: %s\n",
fname, strerror(errno));
close(fsfd);
return -1;
}
/* Get the fs geometry */
if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
fsrprintf("Unable to get geom on fs for: %s\n", fname);
close(fsfd);
return -1;
}
close(fsfd);
tname = gettmpname(fname);
if (tname)
error = fsrfile_common(fname, tname, NULL, fd, &statbuf);
close(fd);
return error;
}
/*
* This is the common defrag code for either a full fs
* defragmentation or a single file. Check as much as
* possible with the file, fork a process to setuid to the
* target file owner's uid and defragment the file.
* This is done so the new extents created in a tmp file are
* reflected in the owners' quota without having to do any
* special code in the kernel. When the existing extents
* are removed, the quotas will be correct. It's ugly but
* it saves us from doing some quota re-construction in
* the extent swap. The price is that the defragmentation
* will fail if the owner of the target file is already at
* their quota limit.
*/
static int
fsrfile_common(
char *fname,
char *tname,
char *fsname,
int fd,
xfs_bstat_t *statp)
{
int pid;
int error;
struct statvfs64 vfss;
struct fsxattr fsx;
int do_rt = 0;
unsigned long bsize;
#ifdef HAVE_CAPABILITIES
cap_t ocap;
cap_value_t cap_setuid = CAP_SETUID;
#endif
if (vflag)
fsrprintf("%s\n", fname);
if (fsync(fd) < 0) {
fsrprintf("sync failed: %s: %s\n", fname, strerror(errno));
return -1;
}
if (statp->bs_size == 0) {
if (vflag)
fsrprintf("%s: zero size, ignoring\n", fname);
return(0);
}
/* Check if a mandatory lock is set on the file to try and
* avoid blocking indefinitely on the reads later. Note that
* someone could still set a mandatory lock after this check
* but before all reads have completed to block fsr reads.
* This change just closes the window a bit.
*/
if ( (statp->bs_mode & S_ISGID) && ( ! (statp->bs_mode&S_IXGRP) ) ) {
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = (off_t)0;
fl.l_len = 0;
if ((fcntl(fd, F_GETLK, &fl)) < 0 ) {
if (vflag)
fsrprintf("locking check failed: %s\n", fname);
return(-1);
}
if (fl.l_type != F_UNLCK) {
/* Mandatory lock is set */
if (vflag)
fsrprintf("mandatory lock: %s: ignoring\n",
fname);
return(-1);
}
}
/* Check if there is room to copy the file */
if ( statvfs64( (fsname == NULL ? fname : fsname), &vfss) < 0) {
fsrprintf(
"unable to get fs stat on %s: %s\n",
fname, strerror(errno));
return (-1);
}
bsize = vfss.f_frsize ? vfss.f_frsize : vfss.f_bsize;
if (statp->bs_size > ((vfss.f_bfree * bsize) - minimumfree)) {
fsrprintf(
"insufficient freespace for: %s: size=%lld: ignoring\n",
fname, statp->bs_size);
return 1;
}
/* Check realtime info */
if ((ioctl(fd, XFS_IOC_FSGETXATTR, &fsx)) < 0) {
fsrprintf("failed to get attrs: %s\n", fname);
return(-1);
}
if (fsx.fsx_xflags == XFS_XFLAG_REALTIME) {
if (xfs_getrt(fd, &vfss) < 0) {
fsrprintf("could not get rt geom for: %s\n", fname);
return(-1);
}
if (statp->bs_size > ((vfss.f_bfree * bsize) - minimumfree)) {
fsrprintf("low on rt free space: %s: ignoring file\n",
fname);
return(-1);
}
do_rt = 1;
}
if ((RealUid != ROOT) && (RealUid != statp->bs_uid)) {
fsrprintf("cannot open: %s: Permission denied\n", fname);
return -1;
}
pid = fork();
if (pid < 0) {
fsrprintf("fork failed: %s\n", strerror(errno));
close(fd);
exit(1);
} else if (pid == 0) {
/*
* Already checked ownership so we're either
* root or our realuid owns the file and can safely
* setuid to the file owner. This is done so
* quotas are maintained for the user. (No group
* quotas so no setgid).
*/
#ifdef HAVE_CAPABILITIES
ocap = cap_acquire(1, &cap_setuid);
#endif
if (setuid(statp->bs_uid) < 0) {
fsrprintf("setuid failed: uid=%d %s: %s\n",
statp->bs_uid, fname, strerror(errno));
#ifdef HAVE_CAPABILITIES
cap_surrender(ocap);
#endif
exit(1);
}
#ifdef HAVE_CAPABILITIES
cap_surrender(ocap);
#endif
error = packfile(fname, tname, fd, statp, do_rt);
exit(error);
} else if (pid > 0) {
wait(&error);
close(fd);
if (WIFEXITED(error))
return(WEXITSTATUS(error));
return -1;
}
/* NOTREACHED */
return 0;
}
/*
* Do the defragmentation of a single file.
* We already are pretty sure we can and want to
* defragment the file. Create the tmp file, copy
* the data (maintaining holes) and call the kernel
* extent swap routinte.
*/
static int
packfile(char *fname, char *tname, int fd, xfs_bstat_t *statp, int do_rt)
{
int tfd;
int srval;
int nextents, extent, cur_nextents, new_nextents;
unsigned blksz_dio;
unsigned dio_min;
struct dioattr dio;
static xfs_swapext_t sx;
struct xfs_flock64 space;
off64_t cnt, pos;
void *fbuf;
int ct, wc, wc_b4;
struct fsxattr tfsx;
char ffname[SMBUFSZ];
int ffd = -1;
/*
* Work out the extent map - nextents will be set to the
* minimum number of extents needed for the file (taking
* into account holes), cur_nextents is the current number
* of extents.
*/
nextents = read_fd_bmap(fd, statp, &cur_nextents);
if ( cur_nextents == 1 || cur_nextents <= nextents ) {
if (vflag)
fsrprintf("%s already fully defragmented.\n", fname);
return 1; /* indicates no change/no error */
}
if (dflag)
fsrprintf("%s extents=%d can_save=%d tmp=%s\n",
fname, cur_nextents, (cur_nextents - nextents),
tname);
if ((tfd = open(tname, openopts, 0666)) < 0) {
if (vflag)
fsrprintf("could not open tmp as uid %d: %s: %s\n",
statp->bs_uid,tname, strerror(errno));
return -1;
}
unlink(tname);
/* Setup extended attributes */
if( statp->bs_xflags & XFS_XFLAG_HASATTR ) {
if (attr_setf(tfd, "X", "X", 1, ATTR_CREATE) != 0) {
fsrprintf("could not set ATTR on tmp: %s:\n", tname);
close(tfd);
return -1;
}
if (dflag)
fsrprintf("%s set temp attr\n", tname);
}
if ((ioctl(tfd, XFS_IOC_DIOINFO, &dio)) < 0 ) {
fsrprintf("could not get I/O info on tmp: %s\n", tname);
close(tfd);
return -1;
}
if (do_rt) {
int rt_textsize = fsgeom.rtextsize * fsgeom.blocksize;
tfsx.fsx_xflags = XFS_XFLAG_REALTIME;
if ((tfsx.fsx_extsize = rt_textsize) <= 0 ) {
fsrprintf("realtime geom not avail for tmp: %s\n", fname);
close(tfd);
return -1;
}
if (ioctl( tfd, XFS_IOC_FSSETXATTR, &tfsx) < 0) {
fsrprintf("could not set rt on tmp: %s\n", tname);
close(tfd);
return -1;
}
}
dio_min = dio.d_miniosz;
if (statp->bs_size <= dio_min)
blksz_dio = dio_min;
else {
blksz_dio = min(dio.d_maxiosz, BUFFER_MAX - pagesize);
if (argv_blksz_dio != 0)
blksz_dio = min(argv_blksz_dio, blksz_dio);
blksz_dio = (min(statp->bs_size, blksz_dio) / dio_min) * dio_min;
}
if (dflag) {
fsrprintf("DEBUG: fsize=%lld blsz_dio=%d d_min=%d d_max=%d pgsz=%d\n",
statp->bs_size, blksz_dio, dio.d_miniosz, dio.d_maxiosz, pagesize);
}
/* Malloc a buffer */
if (! (fbuf = (char *)memalign(dio.d_mem, blksz_dio))) {
fsrprintf("could not allocate buf: %s\n", tname);
close(tfd);
return -1;
}
if (nfrags) {
/* Create new tmp file in same AG as first */
sprintf(ffname, "%s.frag", tname);
/* Open the new file for sync writes */
if ((ffd = open(ffname, openopts, 0666))
< 0) {
fsrprintf("could not open fragfile: %s : %s\n",
ffname, strerror(errno));
return -1;
}
unlink(ffname);
}
/* Loop through block map copying the file. */
for (extent = 0; extent < nextents; extent++) {
pos = outmap[extent].bmv_offset;
if (outmap[extent].bmv_block == -1) {
space.l_whence = 0;
space.l_start = pos;
space.l_len = outmap[extent].bmv_length;
if (ioctl(tfd, XFS_IOC_UNRESVSP64, &space) < 0) {
fsrprintf("could not trunc tmp %s\n",
tname);
}
lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR);
lseek64(fd, outmap[extent].bmv_length, SEEK_CUR);
continue;
} else if (outmap[extent].bmv_length == 0) {
/* to catch holes at the beginning of the file */
continue;
}
if (! nfrags) {
space.l_whence = SEEK_CUR;
space.l_start = 0;
space.l_len = outmap[extent].bmv_length;
if (ioctl(tfd, XFS_IOC_RESVSP64, &space) < 0) {
fsrprintf("could not pre-alloc tmp space: %s\n",
tname);
close(tfd);
free(fbuf);
return -1;
}
}
for (cnt = outmap[extent].bmv_length; cnt > 0;
cnt -= ct, pos += ct) {
if (nfrags && --nfrags) {
ct = min(cnt, dio_min);
} else if (cnt % dio_min == 0) {
ct = min(cnt, blksz_dio);
} else {
ct = min(cnt + dio_min - (cnt % dio_min),
blksz_dio);
}
ct = read(fd, fbuf, ct);
if (ct == 0) {
/* EOF, stop trying to read */
extent = nextents;
break;
}
/* Ensure we do direct I/O to correct block
* boundaries.
*/
if (ct % dio_min != 0) {
wc = ct + dio_min - (ct % dio_min);
} else {
wc = ct;
}
wc_b4 = wc;
if (ct < 0 || ((wc = write(tfd, fbuf, wc)) != wc_b4)) {
if (ct < 0)
fsrprintf("bad read of %d bytes from %s:%s\n",
wc_b4, fname, strerror(errno));
else if (wc < 0)
fsrprintf("bad write of %d bytes to %s: %s\n",
wc_b4, tname, strerror(errno));
else {
/*
* Might be out of space
*
* Try to finish write
*/
int resid = ct-wc;
if ((wc = write(tfd, ((char *)fbuf)+wc,
resid)) == resid) {
/* worked on second attempt? */
continue;
}
else
if (wc < 0) {
fsrprintf("bad write2 of %d bytes to %s: %s\n",
resid, tname, strerror(errno));
} else {
fsrprintf("bad copy to %s\n",
tname);
}
}
free(fbuf);
return -1;
}
if (nfrags) {
/* Do a matching write to the tmp file */
wc = wc_b4;
if (((wc = write(ffd, fbuf, wc)) != wc_b4)) {
fsrprintf("bad write of %d bytes "
"to %s: %s\n",
wc_b4, ffname, strerror(errno));
}
}
}
}
ftruncate64(tfd, statp->bs_size);
if (ffd > 0) close(ffd);
fsync(tfd);
free(fbuf);
sx.sx_stat = *statp; /* struct copy */
sx.sx_version = XFS_SX_VERSION;
sx.sx_fdtarget = fd;
sx.sx_fdtmp = tfd;
sx.sx_offset = 0;
sx.sx_length = statp->bs_size;
/* Check if the extent count improved */
new_nextents = getnextents(tfd);
if (cur_nextents <= new_nextents) {
if (vflag)
fsrprintf("No improvement made: %s\n", fname);
close(tfd);
return 1; /* no change/no error */
}
/* Swap the extents */
srval = xfs_swapext(fd, &sx);
if (srval < 0) {
if (errno == ENOTSUP) {
if (vflag || dflag)
fsrprintf("%s: file type not supported\n", fname);
} else if (errno == EFAULT) {
/* The file has changed since we started the copy */
if (vflag || dflag)
fsrprintf("%s:file modified defrag aborted\n",
fname);
} else if (errno == EBUSY) {
/* Timestamp has changed or mmap'ed file */
if (vflag || dflag)
fsrprintf("%s: file busy \n", fname);
} else {
fsrprintf("XFS_IOC_SWAPEXT failed: %s: %s\n",
fname, strerror(errno));
}
close(tfd);
return -1;
}
/* Report progress */
if (vflag)
fsrprintf("extents before:%d after:%d %s %s\n",
cur_nextents, new_nextents,
(new_nextents <= nextents ? "DONE" : " " ),
fname);
close(tfd);
return 0;
}
char *
gettmpname(char *fname)
{
static char buf[PATH_MAX+1];
char sbuf[SMBUFSZ];
char *ptr;
sprintf(sbuf, "/.fsr%d", getpid());
strcpy(buf, fname);
ptr = strrchr(buf, '/');
if (ptr) {
*ptr = '\0';
} else {
strcpy(buf, ".");
}
if ((strlen(buf) + strlen (sbuf)) > PATH_MAX) {
fsrprintf("tmp file name too long: %s\n", fname);
return(NULL);
}
strcat(buf, sbuf);
return(buf);
}
char *
getparent(char *fname)
{
static char buf[PATH_MAX+1];
char *ptr;
strcpy(buf, fname);
ptr = strrchr(buf, '/');
if (ptr) {
if (ptr == &buf[0])
++ptr;
*ptr = '\0';
} else {
strcpy(buf, ".");
}
return(buf);
}
/*
* Read in block map of the input file, coalesce contiguous
* extents into a single range, keep all holes. Convert from 512 byte
* blocks to bytes.
*
* This code was borrowed from mv.c with some minor mods.
*/
#define MAPSIZE 128
#define OUTMAP_SIZE_INCREMENT MAPSIZE
int read_fd_bmap(int fd, xfs_bstat_t *sin, int *cur_nextents)
{
int i, cnt;
struct getbmap map[MAPSIZE];
#define BUMP_CNT \
if (++cnt >= outmap_size) { \
outmap_size += OUTMAP_SIZE_INCREMENT; \
outmap = (struct getbmap *)realloc(outmap, \
outmap_size*sizeof(*outmap)); \
if (outmap == NULL) { \
fsrprintf("realloc failed: %s\n", \
strerror(errno)); \
exit(1); \
} \
}
/* Initialize the outmap array. It always grows - never shrinks.
* Left-over memory allocation is saved for the next files.
*/
if (outmap_size == 0) {
outmap_size = OUTMAP_SIZE_INCREMENT; /* Initial size */
outmap = (struct getbmap *)malloc(outmap_size*sizeof(*outmap));
if (!outmap) {
fsrprintf("malloc failed: %s\n",
strerror(errno));
exit(1);
}
}
outmap[0].bmv_block = 0;
outmap[0].bmv_offset = 0;
outmap[0].bmv_length = sin->bs_size;
/*
* If a non regular file is involved then forget holes
*/
if (!S_ISREG(sin->bs_mode))
return(1);
outmap[0].bmv_length = 0;
map[0].bmv_offset = 0;
map[0].bmv_block = 0;
map[0].bmv_entries = 0;
map[0].bmv_count = MAPSIZE;
map[0].bmv_length = -1;
cnt = 0;
*cur_nextents = 0;
do {
if (ioctl(fd, XFS_IOC_GETBMAP, map) < 0) {
fsrprintf("failed reading extents: inode %llu",
(unsigned long long)sin->bs_ino);
exit(1);
}
/* Concatenate extents together and replicate holes into
* the output map.
*/
*cur_nextents += map[0].bmv_entries;
for (i = 0; i < map[0].bmv_entries; i++) {
if (map[i + 1].bmv_block == -1) {
BUMP_CNT;
outmap[cnt] = map[i+1];
} else if (outmap[cnt].bmv_block == -1) {
BUMP_CNT;
outmap[cnt] = map[i+1];
} else {
outmap[cnt].bmv_length += map[i + 1].bmv_length;
}
}
} while (map[0].bmv_entries == (MAPSIZE-1));
for (i = 0; i <= cnt; i++) {
outmap[i].bmv_offset = BBTOB(outmap[i].bmv_offset);
outmap[i].bmv_length = BBTOB(outmap[i].bmv_length);
}
outmap[cnt].bmv_length = sin->bs_size - outmap[cnt].bmv_offset;
return(cnt+1);
}
/*
* Read the block map and return the number of extents.
*/
int
getnextents(int fd)
{
int nextents;
struct getbmap map[MAPSIZE];
map[0].bmv_offset = 0;
map[0].bmv_block = 0;
map[0].bmv_entries = 0;
map[0].bmv_count = MAPSIZE;
map[0].bmv_length = -1;
nextents = 0;
do {
if (ioctl(fd,XFS_IOC_GETBMAP, map) < 0) {
fsrprintf("failed reading extents");
exit(1);
}
nextents += map[0].bmv_entries;
} while (map[0].bmv_entries == (MAPSIZE-1));
return(nextents);
}
/*
* Get the fs geometry
*/
int
xfs_getgeom(int fd, xfs_fsop_geom_t * fsgeom)
{
if (xfs_fsgeometry(fd, fsgeom) < 0) {
return -1;
}
return 0;
}
/*
* Get xfs realtime space information
*/
int
xfs_getrt(int fd, struct statvfs64 *sfbp)
{
unsigned long bsize;
unsigned long factor;
xfs_fsop_counts_t cnt;
if (!fsgeom.rtblocks)
return -1;
if (xfs_fscounts(fd, &cnt) < 0) {
close(fd);
return -1;
}
bsize = (sfbp->f_frsize ? sfbp->f_frsize : sfbp->f_bsize);
factor = fsgeom.blocksize / bsize; /* currently this is == 1 */
sfbp->f_bfree = (cnt.freertx * fsgeom.rtextsize) * factor;
return 0;
}
int
fsrprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (gflag) {
static int didopenlog;
if (!didopenlog) {
openlog("fsr", LOG_PID, LOG_USER);
didopenlog = 1;
}
vsyslog(LOG_INFO, fmt, ap);
} else
vprintf(fmt, ap);
va_end(ap);
return 0;
}
/*
* emulate getmntany
*/
static int
getmntany (FILE *filep, struct mntent *mp, struct mntent *mpref)
{
int match = 0;
struct mntent *t = NULL;
while (!match && (t = getmntent(filep)) != 0) {
if (mpref->mnt_fsname != NULL &&
strcmp(mpref->mnt_fsname, t->mnt_fsname) != 0) continue;
if (mpref->mnt_dir != NULL &&
strcmp(mpref->mnt_dir, t->mnt_dir) != 0) continue;
match++;
}
if (match) *mp = *t;
return !match;
}
/*
* Initialize a directory for tmp file use. This is used
* by the full filesystem defragmentation when we're walking
* the inodes and do not know the path for the individual
* files. Multiple directories are used to spread out the
* tmp data around to different ag's (since file data is
* usually allocated to the same ag as the directory and
* directories allocated round robin from the same
* parent directory).
*/
static void
tmp_init(char *mnt)
{
int i;
static char buf[SMBUFSZ];
mode_t mask;
tmp_agi = 0;
sprintf(buf, "%s/.fsr", mnt);
mask = umask(0);
if (mkdir(buf, 0777) < 0) {
if (errno == EEXIST) {
if (dflag)
fsrprintf("tmpdir already exists: %s\n", buf);
} else {
fsrprintf("could not create tmpdir: %s: %s\n",
buf, strerror(errno));
exit(-1);
}
}
for (i=0; i < fsgeom.agcount; i++) {
sprintf(buf, "%s/.fsr/ag%d", mnt, i);
if (mkdir(buf, 0777) < 0) {
if (errno == EEXIST) {
if (dflag)
fsrprintf("tmpdir already exists: %s\n",buf);
} else {
fsrprintf("could not create tmpdir: %s: %s\n",
buf, strerror(errno));
exit(-1);
}
}
}
(void)umask(mask);
return;
}
static char *
tmp_next(char *mnt)
{
static char buf[SMBUFSZ];
sprintf(buf, "%s/.fsr/ag%d/tmp%d",
( (strcmp(mnt, "/") == 0) ? "" : mnt),
tmp_agi,
getpid());
if (++tmp_agi == fsgeom.agcount)
tmp_agi = 0;
return(buf);
}
static void
tmp_close(char *mnt)
{
static char buf[SMBUFSZ];
int i;
/* No data is ever actually written so we can just do rmdir's */
for (i=0; i < fsgeom.agcount; i++) {
sprintf(buf, "%s/.fsr/ag%d", mnt, i);
if (rmdir(buf) < 0) {
if (errno != ENOENT) {
fsrprintf("could not remove tmpdir: %s: %s\n",
buf, strerror(errno));
}
}
}
sprintf(buf, "%s/.fsr", mnt);
if (rmdir(buf) < 0) {
if (errno != ENOENT) {
fsrprintf("could not remove tmpdir: %s: %s\n",
buf, strerror(errno));
}
}
}
|