1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Syscall to write out the instance number data structures to
* stable storage.
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/t_lock.h>
#include <sys/modctl.h>
#include <sys/systm.h>
#include <sys/syscall.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/cred.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/kmem.h>
#include <sys/cladm.h>
#include <sys/sunddi.h>
#include <sys/dditypes.h>
#include <sys/instance.h>
#include <sys/debug.h>
#include <sys/policy.h>
/*
* Userland sees:
*
* int inst_sync(pathname, flags);
*
* Returns zero if instance number information was successfully
* written to 'pathname', -1 plus error code in errno otherwise.
*
* POC notes:
*
* - This could be done as a case of the modctl(2) system call
* though the ability to have it load and unload would disappear.
*
* - 'flags' have either of two meanings:
* INST_SYNC_IF_REQUIRED 'pathname' will be written if there
* has been a change in the kernel's
* internal view of instance number
* information
* INST_SYNC_ALWAYS 'pathname' will be written even if
* the kernel's view hasn't changed.
*
* - Maybe we should pass through two filenames - one to create,
* and the other as the 'final' target i.e. do the rename of
* /etc/instance.new -> /etc/instance in the kernel.
*/
static int in_sync_sys(char *pathname, uint_t flags);
static struct sysent in_sync_sysent = {
2, /* number of arguments */
SE_ARGC | SE_32RVAL1, /* c-style calling, 32-bit return value */
in_sync_sys, /* the handler */
(krwlock_t *)0 /* rw lock allocated/used by framework */
};
static struct modlsys modlsys = {
&mod_syscallops, "instance binding syscall", &in_sync_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32, "32-bit instance binding syscall", &in_sync_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
static int in_write_instance(struct vnode *vp);
static int inst_sync_disable = 0;
static int
in_sync_sys(char *pathname, uint_t flags)
{
struct vnode *vp;
int error;
/* For debugging/testing */
if (inst_sync_disable)
return (0);
/*
* We must have sufficient privilege to do this, since we lock critical
* data structures whilst we're doing it ..
*/
if ((error = secpolicy_sys_devices(CRED())) != 0)
return (set_errno(error));
if (flags != INST_SYNC_ALWAYS && flags != INST_SYNC_IF_REQUIRED)
return (set_errno(EINVAL));
/*
* Only one process is allowed to get the state of the instance
* number assignments on the system at any given time.
*/
e_ddi_enter_instance();
/*
* Recreate the instance file only if the device tree has changed
* or if the caller explicitly requests so.
*/
if (e_ddi_instance_is_clean() && flags != INST_SYNC_ALWAYS) {
error = EALREADY;
goto end;
}
/*
* Create an instance file for writing, giving it a mode that
* will only permit reading. Note that we refuse to overwrite
* an existing file.
*/
if ((error = vn_open(pathname, UIO_USERSPACE,
FCREAT, 0444, &vp, CRCREAT, 0)) != 0) {
if (error == EISDIR)
error = EACCES; /* SVID compliance? */
goto end;
}
/*
* So far so good. We're singly threaded, the vnode is beckoning
* so let's get on with it. Any error, and we just give up and
* hand the first error we get back to userland.
*/
error = in_write_instance(vp);
/*
* If there was any sort of error, we deliberately go and
* remove the file we just created so that any attempts to
* use it will quickly fail.
*/
if (error)
(void) vn_remove(pathname, UIO_USERSPACE, RMFILE);
else
e_ddi_instance_set_clean();
end:
e_ddi_exit_instance();
return (error ? set_errno(error) : 0);
}
/*
* At the risk of reinventing stdio ..
*/
#define FBUFSIZE 512
typedef struct _File {
char *ptr;
int count;
char buf[FBUFSIZE];
vnode_t *vp;
offset_t voffset;
} File;
static int
in_write(struct vnode *vp, offset_t *vo, caddr_t buf, int count)
{
int error;
ssize_t resid;
rlim64_t rlimit = *vo + count + 1;
error = vn_rdwr(UIO_WRITE, vp, buf, count, *vo,
UIO_SYSSPACE, 0, rlimit, CRED(), &resid);
*vo += (offset_t)(count - resid);
return (error);
}
static File *
in_fvpopen(struct vnode *vp)
{
File *fp;
fp = kmem_zalloc(sizeof (File), KM_SLEEP);
fp->vp = vp;
fp->ptr = fp->buf;
return (fp);
}
static int
in_fclose(File *fp)
{
int error;
error = VOP_CLOSE(fp->vp, FCREAT, 1, (offset_t)0, CRED(), NULL);
VN_RELE(fp->vp);
kmem_free(fp, sizeof (File));
return (error);
}
static int
in_fflush(File *fp)
{
int error = 0;
if (fp->count)
error = in_write(fp->vp, &fp->voffset, fp->buf, fp->count);
if (error == 0)
error = VOP_FSYNC(fp->vp, FSYNC, CRED(), NULL);
return (error);
}
static int
in_fputs(File *fp, char *buf)
{
int error = 0;
while (*buf) {
*fp->ptr++ = *buf++;
if (++fp->count == FBUFSIZE) {
error = in_write(fp->vp, &fp->voffset, fp->buf,
fp->count);
if (error)
break;
fp->count = 0;
fp->ptr = fp->buf;
}
}
return (error);
}
/*
* External linkage
*/
static File *in_fp;
/*
* XXX what is the maximum length of the name of a driver? Must be maximum
* XXX file name length (find the correct constant and substitute for this one
*/
#define DRVNAMELEN (1 + 256)
static char linebuffer[MAXPATHLEN + 1 + 1 + 1 + 1 + 10 + 1 + DRVNAMELEN];
/*
* XXX Maybe we should just write 'in_fprintf' instead ..
*/
static int
in_walktree(in_node_t *np, char *this)
{
char *next;
int error = 0;
in_drv_t *dp;
for (error = 0; np; np = np->in_sibling) {
if (np->in_drivers == NULL)
continue;
if (np->in_unit_addr[0] == '\0')
(void) sprintf(this, "/%s", np->in_node_name);
else
(void) sprintf(this, "/%s@%s", np->in_node_name,
np->in_unit_addr);
next = this + strlen(this);
ASSERT(np->in_drivers);
for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
uint_t inst_val = dp->ind_instance;
/*
* Flushing IN_PROVISIONAL could result in duplicate
* instances
* Flushing IN_UNKNOWN results in instance -1
*/
if (dp->ind_state != IN_PERMANENT)
continue;
(void) sprintf(next, "\" %d \"%s\"\n", inst_val,
dp->ind_driver_name);
if (error = in_fputs(in_fp, linebuffer))
return (error);
}
if (np->in_child)
if (error = in_walktree(np->in_child, next))
break;
}
return (error);
}
/*
* Walk the instance tree, writing out what we find.
*
* There's some fairly nasty sharing of buffers in this
* bit of code, so be careful out there when you're
* rewriting it ..
*/
static int
in_write_instance(struct vnode *vp)
{
int error;
char *cp;
in_fp = in_fvpopen(vp);
/*
* Place a bossy comment at the beginning of the file.
*/
error = in_fputs(in_fp,
"#\n#\tCaution! This file contains critical kernel state\n#\n");
if (error == 0) {
in_node_t *root = e_ddi_instance_root();
cp = linebuffer;
*cp++ = '\"';
error = in_walktree(root->in_child, cp);
}
if (error == 0) {
if ((error = in_fflush(in_fp)) == 0)
error = in_fclose(in_fp);
} else
(void) in_fclose(in_fp);
return (error);
}
|