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