2 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
23 #include "xfs_bmap_btree.h"
24 #include "xfs_attr_sf.h"
30 #include <sys/ioctl.h>
32 #include <sys/statvfs.h>
33 #include <sys/xattr.h>
36 #define _PATH_FSRLAST "/var/tmp/.fsrlast_xfs"
37 #define _PATH_PROC_MOUNTS "/proc/mounts"
45 /* static int nflag; */
47 /* static int sflag; */
49 extern int max_ext_size
;
50 static int npasses
= 10;
51 static int startpass
= 0;
53 struct getbmap
*outmap
= NULL
;
57 static __int64_t minimumfree
= 2048;
59 #define MNTTYPE_XFS "xfs"
65 #define TARGETRANGE 10
69 #define BUFFER_SIZE (1<<16)
70 #define BUFFER_MAX (1<<24)
72 static time_t howlong
= 7200; /* default seconds of reorganizing */
73 static char *leftofffile
= _PATH_FSRLAST
; /* where we left off last */
74 static time_t endtime
;
75 static time_t starttime
;
76 static xfs_ino_t leftoffino
= 0;
80 static int fsrfile(char *fname
, xfs_ino_t ino
);
81 static int fsrfile_common( char *fname
, char *tname
, char *mnt
,
82 int fd
, xfs_bstat_t
*statp
);
83 static int packfile(char *fname
, char *tname
, int fd
,
84 xfs_bstat_t
*statp
, struct fsxattr
*fsxp
);
85 static void fsrdir(char *dirname
);
86 static int fsrfs(char *mntdir
, xfs_ino_t ino
, int targetrange
);
87 static void initallfs(char *mtab
);
88 static void fsrallfs(char *mtab
, int howlong
, char *leftofffile
);
89 static void fsrall_cleanup(int timeout
);
90 static int getnextents(int);
91 int xfsrtextsize(int fd
);
92 int xfs_getrt(int fd
, struct statvfs64
*sfbp
);
93 char * gettmpname(char *fname
);
94 char * getparent(char *fname
);
95 int fsrprintf(const char *fmt
, ...);
96 int read_fd_bmap(int, xfs_bstat_t
*, int *);
97 int cmp(const void *, const void *);
98 static void tmp_init(char *mnt
);
99 static char * tmp_next(char *mnt
);
100 static void tmp_close(char *mnt
);
101 int xfs_getgeom(int , xfs_fsop_geom_v1_t
* );
103 xfs_fsop_geom_v1_t fsgeom
; /* geometry of active mounted system */
108 typedef struct fsdesc
{
114 fsdesc_t
*fs
, *fsbase
, *fsend
;
115 int fsbufsize
= 10; /* A starting value */
116 int nfrags
= 0; /* Debug option: Coerse into specific number
118 int openopts
= O_CREAT
|O_EXCL
|O_RDWR
|O_DIRECT
;
121 xfs_fsgeometry(int fd
, xfs_fsop_geom_v1_t
*geom
)
123 return ioctl(fd
, XFS_IOC_FSGEOMETRY_V1
, geom
);
127 xfs_bulkstat_single(int fd
, xfs_ino_t
*lastip
, xfs_bstat_t
*ubuffer
)
129 xfs_fsop_bulkreq_t bulkreq
;
131 bulkreq
.lastip
= (__u64
*)lastip
;
133 bulkreq
.ubuffer
= ubuffer
;
134 bulkreq
.ocount
= NULL
;
135 return ioctl(fd
, XFS_IOC_FSBULKSTAT_SINGLE
, &bulkreq
);
139 xfs_bulkstat(int fd
, xfs_ino_t
*lastip
, int icount
,
140 xfs_bstat_t
*ubuffer
, __s32
*ocount
)
142 xfs_fsop_bulkreq_t bulkreq
;
144 bulkreq
.lastip
= (__u64
*)lastip
;
145 bulkreq
.icount
= icount
;
146 bulkreq
.ubuffer
= ubuffer
;
147 bulkreq
.ocount
= ocount
;
148 return ioctl(fd
, XFS_IOC_FSBULKSTAT
, &bulkreq
);
152 xfs_swapext(int fd
, xfs_swapext_t
*sx
)
154 return ioctl(fd
, XFS_IOC_SWAPEXT
, sx
);
158 xfs_fscounts(int fd
, xfs_fsop_counts_t
*counts
)
160 return ioctl(fd
, XFS_IOC_FSCOUNTS
, counts
);
171 * Check if the argument is either the device name or mountpoint of an XFS
172 * filesystem. Note that we do not care about bind mounted regular files
173 * here - the code that handles defragmentation of invidual files takes care
177 find_mountpoint_check(struct stat
*sb
, struct mntent
*t
)
181 if (S_ISDIR(sb
->st_mode
)) { /* mount point */
182 if (stat(t
->mnt_dir
, &ms
) < 0)
184 if (sb
->st_ino
!= ms
.st_ino
)
186 if (sb
->st_dev
!= ms
.st_dev
)
188 if (strcmp(t
->mnt_type
, MNTTYPE_XFS
) != 0)
190 } else { /* device */
191 if (stat(t
->mnt_fsname
, &ms
) < 0)
193 if (sb
->st_rdev
!= ms
.st_rdev
)
195 if (strcmp(t
->mnt_type
, MNTTYPE_XFS
) != 0)
198 * Make sure the mountpoint given by mtab is accessible
201 if (stat(t
->mnt_dir
, &ms
) < 0)
209 find_mountpoint(char *mtab
, char *argname
, struct stat
*sb
)
211 struct mntent_cursor cursor
;
212 struct mntent
*t
= NULL
;
215 if (platform_mntent_open(&cursor
, mtab
) != 0){
216 fprintf(stderr
, "Error: can't get mntent entries.\n");
220 while ((t
= platform_mntent_next(&cursor
)) != NULL
) {
221 mntp
= find_mountpoint_check(sb
, t
);
226 platform_mntent_close(&cursor
);
231 main(int argc
, char **argv
)
240 progname
= basename(argv
[0]);
242 setlocale(LC_ALL
, "");
243 bindtextdomain(PACKAGE
, LOCALEDIR
);
248 while ((c
= getopt(argc
, argv
, "C:p:e:MgsdnvTt:f:m:b:N:FV")) != -1) {
265 case 's': /* frag stats only */
268 _("%s: Stats not yet supported for XFS\n"),
273 howlong
= atoi(optarg
);
276 leftofffile
= optarg
;
282 argv_blksz_dio
= atoi(optarg
);
285 npasses
= atoi(optarg
);
288 /* Testing opt: coerses frag count in result */
289 if (getenv("FSRXFSTEST") != NULL
) {
290 nfrags
= atoi(optarg
);
295 printf(_("%s version %s\n"), progname
, VERSION
);
303 * If the user did not specify an explicit mount table, try to use
304 * /proc/mounts if it is available, else /etc/mtab. We prefer
305 * /proc/mounts because it is kernel controlled, while /etc/mtab
306 * may contain garbage that userspace tools like pam_mounts wrote
310 if (access(_PATH_PROC_MOUNTS
, R_OK
) == 0)
311 mtab
= _PATH_PROC_MOUNTS
;
313 mtab
= _PATH_MOUNTED
;
317 setbuf(stdout
, NULL
);
321 /* Save the caller's real uid */
324 pagesize
= getpagesize();
327 for (; optind
< argc
; optind
++) {
328 argname
= argv
[optind
];
330 if (lstat(argname
, &sb
) < 0) {
332 _("%s: could not stat: %s: %s\n"),
333 progname
, argname
, strerror(errno
));
337 if (S_ISLNK(sb
.st_mode
)) {
340 if (stat(argname
, &sb2
) == 0 &&
341 (S_ISBLK(sb2
.st_mode
) ||
342 S_ISCHR(sb2
.st_mode
)))
346 mntp
= find_mountpoint(mtab
, argname
, &sb
);
349 } else if (S_ISCHR(sb
.st_mode
)) {
351 "%s: char special not supported: %s\n"),
354 } else if (S_ISDIR(sb
.st_mode
) || S_ISREG(sb
.st_mode
)) {
355 if (!platform_test_xfs_path(argname
)) {
357 "%s: cannot defragment: %s: Not XFS\n"),
361 if (S_ISDIR(sb
.st_mode
))
364 fsrfile(argname
, sb
.st_ino
);
367 _("%s: not fsys dev, dir, or reg file, ignoring\n"),
373 fsrallfs(mtab
, howlong
, leftofffile
);
382 "Usage: %s [-d] [-v] [-g] [-t time] [-p passes] [-f leftf] [-m mtab]\n"
383 " %s [-d] [-v] [-g] xfsdev | dir | file ...\n"
386 " -g Print to syslog (default if stdout not a tty).\n"
387 " -t time How long to run in seconds.\n"
388 " -p passes Number of passes before terminating global re-org.\n"
389 " -f leftoff Use this instead of %s.\n"
390 " -m mtab Use something other than /etc/mtab.\n"
391 " -d Debug, print even more.\n"
392 " -v Verbose, more -v's more verbose.\n"
393 " -V Print version number and exit.\n"
394 ), progname
, progname
, progname
, _PATH_FSRLAST
);
399 * initallfs -- read the mount table and set up an internal form
402 initallfs(char *mtab
)
404 struct mntent_cursor cursor
;
405 struct mntent
*mnt
= NULL
;
410 /* malloc a number of descriptors, increased later if needed */
411 if (!(fsbase
= (fsdesc_t
*)malloc(fsbufsize
* sizeof(fsdesc_t
)))) {
412 fsrprintf(_("out of memory: %s\n"), strerror(errno
));
415 fsend
= (fsbase
+ fsbufsize
- 1);
417 /* find all rw xfs file systems */
421 if (platform_mntent_open(&cursor
, mtab
) != 0){
422 fprintf(stderr
, "Error: can't get mntent entries.\n");
426 while ((mnt
= platform_mntent_next(&cursor
)) != NULL
) {
429 if (strcmp(mnt
->mnt_type
, MNTTYPE_XFS
) != 0 ||
430 stat(mnt
->mnt_fsname
, &sb
) == -1 ||
431 !S_ISBLK(sb
.st_mode
))
434 cp
= strtok(mnt
->mnt_opts
,",");
436 if (strcmp("rw", cp
) == 0)
438 } while ((cp
= strtok(NULL
, ",")) != NULL
);
441 fsrprintf(_("Skipping %s: not mounted rw\n"),
446 if (mi
== fsbufsize
) {
448 if ((fsbase
= (fsdesc_t
*)realloc((char *)fsbase
,
449 fsbufsize
* sizeof(fsdesc_t
))) == NULL
) {
450 fsrprintf(_("out of memory: %s\n"),
455 fsrprintf(_("out of memory on realloc: %s\n"),
459 fs
= (fsbase
+ mi
); /* Needed ? */
462 fs
->dev
= strdup(mnt
->mnt_fsname
);
463 fs
->mnt
= strdup(mnt
->mnt_dir
);
465 if (fs
->dev
== NULL
) {
466 fsrprintf(_("strdup(%s) failed\n"), mnt
->mnt_fsname
);
469 if (fs
->mnt
== NULL
) {
470 fsrprintf(_("strdup(%s) failed\n"), mnt
->mnt_dir
);
476 platform_mntent_close(&cursor
);
479 fsend
= (fsbase
+ numfs
);
481 fsrprintf(_("no rw xfs file systems in mtab: %s\n"), mtab
);
484 if (vflag
|| dflag
) {
485 fsrprintf(_("Found %d mounted, writable, XFS filesystems\n"),
488 for (fs
= fsbase
; fs
< fsend
; fs
++)
489 fsrprintf("\t%-30.30s%-30.30s\n", fs
->dev
, fs
->mnt
);
494 fsrallfs(char *mtab
, int howlong
, char *leftofffile
)
503 xfs_ino_t startino
= 0;
507 fsrprintf("xfs_fsr -m %s -t %d -f %s ...\n", mtab
, howlong
, leftofffile
);
509 endtime
= starttime
+ howlong
;
512 /* where'd we leave off last time? */
513 if (lstat(leftofffile
, &sb
) == 0) {
514 if ( (fd
= open(leftofffile
, O_RDONLY
)) == -1 ) {
515 fsrprintf(_("%s: open failed\n"), leftofffile
);
517 else if ( fstat(fd
, &sb2
) == 0) {
519 * Verify that lstat & fstat point to the
520 * same regular file (no links/no quick spoofs)
522 if ( (sb
.st_dev
!= sb2
.st_dev
) ||
523 (sb
.st_ino
!= sb2
.st_ino
) ||
524 ((sb
.st_mode
& S_IFMT
) != S_IFREG
) ||
525 ((sb2
.st_mode
& S_IFMT
) != S_IFREG
) ||
526 (sb2
.st_uid
!= ROOT
) ||
530 fsrprintf(_("Can't use %s: mode=0%o own=%d"
532 leftofffile
, sb
.st_mode
,
533 sb
.st_uid
, sb
.st_nlink
);
548 if (read(fd
, buf
, SMBUFSZ
) == -1) {
550 fsrprintf(_("could not read %s, starting with %s\n"),
551 leftofffile
, *fs
->dev
);
553 /* Ensure the buffer we read is null terminated */
554 buf
[SMBUFSZ
-1] = '\0';
555 for (fs
= fsbase
; fs
< fsend
; fs
++) {
557 if ((strncmp(buf
,fsname
,strlen(fsname
)) == 0)
558 && buf
[strlen(fsname
)] == ' ') {
566 ptr
= strchr(buf
, ' ');
568 startpass
= atoi(++ptr
);
569 ptr
= strchr(ptr
, ' ');
571 startino
= strtoull(++ptr
, NULL
, 10);
577 /* Init pass counts */
578 for (fsp
= fsbase
; fsp
< fs
; fsp
++) {
579 fsp
->npass
= startpass
+ 1;
581 for (fsp
= fs
; fsp
<= fsend
; fsp
++) {
582 fsp
->npass
= startpass
;
589 fsrprintf(_("START: pass=%d ino=%llu %s %s\n"),
590 fs
->npass
, (unsigned long long)startino
,
594 signal(SIGABRT
, aborter
);
595 signal(SIGHUP
, aborter
);
596 signal(SIGINT
, aborter
);
597 signal(SIGQUIT
, aborter
);
598 signal(SIGTERM
, aborter
);
600 /* reorg for 'howlong' -- checked in 'fsrfs' */
601 while (endtime
> time(0)) {
605 if (fs
->npass
== npasses
) {
606 fsrprintf(_("Completed all %d passes\n"), npasses
);
609 if (npasses
> 1 && !fs
->npass
)
616 fsrprintf(_("couldn't fork sub process:"));
620 error
= fsrfs(fs
->mnt
, startino
, TARGETRANGE
);
625 if (WIFEXITED(error
) && WEXITSTATUS(error
) == 1) {
626 /* child timed out & did fsrall_cleanup */
631 startino
= 0; /* reset after the first time through */
635 fsrall_cleanup(endtime
<= time(0));
639 * fsrall_cleanup -- close files, print next starting location, etc.
642 fsrall_cleanup(int timeout
)
651 fsrprintf(_("%s startpass %d, endpass %d, time %d seconds\n"),
652 progname
, startpass
, fs
->npass
,
653 time(0) - endtime
+ howlong
);
655 /* record where we left off */
656 fd
= open(leftofffile
, O_WRONLY
|O_CREAT
|O_EXCL
, 0644);
658 fsrprintf(_("open(%s) failed: %s\n"),
659 leftofffile
, strerror(errno
));
661 ret
= sprintf(buf
, "%s %d %llu\n", fs
->dev
,
662 fs
->npass
, (unsigned long long)leftoffino
);
663 if (write(fd
, buf
, ret
) < strlen(buf
))
664 fsrprintf(_("write(%s) failed: %s\n"),
665 leftofffile
, strerror(errno
));
672 * fsrfs -- reorganize a file system
675 fsrfs(char *mntdir
, xfs_ino_t startino
, int targetrange
)
682 xfs_bstat_t buf
[GRABSZ
];
685 jdm_fshandle_t
*fshandlep
;
686 xfs_ino_t lastino
= startino
;
688 fsrprintf(_("%s start inode=%llu\n"), mntdir
,
689 (unsigned long long)startino
);
691 fshandlep
= jdm_getfshandle( mntdir
);
693 fsrprintf(_("unable to get handle: %s: %s\n"),
694 mntdir
, strerror( errno
));
698 if ((fsfd
= open(mntdir
, O_RDONLY
)) < 0) {
699 fsrprintf(_("unable to open: %s: %s\n"),
700 mntdir
, strerror( errno
));
705 if (xfs_getgeom(fsfd
, &fsgeom
) < 0 ) {
706 fsrprintf(_("Skipping %s: could not get XFS geometry\n"),
715 while ((ret
= xfs_bulkstat(fsfd
,
716 &lastino
, GRABSZ
, &buf
[0], &buflenout
)) == 0) {
723 /* Each loop through, defrag targetrange percent of the files */
724 count
= (buflenout
* targetrange
) / 100;
726 qsort((char *)buf
, buflenout
, sizeof(struct xfs_bstat
), cmp
);
728 for (p
= buf
, endp
= (buf
+ buflenout
); p
< endp
; p
++) {
729 /* Do some obvious checks now */
730 if (((p
->bs_mode
& S_IFMT
) != S_IFREG
) ||
734 fd
= jdm_open(fshandlep
, p
, O_RDWR
|O_DIRECT
);
736 /* This probably means the file was
737 * removed while in progress of handling
738 * it. Just quietly ignore this file.
741 fsrprintf(_("could not open: "
742 "inode %llu\n"), p
->bs_ino
);
746 /* Don't know the pathname, so make up something */
747 sprintf(fname
, "ino=%lld", (long long)p
->bs_ino
);
749 /* Get a tmp file name */
750 tname
= tmp_next(mntdir
);
752 ret
= fsrfile_common(fname
, tname
, mntdir
, fd
, p
);
754 leftoffino
= p
->bs_ino
;
763 if (endtime
&& endtime
< time(0)) {
771 fsrprintf(_("%s: xfs_bulkstat: %s\n"), progname
, strerror(errno
));
780 * To compare bstat structs for qsort.
783 cmp(const void *s1
, const void *s2
)
785 return( ((xfs_bstat_t
*)s2
)->bs_extents
-
786 ((xfs_bstat_t
*)s1
)->bs_extents
);
791 * reorganize by directory hierarchy.
792 * Stay in dev (a restriction based on structure of this program -- either
793 * call efs_{n,u}mount() around each file, something smarter or this)
796 fsrdir(char *dirname
)
798 fsrprintf(_("%s: Directory defragmentation not supported\n"), dirname
);
802 * Sets up the defragmentation of a file based on the
803 * filepath. It collects the bstat information, does
804 * an open on the file and passes this all to fsrfile_common.
807 fsrfile(char *fname
, xfs_ino_t ino
)
810 jdm_fshandle_t
*fshandlep
;
811 int fd
= -1, fsfd
= -1;
815 fshandlep
= jdm_getfshandle(getparent (fname
) );
817 fsrprintf(_("unable to construct sys handle for %s: %s\n"),
818 fname
, strerror(errno
));
823 * Need to open something on the same filesystem as the
824 * file. Open the parent.
826 fsfd
= open(getparent(fname
), O_RDONLY
);
828 fsrprintf(_("unable to open sys handle for %s: %s\n"),
829 fname
, strerror(errno
));
833 if ((xfs_bulkstat_single(fsfd
, &ino
, &statbuf
)) < 0) {
834 fsrprintf(_("unable to get bstat on %s: %s\n"),
835 fname
, strerror(errno
));
839 fd
= jdm_open(fshandlep
, &statbuf
, O_RDWR
|O_DIRECT
);
841 fsrprintf(_("unable to open handle %s: %s\n"),
842 fname
, strerror(errno
));
846 /* Get the fs geometry */
847 if (xfs_getgeom(fsfd
, &fsgeom
) < 0 ) {
848 fsrprintf(_("Unable to get geom on fs for: %s\n"), fname
);
852 tname
= gettmpname(fname
);
855 error
= fsrfile_common(fname
, tname
, NULL
, fd
, &statbuf
);
869 * This is the common defrag code for either a full fs
870 * defragmentation or a single file. Check as much as
871 * possible with the file, fork a process to setuid to the
872 * target file owner's uid and defragment the file.
873 * This is done so the new extents created in a tmp file are
874 * reflected in the owners' quota without having to do any
875 * special code in the kernel. When the existing extents
876 * are removed, the quotas will be correct. It's ugly but
877 * it saves us from doing some quota re-construction in
878 * the extent swap. The price is that the defragmentation
879 * will fail if the owner of the target file is already at
891 struct statvfs64 vfss
;
896 fsrprintf("%s\n", fname
);
899 fsrprintf(_("sync failed: %s: %s\n"), fname
, strerror(errno
));
903 if (statp
->bs_size
== 0) {
905 fsrprintf(_("%s: zero size, ignoring\n"), fname
);
909 /* Check if a mandatory lock is set on the file to try and
910 * avoid blocking indefinitely on the reads later. Note that
911 * someone could still set a mandatory lock after this check
912 * but before all reads have completed to block fsr reads.
913 * This change just closes the window a bit.
915 if ( (statp
->bs_mode
& S_ISGID
) && ( ! (statp
->bs_mode
&S_IXGRP
) ) ) {
919 fl
.l_whence
= SEEK_SET
;
920 fl
.l_start
= (off_t
)0;
922 if ((fcntl(fd
, F_GETLK
, &fl
)) < 0 ) {
924 fsrprintf(_("locking check failed: %s\n"),
928 if (fl
.l_type
!= F_UNLCK
) {
929 /* Mandatory lock is set */
931 fsrprintf(_("mandatory lock: %s: ignoring\n"),
938 * Check if there is room to copy the file.
940 * Note that xfs_bstat.bs_blksize returns the filesystem blocksize,
941 * not the optimal I/O size as struct stat.
943 if (statvfs64(fsname
? fsname
: fname
, &vfss
) < 0) {
944 fsrprintf(_("unable to get fs stat on %s: %s\n"),
945 fname
, strerror(errno
));
949 bsize
= vfss
.f_frsize
? vfss
.f_frsize
: vfss
.f_bsize
;
951 bsize
= vfss
.f_bsize
;
953 if (statp
->bs_blksize
* statp
->bs_blocks
>
954 vfss
.f_bfree
* bsize
- minimumfree
) {
955 fsrprintf(_("insufficient freespace for: %s: "
956 "size=%lld: ignoring\n"), fname
,
957 statp
->bs_blksize
* statp
->bs_blocks
);
961 if ((ioctl(fd
, FS_IOC_FSGETXATTR
, &fsx
)) < 0) {
962 fsrprintf(_("failed to get inode attrs: %s\n"), fname
);
965 if (fsx
.fsx_xflags
& (FS_XFLAG_IMMUTABLE
|FS_XFLAG_APPEND
)) {
967 fsrprintf(_("%s: immutable/append, ignoring\n"), fname
);
970 if (fsx
.fsx_xflags
& FS_XFLAG_NODEFRAG
) {
972 fsrprintf(_("%s: marked as don't defrag, ignoring\n"),
976 if (fsx
.fsx_xflags
& FS_XFLAG_REALTIME
) {
977 if (xfs_getrt(fd
, &vfss
) < 0) {
978 fsrprintf(_("cannot get realtime geometry for: %s\n"),
982 if (statp
->bs_size
> ((vfss
.f_bfree
* bsize
) - minimumfree
)) {
983 fsrprintf(_("low on realtime free space: %s: "
984 "ignoring file\n"), fname
);
989 if ((RealUid
!= ROOT
) && (RealUid
!= statp
->bs_uid
)) {
990 fsrprintf(_("cannot open: %s: Permission denied\n"), fname
);
995 * Previously the code forked here, & the child changed it's uid to
996 * that of the file's owner and then called packfile(), to keep
997 * quota counts correct. (defragged files could use fewer blocks).
999 * Instead, just fchown() the temp file to the uid,gid of the
1000 * file we're defragging, in packfile().
1003 if ((error
= packfile(fname
, tname
, fd
, statp
, &fsx
)))
1005 return -1; /* no error */
1009 * Attempt to set the attr fork up correctly. This is simple for attr1
1010 * filesystems as they have a fixed inode fork offset. In that case
1011 * just create an attribute and that's all we need to do.
1013 * For attr2 filesystems, see if we have the actual fork offset in
1014 * the bstat structure. If so, just create additional attributes on
1015 * the temporary inode until the offset matches.
1017 * If it doesn't exist, we can only do best effort. Add an attribute at a time
1018 * to move the inode fork around, but take into account that the attribute
1019 * might be too small to move the fork every time we add one. This should
1020 * hopefully put the fork offset in the right place. It's not a big deal if we
1021 * don't get it right - the kernel will reject it when we try to swap extents.
1024 fsr_setup_attr_fork(
1027 xfs_bstat_t
*bstatp
)
1029 #ifdef HAVE_FSETXATTR
1030 struct stat tstatbuf
;
1033 int last_forkoff
= 0;
1034 int no_change_cnt
= 0;
1037 if (!(bstatp
->bs_xflags
& FS_XFLAG_HASATTR
))
1041 * use the old method if we have attr1 or the kernel does not yet
1042 * support passing the fork offset in the bulkstat data.
1044 if (!(fsgeom
.flags
& XFS_FSOP_GEOM_FLAGS_ATTR2
) ||
1045 bstatp
->bs_forkoff
== 0) {
1047 ret
= fsetxattr(tfd
, "user.X", "X", 1, XATTR_CREATE
);
1049 fsrprintf(_("could not set ATTR\n"));
1055 /* attr2 w/ fork offsets */
1057 if (fstat(tfd
, &tstatbuf
) < 0) {
1058 fsrprintf(_("unable to stat temp file: %s\n"),
1070 * bulkstat the temp inode to see what the forkoff is. Use
1071 * this to compare against the target and determine what we
1074 ino
= tstatbuf
.st_ino
;
1075 if ((xfs_bulkstat_single(tfd
, &ino
, &tbstat
)) < 0) {
1076 fsrprintf(_("unable to get bstat on temp file: %s\n"),
1081 fsrprintf(_("orig forkoff %d, temp forkoff %d\n"),
1082 bstatp
->bs_forkoff
, tbstat
.bs_forkoff
);
1083 diff
= tbstat
.bs_forkoff
- bstatp
->bs_forkoff
;
1085 /* if they are equal, we are done */
1089 snprintf(name
, sizeof(name
), "user.%d", i
);
1092 * If there is no attribute, then we need to create one to get
1093 * an attribute fork at the default location.
1095 if (!tbstat
.bs_forkoff
) {
1097 ret
= fsetxattr(tfd
, name
, "XX", 2, XATTR_CREATE
);
1099 fsrprintf(_("could not set ATTR\n"));
1103 } else if (i
== 0) {
1105 * First pass, and temp file already has an inline
1106 * xattr, probably due to selinux.
1108 * It's *possible* that the temp file attr area
1109 * is larger than the target file's:
1112 * +-------+ 0 +-------+ 0
1116 * | | v-------v forkoff
1118 * v-------v forkoff | Attr | local
1120 * +-------+ +-------+
1124 * If target attr area is less than the temp's
1125 * (diff < 0) write a big attr to the temp file to knock
1126 * the attr out of local format.
1127 * (This should actually *increase* the temp file's
1128 * forkoffset when the attr moves out of the inode)
1132 memset(val
, 'X', 2048);
1133 if (fsetxattr(tfd
, name
, val
, 2048, 0)) {
1134 fsrprintf(_("big ATTR set failed\n"));
1137 /* Go back & see where we're at now */
1143 * make a progress check so we don't get stuck trying to extend
1144 * a large btree form attribute fork.
1146 if (last_forkoff
== tbstat
.bs_forkoff
) {
1147 if (no_change_cnt
++ > 10)
1149 } else /* progress! */
1151 last_forkoff
= tbstat
.bs_forkoff
;
1153 /* work out which way to grow the fork */
1154 if (abs(diff
) > fsgeom
.inodesize
- sizeof(struct xfs_dinode
)) {
1155 fsrprintf(_("forkoff diff %d too large!\n"), diff
);
1160 * if the temp inode fork offset is still smaller then we have
1161 * to grow the data fork
1165 * create some temporary extents in the inode to move
1166 * the fork in the direction we need. This can be done
1167 * by preallocating some single block extents at
1168 * non-contiguous offsets.
1170 /* XXX: unimplemented! */
1172 printf(_("data fork growth unimplemented\n"));
1176 /* we need to grow the attr fork, so create another attr */
1177 ret
= fsetxattr(tfd
, name
, "XX", 2, XATTR_CREATE
);
1179 fsrprintf(_("could not set ATTR\n"));
1183 } while (++i
< 100); /* don't go forever */
1187 fsrprintf(_("set temp attr\n"));
1188 /* We failed to resolve the fork difference */
1190 fsrprintf(_("failed to match fork offset\n"));;
1192 #endif /* HAVE_FSETXATTR */
1197 * Do the defragmentation of a single file.
1198 * We already are pretty sure we can and want to
1199 * defragment the file. Create the tmp file, copy
1200 * the data (maintaining holes) and call the kernel
1201 * extent swap routine.
1204 * -1: Some error was encountered
1205 * 0: Successfully defragmented the file
1206 * 1: No change / No Error
1209 packfile(char *fname
, char *tname
, int fd
,
1210 xfs_bstat_t
*statp
, struct fsxattr
*fsxp
)
1214 int retval
= -1; /* Failure is the default */
1215 int nextents
, extent
, cur_nextents
, new_nextents
;
1219 static xfs_swapext_t sx
;
1220 struct xfs_flock64 space
;
1224 char ffname
[SMBUFSZ
];
1228 * Work out the extent map - nextents will be set to the
1229 * minimum number of extents needed for the file (taking
1230 * into account holes), cur_nextents is the current number
1233 nextents
= read_fd_bmap(fd
, statp
, &cur_nextents
);
1235 if (cur_nextents
== 1 || cur_nextents
<= nextents
) {
1237 fsrprintf(_("%s already fully defragmented.\n"), fname
);
1238 retval
= 1; /* indicates no change/no error */
1243 fsrprintf(_("%s extents=%d can_save=%d tmp=%s\n"),
1244 fname
, cur_nextents
, (cur_nextents
- nextents
),
1247 if ((tfd
= open(tname
, openopts
, 0666)) < 0) {
1249 fsrprintf(_("could not open tmp file: %s: %s\n"),
1250 tname
, strerror(errno
));
1255 /* Setup extended attributes */
1256 if (fsr_setup_attr_fork(fd
, tfd
, statp
) != 0) {
1257 fsrprintf(_("failed to set ATTR fork on tmp: %s:\n"), tname
);
1261 /* Setup extended inode flags, project identifier, etc */
1262 if (fsxp
->fsx_xflags
|| fsxp
->fsx_projid
) {
1263 if (ioctl(tfd
, FS_IOC_FSSETXATTR
, fsxp
) < 0) {
1264 fsrprintf(_("could not set inode attrs on tmp: %s\n"),
1270 if ((ioctl(tfd
, XFS_IOC_DIOINFO
, &dio
)) < 0 ) {
1271 fsrprintf(_("could not get DirectIO info on tmp: %s\n"), tname
);
1275 dio_min
= dio
.d_miniosz
;
1276 if (statp
->bs_size
<= dio_min
) {
1277 blksz_dio
= dio_min
;
1279 blksz_dio
= min(dio
.d_maxiosz
, BUFFER_MAX
- pagesize
);
1280 if (argv_blksz_dio
!= 0)
1281 blksz_dio
= min(argv_blksz_dio
, blksz_dio
);
1282 blksz_dio
= (min(statp
->bs_size
, blksz_dio
) / dio_min
) * dio_min
;
1286 fsrprintf(_("DEBUG: "
1287 "fsize=%lld blsz_dio=%d d_min=%d d_max=%d pgsz=%d\n"),
1288 statp
->bs_size
, blksz_dio
, dio
.d_miniosz
,
1289 dio
.d_maxiosz
, pagesize
);
1292 if (!(fbuf
= (char *)memalign(dio
.d_mem
, blksz_dio
))) {
1293 fsrprintf(_("could not allocate buf: %s\n"), tname
);
1298 /* Create new tmp file in same AG as first */
1299 sprintf(ffname
, "%s.frag", tname
);
1301 /* Open the new file for sync writes */
1302 if ((ffd
= open(ffname
, openopts
, 0666)) < 0) {
1303 fsrprintf(_("could not open fragfile: %s : %s\n"),
1304 ffname
, strerror(errno
));
1310 /* Loop through block map allocating new extents */
1311 for (extent
= 0; extent
< nextents
; extent
++) {
1312 pos
= outmap
[extent
].bmv_offset
;
1313 if (outmap
[extent
].bmv_block
== -1) {
1314 space
.l_whence
= SEEK_SET
;
1315 space
.l_start
= pos
;
1316 space
.l_len
= outmap
[extent
].bmv_length
;
1317 if (ioctl(tfd
, XFS_IOC_UNRESVSP64
, &space
) < 0) {
1318 fsrprintf(_("could not trunc tmp %s\n"),
1321 if (lseek(tfd
, outmap
[extent
].bmv_length
, SEEK_CUR
) < 0) {
1322 fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
1323 tname
, strerror(errno
));
1327 } else if (outmap
[extent
].bmv_length
== 0) {
1328 /* to catch holes at the beginning of the file */
1332 space
.l_whence
= SEEK_CUR
;
1334 space
.l_len
= outmap
[extent
].bmv_length
;
1336 if (ioctl(tfd
, XFS_IOC_RESVSP64
, &space
) < 0) {
1337 fsrprintf(_("could not pre-allocate tmp space:"
1341 if (lseek(tfd
, outmap
[extent
].bmv_length
, SEEK_CUR
) < 0) {
1342 fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
1343 tname
, strerror(errno
));
1347 } /* end of space allocation loop */
1349 if (lseek(tfd
, 0, SEEK_SET
)) {
1350 fsrprintf(_("Couldn't rewind on temporary file\n"));
1354 /* Check if the temporary file has fewer extents */
1355 new_nextents
= getnextents(tfd
);
1357 fsrprintf(_("Temporary file has %d extents (%d in original)\n"), new_nextents
, cur_nextents
);
1358 if (cur_nextents
<= new_nextents
) {
1360 fsrprintf(_("No improvement will be made (skipping): %s\n"), fname
);
1361 retval
= 1; /* no change/no error */
1365 /* Loop through block map copying the file. */
1366 for (extent
= 0; extent
< nextents
; extent
++) {
1367 pos
= outmap
[extent
].bmv_offset
;
1368 if (outmap
[extent
].bmv_block
== -1) {
1369 if (lseek(tfd
, outmap
[extent
].bmv_length
, SEEK_CUR
) < 0) {
1370 fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
1371 tname
, strerror(errno
));
1374 if (lseek(fd
, outmap
[extent
].bmv_length
, SEEK_CUR
) < 0) {
1375 fsrprintf(_("could not lseek in file: %s : %s\n"),
1376 fname
, strerror(errno
));
1380 } else if (outmap
[extent
].bmv_length
== 0) {
1381 /* to catch holes at the beginning of the file */
1384 for (cnt
= outmap
[extent
].bmv_length
; cnt
> 0;
1385 cnt
-= ct
, pos
+= ct
) {
1386 if (nfrags
&& --nfrags
) {
1387 ct
= min(cnt
, dio_min
);
1388 } else if (cnt
% dio_min
== 0) {
1389 ct
= min(cnt
, blksz_dio
);
1391 ct
= min(cnt
+ dio_min
- (cnt
% dio_min
),
1394 ct
= read(fd
, fbuf
, ct
);
1396 /* EOF, stop trying to read */
1400 /* Ensure we do direct I/O to correct block
1403 if (ct
% dio_min
!= 0) {
1404 wc
= ct
+ dio_min
- (ct
% dio_min
);
1409 if (ct
< 0 || ((wc
= write(tfd
, fbuf
, wc
)) != wc_b4
)) {
1411 fsrprintf(_("bad read of %d bytes "
1412 "from %s: %s\n"), wc_b4
,
1413 fname
, strerror(errno
));
1415 fsrprintf(_("bad write of %d bytes "
1416 "to %s: %s\n"), wc_b4
,
1417 tname
, strerror(errno
));
1420 * Might be out of space
1422 * Try to finish write
1426 if ((wc
= write(tfd
, ((char *)fbuf
)+wc
,
1428 /* worked on second attempt? */
1432 fsrprintf(_("bad write2 of %d "
1433 "bytes to %s: %s\n"),
1437 fsrprintf(_("bad copy to %s\n"),
1444 /* Do a matching write to the tmp file */
1446 if (((wc
= write(ffd
, fbuf
, wc
)) != wc_b4
)) {
1447 fsrprintf(_("bad write of %d bytes "
1449 wc_b4
, ffname
, strerror(errno
));
1454 if (ftruncate(tfd
, statp
->bs_size
) < 0) {
1455 fsrprintf(_("could not truncate tmpfile: %s : %s\n"),
1456 fname
, strerror(errno
));
1459 if (fsync(tfd
) < 0) {
1460 fsrprintf(_("could not fsync tmpfile: %s : %s\n"),
1461 fname
, strerror(errno
));
1465 sx
.sx_stat
= *statp
; /* struct copy */
1466 sx
.sx_version
= XFS_SX_VERSION
;
1467 sx
.sx_fdtarget
= fd
;
1470 sx
.sx_length
= statp
->bs_size
;
1472 /* switch to the owner's id, to keep quota in line */
1473 if (fchown(tfd
, statp
->bs_uid
, statp
->bs_gid
) < 0) {
1475 fsrprintf(_("failed to fchown tmpfile %s: %s\n"),
1476 tname
, strerror(errno
));
1480 /* Swap the extents */
1481 srval
= xfs_swapext(fd
, &sx
);
1483 if (errno
== ENOTSUP
) {
1485 fsrprintf(_("%s: file type not supported\n"), fname
);
1486 } else if (errno
== EFAULT
) {
1487 /* The file has changed since we started the copy */
1489 fsrprintf(_("%s: file modified defrag aborted\n"),
1491 } else if (errno
== EBUSY
) {
1492 /* Timestamp has changed or mmap'ed file */
1494 fsrprintf(_("%s: file busy\n"), fname
);
1496 fsrprintf(_("XFS_IOC_SWAPEXT failed: %s: %s\n"),
1497 fname
, strerror(errno
));
1502 /* Report progress */
1504 fsrprintf(_("extents before:%d after:%d %s %s\n"),
1505 cur_nextents
, new_nextents
,
1506 (new_nextents
<= nextents
? "DONE" : " " ),
1520 gettmpname(char *fname
)
1522 static char buf
[PATH_MAX
+1];
1526 sprintf(sbuf
, "/.fsr%d", getpid());
1528 strncpy(buf
, fname
, PATH_MAX
);
1529 buf
[PATH_MAX
] = '\0';
1530 ptr
= strrchr(buf
, '/');
1537 if ((strlen(buf
) + strlen (sbuf
)) > PATH_MAX
) {
1538 fsrprintf(_("tmp file name too long: %s\n"), fname
);
1548 getparent(char *fname
)
1550 static char buf
[PATH_MAX
+1];
1553 strncpy(buf
, fname
, PATH_MAX
);
1554 buf
[PATH_MAX
] = '\0';
1555 ptr
= strrchr(buf
, '/');
1568 * Read in block map of the input file, coalesce contiguous
1569 * extents into a single range, keep all holes. Convert from 512 byte
1572 * This code was borrowed from mv.c with some minor mods.
1575 #define OUTMAP_SIZE_INCREMENT MAPSIZE
1577 int read_fd_bmap(int fd
, xfs_bstat_t
*sin
, int *cur_nextents
)
1580 struct getbmap map
[MAPSIZE
];
1583 if (++cnt >= outmap_size) { \
1584 outmap_size += OUTMAP_SIZE_INCREMENT; \
1585 outmap = (struct getbmap *)realloc(outmap, \
1586 outmap_size*sizeof(*outmap)); \
1587 if (outmap == NULL) { \
1588 fsrprintf(_("realloc failed: %s\n"), \
1594 /* Initialize the outmap array. It always grows - never shrinks.
1595 * Left-over memory allocation is saved for the next files.
1597 if (outmap_size
== 0) {
1598 outmap_size
= OUTMAP_SIZE_INCREMENT
; /* Initial size */
1599 outmap
= (struct getbmap
*)malloc(outmap_size
*sizeof(*outmap
));
1601 fsrprintf(_("malloc failed: %s\n"),
1607 outmap
[0].bmv_block
= 0;
1608 outmap
[0].bmv_offset
= 0;
1609 outmap
[0].bmv_length
= sin
->bs_size
;
1612 * If a non regular file is involved then forget holes
1615 if (!S_ISREG(sin
->bs_mode
))
1618 outmap
[0].bmv_length
= 0;
1620 map
[0].bmv_offset
= 0;
1621 map
[0].bmv_block
= 0;
1622 map
[0].bmv_entries
= 0;
1623 map
[0].bmv_count
= MAPSIZE
;
1624 map
[0].bmv_length
= -1;
1630 if (ioctl(fd
, XFS_IOC_GETBMAP
, map
) < 0) {
1631 fsrprintf(_("failed reading extents: inode %llu"),
1632 (unsigned long long)sin
->bs_ino
);
1636 /* Concatenate extents together and replicate holes into
1639 *cur_nextents
+= map
[0].bmv_entries
;
1640 for (i
= 0; i
< map
[0].bmv_entries
; i
++) {
1641 if (map
[i
+ 1].bmv_block
== -1) {
1643 outmap
[cnt
] = map
[i
+1];
1644 } else if (outmap
[cnt
].bmv_block
== -1) {
1646 outmap
[cnt
] = map
[i
+1];
1648 outmap
[cnt
].bmv_length
+= map
[i
+ 1].bmv_length
;
1651 } while (map
[0].bmv_entries
== (MAPSIZE
-1));
1652 for (i
= 0; i
<= cnt
; i
++) {
1653 outmap
[i
].bmv_offset
= BBTOB(outmap
[i
].bmv_offset
);
1654 outmap
[i
].bmv_length
= BBTOB(outmap
[i
].bmv_length
);
1657 outmap
[cnt
].bmv_length
= sin
->bs_size
- outmap
[cnt
].bmv_offset
;
1663 * Read the block map and return the number of extents.
1669 struct getbmap map
[MAPSIZE
];
1671 map
[0].bmv_offset
= 0;
1672 map
[0].bmv_block
= 0;
1673 map
[0].bmv_entries
= 0;
1674 map
[0].bmv_count
= MAPSIZE
;
1675 map
[0].bmv_length
= -1;
1680 if (ioctl(fd
,XFS_IOC_GETBMAP
, map
) < 0) {
1681 fsrprintf(_("failed reading extents"));
1685 nextents
+= map
[0].bmv_entries
;
1686 } while (map
[0].bmv_entries
== (MAPSIZE
-1));
1692 * Get the fs geometry
1695 xfs_getgeom(int fd
, xfs_fsop_geom_v1_t
* fsgeom
)
1697 if (xfs_fsgeometry(fd
, fsgeom
) < 0) {
1704 * Get xfs realtime space information
1707 xfs_getrt(int fd
, struct statvfs64
*sfbp
)
1709 unsigned long bsize
;
1710 unsigned long factor
;
1711 xfs_fsop_counts_t cnt
;
1713 if (!fsgeom
.rtblocks
)
1716 if (xfs_fscounts(fd
, &cnt
) < 0) {
1721 bsize
= (sfbp
->f_frsize
? sfbp
->f_frsize
: sfbp
->f_bsize
);
1723 bsize
= sfbp
->f_bsize
;
1725 factor
= fsgeom
.blocksize
/ bsize
; /* currently this is == 1 */
1726 sfbp
->f_bfree
= (cnt
.freertx
* fsgeom
.rtextsize
) * factor
;
1731 fsrprintf(const char *fmt
, ...)
1737 static int didopenlog
;
1739 openlog("fsr", LOG_PID
, LOG_USER
);
1742 vsyslog(LOG_INFO
, fmt
, ap
);
1750 * Initialize a directory for tmp file use. This is used
1751 * by the full filesystem defragmentation when we're walking
1752 * the inodes and do not know the path for the individual
1753 * files. Multiple directories are used to spread out the
1754 * tmp data around to different ag's (since file data is
1755 * usually allocated to the same ag as the directory and
1756 * directories allocated round robin from the same
1757 * parent directory).
1763 static char buf
[SMBUFSZ
];
1767 sprintf(buf
, "%s/.fsr", mnt
);
1770 if (mkdir(buf
, 0700) < 0) {
1771 if (errno
== EEXIST
) {
1773 fsrprintf(_("tmpdir already exists: %s\n"),
1776 fsrprintf(_("could not create tmpdir: %s: %s\n"),
1777 buf
, strerror(errno
));
1781 for (i
=0; i
< fsgeom
.agcount
; i
++) {
1782 sprintf(buf
, "%s/.fsr/ag%d", mnt
, i
);
1783 if (mkdir(buf
, 0700) < 0) {
1784 if (errno
== EEXIST
) {
1787 _("tmpdir already exists: %s\n"), buf
);
1789 fsrprintf(_("cannot create tmpdir: %s: %s\n"),
1790 buf
, strerror(errno
));
1802 static char buf
[SMBUFSZ
];
1804 sprintf(buf
, "%s/.fsr/ag%d/tmp%d",
1805 ( (strcmp(mnt
, "/") == 0) ? "" : mnt
),
1809 if (++tmp_agi
== fsgeom
.agcount
)
1816 tmp_close(char *mnt
)
1818 static char buf
[SMBUFSZ
];
1821 /* No data is ever actually written so we can just do rmdir's */
1822 for (i
=0; i
< fsgeom
.agcount
; i
++) {
1823 sprintf(buf
, "%s/.fsr/ag%d", mnt
, i
);
1824 if (rmdir(buf
) < 0) {
1825 if (errno
!= ENOENT
) {
1827 _("could not remove tmpdir: %s: %s\n"),
1828 buf
, strerror(errno
));
1832 sprintf(buf
, "%s/.fsr", mnt
);
1833 if (rmdir(buf
) < 0) {
1834 if (errno
!= ENOENT
) {
1835 fsrprintf(_("could not remove tmpdir: %s: %s\n"),
1836 buf
, strerror(errno
));