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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
  25  * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
  26  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
  27  */
  28 
  29 #include <stdio.h>
  30 #include <stdlib.h>
  31 #include <strings.h>
  32 #include <unistd.h>
  33 #include <stddef.h>
  34 #include <libintl.h>
  35 #include <libzfs.h>
  36 
  37 #include "libzfs_impl.h"
  38 
  39 int
  40 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
  41 {
  42         nvlist_t *nvl = zfs_get_clones_nvl(zhp);
  43         nvpair_t *pair;
  44 
  45         if (nvl == NULL)
  46                 return (0);
  47 
  48         for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
  49             pair = nvlist_next_nvpair(nvl, pair)) {
  50                 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
  51                     ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
  52                 if (clone != NULL) {
  53                         int err = func(clone, data);
  54                         if (err != 0)
  55                                 return (err);
  56                 }
  57         }
  58         return (0);
  59 }
  60 
  61 static int
  62 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
  63 {
  64         int rc;
  65         uint64_t        orig_cookie;
  66 
  67         orig_cookie = zc->zc_cookie;
  68 top:
  69         (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
  70         rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
  71 
  72         if (rc == -1) {
  73                 switch (errno) {
  74                 case ENOMEM:
  75                         /* expand nvlist memory and try again */
  76                         if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
  77                                 zcmd_free_nvlists(zc);
  78                                 return (-1);
  79                         }
  80                         zc->zc_cookie = orig_cookie;
  81                         goto top;
  82                 /*
  83                  * An errno value of ESRCH indicates normal completion.
  84                  * If ENOENT is returned, then the underlying dataset
  85                  * has been removed since we obtained the handle.
  86                  */
  87                 case ESRCH:
  88                 case ENOENT:
  89                         rc = 1;
  90                         break;
  91                 default:
  92                         rc = zfs_standard_error(zhp->zfs_hdl, errno,
  93                             dgettext(TEXT_DOMAIN,
  94                             "cannot iterate filesystems"));
  95                         break;
  96                 }
  97         }
  98         return (rc);
  99 }
 100 
 101 /*
 102  * Iterate over all child filesystems
 103  */
 104 int
 105 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
 106 {
 107         zfs_cmd_t zc = { 0 };
 108         zfs_handle_t *nzhp;
 109         int ret;
 110 
 111         if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
 112                 return (0);
 113 
 114         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
 115                 return (-1);
 116 
 117         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
 118             &zc)) == 0) {
 119                 /*
 120                  * Silently ignore errors, as the only plausible explanation is
 121                  * that the pool has since been removed.
 122                  */
 123                 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
 124                     &zc)) == NULL) {
 125                         continue;
 126                 }
 127 
 128                 if ((ret = func(nzhp, data)) != 0) {
 129                         zcmd_free_nvlists(&zc);
 130                         return (ret);
 131                 }
 132         }
 133         zcmd_free_nvlists(&zc);
 134         return ((ret < 0) ? ret : 0);
 135 }
 136 
 137 /*
 138  * Iterate over all snapshots
 139  */
 140 int
 141 zfs_iter_snapshots_internal(zfs_handle_t *zhp, boolean_t simple,
 142     zfs_iter_f func, void *data, boolean_t autosnaps)
 143 {
 144         zfs_cmd_t zc = { 0 };
 145         int ret;
 146 
 147         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
 148             zhp->zfs_type == ZFS_TYPE_BOOKMARK ||
 149             zhp->zfs_type == ZFS_TYPE_AUTOSNAP)
 150                 return (0);
 151 
 152         zc.zc_simple = simple;
 153 
 154         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
 155                 return (-1);
 156 
 157         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
 158             &zc)) == 0) {
 159                 zfs_handle_t *nzhp;
 160 
 161                 if (simple)
 162                         nzhp = make_dataset_simple_handle_zc(zhp, &zc);
 163                 else
 164                         nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
 165                 if (nzhp == NULL)
 166                         continue;
 167 
 168                 if (autosnaps) {
 169                         if (zfs_get_type(nzhp) != ZFS_TYPE_AUTOSNAP) {
 170                                 zfs_close(nzhp);
 171                                 continue;
 172                         }
 173                 } else {
 174                         if (zfs_get_type(nzhp) != ZFS_TYPE_SNAPSHOT) {
 175                                 zfs_close(nzhp);
 176                                 continue;
 177                         }
 178                 }
 179 
 180                 if ((ret = func(nzhp, data)) != 0) {
 181                         zcmd_free_nvlists(&zc);
 182                         return (ret);
 183                 }
 184         }
 185 
 186         zcmd_free_nvlists(&zc);
 187         return ((ret < 0) ? ret : 0);
 188 }
 189 
 190 /*
 191  * Iterate over all snapshots
 192  */
 193 int
 194 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
 195     void *data)
 196 {
 197         return (zfs_iter_snapshots_internal(zhp, simple, func, data, B_FALSE));
 198 }
 199 
 200 int
 201 zfs_iter_autosnapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
 202     void *data)
 203 {
 204         return (zfs_iter_snapshots_internal(zhp, simple, func, data, B_TRUE));
 205 }
 206 
 207 /*
 208  * Iterate over all bookmarks
 209  */
 210 int
 211 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
 212 {
 213         zfs_handle_t *nzhp;
 214         nvlist_t *props = NULL;
 215         nvlist_t *bmarks = NULL;
 216         int err;
 217 
 218         if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
 219             ZFS_TYPE_BOOKMARK | ZFS_TYPE_AUTOSNAP)) != 0)
 220                 return (0);
 221 
 222         /* Setup the requested properties nvlist. */
 223         props = fnvlist_alloc();
 224         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
 225         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
 226         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
 227 
 228         if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
 229                 goto out;
 230 
 231         for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
 232             pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
 233                 char name[ZFS_MAX_DATASET_NAME_LEN];
 234                 char *bmark_name;
 235                 nvlist_t *bmark_props;
 236 
 237                 bmark_name = nvpair_name(pair);
 238                 bmark_props = fnvpair_value_nvlist(pair);
 239 
 240                 (void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
 241                     bmark_name);
 242 
 243                 nzhp = make_bookmark_handle(zhp, name, bmark_props);
 244                 if (nzhp == NULL)
 245                         continue;
 246 
 247                 if ((err = func(nzhp, data)) != 0)
 248                         goto out;
 249         }
 250 
 251 out:
 252         fnvlist_free(props);
 253         fnvlist_free(bmarks);
 254 
 255         return (err);
 256 }
 257 
 258 /*
 259  * Routines for dealing with the sorted snapshot functionality
 260  */
 261 typedef struct zfs_node {
 262         zfs_handle_t    *zn_handle;
 263         avl_node_t      zn_avlnode;
 264 } zfs_node_t;
 265 
 266 static int
 267 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
 268 {
 269         avl_tree_t *avl = data;
 270         zfs_node_t *node;
 271         zfs_node_t search;
 272 
 273         search.zn_handle = zhp;
 274         node = avl_find(avl, &search, NULL);
 275         if (node) {
 276                 /*
 277                  * If this snapshot was renamed while we were creating the
 278                  * AVL tree, it's possible that we already inserted it under
 279                  * its old name. Remove the old handle before adding the new
 280                  * one.
 281                  */
 282                 zfs_close(node->zn_handle);
 283                 avl_remove(avl, node);
 284                 free(node);
 285         }
 286 
 287         node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
 288         node->zn_handle = zhp;
 289         avl_add(avl, node);
 290 
 291         return (0);
 292 }
 293 
 294 static int
 295 zfs_snapshot_compare(const void *larg, const void *rarg)
 296 {
 297         zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
 298         zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
 299         uint64_t lcreate, rcreate;
 300 
 301         /*
 302          * Sort them according to creation time.  We use the hidden
 303          * CREATETXG property to get an absolute ordering of snapshots.
 304          */
 305         lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
 306         rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
 307 
 308         if (lcreate < rcreate)
 309                 return (-1);
 310         else if (lcreate > rcreate)
 311                 return (+1);
 312         else
 313                 return (0);
 314 }
 315 
 316 int
 317 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
 318 {
 319         int ret = 0;
 320         zfs_node_t *node;
 321         avl_tree_t avl;
 322         void *cookie = NULL;
 323 
 324         avl_create(&avl, zfs_snapshot_compare,
 325             sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
 326 
 327         ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
 328 
 329         for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
 330                 ret |= callback(node->zn_handle, data);
 331 
 332         while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
 333                 free(node);
 334 
 335         avl_destroy(&avl);
 336 
 337         return (ret);
 338 }
 339 
 340 typedef struct {
 341         char *ssa_first;
 342         char *ssa_last;
 343         boolean_t ssa_seenfirst;
 344         boolean_t ssa_seenlast;
 345         zfs_iter_f ssa_func;
 346         void *ssa_arg;
 347 } snapspec_arg_t;
 348 
 349 static int
 350 snapspec_cb(zfs_handle_t *zhp, void *arg)
 351 {
 352         snapspec_arg_t *ssa = arg;
 353         const char *shortsnapname;
 354         int err = 0;
 355 
 356         if (ssa->ssa_seenlast)
 357                 return (0);
 358 
 359         shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
 360         if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
 361                 ssa->ssa_seenfirst = B_TRUE;
 362         if (strcmp(shortsnapname, ssa->ssa_last) == 0)
 363                 ssa->ssa_seenlast = B_TRUE;
 364 
 365         if (ssa->ssa_seenfirst) {
 366                 err = ssa->ssa_func(zhp, ssa->ssa_arg);
 367         } else {
 368                 zfs_close(zhp);
 369         }
 370 
 371         return (err);
 372 }
 373 
 374 /*
 375  * spec is a string like "A,B%C,D"
 376  *
 377  * <snaps>, where <snaps> can be:
 378  *      <snap>          (single snapshot)
 379  *      <snap>%<snap>   (range of snapshots, inclusive)
 380  *      %<snap>         (range of snapshots, starting with earliest)
 381  *      <snap>%         (range of snapshots, ending with last)
 382  *      %               (all snapshots)
 383  *      <snaps>[,...]   (comma separated list of the above)
 384  *
 385  * If a snapshot can not be opened, continue trying to open the others, but
 386  * return ENOENT at the end.
 387  */
 388 int
 389 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
 390     zfs_iter_f func, void *arg)
 391 {
 392         char *buf, *comma_separated, *cp;
 393         int err = 0;
 394         int ret = 0;
 395 
 396         buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
 397         cp = buf;
 398 
 399         while ((comma_separated = strsep(&cp, ",")) != NULL) {
 400                 char *pct = strchr(comma_separated, '%');
 401                 if (pct != NULL) {
 402                         snapspec_arg_t ssa = { 0 };
 403                         ssa.ssa_func = func;
 404                         ssa.ssa_arg = arg;
 405 
 406                         if (pct == comma_separated)
 407                                 ssa.ssa_seenfirst = B_TRUE;
 408                         else
 409                                 ssa.ssa_first = comma_separated;
 410                         *pct = '\0';
 411                         ssa.ssa_last = pct + 1;
 412 
 413                         /*
 414                          * If there is a lastname specified, make sure it
 415                          * exists.
 416                          */
 417                         if (ssa.ssa_last[0] != '\0') {
 418                                 char snapname[ZFS_MAX_DATASET_NAME_LEN];
 419                                 (void) snprintf(snapname, sizeof (snapname),
 420                                     "%s@%s", zfs_get_name(fs_zhp),
 421                                     ssa.ssa_last);
 422                                 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
 423                                     snapname, ZFS_TYPE_SNAPSHOT)) {
 424                                         ret = ENOENT;
 425                                         continue;
 426                                 }
 427                         }
 428 
 429                         err = zfs_iter_snapshots_sorted(fs_zhp,
 430                             snapspec_cb, &ssa);
 431                         if (ret == 0)
 432                                 ret = err;
 433                         if (ret == 0 && (!ssa.ssa_seenfirst ||
 434                             (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
 435                                 ret = ENOENT;
 436                         }
 437                 } else {
 438                         char snapname[ZFS_MAX_DATASET_NAME_LEN];
 439                         zfs_handle_t *snap_zhp;
 440                         (void) snprintf(snapname, sizeof (snapname), "%s@%s",
 441                             zfs_get_name(fs_zhp), comma_separated);
 442                         snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
 443                             snapname);
 444                         if (snap_zhp == NULL) {
 445                                 ret = ENOENT;
 446                                 continue;
 447                         }
 448                         err = func(snap_zhp, arg);
 449                         if (ret == 0)
 450                                 ret = err;
 451                 }
 452         }
 453 
 454         free(buf);
 455         return (ret);
 456 }
 457 
 458 /*
 459  * Iterate over all children, snapshots and filesystems
 460  */
 461 int
 462 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
 463 {
 464         int ret;
 465 
 466         if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
 467                 return (ret);
 468 
 469         return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
 470 }
 471 
 472 
 473 typedef struct iter_stack_frame {
 474         struct iter_stack_frame *next;
 475         zfs_handle_t *zhp;
 476 } iter_stack_frame_t;
 477 
 478 typedef struct iter_dependents_arg {
 479         boolean_t first;
 480         boolean_t allowrecursion;
 481         iter_stack_frame_t *stack;
 482         zfs_iter_f func;
 483         void *data;
 484 } iter_dependents_arg_t;
 485 
 486 static int
 487 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
 488 {
 489         iter_dependents_arg_t *ida = arg;
 490         int err = 0;
 491         boolean_t first = ida->first;
 492         ida->first = B_FALSE;
 493 
 494         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
 495                 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
 496         } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
 497                 iter_stack_frame_t isf;
 498                 iter_stack_frame_t *f;
 499 
 500                 /*
 501                  * check if there is a cycle by seeing if this fs is already
 502                  * on the stack.
 503                  */
 504                 for (f = ida->stack; f != NULL; f = f->next) {
 505                         if (f->zhp->zfs_dmustats.dds_guid ==
 506                             zhp->zfs_dmustats.dds_guid) {
 507                                 if (ida->allowrecursion) {
 508                                         zfs_close(zhp);
 509                                         return (0);
 510                                 } else {
 511                                         zfs_error_aux(zhp->zfs_hdl,
 512                                             dgettext(TEXT_DOMAIN,
 513                                             "recursive dependency at '%s'"),
 514                                             zfs_get_name(zhp));
 515                                         err = zfs_error(zhp->zfs_hdl,
 516                                             EZFS_RECURSIVE,
 517                                             dgettext(TEXT_DOMAIN,
 518                                             "cannot determine dependent "
 519                                             "datasets"));
 520                                         zfs_close(zhp);
 521                                         return (err);
 522                                 }
 523                         }
 524                 }
 525 
 526                 isf.zhp = zhp;
 527                 isf.next = ida->stack;
 528                 ida->stack = &isf;
 529                 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
 530                 if (err == 0) {
 531                         err = zfs_iter_snapshots(zhp, B_FALSE,
 532                             iter_dependents_cb, ida);
 533                 }
 534 
 535                 if (err == 0) {
 536                         err = zfs_iter_autosnapshots(zhp, B_FALSE,
 537                             iter_dependents_cb, ida);
 538                 }
 539 
 540                 ida->stack = isf.next;
 541         }
 542 
 543         if (!first && err == 0)
 544                 err = ida->func(zhp, ida->data);
 545         else
 546                 zfs_close(zhp);
 547 
 548         return (err);
 549 }
 550 
 551 int
 552 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
 553     zfs_iter_f func, void *data)
 554 {
 555         iter_dependents_arg_t ida;
 556         ida.allowrecursion = allowrecursion;
 557         ida.stack = NULL;
 558         ida.func = func;
 559         ida.data = data;
 560         ida.first = B_TRUE;
 561         return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
 562 }