1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
16 #define rounddown(x, y) (((x)/(y))*(y))
17 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
19 extern int platform_check_ismounted(char *, char *, struct stat
*, int);
21 static char *logfile_name
;
23 static char LOGFILE_NAME
[] = "/var/tmp/xfs_copy.log.XXXXXX";
25 static char *source_name
;
28 static unsigned int source_blocksize
; /* source filesystem blocksize */
29 static unsigned int source_sectorsize
; /* source disk sectorsize */
31 static xfs_agblock_t first_agbno
;
33 static uint64_t barcount
[11];
35 static unsigned int num_targets
;
36 static target_control
*target
;
39 static wbuf btree_buf
;
41 static unsigned int kids
;
43 static thread_control glob_masks
;
44 static thread_args
*targ
;
46 static pthread_mutex_t mainwait
;
51 xfs_off_t
write_log_trailer(int fd
, wbuf
*w
, xfs_mount_t
*mp
);
52 xfs_off_t
write_log_header(int fd
, wbuf
*w
, xfs_mount_t
*mp
);
53 static int format_logs(struct xfs_mount
*);
55 /* general purpose message reporting routine */
57 #define OUT 0x01 /* use stdout stream */
58 #define ERR 0x02 /* use stderr stream */
59 #define LOG 0x04 /* use logerr stream */
60 #define PRE 0x08 /* append strerror string */
61 #define LAST 0x10 /* final message we print */
64 signal_maskfunc(int addset
, int newset
)
69 sigaddset(&set
, addset
);
70 sigprocmask(newset
, &set
, NULL
);
74 do_message(int flags
, int code
, const char *fmt
, ...)
81 if (vfprintf(logerr
, fmt
, ap
) <= 0)
86 flags
|= ERR
; /* failed, force stderr */
89 vfprintf(stderr
, fmt
, ap
);
91 } else if (flags
& OUT
) {
93 vfprintf(stdout
, fmt
, ap
);
98 do_message(flags
& ~PRE
, 0, ": %s\n", strerror(code
));
101 _("Check logfile \"%s\" for more details\n"),
105 /* logfile is broken, force a write to stderr */
107 fprintf(stderr
, _("%s: could not write to logfile \"%s\".\n"),
108 progname
, logfile_name
);
110 _("Aborting XFS copy -- logfile error -- reason: %s\n"),
116 #define do_out(args...) do_message(OUT|LOG, 0, ## args)
117 #define do_log(args...) do_message(ERR|LOG, 0, ## args)
118 #define do_warn(args...) do_message(LOG, 0, ## args)
119 #define do_error(e,s) do_message(ERR|LOG|PRE, e, s)
120 #define do_fatal(e,s) do_message(ERR|LOG|PRE|LAST, e, s)
121 #define do_vfatal(e,s,args...) do_message(ERR|LOG|PRE|LAST, e, s, ## args)
122 #define die_perror() \
124 do_message(ERR|LOG|PRE|LAST, errno, \
125 _("Aborting XFS copy - reason")); \
129 /* workaround craziness in the xlog routines */
130 int xlog_recover_do_trans(struct xlog
*log
, struct xlog_recover
*t
, int p
)
138 int i
, first_error
= 0;
140 for (i
= 0; i
< num_targets
; i
++) {
141 if (target
[i
].state
== INACTIVE
) {
142 if (first_error
== 0) {
145 _("THE FOLLOWING COPIES FAILED TO COMPLETE\n"));
147 do_log(" %s -- ", target
[i
].name
);
148 if (target
[i
].err_type
== 0)
149 do_log(_("write error"));
151 do_log(_("lseek error"));
152 do_log(_(" at offset %lld\n"), target
[i
].position
);
155 if (first_error
== 0) {
156 fprintf(stdout
, _("All copies completed.\n"));
159 fprintf(stderr
, _("See \"%s\" for more details.\n"),
166 * don't have to worry about alignment and mins because those
167 * are taken care of when the buffer's read in
180 if (target
[args
->id
].position
!= buf
->position
) {
181 if (lseek(args
->fd
, buf
->position
, SEEK_SET
) < 0) {
182 error
= target
[args
->id
].err_type
= 1;
184 target
[args
->id
].position
= buf
->position
;
188 if ((res
= write(target
[args
->id
].fd
, buf
->data
,
189 buf
->length
)) == buf
->length
) {
190 target
[args
->id
].position
+= res
;
196 target
[args
->id
].error
= errno
;
197 target
[args
->id
].position
= buf
->position
;
203 begin_reader(void *arg
)
205 thread_args
*args
= arg
;
208 pthread_mutex_lock(&args
->wait
);
209 if (do_write(args
, NULL
))
211 pthread_mutex_lock(&glob_masks
.mutex
);
212 if (--glob_masks
.num_working
== 0)
213 pthread_mutex_unlock(&mainwait
);
214 pthread_mutex_unlock(&glob_masks
.mutex
);
219 /* error will be logged by primary thread */
221 pthread_mutex_lock(&glob_masks
.mutex
);
222 target
[args
->id
].state
= INACTIVE
;
223 if (--glob_masks
.num_working
== 0)
224 pthread_mutex_unlock(&mainwait
);
225 pthread_mutex_unlock(&glob_masks
.mutex
);
240 for (i
= 0; i
< num_targets
; i
++) {
241 if (target
[i
].pid
== pid
) {
242 if (target
[i
].state
== INACTIVE
) {
243 /* thread got an I/O error */
245 if (target
[i
].err_type
== 0) {
247 _("%s: write error on target %d \"%s\" at offset %lld\n"),
248 progname
, i
, target
[i
].name
,
252 _("%s: lseek error on target %d \"%s\" at offset %lld\n"),
253 progname
, i
, target
[i
].name
,
257 do_vfatal(target
[i
].error
,
258 _("Aborting target %d - reason"), i
);
262 _("Aborting XFS copy - no more targets.\n"));
267 signal(SIGCHLD
, handler
);
270 /* it just croaked it bigtime, log it */
273 _("%s: thread %d died unexpectedly, target \"%s\" incomplete\n"),
274 progname
, i
, target
[i
].name
);
275 do_warn(_("%s: offset was probably %lld\n"),
276 progname
, target
[i
].position
);
277 do_fatal(target
[i
].error
,
278 _("Aborting XFS copy - reason"));
284 /* unknown child -- something very wrong */
286 do_warn(_("%s: Unknown child died (should never happen!)\n"), progname
);
289 signal(SIGCHLD
, handler
);
296 _("Usage: %s [-bdV] [-L logfile] source target [target ...]\n"),
302 init_bar(uint64_t source_blocks
)
306 for (i
= 0; i
< 11; i
++)
307 barcount
[i
] = (source_blocks
/10)*i
;
311 bump_bar(int tenths
, uint64_t numblocks
)
313 static char *bar
[11] = {
328 printf("%s", bar
[10]);
331 while (tenths
< 10 && numblocks
> barcount
[tenths
]) {
332 printf("%s", bar
[tenths
]);
340 static xfs_off_t source_position
= -1;
343 wbuf_init(wbuf
*buf
, int data_size
, int data_align
, int min_io_size
, int id
)
345 ASSERT(data_size
% BBSIZE
== 0);
346 while ((buf
->data
= memalign(data_align
, data_size
)) == NULL
) {
348 if (data_size
< min_io_size
)
351 ASSERT(min_io_size
% BBSIZE
== 0);
352 buf
->data_align
= data_align
;
353 buf
->min_io_size
= min_io_size
;
354 buf
->size
= data_size
;
360 read_wbuf(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
367 newpos
= rounddown(buf
->position
, (xfs_off_t
) buf
->min_io_size
);
369 if (newpos
!= buf
->position
) {
370 diff
= buf
->position
- newpos
;
371 buf
->position
= newpos
;
376 if (source_position
!= buf
->position
) {
377 lres
= lseek(fd
, buf
->position
, SEEK_SET
);
379 do_warn(_("%s: lseek failure at offset %lld\n"),
380 progname
, source_position
);
383 source_position
= buf
->position
;
386 ASSERT(source_position
% source_sectorsize
== 0);
388 /* round up length for direct I/O if necessary */
390 if (buf
->length
% buf
->min_io_size
!= 0)
391 buf
->length
= roundup(buf
->length
, buf
->min_io_size
);
393 if (buf
->length
> buf
->size
) {
394 do_warn(_("assert error: buf->length = %d, buf->size = %d\n"),
395 buf
->length
, buf
->size
);
399 if ((res
= read(fd
, buf
->data
, buf
->length
)) < 0) {
400 do_warn(_("%s: read failure at offset %lld\n"),
401 progname
, source_position
);
405 if (res
< buf
->length
&&
406 source_position
+ res
== mp
->m_sb
.sb_dblocks
* source_blocksize
)
409 ASSERT(res
== buf
->length
);
410 source_position
+= res
;
415 read_ag_header(int fd
, xfs_agnumber_t agno
, wbuf
*buf
, ag_header_t
*ag
,
416 xfs_mount_t
*mp
, int blocksize
, int sectorsize
)
423 /* initial settings */
426 off
= XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
);
427 buf
->position
= (xfs_off_t
) off
* (xfs_off_t
) BBSIZE
;
428 length
= buf
->length
= first_agbno
* blocksize
;
430 do_log(_("ag header buffer invalid!\n"));
434 /* handle alignment stuff */
436 newpos
= rounddown(buf
->position
, (xfs_off_t
) buf
->min_io_size
);
437 if (newpos
!= buf
->position
) {
438 diff
= buf
->position
- newpos
;
439 buf
->position
= newpos
;
443 /* round up length for direct I/O if necessary */
445 if (buf
->length
% buf
->min_io_size
!= 0)
446 buf
->length
= roundup(buf
->length
, buf
->min_io_size
);
448 read_wbuf(fd
, buf
, mp
);
449 ASSERT(buf
->length
>= length
);
451 ag
->xfs_sb
= (xfs_dsb_t
*) (buf
->data
+ diff
);
452 ASSERT(be32_to_cpu(ag
->xfs_sb
->sb_magicnum
) == XFS_SB_MAGIC
);
453 ag
->xfs_agf
= (xfs_agf_t
*) (buf
->data
+ diff
+ sectorsize
);
454 ASSERT(be32_to_cpu(ag
->xfs_agf
->agf_magicnum
) == XFS_AGF_MAGIC
);
455 ag
->xfs_agi
= (xfs_agi_t
*) (buf
->data
+ diff
+ 2 * sectorsize
);
456 ASSERT(be32_to_cpu(ag
->xfs_agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
457 ag
->xfs_agfl
= (xfs_agfl_t
*) (buf
->data
+ diff
+ 3 * sectorsize
);
467 /* verify target threads */
468 for (i
= 0; i
< num_targets
; i
++)
469 if (target
[i
].state
!= INACTIVE
)
470 glob_masks
.num_working
++;
472 /* release target threads */
473 for (i
= 0; i
< num_targets
; i
++)
474 if (target
[i
].state
!= INACTIVE
)
475 pthread_mutex_unlock(&targ
[i
].wait
); /* wake up */
480 * If all the targets are inactive then there won't be any io
481 * threads left to release mainwait. We're screwed, so bail out.
483 if (badness
== num_targets
) {
488 signal_maskfunc(SIGCHLD
, SIG_UNBLOCK
);
489 pthread_mutex_lock(&mainwait
);
490 signal_maskfunc(SIGCHLD
, SIG_BLOCK
);
495 xfs_sb_t
*sb
, /* Original fs superblock */
496 ag_header_t
*ag_hdr
, /* AG hdr to update for this copy */
497 thread_args
*tcarg
) /* Args for this thread, with UUID */
500 * If this filesystem has CRCs, the original UUID is stamped into
501 * all metadata. If we don't have an existing meta_uuid field in the
502 * the original filesystem and we are changing the UUID in this copy,
503 * we must copy the original sb_uuid to the sb_meta_uuid slot and set
504 * the incompat flag for the feature on this copy.
506 if (xfs_sb_version_hascrc(sb
) && !xfs_sb_version_hasmetauuid(sb
) &&
507 !uuid_equal(&tcarg
->uuid
, &sb
->sb_uuid
)) {
510 feat
= be32_to_cpu(ag_hdr
->xfs_sb
->sb_features_incompat
);
511 feat
|= XFS_SB_FEAT_INCOMPAT_META_UUID
;
512 ag_hdr
->xfs_sb
->sb_features_incompat
= cpu_to_be32(feat
);
513 platform_uuid_copy(&ag_hdr
->xfs_sb
->sb_meta_uuid
,
517 /* Copy the (possibly new) fs-identifier UUID into sb_uuid */
518 platform_uuid_copy(&ag_hdr
->xfs_sb
->sb_uuid
, &tcarg
->uuid
);
520 /* We may have changed the UUID, so update the superblock CRC */
521 if (xfs_sb_version_hascrc(sb
))
522 xfs_update_cksum((char *)ag_hdr
->xfs_sb
, sb
->sb_sectsize
,
527 main(int argc
, char **argv
)
536 uint64_t size
, sizeb
;
537 uint64_t numblocks
= 0;
544 int source_is_file
= 0;
545 int buffered_output
= 0;
547 uint btree_levels
, current_level
;
554 xfs_agnumber_t num_ags
, agno
;
556 xfs_daddr_t begin
, next_begin
, ag_begin
, new_begin
, ag_end
;
557 struct xfs_btree_block
*block
;
558 xfs_alloc_ptr_t
*ptr
;
559 xfs_alloc_rec_t
*rec_ptr
;
566 progname
= basename(argv
[0]);
568 setlocale(LC_ALL
, "");
569 bindtextdomain(PACKAGE
, LOCALEDIR
);
572 while ((c
= getopt(argc
, argv
, "bdL:V")) != EOF
) {
581 logfile_name
= optarg
;
584 printf(_("%s version %s\n"), progname
, VERSION
);
591 if (argc
- optind
< 2)
595 logfd
= open(logfile_name
, O_CREAT
|O_WRONLY
|O_EXCL
, 0600);
597 logfile_name
= LOGFILE_NAME
;
598 logfd
= mkstemp(logfile_name
);
602 fprintf(stderr
, _("%s: couldn't open log file \"%s\"\n"),
603 progname
, logfile_name
);
604 perror(_("Aborting XFS copy - reason"));
608 if ((logerr
= fdopen(logfd
, "w")) == NULL
) {
609 fprintf(stderr
, _("%s: couldn't set up logfile stream\n"),
611 perror(_("Aborting XFS copy - reason"));
615 source_name
= argv
[optind
];
619 num_targets
= argc
- optind
;
620 if ((target
= malloc(sizeof(target_control
) * num_targets
)) == NULL
) {
621 do_log(_("Couldn't allocate target array\n"));
624 for (i
= 0; optind
< argc
; i
++, optind
++) {
625 target
[i
].name
= argv
[optind
];
627 target
[i
].position
= -1;
628 target
[i
].state
= INACTIVE
;
630 target
[i
].err_type
= 0;
633 /* open up source -- is it a file? */
635 open_flags
= O_RDONLY
;
637 if ((source_fd
= open(source_name
, open_flags
)) < 0) {
638 do_log(_("%s: couldn't open source \"%s\"\n"),
639 progname
, source_name
);
643 if (fstat(source_fd
, &statbuf
) < 0) {
644 do_log(_("%s: couldn't stat source \"%s\"\n"),
645 progname
, source_name
);
649 if (S_ISREG(statbuf
.st_mode
))
652 if (source_is_file
&& platform_test_xfs_fd(source_fd
)) {
653 if (fcntl(source_fd
, F_SETFL
, open_flags
| O_DIRECT
) < 0) {
654 do_log(_("%s: Cannot set direct I/O flag on \"%s\".\n"),
655 progname
, source_name
);
658 if (xfsctl(source_name
, source_fd
, XFS_IOC_DIOINFO
, &d
) < 0) {
659 do_log(_("%s: xfsctl on file \"%s\" failed.\n"),
660 progname
, source_name
);
664 wbuf_align
= d
.d_mem
;
665 wbuf_size
= min(d
.d_maxiosz
, 1 * 1024 * 1024);
666 wbuf_miniosize
= d
.d_miniosz
;
668 /* set arbitrary I/O params, miniosize at least 1 disk block */
670 wbuf_align
= getpagesize();
671 wbuf_size
= 1 * 1024 * 1024;
672 wbuf_miniosize
= -1; /* set after mounting source fs */
675 if (!source_is_file
) {
677 * check to make sure a filesystem isn't mounted
680 if (platform_check_ismounted(source_name
, NULL
, &statbuf
, 0)) {
682 _("%s: Warning -- a filesystem is mounted on the source device.\n"),
685 _("\t\tGenerated copies may be corrupt unless the source is\n"));
687 _("\t\tunmounted or mounted read-only. Copy proceeding...\n"));
691 /* prepare the libxfs_init structure */
693 memset(&xargs
, 0, sizeof(xargs
));
694 xargs
.isdirect
= LIBXFS_DIRECT
;
695 xargs
.isreadonly
= LIBXFS_ISREADONLY
;
697 if (source_is_file
) {
698 xargs
.dname
= source_name
;
701 xargs
.volname
= source_name
;
703 if (!libxfs_init(&xargs
)) {
704 do_log(_("%s: couldn't initialize XFS library\n"
705 "%s: Aborting.\n"), progname
, progname
);
709 memset(&mbuf
, 0, sizeof(xfs_mount_t
));
711 /* We don't yet know the sector size, so read maximal size */
712 libxfs_buftarg_init(&mbuf
, xargs
.ddev
, xargs
.logdev
, xargs
.rtdev
);
713 sbp
= libxfs_readbuf(mbuf
.m_ddev_targp
, XFS_SB_DADDR
,
714 1 << (XFS_MAX_SECTORSIZE_LOG
- BBSHIFT
), 0, NULL
);
716 libxfs_sb_from_disk(sb
, XFS_BUF_TO_SBP(sbp
));
718 /* Do it again, now with proper length and verifier */
720 libxfs_purgebuf(sbp
);
721 sbp
= libxfs_readbuf(mbuf
.m_ddev_targp
, XFS_SB_DADDR
,
722 1 << (sb
->sb_sectlog
- BBSHIFT
),
726 mp
= libxfs_mount(&mbuf
, sb
, xargs
.ddev
, xargs
.logdev
, xargs
.rtdev
, 0);
728 do_log(_("%s: %s filesystem failed to initialize\n"
729 "%s: Aborting.\n"), progname
, source_name
, progname
);
731 } else if (mp
->m_sb
.sb_inprogress
) {
732 do_log(_("%s %s filesystem failed to initialize\n"
733 "%s: Aborting.\n"), progname
, source_name
, progname
);
735 } else if (mp
->m_sb
.sb_logstart
== 0) {
736 do_log(_("%s: %s has an external log.\n%s: Aborting.\n"),
737 progname
, source_name
, progname
);
739 } else if (mp
->m_sb
.sb_rextents
!= 0) {
740 do_log(_("%s: %s has a real-time section.\n"
741 "%s: Aborting.\n"), progname
, source_name
, progname
);
747 * Set up the mount pointer to access the log and check whether the log
748 * is clean. Fail on a dirty or corrupt log in non-duplicate mode
749 * because the log is formatted as part of the copy and we don't want to
750 * destroy data. We also need the current log cycle to format v5
751 * superblock logs correctly.
753 memset(&xlog
, 0, sizeof(struct xlog
));
755 c
= xlog_is_dirty(mp
, mp
->m_log
, &xargs
, 0);
759 "Error: source filesystem log is dirty. Mount the filesystem to replay the\n"
760 "log, unmount and retry xfs_copy.\n"));
764 "Error: could not determine the log head or tail of the source filesystem.\n"
765 "Mount the filesystem to replay the log or run xfs_repair.\n"));
770 source_blocksize
= mp
->m_sb
.sb_blocksize
;
771 source_sectorsize
= mp
->m_sb
.sb_sectsize
;
773 if (wbuf_miniosize
== -1)
774 wbuf_miniosize
= source_sectorsize
;
776 ASSERT(source_blocksize
% source_sectorsize
== 0);
777 ASSERT(source_sectorsize
% BBSIZE
== 0);
779 if (source_blocksize
< source_sectorsize
) {
780 do_log(_("Error: filesystem block size is smaller than the"
781 " disk sectorsize.\nAborting XFS copy now.\n"));
785 first_agbno
= XFS_AGFL_BLOCK(mp
) + 1;
787 /* now open targets */
791 for (i
= 0; i
< num_targets
; i
++) {
792 int write_last_block
= 0;
794 if (stat(target
[i
].name
, &statbuf
) < 0) {
795 /* ok, assume it's a file and create it */
797 do_out(_("Creating file %s\n"), target
[i
].name
);
799 open_flags
|= O_CREAT
;
800 if (!buffered_output
)
801 open_flags
|= O_DIRECT
;
802 write_last_block
= 1;
803 } else if (S_ISREG(statbuf
.st_mode
)) {
804 open_flags
|= O_TRUNC
;
805 if (!buffered_output
)
806 open_flags
|= O_DIRECT
;
807 write_last_block
= 1;
810 * check to make sure a filesystem isn't mounted
813 if (platform_check_ismounted(target
[i
].name
,
814 NULL
, &statbuf
, 0)) {
815 do_log(_("%s: a filesystem is mounted "
816 "on target device \"%s\".\n"
817 "%s cannot copy to mounted filesystems."
819 progname
, target
[i
].name
, progname
);
824 target
[i
].fd
= open(target
[i
].name
, open_flags
, 0644);
825 if (target
[i
].fd
< 0) {
826 do_log(_("%s: couldn't open target \"%s\"\n"),
827 progname
, target
[i
].name
);
831 if (write_last_block
) {
832 /* ensure regular files are correctly sized */
834 if (ftruncate(target
[i
].fd
, mp
->m_sb
.sb_dblocks
*
836 do_log(_("%s: cannot grow data section.\n"),
840 if (platform_test_xfs_fd(target
[i
].fd
)) {
841 if (xfsctl(target
[i
].name
, target
[i
].fd
,
842 XFS_IOC_DIOINFO
, &d
) < 0) {
844 _("%s: xfsctl on \"%s\" failed.\n"),
845 progname
, target
[i
].name
);
848 wbuf_align
= max(wbuf_align
, d
.d_mem
);
849 wbuf_size
= min(d
.d_maxiosz
, wbuf_size
);
850 wbuf_miniosize
= max(d
.d_miniosz
,
855 char *lb
[XFS_MAX_SECTORSIZE
] = { NULL
};
858 /* ensure device files are sufficiently large */
860 off
= mp
->m_sb
.sb_dblocks
* source_blocksize
;
862 if (pwrite(target
[i
].fd
, lb
, sizeof(lb
), off
) < 0) {
863 do_log(_("%s: failed to write last block\n"),
865 do_log(_("\tIs target \"%s\" too small?\n"),
872 /* initialize locks and bufs */
874 if (pthread_mutex_init(&glob_masks
.mutex
, NULL
) != 0) {
875 do_log(_("Couldn't initialize global thread mask\n"));
878 glob_masks
.num_working
= 0;
880 if (wbuf_init(&w_buf
, wbuf_size
, wbuf_align
,
881 wbuf_miniosize
, 0) == NULL
) {
882 do_log(_("Error initializing wbuf 0\n"));
886 wblocks
= wbuf_size
/ BBSIZE
;
888 if (wbuf_init(&btree_buf
, max(source_blocksize
, wbuf_miniosize
),
889 wbuf_align
, wbuf_miniosize
, 1) == NULL
) {
890 do_log(_("Error initializing btree buf 1\n"));
894 if (pthread_mutex_init(&mainwait
,NULL
) != 0) {
895 do_log(_("Error creating first semaphore.\n"));
899 /* need to start out blocking */
900 pthread_mutex_lock(&mainwait
);
902 /* set up sigchild signal handler */
904 signal(SIGCHLD
, handler
);
905 signal_maskfunc(SIGCHLD
, SIG_BLOCK
);
909 if ((targ
= malloc(num_targets
* sizeof(thread_args
))) == NULL
) {
910 do_log(_("Couldn't malloc space for thread args\n"));
915 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++, tcarg
++) {
917 platform_uuid_generate(&tcarg
->uuid
);
919 platform_uuid_copy(&tcarg
->uuid
, &mp
->m_sb
.sb_uuid
);
921 if (pthread_mutex_init(&tcarg
->wait
, NULL
) != 0) {
922 do_log(_("Error creating thread mutex %d\n"), i
);
926 /* need to start out blocking */
927 pthread_mutex_lock(&tcarg
->wait
);
930 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++, tcarg
++) {
932 tcarg
->fd
= target
[i
].fd
;
934 target
[i
].state
= ACTIVE
;
937 if (pthread_create(&target
[i
].pid
, NULL
,
938 begin_reader
, (void *)tcarg
)) {
939 do_log(_("Error creating thread for target %d\n"), i
);
944 ASSERT(num_targets
== num_threads
);
946 /* set up statistics */
948 num_ags
= mp
->m_sb
.sb_agcount
;
950 init_bar(mp
->m_sb
.sb_blocksize
/ BBSIZE
951 * ((uint64_t)mp
->m_sb
.sb_dblocks
952 - (uint64_t)mp
->m_sb
.sb_fdblocks
+ 10 * num_ags
));
956 for (agno
= 0; agno
< num_ags
&& kids
> 0; agno
++) {
957 /* read in first blocks of the ag */
959 read_ag_header(source_fd
, agno
, &w_buf
, &ag_hdr
, mp
,
960 source_blocksize
, source_sectorsize
);
962 /* set the in_progress bit for the first AG */
965 ag_hdr
.xfs_sb
->sb_inprogress
= 1;
967 /* save what we need (agf) in the btree buffer */
969 memmove(btree_buf
.data
, ag_hdr
.xfs_agf
, source_sectorsize
);
970 ag_hdr
.xfs_agf
= (xfs_agf_t
*) btree_buf
.data
;
971 btree_buf
.length
= source_blocksize
;
973 /* write the ag header out */
977 /* traverse btree until we get to the leftmost leaf node */
979 bno
= be32_to_cpu(ag_hdr
.xfs_agf
->agf_roots
[XFS_BTNUM_BNOi
]);
981 btree_levels
= be32_to_cpu(ag_hdr
.xfs_agf
->
982 agf_levels
[XFS_BTNUM_BNOi
]);
984 ag_end
= XFS_AGB_TO_DADDR(mp
, agno
,
985 be32_to_cpu(ag_hdr
.xfs_agf
->agf_length
) - 1)
986 + source_blocksize
/ BBSIZE
;
989 /* none of this touches the w_buf buffer */
991 if (current_level
>= btree_levels
) {
993 _("Error: current level %d >= btree levels %d\n"),
994 current_level
, btree_levels
);
1000 btree_buf
.position
= pos
= (xfs_off_t
)
1001 XFS_AGB_TO_DADDR(mp
,agno
,bno
) << BBSHIFT
;
1002 btree_buf
.length
= source_blocksize
;
1004 read_wbuf(source_fd
, &btree_buf
, mp
);
1005 block
= (struct xfs_btree_block
*)
1006 ((char *)btree_buf
.data
+
1007 pos
- btree_buf
.position
);
1009 if (be32_to_cpu(block
->bb_magic
) !=
1010 (xfs_sb_version_hascrc(&mp
->m_sb
) ?
1011 XFS_ABTB_CRC_MAGIC
: XFS_ABTB_MAGIC
)) {
1012 do_log(_("Bad btree magic 0x%x\n"),
1013 be32_to_cpu(block
->bb_magic
));
1017 if (be16_to_cpu(block
->bb_level
) == 0)
1020 ptr
= XFS_ALLOC_PTR_ADDR(mp
, block
, 1,
1021 mp
->m_alloc_mxr
[1]);
1022 bno
= be32_to_cpu(ptr
[0]);
1025 /* align first data copy but don't overwrite ag header */
1027 pos
= w_buf
.position
>> BBSHIFT
;
1028 length
= w_buf
.length
>> BBSHIFT
;
1029 next_begin
= pos
+ length
;
1030 ag_begin
= next_begin
;
1032 ASSERT(w_buf
.position
% source_sectorsize
== 0);
1034 /* handle the rest of the ag */
1037 if (be16_to_cpu(block
->bb_level
) != 0) {
1039 _("WARNING: source filesystem inconsistent.\n"));
1041 _(" A leaf btree rec isn't a leaf. Aborting now.\n"));
1045 rec_ptr
= XFS_ALLOC_REC_ADDR(mp
, block
, 1);
1046 for (i
= 0; i
< be16_to_cpu(block
->bb_numrecs
);
1048 /* calculate in daddr's */
1053 * protect against pathological case of a
1054 * hole right after the ag header in a
1058 if (begin
< ag_begin
)
1062 * round size up to ensure we copy a
1063 * range bigger than required
1066 sizeb
= XFS_AGB_TO_DADDR(mp
, agno
,
1067 be32_to_cpu(rec_ptr
->ar_startblock
)) -
1069 size
= roundup(sizeb
<<BBSHIFT
, wbuf_miniosize
);
1073 w_buf
.position
= (xfs_off_t
)
1078 * let lower layer do alignment
1080 if (size
> w_buf
.size
) {
1081 w_buf
.length
= w_buf
.size
;
1084 numblocks
+= wblocks
;
1086 w_buf
.length
= size
;
1091 read_wbuf(source_fd
, &w_buf
, mp
);
1094 w_buf
.position
+= w_buf
.length
;
1101 /* round next starting point down */
1103 new_begin
= XFS_AGB_TO_DADDR(mp
, agno
,
1104 be32_to_cpu(rec_ptr
->ar_startblock
) +
1105 be32_to_cpu(rec_ptr
->ar_blockcount
));
1106 next_begin
= rounddown(new_begin
,
1107 w_buf
.min_io_size
>> BBSHIFT
);
1110 if (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
)
1113 /* read in next btree record block */
1115 btree_buf
.position
= pos
= (xfs_off_t
)
1116 XFS_AGB_TO_DADDR(mp
, agno
, be32_to_cpu(
1117 block
->bb_u
.s
.bb_rightsib
)) << BBSHIFT
;
1118 btree_buf
.length
= source_blocksize
;
1120 /* let read_wbuf handle alignment */
1122 read_wbuf(source_fd
, &btree_buf
, mp
);
1124 block
= (struct xfs_btree_block
*)
1125 ((char *) btree_buf
.data
+
1126 pos
- btree_buf
.position
);
1128 ASSERT(be32_to_cpu(block
->bb_magic
) == XFS_ABTB_MAGIC
||
1129 be32_to_cpu(block
->bb_magic
) == XFS_ABTB_CRC_MAGIC
);
1133 * write out range of used blocks after last range
1134 * of free blocks in AG
1136 if (next_begin
< ag_end
) {
1139 sizeb
= ag_end
- begin
;
1140 size
= roundup(sizeb
<< BBSHIFT
, wbuf_miniosize
);
1145 w_buf
.position
= (xfs_off_t
) begin
<< BBSHIFT
;
1149 * let lower layer do alignment
1151 if (size
> w_buf
.size
) {
1152 w_buf
.length
= w_buf
.size
;
1155 numblocks
+= wblocks
;
1157 w_buf
.length
= size
;
1162 read_wbuf(source_fd
, &w_buf
, mp
);
1165 w_buf
.position
+= w_buf
.length
;
1167 howfar
= bump_bar(howfar
, numblocks
);
1175 /* write a clean log using the specified UUID */
1180 /* reread and rewrite superblocks (UUID and in-progress) */
1181 /* [backwards, so inprogress bit only updated when done] */
1183 for (i
= num_ags
- 1; i
>= 0; i
--) {
1184 read_ag_header(source_fd
, i
, &w_buf
, &ag_hdr
, mp
,
1185 source_blocksize
, source_sectorsize
);
1187 ag_hdr
.xfs_sb
->sb_inprogress
= 0;
1189 /* do each thread in turn, each has its own UUID */
1191 for (j
= 0, tcarg
= targ
; j
< num_targets
; j
++) {
1192 sb_update_uuid(sb
, &ag_hdr
, tcarg
);
1193 do_write(tcarg
, NULL
);
1209 next_log_chunk(char *p
, int offset
, void *private)
1211 wbuf
*buf
= (wbuf
*)private;
1213 if (buf
->length
< (int)(p
- buf
->data
) + offset
) {
1214 /* need to flush this one, then start afresh */
1216 do_write(buf
->owner
, NULL
);
1217 memset(buf
->data
, 0, buf
->length
);
1224 * Writes a log header at the start of the log (with the real
1225 * filesystem UUID embedded into it), and writes to all targets.
1227 * Returns the next buffer-length-aligned disk address.
1230 write_log_header(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
1232 char *p
= buf
->data
;
1236 logstart
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1237 buf
->position
= rounddown(logstart
, (xfs_off_t
)buf
->length
);
1239 memset(p
, 0, buf
->size
);
1240 if (logstart
% buf
->length
) { /* unaligned */
1241 read_wbuf(fd
, buf
, mp
);
1242 offset
= logstart
- buf
->position
;
1244 memset(p
, 0, buf
->length
- offset
);
1247 offset
= libxfs_log_header(p
, &buf
->owner
->uuid
,
1248 xfs_sb_version_haslogv2(&mp
->m_sb
) ? 2 : 1,
1249 mp
->m_sb
.sb_logsunit
, XLOG_FMT
, NULLCOMMITLSN
,
1250 NULLCOMMITLSN
, next_log_chunk
, buf
);
1251 do_write(buf
->owner
, NULL
);
1253 return roundup(logstart
+ offset
, buf
->length
);
1257 * May do an aligned read of the last buffer in the log (& zero
1258 * the start of that buffer). Returns the disk address at the
1259 * end of last aligned buffer in the log.
1262 write_log_trailer(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
1267 logend
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1268 logend
+= XFS_FSB_TO_B(mp
, mp
->m_sb
.sb_logblocks
);
1270 buf
->position
= rounddown(logend
, (xfs_off_t
)buf
->length
);
1272 if (logend
% buf
->length
) { /* unaligned */
1273 read_wbuf(fd
, buf
, mp
);
1274 offset
= (int)(logend
- buf
->position
);
1275 memset(buf
->data
, 0, offset
);
1276 do_write(buf
->owner
, NULL
);
1279 return buf
->position
;
1283 * Clear a log by writing a record at the head, the tail and zeroing everything
1288 struct xfs_mount
*mp
,
1294 w_buf
.owner
= tcarg
;
1295 w_buf
.length
= rounddown(w_buf
.size
, w_buf
.min_io_size
);
1296 pos
= write_log_header(source_fd
, &w_buf
, mp
);
1297 end_pos
= write_log_trailer(source_fd
, &w_buf
, mp
);
1298 w_buf
.position
= pos
;
1299 memset(w_buf
.data
, 0, w_buf
.length
);
1301 while (w_buf
.position
< end_pos
) {
1302 do_write(tcarg
, NULL
);
1303 w_buf
.position
+= w_buf
.length
;
1308 * Format the log to a particular cycle number. This is required for version 5
1309 * superblock filesystems to provide metadata LSN validity guarantees.
1313 struct xfs_mount
*mp
,
1319 int cycle
= XLOG_INIT_CYCLE
;
1322 buf
->length
= buf
->size
;
1323 buf
->position
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1325 logstart
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logstart
);
1326 length
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logblocks
);
1329 * Bump the cycle number on v5 superblock filesystems to guarantee that
1330 * all existing metadata LSNs are valid (behind the current LSN) on the
1333 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1334 cycle
= mp
->m_log
->l_curr_cycle
+ 1;
1337 * Format the entire log into the memory buffer and write it out. If the
1338 * write fails, mark the target inactive so the failure is reported.
1340 libxfs_log_clear(NULL
, buf
->data
, logstart
, length
, &buf
->owner
->uuid
,
1341 xfs_sb_version_haslogv2(&mp
->m_sb
) ? 2 : 1,
1342 mp
->m_sb
.sb_logsunit
, XLOG_FMT
, cycle
, true);
1343 if (do_write(buf
->owner
, buf
))
1344 target
[tcarg
->id
].state
= INACTIVE
;
1349 struct xfs_mount
*mp
)
1356 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1357 logsize
= XFS_FSB_TO_B(mp
, mp
->m_sb
.sb_logblocks
);
1358 if (!wbuf_init(&logbuf
, logsize
, w_buf
.data_align
,
1359 w_buf
.min_io_size
, w_buf
.id
))
1363 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++) {
1364 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1365 format_log(mp
, tcarg
, &logbuf
);
1367 clear_log(mp
, tcarg
);
1371 if (xfs_sb_version_hascrc(&mp
->m_sb
))