be.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <sys/param.h>
  28. #include <sys/module.h>
  29. #include <sys/mount.h>
  30. #include <sys/stat.h>
  31. #include <sys/ucred.h>
  32. #include <sys/queue.h>
  33. #include <sys/zfs_context.h>
  34. #include <sys/mntent.h>
  35. #include <sys/zfs_ioctl.h>
  36. #include <libzutil.h>
  37. #include <ctype.h>
  38. #include <libgen.h>
  39. #include <libzfs_core.h>
  40. #include <libzfs_impl.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #include <libzfsbootenv.h>
  46. #include "be.h"
  47. #include "be_impl.h"
  48. struct promote_entry {
  49. char name[BE_MAXPATHLEN];
  50. SLIST_ENTRY(promote_entry) link;
  51. };
  52. struct be_destroy_data {
  53. libbe_handle_t *lbh;
  54. char target_name[BE_MAXPATHLEN];
  55. char *snapname;
  56. SLIST_HEAD(, promote_entry) promotelist;
  57. };
  58. #if SOON
  59. static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
  60. const char *child_path);
  61. static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
  62. #endif
  63. /* Arbitrary... should tune */
  64. #define BE_SNAP_SERIAL_MAX 1024
  65. /*
  66. * Iterator function for locating the rootfs amongst the children of the
  67. * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *.
  68. */
  69. static int
  70. be_locate_rootfs(libbe_handle_t *lbh)
  71. {
  72. struct statfs sfs;
  73. struct mnttab entry;
  74. zfs_handle_t *zfs;
  75. /*
  76. * Check first if root is ZFS; if not, we'll bail on rootfs capture.
  77. * Unfortunately needed because zfs_path_to_zhandle will emit to
  78. * stderr if / isn't actually a ZFS filesystem, which we'd like
  79. * to avoid.
  80. */
  81. if (statfs("/", &sfs) == 0) {
  82. statfs2mnttab(&sfs, &entry);
  83. if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
  84. return (1);
  85. } else
  86. return (1);
  87. zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
  88. if (zfs == NULL)
  89. return (1);
  90. strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
  91. zfs_close(zfs);
  92. return (0);
  93. }
  94. /*
  95. * Initializes the libbe context to operate in the root boot environment
  96. * dataset, for example, zroot/ROOT.
  97. */
  98. libbe_handle_t *
  99. libbe_init(const char *root)
  100. {
  101. char altroot[MAXPATHLEN];
  102. libbe_handle_t *lbh;
  103. char *poolname, *pos;
  104. int pnamelen;
  105. lbh = NULL;
  106. poolname = pos = NULL;
  107. /*
  108. * If the zfs kmod's not loaded then the later libzfs_init() will load
  109. * the module for us, but that's not desirable for a couple reasons. If
  110. * the module's not loaded, there's no pool imported and we're going to
  111. * fail anyways. We also don't really want libbe consumers to have that
  112. * kind of side-effect (module loading) in the general case.
  113. */
  114. if (modfind("zfs") < 0)
  115. goto err;
  116. if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
  117. goto err;
  118. if ((lbh->lzh = libzfs_init()) == NULL)
  119. goto err;
  120. /*
  121. * Grab rootfs, we'll work backwards from there if an optional BE root
  122. * has not been passed in.
  123. */
  124. if (be_locate_rootfs(lbh) != 0) {
  125. if (root == NULL)
  126. goto err;
  127. *lbh->rootfs = '\0';
  128. }
  129. if (root == NULL) {
  130. /* Strip off the final slash from rootfs to get the be root */
  131. strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
  132. pos = strrchr(lbh->root, '/');
  133. if (pos == NULL)
  134. goto err;
  135. *pos = '\0';
  136. } else
  137. strlcpy(lbh->root, root, sizeof(lbh->root));
  138. if ((pos = strchr(lbh->root, '/')) == NULL)
  139. goto err;
  140. pnamelen = pos - lbh->root;
  141. poolname = malloc(pnamelen + 1);
  142. if (poolname == NULL)
  143. goto err;
  144. strlcpy(poolname, lbh->root, pnamelen + 1);
  145. if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
  146. goto err;
  147. free(poolname);
  148. poolname = NULL;
  149. if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
  150. sizeof(lbh->bootfs), NULL, true) != 0)
  151. goto err;
  152. if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT,
  153. altroot, sizeof(altroot), NULL, true) == 0 &&
  154. strcmp(altroot, "-") != 0)
  155. lbh->altroot_len = strlen(altroot);
  156. (void) lzbe_get_boot_device(zpool_get_name(lbh->active_phandle),
  157. &lbh->bootonce);
  158. return (lbh);
  159. err:
  160. if (lbh != NULL) {
  161. if (lbh->active_phandle != NULL)
  162. zpool_close(lbh->active_phandle);
  163. if (lbh->lzh != NULL)
  164. libzfs_fini(lbh->lzh);
  165. free(lbh);
  166. }
  167. free(poolname);
  168. return (NULL);
  169. }
  170. /*
  171. * Free memory allocated by libbe_init()
  172. */
  173. void
  174. libbe_close(libbe_handle_t *lbh)
  175. {
  176. if (lbh->active_phandle != NULL)
  177. zpool_close(lbh->active_phandle);
  178. libzfs_fini(lbh->lzh);
  179. free(lbh->bootonce);
  180. free(lbh);
  181. }
  182. /*
  183. * Proxy through to libzfs for the moment.
  184. */
  185. void
  186. be_nicenum(uint64_t num, char *buf, size_t buflen)
  187. {
  188. zfs_nicenum(num, buf, buflen);
  189. }
  190. static bool
  191. be_should_promote_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd)
  192. {
  193. char *atpos;
  194. if (zfs_get_type(zfs_hdl) != ZFS_TYPE_SNAPSHOT)
  195. return (false);
  196. /*
  197. * If we're deleting a snapshot, we need to make sure we only promote
  198. * clones that are derived from one of the snapshots we're deleting,
  199. * rather than that of a snapshot we're not touching. This keeps stuff
  200. * in a consistent state, making sure that we don't error out unless
  201. * we really need to.
  202. */
  203. if (bdd->snapname == NULL)
  204. return (true);
  205. atpos = strchr(zfs_get_name(zfs_hdl), '@');
  206. return (strcmp(atpos + 1, bdd->snapname) == 0);
  207. }
  208. /*
  209. * This is executed from be_promote_dependent_clones via zfs_iter_dependents,
  210. * It checks if the dependent type is a snapshot then attempts to find any
  211. * clones associated with it. Any clones not related to the destroy target are
  212. * added to the promote list.
  213. */
  214. static int
  215. be_dependent_clone_cb(zfs_handle_t *zfs_hdl, void *data)
  216. {
  217. int err;
  218. bool found;
  219. const char *name;
  220. struct nvlist *nvl;
  221. struct nvpair *nvp;
  222. struct be_destroy_data *bdd;
  223. struct promote_entry *entry, *newentry;
  224. nvp = NULL;
  225. err = 0;
  226. bdd = (struct be_destroy_data *)data;
  227. if (be_should_promote_clones(zfs_hdl, bdd) &&
  228. (nvl = zfs_get_clones_nvl(zfs_hdl)) != NULL) {
  229. while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
  230. name = nvpair_name(nvp);
  231. /*
  232. * Skip if the clone is equal to, or a child of, the
  233. * destroy target.
  234. */
  235. if (strncmp(name, bdd->target_name,
  236. strlen(bdd->target_name)) == 0 ||
  237. strstr(name, bdd->target_name) == name) {
  238. continue;
  239. }
  240. found = false;
  241. SLIST_FOREACH(entry, &bdd->promotelist, link) {
  242. if (strcmp(entry->name, name) == 0) {
  243. found = true;
  244. break;
  245. }
  246. }
  247. if (found)
  248. continue;
  249. newentry = malloc(sizeof(struct promote_entry));
  250. if (newentry == NULL) {
  251. err = ENOMEM;
  252. break;
  253. }
  254. #define BE_COPY_NAME(entry, src) \
  255. strlcpy((entry)->name, (src), sizeof((entry)->name))
  256. if (BE_COPY_NAME(newentry, name) >=
  257. sizeof(newentry->name)) {
  258. /* Shouldn't happen. */
  259. free(newentry);
  260. err = ENAMETOOLONG;
  261. break;
  262. }
  263. #undef BE_COPY_NAME
  264. /*
  265. * We're building up a SLIST here to make sure both that
  266. * we get the order right and so that we don't
  267. * inadvertently observe the wrong state by promoting
  268. * datasets while we're still walking the tree. The
  269. * latter can lead to situations where we promote a BE
  270. * then effectively demote it again.
  271. */
  272. SLIST_INSERT_HEAD(&bdd->promotelist, newentry, link);
  273. }
  274. nvlist_free(nvl);
  275. }
  276. zfs_close(zfs_hdl);
  277. return (err);
  278. }
  279. /*
  280. * This is called before a destroy, so that any datasets(environments) that are
  281. * dependent on this one get promoted before destroying the target.
  282. */
  283. static int
  284. be_promote_dependent_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd)
  285. {
  286. int err;
  287. zfs_handle_t *clone;
  288. struct promote_entry *entry;
  289. snprintf(bdd->target_name, BE_MAXPATHLEN, "%s/", zfs_get_name(zfs_hdl));
  290. err = zfs_iter_dependents(zfs_hdl, true, be_dependent_clone_cb, bdd);
  291. /*
  292. * Drain the list and walk away from it if we're only deleting a
  293. * snapshot.
  294. */
  295. if (bdd->snapname != NULL && !SLIST_EMPTY(&bdd->promotelist))
  296. err = BE_ERR_HASCLONES;
  297. while (!SLIST_EMPTY(&bdd->promotelist)) {
  298. entry = SLIST_FIRST(&bdd->promotelist);
  299. SLIST_REMOVE_HEAD(&bdd->promotelist, link);
  300. #define ZFS_GRAB_CLONE() \
  301. zfs_open(bdd->lbh->lzh, entry->name, ZFS_TYPE_FILESYSTEM)
  302. /*
  303. * Just skip this part on error, we still want to clean up the
  304. * promotion list after the first error. We'll then preserve it
  305. * all the way back.
  306. */
  307. if (err == 0 && (clone = ZFS_GRAB_CLONE()) != NULL) {
  308. err = zfs_promote(clone);
  309. if (err != 0)
  310. err = BE_ERR_DESTROYMNT;
  311. zfs_close(clone);
  312. }
  313. #undef ZFS_GRAB_CLONE
  314. free(entry);
  315. }
  316. return (err);
  317. }
  318. static int
  319. be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
  320. {
  321. char path[BE_MAXPATHLEN];
  322. struct be_destroy_data *bdd;
  323. zfs_handle_t *snap;
  324. int err;
  325. bdd = (struct be_destroy_data *)data;
  326. if (bdd->snapname == NULL) {
  327. err = zfs_iter_children(zfs_hdl, be_destroy_cb, data);
  328. if (err != 0)
  329. return (err);
  330. return (zfs_destroy(zfs_hdl, false));
  331. }
  332. /* If we're dealing with snapshots instead, delete that one alone */
  333. err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data);
  334. if (err != 0)
  335. return (err);
  336. /*
  337. * This part is intentionally glossing over any potential errors,
  338. * because there's a lot less potential for errors when we're cleaning
  339. * up snapshots rather than a full deep BE. The primary error case
  340. * here being if the snapshot doesn't exist in the first place, which
  341. * the caller will likely deem insignificant as long as it doesn't
  342. * exist after the call. Thus, such a missing snapshot shouldn't jam
  343. * up the destruction.
  344. */
  345. snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl),
  346. bdd->snapname);
  347. if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
  348. return (0);
  349. snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT);
  350. if (snap != NULL)
  351. zfs_destroy(snap, false);
  352. return (0);
  353. }
  354. #define BE_DESTROY_WANTORIGIN (BE_DESTROY_ORIGIN | BE_DESTROY_AUTOORIGIN)
  355. /*
  356. * Destroy the boot environment or snapshot specified by the name
  357. * parameter. Options are or'd together with the possible values:
  358. * BE_DESTROY_FORCE : forces operation on mounted datasets
  359. * BE_DESTROY_ORIGIN: destroy the origin snapshot as well
  360. */
  361. static int
  362. be_destroy_internal(libbe_handle_t *lbh, const char *name, int options,
  363. bool odestroyer)
  364. {
  365. struct be_destroy_data bdd;
  366. char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN];
  367. zfs_handle_t *fs;
  368. char *snapdelim;
  369. int err, force, mounted;
  370. size_t rootlen;
  371. bdd.lbh = lbh;
  372. bdd.snapname = NULL;
  373. SLIST_INIT(&bdd.promotelist);
  374. force = options & BE_DESTROY_FORCE;
  375. *origin = '\0';
  376. be_root_concat(lbh, name, path);
  377. if ((snapdelim = strchr(path, '@')) == NULL) {
  378. if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
  379. return (set_error(lbh, BE_ERR_NOENT));
  380. if (strcmp(path, lbh->rootfs) == 0 ||
  381. strcmp(path, lbh->bootfs) == 0)
  382. return (set_error(lbh, BE_ERR_DESTROYACT));
  383. fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM);
  384. if (fs == NULL)
  385. return (set_error(lbh, BE_ERR_ZFSOPEN));
  386. /* Don't destroy a mounted dataset unless force is specified */
  387. if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
  388. if (force) {
  389. zfs_unmount(fs, NULL, 0);
  390. } else {
  391. free(bdd.snapname);
  392. return (set_error(lbh, BE_ERR_DESTROYMNT));
  393. }
  394. }
  395. /* Handle destroying bootonce */
  396. if (lbh->bootonce != NULL &&
  397. strcmp(path, lbh->bootonce) == 0)
  398. (void) lzbe_set_boot_device(
  399. zpool_get_name(lbh->active_phandle), lzbe_add, NULL);
  400. } else {
  401. /*
  402. * If we're initially destroying a snapshot, origin options do
  403. * not make sense. If we're destroying the origin snapshot of
  404. * a BE, we want to maintain the options in case we need to
  405. * fake success after failing to promote.
  406. */
  407. if (!odestroyer)
  408. options &= ~BE_DESTROY_WANTORIGIN;
  409. if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
  410. return (set_error(lbh, BE_ERR_NOENT));
  411. bdd.snapname = strdup(snapdelim + 1);
  412. if (bdd.snapname == NULL)
  413. return (set_error(lbh, BE_ERR_NOMEM));
  414. *snapdelim = '\0';
  415. fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET);
  416. if (fs == NULL) {
  417. free(bdd.snapname);
  418. return (set_error(lbh, BE_ERR_ZFSOPEN));
  419. }
  420. }
  421. /*
  422. * Whether we're destroying a BE or a single snapshot, we need to walk
  423. * the tree of what we're going to destroy and promote everything in our
  424. * path so that we can make it happen.
  425. */
  426. if ((err = be_promote_dependent_clones(fs, &bdd)) != 0) {
  427. free(bdd.snapname);
  428. /*
  429. * If we're just destroying the origin of some other dataset
  430. * we were invoked to destroy, then we just ignore
  431. * BE_ERR_HASCLONES and return success unless the caller wanted
  432. * to force the issue.
  433. */
  434. if (odestroyer && err == BE_ERR_HASCLONES &&
  435. (options & BE_DESTROY_AUTOORIGIN) != 0)
  436. return (0);
  437. return (set_error(lbh, err));
  438. }
  439. /*
  440. * This was deferred until after we promote all of the derivatives so
  441. * that we grab the new origin after everything's settled down.
  442. */
  443. if ((options & BE_DESTROY_WANTORIGIN) != 0 &&
  444. zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin),
  445. NULL, NULL, 0, 1) != 0 &&
  446. (options & BE_DESTROY_ORIGIN) != 0)
  447. return (set_error(lbh, BE_ERR_NOORIGIN));
  448. /*
  449. * If the caller wants auto-origin destruction and the origin
  450. * name matches one of our automatically created snapshot names
  451. * (i.e. strftime("%F-%T") with a serial at the end), then
  452. * we'll set the DESTROY_ORIGIN flag and nuke it
  453. * be_is_auto_snapshot_name is exported from libbe(3) so that
  454. * the caller can determine if it needs to warn about the origin
  455. * not being destroyed or not.
  456. */
  457. if ((options & BE_DESTROY_AUTOORIGIN) != 0 && *origin != '\0' &&
  458. be_is_auto_snapshot_name(lbh, origin))
  459. options |= BE_DESTROY_ORIGIN;
  460. err = be_destroy_cb(fs, &bdd);
  461. zfs_close(fs);
  462. free(bdd.snapname);
  463. if (err != 0) {
  464. /* Children are still present or the mount is referenced */
  465. if (err == EBUSY)
  466. return (set_error(lbh, BE_ERR_DESTROYMNT));
  467. return (set_error(lbh, BE_ERR_UNKNOWN));
  468. }
  469. if ((options & BE_DESTROY_ORIGIN) == 0)
  470. return (0);
  471. /* The origin can't possibly be shorter than the BE root */
  472. rootlen = strlen(lbh->root);
  473. if (*origin == '\0' || strlen(origin) <= rootlen + 1)
  474. return (set_error(lbh, BE_ERR_INVORIGIN));
  475. /*
  476. * We'll be chopping off the BE root and running this back through
  477. * be_destroy, so that we properly handle the origin snapshot whether
  478. * it be that of a deep BE or not.
  479. */
  480. if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/')
  481. return (0);
  482. return (be_destroy_internal(lbh, origin + rootlen + 1,
  483. options & ~BE_DESTROY_ORIGIN, true));
  484. }
  485. int
  486. be_destroy(libbe_handle_t *lbh, const char *name, int options)
  487. {
  488. /*
  489. * The consumer must not set both BE_DESTROY_AUTOORIGIN and
  490. * BE_DESTROY_ORIGIN. Internally, we'll set the latter from the former.
  491. * The latter should imply that we must succeed at destroying the
  492. * origin, or complain otherwise.
  493. */
  494. if ((options & BE_DESTROY_WANTORIGIN) == BE_DESTROY_WANTORIGIN)
  495. return (set_error(lbh, BE_ERR_UNKNOWN));
  496. return (be_destroy_internal(lbh, name, options, false));
  497. }
  498. static void
  499. be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen)
  500. {
  501. time_t rawtime;
  502. int len, serial;
  503. time(&rawtime);
  504. len = strlen(buf);
  505. len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime));
  506. /* No room for serial... caller will do its best */
  507. if (buflen - len < 2)
  508. return;
  509. for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) {
  510. snprintf(buf + len, buflen - len, "-%d", serial);
  511. if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT))
  512. return;
  513. }
  514. }
  515. bool
  516. be_is_auto_snapshot_name(libbe_handle_t *lbh __unused, const char *name)
  517. {
  518. const char *snap;
  519. int day, hour, minute, month, second, serial, year;
  520. if ((snap = strchr(name, '@')) == NULL)
  521. return (false);
  522. ++snap;
  523. /* We'll grab the individual components and do some light validation. */
  524. if (sscanf(snap, "%d-%d-%d-%d:%d:%d-%d", &year, &month, &day, &hour,
  525. &minute, &second, &serial) != 7)
  526. return (false);
  527. return (year >= 1970) && (month >= 1 && month <= 12) &&
  528. (day >= 1 && day <= 31) && (hour >= 0 && hour <= 23) &&
  529. (minute >= 0 && minute <= 59) && (second >= 0 && second <= 60) &&
  530. serial >= 0;
  531. }
  532. int
  533. be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
  534. bool recursive, char *result)
  535. {
  536. char buf[BE_MAXPATHLEN];
  537. int err;
  538. be_root_concat(lbh, source, buf);
  539. if ((err = be_exists(lbh, buf)) != 0)
  540. return (set_error(lbh, err));
  541. if (snap_name != NULL) {
  542. if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
  543. return (set_error(lbh, BE_ERR_INVALIDNAME));
  544. if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
  545. return (set_error(lbh, BE_ERR_INVALIDNAME));
  546. if (result != NULL)
  547. snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
  548. snap_name);
  549. } else {
  550. be_setup_snapshot_name(lbh, buf, sizeof(buf));
  551. if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
  552. sizeof(buf)) >= sizeof(buf))
  553. return (set_error(lbh, BE_ERR_INVALIDNAME));
  554. }
  555. if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
  556. switch (err) {
  557. case EZFS_INVALIDNAME:
  558. return (set_error(lbh, BE_ERR_INVALIDNAME));
  559. default:
  560. /*
  561. * The other errors that zfs_ioc_snapshot might return
  562. * shouldn't happen if we've set things up properly, so
  563. * we'll gloss over them and call it UNKNOWN as it will
  564. * require further triage.
  565. */
  566. if (errno == ENOTSUP)
  567. return (set_error(lbh, BE_ERR_NOPOOL));
  568. return (set_error(lbh, BE_ERR_UNKNOWN));
  569. }
  570. }
  571. return (BE_ERR_SUCCESS);
  572. }
  573. /*
  574. * Create the boot environment specified by the name parameter
  575. */
  576. int
  577. be_create(libbe_handle_t *lbh, const char *name)
  578. {
  579. int err;
  580. err = be_create_from_existing(lbh, name, be_active_path(lbh));
  581. return (set_error(lbh, err));
  582. }
  583. static int
  584. be_deep_clone_prop(int prop, void *cb)
  585. {
  586. int err;
  587. struct libbe_dccb *dccb;
  588. zprop_source_t src;
  589. char pval[BE_MAXPATHLEN];
  590. char source[BE_MAXPATHLEN];
  591. char *val;
  592. dccb = cb;
  593. /* Skip some properties we don't want to touch */
  594. if (prop == ZFS_PROP_CANMOUNT)
  595. return (ZPROP_CONT);
  596. /* Don't copy readonly properties */
  597. if (zfs_prop_readonly(prop))
  598. return (ZPROP_CONT);
  599. if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
  600. sizeof(pval), &src, (char *)&source, sizeof(source), false)))
  601. /* Just continue if we fail to read a property */
  602. return (ZPROP_CONT);
  603. /*
  604. * Only copy locally defined or received properties. This continues
  605. * to avoid temporary/default/local properties intentionally without
  606. * breaking received datasets.
  607. */
  608. if (src != ZPROP_SRC_LOCAL && src != ZPROP_SRC_RECEIVED)
  609. return (ZPROP_CONT);
  610. /* Augment mountpoint with altroot, if needed */
  611. val = pval;
  612. if (prop == ZFS_PROP_MOUNTPOINT)
  613. val = be_mountpoint_augmented(dccb->lbh, val);
  614. nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
  615. return (ZPROP_CONT);
  616. }
  617. /*
  618. * Return the corresponding boot environment path for a given
  619. * dataset path, the constructed path is placed in 'result'.
  620. *
  621. * example: say our new boot environment name is 'bootenv' and
  622. * the dataset path is 'zroot/ROOT/default/data/set'.
  623. *
  624. * result should produce: 'zroot/ROOT/bootenv/data/set'
  625. */
  626. static int
  627. be_get_path(struct libbe_deep_clone *ldc, const char *dspath, char *result, int result_size)
  628. {
  629. char *pos;
  630. char *child_dataset;
  631. /* match the root path for the boot environments */
  632. pos = strstr(dspath, ldc->lbh->root);
  633. /* no match, different pools? */
  634. if (pos == NULL)
  635. return (BE_ERR_BADPATH);
  636. /* root path of the new boot environment */
  637. snprintf(result, result_size, "%s/%s", ldc->lbh->root, ldc->bename);
  638. /* gets us to the parent dataset, the +1 consumes a trailing slash */
  639. pos += strlen(ldc->lbh->root) + 1;
  640. /* skip the parent dataset */
  641. if ((child_dataset = strchr(pos, '/')) != NULL)
  642. strlcat(result, child_dataset, result_size);
  643. return (BE_ERR_SUCCESS);
  644. }
  645. static int
  646. be_clone_cb(zfs_handle_t *ds, void *data)
  647. {
  648. int err;
  649. char be_path[BE_MAXPATHLEN];
  650. char snap_path[BE_MAXPATHLEN];
  651. const char *dspath;
  652. zfs_handle_t *snap_hdl;
  653. nvlist_t *props;
  654. struct libbe_deep_clone *ldc;
  655. struct libbe_dccb dccb;
  656. ldc = (struct libbe_deep_clone *)data;
  657. dspath = zfs_get_name(ds);
  658. snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, ldc->snapname);
  659. /* construct the boot environment path from the dataset we're cloning */
  660. if (be_get_path(ldc, dspath, be_path, sizeof(be_path)) != BE_ERR_SUCCESS)
  661. return (BE_ERR_UNKNOWN);
  662. /* the dataset to be created (i.e. the boot environment) already exists */
  663. if (zfs_dataset_exists(ldc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
  664. return (BE_ERR_EXISTS);
  665. /* no snapshot found for this dataset, silently skip it */
  666. if (!zfs_dataset_exists(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT))
  667. return (0);
  668. if ((snap_hdl =
  669. zfs_open(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
  670. return (BE_ERR_ZFSOPEN);
  671. nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
  672. nvlist_add_string(props, "canmount", "noauto");
  673. dccb.lbh = ldc->lbh;
  674. dccb.zhp = ds;
  675. dccb.props = props;
  676. if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
  677. ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
  678. return (-1);
  679. if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
  680. return (BE_ERR_ZFSCLONE);
  681. nvlist_free(props);
  682. zfs_close(snap_hdl);
  683. if (ldc->depth_limit == -1 || ldc->depth < ldc->depth_limit) {
  684. ldc->depth++;
  685. err = zfs_iter_filesystems(ds, be_clone_cb, ldc);
  686. ldc->depth--;
  687. }
  688. return (err);
  689. }
  690. /*
  691. * Create a boot environment with a given name from a given snapshot.
  692. * Snapshots can be in the format 'zroot/ROOT/default@snapshot' or
  693. * 'default@snapshot'. In the latter case, 'default@snapshot' will be prepended
  694. * with the root path that libbe was initailized with.
  695. */
  696. static int
  697. be_clone(libbe_handle_t *lbh, const char *bename, const char *snapshot, int depth)
  698. {
  699. int err;
  700. char snap_path[BE_MAXPATHLEN];
  701. char *parentname, *snapname;
  702. zfs_handle_t *parent_hdl;
  703. struct libbe_deep_clone ldc;
  704. /* ensure the boot environment name is valid */
  705. if ((err = be_validate_name(lbh, bename)) != 0)
  706. return (set_error(lbh, err));
  707. /*
  708. * prepend the boot environment root path if we're
  709. * given a partial snapshot name.
  710. */
  711. if ((err = be_root_concat(lbh, snapshot, snap_path)) != 0)
  712. return (set_error(lbh, err));
  713. /* ensure the snapshot exists */
  714. if ((err = be_validate_snap(lbh, snap_path)) != 0)
  715. return (set_error(lbh, err));
  716. /* get a copy of the snapshot path so we can disect it */
  717. if ((parentname = strdup(snap_path)) == NULL)
  718. return (set_error(lbh, BE_ERR_UNKNOWN));
  719. /* split dataset name from snapshot name */
  720. snapname = strchr(parentname, '@');
  721. if (snapname == NULL) {
  722. free(parentname);
  723. return (set_error(lbh, BE_ERR_UNKNOWN));
  724. }
  725. *snapname = '\0';
  726. snapname++;
  727. /* set-up the boot environment */
  728. ldc.lbh = lbh;
  729. ldc.bename = bename;
  730. ldc.snapname = snapname;
  731. ldc.depth = 0;
  732. ldc.depth_limit = depth;
  733. /* the boot environment will be cloned from this dataset */
  734. parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
  735. /* create the boot environment */
  736. err = be_clone_cb(parent_hdl, &ldc);
  737. free(parentname);
  738. return (set_error(lbh, err));
  739. }
  740. /*
  741. * Create a boot environment from pre-existing snapshot, specifying a depth.
  742. */
  743. int be_create_depth(libbe_handle_t *lbh, const char *bename,
  744. const char *snap, int depth)
  745. {
  746. return (be_clone(lbh, bename, snap, depth));
  747. }
  748. /*
  749. * Create the boot environment from pre-existing snapshot
  750. */
  751. int
  752. be_create_from_existing_snap(libbe_handle_t *lbh, const char *bename,
  753. const char *snap)
  754. {
  755. return (be_clone(lbh, bename, snap, -1));
  756. }
  757. /*
  758. * Create a boot environment from an existing boot environment
  759. */
  760. int
  761. be_create_from_existing(libbe_handle_t *lbh, const char *bename, const char *old)
  762. {
  763. int err;
  764. char snap[BE_MAXPATHLEN];
  765. if ((err = be_snapshot(lbh, old, NULL, true, snap)) != 0)
  766. return (set_error(lbh, err));
  767. err = be_clone(lbh, bename, snap, -1);
  768. return (set_error(lbh, err));
  769. }
  770. /*
  771. * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
  772. * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
  773. * failure. Does not set the internal library error state.
  774. */
  775. int
  776. be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
  777. {
  778. if (strlen(snap_name) >= BE_MAXPATHLEN)
  779. return (BE_ERR_PATHLEN);
  780. if (!zfs_name_valid(snap_name, ZFS_TYPE_SNAPSHOT))
  781. return (BE_ERR_INVALIDNAME);
  782. if (!zfs_dataset_exists(lbh->lzh, snap_name,
  783. ZFS_TYPE_SNAPSHOT))
  784. return (BE_ERR_NOENT);
  785. return (BE_ERR_SUCCESS);
  786. }
  787. /*
  788. * Idempotently appends the name argument to the root boot environment path
  789. * and copies the resulting string into the result buffer (which is assumed
  790. * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
  791. * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
  792. * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
  793. * zfs_be_root. Does not set internal library error state.
  794. */
  795. int
  796. be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
  797. {
  798. size_t name_len, root_len;
  799. name_len = strlen(name);
  800. root_len = strlen(lbh->root);
  801. /* Act idempotently; return be name if it is already a full path */
  802. if (strrchr(name, '/') != NULL) {
  803. if (strstr(name, lbh->root) != name)
  804. return (BE_ERR_INVALIDNAME);
  805. if (name_len >= BE_MAXPATHLEN)
  806. return (BE_ERR_PATHLEN);
  807. strlcpy(result, name, BE_MAXPATHLEN);
  808. return (BE_ERR_SUCCESS);
  809. } else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
  810. snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
  811. name);
  812. return (BE_ERR_SUCCESS);
  813. }
  814. return (BE_ERR_PATHLEN);
  815. }
  816. /*
  817. * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
  818. * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
  819. * or BE_ERR_PATHLEN.
  820. * Does not set internal library error state.
  821. */
  822. int
  823. be_validate_name(libbe_handle_t *lbh, const char *name)
  824. {
  825. /*
  826. * Impose the additional restriction that the entire dataset name must
  827. * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
  828. */
  829. if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
  830. return (BE_ERR_PATHLEN);
  831. if (!zfs_name_valid(name, ZFS_TYPE_DATASET))
  832. return (BE_ERR_INVALIDNAME);
  833. /*
  834. * ZFS allows spaces in boot environment names, but the kernel can't
  835. * handle booting from such a dataset right now. vfs.root.mountfrom
  836. * is defined to be a space-separated list, and there's no protocol for
  837. * escaping whitespace in the path component of a dev:path spec. So
  838. * while loader can handle this situation alright, it can't safely pass
  839. * it on to mountroot.
  840. */
  841. if (strchr(name, ' ') != NULL)
  842. return (BE_ERR_INVALIDNAME);
  843. return (BE_ERR_SUCCESS);
  844. }
  845. /*
  846. * usage
  847. */
  848. int
  849. be_rename(libbe_handle_t *lbh, const char *old, const char *new)
  850. {
  851. char full_old[BE_MAXPATHLEN];
  852. char full_new[BE_MAXPATHLEN];
  853. zfs_handle_t *zfs_hdl;
  854. int err;
  855. /*
  856. * be_validate_name is documented not to set error state, so we should
  857. * do so here.
  858. */
  859. if ((err = be_validate_name(lbh, new)) != 0)
  860. return (set_error(lbh, err));
  861. if ((err = be_root_concat(lbh, old, full_old)) != 0)
  862. return (set_error(lbh, err));
  863. if ((err = be_root_concat(lbh, new, full_new)) != 0)
  864. return (set_error(lbh, err));
  865. if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
  866. return (set_error(lbh, BE_ERR_NOENT));
  867. if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
  868. return (set_error(lbh, BE_ERR_EXISTS));
  869. if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
  870. ZFS_TYPE_FILESYSTEM)) == NULL)
  871. return (set_error(lbh, BE_ERR_ZFSOPEN));
  872. /* recurse, nounmount, forceunmount */
  873. struct renameflags flags = {
  874. .nounmount = 1,
  875. };
  876. err = zfs_rename(zfs_hdl, full_new, flags);
  877. if (err != 0)
  878. goto error;
  879. /* handle renaming bootonce */
  880. if (lbh->bootonce != NULL &&
  881. strcmp(full_old, lbh->bootonce) == 0)
  882. err = be_activate(lbh, new, true);
  883. error:
  884. zfs_close(zfs_hdl);
  885. return (set_error(lbh, err));
  886. }
  887. int
  888. be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
  889. {
  890. char snap_name[BE_MAXPATHLEN];
  891. char buf[BE_MAXPATHLEN];
  892. zfs_handle_t *zfs;
  893. sendflags_t flags = { 0 };
  894. int err;
  895. if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
  896. /* Use the error set by be_snapshot */
  897. return (err);
  898. be_root_concat(lbh, snap_name, buf);
  899. if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
  900. return (set_error(lbh, BE_ERR_ZFSOPEN));
  901. err = zfs_send_one(zfs, NULL, fd, &flags, /* redactbook */ NULL);
  902. zfs_close(zfs);
  903. return (err);
  904. }
  905. int
  906. be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
  907. {
  908. char buf[BE_MAXPATHLEN];
  909. nvlist_t *props;
  910. zfs_handle_t *zfs;
  911. recvflags_t flags = { .nomount = 1 };
  912. int err;
  913. be_root_concat(lbh, bootenv, buf);
  914. if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) {
  915. switch (err) {
  916. case EINVAL:
  917. return (set_error(lbh, BE_ERR_NOORIGIN));
  918. case ENOENT:
  919. return (set_error(lbh, BE_ERR_NOENT));
  920. case EIO:
  921. return (set_error(lbh, BE_ERR_IO));
  922. default:
  923. return (set_error(lbh, BE_ERR_UNKNOWN));
  924. }
  925. }
  926. if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL)
  927. return (set_error(lbh, BE_ERR_ZFSOPEN));
  928. nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
  929. nvlist_add_string(props, "canmount", "noauto");
  930. nvlist_add_string(props, "mountpoint", "none");
  931. err = zfs_prop_set_list(zfs, props);
  932. nvlist_free(props);
  933. zfs_close(zfs);
  934. if (err != 0)
  935. return (set_error(lbh, BE_ERR_UNKNOWN));
  936. return (0);
  937. }
  938. #if SOON
  939. static int
  940. be_create_child_noent(libbe_handle_t *lbh, const char *active,
  941. const char *child_path)
  942. {
  943. nvlist_t *props;
  944. zfs_handle_t *zfs;
  945. int err;
  946. nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
  947. nvlist_add_string(props, "canmount", "noauto");
  948. nvlist_add_string(props, "mountpoint", child_path);
  949. /* Create */
  950. if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
  951. props)) != 0) {
  952. switch (err) {
  953. case EZFS_EXISTS:
  954. return (set_error(lbh, BE_ERR_EXISTS));
  955. case EZFS_NOENT:
  956. return (set_error(lbh, BE_ERR_NOENT));
  957. case EZFS_BADTYPE:
  958. case EZFS_BADVERSION:
  959. return (set_error(lbh, BE_ERR_NOPOOL));
  960. case EZFS_BADPROP:
  961. default:
  962. /* We set something up wrong, probably... */
  963. return (set_error(lbh, BE_ERR_UNKNOWN));
  964. }
  965. }
  966. nvlist_free(props);
  967. if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
  968. return (set_error(lbh, BE_ERR_ZFSOPEN));
  969. /* Set props */
  970. if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
  971. zfs_close(zfs);
  972. /*
  973. * Similar to other cases, this shouldn't fail unless we've
  974. * done something wrong. This is a new dataset that shouldn't
  975. * have been mounted anywhere between creation and now.
  976. */
  977. if (err == EZFS_NOMEM)
  978. return (set_error(lbh, BE_ERR_NOMEM));
  979. return (set_error(lbh, BE_ERR_UNKNOWN));
  980. }
  981. zfs_close(zfs);
  982. return (BE_ERR_SUCCESS);
  983. }
  984. static int
  985. be_create_child_cloned(libbe_handle_t *lbh, const char *active)
  986. {
  987. char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];
  988. zfs_handle_t *zfs;
  989. int err;
  990. /* XXX TODO ? */
  991. /*
  992. * Establish if the existing path is a zfs dataset or just
  993. * the subdirectory of one
  994. */
  995. strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
  996. if (mktemp(tmp) == NULL)
  997. return (set_error(lbh, BE_ERR_UNKNOWN));
  998. be_root_concat(lbh, tmp, buf);
  999. printf("Here %s?\n", buf);
  1000. if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
  1001. switch (err) {
  1002. case EZFS_INVALIDNAME:
  1003. return (set_error(lbh, BE_ERR_INVALIDNAME));
  1004. default:
  1005. /*
  1006. * The other errors that zfs_ioc_snapshot might return
  1007. * shouldn't happen if we've set things up properly, so
  1008. * we'll gloss over them and call it UNKNOWN as it will
  1009. * require further triage.
  1010. */
  1011. if (errno == ENOTSUP)
  1012. return (set_error(lbh, BE_ERR_NOPOOL));
  1013. return (set_error(lbh, BE_ERR_UNKNOWN));
  1014. }
  1015. }
  1016. /* Clone */
  1017. if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
  1018. return (BE_ERR_ZFSOPEN);
  1019. if ((err = zfs_clone(zfs, active, NULL)) != 0)
  1020. /* XXX TODO correct error */
  1021. return (set_error(lbh, BE_ERR_UNKNOWN));
  1022. /* set props */
  1023. zfs_close(zfs);
  1024. return (BE_ERR_SUCCESS);
  1025. }
  1026. int
  1027. be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
  1028. {
  1029. struct stat sb;
  1030. char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
  1031. nvlist_t *props;
  1032. const char *s;
  1033. /* Require absolute paths */
  1034. if (*child_path != '/')
  1035. return (set_error(lbh, BE_ERR_BADPATH));
  1036. strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
  1037. strcpy(buf, active);
  1038. /* Create non-mountable parent dataset(s) */
  1039. s = child_path;
  1040. for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
  1041. size_t len = p - s;
  1042. strncat(buf, s, len);
  1043. nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
  1044. nvlist_add_string(props, "canmount", "off");
  1045. nvlist_add_string(props, "mountpoint", "none");
  1046. zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
  1047. nvlist_free(props);
  1048. }
  1049. /* Path does not exist as a descendent of / yet */
  1050. if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
  1051. return (set_error(lbh, BE_ERR_PATHLEN));
  1052. if (stat(child_path, &sb) != 0) {
  1053. /* Verify that error is ENOENT */
  1054. if (errno != ENOENT)
  1055. return (set_error(lbh, BE_ERR_UNKNOWN));
  1056. return (be_create_child_noent(lbh, active, child_path));
  1057. } else if (cp_if_exists)
  1058. /* Path is already a descendent of / and should be copied */
  1059. return (be_create_child_cloned(lbh, active));
  1060. return (set_error(lbh, BE_ERR_EXISTS));
  1061. }
  1062. #endif /* SOON */
  1063. /*
  1064. * Deactivate old BE dataset; currently just sets canmount=noauto or
  1065. * resets boot once configuration.
  1066. */
  1067. int
  1068. be_deactivate(libbe_handle_t *lbh, const char *ds, bool temporary)
  1069. {
  1070. zfs_handle_t *zfs;
  1071. if (temporary) {
  1072. return (lzbe_set_boot_device(
  1073. zpool_get_name(lbh->active_phandle), lzbe_add, NULL));
  1074. }
  1075. if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL)
  1076. return (1);
  1077. if (zfs_prop_set(zfs, "canmount", "noauto") != 0)
  1078. return (1);
  1079. zfs_close(zfs);
  1080. return (0);
  1081. }
  1082. static int
  1083. be_zfs_promote_cb(zfs_handle_t *zhp, void *data)
  1084. {
  1085. char origin[BE_MAXPATHLEN];
  1086. bool *found_origin = (bool *)data;
  1087. int err;
  1088. if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof(origin),
  1089. NULL, NULL, 0, true) == 0) {
  1090. *found_origin = true;
  1091. err = zfs_promote(zhp);
  1092. if (err)
  1093. return (err);
  1094. }
  1095. return (zfs_iter_filesystems(zhp, be_zfs_promote_cb, data));
  1096. }
  1097. static int
  1098. be_zfs_promote(zfs_handle_t *zhp, bool *found_origin)
  1099. {
  1100. *found_origin = false;
  1101. return (be_zfs_promote_cb(zhp, (void *)found_origin));
  1102. }
  1103. int
  1104. be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
  1105. {
  1106. char be_path[BE_MAXPATHLEN];
  1107. zfs_handle_t *zhp;
  1108. int err;
  1109. bool found_origin;
  1110. be_root_concat(lbh, bootenv, be_path);
  1111. /* Note: be_exists fails if mountpoint is not / */
  1112. if ((err = be_exists(lbh, be_path)) != 0)
  1113. return (set_error(lbh, err));
  1114. if (temporary) {
  1115. return (lzbe_set_boot_device(
  1116. zpool_get_name(lbh->active_phandle), lzbe_add, be_path));
  1117. } else {
  1118. if (strncmp(lbh->bootfs, "-", 1) != 0 &&
  1119. be_deactivate(lbh, lbh->bootfs, false) != 0)
  1120. return (-1);
  1121. /* Obtain bootenv zpool */
  1122. err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
  1123. if (err)
  1124. return (-1);
  1125. for (;;) {
  1126. zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
  1127. if (zhp == NULL)
  1128. return (-1);
  1129. err = be_zfs_promote(zhp, &found_origin);
  1130. zfs_close(zhp);
  1131. if (!found_origin)
  1132. break;
  1133. if (err)
  1134. return (err);
  1135. }
  1136. if (err)
  1137. return (-1);
  1138. }
  1139. return (BE_ERR_SUCCESS);
  1140. }