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
19 #include <xfs/libxfs.h>
27 #define rounddown(x, y) (((x)/(y))*(y))
29 extern int platform_check_ismounted(char *, char *, struct stat64
*, int);
34 char LOGFILE_NAME
[] = "/var/tmp/xfs_copy.log.XXXXXX";
39 unsigned int source_blocksize
; /* source filesystem blocksize */
40 unsigned int source_sectorsize
; /* source disk sectorsize */
42 xfs_agblock_t first_agbno
;
44 __uint64_t barcount
[11];
46 unsigned int num_targets
;
47 target_control
*target
;
55 thread_control glob_masks
;
58 pthread_mutex_t mainwait
;
63 xfs_off_t
write_log_trailer(int fd
, wbuf
*w
, xfs_mount_t
*mp
);
64 xfs_off_t
write_log_header(int fd
, wbuf
*w
, xfs_mount_t
*mp
);
66 /* general purpose message reporting routine */
68 #define OUT 0x01 /* use stdout stream */
69 #define ERR 0x02 /* use stderr stream */
70 #define LOG 0x04 /* use logerr stream */
71 #define PRE 0x08 /* append strerror string */
72 #define LAST 0x10 /* final message we print */
75 do_message(int flags
, int code
, const char *fmt
, ...)
82 if (vfprintf(logerr
, fmt
, ap
) <= 0)
87 flags
|= ERR
; /* failed, force stderr */
90 vfprintf(stderr
, fmt
, ap
);
92 } else if (flags
& OUT
) {
94 vfprintf(stdout
, fmt
, ap
);
99 do_message(flags
& ~PRE
, 0, ": %s\n", strerror(code
));
102 _("Check logfile \"%s\" for more details\n"),
106 /* logfile is broken, force a write to stderr */
108 fprintf(stderr
, _("%s: could not write to logfile \"%s\".\n"),
109 progname
, logfile_name
);
111 _("Aborting XFS copy -- logfile error -- reason: %s\n"),
117 #define do_out(args...) do_message(OUT|LOG, 0, ## args)
118 #define do_log(args...) do_message(ERR|LOG, 0, ## args)
119 #define do_warn(args...) do_message(LOG, 0, ## args)
120 #define do_error(e,s) do_message(ERR|LOG|PRE, e, s)
121 #define do_fatal(e,s) do_message(ERR|LOG|PRE|LAST, e, s)
122 #define do_vfatal(e,s,args...) do_message(ERR|LOG|PRE|LAST, e, s, ## args)
123 #define die_perror() \
125 do_message(ERR|LOG|PRE|LAST, errno, \
126 _("Aborting XFS copy - reason")); \
133 int i
, first_error
= 0;
135 for (i
= 0; i
< num_targets
; i
++) {
136 if (target
[i
].state
== INACTIVE
) {
137 if (first_error
== 0) {
140 _("THE FOLLOWING COPIES FAILED TO COMPLETE\n"));
142 do_log(" %s -- ", target
[i
].name
);
143 if (target
[i
].err_type
== 0)
144 do_log(_("write error"));
146 do_log(_("lseek64 error"));
147 do_log(_(" at offset %lld\n"), target
[i
].position
);
150 if (first_error
== 0) {
151 fprintf(stdout
, _("All copies completed.\n"));
154 fprintf(stderr
, _("See \"%s\" for more details.\n"),
161 * don't have to worry about alignment and mins because those
162 * are taken care of when the buffer's read in
165 do_write(thread_args
*args
)
169 if (target
[args
->id
].position
!= w_buf
.position
) {
170 if (lseek64(args
->fd
, w_buf
.position
, SEEK_SET
) < 0) {
171 error
= target
[args
->id
].err_type
= 1;
173 target
[args
->id
].position
= w_buf
.position
;
177 if ((res
= write(target
[args
->id
].fd
, w_buf
.data
,
178 w_buf
.length
)) == w_buf
.length
) {
179 target
[args
->id
].position
+= res
;
185 target
[args
->id
].error
= errno
;
186 target
[args
->id
].position
= w_buf
.position
;
192 begin_reader(void *arg
)
194 thread_args
*args
= arg
;
197 pthread_mutex_lock(&args
->wait
);
200 pthread_mutex_lock(&glob_masks
.mutex
);
201 if (--glob_masks
.num_working
== 0)
202 pthread_mutex_unlock(&mainwait
);
203 pthread_mutex_unlock(&glob_masks
.mutex
);
208 /* error will be logged by primary thread */
210 pthread_mutex_lock(&glob_masks
.mutex
);
211 target
[args
->id
].state
= INACTIVE
;
212 if (--glob_masks
.num_working
== 0)
213 pthread_mutex_unlock(&mainwait
);
214 pthread_mutex_unlock(&glob_masks
.mutex
);
224 /* only the parent gets to kill things */
226 if (getpid() != parent_pid
)
229 for (i
= 0; i
< num_targets
; i
++) {
230 if (target
[i
].state
== ACTIVE
) {
231 /* kill up target threads */
232 pthread_kill(target
[i
].pid
, SIGKILL
);
233 pthread_mutex_unlock(&targ
[i
].wait
);
241 pid_t pid
= getpid();
248 for (i
= 0; i
< num_targets
; i
++) {
249 if (target
[i
].pid
== pid
) {
250 if (target
[i
].state
== INACTIVE
) {
251 /* thread got an I/O error */
253 if (target
[i
].err_type
== 0) {
255 _("%s: write error on target %d \"%s\" at offset %lld\n"),
256 progname
, i
, target
[i
].name
,
260 _("%s: lseek64 error on target %d \"%s\" at offset %lld\n"),
261 progname
, i
, target
[i
].name
,
265 do_vfatal(target
[i
].error
,
266 _("Aborting target %d - reason"), i
);
270 _("Aborting XFS copy - no more targets.\n"));
275 signal(SIGCHLD
, handler
);
278 /* it just croaked it bigtime, log it */
281 _("%s: thread %d died unexpectedly, target \"%s\" incomplete\n"),
282 progname
, i
, target
[i
].name
);
283 do_warn(_("%s: offset was probably %lld\n"),
284 progname
, target
[i
].position
);
285 do_fatal(target
[i
].error
,
286 _("Aborting XFS copy - reason"));
292 /* unknown child -- something very wrong */
294 do_warn(_("%s: Unknown child died (should never happen!)\n"), progname
);
297 signal(SIGCHLD
, handler
);
304 _("Usage: %s [-bd] [-L logfile] source target [target ...]\n"),
310 init_bar(__uint64_t source_blocks
)
314 for (i
= 0; i
< 11; i
++)
315 barcount
[i
] = (source_blocks
/10)*i
;
319 bump_bar(int tenths
, __uint64_t numblocks
)
321 static char *bar
[11] = {
336 printf("%s", bar
[10]);
339 while (tenths
< 10 && numblocks
> barcount
[tenths
]) {
340 printf("%s", bar
[tenths
]);
348 static xfs_off_t source_position
= -1;
351 wbuf_init(wbuf
*buf
, int data_size
, int data_align
, int min_io_size
, int id
)
353 ASSERT(data_size
% BBSIZE
== 0);
354 while ((buf
->data
= memalign(data_align
, data_size
)) == NULL
) {
356 if (data_size
< min_io_size
)
359 ASSERT(min_io_size
% BBSIZE
== 0);
360 buf
->min_io_size
= min_io_size
;
361 buf
->size
= data_size
;
367 read_wbuf(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
374 newpos
= rounddown(buf
->position
, (xfs_off_t
) buf
->min_io_size
);
376 if (newpos
!= buf
->position
) {
377 diff
= buf
->position
- newpos
;
378 buf
->position
= newpos
;
383 if (source_position
!= buf
->position
) {
384 lres
= lseek64(fd
, buf
->position
, SEEK_SET
);
386 do_warn(_("%s: lseek64 failure at offset %lld\n"),
387 progname
, source_position
);
390 source_position
= buf
->position
;
393 ASSERT(source_position
% source_sectorsize
== 0);
395 /* round up length for direct I/O if necessary */
397 if (buf
->length
% buf
->min_io_size
!= 0)
398 buf
->length
= roundup(buf
->length
, buf
->min_io_size
);
400 if (buf
->length
> buf
->size
) {
401 do_warn(_("assert error: buf->length = %d, buf->size = %d\n"),
402 buf
->length
, buf
->size
);
407 if ((res
= read(fd
, buf
->data
, buf
->length
)) < 0) {
408 do_warn(_("%s: read failure at offset %lld\n"),
409 progname
, source_position
);
413 if (res
< buf
->length
&&
414 source_position
+ res
== mp
->m_sb
.sb_dblocks
* source_blocksize
)
417 ASSERT(res
== buf
->length
);
418 source_position
+= res
;
423 read_ag_header(int fd
, xfs_agnumber_t agno
, wbuf
*buf
, ag_header_t
*ag
,
424 xfs_mount_t
*mp
, int blocksize
, int sectorsize
)
431 /* initial settings */
434 off
= XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
);
435 buf
->position
= (xfs_off_t
) off
* (xfs_off_t
) BBSIZE
;
436 length
= buf
->length
= first_agbno
* blocksize
;
438 /* handle alignment stuff */
440 newpos
= rounddown(buf
->position
, (xfs_off_t
) buf
->min_io_size
);
441 if (newpos
!= buf
->position
) {
442 diff
= buf
->position
- newpos
;
443 buf
->position
= newpos
;
447 /* round up length for direct I/O if necessary */
449 if (buf
->length
% buf
->min_io_size
!= 0)
450 buf
->length
= roundup(buf
->length
, buf
->min_io_size
);
453 read_wbuf(fd
, buf
, mp
);
454 ASSERT(buf
->length
>= length
);
456 ag
->xfs_sb
= (xfs_dsb_t
*) (buf
->data
+ diff
);
457 ASSERT(be32_to_cpu(ag
->xfs_sb
->sb_magicnum
) == XFS_SB_MAGIC
);
458 ag
->xfs_agf
= (xfs_agf_t
*) (buf
->data
+ diff
+ sectorsize
);
459 ASSERT(be32_to_cpu(ag
->xfs_agf
->agf_magicnum
) == XFS_AGF_MAGIC
);
460 ag
->xfs_agi
= (xfs_agi_t
*) (buf
->data
+ diff
+ 2 * sectorsize
);
461 ASSERT(be32_to_cpu(ag
->xfs_agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
462 ag
->xfs_agfl
= (xfs_agfl_t
*) (buf
->data
+ diff
+ 3 * sectorsize
);
471 /* verify target threads */
472 for (i
= 0; i
< num_targets
; i
++)
473 if (target
[i
].state
!= INACTIVE
)
474 glob_masks
.num_working
++;
476 /* release target threads */
477 for (i
= 0; i
< num_targets
; i
++)
478 if (target
[i
].state
!= INACTIVE
)
479 pthread_mutex_unlock(&targ
[i
].wait
); /* wake up */
482 pthread_mutex_lock(&mainwait
);
488 main(int argc
, char **argv
)
493 xfs_off_t pos
, end_pos
;
495 int c
, first_residue
, tmp_residue
;
496 __uint64_t size
, sizeb
;
497 __uint64_t numblocks
= 0;
504 int source_is_file
= 0;
505 int buffered_output
= 0;
507 uint btree_levels
, current_level
;
513 xfs_agnumber_t num_ags
, agno
;
515 xfs_daddr_t begin
, next_begin
, ag_begin
, new_begin
, ag_end
;
516 xfs_alloc_block_t
*block
;
517 xfs_alloc_ptr_t
*ptr
;
518 xfs_alloc_rec_t
*rec_ptr
;
523 struct stat64 statbuf
;
525 progname
= basename(argv
[0]);
527 setlocale(LC_ALL
, "");
528 bindtextdomain(PACKAGE
, LOCALEDIR
);
531 while ((c
= getopt(argc
, argv
, "bdL:V")) != EOF
) {
540 logfile_name
= optarg
;
543 printf(_("%s version %s\n"), progname
, VERSION
);
550 if (argc
- optind
< 2)
554 logfd
= open(logfile_name
, O_CREAT
|O_WRONLY
|O_EXCL
, 0600);
556 logfile_name
= LOGFILE_NAME
;
557 logfd
= mkstemp(logfile_name
);
561 fprintf(stderr
, _("%s: couldn't open log file \"%s\"\n"),
562 progname
, logfile_name
);
563 perror(_("Aborting XFS copy - reason"));
567 if ((logerr
= fdopen(logfd
, "w")) == NULL
) {
568 fprintf(stderr
, _("%s: couldn't set up logfile stream\n"),
570 perror(_("Aborting XFS copy - reason"));
574 source_name
= argv
[optind
];
578 num_targets
= argc
- optind
;
579 if ((target
= malloc(sizeof(target_control
) * num_targets
)) == NULL
) {
580 do_log(_("Couldn't allocate target array\n"));
583 for (i
= 0; optind
< argc
; i
++, optind
++) {
584 target
[i
].name
= argv
[optind
];
586 target
[i
].position
= -1;
587 target
[i
].state
= INACTIVE
;
589 target
[i
].err_type
= 0;
592 parent_pid
= getpid();
594 if (atexit(killall
)) {
595 do_log(_("%s: couldn't register atexit function.\n"), progname
);
599 /* open up source -- is it a file? */
601 open_flags
= O_RDONLY
;
603 if ((source_fd
= open(source_name
, open_flags
)) < 0) {
604 do_log(_("%s: couldn't open source \"%s\"\n"),
605 progname
, source_name
);
609 if (fstat64(source_fd
, &statbuf
) < 0) {
610 do_log(_("%s: couldn't stat source \"%s\"\n"),
611 progname
, source_name
);
615 if (S_ISREG(statbuf
.st_mode
))
618 if (source_is_file
&& platform_test_xfs_fd(source_fd
)) {
619 if (fcntl(source_fd
, F_SETFL
, open_flags
| O_DIRECT
) < 0) {
620 do_log(_("%s: Cannot set direct I/O flag on \"%s\".\n"),
621 progname
, source_name
);
624 if (xfsctl(source_name
, source_fd
, XFS_IOC_DIOINFO
, &d
) < 0) {
625 do_log(_("%s: xfsctl on file \"%s\" failed.\n"),
626 progname
, source_name
);
630 wbuf_align
= d
.d_mem
;
631 wbuf_size
= MIN(d
.d_maxiosz
, 1 * 1024 * 1024);
632 wbuf_miniosize
= d
.d_miniosz
;
634 /* set arbitrary I/O params, miniosize at least 1 disk block */
636 wbuf_align
= getpagesize();
637 wbuf_size
= 1 * 1024 * 1024;
638 wbuf_miniosize
= -1; /* set after mounting source fs */
641 if (!source_is_file
) {
643 * check to make sure a filesystem isn't mounted
646 if (platform_check_ismounted(source_name
, NULL
, &statbuf
, 0)) {
648 _("%s: Warning -- a filesystem is mounted on the source device.\n"),
651 _("\t\tGenerated copies may be corrupt unless the source is\n"));
653 _("\t\tunmounted or mounted read-only. Copy proceeding...\n"));
657 /* prepare the libxfs_init structure */
659 memset(&xargs
, 0, sizeof(xargs
));
660 xargs
.isdirect
= LIBXFS_DIRECT
;
661 xargs
.isreadonly
= LIBXFS_ISREADONLY
;
663 if (source_is_file
) {
664 xargs
.dname
= source_name
;
667 xargs
.volname
= source_name
;
669 if (!libxfs_init(&xargs
)) {
670 do_log(_("%s: couldn't initialize XFS library\n"
671 "%s: Aborting.\n"), progname
, progname
);
675 /* prepare the mount structure */
677 sbp
= libxfs_readbuf(xargs
.ddev
, XFS_SB_DADDR
, 1, 0);
678 memset(&mbuf
, 0, sizeof(xfs_mount_t
));
680 libxfs_sb_from_disk(sb
, XFS_BUF_TO_SBP(sbp
));
682 mp
= libxfs_mount(&mbuf
, sb
, xargs
.ddev
, xargs
.logdev
, xargs
.rtdev
, 1);
684 do_log(_("%s: %s filesystem failed to initialize\n"
685 "%s: Aborting.\n"), progname
, source_name
, progname
);
687 } else if (mp
->m_sb
.sb_inprogress
) {
688 do_log(_("%s %s filesystem failed to initialize\n"
689 "%s: Aborting.\n"), progname
, source_name
, progname
);
691 } else if (mp
->m_sb
.sb_logstart
== 0) {
692 do_log(_("%s: %s has an external log.\n%s: Aborting.\n"),
693 progname
, source_name
, progname
);
695 } else if (mp
->m_sb
.sb_rextents
!= 0) {
696 do_log(_("%s: %s has a real-time section.\n"
697 "%s: Aborting.\n"), progname
, source_name
, progname
);
701 source_blocksize
= mp
->m_sb
.sb_blocksize
;
702 source_sectorsize
= mp
->m_sb
.sb_sectsize
;
704 if (wbuf_miniosize
== -1)
705 wbuf_miniosize
= source_sectorsize
;
707 ASSERT(source_blocksize
% source_sectorsize
== 0);
708 ASSERT(source_sectorsize
% BBSIZE
== 0);
710 if (source_blocksize
> source_sectorsize
) {
711 /* get number of leftover sectors in last block of ag header */
713 tmp_residue
= ((XFS_AGFL_DADDR(mp
) + 1) * source_sectorsize
)
715 first_residue
= (tmp_residue
== 0) ? 0 :
716 source_blocksize
- tmp_residue
;
717 ASSERT(first_residue
% source_sectorsize
== 0);
718 } else if (source_blocksize
== source_sectorsize
) {
721 do_log(_("Error: filesystem block size is smaller than the"
722 " disk sectorsize.\nAborting XFS copy now.\n"));
726 first_agbno
= (((XFS_AGFL_DADDR(mp
) + 1) * source_sectorsize
)
727 + first_residue
) / source_blocksize
;
728 ASSERT(first_agbno
!= 0);
729 ASSERT( ((((XFS_AGFL_DADDR(mp
) + 1) * source_sectorsize
)
730 + first_residue
) % source_blocksize
) == 0);
732 /* now open targets */
736 for (i
= 0; i
< num_targets
; i
++) {
737 int write_last_block
= 0;
739 if (stat64(target
[i
].name
, &statbuf
) < 0) {
740 /* ok, assume it's a file and create it */
742 do_out(_("Creating file %s\n"), target
[i
].name
);
744 open_flags
|= O_CREAT
;
745 if (!buffered_output
)
746 open_flags
|= O_DIRECT
;
747 write_last_block
= 1;
748 } else if (S_ISREG(statbuf
.st_mode
)) {
749 open_flags
|= O_TRUNC
;
750 if (!buffered_output
)
751 open_flags
|= O_DIRECT
;
752 write_last_block
= 1;
755 * check to make sure a filesystem isn't mounted
758 if (platform_check_ismounted(target
[i
].name
,
759 NULL
, &statbuf
, 0)) {
760 do_log(_("%s: a filesystem is mounted "
761 "on target device \"%s\".\n"
762 "%s cannot copy to mounted filesystems."
764 progname
, target
[i
].name
, progname
);
769 target
[i
].fd
= open(target
[i
].name
, open_flags
, 0644);
770 if (target
[i
].fd
< 0) {
771 do_log(_("%s: couldn't open target \"%s\"\n"),
772 progname
, target
[i
].name
);
776 if (write_last_block
) {
777 /* ensure regular files are correctly sized */
779 if (ftruncate64(target
[i
].fd
, mp
->m_sb
.sb_dblocks
*
781 do_log(_("%s: cannot grow data section.\n"),
785 if (platform_test_xfs_fd(target
[i
].fd
)) {
786 if (xfsctl(target
[i
].name
, target
[i
].fd
,
787 XFS_IOC_DIOINFO
, &d
) < 0) {
789 _("%s: xfsctl on \"%s\" failed.\n"),
790 progname
, target
[i
].name
);
793 wbuf_align
= MAX(wbuf_align
, d
.d_mem
);
794 wbuf_size
= MIN(d
.d_maxiosz
, wbuf_size
);
795 wbuf_miniosize
= MAX(d
.d_miniosz
,
800 char *lb
[XFS_MAX_SECTORSIZE
] = { NULL
};
803 /* ensure device files are sufficiently large */
805 off
= mp
->m_sb
.sb_dblocks
* source_blocksize
;
807 if (pwrite64(target
[i
].fd
, lb
, sizeof(lb
), off
) < 0) {
808 do_log(_("%s: failed to write last block\n"),
810 do_log(_("\tIs target \"%s\" too small?\n"),
817 /* initialize locks and bufs */
819 if (pthread_mutex_init(&glob_masks
.mutex
, NULL
) != 0) {
820 do_log(_("Couldn't initialize global thread mask\n"));
823 glob_masks
.num_working
= 0;
825 if (wbuf_init(&w_buf
, wbuf_size
, wbuf_align
,
826 wbuf_miniosize
, 0) == NULL
) {
827 do_log(_("Error initializing wbuf 0\n"));
831 wblocks
= wbuf_size
/ BBSIZE
;
833 if (wbuf_init(&btree_buf
, MAX(source_blocksize
, wbuf_miniosize
),
834 wbuf_align
, wbuf_miniosize
, 1) == NULL
) {
835 do_log(_("Error initializing btree buf 1\n"));
839 if (pthread_mutex_init(&mainwait
,NULL
) != 0) {
840 do_log(_("Error creating first semaphore.\n"));
844 /* need to start out blocking */
845 pthread_mutex_lock(&mainwait
);
847 /* set up sigchild signal handler */
849 signal(SIGCHLD
, handler
);
854 if ((targ
= malloc(num_targets
* sizeof(thread_args
))) == NULL
) {
855 do_log(_("Couldn't malloc space for thread args\n"));
860 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++, tcarg
++) {
862 platform_uuid_generate(&tcarg
->uuid
);
864 platform_uuid_copy(&tcarg
->uuid
, &mp
->m_sb
.sb_uuid
);
866 if (pthread_mutex_init(&tcarg
->wait
, NULL
) != 0) {
867 do_log(_("Error creating thread mutex %d\n"), i
);
871 /* need to start out blocking */
872 pthread_mutex_lock(&tcarg
->wait
);
875 for (i
= 0, tcarg
= targ
; i
< num_targets
; i
++, tcarg
++) {
877 tcarg
->fd
= target
[i
].fd
;
879 target
[i
].state
= ACTIVE
;
882 if (pthread_create(&target
[i
].pid
, NULL
,
883 begin_reader
, (void *)tcarg
)) {
884 do_log(_("Error creating thread for target %d\n"), i
);
889 ASSERT(num_targets
== num_threads
);
891 /* set up statistics */
893 num_ags
= mp
->m_sb
.sb_agcount
;
895 init_bar(mp
->m_sb
.sb_blocksize
/ BBSIZE
896 * ((__uint64_t
)mp
->m_sb
.sb_dblocks
897 - (__uint64_t
)mp
->m_sb
.sb_fdblocks
+ 10 * num_ags
));
900 block
= (xfs_alloc_block_t
*) btree_buf
.data
;
902 for (agno
= 0; agno
< num_ags
&& kids
> 0; agno
++) {
903 /* read in first blocks of the ag */
905 read_ag_header(source_fd
, agno
, &w_buf
, &ag_hdr
, mp
,
906 source_blocksize
, source_sectorsize
);
908 /* set the in_progress bit for the first AG */
911 ag_hdr
.xfs_sb
->sb_inprogress
= 1;
913 /* save what we need (agf) in the btree buffer */
915 memmove(btree_buf
.data
, ag_hdr
.xfs_agf
, source_sectorsize
);
916 ag_hdr
.xfs_agf
= (xfs_agf_t
*) btree_buf
.data
;
917 btree_buf
.length
= source_blocksize
;
919 /* write the ag header out */
923 /* traverse btree until we get to the leftmost leaf node */
925 bno
= be32_to_cpu(ag_hdr
.xfs_agf
->agf_roots
[XFS_BTNUM_BNOi
]);
927 btree_levels
= be32_to_cpu(ag_hdr
.xfs_agf
->
928 agf_levels
[XFS_BTNUM_BNOi
]);
930 ag_end
= XFS_AGB_TO_DADDR(mp
, agno
,
931 be32_to_cpu(ag_hdr
.xfs_agf
->agf_length
) - 1)
932 + source_blocksize
/ BBSIZE
;
935 /* none of this touches the w_buf buffer */
937 ASSERT(current_level
< btree_levels
);
941 btree_buf
.position
= pos
= (xfs_off_t
)
942 XFS_AGB_TO_DADDR(mp
,agno
,bno
) << BBSHIFT
;
943 btree_buf
.length
= source_blocksize
;
945 read_wbuf(source_fd
, &btree_buf
, mp
);
946 block
= (xfs_alloc_block_t
*) ((char *) btree_buf
.data
947 + pos
- btree_buf
.position
);
949 ASSERT(be32_to_cpu(block
->bb_magic
) == XFS_ABTB_MAGIC
);
951 if (be16_to_cpu(block
->bb_level
) == 0)
954 ptr
= XFS_BTREE_PTR_ADDR(xfs_alloc
, block
, 1,
956 bno
= be32_to_cpu(ptr
[0]);
959 /* align first data copy but don't overwrite ag header */
961 pos
= w_buf
.position
>> BBSHIFT
;
962 length
= w_buf
.length
>> BBSHIFT
;
963 next_begin
= pos
+ length
;
964 ag_begin
= next_begin
;
966 ASSERT(w_buf
.position
% source_sectorsize
== 0);
968 /* handle the rest of the ag */
971 if (be16_to_cpu(block
->bb_level
) != 0) {
973 _("WARNING: source filesystem inconsistent.\n"));
975 _(" A leaf btree rec isn't a leaf. Aborting now.\n"));
979 rec_ptr
= XFS_BTREE_REC_ADDR(xfs_alloc
, block
, 1);
980 for (i
= 0; i
< be16_to_cpu(block
->bb_numrecs
);
982 /* calculate in daddr's */
987 * protect against pathological case of a
988 * hole right after the ag header in a
992 if (begin
< ag_begin
)
996 * round size up to ensure we copy a
997 * range bigger than required
1000 sizeb
= XFS_AGB_TO_DADDR(mp
, agno
,
1001 be32_to_cpu(rec_ptr
->ar_startblock
)) -
1003 size
= roundup(sizeb
<<BBSHIFT
, wbuf_miniosize
);
1007 w_buf
.position
= (xfs_off_t
)
1012 * let lower layer do alignment
1014 if (size
> w_buf
.size
) {
1015 w_buf
.length
= w_buf
.size
;
1018 numblocks
+= wblocks
;
1020 w_buf
.length
= size
;
1025 read_wbuf(source_fd
, &w_buf
, mp
);
1028 w_buf
.position
+= w_buf
.length
;
1035 /* round next starting point down */
1037 new_begin
= XFS_AGB_TO_DADDR(mp
, agno
,
1038 be32_to_cpu(rec_ptr
->ar_startblock
) +
1039 be32_to_cpu(rec_ptr
->ar_blockcount
));
1040 next_begin
= rounddown(new_begin
,
1041 w_buf
.min_io_size
>> BBSHIFT
);
1044 if (be32_to_cpu(block
->bb_rightsib
) == NULLAGBLOCK
)
1047 /* read in next btree record block */
1049 btree_buf
.position
= pos
= (xfs_off_t
)
1050 XFS_AGB_TO_DADDR(mp
, agno
, be32_to_cpu(
1051 block
->bb_rightsib
)) << BBSHIFT
;
1052 btree_buf
.length
= source_blocksize
;
1054 /* let read_wbuf handle alignment */
1056 read_wbuf(source_fd
, &btree_buf
, mp
);
1058 block
= (xfs_alloc_block_t
*) ((char *) btree_buf
.data
1059 + pos
- btree_buf
.position
);
1061 ASSERT(be32_to_cpu(block
->bb_magic
) == XFS_ABTB_MAGIC
);
1065 * write out range of used blocks after last range
1066 * of free blocks in AG
1068 if (next_begin
< ag_end
) {
1071 sizeb
= ag_end
- begin
;
1072 size
= roundup(sizeb
<< BBSHIFT
, wbuf_miniosize
);
1077 w_buf
.position
= (xfs_off_t
) begin
<< BBSHIFT
;
1081 * let lower layer do alignment
1083 if (size
> w_buf
.size
) {
1084 w_buf
.length
= w_buf
.size
;
1087 numblocks
+= wblocks
;
1089 w_buf
.length
= size
;
1094 read_wbuf(source_fd
, &w_buf
, mp
);
1097 w_buf
.position
+= w_buf
.length
;
1099 howfar
= bump_bar(howfar
, numblocks
);
1108 /* write a clean log using the specified UUID */
1109 for (j
= 0, tcarg
= targ
; j
< num_targets
; j
++) {
1110 w_buf
.owner
= tcarg
;
1111 w_buf
.length
= rounddown(w_buf
.size
,
1113 pos
= write_log_header(
1114 source_fd
, &w_buf
, mp
);
1115 end_pos
= write_log_trailer(
1116 source_fd
, &w_buf
, mp
);
1117 w_buf
.position
= pos
;
1118 memset(w_buf
.data
, 0, w_buf
.length
);
1120 while (w_buf
.position
< end_pos
) {
1122 w_buf
.position
+= w_buf
.length
;
1130 /* reread and rewrite superblocks (UUID and in-progress) */
1131 /* [backwards, so inprogress bit only updated when done] */
1133 for (i
= num_ags
- 1; i
>= 0; i
--) {
1134 read_ag_header(source_fd
, i
, &w_buf
, &ag_hdr
, mp
,
1135 source_blocksize
, source_sectorsize
);
1137 ag_hdr
.xfs_sb
->sb_inprogress
= 0;
1139 /* do each thread in turn, each has its own UUID */
1141 for (j
= 0, tcarg
= targ
; j
< num_targets
; j
++) {
1142 platform_uuid_copy(&ag_hdr
.xfs_sb
->sb_uuid
,
1160 next_log_chunk(xfs_caddr_t p
, int offset
, void *private)
1162 wbuf
*buf
= (wbuf
*)private;
1164 if (buf
->length
< (int)(p
- buf
->data
) + offset
) {
1165 /* need to flush this one, then start afresh */
1167 do_write(buf
->owner
);
1168 memset(buf
->data
, 0, buf
->length
);
1175 * Writes a log header at the start of the log (with the real
1176 * filesystem UUID embedded into it), and writes to all targets.
1178 * Returns the next buffer-length-aligned disk address.
1181 write_log_header(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
1183 xfs_caddr_t p
= buf
->data
;
1187 logstart
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1188 buf
->position
= rounddown(logstart
, (xfs_off_t
)buf
->length
);
1190 memset(p
, 0, buf
->size
);
1191 if (logstart
% buf
->length
) { /* unaligned */
1192 read_wbuf(fd
, buf
, mp
);
1193 offset
= logstart
- buf
->position
;
1195 memset(p
, 0, buf
->length
- offset
);
1198 offset
= libxfs_log_header(p
, &buf
->owner
->uuid
,
1199 xfs_sb_version_haslogv2(&mp
->m_sb
) ? 2 : 1,
1200 mp
->m_sb
.sb_logsunit
, XLOG_FMT
,
1201 next_log_chunk
, buf
);
1202 do_write(buf
->owner
);
1204 return roundup(logstart
+ offset
, buf
->length
);
1208 * May do an aligned read of the last buffer in the log (& zero
1209 * the start of that buffer). Returns the disk address at the
1210 * end of last aligned buffer in the log.
1213 write_log_trailer(int fd
, wbuf
*buf
, xfs_mount_t
*mp
)
1218 logend
= XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
) << BBSHIFT
;
1219 logend
+= XFS_FSB_TO_B(mp
, mp
->m_sb
.sb_logblocks
);
1221 buf
->position
= rounddown(logend
, (xfs_off_t
)buf
->length
);
1223 if (logend
% buf
->length
) { /* unaligned */
1224 read_wbuf(fd
, buf
, mp
);
1225 offset
= (int)(logend
- buf
->position
);
1226 memset(buf
->data
, 0, offset
);
1227 do_write(buf
->owner
);
1230 return buf
->position
;