2 * Copyright (c) 2000-2005 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
28 #define rounddown(x, y) (((x)/(y))*(y))
29 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
31 extern int platform_check_ismounted(char *, char *, struct stat
*, int);
36 char LOGFILE_NAME
[] = "/var/tmp/xfs_copy.log.XXXXXX";
41 unsigned int source_blocksize
; /* source filesystem blocksize */
42 unsigned int source_sectorsize
; /* source disk sectorsize */
44 xfs_agblock_t first_agbno
;
46 uint64_t barcount
[11];
48 unsigned int num_targets
;
49 target_control
*target
;
57 thread_control glob_masks
;
60 pthread_mutex_t mainwait
;
65 xfs_off_t
write_log_trailer(int fd
, wbuf
*w
, xfs_mount_t
*mp
);
66 xfs_off_t
write_log_header(int fd
, wbuf
*w
, xfs_mount_t
*mp
);
67 static int format_logs(struct xfs_mount
*);
69 /* general purpose message reporting routine */
71 #define OUT 0x01 /* use stdout stream */
72 #define ERR 0x02 /* use stderr stream */
73 #define LOG 0x04 /* use logerr stream */
74 #define PRE 0x08 /* append strerror string */
75 #define LAST 0x10 /* final message we print */
78 signal_maskfunc(int addset
, int newset
)
83 sigaddset(&set
, addset
);
84 sigprocmask(newset
, &set
, NULL
);
88 do_message(int flags
, int code
, const char *fmt
, ...)
95 if (vfprintf(logerr
, fmt
, ap
) <= 0)
100 flags
|= ERR
; /* failed, force stderr */
103 vfprintf(stderr
, fmt
, ap
);
105 } else if (flags
& OUT
) {
107 vfprintf(stdout
, fmt
, ap
);
112 do_message(flags
& ~PRE
, 0, ": %s\n", strerror(code
));
115 _("Check logfile \"%s\" for more details\n"),
119 /* logfile is broken, force a write to stderr */
121 fprintf(stderr
, _("%s: could not write to logfile \"%s\".\n"),
122 progname
, logfile_name
);
124 _("Aborting XFS copy -- logfile error -- reason: %s\n"),
130 #define do_out(args...) do_message(OUT|LOG, 0, ## args)
131 #define do_log(args...) do_message(ERR|LOG, 0, ## args)
132 #define do_warn(args...) do_message(LOG, 0, ## args)
133 #define do_error(e,s) do_message(ERR|LOG|PRE, e, s)
134 #define do_fatal(e,s) do_message(ERR|LOG|PRE|LAST, e, s)
135 #define do_vfatal(e,s,args...) do_message(ERR|LOG|PRE|LAST, e, s, ## args)
136 #define die_perror() \
138 do_message(ERR|LOG|PRE|LAST, errno, \
139 _("Aborting XFS copy - reason")); \
143 /* workaround craziness in the xlog routines */
144 int xlog_recover_do_trans(struct xlog
*log
, struct xlog_recover
*t
, int p
)
152 int i
, first_error
= 0;
154 for (i
= 0; i
< num_targets
; i
++) {
155 if (target
[i
].state
== INACTIVE
) {
156 if (first_error
== 0) {
159 _("THE FOLLOWING COPIES FAILED TO COMPLETE\n"));
161 do_log(" %s -- ", target
[i
].name
);
162 if (target
[i
].err_type
== 0)
163 do_log(_("write error"));
165 do_log(_("lseek error"));
166 do_log(_(" at offset %lld\n"), target
[i
].position
);
169 if (first_error
== 0) {
170 fprintf(stdout
, _("All copies completed.\n"));
173 fprintf(stderr
, _("See \"%s\" for more details.\n"),
180 * don't have to worry about alignment and mins because those
181 * are taken care of when the buffer's read in
194 if (target
[args
->id
].position
!= buf
->position
) {
195 if (lseek(args
->fd
, buf
->position
, SEEK_SET
) < 0) {
196 error
= target
[args
->id
].err_type
= 1;
198 target
[args
->id
].position
= buf
->position
;
202 if ((res
= write(target
[args
->id
].fd
, buf
->data
,
203 buf
->length
)) == buf
->length
) {
204 target
[args
->id
].position
+= res
;
210 target
[args
->id
].error
= errno
;
211 target
[args
->id
].position
= buf
->position
;
217 begin_reader(void *arg
)
219 thread_args
*args
= arg
;
222 pthread_mutex_lock(&args
->wait
);
223 if (do_write(args
, NULL
))
225 pthread_mutex_lock(&glob_masks
.mutex
);
226 if (--glob_masks
.num_working
== 0)
227 pthread_mutex_unlock(&mainwait
);
228 pthread_mutex_unlock(&glob_masks
.mutex
);
233 /* error will be logged by primary thread */
235 pthread_mutex_lock(&glob_masks
.mutex
);
236 target
[args
->id
].state
= INACTIVE
;
237 if (--glob_masks
.num_working
== 0)
238 pthread_mutex_unlock(&mainwait
);
239 pthread_mutex_unlock(&glob_masks
.mutex
);
254 for (i
= 0; i
< num_targets
; i
++) {
255 if (target
[i
].pid
== pid
) {
256 if (target
[i
].state
== INACTIVE
) {
257 /* thread got an I/O error */
259 if (target
[i
].err_type
== 0) {
261 _("%s: write error on target %d \"%s\" at offset %lld\n"),
262 progname
, i
, target
[i
].name
,
266 _("%s: lseek error on target %d \"%s\" at offset %lld\n"),
267 progname
, i
, target
[i
].name
,
271 do_vfatal(target
[i
].error
,
272 _("Aborting target %d - reason"), i
);
276 _("Aborting XFS copy - no more targets.\n"));
281 signal(SIGCHLD
, handler
);
284 /* it just croaked it bigtime, log it */
287 _("%s: thread %d died unexpectedly, target \"%s\" incomplete\n"),
288 progname
, i
, target
[i
].name
);
289 do_warn(_("%s: offset was probably %lld\n"),
290 progname
, target
[i
].position
);
291 do_fatal(target
[i
].error
,
292 _("Aborting XFS copy - reason"));
298 /* unknown child -- something very wrong */
300 do_warn(_("%s: Unknown child died (should never happen!)\n"), progname
);
303 signal(SIGCHLD
, handler
);
310 _("Usage: %s [-bdV] [-L logfile] source target [target ...]\n"),
316 init_bar(uint64_t source_blocks
)
320 for (i
= 0; i
< 11; i
++)
321 barcount
[i
] = (source_blocks
/10)*i
;
325 bump_bar(int tenths
, uint64_t numblocks
)
327 static char *bar
[11] = {
342 printf("%s", bar
[10]);
345 while (tenths
< 10 && numblocks
> barcount
[tenths
]) {
346 printf("%s", bar
[tenths
]);
354 static xfs_off_t source_position
= -1;
357 wbuf_init(wbuf
*buf
, int data_size
, int data_align
, int min_io_size
, int id
)
359 ASSERT(data_size
% BBSIZE
== 0);
360 while ((buf
->data
= memalign(data_align
, data_size
)) == NULL
) {
362 if (data_size
< min_io_size
)
365 ASSERT(min_io_size
% BBSIZE
== 0);
366 buf
->data_align
= data_align
;
367 buf
->min_io_size
= min_io_size
;
368 buf
->size
= data_size
;
374 read_wbuf(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
381 newpos
= rounddown(buf
->position
, (xfs_off_t
) buf
->min_io_size
);
383 if (newpos
!= buf
->position
) {
384 diff
= buf
->position
- newpos
;
385 buf
->position
= newpos
;
390 if (source_position
!= buf
->position
) {
391 lres
= lseek(fd
, buf
->position
, SEEK_SET
);
393 do_warn(_("%s: lseek failure at offset %lld\n"),
394 progname
, source_position
);
397 source_position
= buf
->position
;
400 ASSERT(source_position
% source_sectorsize
== 0);
402 /* round up length for direct I/O if necessary */
404 if (buf
->length
% buf
->min_io_size
!= 0)
405 buf
->length
= roundup(buf
->length
, buf
->min_io_size
);
407 if (buf
->length
> buf
->size
) {
408 do_warn(_("assert error: buf->length = %d, buf->size = %d\n"),
409 buf
->length
, buf
->size
);
413 if ((res
= read(fd
, buf
->data
, buf
->length
)) < 0) {
414 do_warn(_("%s: read failure at offset %lld\n"),
415 progname
, source_position
);
419 if (res
< buf
->length
&&
420 source_position
+ res
== mp
->m_sb
.sb_dblocks
* source_blocksize
)
423 ASSERT(res
== buf
->length
);
424 source_position
+= res
;
429 read_ag_header(int fd
, xfs_agnumber_t agno
, wbuf
*buf
, ag_header_t
*ag
,
430 xfs_mount_t
*mp
, int blocksize
, int sectorsize
)
437 /* initial settings */
440 off
= XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
);
441 buf
->position
= (xfs_off_t
) off
* (xfs_off_t
) BBSIZE
;
442 length
= buf
->length
= first_agbno
* blocksize
;
444 do_log(_("ag header buffer invalid!\n"));
448 /* handle alignment stuff */
450 newpos
= rounddown(buf
->position
, (xfs_off_t
) buf
->min_io_size
);
451 if (newpos
!= buf
->position
) {
452 diff
= buf
->position
- newpos
;
453 buf
->position
= newpos
;
457 /* round up length for direct I/O if necessary */
459 if (buf
->length
% buf
->min_io_size
!= 0)
460 buf
->length
= roundup(buf
->length
, buf
->min_io_size
);
462 read_wbuf(fd
, buf
, mp
);
463 ASSERT(buf
->length
>= length
);
465 ag
->xfs_sb
= (xfs_dsb_t
*) (buf
->data
+ diff
);
466 ASSERT(be32_to_cpu(ag
->xfs_sb
->sb_magicnum
) == XFS_SB_MAGIC
);
467 ag
->xfs_agf
= (xfs_agf_t
*) (buf
->data
+ diff
+ sectorsize
);
468 ASSERT(be32_to_cpu(ag
->xfs_agf
->agf_magicnum
) == XFS_AGF_MAGIC
);
469 ag
->xfs_agi
= (xfs_agi_t
*) (buf
->data
+ diff
+ 2 * sectorsize
);
470 ASSERT(be32_to_cpu(ag
->xfs_agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
471 ag
->xfs_agfl
= (xfs_agfl_t
*) (buf
->data
+ diff
+ 3 * sectorsize
);
481 /* verify target threads */
482 for (i
= 0; i
< num_targets
; i
++)
483 if (target
[i
].state
!= INACTIVE
)
484 glob_masks
.num_working
++;
486 /* release target threads */
487 for (i
= 0; i
< num_targets
; i
++)
488 if (target
[i
].state
!= INACTIVE
)
489 pthread_mutex_unlock(&targ
[i
].wait
); /* wake up */
494 * If all the targets are inactive then there won't be any io
495 * threads left to release mainwait. We're screwed, so bail out.
497 if (badness
== num_targets
) {
502 signal_maskfunc(SIGCHLD
, SIG_UNBLOCK
);
503 pthread_mutex_lock(&mainwait
);
504 signal_maskfunc(SIGCHLD
, SIG_BLOCK
);
509 xfs_sb_t
*sb
, /* Original fs superblock */
510 ag_header_t
*ag_hdr
, /* AG hdr to update for this copy */
511 thread_args
*tcarg
) /* Args for this thread, with UUID */
514 * If this filesystem has CRCs, the original UUID is stamped into
515 * all metadata. If we don't have an existing meta_uuid field in the
516 * the original filesystem and we are changing the UUID in this copy,
517 * we must copy the original sb_uuid to the sb_meta_uuid slot and set
518 * the incompat flag for the feature on this copy.
520 if (xfs_sb_version_hascrc(sb
) && !xfs_sb_version_hasmetauuid(sb
) &&
521 !uuid_equal(&tcarg
->uuid
, &sb
->sb_uuid
)) {
524 feat
= be32_to_cpu(ag_hdr
->xfs_sb
->sb_features_incompat
);
525 feat
|= XFS_SB_FEAT_INCOMPAT_META_UUID
;
526 ag_hdr
->xfs_sb
->sb_features_incompat
= cpu_to_be32(feat
);
527 platform_uuid_copy(&ag_hdr
->xfs_sb
->sb_meta_uuid
,
531 /* Copy the (possibly new) fs-identifier UUID into sb_uuid */
532 platform_uuid_copy(&ag_hdr
->xfs_sb
->sb_uuid
, &tcarg
->uuid
);
534 /* We may have changed the UUID, so update the superblock CRC */
535 if (xfs_sb_version_hascrc(sb
))
536 xfs_update_cksum((char *)ag_hdr
->xfs_sb
, sb
->sb_sectsize
,
541 main(int argc
, char **argv
)
549 uint64_t size
, sizeb
;
550 uint64_t numblocks
= 0;
557 int source_is_file
= 0;
558 int buffered_output
= 0;
560 uint btree_levels
, current_level
;
567 xfs_agnumber_t num_ags
, agno
;
569 xfs_daddr_t begin
, next_begin
, ag_begin
, new_begin
, ag_end
;
570 struct xfs_btree_block
*block
;
571 xfs_alloc_ptr_t
*ptr
;
572 xfs_alloc_rec_t
*rec_ptr
;
579 progname
= basename(argv
[0]);
581 setlocale(LC_ALL
, "");
582 bindtextdomain(PACKAGE
, LOCALEDIR
);
585 while ((c
= getopt(argc
, argv
, "bdL:V")) != EOF
) {
594 logfile_name
= optarg
;
597 printf(_("%s version %s\n"), progname
, VERSION
);
604 if (argc
- optind
< 2)
608 logfd
= open(logfile_name
, O_CREAT
|O_WRONLY
|O_EXCL
, 0600);
610 logfile_name
= LOGFILE_NAME
;
611 logfd
= mkstemp(logfile_name
);
615 fprintf(stderr
, _("%s: couldn't open log file \"%s\"\n"),
616 progname
, logfile_name
);
617 perror(_("Aborting XFS copy - reason"));
621 if ((logerr
= fdopen(logfd
, "w")) == NULL
) {
622 fprintf(stderr
, _("%s: couldn't set up logfile stream\n"),
624 perror(_("Aborting XFS copy - reason"));
628 source_name
= argv
[optind
];
632 num_targets
= argc
- optind
;
633 if ((target
= malloc(sizeof(target_control
) * num_targets
)) == NULL
) {
634 do_log(_("Couldn't allocate target array\n"));
637 for (i
= 0; optind
< argc
; i
++, optind
++) {
638 target
[i
].name
= argv
[optind
];
640 target
[i
].position
= -1;
641 target
[i
].state
= INACTIVE
;
643 target
[i
].err_type
= 0;
646 parent_pid
= getpid();
648 /* open up source -- is it a file? */
650 open_flags
= O_RDONLY
;
652 if ((source_fd
= open(source_name
, open_flags
)) < 0) {
653 do_log(_("%s: couldn't open source \"%s\"\n"),
654 progname
, source_name
);
658 if (fstat(source_fd
, &statbuf
) < 0) {
659 do_log(_("%s: couldn't stat source \"%s\"\n"),
660 progname
, source_name
);
664 if (S_ISREG(statbuf
.st_mode
))
667 if (source_is_file
&& platform_test_xfs_fd(source_fd
)) {
668 if (fcntl(source_fd
, F_SETFL
, open_flags
| O_DIRECT
) < 0) {
669 do_log(_("%s: Cannot set direct I/O flag on \"%s\".\n"),
670 progname
, source_name
);
673 if (xfsctl(source_name
, source_fd
, XFS_IOC_DIOINFO
, &d
) < 0) {
674 do_log(_("%s: xfsctl on file \"%s\" failed.\n"),
675 progname
, source_name
);
679 wbuf_align
= d
.d_mem
;
680 wbuf_size
= MIN(d
.d_maxiosz
, 1 * 1024 * 1024);
681 wbuf_miniosize
= d
.d_miniosz
;
683 /* set arbitrary I/O params, miniosize at least 1 disk block */
685 wbuf_align
= getpagesize();
686 wbuf_size
= 1 * 1024 * 1024;
687 wbuf_miniosize
= -1; /* set after mounting source fs */
690 if (!source_is_file
) {
692 * check to make sure a filesystem isn't mounted
695 if (platform_check_ismounted(source_name
, NULL
, &statbuf
, 0)) {
697 _("%s: Warning -- a filesystem is mounted on the source device.\n"),
700 _("\t\tGenerated copies may be corrupt unless the source is\n"));
702 _("\t\tunmounted or mounted read-only. Copy proceeding...\n"));
706 /* prepare the libxfs_init structure */
708 memset(&xargs
, 0, sizeof(xargs
));
709 xargs
.isdirect
= LIBXFS_DIRECT
;
710 xargs
.isreadonly
= LIBXFS_ISREADONLY
;
712 if (source_is_file
) {
713 xargs
.dname
= source_name
;
716 xargs
.volname
= source_name
;
718 if (!libxfs_init(&xargs
)) {
719 do_log(_("%s: couldn't initialize XFS library\n"
720 "%s: Aborting.\n"), progname
, progname
);
724 memset(&mbuf
, 0, sizeof(xfs_mount_t
));
726 /* We don't yet know the sector size, so read maximal size */
727 libxfs_buftarg_init(&mbuf
, xargs
.ddev
, xargs
.logdev
, xargs
.rtdev
);
728 sbp
= libxfs_readbuf(mbuf
.m_ddev_targp
, XFS_SB_DADDR
,
729 1 << (XFS_MAX_SECTORSIZE_LOG
- BBSHIFT
), 0, NULL
);
731 libxfs_sb_from_disk(sb
, XFS_BUF_TO_SBP(sbp
));
733 /* Do it again, now with proper length and verifier */
735 libxfs_purgebuf(sbp
);
736 sbp
= libxfs_readbuf(mbuf
.m_ddev_targp
, XFS_SB_DADDR
,
737 1 << (sb
->sb_sectlog
- BBSHIFT
),
741 mp
= libxfs_mount(&mbuf
, sb
, xargs
.ddev
, xargs
.logdev
, xargs
.rtdev
, 0);
743 do_log(_("%s: %s filesystem failed to initialize\n"
744 "%s: Aborting.\n"), progname
, source_name
, progname
);
746 } else if (mp
->m_sb
.sb_inprogress
) {
747 do_log(_("%s %s filesystem failed to initialize\n"
748 "%s: Aborting.\n"), progname
, source_name
, progname
);
750 } else if (mp
->m_sb
.sb_logstart
== 0) {
751 do_log(_("%s: %s has an external log.\n%s: Aborting.\n"),
752 progname
, source_name
, progname
);
754 } else if (mp
->m_sb
.sb_rextents
!= 0) {
755 do_log(_("%s: %s has a real-time section.\n"
756 "%s: Aborting.\n"), progname
, source_name
, progname
);
762 * Set up the mount pointer to access the log and check whether the log
763 * is clean. Fail on a dirty or corrupt log in non-duplicate mode
764 * because the log is formatted as part of the copy and we don't want to
765 * destroy data. We also need the current log cycle to format v5
766 * superblock logs correctly.
768 memset(&xlog
, 0, sizeof(struct xlog
));
770 c
= xlog_is_dirty(mp
, mp
->m_log
, &xargs
, 0);
774 "Error: source filesystem log is dirty. Mount the filesystem to replay the\n"
775 "log, unmount and retry xfs_copy.\n"));
779 "Error: could not determine the log head or tail of the source filesystem.\n"
780 "Mount the filesystem to replay the log or run xfs_repair.\n"));
785 source_blocksize
= mp
->m_sb
.sb_blocksize
;
786 source_sectorsize
= mp
->m_sb
.sb_sectsize
;
788 if (wbuf_miniosize
== -1)
789 wbuf_miniosize
= source_sectorsize
;
791 ASSERT(source_blocksize
% source_sectorsize
== 0);
792 ASSERT(source_sectorsize
% BBSIZE
== 0);
794 if (source_blocksize
< source_sectorsize
) {
795 do_log(_("Error: filesystem block size is smaller than the"
796 " disk sectorsize.\nAborting XFS copy now.\n"));
800 first_agbno
= XFS_AGFL_BLOCK(mp
) + 1;
802 /* now open targets */
806 for (i
= 0; i
< num_targets
; i
++) {
807 int write_last_block
= 0;
809 if (stat(target
[i
].name
, &statbuf
) < 0) {
810 /* ok, assume it's a file and create it */
812 do_out(_("Creating file %s\n"), target
[i
].name
);
814 open_flags
|= O_CREAT
;
815 if (!buffered_output
)
816 open_flags
|= O_DIRECT
;
817 write_last_block
= 1;
818 } else if (S_ISREG(statbuf
.st_mode
)) {
819 open_flags
|= O_TRUNC
;
820 if (!buffered_output
)
821 open_flags
|= O_DIRECT
;
822 write_last_block
= 1;
825 * check to make sure a filesystem isn't mounted
828 if (platform_check_ismounted(target
[i
].name
,
829 NULL
, &statbuf
, 0)) {
830 do_log(_("%s: a filesystem is mounted "
831 "on target device \"%s\".\n"
832 "%s cannot copy to mounted filesystems."
834 progname
, target
[i
].name
, progname
);
839 target
[i
].fd
= open(target
[i
].name
, open_flags
, 0644);
840 if (target
[i
].fd
< 0) {
841 do_log(_("%s: couldn't open target \"%s\"\n"),
842 progname
, target
[i
].name
);
846 if (write_last_block
) {
847 /* ensure regular files are correctly sized */
849 if (ftruncate(target
[i
].fd
, mp
->m_sb
.sb_dblocks
*
851 do_log(_("%s: cannot grow data section.\n"),
855 if (platform_test_xfs_fd(target
[i
].fd
)) {
856 if (xfsctl(target
[i
].name
, target
[i
].fd
,
857 XFS_IOC_DIOINFO
, &d
) < 0) {
859 _("%s: xfsctl on \"%s\" failed.\n"),
860 progname
, target
[i
].name
);
863 wbuf_align
= MAX(wbuf_align
, d
.d_mem
);
864 wbuf_size
= MIN(d
.d_maxiosz
, wbuf_size
);
865 wbuf_miniosize
= MAX(d
.d_miniosz
,
870 char *lb
[XFS_MAX_SECTORSIZE
] = { NULL
};
873 /* ensure device files are sufficiently large */
875 off
= mp
->m_sb
.sb_dblocks
* source_blocksize
;
877 if (pwrite(target
[i
].fd
, lb
, sizeof(lb
), off
) < 0) {
878 do_log(_("%s: failed to write last block\n"),
880 do_log(_("\tIs target \"%s\" too small?\n"),
887 /* initialize locks and bufs */
889 if (pthread_mutex_init(&glob_masks
.mutex
, NULL
) != 0) {
890 do_log(_("Couldn't initialize global thread mask\n"));
893 glob_masks
.num_working
= 0;
895 if (wbuf_init(&w_buf
, wbuf_size
, wbuf_align
,
896 wbuf_miniosize
, 0) == NULL
) {
897 do_log(_("Error initializing wbuf 0\n"));
901 wblocks
= wbuf_size
/ BBSIZE
;
903 if (wbuf_init(&btree_buf
, MAX(source_blocksize
, wbuf_miniosize
),
904 wbuf_align
, wbuf_miniosize
, 1) == NULL
) {
905 do_log(_("Error initializing btree buf 1\n"));
909 if (pthread_mutex_init(&mainwait
,NULL
) != 0) {
910 do_log(_("Error creating first semaphore.\n"));
914 /* need to start out blocking */
915 pthread_mutex_lock(&mainwait
);
917 /* set up sigchild signal handler */
919 signal(SIGCHLD
, handler
);
920 signal_maskfunc(SIGCHLD
, SIG_BLOCK
);
924 if ((targ
= malloc(num_targets
* sizeof(thread_args
))) == NULL
) {
925 do_log(_("Couldn't malloc space for thread args\n"));
930 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++, tcarg
++) {
932 platform_uuid_generate(&tcarg
->uuid
);
934 platform_uuid_copy(&tcarg
->uuid
, &mp
->m_sb
.sb_uuid
);
936 if (pthread_mutex_init(&tcarg
->wait
, NULL
) != 0) {
937 do_log(_("Error creating thread mutex %d\n"), i
);
941 /* need to start out blocking */
942 pthread_mutex_lock(&tcarg
->wait
);
945 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++, tcarg
++) {
947 tcarg
->fd
= target
[i
].fd
;
949 target
[i
].state
= ACTIVE
;
952 if (pthread_create(&target
[i
].pid
, NULL
,
953 begin_reader
, (void *)tcarg
)) {
954 do_log(_("Error creating thread for target %d\n"), i
);
959 ASSERT(num_targets
== num_threads
);
961 /* set up statistics */
963 num_ags
= mp
->m_sb
.sb_agcount
;
965 init_bar(mp
->m_sb
.sb_blocksize
/ BBSIZE
966 * ((uint64_t)mp
->m_sb
.sb_dblocks
967 - (uint64_t)mp
->m_sb
.sb_fdblocks
+ 10 * num_ags
));
971 for (agno
= 0; agno
< num_ags
&& kids
> 0; agno
++) {
972 /* read in first blocks of the ag */
974 read_ag_header(source_fd
, agno
, &w_buf
, &ag_hdr
, mp
,
975 source_blocksize
, source_sectorsize
);
977 /* set the in_progress bit for the first AG */
980 ag_hdr
.xfs_sb
->sb_inprogress
= 1;
982 /* save what we need (agf) in the btree buffer */
984 memmove(btree_buf
.data
, ag_hdr
.xfs_agf
, source_sectorsize
);
985 ag_hdr
.xfs_agf
= (xfs_agf_t
*) btree_buf
.data
;
986 btree_buf
.length
= source_blocksize
;
988 /* write the ag header out */
992 /* traverse btree until we get to the leftmost leaf node */
994 bno
= be32_to_cpu(ag_hdr
.xfs_agf
->agf_roots
[XFS_BTNUM_BNOi
]);
996 btree_levels
= be32_to_cpu(ag_hdr
.xfs_agf
->
997 agf_levels
[XFS_BTNUM_BNOi
]);
999 ag_end
= XFS_AGB_TO_DADDR(mp
, agno
,
1000 be32_to_cpu(ag_hdr
.xfs_agf
->agf_length
) - 1)
1001 + source_blocksize
/ BBSIZE
;
1004 /* none of this touches the w_buf buffer */
1006 if (current_level
>= btree_levels
) {
1008 _("Error: current level %d >= btree levels %d\n"),
1009 current_level
, btree_levels
);
1015 btree_buf
.position
= pos
= (xfs_off_t
)
1016 XFS_AGB_TO_DADDR(mp
,agno
,bno
) << BBSHIFT
;
1017 btree_buf
.length
= source_blocksize
;
1019 read_wbuf(source_fd
, &btree_buf
, mp
);
1020 block
= (struct xfs_btree_block
*)
1021 ((char *)btree_buf
.data
+
1022 pos
- btree_buf
.position
);
1024 if (be32_to_cpu(block
->bb_magic
) !=
1025 (xfs_sb_version_hascrc(&mp
->m_sb
) ?
1026 XFS_ABTB_CRC_MAGIC
: XFS_ABTB_MAGIC
)) {
1027 do_log(_("Bad btree magic 0x%x\n"),
1028 be32_to_cpu(block
->bb_magic
));
1032 if (be16_to_cpu(block
->bb_level
) == 0)
1035 ptr
= XFS_ALLOC_PTR_ADDR(mp
, block
, 1,
1036 mp
->m_alloc_mxr
[1]);
1037 bno
= be32_to_cpu(ptr
[0]);
1040 /* align first data copy but don't overwrite ag header */
1042 pos
= w_buf
.position
>> BBSHIFT
;
1043 length
= w_buf
.length
>> BBSHIFT
;
1044 next_begin
= pos
+ length
;
1045 ag_begin
= next_begin
;
1047 ASSERT(w_buf
.position
% source_sectorsize
== 0);
1049 /* handle the rest of the ag */
1052 if (be16_to_cpu(block
->bb_level
) != 0) {
1054 _("WARNING: source filesystem inconsistent.\n"));
1056 _(" A leaf btree rec isn't a leaf. Aborting now.\n"));
1060 rec_ptr
= XFS_ALLOC_REC_ADDR(mp
, block
, 1);
1061 for (i
= 0; i
< be16_to_cpu(block
->bb_numrecs
);
1063 /* calculate in daddr's */
1068 * protect against pathological case of a
1069 * hole right after the ag header in a
1073 if (begin
< ag_begin
)
1077 * round size up to ensure we copy a
1078 * range bigger than required
1081 sizeb
= XFS_AGB_TO_DADDR(mp
, agno
,
1082 be32_to_cpu(rec_ptr
->ar_startblock
)) -
1084 size
= roundup(sizeb
<<BBSHIFT
, wbuf_miniosize
);
1088 w_buf
.position
= (xfs_off_t
)
1093 * let lower layer do alignment
1095 if (size
> w_buf
.size
) {
1096 w_buf
.length
= w_buf
.size
;
1099 numblocks
+= wblocks
;
1101 w_buf
.length
= size
;
1106 read_wbuf(source_fd
, &w_buf
, mp
);
1109 w_buf
.position
+= w_buf
.length
;
1116 /* round next starting point down */
1118 new_begin
= XFS_AGB_TO_DADDR(mp
, agno
,
1119 be32_to_cpu(rec_ptr
->ar_startblock
) +
1120 be32_to_cpu(rec_ptr
->ar_blockcount
));
1121 next_begin
= rounddown(new_begin
,
1122 w_buf
.min_io_size
>> BBSHIFT
);
1125 if (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
)
1128 /* read in next btree record block */
1130 btree_buf
.position
= pos
= (xfs_off_t
)
1131 XFS_AGB_TO_DADDR(mp
, agno
, be32_to_cpu(
1132 block
->bb_u
.s
.bb_rightsib
)) << BBSHIFT
;
1133 btree_buf
.length
= source_blocksize
;
1135 /* let read_wbuf handle alignment */
1137 read_wbuf(source_fd
, &btree_buf
, mp
);
1139 block
= (struct xfs_btree_block
*)
1140 ((char *) btree_buf
.data
+
1141 pos
- btree_buf
.position
);
1143 ASSERT(be32_to_cpu(block
->bb_magic
) == XFS_ABTB_MAGIC
||
1144 be32_to_cpu(block
->bb_magic
) == XFS_ABTB_CRC_MAGIC
);
1148 * write out range of used blocks after last range
1149 * of free blocks in AG
1151 if (next_begin
< ag_end
) {
1154 sizeb
= ag_end
- begin
;
1155 size
= roundup(sizeb
<< BBSHIFT
, wbuf_miniosize
);
1160 w_buf
.position
= (xfs_off_t
) begin
<< BBSHIFT
;
1164 * let lower layer do alignment
1166 if (size
> w_buf
.size
) {
1167 w_buf
.length
= w_buf
.size
;
1170 numblocks
+= wblocks
;
1172 w_buf
.length
= size
;
1177 read_wbuf(source_fd
, &w_buf
, mp
);
1180 w_buf
.position
+= w_buf
.length
;
1182 howfar
= bump_bar(howfar
, numblocks
);
1190 /* write a clean log using the specified UUID */
1195 /* reread and rewrite superblocks (UUID and in-progress) */
1196 /* [backwards, so inprogress bit only updated when done] */
1198 for (i
= num_ags
- 1; i
>= 0; i
--) {
1199 read_ag_header(source_fd
, i
, &w_buf
, &ag_hdr
, mp
,
1200 source_blocksize
, source_sectorsize
);
1202 ag_hdr
.xfs_sb
->sb_inprogress
= 0;
1204 /* do each thread in turn, each has its own UUID */
1206 for (j
= 0, tcarg
= targ
; j
< num_targets
; j
++) {
1207 sb_update_uuid(sb
, &ag_hdr
, tcarg
);
1208 do_write(tcarg
, NULL
);
1224 next_log_chunk(char *p
, int offset
, void *private)
1226 wbuf
*buf
= (wbuf
*)private;
1228 if (buf
->length
< (int)(p
- buf
->data
) + offset
) {
1229 /* need to flush this one, then start afresh */
1231 do_write(buf
->owner
, NULL
);
1232 memset(buf
->data
, 0, buf
->length
);
1239 * Writes a log header at the start of the log (with the real
1240 * filesystem UUID embedded into it), and writes to all targets.
1242 * Returns the next buffer-length-aligned disk address.
1245 write_log_header(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
1247 char *p
= buf
->data
;
1251 logstart
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1252 buf
->position
= rounddown(logstart
, (xfs_off_t
)buf
->length
);
1254 memset(p
, 0, buf
->size
);
1255 if (logstart
% buf
->length
) { /* unaligned */
1256 read_wbuf(fd
, buf
, mp
);
1257 offset
= logstart
- buf
->position
;
1259 memset(p
, 0, buf
->length
- offset
);
1262 offset
= libxfs_log_header(p
, &buf
->owner
->uuid
,
1263 xfs_sb_version_haslogv2(&mp
->m_sb
) ? 2 : 1,
1264 mp
->m_sb
.sb_logsunit
, XLOG_FMT
, NULLCOMMITLSN
,
1265 NULLCOMMITLSN
, next_log_chunk
, buf
);
1266 do_write(buf
->owner
, NULL
);
1268 return roundup(logstart
+ offset
, buf
->length
);
1272 * May do an aligned read of the last buffer in the log (& zero
1273 * the start of that buffer). Returns the disk address at the
1274 * end of last aligned buffer in the log.
1277 write_log_trailer(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
1282 logend
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1283 logend
+= XFS_FSB_TO_B(mp
, mp
->m_sb
.sb_logblocks
);
1285 buf
->position
= rounddown(logend
, (xfs_off_t
)buf
->length
);
1287 if (logend
% buf
->length
) { /* unaligned */
1288 read_wbuf(fd
, buf
, mp
);
1289 offset
= (int)(logend
- buf
->position
);
1290 memset(buf
->data
, 0, offset
);
1291 do_write(buf
->owner
, NULL
);
1294 return buf
->position
;
1298 * Clear a log by writing a record at the head, the tail and zeroing everything
1303 struct xfs_mount
*mp
,
1309 w_buf
.owner
= tcarg
;
1310 w_buf
.length
= rounddown(w_buf
.size
, w_buf
.min_io_size
);
1311 pos
= write_log_header(source_fd
, &w_buf
, mp
);
1312 end_pos
= write_log_trailer(source_fd
, &w_buf
, mp
);
1313 w_buf
.position
= pos
;
1314 memset(w_buf
.data
, 0, w_buf
.length
);
1316 while (w_buf
.position
< end_pos
) {
1317 do_write(tcarg
, NULL
);
1318 w_buf
.position
+= w_buf
.length
;
1323 * Format the log to a particular cycle number. This is required for version 5
1324 * superblock filesystems to provide metadata LSN validity guarantees.
1328 struct xfs_mount
*mp
,
1334 int cycle
= XLOG_INIT_CYCLE
;
1337 buf
->length
= buf
->size
;
1338 buf
->position
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1340 logstart
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logstart
);
1341 length
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logblocks
);
1344 * Bump the cycle number on v5 superblock filesystems to guarantee that
1345 * all existing metadata LSNs are valid (behind the current LSN) on the
1348 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1349 cycle
= mp
->m_log
->l_curr_cycle
+ 1;
1352 * Format the entire log into the memory buffer and write it out. If the
1353 * write fails, mark the target inactive so the failure is reported.
1355 libxfs_log_clear(NULL
, buf
->data
, logstart
, length
, &buf
->owner
->uuid
,
1356 xfs_sb_version_haslogv2(&mp
->m_sb
) ? 2 : 1,
1357 mp
->m_sb
.sb_logsunit
, XLOG_FMT
, cycle
, true);
1358 if (do_write(buf
->owner
, buf
))
1359 target
[tcarg
->id
].state
= INACTIVE
;
1364 struct xfs_mount
*mp
)
1371 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1372 logsize
= XFS_FSB_TO_B(mp
, mp
->m_sb
.sb_logblocks
);
1373 if (!wbuf_init(&logbuf
, logsize
, w_buf
.data_align
,
1374 w_buf
.min_io_size
, w_buf
.id
))
1378 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++) {
1379 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1380 format_log(mp
, tcarg
, &logbuf
);
1382 clear_log(mp
, tcarg
);
1386 if (xfs_sb_version_hascrc(&mp
->m_sb
))