237
238 return (error);
239 }
240
241 /*
242 * Destroys snapshots.
243 *
244 * The keys in the snaps nvlist are the snapshots to be destroyed.
245 * They must all be in the same pool.
246 *
247 * Snapshots that do not exist will be silently ignored.
248 *
249 * If 'defer' is not set, and a snapshot has user holds or clones, the
250 * destroy operation will fail and none of the snapshots will be
251 * destroyed.
252 *
253 * If 'defer' is set, and a snapshot has user holds or clones, it will be
254 * marked for deferred destruction, and will be destroyed when the last hold
255 * or clone is removed/destroyed.
256 *
257 * The return value will be 0 if all snapshots were destroyed (or marked for
258 * later destruction if 'defer' is set) or didn't exist to begin with.
259 *
260 * Otherwise the return value will be the errno of a (unspecified) snapshot
261 * that failed, no snapshots will be destroyed, and the errlist will have an
262 * entry for each snapshot that failed. The value in the errlist will be
263 * the (int32) error code.
264 */
265 int
266 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
267 {
268 nvpair_t *elem;
269 nvlist_t *args;
270 int error;
271 char pool[MAXNAMELEN];
272
273 /* determine the pool name */
274 elem = nvlist_next_nvpair(snaps, NULL);
275 if (elem == NULL)
276 return (0);
277 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
278 pool[strcspn(pool, "/@")] = '\0';
279
280 args = fnvlist_alloc();
281 fnvlist_add_nvlist(args, "snaps", snaps);
282 if (defer)
283 fnvlist_add_boolean(args, "defer");
284
285 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
286 nvlist_free(args);
287
288 return (error);
289
290 }
291
292 int
293 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
294 uint64_t *usedp)
295 {
296 nvlist_t *args;
297 nvlist_t *result;
298 int err;
299 char fs[MAXNAMELEN];
300 char *atp;
301
302 /* determine the fs name */
303 (void) strlcpy(fs, firstsnap, sizeof (fs));
304 atp = strchr(fs, '@');
305 if (atp == NULL)
306 return (EINVAL);
307 *atp = '\0';
308
309 args = fnvlist_alloc();
329
330 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
331 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
332 }
333
334 /*
335 * Create "user holds" on snapshots. If there is a hold on a snapshot,
336 * the snapshot can not be destroyed. (However, it can be marked for deletion
337 * by lzc_destroy_snaps(defer=B_TRUE).)
338 *
339 * The keys in the nvlist are snapshot names.
340 * The snapshots must all be in the same pool.
341 * The value is the name of the hold (string type).
342 *
343 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
344 * In this case, when the cleanup_fd is closed (including on process
345 * termination), the holds will be released. If the system is shut down
346 * uncleanly, the holds will be released when the pool is next opened
347 * or imported.
348 *
349 * The return value will be 0 if all holds were created. Otherwise the return
350 * value will be the errno of a (unspecified) hold that failed, no holds will
351 * be created, and the errlist will have an entry for each hold that
352 * failed (name = snapshot). The value in the errlist will be the error
353 * code (int32).
354 */
355 int
356 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
357 {
358 char pool[MAXNAMELEN];
359 nvlist_t *args;
360 nvpair_t *elem;
361 int error;
362
363 /* determine the pool name */
364 elem = nvlist_next_nvpair(holds, NULL);
365 if (elem == NULL)
366 return (0);
367 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
368 pool[strcspn(pool, "/@")] = '\0';
369
370 args = fnvlist_alloc();
371 fnvlist_add_nvlist(args, "holds", holds);
372 if (cleanup_fd != -1)
373 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
374
375 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
376 nvlist_free(args);
377 return (error);
378 }
379
380 /*
381 * Release "user holds" on snapshots. If the snapshot has been marked for
382 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
383 * any clones, and all the user holds are removed, then the snapshot will be
384 * destroyed.
385 *
386 * The keys in the nvlist are snapshot names.
387 * The snapshots must all be in the same pool.
388 * The value is a nvlist whose keys are the holds to remove.
389 *
390 * The return value will be 0 if all holds were removed.
391 * Otherwise the return value will be the errno of a (unspecified) release
392 * that failed, no holds will be released, and the errlist will have an
393 * entry for each snapshot that has failed releases (name = snapshot).
394 * The value in the errlist will be the error code (int32) of a failed release.
395 */
396 int
397 lzc_release(nvlist_t *holds, nvlist_t **errlist)
398 {
399 char pool[MAXNAMELEN];
400 nvpair_t *elem;
401
402 /* determine the pool name */
403 elem = nvlist_next_nvpair(holds, NULL);
404 if (elem == NULL)
405 return (0);
406 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
407 pool[strcspn(pool, "/@")] = '\0';
408
409 return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
410 }
411
412 /*
413 * Retrieve list of user holds on the specified snapshot.
414 *
|
237
238 return (error);
239 }
240
241 /*
242 * Destroys snapshots.
243 *
244 * The keys in the snaps nvlist are the snapshots to be destroyed.
245 * They must all be in the same pool.
246 *
247 * Snapshots that do not exist will be silently ignored.
248 *
249 * If 'defer' is not set, and a snapshot has user holds or clones, the
250 * destroy operation will fail and none of the snapshots will be
251 * destroyed.
252 *
253 * If 'defer' is set, and a snapshot has user holds or clones, it will be
254 * marked for deferred destruction, and will be destroyed when the last hold
255 * or clone is removed/destroyed.
256 *
257 * The return value will be ENOENT if none of the snapshots existed.
258 *
259 * The return value will be 0 if all snapshots were destroyed (or marked for
260 * later destruction if 'defer' is set) or didn't exist to begin with and
261 * at least one snapshot was destroyed.
262 *
263 * Otherwise the return value will be the errno of a (unspecified) snapshot
264 * that failed, no snapshots will be destroyed, and the errlist will have an
265 * entry for each snapshot that failed. The value in the errlist will be
266 * the (int32) error code.
267 */
268 int
269 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
270 {
271 nvpair_t *elem;
272 nvlist_t *args;
273 int error;
274 char pool[MAXNAMELEN];
275
276 /* determine the pool name */
277 elem = nvlist_next_nvpair(snaps, NULL);
278 if (elem == NULL)
279 return (0);
280 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
281 pool[strcspn(pool, "/@")] = '\0';
282
283 args = fnvlist_alloc();
284 fnvlist_add_nvlist(args, "snaps", snaps);
285 if (defer)
286 fnvlist_add_boolean(args, "defer");
287
288 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
289 nvlist_free(args);
290
291 return (error);
292 }
293
294 int
295 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
296 uint64_t *usedp)
297 {
298 nvlist_t *args;
299 nvlist_t *result;
300 int err;
301 char fs[MAXNAMELEN];
302 char *atp;
303
304 /* determine the fs name */
305 (void) strlcpy(fs, firstsnap, sizeof (fs));
306 atp = strchr(fs, '@');
307 if (atp == NULL)
308 return (EINVAL);
309 *atp = '\0';
310
311 args = fnvlist_alloc();
331
332 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
333 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
334 }
335
336 /*
337 * Create "user holds" on snapshots. If there is a hold on a snapshot,
338 * the snapshot can not be destroyed. (However, it can be marked for deletion
339 * by lzc_destroy_snaps(defer=B_TRUE).)
340 *
341 * The keys in the nvlist are snapshot names.
342 * The snapshots must all be in the same pool.
343 * The value is the name of the hold (string type).
344 *
345 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
346 * In this case, when the cleanup_fd is closed (including on process
347 * termination), the holds will be released. If the system is shut down
348 * uncleanly, the holds will be released when the pool is next opened
349 * or imported.
350 *
351 * Holds for snapshots which don't exist will be skipped and have an entry
352 * added to errlist, but will not cause an overall failure, except in the
353 * case that all holds where skipped.
354 *
355 * The return value will be ENOENT if none of the snapshots for the requested
356 * holds existed.
357 *
358 * The return value will be 0 if the nvl holds was empty or all holds, for
359 * snapshots that existed, were succesfully created and at least one hold
360 * was created.
361 *
362 * Otherwise the return value will be the errno of a (unspecified) hold that
363 * failed and no holds will be created.
364 *
365 * In all cases the errlist will have an entry for each hold that failed
366 * (name = snapshot), with its value being the error code (int32).
367 */
368 int
369 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
370 {
371 char pool[MAXNAMELEN];
372 nvlist_t *args;
373 nvpair_t *elem;
374 int error;
375
376 /* determine the pool name */
377 elem = nvlist_next_nvpair(holds, NULL);
378 if (elem == NULL)
379 return (0);
380 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
381 pool[strcspn(pool, "/@")] = '\0';
382
383 args = fnvlist_alloc();
384 fnvlist_add_nvlist(args, "holds", holds);
385 if (cleanup_fd != -1)
386 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
387
388 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
389 nvlist_free(args);
390 return (error);
391 }
392
393 /*
394 * Release "user holds" on snapshots. If the snapshot has been marked for
395 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
396 * any clones, and all the user holds are removed, then the snapshot will be
397 * destroyed.
398 *
399 * The keys in the nvlist are snapshot names.
400 * The snapshots must all be in the same pool.
401 * The value is a nvlist whose keys are the holds to remove.
402 *
403 * Holds which failed to release because they didn't exist will have an entry
404 * added to errlist, but will not cause an overall failure, except in the
405 * case that all releases where skipped.
406 *
407 * The return value will be ENOENT if none of the specified holds existed.
408 *
409 * The return value will be 0 if the nvl holds was empty or all holds, that
410 * existed, were succesfully removed and at least one hold was removed.
411 *
412 * Otherwise the return value will be the errno of a (unspecified) hold that
413 * failed to release and no holds will be released.
414 *
415 * In all cases the errlist will have an entry for each hold that failed to
416 * to release.
417 */
418 int
419 lzc_release(nvlist_t *holds, nvlist_t **errlist)
420 {
421 char pool[MAXNAMELEN];
422 nvpair_t *elem;
423
424 /* determine the pool name */
425 elem = nvlist_next_nvpair(holds, NULL);
426 if (elem == NULL)
427 return (0);
428 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
429 pool[strcspn(pool, "/@")] = '\0';
430
431 return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
432 }
433
434 /*
435 * Retrieve list of user holds on the specified snapshot.
436 *
|