]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - cddl/contrib/opensolaris/lib/libzfs_core/common/libzfs_core.c
Fix working with zfs_ioctl_version in libzfs_compat.h and include mirror
[FreeBSD/FreeBSD.git] / cddl / contrib / opensolaris / lib / libzfs_core / common / libzfs_core.c
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) 2012 by Delphix. All rights reserved.
24  */
25
26 /*
27  * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
28  * It has the following characteristics:
29  *
30  *  - Thread Safe.  libzfs_core is accessible concurrently from multiple
31  *  threads.  This is accomplished primarily by avoiding global data
32  *  (e.g. caching).  Since it's thread-safe, there is no reason for a
33  *  process to have multiple libzfs "instances".  Therefore, we store
34  *  our few pieces of data (e.g. the file descriptor) in global
35  *  variables.  The fd is reference-counted so that the libzfs_core
36  *  library can be "initialized" multiple times (e.g. by different
37  *  consumers within the same process).
38  *
39  *  - Committed Interface.  The libzfs_core interface will be committed,
40  *  therefore consumers can compile against it and be confident that
41  *  their code will continue to work on future releases of this code.
42  *  Currently, the interface is Evolving (not Committed), but we intend
43  *  to commit to it once it is more complete and we determine that it
44  *  meets the needs of all consumers.
45  *
46  *  - Programatic Error Handling.  libzfs_core communicates errors with
47  *  defined error numbers, and doesn't print anything to stdout/stderr.
48  *
49  *  - Thin Layer.  libzfs_core is a thin layer, marshaling arguments
50  *  to/from the kernel ioctls.  There is generally a 1:1 correspondence
51  *  between libzfs_core functions and ioctls to /dev/zfs.
52  *
53  *  - Clear Atomicity.  Because libzfs_core functions are generally 1:1
54  *  with kernel ioctls, and kernel ioctls are general atomic, each
55  *  libzfs_core function is atomic.  For example, creating multiple
56  *  snapshots with a single call to lzc_snapshot() is atomic -- it
57  *  can't fail with only some of the requested snapshots created, even
58  *  in the event of power loss or system crash.
59  *
60  *  - Continued libzfs Support.  Some higher-level operations (e.g.
61  *  support for "zfs send -R") are too complicated to fit the scope of
62  *  libzfs_core.  This functionality will continue to live in libzfs.
63  *  Where appropriate, libzfs will use the underlying atomic operations
64  *  of libzfs_core.  For example, libzfs may implement "zfs send -R |
65  *  zfs receive" by using individual "send one snapshot", rename,
66  *  destroy, and "receive one snapshot" operations in libzfs_core.
67  *  /sbin/zfs and /zbin/zpool will link with both libzfs and
68  *  libzfs_core.  Other consumers should aim to use only libzfs_core,
69  *  since that will be the supported, stable interface going forwards.
70  */
71
72 #define _IN_LIBZFS_CORE_
73
74 #include <libzfs_core.h>
75 #include <ctype.h>
76 #include <unistd.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <pthread.h>
82 #include <sys/nvpair.h>
83 #include <sys/param.h>
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #include <sys/zfs_ioctl.h>
87 #include "libzfs_core_compat.h"
88 #include "libzfs_compat.h"
89
90 #ifdef __FreeBSD__
91 int lzc_ioctl_version = -1;
92 #endif
93
94 static int g_fd;
95 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
96 static int g_refcount;
97
98 int
99 libzfs_core_init(void)
100 {
101         (void) pthread_mutex_lock(&g_lock);
102         if (g_refcount == 0) {
103                 g_fd = open("/dev/zfs", O_RDWR);
104                 if (g_fd < 0) {
105                         (void) pthread_mutex_unlock(&g_lock);
106                         return (errno);
107                 }
108         }
109         g_refcount++;
110         (void) pthread_mutex_unlock(&g_lock);
111
112         return (0);
113 }
114
115 void
116 libzfs_core_fini(void)
117 {
118         (void) pthread_mutex_lock(&g_lock);
119         ASSERT3S(g_refcount, >, 0);
120         g_refcount--;
121         if (g_refcount == 0)
122                 (void) close(g_fd);
123         (void) pthread_mutex_unlock(&g_lock);
124 }
125
126 static int
127 lzc_ioctl(zfs_ioc_t ioc, const char *name,
128     nvlist_t *source, nvlist_t **resultp)
129 {
130         zfs_cmd_t zc = { 0 };
131         int error = 0;
132         char *packed;
133 #ifdef __FreeBSD__
134         nvlist_t *oldsource;
135 #endif
136         size_t size;
137
138         ASSERT3S(g_refcount, >, 0);
139
140         (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
141
142 #ifdef __FreeBSD__
143         if (lzc_ioctl_version == -1)
144                 lzc_ioctl_version = get_zfs_ioctl_version();
145
146         if (lzc_ioctl_version < ZFS_IOCVER_LZC) {
147                 oldsource = source;
148                 error = lzc_compat_pre(&zc, &ioc, &source);
149                 if (error)
150                         return (error);
151         }
152 #endif
153
154         packed = fnvlist_pack(source, &size);
155         zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
156         zc.zc_nvlist_src_size = size;
157
158         if (resultp != NULL) {
159                 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
160                 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
161                     malloc(zc.zc_nvlist_dst_size);
162 #ifdef illumos
163                 if (zc.zc_nvlist_dst == NULL) {
164 #else
165                 if (zc.zc_nvlist_dst == 0) {
166 #endif
167                         error = ENOMEM;
168                         goto out;
169                 }
170         }
171
172         while (ioctl(g_fd, ioc, &zc) != 0) {
173                 if (errno == ENOMEM && resultp != NULL) {
174                         free((void *)(uintptr_t)zc.zc_nvlist_dst);
175                         zc.zc_nvlist_dst_size *= 2;
176                         zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
177                             malloc(zc.zc_nvlist_dst_size);
178 #ifdef illumos
179                         if (zc.zc_nvlist_dst == NULL) {
180 #else
181                         if (zc.zc_nvlist_dst == 0) {
182 #endif
183                                 error = ENOMEM;
184                                 goto out;
185                         }
186                 } else {
187                         error = errno;
188                         break;
189                 }
190         }
191
192 #ifdef __FreeBSD__
193         if (lzc_ioctl_version < ZFS_IOCVER_LZC)
194                 lzc_compat_post(&zc, ioc);
195 #endif
196         if (zc.zc_nvlist_dst_filled) {
197                 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
198                     zc.zc_nvlist_dst_size);
199         } else if (resultp != NULL) {
200                 *resultp = NULL;
201         }
202 #ifdef __FreeBSD__
203         if (lzc_ioctl_version < ZFS_IOCVER_LZC)
204                 lzc_compat_outnvl(&zc, ioc, resultp);
205 #endif
206 out:
207 #ifdef __FreeBSD__
208         if (lzc_ioctl_version < ZFS_IOCVER_LZC) {
209                 if (source != oldsource)
210                         nvlist_free(source);
211                 source = oldsource;
212         }
213 #endif
214         fnvlist_pack_free(packed, size);
215         free((void *)(uintptr_t)zc.zc_nvlist_dst);
216         return (error);
217 }
218
219 int
220 lzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
221 {
222         int error;
223         nvlist_t *args = fnvlist_alloc();
224         fnvlist_add_int32(args, "type", type);
225         if (props != NULL)
226                 fnvlist_add_nvlist(args, "props", props);
227         error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
228         nvlist_free(args);
229         return (error);
230 }
231
232 int
233 lzc_clone(const char *fsname, const char *origin,
234     nvlist_t *props)
235 {
236         int error;
237         nvlist_t *args = fnvlist_alloc();
238         fnvlist_add_string(args, "origin", origin);
239         if (props != NULL)
240                 fnvlist_add_nvlist(args, "props", props);
241         error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
242         nvlist_free(args);
243         return (error);
244 }
245
246 /*
247  * Creates snapshots.
248  *
249  * The keys in the snaps nvlist are the snapshots to be created.
250  * They must all be in the same pool.
251  *
252  * The props nvlist is properties to set.  Currently only user properties
253  * are supported.  { user:prop_name -> string value }
254  *
255  * The returned results nvlist will have an entry for each snapshot that failed.
256  * The value will be the (int32) error code.
257  *
258  * The return value will be 0 if all snapshots were created, otherwise it will
259  * be the errno of a (undetermined) snapshot that failed.
260  */
261 int
262 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
263 {
264         nvpair_t *elem;
265         nvlist_t *args;
266         int error;
267         char pool[MAXNAMELEN];
268
269         *errlist = NULL;
270
271         /* determine the pool name */
272         elem = nvlist_next_nvpair(snaps, NULL);
273         if (elem == NULL)
274                 return (0);
275         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
276         pool[strcspn(pool, "/@")] = '\0';
277
278         args = fnvlist_alloc();
279         fnvlist_add_nvlist(args, "snaps", snaps);
280         if (props != NULL)
281                 fnvlist_add_nvlist(args, "props", props);
282
283         error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
284         nvlist_free(args);
285
286         return (error);
287 }
288
289 /*
290  * Destroys snapshots.
291  *
292  * The keys in the snaps nvlist are the snapshots to be destroyed.
293  * They must all be in the same pool.
294  *
295  * Snapshots that do not exist will be silently ignored.
296  *
297  * If 'defer' is not set, and a snapshot has user holds or clones, the
298  * destroy operation will fail and none of the snapshots will be
299  * destroyed.
300  *
301  * If 'defer' is set, and a snapshot has user holds or clones, it will be
302  * marked for deferred destruction, and will be destroyed when the last hold
303  * or clone is removed/destroyed.
304  *
305  * The return value will be 0 if all snapshots were destroyed (or marked for
306  * later destruction if 'defer' is set) or didn't exist to begin with.
307  *
308  * Otherwise the return value will be the errno of a (undetermined) snapshot
309  * that failed, no snapshots will be destroyed, and the errlist will have an
310  * entry for each snapshot that failed.  The value in the errlist will be
311  * the (int32) error code.
312  */
313 int
314 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
315 {
316         nvpair_t *elem;
317         nvlist_t *args;
318         int error;
319         char pool[MAXNAMELEN];
320
321         /* determine the pool name */
322         elem = nvlist_next_nvpair(snaps, NULL);
323         if (elem == NULL)
324                 return (0);
325         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
326         pool[strcspn(pool, "/@")] = '\0';
327
328         args = fnvlist_alloc();
329         fnvlist_add_nvlist(args, "snaps", snaps);
330         if (defer)
331                 fnvlist_add_boolean(args, "defer");
332
333         error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
334         nvlist_free(args);
335
336         return (error);
337
338 }
339
340 int
341 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
342     uint64_t *usedp)
343 {
344         nvlist_t *args;
345         nvlist_t *result;
346         int err;
347         char fs[MAXNAMELEN];
348         char *atp;
349
350         /* determine the fs name */
351         (void) strlcpy(fs, firstsnap, sizeof (fs));
352         atp = strchr(fs, '@');
353         if (atp == NULL)
354                 return (EINVAL);
355         *atp = '\0';
356
357         args = fnvlist_alloc();
358         fnvlist_add_string(args, "firstsnap", firstsnap);
359
360         err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
361         nvlist_free(args);
362         if (err == 0)
363                 *usedp = fnvlist_lookup_uint64(result, "used");
364         fnvlist_free(result);
365
366         return (err);
367 }
368
369 boolean_t
370 lzc_exists(const char *dataset)
371 {
372         /*
373          * The objset_stats ioctl is still legacy, so we need to construct our
374          * own zfs_cmd_t rather than using zfsc_ioctl().
375          */
376         zfs_cmd_t zc = { 0 };
377
378         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
379         return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
380 }
381
382 /*
383  * If fromsnap is NULL, a full (non-incremental) stream will be sent.
384  */
385 int
386 lzc_send(const char *snapname, const char *fromsnap, int fd)
387 {
388         nvlist_t *args;
389         int err;
390
391         args = fnvlist_alloc();
392         fnvlist_add_int32(args, "fd", fd);
393         if (fromsnap != NULL)
394                 fnvlist_add_string(args, "fromsnap", fromsnap);
395         err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
396         nvlist_free(args);
397         return (err);
398 }
399
400 /*
401  * If fromsnap is NULL, a full (non-incremental) stream will be estimated.
402  */
403 int
404 lzc_send_space(const char *snapname, const char *fromsnap, uint64_t *spacep)
405 {
406         nvlist_t *args;
407         nvlist_t *result;
408         int err;
409
410         args = fnvlist_alloc();
411         if (fromsnap != NULL)
412                 fnvlist_add_string(args, "fromsnap", fromsnap);
413         err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
414         nvlist_free(args);
415         if (err == 0)
416                 *spacep = fnvlist_lookup_uint64(result, "space");
417         nvlist_free(result);
418         return (err);
419 }
420
421 static int
422 recv_read(int fd, void *buf, int ilen)
423 {
424         char *cp = buf;
425         int rv;
426         int len = ilen;
427
428         do {
429                 rv = read(fd, cp, len);
430                 cp += rv;
431                 len -= rv;
432         } while (rv > 0);
433
434         if (rv < 0 || len != 0)
435                 return (EIO);
436
437         return (0);
438 }
439
440 /*
441  * The simplest receive case: receive from the specified fd, creating the
442  * specified snapshot.  Apply the specified properties a "received" properties
443  * (which can be overridden by locally-set properties).  If the stream is a
444  * clone, its origin snapshot must be specified by 'origin'.  The 'force'
445  * flag will cause the target filesystem to be rolled back or destroyed if
446  * necessary to receive.
447  *
448  * Return 0 on success or an errno on failure.
449  *
450  * Note: this interface does not work on dedup'd streams
451  * (those with DMU_BACKUP_FEATURE_DEDUP).
452  */
453 int
454 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
455     boolean_t force, int fd)
456 {
457         /*
458          * The receive ioctl is still legacy, so we need to construct our own
459          * zfs_cmd_t rather than using zfsc_ioctl().
460          */
461         zfs_cmd_t zc = { 0 };
462         char *atp;
463         char *packed = NULL;
464         size_t size;
465         dmu_replay_record_t drr;
466         int error;
467
468         ASSERT3S(g_refcount, >, 0);
469
470         /* zc_name is name of containing filesystem */
471         (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
472         atp = strchr(zc.zc_name, '@');
473         if (atp == NULL)
474                 return (EINVAL);
475         *atp = '\0';
476
477         /* if the fs does not exist, try its parent. */
478         if (!lzc_exists(zc.zc_name)) {
479                 char *slashp = strrchr(zc.zc_name, '/');
480                 if (slashp == NULL)
481                         return (ENOENT);
482                 *slashp = '\0';
483
484         }
485
486         /* zc_value is full name of the snapshot to create */
487         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
488
489         if (props != NULL) {
490                 /* zc_nvlist_src is props to set */
491                 packed = fnvlist_pack(props, &size);
492                 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
493                 zc.zc_nvlist_src_size = size;
494         }
495
496         /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
497         if (origin != NULL)
498                 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
499
500         /* zc_begin_record is non-byteswapped BEGIN record */
501         error = recv_read(fd, &drr, sizeof (drr));
502         if (error != 0)
503                 goto out;
504         zc.zc_begin_record = drr.drr_u.drr_begin;
505
506         /* zc_cookie is fd to read from */
507         zc.zc_cookie = fd;
508
509         /* zc guid is force flag */
510         zc.zc_guid = force;
511
512         /* zc_cleanup_fd is unused */
513         zc.zc_cleanup_fd = -1;
514
515         error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
516         if (error != 0)
517                 error = errno;
518
519 out:
520         if (packed != NULL)
521                 fnvlist_pack_free(packed, size);
522         free((void*)(uintptr_t)zc.zc_nvlist_dst);
523         return (error);
524 }