]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - fsr/xfs_fsr.c
libfrog: refactor open-coded bulkstat calls
[thirdparty/xfsprogs-dev.git] / fsr / xfs_fsr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "xfs.h"
9 #include "xfs_types.h"
10 #include "jdm.h"
11 #include "xfs_bmap_btree.h"
12 #include "xfs_attr_sf.h"
13 #include "path.h"
14 #include "fsgeom.h"
15 #include "libfrog/bulkstat.h"
16
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <syslog.h>
20 #include <signal.h>
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #include <sys/statvfs.h>
24 #include <sys/xattr.h>
25 #include <paths.h>
26
27 #define _PATH_FSRLAST "/var/tmp/.fsrlast_xfs"
28 #define _PATH_PROC_MOUNTS "/proc/mounts"
29
30
31 char *progname;
32
33 static int vflag;
34 static int gflag;
35 static int Mflag;
36 /* static int nflag; */
37 static int dflag = 0;
38 /* static int sflag; */
39 static int argv_blksz_dio;
40 extern int max_ext_size;
41 static int npasses = 10;
42 static int startpass = 0;
43
44 static struct getbmap *outmap = NULL;
45 static int outmap_size = 0;
46 static int RealUid;
47 static int tmp_agi;
48 static int64_t minimumfree = 2048;
49
50 #define MNTTYPE_XFS "xfs"
51
52 #define SMBUFSZ 1024
53 #define ROOT 0
54 #define NULLFD -1
55 #define GRABSZ 64
56 #define TARGETRANGE 10
57 #define BUFFER_MAX (1<<24)
58
59 static time_t howlong = 7200; /* default seconds of reorganizing */
60 static char *leftofffile = _PATH_FSRLAST; /* where we left off last */
61 static time_t endtime;
62 static time_t starttime;
63 static xfs_ino_t leftoffino = 0;
64 static int pagesize;
65
66 void usage(int ret);
67 static int fsrfile(char *fname, xfs_ino_t ino);
68 static int fsrfile_common( char *fname, char *tname, char *mnt,
69 int fd, struct xfs_bstat *statp);
70 static int packfile(char *fname, char *tname, int fd,
71 struct xfs_bstat *statp, struct fsxattr *fsxp);
72 static void fsrdir(char *dirname);
73 static int fsrfs(char *mntdir, xfs_ino_t ino, int targetrange);
74 static void initallfs(char *mtab);
75 static void fsrallfs(char *mtab, int howlong, char *leftofffile);
76 static void fsrall_cleanup(int timeout);
77 static int getnextents(int);
78 int xfsrtextsize(int fd);
79 int xfs_getrt(int fd, struct statvfs *sfbp);
80 char * gettmpname(char *fname);
81 char * getparent(char *fname);
82 int fsrprintf(const char *fmt, ...);
83 int read_fd_bmap(int, struct xfs_bstat *, int *);
84 int cmp(const void *, const void *);
85 static void tmp_init(char *mnt);
86 static char * tmp_next(char *mnt);
87 static void tmp_close(char *mnt);
88
89 static struct xfs_fsop_geom fsgeom; /* geometry of active mounted system */
90
91 #define NMOUNT 64
92 static int numfs;
93
94 typedef struct fsdesc {
95 char *dev;
96 char *mnt;
97 int npass;
98 } fsdesc_t;
99
100 static fsdesc_t *fs, *fsbase, *fsend;
101 static int fsbufsize = 10; /* A starting value */
102 static int nfrags = 0; /* Debug option: Coerse into specific number
103 * of extents */
104 static int openopts = O_CREAT|O_EXCL|O_RDWR|O_DIRECT;
105
106 static int
107 xfs_swapext(int fd, xfs_swapext_t *sx)
108 {
109 return ioctl(fd, XFS_IOC_SWAPEXT, sx);
110 }
111
112 static int
113 xfs_fscounts(int fd, xfs_fsop_counts_t *counts)
114 {
115 return ioctl(fd, XFS_IOC_FSCOUNTS, counts);
116 }
117
118 static void
119 aborter(int unused)
120 {
121 fsrall_cleanup(1);
122 exit(1);
123 }
124
125 int
126 main(int argc, char **argv)
127 {
128 struct stat sb;
129 char *argname;
130 int c;
131 struct fs_path *fsp;
132 char *mtab = NULL;
133
134 setlinebuf(stdout);
135 progname = basename(argv[0]);
136
137 setlocale(LC_ALL, "");
138 bindtextdomain(PACKAGE, LOCALEDIR);
139 textdomain(PACKAGE);
140
141 gflag = ! isatty(0);
142
143 while ((c = getopt(argc, argv, "C:p:e:MgsdnvTt:f:m:b:N:FV")) != -1) {
144 switch (c) {
145 case 'M':
146 Mflag = 1;
147 break;
148 case 'g':
149 gflag = 1;
150 break;
151 case 'n':
152 /* nflag = 1; */
153 break;
154 case 'v':
155 ++vflag;
156 break;
157 case 'd':
158 dflag = 1;
159 break;
160 case 's': /* frag stats only */
161 /* sflag = 1; */
162 fprintf(stderr,
163 _("%s: Stats not yet supported for XFS\n"),
164 progname);
165 usage(1);
166 break;
167 case 't':
168 howlong = atoi(optarg);
169 break;
170 case 'f':
171 leftofffile = optarg;
172 break;
173 case 'm':
174 mtab = optarg;
175 break;
176 case 'b':
177 argv_blksz_dio = atoi(optarg);
178 break;
179 case 'p':
180 npasses = atoi(optarg);
181 break;
182 case 'C':
183 /* Testing opt: coerses frag count in result */
184 if (getenv("FSRXFSTEST") != NULL) {
185 nfrags = atoi(optarg);
186 openopts |= O_SYNC;
187 }
188 break;
189 case 'V':
190 printf(_("%s version %s\n"), progname, VERSION);
191 exit(0);
192 default:
193 usage(1);
194 }
195 }
196
197 /*
198 * If the user did not specify an explicit mount table, try to use
199 * /proc/mounts if it is available, else /etc/mtab. We prefer
200 * /proc/mounts because it is kernel controlled, while /etc/mtab
201 * may contain garbage that userspace tools like pam_mounts wrote
202 * into it.
203 */
204 if (!mtab) {
205 if (access(_PATH_PROC_MOUNTS, R_OK) == 0)
206 mtab = _PATH_PROC_MOUNTS;
207 else
208 mtab = _PATH_MOUNTED;
209 }
210
211 if (vflag)
212 setbuf(stdout, NULL);
213
214 starttime = time(NULL);
215
216 /* Save the caller's real uid */
217 RealUid = getuid();
218
219 pagesize = getpagesize();
220 fs_table_initialise(0, NULL, 0, NULL);
221 if (optind < argc) {
222 for (; optind < argc; optind++) {
223 argname = argv[optind];
224
225 if (lstat(argname, &sb) < 0) {
226 fprintf(stderr,
227 _("%s: could not stat: %s: %s\n"),
228 progname, argname, strerror(errno));
229 continue;
230 }
231
232 if (S_ISLNK(sb.st_mode)) {
233 struct stat sb2;
234
235 if (stat(argname, &sb2) == 0 &&
236 (S_ISBLK(sb2.st_mode) ||
237 S_ISCHR(sb2.st_mode)))
238 sb = sb2;
239 }
240
241 fsp = fs_table_lookup_mount(argname);
242 if (!fsp)
243 fsp = fs_table_lookup_blkdev(argname);
244 if (fsp != NULL) {
245 fsrfs(fsp->fs_dir, 0, 100);
246 } else if (S_ISCHR(sb.st_mode)) {
247 fprintf(stderr, _(
248 "%s: char special not supported: %s\n"),
249 progname, argname);
250 exit(1);
251 } else if (S_ISDIR(sb.st_mode) || S_ISREG(sb.st_mode)) {
252 if (!platform_test_xfs_path(argname)) {
253 fprintf(stderr, _(
254 "%s: cannot defragment: %s: Not XFS\n"),
255 progname, argname);
256 continue;
257 }
258 if (S_ISDIR(sb.st_mode))
259 fsrdir(argname);
260 else
261 fsrfile(argname, sb.st_ino);
262 } else {
263 printf(
264 _("%s: not fsys dev, dir, or reg file, ignoring\n"),
265 argname);
266 }
267 }
268 } else {
269 initallfs(mtab);
270 fsrallfs(mtab, howlong, leftofffile);
271 }
272 return 0;
273 }
274
275 void
276 usage(int ret)
277 {
278 fprintf(stderr, _(
279 "Usage: %s [-d] [-v] [-g] [-t time] [-p passes] [-f leftf] [-m mtab]\n"
280 " %s [-d] [-v] [-g] xfsdev | dir | file ...\n"
281 " %s -V\n\n"
282 "Options:\n"
283 " -g Print to syslog (default if stdout not a tty).\n"
284 " -t time How long to run in seconds.\n"
285 " -p passes Number of passes before terminating global re-org.\n"
286 " -f leftoff Use this instead of %s.\n"
287 " -m mtab Use something other than /etc/mtab.\n"
288 " -d Debug, print even more.\n"
289 " -v Verbose, more -v's more verbose.\n"
290 " -V Print version number and exit.\n"
291 ), progname, progname, progname, _PATH_FSRLAST);
292 exit(ret);
293 }
294
295 /*
296 * initallfs -- read the mount table and set up an internal form
297 */
298 static void
299 initallfs(char *mtab)
300 {
301 struct mntent_cursor cursor;
302 struct mntent *mnt= NULL;
303 int mi;
304 char *cp;
305 struct stat sb;
306
307 /* malloc a number of descriptors, increased later if needed */
308 if (!(fsbase = (fsdesc_t *)malloc(fsbufsize * sizeof(fsdesc_t)))) {
309 fsrprintf(_("out of memory: %s\n"), strerror(errno));
310 exit(1);
311 }
312 fsend = (fsbase + fsbufsize - 1);
313
314 /* find all rw xfs file systems */
315 mi = 0;
316 fs = fsbase;
317
318 if (platform_mntent_open(&cursor, mtab) != 0){
319 fprintf(stderr, "Error: can't get mntent entries.\n");
320 exit(1);
321 }
322
323 while ((mnt = platform_mntent_next(&cursor)) != NULL) {
324 int rw = 0;
325
326 if (strcmp(mnt->mnt_type, MNTTYPE_XFS ) != 0 ||
327 stat(mnt->mnt_fsname, &sb) == -1 ||
328 !S_ISBLK(sb.st_mode))
329 continue;
330
331 cp = strtok(mnt->mnt_opts,",");
332 do {
333 if (strcmp("rw", cp) == 0)
334 rw++;
335 } while ((cp = strtok(NULL, ",")) != NULL);
336 if (rw == 0) {
337 if (dflag)
338 fsrprintf(_("Skipping %s: not mounted rw\n"),
339 mnt->mnt_fsname);
340 continue;
341 }
342
343 if (mi == fsbufsize) {
344 fsbufsize += NMOUNT;
345 if ((fsbase = (fsdesc_t *)realloc((char *)fsbase,
346 fsbufsize * sizeof(fsdesc_t))) == NULL) {
347 fsrprintf(_("out of memory: %s\n"),
348 strerror(errno));
349 exit(1);
350 }
351 if (!fsbase) {
352 fsrprintf(_("out of memory on realloc: %s\n"),
353 strerror(errno));
354 exit(1);
355 }
356 fs = (fsbase + mi); /* Needed ? */
357 }
358
359 fs->dev = strdup(mnt->mnt_fsname);
360 fs->mnt = strdup(mnt->mnt_dir);
361
362 if (fs->dev == NULL) {
363 fsrprintf(_("strdup(%s) failed\n"), mnt->mnt_fsname);
364 exit(1);
365 }
366 if (fs->mnt == NULL) {
367 fsrprintf(_("strdup(%s) failed\n"), mnt->mnt_dir);
368 exit(1);
369 }
370 mi++;
371 fs++;
372 }
373 platform_mntent_close(&cursor);
374
375 numfs = mi;
376 fsend = (fsbase + numfs);
377 if (numfs == 0) {
378 fsrprintf(_("no rw xfs file systems in mtab: %s\n"), mtab);
379 exit(0);
380 }
381 if (vflag || dflag) {
382 fsrprintf(_("Found %d mounted, writable, XFS filesystems\n"),
383 numfs);
384 if (dflag)
385 for (fs = fsbase; fs < fsend; fs++)
386 fsrprintf("\t%-30.30s%-30.30s\n", fs->dev, fs->mnt);
387 }
388 }
389
390 static void
391 fsrallfs(char *mtab, int howlong, char *leftofffile)
392 {
393 int fd;
394 int error;
395 int found = 0;
396 char *fsname;
397 char buf[SMBUFSZ];
398 int mdonly = Mflag;
399 char *ptr;
400 xfs_ino_t startino = 0;
401 fsdesc_t *fsp;
402 struct stat sb, sb2;
403
404 fsrprintf("xfs_fsr -m %s -t %d -f %s ...\n", mtab, howlong, leftofffile);
405
406 endtime = starttime + howlong;
407 fs = fsbase;
408
409 /* where'd we leave off last time? */
410 if (lstat(leftofffile, &sb) == 0) {
411 if ( (fd = open(leftofffile, O_RDONLY)) == -1 ) {
412 fsrprintf(_("%s: open failed\n"), leftofffile);
413 }
414 else if ( fstat(fd, &sb2) == 0) {
415 /*
416 * Verify that lstat & fstat point to the
417 * same regular file (no links/no quick spoofs)
418 */
419 if ( (sb.st_dev != sb2.st_dev) ||
420 (sb.st_ino != sb2.st_ino) ||
421 ((sb.st_mode & S_IFMT) != S_IFREG) ||
422 ((sb2.st_mode & S_IFMT) != S_IFREG) ||
423 (sb2.st_uid != ROOT) ||
424 (sb2.st_nlink != 1)
425 )
426 {
427 fsrprintf(_("Can't use %s: mode=0%o own=%d"
428 " nlink=%d\n"),
429 leftofffile, sb.st_mode,
430 sb.st_uid, sb.st_nlink);
431 close(fd);
432 fd = NULLFD;
433 }
434 }
435 else {
436 close(fd);
437 fd = NULLFD;
438 }
439 }
440 else {
441 fd = NULLFD;
442 }
443
444 if (fd != NULLFD) {
445 if (read(fd, buf, SMBUFSZ) == -1) {
446 fs = fsbase;
447 fsrprintf(_("could not read %s, starting with %s\n"),
448 leftofffile, *fs->dev);
449 } else {
450 /* Ensure the buffer we read is null terminated */
451 buf[SMBUFSZ-1] = '\0';
452 for (fs = fsbase; fs < fsend; fs++) {
453 fsname = fs->dev;
454 if ((strncmp(buf,fsname,strlen(fsname)) == 0)
455 && buf[strlen(fsname)] == ' ') {
456 found = 1;
457 break;
458 }
459 }
460 if (! found)
461 fs = fsbase;
462
463 ptr = strchr(buf, ' ');
464 if (ptr) {
465 startpass = atoi(++ptr);
466 ptr = strchr(ptr, ' ');
467 if (ptr) {
468 startino = strtoull(++ptr, NULL, 10);
469 }
470 }
471 if (startpass < 0)
472 startpass = 0;
473
474 /* Init pass counts */
475 for (fsp = fsbase; fsp < fs; fsp++) {
476 fsp->npass = startpass + 1;
477 }
478 for (fsp = fs; fsp <= fsend; fsp++) {
479 fsp->npass = startpass;
480 }
481 }
482 close(fd);
483 }
484
485 if (vflag) {
486 fsrprintf(_("START: pass=%d ino=%llu %s %s\n"),
487 fs->npass, (unsigned long long)startino,
488 fs->dev, fs->mnt);
489 }
490
491 signal(SIGABRT, aborter);
492 signal(SIGHUP, aborter);
493 signal(SIGINT, aborter);
494 signal(SIGQUIT, aborter);
495 signal(SIGTERM, aborter);
496
497 /* reorg for 'howlong' -- checked in 'fsrfs' */
498 while (endtime > time(NULL)) {
499 pid_t pid;
500
501 if (npasses > 1 && !fs->npass)
502 Mflag = 1;
503 else
504 Mflag = mdonly;
505 pid = fork();
506 switch(pid) {
507 case -1:
508 fsrprintf(_("couldn't fork sub process:"));
509 exit(1);
510 break;
511 case 0:
512 error = fsrfs(fs->mnt, startino, TARGETRANGE);
513 exit (error);
514 break;
515 default:
516 wait(&error);
517 if (WIFEXITED(error) && WEXITSTATUS(error) == 1) {
518 /* child timed out & did fsrall_cleanup */
519 exit(0);
520 }
521 break;
522 }
523 startino = 0; /* reset after the first time through */
524 fs->npass++;
525 fs++;
526 if (fs == fsend)
527 fs = fsbase;
528 if (fs->npass == npasses) {
529 fsrprintf(_("Completed all %d passes\n"), npasses);
530 break;
531 }
532 }
533 fsrall_cleanup(endtime <= time(NULL));
534 }
535
536 /*
537 * fsrall_cleanup -- close files, print next starting location, etc.
538 */
539 static void
540 fsrall_cleanup(int timeout)
541 {
542 int fd;
543 int ret;
544 char buf[SMBUFSZ];
545
546 unlink(leftofffile);
547
548 if (timeout) {
549 fsrprintf(_("%s startpass %d, endpass %d, time %d seconds\n"),
550 progname, startpass, fs->npass,
551 time(NULL) - endtime + howlong);
552
553 /* record where we left off */
554 fd = open(leftofffile, O_WRONLY|O_CREAT|O_EXCL, 0644);
555 if (fd == -1) {
556 fsrprintf(_("open(%s) failed: %s\n"),
557 leftofffile, strerror(errno));
558 } else {
559 ret = sprintf(buf, "%s %d %llu\n", fs->dev,
560 fs->npass, (unsigned long long)leftoffino);
561 if (write(fd, buf, ret) < strlen(buf))
562 fsrprintf(_("write(%s) failed: %s\n"),
563 leftofffile, strerror(errno));
564 close(fd);
565 }
566 }
567 }
568
569 /*
570 * fsrfs -- reorganize a file system
571 */
572 static int
573 fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
574 {
575 struct xfs_fd fsxfd = XFS_FD_INIT_EMPTY;
576 int fd;
577 int count = 0;
578 int ret;
579 uint32_t buflenout;
580 struct xfs_bstat buf[GRABSZ];
581 char fname[64];
582 char *tname;
583 jdm_fshandle_t *fshandlep;
584 xfs_ino_t lastino = startino;
585
586 fsrprintf(_("%s start inode=%llu\n"), mntdir,
587 (unsigned long long)startino);
588
589 fshandlep = jdm_getfshandle( mntdir );
590 if ( ! fshandlep ) {
591 fsrprintf(_("unable to get handle: %s: %s\n"),
592 mntdir, strerror( errno ));
593 return -1;
594 }
595
596 if ((fsxfd.fd = open(mntdir, O_RDONLY)) < 0) {
597 fsrprintf(_("unable to open: %s: %s\n"),
598 mntdir, strerror( errno ));
599 free(fshandlep);
600 return -1;
601 }
602
603 ret = xfd_prepare_geometry(&fsxfd);
604 if (ret) {
605 fsrprintf(_("Skipping %s: could not get XFS geometry\n"),
606 mntdir);
607 xfd_close(&fsxfd);
608 free(fshandlep);
609 return -1;
610 }
611 memcpy(&fsgeom, &fsxfd.fsgeom, sizeof(fsgeom));
612
613 tmp_init(mntdir);
614
615 while ((ret = xfrog_bulkstat(&fsxfd, &lastino, GRABSZ, &buf[0],
616 &buflenout)) == 0) {
617 struct xfs_bstat *p;
618 struct xfs_bstat *endp;
619
620 if (buflenout == 0)
621 goto out0;
622
623 /* Each loop through, defrag targetrange percent of the files */
624 count = (buflenout * targetrange) / 100;
625
626 qsort((char *)buf, buflenout, sizeof(struct xfs_bstat), cmp);
627
628 for (p = buf, endp = (buf + buflenout); p < endp ; p++) {
629 /* Do some obvious checks now */
630 if (((p->bs_mode & S_IFMT) != S_IFREG) ||
631 (p->bs_extents < 2))
632 continue;
633
634 fd = jdm_open(fshandlep, p, O_RDWR|O_DIRECT);
635 if (fd < 0) {
636 /* This probably means the file was
637 * removed while in progress of handling
638 * it. Just quietly ignore this file.
639 */
640 if (dflag)
641 fsrprintf(_("could not open: "
642 "inode %llu\n"), p->bs_ino);
643 continue;
644 }
645
646 /* Don't know the pathname, so make up something */
647 sprintf(fname, "ino=%lld", (long long)p->bs_ino);
648
649 /* Get a tmp file name */
650 tname = tmp_next(mntdir);
651
652 ret = fsrfile_common(fname, tname, mntdir, fd, p);
653
654 leftoffino = p->bs_ino;
655
656 close(fd);
657
658 if (ret == 0) {
659 if (--count <= 0)
660 break;
661 }
662 }
663 if (endtime && endtime < time(NULL)) {
664 tmp_close(mntdir);
665 xfd_close(&fsxfd);
666 fsrall_cleanup(1);
667 exit(1);
668 }
669 }
670 if (ret)
671 fsrprintf(_("%s: bulkstat: %s\n"), progname, strerror(ret));
672 out0:
673 tmp_close(mntdir);
674 xfd_close(&fsxfd);
675 free(fshandlep);
676 return 0;
677 }
678
679 /*
680 * To compare bstat structs for qsort.
681 */
682 int
683 cmp(const void *s1, const void *s2)
684 {
685 return( ((struct xfs_bstat *)s2)->bs_extents -
686 ((struct xfs_bstat *)s1)->bs_extents);
687
688 }
689
690 /*
691 * reorganize by directory hierarchy.
692 * Stay in dev (a restriction based on structure of this program -- either
693 * call efs_{n,u}mount() around each file, something smarter or this)
694 */
695 static void
696 fsrdir(char *dirname)
697 {
698 fsrprintf(_("%s: Directory defragmentation not supported\n"), dirname);
699 }
700
701 /*
702 * Sets up the defragmentation of a file based on the
703 * filepath. It collects the bstat information, does
704 * an open on the file and passes this all to fsrfile_common.
705 */
706 static int
707 fsrfile(
708 char *fname,
709 xfs_ino_t ino)
710 {
711 struct xfs_fd fsxfd = XFS_FD_INIT_EMPTY;
712 struct xfs_bstat statbuf;
713 jdm_fshandle_t *fshandlep;
714 int fd = -1;
715 int error = -1;
716 char *tname;
717
718 fshandlep = jdm_getfshandle(getparent (fname) );
719 if (!fshandlep) {
720 fsrprintf(_("unable to construct sys handle for %s: %s\n"),
721 fname, strerror(errno));
722 goto out;
723 }
724
725 /*
726 * Need to open something on the same filesystem as the
727 * file. Open the parent.
728 */
729 fsxfd.fd = open(getparent(fname), O_RDONLY);
730 if (fsxfd.fd < 0) {
731 fsrprintf(_("unable to open sys handle for %s: %s\n"),
732 fname, strerror(errno));
733 goto out;
734 }
735
736 error = xfd_prepare_geometry(&fsxfd);
737 if (error) {
738 fsrprintf(_("Unable to get geom on fs for: %s\n"), fname);
739 goto out;
740 }
741
742 error = xfrog_bulkstat_single(&fsxfd, ino, &statbuf);
743 if (error) {
744 fsrprintf(_("unable to get bstat on %s: %s\n"),
745 fname, strerror(error));
746 goto out;
747 }
748
749 fd = jdm_open(fshandlep, &statbuf, O_RDWR|O_DIRECT);
750 if (fd < 0) {
751 fsrprintf(_("unable to open handle %s: %s\n"),
752 fname, strerror(errno));
753 goto out;
754 }
755
756 /* Stash the fs geometry for general use. */
757 memcpy(&fsgeom, &fsxfd.fsgeom, sizeof(fsgeom));
758
759 tname = gettmpname(fname);
760
761 if (tname)
762 error = fsrfile_common(fname, tname, NULL, fd, &statbuf);
763
764 out:
765 xfd_close(&fsxfd);
766 if (fd >= 0)
767 close(fd);
768 free(fshandlep);
769
770 return error;
771 }
772
773
774 /*
775 * This is the common defrag code for either a full fs
776 * defragmentation or a single file. Check as much as
777 * possible with the file, fork a process to setuid to the
778 * target file owner's uid and defragment the file.
779 * This is done so the new extents created in a tmp file are
780 * reflected in the owners' quota without having to do any
781 * special code in the kernel. When the existing extents
782 * are removed, the quotas will be correct. It's ugly but
783 * it saves us from doing some quota re-construction in
784 * the extent swap. The price is that the defragmentation
785 * will fail if the owner of the target file is already at
786 * their quota limit.
787 */
788 static int
789 fsrfile_common(
790 char *fname,
791 char *tname,
792 char *fsname,
793 int fd,
794 struct xfs_bstat *statp)
795 {
796 int error;
797 struct statvfs vfss;
798 struct fsxattr fsx;
799 unsigned long bsize;
800
801 if (vflag)
802 fsrprintf("%s\n", fname);
803
804 if (fsync(fd) < 0) {
805 fsrprintf(_("sync failed: %s: %s\n"), fname, strerror(errno));
806 return -1;
807 }
808
809 if (statp->bs_size == 0) {
810 if (vflag)
811 fsrprintf(_("%s: zero size, ignoring\n"), fname);
812 return(0);
813 }
814
815 /* Check if a mandatory lock is set on the file to try and
816 * avoid blocking indefinitely on the reads later. Note that
817 * someone could still set a mandatory lock after this check
818 * but before all reads have completed to block fsr reads.
819 * This change just closes the window a bit.
820 */
821 if ( (statp->bs_mode & S_ISGID) && ( ! (statp->bs_mode&S_IXGRP) ) ) {
822 struct flock fl;
823
824 fl.l_type = F_RDLCK;
825 fl.l_whence = SEEK_SET;
826 fl.l_start = (off_t)0;
827 fl.l_len = 0;
828 if ((fcntl(fd, F_GETLK, &fl)) < 0 ) {
829 if (vflag)
830 fsrprintf(_("locking check failed: %s\n"),
831 fname);
832 return(-1);
833 }
834 if (fl.l_type != F_UNLCK) {
835 /* Mandatory lock is set */
836 if (vflag)
837 fsrprintf(_("mandatory lock: %s: ignoring\n"),
838 fname);
839 return(-1);
840 }
841 }
842
843 /*
844 * Check if there is room to copy the file.
845 *
846 * Note that xfs_bstat.bs_blksize returns the filesystem blocksize,
847 * not the optimal I/O size as struct stat.
848 */
849 if (statvfs(fsname ? fsname : fname, &vfss) < 0) {
850 fsrprintf(_("unable to get fs stat on %s: %s\n"),
851 fname, strerror(errno));
852 return -1;
853 }
854 bsize = vfss.f_frsize ? vfss.f_frsize : vfss.f_bsize;
855 if (statp->bs_blksize * statp->bs_blocks >
856 vfss.f_bfree * bsize - minimumfree) {
857 fsrprintf(_("insufficient freespace for: %s: "
858 "size=%lld: ignoring\n"), fname,
859 statp->bs_blksize * statp->bs_blocks);
860 return 1;
861 }
862
863 if ((ioctl(fd, FS_IOC_FSGETXATTR, &fsx)) < 0) {
864 fsrprintf(_("failed to get inode attrs: %s\n"), fname);
865 return(-1);
866 }
867 if (fsx.fsx_xflags & (FS_XFLAG_IMMUTABLE|FS_XFLAG_APPEND)) {
868 if (vflag)
869 fsrprintf(_("%s: immutable/append, ignoring\n"), fname);
870 return(0);
871 }
872 if (fsx.fsx_xflags & FS_XFLAG_NODEFRAG) {
873 if (vflag)
874 fsrprintf(_("%s: marked as don't defrag, ignoring\n"),
875 fname);
876 return(0);
877 }
878 if (fsx.fsx_xflags & FS_XFLAG_REALTIME) {
879 if (xfs_getrt(fd, &vfss) < 0) {
880 fsrprintf(_("cannot get realtime geometry for: %s\n"),
881 fname);
882 return(-1);
883 }
884 if (statp->bs_size > ((vfss.f_bfree * bsize) - minimumfree)) {
885 fsrprintf(_("low on realtime free space: %s: "
886 "ignoring file\n"), fname);
887 return(-1);
888 }
889 }
890
891 if ((RealUid != ROOT) && (RealUid != statp->bs_uid)) {
892 fsrprintf(_("cannot open: %s: Permission denied\n"), fname);
893 return -1;
894 }
895
896 /*
897 * Previously the code forked here, & the child changed it's uid to
898 * that of the file's owner and then called packfile(), to keep
899 * quota counts correct. (defragged files could use fewer blocks).
900 *
901 * Instead, just fchown() the temp file to the uid,gid of the
902 * file we're defragging, in packfile().
903 */
904
905 if ((error = packfile(fname, tname, fd, statp, &fsx)))
906 return error;
907 return -1; /* no error */
908 }
909
910 /*
911 * Attempt to set the attr fork up correctly. This is simple for attr1
912 * filesystems as they have a fixed inode fork offset. In that case
913 * just create an attribute and that's all we need to do.
914 *
915 * For attr2 filesystems, see if we have the actual fork offset in
916 * the bstat structure. If so, just create additional attributes on
917 * the temporary inode until the offset matches.
918 *
919 * If it doesn't exist, we can only do best effort. Add an attribute at a time
920 * to move the inode fork around, but take into account that the attribute
921 * might be too small to move the fork every time we add one. This should
922 * hopefully put the fork offset in the right place. It's not a big deal if we
923 * don't get it right - the kernel will reject it when we try to swap extents.
924 */
925 static int
926 fsr_setup_attr_fork(
927 int fd,
928 int tfd,
929 struct xfs_bstat *bstatp)
930 {
931 #ifdef HAVE_FSETXATTR
932 struct xfs_fd txfd = XFS_FD_INIT(tfd);
933 struct stat tstatbuf;
934 int i;
935 int diff = 0;
936 int last_forkoff = 0;
937 int no_change_cnt = 0;
938 int ret;
939
940 if (!(bstatp->bs_xflags & FS_XFLAG_HASATTR))
941 return 0;
942
943 /*
944 * use the old method if we have attr1 or the kernel does not yet
945 * support passing the fork offset in the bulkstat data.
946 */
947 if (!(fsgeom.flags & XFS_FSOP_GEOM_FLAGS_ATTR2) ||
948 bstatp->bs_forkoff == 0) {
949 /* attr1 */
950 ret = fsetxattr(txfd.fd, "user.X", "X", 1, XATTR_CREATE);
951 if (ret) {
952 fsrprintf(_("could not set ATTR\n"));
953 return -1;
954 }
955 goto out;
956 }
957
958 /* attr2 w/ fork offsets */
959
960 if (fstat(txfd.fd, &tstatbuf) < 0) {
961 fsrprintf(_("unable to stat temp file: %s\n"),
962 strerror(errno));
963 return -1;
964 }
965
966 i = 0;
967 do {
968 struct xfs_bstat tbstat;
969 char name[64];
970 int ret;
971
972 /*
973 * bulkstat the temp inode to see what the forkoff is. Use
974 * this to compare against the target and determine what we
975 * need to do.
976 */
977 ret = xfrog_bulkstat_single(&txfd, tstatbuf.st_ino, &tbstat);
978 if (ret) {
979 fsrprintf(_("unable to get bstat on temp file: %s\n"),
980 strerror(ret));
981 return -1;
982 }
983 if (dflag)
984 fsrprintf(_("orig forkoff %d, temp forkoff %d\n"),
985 bstatp->bs_forkoff, tbstat.bs_forkoff);
986 diff = tbstat.bs_forkoff - bstatp->bs_forkoff;
987
988 /* if they are equal, we are done */
989 if (!diff)
990 goto out;
991
992 snprintf(name, sizeof(name), "user.%d", i);
993
994 /*
995 * If there is no attribute, then we need to create one to get
996 * an attribute fork at the default location.
997 */
998 if (!tbstat.bs_forkoff) {
999 ASSERT(i == 0);
1000 ret = fsetxattr(txfd.fd, name, "XX", 2, XATTR_CREATE);
1001 if (ret) {
1002 fsrprintf(_("could not set ATTR\n"));
1003 return -1;
1004 }
1005 continue;
1006 } else if (i == 0) {
1007 /*
1008 * First pass, and temp file already has an inline
1009 * xattr, probably due to selinux.
1010 *
1011 * It's *possible* that the temp file attr area
1012 * is larger than the target file's:
1013 *
1014 * Target Temp
1015 * +-------+ 0 +-------+ 0
1016 * | | | |
1017 * | | | Data |
1018 * | Data | | |
1019 * | | v-------v forkoff
1020 * | | | |
1021 * v-------v forkoff | Attr | local
1022 * | Attr | | |
1023 * +-------+ +-------+
1024 */
1025
1026 /*
1027 * If target attr area is less than the temp's
1028 * (diff < 0) write a big attr to the temp file to knock
1029 * the attr out of local format.
1030 * (This should actually *increase* the temp file's
1031 * forkoffset when the attr moves out of the inode)
1032 */
1033 if (diff < 0) {
1034 char val[2048];
1035 memset(val, 'X', 2048);
1036 if (fsetxattr(txfd.fd, name, val, 2048, 0)) {
1037 fsrprintf(_("big ATTR set failed\n"));
1038 return -1;
1039 }
1040 /* Go back & see where we're at now */
1041 continue;
1042 }
1043 }
1044
1045 /*
1046 * make a progress check so we don't get stuck trying to extend
1047 * a large btree form attribute fork.
1048 */
1049 if (last_forkoff == tbstat.bs_forkoff) {
1050 if (no_change_cnt++ > 10)
1051 break;
1052 } else /* progress! */
1053 no_change_cnt = 0;
1054 last_forkoff = tbstat.bs_forkoff;
1055
1056 /* work out which way to grow the fork */
1057 if (abs(diff) > fsgeom.inodesize - sizeof(struct xfs_dinode)) {
1058 fsrprintf(_("forkoff diff %d too large!\n"), diff);
1059 return -1;
1060 }
1061
1062 /*
1063 * if the temp inode fork offset is still smaller then we have
1064 * to grow the data fork
1065 */
1066 if (diff < 0) {
1067 /*
1068 * create some temporary extents in the inode to move
1069 * the fork in the direction we need. This can be done
1070 * by preallocating some single block extents at
1071 * non-contiguous offsets.
1072 */
1073 /* XXX: unimplemented! */
1074 if (dflag)
1075 printf(_("data fork growth unimplemented\n"));
1076 goto out;
1077 }
1078
1079 /* we need to grow the attr fork, so create another attr */
1080 ret = fsetxattr(txfd.fd, name, "XX", 2, XATTR_CREATE);
1081 if (ret) {
1082 fsrprintf(_("could not set ATTR\n"));
1083 return -1;
1084 }
1085
1086 } while (++i < 100); /* don't go forever */
1087
1088 out:
1089 if (dflag)
1090 fsrprintf(_("set temp attr\n"));
1091 /* We failed to resolve the fork difference */
1092 if (dflag && diff)
1093 fsrprintf(_("failed to match fork offset\n"));;
1094
1095 #endif /* HAVE_FSETXATTR */
1096 return 0;
1097 }
1098
1099 /*
1100 * Do the defragmentation of a single file.
1101 * We already are pretty sure we can and want to
1102 * defragment the file. Create the tmp file, copy
1103 * the data (maintaining holes) and call the kernel
1104 * extent swap routine.
1105 *
1106 * Return values:
1107 * -1: Some error was encountered
1108 * 0: Successfully defragmented the file
1109 * 1: No change / No Error
1110 */
1111 static int
1112 packfile(char *fname, char *tname, int fd,
1113 struct xfs_bstat *statp, struct fsxattr *fsxp)
1114 {
1115 int tfd = -1;
1116 int srval;
1117 int retval = -1; /* Failure is the default */
1118 int nextents, extent, cur_nextents, new_nextents;
1119 unsigned blksz_dio;
1120 unsigned dio_min;
1121 struct dioattr dio;
1122 static xfs_swapext_t sx;
1123 struct xfs_flock64 space;
1124 off64_t cnt, pos;
1125 void *fbuf = NULL;
1126 int ct, wc, wc_b4;
1127 char ffname[SMBUFSZ];
1128 int ffd = -1;
1129
1130 /*
1131 * Work out the extent map - nextents will be set to the
1132 * minimum number of extents needed for the file (taking
1133 * into account holes), cur_nextents is the current number
1134 * of extents.
1135 */
1136 nextents = read_fd_bmap(fd, statp, &cur_nextents);
1137
1138 if (cur_nextents == 1 || cur_nextents <= nextents) {
1139 if (vflag)
1140 fsrprintf(_("%s already fully defragmented.\n"), fname);
1141 retval = 1; /* indicates no change/no error */
1142 goto out;
1143 }
1144
1145 if (dflag)
1146 fsrprintf(_("%s extents=%d can_save=%d tmp=%s\n"),
1147 fname, cur_nextents, (cur_nextents - nextents),
1148 tname);
1149
1150 if ((tfd = open(tname, openopts, 0666)) < 0) {
1151 if (vflag)
1152 fsrprintf(_("could not open tmp file: %s: %s\n"),
1153 tname, strerror(errno));
1154 goto out;
1155 }
1156 unlink(tname);
1157
1158 /* Setup extended attributes */
1159 if (fsr_setup_attr_fork(fd, tfd, statp) != 0) {
1160 fsrprintf(_("failed to set ATTR fork on tmp: %s:\n"), tname);
1161 goto out;
1162 }
1163
1164 /* Setup extended inode flags, project identifier, etc */
1165 if (fsxp->fsx_xflags || fsxp->fsx_projid) {
1166 if (ioctl(tfd, FS_IOC_FSSETXATTR, fsxp) < 0) {
1167 fsrprintf(_("could not set inode attrs on tmp: %s\n"),
1168 tname);
1169 goto out;
1170 }
1171 }
1172
1173 if ((ioctl(tfd, XFS_IOC_DIOINFO, &dio)) < 0 ) {
1174 fsrprintf(_("could not get DirectIO info on tmp: %s\n"), tname);
1175 goto out;
1176 }
1177
1178 dio_min = dio.d_miniosz;
1179 if (statp->bs_size <= dio_min) {
1180 blksz_dio = dio_min;
1181 } else {
1182 blksz_dio = min(dio.d_maxiosz, BUFFER_MAX - pagesize);
1183 if (argv_blksz_dio != 0)
1184 blksz_dio = min(argv_blksz_dio, blksz_dio);
1185 blksz_dio = (min(statp->bs_size, blksz_dio) / dio_min) * dio_min;
1186 }
1187
1188 if (dflag) {
1189 fsrprintf(_("DEBUG: "
1190 "fsize=%lld blsz_dio=%d d_min=%d d_max=%d pgsz=%d\n"),
1191 statp->bs_size, blksz_dio, dio.d_miniosz,
1192 dio.d_maxiosz, pagesize);
1193 }
1194
1195 if (!(fbuf = (char *)memalign(dio.d_mem, blksz_dio))) {
1196 fsrprintf(_("could not allocate buf: %s\n"), tname);
1197 goto out;
1198 }
1199
1200 if (nfrags) {
1201 /* Create new tmp file in same AG as first */
1202 sprintf(ffname, "%s.frag", tname);
1203
1204 /* Open the new file for sync writes */
1205 if ((ffd = open(ffname, openopts, 0666)) < 0) {
1206 fsrprintf(_("could not open fragfile: %s : %s\n"),
1207 ffname, strerror(errno));
1208 goto out;
1209 }
1210 unlink(ffname);
1211 }
1212
1213 /* Loop through block map allocating new extents */
1214 for (extent = 0; extent < nextents; extent++) {
1215 pos = outmap[extent].bmv_offset;
1216 if (outmap[extent].bmv_block == -1) {
1217 space.l_whence = SEEK_SET;
1218 space.l_start = pos;
1219 space.l_len = outmap[extent].bmv_length;
1220 if (ioctl(tfd, XFS_IOC_UNRESVSP64, &space) < 0) {
1221 fsrprintf(_("could not trunc tmp %s\n"),
1222 tname);
1223 }
1224 if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
1225 fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
1226 tname, strerror(errno));
1227 goto out;
1228 }
1229 continue;
1230 } else if (outmap[extent].bmv_length == 0) {
1231 /* to catch holes at the beginning of the file */
1232 continue;
1233 }
1234 if (! nfrags) {
1235 space.l_whence = SEEK_CUR;
1236 space.l_start = 0;
1237 space.l_len = outmap[extent].bmv_length;
1238
1239 if (ioctl(tfd, XFS_IOC_RESVSP64, &space) < 0) {
1240 fsrprintf(_("could not pre-allocate tmp space:"
1241 " %s\n"), tname);
1242 goto out;
1243 }
1244 if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
1245 fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
1246 tname, strerror(errno));
1247 goto out;
1248 }
1249 }
1250 } /* end of space allocation loop */
1251
1252 if (lseek(tfd, 0, SEEK_SET)) {
1253 fsrprintf(_("Couldn't rewind on temporary file\n"));
1254 goto out;
1255 }
1256
1257 /* Check if the temporary file has fewer extents */
1258 new_nextents = getnextents(tfd);
1259 if (dflag)
1260 fsrprintf(_("Temporary file has %d extents (%d in original)\n"), new_nextents, cur_nextents);
1261 if (cur_nextents <= new_nextents) {
1262 if (vflag)
1263 fsrprintf(_("No improvement will be made (skipping): %s\n"), fname);
1264 retval = 1; /* no change/no error */
1265 goto out;
1266 }
1267
1268 /* Loop through block map copying the file. */
1269 for (extent = 0; extent < nextents; extent++) {
1270 pos = outmap[extent].bmv_offset;
1271 if (outmap[extent].bmv_block == -1) {
1272 if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
1273 fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
1274 tname, strerror(errno));
1275 goto out;
1276 }
1277 if (lseek(fd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
1278 fsrprintf(_("could not lseek in file: %s : %s\n"),
1279 fname, strerror(errno));
1280 goto out;
1281 }
1282 continue;
1283 } else if (outmap[extent].bmv_length == 0) {
1284 /* to catch holes at the beginning of the file */
1285 continue;
1286 }
1287 for (cnt = outmap[extent].bmv_length; cnt > 0;
1288 cnt -= ct, pos += ct) {
1289 if (nfrags && --nfrags) {
1290 ct = min(cnt, dio_min);
1291 } else if (cnt % dio_min == 0) {
1292 ct = min(cnt, blksz_dio);
1293 } else {
1294 ct = min(cnt + dio_min - (cnt % dio_min),
1295 blksz_dio);
1296 }
1297 ct = read(fd, fbuf, ct);
1298 if (ct == 0) {
1299 /* EOF, stop trying to read */
1300 extent = nextents;
1301 break;
1302 }
1303 /* Ensure we do direct I/O to correct block
1304 * boundaries.
1305 */
1306 if (ct % dio_min != 0) {
1307 wc = ct + dio_min - (ct % dio_min);
1308 } else {
1309 wc = ct;
1310 }
1311 wc_b4 = wc;
1312 if (ct < 0 || ((wc = write(tfd, fbuf, wc)) != wc_b4)) {
1313 if (ct < 0)
1314 fsrprintf(_("bad read of %d bytes "
1315 "from %s: %s\n"), wc_b4,
1316 fname, strerror(errno));
1317 else if (wc < 0)
1318 fsrprintf(_("bad write of %d bytes "
1319 "to %s: %s\n"), wc_b4,
1320 tname, strerror(errno));
1321 else {
1322 /*
1323 * Might be out of space
1324 *
1325 * Try to finish write
1326 */
1327 int resid = ct-wc;
1328
1329 if ((wc = write(tfd, ((char *)fbuf)+wc,
1330 resid)) == resid) {
1331 /* worked on second attempt? */
1332 continue;
1333 }
1334 else if (wc < 0) {
1335 fsrprintf(_("bad write2 of %d "
1336 "bytes to %s: %s\n"),
1337 resid, tname,
1338 strerror(errno));
1339 } else {
1340 fsrprintf(_("bad copy to %s\n"),
1341 tname);
1342 }
1343 }
1344 goto out;
1345 }
1346 if (nfrags) {
1347 /* Do a matching write to the tmp file */
1348 wc_b4 = wc;
1349 if (((wc = write(ffd, fbuf, wc)) != wc_b4)) {
1350 fsrprintf(_("bad write of %d bytes "
1351 "to %s: %s\n"),
1352 wc_b4, ffname, strerror(errno));
1353 }
1354 }
1355 }
1356 }
1357 if (ftruncate(tfd, statp->bs_size) < 0) {
1358 fsrprintf(_("could not truncate tmpfile: %s : %s\n"),
1359 fname, strerror(errno));
1360 goto out;
1361 }
1362 if (fsync(tfd) < 0) {
1363 fsrprintf(_("could not fsync tmpfile: %s : %s\n"),
1364 fname, strerror(errno));
1365 goto out;
1366 }
1367
1368 sx.sx_stat = *statp; /* struct copy */
1369 sx.sx_version = XFS_SX_VERSION;
1370 sx.sx_fdtarget = fd;
1371 sx.sx_fdtmp = tfd;
1372 sx.sx_offset = 0;
1373 sx.sx_length = statp->bs_size;
1374
1375 /* switch to the owner's id, to keep quota in line */
1376 if (fchown(tfd, statp->bs_uid, statp->bs_gid) < 0) {
1377 if (vflag)
1378 fsrprintf(_("failed to fchown tmpfile %s: %s\n"),
1379 tname, strerror(errno));
1380 goto out;
1381 }
1382
1383 /* Swap the extents */
1384 srval = xfs_swapext(fd, &sx);
1385 if (srval < 0) {
1386 if (errno == ENOTSUP) {
1387 if (vflag || dflag)
1388 fsrprintf(_("%s: file type not supported\n"), fname);
1389 } else if (errno == EFAULT) {
1390 /* The file has changed since we started the copy */
1391 if (vflag || dflag)
1392 fsrprintf(_("%s: file modified defrag aborted\n"),
1393 fname);
1394 } else if (errno == EBUSY) {
1395 /* Timestamp has changed or mmap'ed file */
1396 if (vflag || dflag)
1397 fsrprintf(_("%s: file busy\n"), fname);
1398 } else {
1399 fsrprintf(_("XFS_IOC_SWAPEXT failed: %s: %s\n"),
1400 fname, strerror(errno));
1401 }
1402 goto out;
1403 }
1404
1405 /* Report progress */
1406 if (vflag)
1407 fsrprintf(_("extents before:%d after:%d %s %s\n"),
1408 cur_nextents, new_nextents,
1409 (new_nextents <= nextents ? "DONE" : " " ),
1410 fname);
1411 retval = 0;
1412
1413 out:
1414 free(fbuf);
1415 if (tfd != -1)
1416 close(tfd);
1417 if (ffd != -1)
1418 close(ffd);
1419 return retval;
1420 }
1421
1422 char *
1423 gettmpname(char *fname)
1424 {
1425 static char buf[PATH_MAX+1];
1426 char sbuf[SMBUFSZ];
1427 char *ptr;
1428
1429 sprintf(sbuf, "/.fsr%d", getpid());
1430
1431 strncpy(buf, fname, PATH_MAX);
1432 buf[PATH_MAX] = '\0';
1433 ptr = strrchr(buf, '/');
1434 if (ptr) {
1435 *ptr = '\0';
1436 } else {
1437 strcpy(buf, ".");
1438 }
1439
1440 if ((strlen(buf) + strlen (sbuf)) > PATH_MAX) {
1441 fsrprintf(_("tmp file name too long: %s\n"), fname);
1442 return(NULL);
1443 }
1444
1445 strcat(buf, sbuf);
1446
1447 return(buf);
1448 }
1449
1450 char *
1451 getparent(char *fname)
1452 {
1453 static char buf[PATH_MAX+1];
1454 char *ptr;
1455
1456 strncpy(buf, fname, PATH_MAX);
1457 buf[PATH_MAX] = '\0';
1458 ptr = strrchr(buf, '/');
1459 if (ptr) {
1460 if (ptr == &buf[0])
1461 ++ptr;
1462 *ptr = '\0';
1463 } else {
1464 strcpy(buf, ".");
1465 }
1466
1467 return(buf);
1468 }
1469
1470 /*
1471 * Read in block map of the input file, coalesce contiguous
1472 * extents into a single range, keep all holes. Convert from 512 byte
1473 * blocks to bytes.
1474 *
1475 * This code was borrowed from mv.c with some minor mods.
1476 */
1477 #define MAPSIZE 128
1478 #define OUTMAP_SIZE_INCREMENT MAPSIZE
1479
1480 int read_fd_bmap(int fd, struct xfs_bstat *sin, int *cur_nextents)
1481 {
1482 int i, cnt;
1483 struct getbmap map[MAPSIZE];
1484
1485 #define BUMP_CNT \
1486 if (++cnt >= outmap_size) { \
1487 outmap_size += OUTMAP_SIZE_INCREMENT; \
1488 outmap = (struct getbmap *)realloc(outmap, \
1489 outmap_size*sizeof(*outmap)); \
1490 if (outmap == NULL) { \
1491 fsrprintf(_("realloc failed: %s\n"), \
1492 strerror(errno)); \
1493 exit(1); \
1494 } \
1495 }
1496
1497 /* Initialize the outmap array. It always grows - never shrinks.
1498 * Left-over memory allocation is saved for the next files.
1499 */
1500 if (outmap_size == 0) {
1501 outmap_size = OUTMAP_SIZE_INCREMENT; /* Initial size */
1502 outmap = (struct getbmap *)malloc(outmap_size*sizeof(*outmap));
1503 if (!outmap) {
1504 fsrprintf(_("malloc failed: %s\n"),
1505 strerror(errno));
1506 exit(1);
1507 }
1508 }
1509
1510 outmap[0].bmv_block = 0;
1511 outmap[0].bmv_offset = 0;
1512 outmap[0].bmv_length = sin->bs_size;
1513
1514 /*
1515 * If a non regular file is involved then forget holes
1516 */
1517
1518 if (!S_ISREG(sin->bs_mode))
1519 return(1);
1520
1521 outmap[0].bmv_length = 0;
1522
1523 map[0].bmv_offset = 0;
1524 map[0].bmv_block = 0;
1525 map[0].bmv_entries = 0;
1526 map[0].bmv_count = MAPSIZE;
1527 map[0].bmv_length = -1;
1528
1529 cnt = 0;
1530 *cur_nextents = 0;
1531
1532 do {
1533 if (ioctl(fd, XFS_IOC_GETBMAP, map) < 0) {
1534 fsrprintf(_("failed reading extents: inode %llu"),
1535 (unsigned long long)sin->bs_ino);
1536 exit(1);
1537 }
1538
1539 /* Concatenate extents together and replicate holes into
1540 * the output map.
1541 */
1542 *cur_nextents += map[0].bmv_entries;
1543 for (i = 0; i < map[0].bmv_entries; i++) {
1544 if (map[i + 1].bmv_block == -1) {
1545 BUMP_CNT;
1546 outmap[cnt] = map[i+1];
1547 } else if (outmap[cnt].bmv_block == -1) {
1548 BUMP_CNT;
1549 outmap[cnt] = map[i+1];
1550 } else {
1551 outmap[cnt].bmv_length += map[i + 1].bmv_length;
1552 }
1553 }
1554 } while (map[0].bmv_entries == (MAPSIZE-1));
1555 for (i = 0; i <= cnt; i++) {
1556 outmap[i].bmv_offset = BBTOB(outmap[i].bmv_offset);
1557 outmap[i].bmv_length = BBTOB(outmap[i].bmv_length);
1558 }
1559
1560 outmap[cnt].bmv_length = sin->bs_size - outmap[cnt].bmv_offset;
1561
1562 return(cnt+1);
1563 }
1564
1565 /*
1566 * Read the block map and return the number of extents.
1567 */
1568 static int
1569 getnextents(int fd)
1570 {
1571 int nextents;
1572 struct getbmap map[MAPSIZE];
1573
1574 map[0].bmv_offset = 0;
1575 map[0].bmv_block = 0;
1576 map[0].bmv_entries = 0;
1577 map[0].bmv_count = MAPSIZE;
1578 map[0].bmv_length = -1;
1579
1580 nextents = 0;
1581
1582 do {
1583 if (ioctl(fd,XFS_IOC_GETBMAP, map) < 0) {
1584 fsrprintf(_("failed reading extents"));
1585 exit(1);
1586 }
1587
1588 nextents += map[0].bmv_entries;
1589 } while (map[0].bmv_entries == (MAPSIZE-1));
1590
1591 return(nextents);
1592 }
1593
1594 /*
1595 * Get xfs realtime space information
1596 */
1597 int
1598 xfs_getrt(int fd, struct statvfs *sfbp)
1599 {
1600 unsigned long bsize;
1601 unsigned long factor;
1602 xfs_fsop_counts_t cnt;
1603
1604 if (!fsgeom.rtblocks)
1605 return -1;
1606
1607 if (xfs_fscounts(fd, &cnt) < 0) {
1608 close(fd);
1609 return -1;
1610 }
1611 bsize = (sfbp->f_frsize ? sfbp->f_frsize : sfbp->f_bsize);
1612 factor = fsgeom.blocksize / bsize; /* currently this is == 1 */
1613 sfbp->f_bfree = (cnt.freertx * fsgeom.rtextsize) * factor;
1614 return 0;
1615 }
1616
1617 int
1618 fsrprintf(const char *fmt, ...)
1619 {
1620 va_list ap;
1621
1622 va_start(ap, fmt);
1623 if (gflag) {
1624 static int didopenlog;
1625 if (!didopenlog) {
1626 openlog("fsr", LOG_PID, LOG_USER);
1627 didopenlog = 1;
1628 }
1629 vsyslog(LOG_INFO, fmt, ap);
1630 } else
1631 vprintf(fmt, ap);
1632 va_end(ap);
1633 return 0;
1634 }
1635
1636 /*
1637 * Initialize a directory for tmp file use. This is used
1638 * by the full filesystem defragmentation when we're walking
1639 * the inodes and do not know the path for the individual
1640 * files. Multiple directories are used to spread out the
1641 * tmp data around to different ag's (since file data is
1642 * usually allocated to the same ag as the directory and
1643 * directories allocated round robin from the same
1644 * parent directory).
1645 */
1646 static void
1647 tmp_init(char *mnt)
1648 {
1649 int i;
1650 static char buf[SMBUFSZ];
1651 mode_t mask;
1652
1653 tmp_agi = 0;
1654 sprintf(buf, "%s/.fsr", mnt);
1655
1656 mask = umask(0);
1657 if (mkdir(buf, 0700) < 0) {
1658 if (errno == EEXIST) {
1659 if (dflag)
1660 fsrprintf(_("tmpdir already exists: %s\n"),
1661 buf);
1662 } else {
1663 fsrprintf(_("could not create tmpdir: %s: %s\n"),
1664 buf, strerror(errno));
1665 exit(-1);
1666 }
1667 }
1668 for (i=0; i < fsgeom.agcount; i++) {
1669 sprintf(buf, "%s/.fsr/ag%d", mnt, i);
1670 if (mkdir(buf, 0700) < 0) {
1671 if (errno == EEXIST) {
1672 if (dflag)
1673 fsrprintf(
1674 _("tmpdir already exists: %s\n"), buf);
1675 } else {
1676 fsrprintf(_("cannot create tmpdir: %s: %s\n"),
1677 buf, strerror(errno));
1678 exit(-1);
1679 }
1680 }
1681 }
1682 (void)umask(mask);
1683 return;
1684 }
1685
1686 static char *
1687 tmp_next(char *mnt)
1688 {
1689 static char buf[SMBUFSZ];
1690
1691 sprintf(buf, "%s/.fsr/ag%d/tmp%d",
1692 ( (strcmp(mnt, "/") == 0) ? "" : mnt),
1693 tmp_agi,
1694 getpid());
1695
1696 if (++tmp_agi == fsgeom.agcount)
1697 tmp_agi = 0;
1698
1699 return(buf);
1700 }
1701
1702 static void
1703 tmp_close(char *mnt)
1704 {
1705 static char buf[SMBUFSZ];
1706 int i;
1707
1708 /* No data is ever actually written so we can just do rmdir's */
1709 for (i=0; i < fsgeom.agcount; i++) {
1710 sprintf(buf, "%s/.fsr/ag%d", mnt, i);
1711 if (rmdir(buf) < 0) {
1712 if (errno != ENOENT) {
1713 fsrprintf(
1714 _("could not remove tmpdir: %s: %s\n"),
1715 buf, strerror(errno));
1716 }
1717 }
1718 }
1719 sprintf(buf, "%s/.fsr", mnt);
1720 if (rmdir(buf) < 0) {
1721 if (errno != ENOENT) {
1722 fsrprintf(_("could not remove tmpdir: %s: %s\n"),
1723 buf, strerror(errno));
1724 }
1725 }
1726 }