]> git.proxmox.com Git - mirror_zfs-debian.git/blame - lib/libzfs/libzfs_diff.c
New upstream version 0.7.2
[mirror_zfs-debian.git] / lib / libzfs / libzfs_diff.c
CommitLineData
572e2857
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
e10b0808 24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
cae5b340
AX
25 * Copyright (c) 2015 by Delphix. All rights reserved.
26 * Copyright 2016 Joyent, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
572e2857
BB
28 */
29
30/*
31 * zfs diff support
32 */
33#include <ctype.h>
34#include <errno.h>
35#include <libintl.h>
36#include <string.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <fcntl.h>
40#include <attr.h>
41#include <stddef.h>
42#include <unistd.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <stropts.h>
46#include <pthread.h>
47#include <sys/zfs_ioctl.h>
48#include <libzfs.h>
49#include "libzfs_impl.h"
50
51#define ZDIFF_SNAPDIR "/.zfs/snapshot/"
52#define ZDIFF_SHARESDIR "/.zfs/shares/"
53#define ZDIFF_PREFIX "zfs-diff-%d"
54
55#define ZDIFF_ADDED '+'
56#define ZDIFF_MODIFIED 'M'
57#define ZDIFF_REMOVED '-'
58#define ZDIFF_RENAMED 'R'
59
572e2857
BB
60typedef struct differ_info {
61 zfs_handle_t *zhp;
62 char *fromsnap;
63 char *frommnt;
64 char *tosnap;
65 char *tomnt;
66 char *ds;
67 char *dsmnt;
68 char *tmpsnap;
69 char errbuf[1024];
70 boolean_t isclone;
71 boolean_t scripted;
72 boolean_t classify;
73 boolean_t timestamped;
74 uint64_t shares;
75 int zerr;
76 int cleanupfd;
77 int outputfd;
78 int datafd;
79} differ_info_t;
80
81/*
82 * Given a {dsname, object id}, get the object path
83 */
84static int
85get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
86 char *pn, int maxlen, zfs_stat_t *sb)
87{
a08ee875 88 zfs_cmd_t zc = {"\0"};
572e2857
BB
89 int error;
90
91 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
92 zc.zc_obj = obj;
93
94 errno = 0;
95 error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
96 di->zerr = errno;
97
98 /* we can get stats even if we failed to get a path */
99 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
100 if (error == 0) {
101 ASSERT(di->zerr == 0);
102 (void) strlcpy(pn, zc.zc_value, maxlen);
103 return (0);
104 }
105
106 if (di->zerr == EPERM) {
107 (void) snprintf(di->errbuf, sizeof (di->errbuf),
108 dgettext(TEXT_DOMAIN,
109 "The sys_config privilege or diff delegated permission "
110 "is needed\nto discover path names"));
111 return (-1);
112 } else {
113 (void) snprintf(di->errbuf, sizeof (di->errbuf),
114 dgettext(TEXT_DOMAIN,
115 "Unable to determine path or stats for "
b8864a23 116 "object %lld in %s"), (longlong_t)obj, dsname);
572e2857
BB
117 return (-1);
118 }
119}
120
121/*
122 * stream_bytes
123 *
124 * Prints a file name out a character at a time. If the character is
125 * not in the range of what we consider "printable" ASCII, display it
e10b0808 126 * as an escaped 4-digit octal value. ASCII values less than a space
572e2857
BB
127 * are all control characters and we declare the upper end as the
128 * DELete character. This also is the last 7-bit ASCII character.
129 * We choose to treat all 8-bit ASCII as not printable for this
130 * application.
131 */
132static void
133stream_bytes(FILE *fp, const char *string)
134{
cae5b340
AX
135 char c;
136
137 while ((c = *string++) != '\0') {
138 if (c > ' ' && c != '\\' && c < '\177') {
139 (void) fprintf(fp, "%c", c);
140 } else {
141 (void) fprintf(fp, "\\%04o", (uint8_t)c);
142 }
572e2857
BB
143 }
144}
145
146static void
147print_what(FILE *fp, mode_t what)
148{
149 char symbol;
150
151 switch (what & S_IFMT) {
152 case S_IFBLK:
153 symbol = 'B';
154 break;
155 case S_IFCHR:
156 symbol = 'C';
157 break;
158 case S_IFDIR:
159 symbol = '/';
160 break;
054bc00b 161#ifdef S_IFDOOR
572e2857
BB
162 case S_IFDOOR:
163 symbol = '>';
164 break;
054bc00b 165#endif
572e2857
BB
166 case S_IFIFO:
167 symbol = '|';
168 break;
169 case S_IFLNK:
170 symbol = '@';
171 break;
054bc00b 172#ifdef S_IFPORT
572e2857
BB
173 case S_IFPORT:
174 symbol = 'P';
175 break;
054bc00b 176#endif
572e2857
BB
177 case S_IFSOCK:
178 symbol = '=';
179 break;
180 case S_IFREG:
181 symbol = 'F';
182 break;
183 default:
184 symbol = '?';
185 break;
186 }
187 (void) fprintf(fp, "%c", symbol);
188}
189
190static void
191print_cmn(FILE *fp, differ_info_t *di, const char *file)
192{
193 stream_bytes(fp, di->dsmnt);
194 stream_bytes(fp, file);
195}
196
197static void
198print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
199 zfs_stat_t *isb)
200{
201 if (di->timestamped)
202 (void) fprintf(fp, "%10lld.%09lld\t",
203 (longlong_t)isb->zs_ctime[0],
204 (longlong_t)isb->zs_ctime[1]);
205 (void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
206 if (di->classify) {
207 print_what(fp, isb->zs_mode);
208 (void) fprintf(fp, "\t");
209 }
210 print_cmn(fp, di, old);
211 if (di->scripted)
212 (void) fprintf(fp, "\t");
213 else
214 (void) fprintf(fp, " -> ");
215 print_cmn(fp, di, new);
216 (void) fprintf(fp, "\n");
217}
218
219static void
220print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
221 zfs_stat_t *isb)
222{
223 if (di->timestamped)
224 (void) fprintf(fp, "%10lld.%09lld\t",
225 (longlong_t)isb->zs_ctime[0],
226 (longlong_t)isb->zs_ctime[1]);
227 (void) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
228 if (di->classify) {
229 print_what(fp, isb->zs_mode);
230 (void) fprintf(fp, "\t");
231 }
232 print_cmn(fp, di, file);
233 (void) fprintf(fp, "\t(%+d)", delta);
234 (void) fprintf(fp, "\n");
235}
236
237static void
238print_file(FILE *fp, differ_info_t *di, char type, const char *file,
239 zfs_stat_t *isb)
240{
241 if (di->timestamped)
242 (void) fprintf(fp, "%10lld.%09lld\t",
243 (longlong_t)isb->zs_ctime[0],
244 (longlong_t)isb->zs_ctime[1]);
245 (void) fprintf(fp, "%c\t", type);
246 if (di->classify) {
247 print_what(fp, isb->zs_mode);
248 (void) fprintf(fp, "\t");
249 }
250 print_cmn(fp, di, file);
251 (void) fprintf(fp, "\n");
252}
253
254static int
255write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
256{
257 struct zfs_stat fsb, tsb;
572e2857
BB
258 mode_t fmode, tmode;
259 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
260 int fobjerr, tobjerr;
261 int change;
262
263 if (dobj == di->shares)
264 return (0);
265
266 /*
267 * Check the from and to snapshots for info on the object. If
268 * we get ENOENT, then the object just didn't exist in that
269 * snapshot. If we get ENOTSUP, then we tried to get
270 * info on a non-ZPL object, which we don't care about anyway.
271 */
272 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
273 MAXPATHLEN, &fsb);
274 if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
275 return (-1);
276
277 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
278 MAXPATHLEN, &tsb);
279 if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
280 return (-1);
281
282 /*
283 * Unallocated object sharing the same meta dnode block
284 */
285 if (fobjerr && tobjerr) {
286 ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
287 di->zerr = 0;
288 return (0);
289 }
290
291 di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
292 fmode = fsb.zs_mode & S_IFMT;
293 tmode = tsb.zs_mode & S_IFMT;
294 if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
295 tsb.zs_links == 0)
296 change = 0;
297 else
298 change = tsb.zs_links - fsb.zs_links;
299
300 if (fobjerr) {
301 if (change) {
302 print_link_change(fp, di, change, tobjname, &tsb);
303 return (0);
304 }
305 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
306 return (0);
307 } else if (tobjerr) {
308 if (change) {
309 print_link_change(fp, di, change, fobjname, &fsb);
310 return (0);
311 }
312 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
313 return (0);
314 }
315
316 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
317 tsb.zs_gen++; /* Force a generational difference */
572e2857
BB
318
319 /* Simple modification or no change */
320 if (fsb.zs_gen == tsb.zs_gen) {
321 /* No apparent changes. Could we assert !this? */
322 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
323 fsb.zs_ctime[1] == tsb.zs_ctime[1])
324 return (0);
325 if (change) {
326 print_link_change(fp, di, change,
327 change > 0 ? fobjname : tobjname, &tsb);
cae5b340 328 } else if (strcmp(fobjname, tobjname) == 0) {
572e2857
BB
329 print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
330 } else {
331 print_rename(fp, di, fobjname, tobjname, &tsb);
332 }
333 return (0);
334 } else {
335 /* file re-created or object re-used */
336 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
337 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
338 return (0);
339 }
340}
341
342static int
343write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
344{
345 uint64_t o;
346 int err;
347
348 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
cae5b340 349 if ((err = write_inuse_diffs_one(fp, di, o)) != 0)
572e2857
BB
350 return (err);
351 }
352 return (0);
353}
354
355static int
356describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
357 int maxlen)
358{
359 struct zfs_stat sb;
360
361 if (get_stats_for_obj(di, di->fromsnap, object, namebuf,
362 maxlen, &sb) != 0) {
363 /* Let it slide, if in the delete queue on from side */
364 if (di->zerr == ENOENT && sb.zs_links == 0) {
365 di->zerr = 0;
366 return (0);
367 }
368 return (-1);
369 }
370
371 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
372 return (0);
373}
374
375static int
376write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
377{
a08ee875 378 zfs_cmd_t zc = {"\0"};
572e2857
BB
379 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
380 char fobjname[MAXPATHLEN];
381
382 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
383 zc.zc_obj = dr->ddr_first - 1;
384
385 ASSERT(di->zerr == 0);
386
387 while (zc.zc_obj < dr->ddr_last) {
388 int err;
389
390 err = ioctl(lhdl->libzfs_fd, ZFS_IOC_NEXT_OBJ, &zc);
391 if (err == 0) {
392 if (zc.zc_obj == di->shares) {
393 zc.zc_obj++;
394 continue;
395 }
396 if (zc.zc_obj > dr->ddr_last) {
397 break;
398 }
399 err = describe_free(fp, di, zc.zc_obj, fobjname,
400 MAXPATHLEN);
401 if (err)
402 break;
403 } else if (errno == ESRCH) {
404 break;
405 } else {
406 (void) snprintf(di->errbuf, sizeof (di->errbuf),
407 dgettext(TEXT_DOMAIN,
408 "next allocated object (> %lld) find failure"),
b8864a23 409 (longlong_t)zc.zc_obj);
572e2857
BB
410 di->zerr = errno;
411 break;
412 }
413 }
414 if (di->zerr)
415 return (-1);
416 return (0);
417}
418
419static void *
420differ(void *arg)
421{
422 differ_info_t *di = arg;
423 dmu_diff_record_t dr;
424 FILE *ofp;
425 int err = 0;
426
427 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
428 di->zerr = errno;
cae5b340 429 strlcpy(di->errbuf, strerror(errno), sizeof (di->errbuf));
572e2857
BB
430 (void) close(di->datafd);
431 return ((void *)-1);
432 }
433
434 for (;;) {
435 char *cp = (char *)&dr;
436 int len = sizeof (dr);
437 int rv;
438
439 do {
440 rv = read(di->datafd, cp, len);
441 cp += rv;
442 len -= rv;
443 } while (len > 0 && rv > 0);
444
445 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
446 di->zerr = EPIPE;
447 break;
448 } else if (rv == 0) {
449 /* end of file at a natural breaking point */
450 break;
451 }
452
453 switch (dr.ddr_type) {
454 case DDR_FREE:
455 err = write_free_diffs(ofp, di, &dr);
456 break;
457 case DDR_INUSE:
458 err = write_inuse_diffs(ofp, di, &dr);
459 break;
460 default:
461 di->zerr = EPIPE;
462 break;
463 }
464
465 if (err || di->zerr)
466 break;
467 }
468
469 (void) fclose(ofp);
470 (void) close(di->datafd);
471 if (err)
472 return ((void *)-1);
473 if (di->zerr) {
474 ASSERT(di->zerr == EINVAL);
475 (void) snprintf(di->errbuf, sizeof (di->errbuf),
476 dgettext(TEXT_DOMAIN,
477 "Internal error: bad data from diff IOCTL"));
478 return ((void *)-1);
479 }
480 return ((void *)0);
481}
482
483static int
484find_shares_object(differ_info_t *di)
485{
486 char fullpath[MAXPATHLEN];
487 struct stat64 sb = { 0 };
488
489 (void) strlcpy(fullpath, di->dsmnt, MAXPATHLEN);
490 (void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN);
491
492 if (stat64(fullpath, &sb) != 0) {
493 (void) snprintf(di->errbuf, sizeof (di->errbuf),
494 dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
495 return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
496 }
497
498 di->shares = (uint64_t)sb.st_ino;
499 return (0);
500}
501
502static int
503make_temp_snapshot(differ_info_t *di)
504{
505 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
a08ee875 506 zfs_cmd_t zc = {"\0"};
572e2857
BB
507
508 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
509 ZDIFF_PREFIX, getpid());
510 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
511 zc.zc_cleanup_fd = di->cleanupfd;
512
513 if (ioctl(hdl->libzfs_fd, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
514 int err = errno;
515 if (err == EPERM) {
516 (void) snprintf(di->errbuf, sizeof (di->errbuf),
517 dgettext(TEXT_DOMAIN, "The diff delegated "
518 "permission is needed in order\nto create a "
519 "just-in-time snapshot for diffing\n"));
520 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
521 } else {
522 (void) snprintf(di->errbuf, sizeof (di->errbuf),
523 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
524 "snapshot of '%s'"), zc.zc_name);
525 return (zfs_standard_error(hdl, err, di->errbuf));
526 }
527 }
528
529 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
530 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
531 return (0);
532}
533
534static void
535teardown_differ_info(differ_info_t *di)
536{
537 free(di->ds);
538 free(di->dsmnt);
539 free(di->fromsnap);
540 free(di->frommnt);
541 free(di->tosnap);
542 free(di->tmpsnap);
543 free(di->tomnt);
544 (void) close(di->cleanupfd);
545}
546
547static int
548get_snapshot_names(differ_info_t *di, const char *fromsnap,
549 const char *tosnap)
550{
551 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
552 char *atptrf = NULL;
553 char *atptrt = NULL;
554 int fdslen, fsnlen;
555 int tdslen, tsnlen;
556
557 /*
558 * Can accept
cae5b340
AX
559 * fdslen fsnlen tdslen tsnlen
560 * dataset@snap1
561 * 0. dataset@snap1 dataset@snap2 >0 >1 >0 >1
562 * 1. dataset@snap1 @snap2 >0 >1 ==0 >1
563 * 2. dataset@snap1 dataset >0 >1 >0 ==0
564 * 3. @snap1 dataset@snap2 ==0 >1 >0 >1
565 * 4. @snap1 dataset ==0 >1 >0 ==0
572e2857
BB
566 */
567 if (tosnap == NULL) {
568 /* only a from snapshot given, must be valid */
569 (void) snprintf(di->errbuf, sizeof (di->errbuf),
570 dgettext(TEXT_DOMAIN,
571 "Badly formed snapshot name %s"), fromsnap);
572
573 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
574 B_FALSE)) {
575 return (zfs_error(hdl, EZFS_INVALIDNAME,
576 di->errbuf));
577 }
578
579 atptrf = strchr(fromsnap, '@');
580 ASSERT(atptrf != NULL);
581 fdslen = atptrf - fromsnap;
582
583 di->fromsnap = zfs_strdup(hdl, fromsnap);
584 di->ds = zfs_strdup(hdl, fromsnap);
585 di->ds[fdslen] = '\0';
586
587 /* the to snap will be a just-in-time snap of the head */
588 return (make_temp_snapshot(di));
589 }
590
591 (void) snprintf(di->errbuf, sizeof (di->errbuf),
592 dgettext(TEXT_DOMAIN,
593 "Unable to determine which snapshots to compare"));
594
595 atptrf = strchr(fromsnap, '@');
596 atptrt = strchr(tosnap, '@');
597 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
598 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
599 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
600 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
601
cae5b340 602 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0)) {
572e2857
BB
603 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
604 } else if ((fdslen > 0 && tdslen > 0) &&
605 ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
606 /*
607 * not the same dataset name, might be okay if
608 * tosnap is a clone of a fromsnap descendant.
609 */
cae5b340 610 char origin[ZFS_MAX_DATASET_NAME_LEN];
572e2857
BB
611 zprop_source_t src;
612 zfs_handle_t *zhp;
613
614 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
615 (void) strncpy(di->ds, tosnap, tdslen);
616 di->ds[tdslen] = '\0';
617
618 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
619 while (zhp != NULL) {
e10b0808
AX
620 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
621 sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
622 (void) zfs_close(zhp);
623 zhp = NULL;
624 break;
625 }
572e2857
BB
626 if (strncmp(origin, fromsnap, fsnlen) == 0)
627 break;
628
629 (void) zfs_close(zhp);
630 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
631 }
632
633 if (zhp == NULL) {
634 (void) snprintf(di->errbuf, sizeof (di->errbuf),
635 dgettext(TEXT_DOMAIN,
636 "Not an earlier snapshot from the same fs"));
637 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
638 } else {
639 (void) zfs_close(zhp);
640 }
641
642 di->isclone = B_TRUE;
643 di->fromsnap = zfs_strdup(hdl, fromsnap);
644 if (tsnlen) {
645 di->tosnap = zfs_strdup(hdl, tosnap);
646 } else {
647 return (make_temp_snapshot(di));
648 }
649 } else {
650 int dslen = fdslen ? fdslen : tdslen;
651
652 di->ds = zfs_alloc(hdl, dslen + 1);
653 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
654 di->ds[dslen] = '\0';
655
656 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
657 if (tsnlen) {
658 di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
659 } else {
660 return (make_temp_snapshot(di));
661 }
662 }
663 return (0);
664}
665
666static int
667get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
668{
669 boolean_t mounted;
670
671 mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
672 if (mounted == B_FALSE) {
673 (void) snprintf(di->errbuf, sizeof (di->errbuf),
674 dgettext(TEXT_DOMAIN,
675 "Cannot diff an unmounted snapshot"));
676 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
677 }
678
679 /* Avoid a double slash at the beginning of root-mounted datasets */
680 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
681 **mntpt = '\0';
682 return (0);
683}
684
685static int
686get_mountpoints(differ_info_t *di)
687{
688 char *strptr;
689 char *frommntpt;
690
691 /*
692 * first get the mountpoint for the parent dataset
693 */
694 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
695 return (-1);
696
697 strptr = strchr(di->tosnap, '@');
698 ASSERT3P(strptr, !=, NULL);
699 di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
700 ZDIFF_SNAPDIR, ++strptr);
701
702 strptr = strchr(di->fromsnap, '@');
703 ASSERT3P(strptr, !=, NULL);
704
705 frommntpt = di->dsmnt;
706 if (di->isclone) {
707 char *mntpt;
708 int err;
709
710 *strptr = '\0';
711 err = get_mountpoint(di, di->fromsnap, &mntpt);
712 *strptr = '@';
713 if (err != 0)
714 return (-1);
715 frommntpt = mntpt;
716 }
717
718 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
719 ZDIFF_SNAPDIR, ++strptr);
720
721 if (di->isclone)
722 free(frommntpt);
723
724 return (0);
725}
726
727static int
728setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
729 const char *tosnap, differ_info_t *di)
730{
731 di->zhp = zhp;
732
325f0235 733 di->cleanupfd = open(ZFS_DEV, O_RDWR);
572e2857
BB
734 VERIFY(di->cleanupfd >= 0);
735
736 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
737 return (-1);
738
739 if (get_mountpoints(di) != 0)
740 return (-1);
741
742 if (find_shares_object(di) != 0)
743 return (-1);
744
745 return (0);
746}
747
748int
749zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
750 const char *tosnap, int flags)
751{
a08ee875 752 zfs_cmd_t zc = {"\0"};
572e2857
BB
753 char errbuf[1024];
754 differ_info_t di = { 0 };
755 pthread_t tid;
756 int pipefd[2];
757 int iocerr;
758
759 (void) snprintf(errbuf, sizeof (errbuf),
760 dgettext(TEXT_DOMAIN, "zfs diff failed"));
761
762 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
763 teardown_differ_info(&di);
764 return (-1);
765 }
766
767 if (pipe(pipefd)) {
768 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
769 teardown_differ_info(&di);
770 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
771 }
772
773 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
774 di.classify = (flags & ZFS_DIFF_CLASSIFY);
775 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
776
777 di.outputfd = outfd;
778 di.datafd = pipefd[0];
779
780 if (pthread_create(&tid, NULL, differ, &di)) {
781 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
782 (void) close(pipefd[0]);
783 (void) close(pipefd[1]);
784 teardown_differ_info(&di);
785 return (zfs_error(zhp->zfs_hdl,
786 EZFS_THREADCREATEFAILED, errbuf));
787 }
788
789 /* do the ioctl() */
790 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
791 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
792 zc.zc_cookie = pipefd[1];
793
794 iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
795 if (iocerr != 0) {
796 (void) snprintf(errbuf, sizeof (errbuf),
797 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
798 if (errno == EPERM) {
799 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
800 "\n The sys_mount privilege or diff delegated "
801 "permission is needed\n to execute the "
802 "diff ioctl"));
803 } else if (errno == EXDEV) {
804 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
805 "\n Not an earlier snapshot from the same fs"));
806 } else if (errno != EPIPE || di.zerr == 0) {
807 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
808 }
809 (void) close(pipefd[1]);
810 (void) pthread_cancel(tid);
811 (void) pthread_join(tid, NULL);
812 teardown_differ_info(&di);
813 if (di.zerr != 0 && di.zerr != EPIPE) {
814 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
815 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
816 } else {
817 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
818 }
819 }
820
821 (void) close(pipefd[1]);
822 (void) pthread_join(tid, NULL);
823
824 if (di.zerr != 0) {
825 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
826 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
827 }
828 teardown_differ_info(&di);
829 return (0);
830}