4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015, 2018 by Delphix. All rights reserved.
26 * Copyright 2016 Joyent, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
37 #include <sys/types.h>
46 #include <sys/zfs_ioctl.h>
48 #include "libzfs_impl.h"
50 #define ZDIFF_SNAPDIR "/.zfs/snapshot/"
51 #define ZDIFF_PREFIX "zfs-diff-%d"
53 #define ZDIFF_ADDED '+'
54 #define ZDIFF_MODIFIED "M"
55 #define ZDIFF_REMOVED '-'
56 #define ZDIFF_RENAMED "R"
60 * Given a {dsname, object id}, get the object path
63 get_stats_for_obj(differ_info_t
*di
, const char *dsname
, uint64_t obj
,
64 char *pn
, int maxlen
, zfs_stat_t
*sb
)
66 zfs_cmd_t zc
= {"\0"};
69 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
73 error
= zfs_ioctl(di
->zhp
->zfs_hdl
, ZFS_IOC_OBJ_TO_STATS
, &zc
);
76 /* we can get stats even if we failed to get a path */
77 (void) memcpy(sb
, &zc
.zc_stat
, sizeof (zfs_stat_t
));
79 ASSERT(di
->zerr
== 0);
80 (void) strlcpy(pn
, zc
.zc_value
, maxlen
);
84 if (di
->zerr
== ESTALE
) {
85 (void) snprintf(pn
, maxlen
, "(on_delete_queue)");
87 } else if (di
->zerr
== EPERM
) {
88 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
90 "The sys_config privilege or diff delegated permission "
91 "is needed\nto discover path names"));
93 } else if (di
->zerr
== EACCES
) {
94 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
96 "Key must be loaded to discover path names"));
99 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
100 dgettext(TEXT_DOMAIN
,
101 "Unable to determine path or stats for "
102 "object %lld in %s"), (longlong_t
)obj
, dsname
);
110 * Prints a file name out a character at a time. If the character is
111 * not in the range of what we consider "printable" ASCII, display it
112 * as an escaped 4-digit octal value. ASCII values less than a space
113 * are all control characters and we declare the upper end as the
114 * DELete character. This also is the last 7-bit ASCII character.
115 * We choose to treat all 8-bit ASCII as not printable for this
119 stream_bytes(FILE *fp
, const char *string
)
123 while ((c
= *string
++) != '\0') {
124 if (c
> ' ' && c
!= '\\' && c
< '\177') {
127 (void) fprintf(fp
, "\\%04hho", (uint8_t)c
);
133 get_what(mode_t what
)
135 switch (what
& S_IFMT
) {
164 print_cmn(FILE *fp
, differ_info_t
*di
, const char *file
)
166 if (!di
->no_mangle
) {
167 stream_bytes(fp
, di
->dsmnt
);
168 stream_bytes(fp
, file
);
170 (void) fputs(di
->dsmnt
, fp
);
171 (void) fputs(file
, fp
);
176 print_rename(FILE *fp
, differ_info_t
*di
, const char *old
, const char *new,
180 (void) fprintf(fp
, "%10lld.%09lld\t",
181 (longlong_t
)isb
->zs_ctime
[0],
182 (longlong_t
)isb
->zs_ctime
[1]);
183 (void) fputs(ZDIFF_RENAMED
"\t", fp
);
185 (void) fprintf(fp
, "%c\t", get_what(isb
->zs_mode
));
186 print_cmn(fp
, di
, old
);
187 (void) fputs(di
->scripted
? "\t" : " -> ", fp
);
188 print_cmn(fp
, di
, new);
189 (void) fputc('\n', fp
);
193 print_link_change(FILE *fp
, differ_info_t
*di
, int delta
, const char *file
,
197 (void) fprintf(fp
, "%10lld.%09lld\t",
198 (longlong_t
)isb
->zs_ctime
[0],
199 (longlong_t
)isb
->zs_ctime
[1]);
200 (void) fputs(ZDIFF_MODIFIED
"\t", fp
);
202 (void) fprintf(fp
, "%c\t", get_what(isb
->zs_mode
));
203 print_cmn(fp
, di
, file
);
204 (void) fprintf(fp
, "\t(%+d)\n", delta
);
208 print_file(FILE *fp
, differ_info_t
*di
, char type
, const char *file
,
212 (void) fprintf(fp
, "%10lld.%09lld\t",
213 (longlong_t
)isb
->zs_ctime
[0],
214 (longlong_t
)isb
->zs_ctime
[1]);
215 (void) fprintf(fp
, "%c\t", type
);
217 (void) fprintf(fp
, "%c\t", get_what(isb
->zs_mode
));
218 print_cmn(fp
, di
, file
);
219 (void) fputc('\n', fp
);
223 write_inuse_diffs_one(FILE *fp
, differ_info_t
*di
, uint64_t dobj
)
225 struct zfs_stat fsb
, tsb
;
227 char fobjname
[MAXPATHLEN
], tobjname
[MAXPATHLEN
];
228 boolean_t already_logged
= B_FALSE
;
229 int fobjerr
, tobjerr
;
232 if (dobj
== di
->shares
)
236 * Check the from and to snapshots for info on the object. If
237 * we get ENOENT, then the object just didn't exist in that
238 * snapshot. If we get ENOTSUP, then we tried to get
239 * info on a non-ZPL object, which we don't care about anyway.
240 * For any other error we print a warning which includes the
241 * errno and continue.
244 fobjerr
= get_stats_for_obj(di
, di
->fromsnap
, dobj
, fobjname
,
246 if (fobjerr
&& di
->zerr
!= ENOTSUP
&& di
->zerr
!= ENOENT
) {
247 zfs_error_aux(di
->zhp
->zfs_hdl
, "%s", strerror(di
->zerr
));
248 zfs_error(di
->zhp
->zfs_hdl
, di
->zerr
, di
->errbuf
);
250 * Let's not print an error for the same object more than
251 * once if it happens in both snapshots
253 already_logged
= B_TRUE
;
256 tobjerr
= get_stats_for_obj(di
, di
->tosnap
, dobj
, tobjname
,
259 if (tobjerr
&& di
->zerr
!= ENOTSUP
&& di
->zerr
!= ENOENT
) {
260 if (!already_logged
) {
261 zfs_error_aux(di
->zhp
->zfs_hdl
,
262 "%s", strerror(di
->zerr
));
263 zfs_error(di
->zhp
->zfs_hdl
, di
->zerr
, di
->errbuf
);
267 * Unallocated object sharing the same meta dnode block
269 if (fobjerr
&& tobjerr
) {
274 di
->zerr
= 0; /* negate get_stats_for_obj() from side that failed */
275 fmode
= fsb
.zs_mode
& S_IFMT
;
276 tmode
= tsb
.zs_mode
& S_IFMT
;
277 if (fmode
== S_IFDIR
|| tmode
== S_IFDIR
|| fsb
.zs_links
== 0 ||
281 change
= tsb
.zs_links
- fsb
.zs_links
;
285 print_link_change(fp
, di
, change
, tobjname
, &tsb
);
288 print_file(fp
, di
, ZDIFF_ADDED
, tobjname
, &tsb
);
290 } else if (tobjerr
) {
292 print_link_change(fp
, di
, change
, fobjname
, &fsb
);
295 print_file(fp
, di
, ZDIFF_REMOVED
, fobjname
, &fsb
);
299 if (fmode
!= tmode
&& fsb
.zs_gen
== tsb
.zs_gen
)
300 tsb
.zs_gen
++; /* Force a generational difference */
302 /* Simple modification or no change */
303 if (fsb
.zs_gen
== tsb
.zs_gen
) {
304 /* No apparent changes. Could we assert !this? */
305 if (fsb
.zs_ctime
[0] == tsb
.zs_ctime
[0] &&
306 fsb
.zs_ctime
[1] == tsb
.zs_ctime
[1])
309 print_link_change(fp
, di
, change
,
310 change
> 0 ? fobjname
: tobjname
, &tsb
);
311 } else if (strcmp(fobjname
, tobjname
) == 0) {
312 print_file(fp
, di
, *ZDIFF_MODIFIED
, fobjname
, &tsb
);
314 print_rename(fp
, di
, fobjname
, tobjname
, &tsb
);
318 /* file re-created or object re-used */
319 print_file(fp
, di
, ZDIFF_REMOVED
, fobjname
, &fsb
);
320 print_file(fp
, di
, ZDIFF_ADDED
, tobjname
, &tsb
);
326 write_inuse_diffs(FILE *fp
, differ_info_t
*di
, dmu_diff_record_t
*dr
)
331 for (o
= dr
->ddr_first
; o
<= dr
->ddr_last
; o
++) {
332 if ((err
= write_inuse_diffs_one(fp
, di
, o
)) != 0)
339 describe_free(FILE *fp
, differ_info_t
*di
, uint64_t object
, char *namebuf
,
344 (void) get_stats_for_obj(di
, di
->fromsnap
, object
, namebuf
,
347 /* Don't print if in the delete queue on from side */
348 if (di
->zerr
== ESTALE
|| di
->zerr
== ENOENT
) {
353 print_file(fp
, di
, ZDIFF_REMOVED
, namebuf
, &sb
);
358 write_free_diffs(FILE *fp
, differ_info_t
*di
, dmu_diff_record_t
*dr
)
360 zfs_cmd_t zc
= {"\0"};
361 libzfs_handle_t
*lhdl
= di
->zhp
->zfs_hdl
;
362 char fobjname
[MAXPATHLEN
];
364 (void) strlcpy(zc
.zc_name
, di
->fromsnap
, sizeof (zc
.zc_name
));
365 zc
.zc_obj
= dr
->ddr_first
- 1;
367 ASSERT(di
->zerr
== 0);
369 while (zc
.zc_obj
< dr
->ddr_last
) {
372 err
= zfs_ioctl(lhdl
, ZFS_IOC_NEXT_OBJ
, &zc
);
374 if (zc
.zc_obj
== di
->shares
) {
378 if (zc
.zc_obj
> dr
->ddr_last
) {
381 err
= describe_free(fp
, di
, zc
.zc_obj
, fobjname
,
383 } else if (errno
== ESRCH
) {
386 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
387 dgettext(TEXT_DOMAIN
,
388 "next allocated object (> %lld) find failure"),
389 (longlong_t
)zc
.zc_obj
);
402 differ_info_t
*di
= arg
;
403 dmu_diff_record_t dr
;
407 if ((ofp
= fdopen(di
->outputfd
, "w")) == NULL
) {
409 strlcpy(di
->errbuf
, strerror(errno
), sizeof (di
->errbuf
));
410 (void) close(di
->datafd
);
415 char *cp
= (char *)&dr
;
416 int len
= sizeof (dr
);
420 rv
= read(di
->datafd
, cp
, len
);
423 } while (len
> 0 && rv
> 0);
425 if (rv
< 0 || (rv
== 0 && len
!= sizeof (dr
))) {
428 } else if (rv
== 0) {
429 /* end of file at a natural breaking point */
433 switch (dr
.ddr_type
) {
435 err
= write_free_diffs(ofp
, di
, &dr
);
438 err
= write_inuse_diffs(ofp
, di
, &dr
);
450 (void) close(di
->datafd
);
454 ASSERT(di
->zerr
== EPIPE
);
455 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
456 dgettext(TEXT_DOMAIN
,
457 "Internal error: bad data from diff IOCTL"));
464 make_temp_snapshot(differ_info_t
*di
)
466 libzfs_handle_t
*hdl
= di
->zhp
->zfs_hdl
;
467 zfs_cmd_t zc
= {"\0"};
469 (void) snprintf(zc
.zc_value
, sizeof (zc
.zc_value
),
470 ZDIFF_PREFIX
, getpid());
471 (void) strlcpy(zc
.zc_name
, di
->ds
, sizeof (zc
.zc_name
));
472 zc
.zc_cleanup_fd
= di
->cleanupfd
;
474 if (zfs_ioctl(hdl
, ZFS_IOC_TMP_SNAPSHOT
, &zc
) != 0) {
477 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
478 dgettext(TEXT_DOMAIN
, "The diff delegated "
479 "permission is needed in order\nto create a "
480 "just-in-time snapshot for diffing\n"));
481 return (zfs_error(hdl
, EZFS_DIFF
, di
->errbuf
));
483 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
484 dgettext(TEXT_DOMAIN
, "Cannot create just-in-time "
485 "snapshot of '%s'"), zc
.zc_name
);
486 return (zfs_standard_error(hdl
, err
, di
->errbuf
));
490 di
->tmpsnap
= zfs_strdup(hdl
, zc
.zc_value
);
491 di
->tosnap
= zfs_asprintf(hdl
, "%s@%s", di
->ds
, di
->tmpsnap
);
496 teardown_differ_info(differ_info_t
*di
)
505 (void) close(di
->cleanupfd
);
509 get_snapshot_names(differ_info_t
*di
, const char *fromsnap
,
512 libzfs_handle_t
*hdl
= di
->zhp
->zfs_hdl
;
520 * fdslen fsnlen tdslen tsnlen
522 * 0. dataset@snap1 dataset@snap2 >0 >1 >0 >1
523 * 1. dataset@snap1 @snap2 >0 >1 ==0 >1
524 * 2. dataset@snap1 dataset >0 >1 >0 ==0
525 * 3. @snap1 dataset@snap2 ==0 >1 >0 >1
526 * 4. @snap1 dataset ==0 >1 >0 ==0
528 if (tosnap
== NULL
) {
529 /* only a from snapshot given, must be valid */
530 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
531 dgettext(TEXT_DOMAIN
,
532 "Badly formed snapshot name %s"), fromsnap
);
534 if (!zfs_validate_name(hdl
, fromsnap
, ZFS_TYPE_SNAPSHOT
,
536 return (zfs_error(hdl
, EZFS_INVALIDNAME
,
540 atptrf
= strchr(fromsnap
, '@');
541 ASSERT(atptrf
!= NULL
);
542 fdslen
= atptrf
- fromsnap
;
544 di
->fromsnap
= zfs_strdup(hdl
, fromsnap
);
545 di
->ds
= zfs_strdup(hdl
, fromsnap
);
546 di
->ds
[fdslen
] = '\0';
548 /* the to snap will be a just-in-time snap of the head */
549 return (make_temp_snapshot(di
));
552 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
553 dgettext(TEXT_DOMAIN
,
554 "Unable to determine which snapshots to compare"));
556 atptrf
= strchr(fromsnap
, '@');
557 atptrt
= strchr(tosnap
, '@');
558 fdslen
= atptrf
? atptrf
- fromsnap
: strlen(fromsnap
);
559 tdslen
= atptrt
? atptrt
- tosnap
: strlen(tosnap
);
560 fsnlen
= strlen(fromsnap
) - fdslen
; /* includes @ sign */
561 tsnlen
= strlen(tosnap
) - tdslen
; /* includes @ sign */
563 if (fsnlen
<= 1 || tsnlen
== 1 || (fdslen
== 0 && tdslen
== 0)) {
564 return (zfs_error(hdl
, EZFS_INVALIDNAME
, di
->errbuf
));
565 } else if ((fdslen
> 0 && tdslen
> 0) &&
566 ((tdslen
!= fdslen
|| strncmp(fromsnap
, tosnap
, fdslen
) != 0))) {
568 * not the same dataset name, might be okay if
569 * tosnap is a clone of a fromsnap descendant.
571 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
575 di
->ds
= zfs_alloc(di
->zhp
->zfs_hdl
, tdslen
+ 1);
576 (void) strncpy(di
->ds
, tosnap
, tdslen
);
577 di
->ds
[tdslen
] = '\0';
579 zhp
= zfs_open(hdl
, di
->ds
, ZFS_TYPE_FILESYSTEM
);
580 while (zhp
!= NULL
) {
581 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, origin
,
582 sizeof (origin
), &src
, NULL
, 0, B_FALSE
) != 0) {
583 (void) zfs_close(zhp
);
587 if (strncmp(origin
, fromsnap
, fsnlen
) == 0)
590 (void) zfs_close(zhp
);
591 zhp
= zfs_open(hdl
, origin
, ZFS_TYPE_FILESYSTEM
);
595 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
596 dgettext(TEXT_DOMAIN
,
597 "Not an earlier snapshot from the same fs"));
598 return (zfs_error(hdl
, EZFS_INVALIDNAME
, di
->errbuf
));
600 (void) zfs_close(zhp
);
603 di
->isclone
= B_TRUE
;
604 di
->fromsnap
= zfs_strdup(hdl
, fromsnap
);
606 di
->tosnap
= zfs_strdup(hdl
, tosnap
);
608 return (make_temp_snapshot(di
));
610 int dslen
= fdslen
? fdslen
: tdslen
;
612 di
->ds
= zfs_alloc(hdl
, dslen
+ 1);
613 (void) strncpy(di
->ds
, fdslen
? fromsnap
: tosnap
, dslen
);
614 di
->ds
[dslen
] = '\0';
616 di
->fromsnap
= zfs_asprintf(hdl
, "%s%s", di
->ds
, atptrf
);
618 di
->tosnap
= zfs_asprintf(hdl
, "%s%s", di
->ds
, atptrt
);
620 return (make_temp_snapshot(di
));
627 get_mountpoint(differ_info_t
*di
, char *dsnm
, char **mntpt
)
631 mounted
= is_mounted(di
->zhp
->zfs_hdl
, dsnm
, mntpt
);
632 if (mounted
== B_FALSE
) {
633 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
634 dgettext(TEXT_DOMAIN
,
635 "Cannot diff an unmounted snapshot"));
636 return (zfs_error(di
->zhp
->zfs_hdl
, EZFS_BADTYPE
, di
->errbuf
));
639 /* Avoid a double slash at the beginning of root-mounted datasets */
640 if (**mntpt
== '/' && *(*mntpt
+ 1) == '\0')
646 get_mountpoints(differ_info_t
*di
)
652 * first get the mountpoint for the parent dataset
654 if (get_mountpoint(di
, di
->ds
, &di
->dsmnt
) != 0)
657 strptr
= strchr(di
->tosnap
, '@');
658 ASSERT3P(strptr
, !=, NULL
);
659 di
->tomnt
= zfs_asprintf(di
->zhp
->zfs_hdl
, "%s%s%s", di
->dsmnt
,
660 ZDIFF_SNAPDIR
, ++strptr
);
662 strptr
= strchr(di
->fromsnap
, '@');
663 ASSERT3P(strptr
, !=, NULL
);
665 frommntpt
= di
->dsmnt
;
671 err
= get_mountpoint(di
, di
->fromsnap
, &mntpt
);
678 di
->frommnt
= zfs_asprintf(di
->zhp
->zfs_hdl
, "%s%s%s", frommntpt
,
679 ZDIFF_SNAPDIR
, ++strptr
);
688 setup_differ_info(zfs_handle_t
*zhp
, const char *fromsnap
,
689 const char *tosnap
, differ_info_t
*di
)
693 di
->cleanupfd
= open(ZFS_DEV
, O_RDWR
| O_CLOEXEC
);
694 VERIFY(di
->cleanupfd
>= 0);
696 if (get_snapshot_names(di
, fromsnap
, tosnap
) != 0)
699 if (get_mountpoints(di
) != 0)
702 if (find_shares_object(di
) != 0)
709 zfs_show_diffs(zfs_handle_t
*zhp
, int outfd
, const char *fromsnap
,
710 const char *tosnap
, int flags
)
712 zfs_cmd_t zc
= {"\0"};
714 differ_info_t di
= { 0 };
719 (void) snprintf(errbuf
, sizeof (errbuf
),
720 dgettext(TEXT_DOMAIN
, "zfs diff failed"));
722 if (setup_differ_info(zhp
, fromsnap
, tosnap
, &di
)) {
723 teardown_differ_info(&di
);
727 if (pipe2(pipefd
, O_CLOEXEC
)) {
728 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(errno
));
729 teardown_differ_info(&di
);
730 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
, errbuf
));
733 di
.scripted
= (flags
& ZFS_DIFF_PARSEABLE
);
734 di
.classify
= (flags
& ZFS_DIFF_CLASSIFY
);
735 di
.timestamped
= (flags
& ZFS_DIFF_TIMESTAMP
);
736 di
.no_mangle
= (flags
& ZFS_DIFF_NO_MANGLE
);
739 di
.datafd
= pipefd
[0];
741 if (pthread_create(&tid
, NULL
, differ
, &di
)) {
742 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(errno
));
743 (void) close(pipefd
[0]);
744 (void) close(pipefd
[1]);
745 teardown_differ_info(&di
);
746 return (zfs_error(zhp
->zfs_hdl
,
747 EZFS_THREADCREATEFAILED
, errbuf
));
751 (void) strlcpy(zc
.zc_value
, di
.fromsnap
, strlen(di
.fromsnap
) + 1);
752 (void) strlcpy(zc
.zc_name
, di
.tosnap
, strlen(di
.tosnap
) + 1);
753 zc
.zc_cookie
= pipefd
[1];
755 iocerr
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DIFF
, &zc
);
757 (void) snprintf(errbuf
, sizeof (errbuf
),
758 dgettext(TEXT_DOMAIN
, "Unable to obtain diffs"));
759 if (errno
== EPERM
) {
760 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
761 "\n The sys_mount privilege or diff delegated "
762 "permission is needed\n to execute the "
764 } else if (errno
== EXDEV
) {
765 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
766 "\n Not an earlier snapshot from the same fs"));
767 } else if (errno
!= EPIPE
|| di
.zerr
== 0) {
768 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(errno
));
770 (void) close(pipefd
[1]);
771 (void) pthread_cancel(tid
);
772 (void) pthread_join(tid
, NULL
);
773 teardown_differ_info(&di
);
774 if (di
.zerr
!= 0 && di
.zerr
!= EPIPE
) {
775 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(di
.zerr
));
776 return (zfs_error(zhp
->zfs_hdl
, EZFS_DIFF
, di
.errbuf
));
778 return (zfs_error(zhp
->zfs_hdl
, EZFS_DIFFDATA
, errbuf
));
782 (void) close(pipefd
[1]);
783 (void) pthread_join(tid
, NULL
);
786 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(di
.zerr
));
787 return (zfs_error(zhp
->zfs_hdl
, EZFS_DIFF
, di
.errbuf
));
789 teardown_differ_info(&di
);