include: gcc 7's cpp has problems with the line continuations in .x files
[unleashed.git] / kernel / os / inst_sync.c
blobad95227fd82f4d06b06feacb8906e15b69b8db21
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Syscall to write out the instance number data structures to
27 * stable storage.
30 #include <sys/types.h>
31 #include <sys/errno.h>
32 #include <sys/t_lock.h>
33 #include <sys/modctl.h>
34 #include <sys/systm.h>
35 #include <sys/syscall.h>
36 #include <sys/vfs.h>
37 #include <sys/vnode.h>
38 #include <sys/cred.h>
39 #include <sys/file.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kmem.h>
42 #include <sys/sunddi.h>
43 #include <sys/dditypes.h>
44 #include <sys/instance.h>
45 #include <sys/debug.h>
46 #include <sys/policy.h>
49 * Userland sees:
51 * int inst_sync(pathname, flags);
53 * Returns zero if instance number information was successfully
54 * written to 'pathname', -1 plus error code in errno otherwise.
56 * POC notes:
58 * - This could be done as a case of the modctl(2) system call
59 * though the ability to have it load and unload would disappear.
61 * - 'flags' have either of two meanings:
62 * INST_SYNC_IF_REQUIRED 'pathname' will be written if there
63 * has been a change in the kernel's
64 * internal view of instance number
65 * information
66 * INST_SYNC_ALWAYS 'pathname' will be written even if
67 * the kernel's view hasn't changed.
69 * - Maybe we should pass through two filenames - one to create,
70 * and the other as the 'final' target i.e. do the rename of
71 * /etc/instance.new -> /etc/instance in the kernel.
74 static int in_sync_sys(char *pathname, uint_t flags);
76 static struct sysent in_sync_sysent = {
77 2, /* number of arguments */
78 SE_ARGC | SE_32RVAL1, /* c-style calling, 32-bit return value */
79 in_sync_sys, /* the handler */
80 (krwlock_t *)0 /* rw lock allocated/used by framework */
83 static struct modlsys modlsys = {
84 &mod_syscallops, "instance binding syscall", &in_sync_sysent
87 #ifdef _SYSCALL32_IMPL
88 static struct modlsys modlsys32 = {
89 &mod_syscallops32, "32-bit instance binding syscall", &in_sync_sysent
91 #endif
93 static struct modlinkage modlinkage = {
94 MODREV_1,
95 &modlsys,
96 #ifdef _SYSCALL32_IMPL
97 &modlsys32,
98 #endif
99 NULL
103 _init(void)
105 return (mod_install(&modlinkage));
109 _info(struct modinfo *modinfop)
111 return (mod_info(&modlinkage, modinfop));
115 _fini(void)
117 return (mod_remove(&modlinkage));
120 static int in_write_instance(struct vnode *vp);
122 static int inst_sync_disable = 0;
124 static int
125 in_sync_sys(char *pathname, uint_t flags)
127 struct vnode *vp;
128 int error;
130 /* For debugging/testing */
131 if (inst_sync_disable)
132 return (0);
135 * We must have sufficient privilege to do this, since we lock critical
136 * data structures whilst we're doing it ..
138 if ((error = secpolicy_sys_devices(CRED())) != 0)
139 return (set_errno(error));
141 if (flags != INST_SYNC_ALWAYS && flags != INST_SYNC_IF_REQUIRED)
142 return (set_errno(EINVAL));
145 * Only one process is allowed to get the state of the instance
146 * number assignments on the system at any given time.
148 e_ddi_enter_instance();
151 * Recreate the instance file only if the device tree has changed
152 * or if the caller explicitly requests so.
154 if (e_ddi_instance_is_clean() && flags != INST_SYNC_ALWAYS) {
155 error = EALREADY;
156 goto end;
160 * Create an instance file for writing, giving it a mode that
161 * will only permit reading. Note that we refuse to overwrite
162 * an existing file.
164 if ((error = vn_open(pathname, UIO_USERSPACE,
165 FCREAT, 0444, &vp, CRCREAT, 0)) != 0) {
166 if (error == EISDIR)
167 error = EACCES; /* SVID compliance? */
168 goto end;
172 * So far so good. We're singly threaded, the vnode is beckoning
173 * so let's get on with it. Any error, and we just give up and
174 * hand the first error we get back to userland.
176 error = in_write_instance(vp);
179 * If there was any sort of error, we deliberately go and
180 * remove the file we just created so that any attempts to
181 * use it will quickly fail.
183 if (error)
184 (void) vn_remove(pathname, UIO_USERSPACE, RMFILE);
185 else
186 e_ddi_instance_set_clean();
187 end:
188 e_ddi_exit_instance();
189 return (error ? set_errno(error) : 0);
193 * At the risk of reinventing stdio ..
195 #define FBUFSIZE 512
197 typedef struct _File {
198 char *ptr;
199 int count;
200 char buf[FBUFSIZE];
201 vnode_t *vp;
202 offset_t voffset;
203 } File;
205 static int
206 in_write(struct vnode *vp, offset_t *vo, caddr_t buf, int count)
208 int error;
209 ssize_t resid;
210 rlim64_t rlimit = *vo + count + 1;
212 error = vn_rdwr(UIO_WRITE, vp, buf, count, *vo,
213 UIO_SYSSPACE, 0, rlimit, CRED(), &resid);
215 *vo += (offset_t)(count - resid);
217 return (error);
220 static File *
221 in_fvpopen(struct vnode *vp)
223 File *fp;
225 fp = kmem_zalloc(sizeof (File), KM_SLEEP);
226 fp->vp = vp;
227 fp->ptr = fp->buf;
229 return (fp);
232 static int
233 in_fclose(File *fp)
235 int error;
237 error = fop_close(fp->vp, FCREAT, 1, 0, CRED(), NULL);
238 VN_RELE(fp->vp);
239 kmem_free(fp, sizeof (File));
240 return (error);
243 static int
244 in_fflush(File *fp)
246 int error = 0;
248 if (fp->count)
249 error = in_write(fp->vp, &fp->voffset, fp->buf, fp->count);
250 if (error == 0)
251 error = fop_fsync(fp->vp, FSYNC, CRED(), NULL);
252 return (error);
255 static int
256 in_fputs(File *fp, char *buf)
258 int error = 0;
260 while (*buf) {
261 *fp->ptr++ = *buf++;
262 if (++fp->count == FBUFSIZE) {
263 error = in_write(fp->vp, &fp->voffset, fp->buf,
264 fp->count);
265 if (error)
266 break;
267 fp->count = 0;
268 fp->ptr = fp->buf;
272 return (error);
276 * External linkage
278 static File *in_fp;
281 * XXX what is the maximum length of the name of a driver? Must be maximum
282 * XXX file name length (find the correct constant and substitute for this one
284 #define DRVNAMELEN (1 + 256)
285 static char linebuffer[MAXPATHLEN + 1 + 1 + 1 + 1 + 10 + 1 + DRVNAMELEN];
288 * XXX Maybe we should just write 'in_fprintf' instead ..
290 static int
291 in_walktree(in_node_t *np, char *this)
293 char *next;
294 int error = 0;
295 in_drv_t *dp;
297 for (error = 0; np; np = np->in_sibling) {
299 if (np->in_drivers == NULL)
300 continue;
302 if (np->in_unit_addr[0] == '\0')
303 (void) sprintf(this, "/%s", np->in_node_name);
304 else
305 (void) sprintf(this, "/%s@%s", np->in_node_name,
306 np->in_unit_addr);
307 next = this + strlen(this);
309 ASSERT(np->in_drivers);
311 for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
312 uint_t inst_val = dp->ind_instance;
315 * Flushing IN_PROVISIONAL could result in duplicate
316 * instances
317 * Flushing IN_UNKNOWN results in instance -1
319 if (dp->ind_state != IN_PERMANENT)
320 continue;
322 (void) sprintf(next, "\" %d \"%s\"\n", inst_val,
323 dp->ind_driver_name);
324 if (error = in_fputs(in_fp, linebuffer))
325 return (error);
328 if (np->in_child)
329 if (error = in_walktree(np->in_child, next))
330 break;
332 return (error);
337 * Walk the instance tree, writing out what we find.
339 * There's some fairly nasty sharing of buffers in this
340 * bit of code, so be careful out there when you're
341 * rewriting it ..
343 static int
344 in_write_instance(struct vnode *vp)
346 int error;
347 char *cp;
349 in_fp = in_fvpopen(vp);
352 * Place a bossy comment at the beginning of the file.
354 error = in_fputs(in_fp,
355 "#\n#\tCaution! This file contains critical kernel state\n#\n");
357 if (error == 0) {
358 in_node_t *root = e_ddi_instance_root();
359 cp = linebuffer;
360 *cp++ = '\"';
361 error = in_walktree(root->in_child, cp);
364 if (error == 0) {
365 if ((error = in_fflush(in_fp)) == 0)
366 error = in_fclose(in_fp);
367 } else
368 (void) in_fclose(in_fp);
370 return (error);