CI: Automate some GitHub PR status labels manipulations
[zfs.git] / module / os / freebsd / spl / spl_zone.c
blobe3896517bd611a7cede42f5b0d6031ea688061a9
1 /*
2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/proc.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/sx.h>
35 #include <sys/malloc.h>
36 #include <sys/queue.h>
37 #include <sys/jail.h>
38 #include <sys/osd.h>
39 #include <sys/priv.h>
40 #include <sys/zone.h>
42 #include <sys/policy.h>
44 static MALLOC_DEFINE(M_ZONES, "zones_data", "Zones data");
47 * Structure to record list of ZFS datasets exported to a zone.
49 typedef struct zone_dataset {
50 LIST_ENTRY(zone_dataset) zd_next;
51 char zd_dataset[0];
52 } zone_dataset_t;
54 LIST_HEAD(zone_dataset_head, zone_dataset);
56 static int zone_slot;
58 int
59 zone_dataset_attach(struct ucred *cred, const char *dataset, int jailid)
61 struct zone_dataset_head *head;
62 zone_dataset_t *zd, *zd2;
63 struct prison *pr;
64 int dofree, error;
66 if ((error = priv_check_cred(cred, PRIV_ZFS_JAIL)) != 0)
67 return (error);
69 /* Allocate memory before we grab prison's mutex. */
70 zd = malloc(sizeof (*zd) + strlen(dataset) + 1, M_ZONES, M_WAITOK);
72 sx_slock(&allprison_lock);
73 pr = prison_find(jailid); /* Locks &pr->pr_mtx. */
74 sx_sunlock(&allprison_lock);
75 if (pr == NULL) {
76 free(zd, M_ZONES);
77 return (ENOENT);
80 head = osd_jail_get(pr, zone_slot);
81 if (head != NULL) {
82 dofree = 0;
83 LIST_FOREACH(zd2, head, zd_next) {
84 if (strcmp(dataset, zd2->zd_dataset) == 0) {
85 free(zd, M_ZONES);
86 error = EEXIST;
87 goto end;
90 } else {
91 dofree = 1;
92 prison_hold_locked(pr);
93 mtx_unlock(&pr->pr_mtx);
94 head = malloc(sizeof (*head), M_ZONES, M_WAITOK);
95 LIST_INIT(head);
96 mtx_lock(&pr->pr_mtx);
97 error = osd_jail_set(pr, zone_slot, head);
98 KASSERT(error == 0, ("osd_jail_set() failed (error=%d)",
99 error));
101 strcpy(zd->zd_dataset, dataset);
102 LIST_INSERT_HEAD(head, zd, zd_next);
103 end:
104 if (dofree)
105 prison_free_locked(pr);
106 else
107 mtx_unlock(&pr->pr_mtx);
108 return (error);
112 zone_dataset_detach(struct ucred *cred, const char *dataset, int jailid)
114 struct zone_dataset_head *head;
115 zone_dataset_t *zd;
116 struct prison *pr;
117 int error;
119 if ((error = priv_check_cred(cred, PRIV_ZFS_JAIL)) != 0)
120 return (error);
122 sx_slock(&allprison_lock);
123 pr = prison_find(jailid);
124 sx_sunlock(&allprison_lock);
125 if (pr == NULL)
126 return (ENOENT);
127 head = osd_jail_get(pr, zone_slot);
128 if (head == NULL) {
129 error = ENOENT;
130 goto end;
132 LIST_FOREACH(zd, head, zd_next) {
133 if (strcmp(dataset, zd->zd_dataset) == 0)
134 break;
136 if (zd == NULL)
137 error = ENOENT;
138 else {
139 LIST_REMOVE(zd, zd_next);
140 free(zd, M_ZONES);
141 if (LIST_EMPTY(head))
142 osd_jail_del(pr, zone_slot);
143 error = 0;
145 end:
146 mtx_unlock(&pr->pr_mtx);
147 return (error);
151 * Returns true if the named dataset is visible in the current zone.
152 * The 'write' parameter is set to 1 if the dataset is also writable.
155 zone_dataset_visible(const char *dataset, int *write)
157 struct zone_dataset_head *head;
158 zone_dataset_t *zd;
159 struct prison *pr;
160 size_t len;
161 int ret = 0;
163 if (dataset[0] == '\0')
164 return (0);
165 if (INGLOBALZONE(curproc)) {
166 if (write != NULL)
167 *write = 1;
168 return (1);
170 pr = curthread->td_ucred->cr_prison;
171 mtx_lock(&pr->pr_mtx);
172 head = osd_jail_get(pr, zone_slot);
173 if (head == NULL)
174 goto end;
177 * Walk the list once, looking for datasets which match exactly, or
178 * specify a dataset underneath an exported dataset. If found, return
179 * true and note that it is writable.
181 LIST_FOREACH(zd, head, zd_next) {
182 len = strlen(zd->zd_dataset);
183 if (strlen(dataset) >= len &&
184 memcmp(dataset, zd->zd_dataset, len) == 0 &&
185 (dataset[len] == '\0' || dataset[len] == '/' ||
186 dataset[len] == '@')) {
187 if (write)
188 *write = 1;
189 ret = 1;
190 goto end;
195 * Walk the list a second time, searching for datasets which are parents
196 * of exported datasets. These should be visible, but read-only.
198 * Note that we also have to support forms such as 'pool/dataset/', with
199 * a trailing slash.
201 LIST_FOREACH(zd, head, zd_next) {
202 len = strlen(dataset);
203 if (dataset[len - 1] == '/')
204 len--; /* Ignore trailing slash */
205 if (len < strlen(zd->zd_dataset) &&
206 memcmp(dataset, zd->zd_dataset, len) == 0 &&
207 zd->zd_dataset[len] == '/') {
208 if (write)
209 *write = 0;
210 ret = 1;
211 goto end;
214 end:
215 mtx_unlock(&pr->pr_mtx);
216 return (ret);
219 static void
220 zone_destroy(void *arg)
222 struct zone_dataset_head *head;
223 zone_dataset_t *zd;
225 head = arg;
226 while ((zd = LIST_FIRST(head)) != NULL) {
227 LIST_REMOVE(zd, zd_next);
228 free(zd, M_ZONES);
230 free(head, M_ZONES);
233 uint32_t
234 zone_get_hostid(void *ptr)
237 KASSERT(ptr == NULL, ("only NULL pointer supported in %s", __func__));
239 return ((uint32_t)curthread->td_ucred->cr_prison->pr_hostid);
242 static void
243 zone_sysinit(void *arg __unused)
246 zone_slot = osd_jail_register(zone_destroy, NULL);
249 static void
250 zone_sysuninit(void *arg __unused)
253 osd_jail_deregister(zone_slot);
256 SYSINIT(zone_sysinit, SI_SUB_DRIVERS, SI_ORDER_ANY, zone_sysinit, NULL);
257 SYSUNINIT(zone_sysuninit, SI_SUB_DRIVERS, SI_ORDER_ANY, zone_sysuninit, NULL);