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]
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Syscall to write out the instance number data structures to
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>
37 #include <sys/vnode.h>
40 #include <sys/cmn_err.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>
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.
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
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
93 static struct modlinkage modlinkage
= {
96 #ifdef _SYSCALL32_IMPL
105 return (mod_install(&modlinkage
));
109 _info(struct modinfo
*modinfop
)
111 return (mod_info(&modlinkage
, modinfop
));
117 return (mod_remove(&modlinkage
));
120 static int in_write_instance(struct vnode
*vp
);
122 static int inst_sync_disable
= 0;
125 in_sync_sys(char *pathname
, uint_t flags
)
130 /* For debugging/testing */
131 if (inst_sync_disable
)
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
) {
160 * Create an instance file for writing, giving it a mode that
161 * will only permit reading. Note that we refuse to overwrite
164 if ((error
= vn_open(pathname
, UIO_USERSPACE
,
165 FCREAT
, 0444, &vp
, CRCREAT
, 0)) != 0) {
167 error
= EACCES
; /* SVID compliance? */
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.
184 (void) vn_remove(pathname
, UIO_USERSPACE
, RMFILE
);
186 e_ddi_instance_set_clean();
188 e_ddi_exit_instance();
189 return (error
? set_errno(error
) : 0);
193 * At the risk of reinventing stdio ..
197 typedef struct _File
{
206 in_write(struct vnode
*vp
, offset_t
*vo
, caddr_t buf
, int count
)
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
);
221 in_fvpopen(struct vnode
*vp
)
225 fp
= kmem_zalloc(sizeof (File
), KM_SLEEP
);
237 error
= fop_close(fp
->vp
, FCREAT
, 1, 0, CRED(), NULL
);
239 kmem_free(fp
, sizeof (File
));
249 error
= in_write(fp
->vp
, &fp
->voffset
, fp
->buf
, fp
->count
);
251 error
= fop_fsync(fp
->vp
, FSYNC
, CRED(), NULL
);
256 in_fputs(File
*fp
, char *buf
)
262 if (++fp
->count
== FBUFSIZE
) {
263 error
= in_write(fp
->vp
, &fp
->voffset
, fp
->buf
,
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 ..
291 in_walktree(in_node_t
*np
, char *this)
297 for (error
= 0; np
; np
= np
->in_sibling
) {
299 if (np
->in_drivers
== NULL
)
302 if (np
->in_unit_addr
[0] == '\0')
303 (void) sprintf(this, "/%s", np
->in_node_name
);
305 (void) sprintf(this, "/%s@%s", np
->in_node_name
,
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
317 * Flushing IN_UNKNOWN results in instance -1
319 if (dp
->ind_state
!= IN_PERMANENT
)
322 (void) sprintf(next
, "\" %d \"%s\"\n", inst_val
,
323 dp
->ind_driver_name
);
324 if (error
= in_fputs(in_fp
, linebuffer
))
329 if (error
= in_walktree(np
->in_child
, next
))
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
344 in_write_instance(struct vnode
*vp
)
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");
358 in_node_t
*root
= e_ddi_instance_root();
361 error
= in_walktree(root
->in_child
, cp
);
365 if ((error
= in_fflush(in_fp
)) == 0)
366 error
= in_fclose(in_fp
);
368 (void) in_fclose(in_fp
);