Repository navigation
Expand file tree
/
Copy pathlog.c
More file actions
507 lines (450 loc) · 14.3 KB
/
Copy pathlog.c
File metadata and controls
507 lines (450 loc) · 14.3 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/* This file contains a driver for:
* /dev/klog - system log device
*
* Changes:
* 21 July 2005 - Support for diagnostic messages (Jorrit N. Herder)
* 7 July 2005 - Created (Ben Gras)
*/
#include "log.h"
#include <sys/time.h>
#include <sys/select.h>
#include "../../kernel/const.h"
#include "../../kernel/type.h"
#define LOG_DEBUG 0 /* enable/ disable debugging */
#define NR_DEVS 1 /* number of minor devices */
#define MINOR_KLOG 0 /* /dev/klog */
#define LOGINC(n, i) do { (n) = (((n) + (i)) % LOG_SIZE); } while(0)
PUBLIC struct logdevice logdevices[NR_DEVS];
PRIVATE struct device log_geom[NR_DEVS]; /* base and size of devices */
PRIVATE int log_device = -1; /* current device */
FORWARD _PROTOTYPE( char *log_name, (void) );
FORWARD _PROTOTYPE( struct device *log_prepare, (int device) );
FORWARD _PROTOTYPE( int log_transfer, (int proc_nr, int opcode, u64_t position,
iovec_t *iov, unsigned nr_req, int safe) );
FORWARD _PROTOTYPE( int log_do_open, (struct driver *dp, message *m_ptr) );
FORWARD _PROTOTYPE( int log_cancel, (struct driver *dp, message *m_ptr) );
FORWARD _PROTOTYPE( int log_select, (struct driver *dp, message *m_ptr) );
FORWARD _PROTOTYPE( void log_signal, (struct driver *dp, message *m_ptr) );
FORWARD _PROTOTYPE( int log_other, (struct driver *dp, message *m_ptr, int) );
FORWARD _PROTOTYPE( void log_geometry, (struct partition *entry) );
FORWARD _PROTOTYPE( int subread, (struct logdevice *log, int count, int proc_nr, vir_bytes user_vir, size_t, int safe) );
/* Entry points to this driver. */
PRIVATE struct driver log_dtab = {
log_name, /* current device's name */
log_do_open, /* open or mount */
do_nop, /* nothing on a close */
nop_ioctl, /* ioctl nop */
log_prepare, /* prepare for I/O on a given minor device */
log_transfer, /* do the I/O */
nop_cleanup, /* no need to clean up */
log_geometry, /* geometry */
log_signal, /* handle system signal */
nop_alarm, /* no alarm */
log_cancel, /* CANCEL request */
log_select, /* DEV_SELECT request */
log_other, /* Unrecognized messages */
NULL /* HW int */
};
extern int device_caller;
/*===========================================================================*
* main *
*===========================================================================*/
PUBLIC int main(void)
{
int i;
for(i = 0; i < NR_DEVS; i++) {
log_geom[i].dv_size = cvul64(LOG_SIZE);
log_geom[i].dv_base = cvul64((long)logdevices[i].log_buffer);
logdevices[i].log_size = logdevices[i].log_read =
logdevices[i].log_write =
logdevices[i].log_select_alerted =
logdevices[i].log_selected =
logdevices[i].log_select_ready_ops = 0;
logdevices[i].log_proc_nr = 0;
logdevices[i].log_revive_alerted = 0;
}
driver_task(&log_dtab);
return(OK);
}
/*===========================================================================*
* log_name *
*===========================================================================*/
PRIVATE char *log_name()
{
/* Return a name for the current device. */
static char name[] = "log";
return name;
}
/*===========================================================================*
* log_prepare *
*===========================================================================*/
PRIVATE struct device *log_prepare(device)
int device;
{
/* Prepare for I/O on a device: check if the minor device number is ok. */
if (device < 0 || device >= NR_DEVS) return(NIL_DEV);
log_device = device;
return(&log_geom[device]);
}
/*===========================================================================*
* subwrite *
*===========================================================================*/
PRIVATE int
subwrite(struct logdevice *log, int count, int proc_nr,
vir_bytes user_vir, size_t offset, int safe)
{
char *buf;
int r;
if (log->log_write + count > LOG_SIZE)
count = LOG_SIZE - log->log_write;
buf = log->log_buffer + log->log_write;
if(proc_nr == SELF) {
memcpy(buf, (char *) user_vir, count);
}
else {
if(safe) {
if((r=sys_safecopyfrom(proc_nr, user_vir, offset,
(vir_bytes)buf, count, D)) != OK)
return r;
} else {
if((r=sys_vircopy(proc_nr, D,
user_vir + offset, SELF,D,(int)buf, count)) != OK)
return r;
}
}
LOGINC(log->log_write, count);
log->log_size += count;
if(log->log_size > LOG_SIZE) {
int overflow;
overflow = log->log_size - LOG_SIZE;
log->log_size -= overflow;
LOGINC(log->log_read, overflow);
}
if(log->log_size > 0 && log->log_proc_nr && !log->log_revive_alerted) {
/* Someone who was suspended on read can now
* be revived.
*/
log->log_status = subread(log, log->log_iosize,
log->log_proc_nr, log->log_user_vir_g,
log->log_user_vir_offset, log->log_safe);
notify(log->log_source);
log->log_revive_alerted = 1;
}
if(log->log_size > 0)
log->log_select_ready_ops |= SEL_RD;
if(log->log_size > 0 && log->log_selected &&
!(log->log_select_alerted)) {
/* Someone(s) who was/were select()ing can now
* be awoken. If there was a blocking read (above),
* this can only happen if the blocking read didn't
* swallow all the data (log_size > 0).
*/
if(log->log_selected & SEL_RD) {
notify(log->log_select_proc);
log->log_select_alerted = 1;
#if LOG_DEBUG
printf("log notified %d\n", log->log_select_proc);
#endif
}
}
return count;
}
/*===========================================================================*
* log_append *
*===========================================================================*/
PUBLIC void
log_append(char *buf, int count)
{
int w = 0, skip = 0;
if(count < 1) return;
if(count > LOG_SIZE) skip = count - LOG_SIZE;
count -= skip;
buf += skip;
w = subwrite(&logdevices[0], count, SELF, (vir_bytes) buf,0,0);
if(w > 0 && w < count)
subwrite(&logdevices[0], count-w, SELF, (vir_bytes) buf+w,0,0);
return;
}
/*===========================================================================*
* subread *
*===========================================================================*/
PRIVATE int
subread(struct logdevice *log, int count, int proc_nr,
vir_bytes user_vir, size_t offset, int safe)
{
char *buf;
int r;
if (count > log->log_size)
count = log->log_size;
if (log->log_read + count > LOG_SIZE)
count = LOG_SIZE - log->log_read;
buf = log->log_buffer + log->log_read;
if(safe) {
if((r=sys_safecopyto(proc_nr, user_vir, offset,
(vir_bytes)buf, count, D)) != OK)
return r;
} else {
if((r=sys_vircopy(SELF,D,(int)buf,
proc_nr, safe ? GRANT_SEG : D, user_vir + offset, count)) != OK)
return r;
}
LOGINC(log->log_read, count);
log->log_size -= count;
return count;
}
/*===========================================================================*
* log_transfer *
*===========================================================================*/
PRIVATE int log_transfer(proc_nr, opcode, position, iov, nr_req, safe)
int proc_nr; /* process doing the request */
int opcode; /* DEV_GATHER_S or DEV_SCATTER_S */
u64_t position; /* offset on device to read or write */
iovec_t *iov; /* pointer to read or write request vector */
unsigned nr_req; /* length of request vector */
int safe; /* safe copies? */
{
/* Read or write one the driver's minor devices. */
unsigned count;
vir_bytes user_vir;
struct device *dv;
unsigned long dv_size;
int accumulated_read = 0;
struct logdevice *log;
static int f;
size_t vir_offset = 0;
if(log_device < 0 || log_device >= NR_DEVS)
return EIO;
/* Get minor device number and check for /dev/null. */
dv = &log_geom[log_device];
dv_size = cv64ul(dv->dv_size);
log = &logdevices[log_device];
while (nr_req > 0) {
/* How much to transfer and where to / from. */
count = iov->iov_size;
user_vir = iov->iov_addr;
switch (log_device) {
case MINOR_KLOG:
if (opcode == DEV_GATHER_S) {
if (log->log_proc_nr || count < 1) {
/* There's already someone hanging to read, or
* no real I/O requested.
*/
return(OK);
}
if (!log->log_size) {
if(accumulated_read)
return OK;
/* No data available; let caller block. */
log->log_proc_nr = proc_nr;
log->log_iosize = count;
log->log_user_vir_g = user_vir;
log->log_user_vir_offset = 0;
log->log_revive_alerted = 0;
log->log_safe = safe;
/* Device_caller is a global in drivers library. */
log->log_source = device_caller;
#if LOG_DEBUG
printf("blocked %d (%d)\n",
log->log_source, log->log_proc_nr);
#endif
return(SUSPEND);
}
count = subread(log, count, proc_nr, user_vir, vir_offset, safe);
if(count < 0) {
return count;
}
accumulated_read += count;
} else {
count = subwrite(log, count, proc_nr, user_vir, vir_offset, safe);
if(count < 0)
return count;
}
break;
/* Unknown (illegal) minor device. */
default:
return(EINVAL);
}
/* Book the number of bytes transferred. */
vir_offset += count;
if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
}
return(OK);
}
/*============================================================================*
* log_do_open *
*============================================================================*/
PRIVATE int log_do_open(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
if (log_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
return(OK);
}
/*============================================================================*
* log_geometry *
*============================================================================*/
PRIVATE void log_geometry(entry)
struct partition *entry;
{
/* take a page from the fake memory device geometry */
entry->heads = 64;
entry->sectors = 32;
entry->cylinders = div64u(log_geom[log_device].dv_size, SECTOR_SIZE) /
(entry->heads * entry->sectors);
}
/*============================================================================*
* log_cancel *
*============================================================================*/
PRIVATE int log_cancel(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
int d;
d = m_ptr->TTY_LINE;
if(d < 0 || d >= NR_DEVS)
return EINVAL;
logdevices[d].log_proc_nr = 0;
logdevices[d].log_revive_alerted = 0;
return(OK);
}
/*============================================================================*
* do_status *
*============================================================================*/
PRIVATE void do_status(message *m_ptr)
{
int d;
message m;
/* Caller has requested pending status information, which currently
* can be pending available select()s, or REVIVE events. One message
* is returned for every event, or DEV_NO_STATUS if no (more) events
* are to be returned.
*/
for(d = 0; d < NR_DEVS; d++) {
/* Check for revive callback. */
if(logdevices[d].log_proc_nr && logdevices[d].log_revive_alerted
&& logdevices[d].log_source == m_ptr->m_source) {
m.m_type = DEV_REVIVE;
m.REP_ENDPT = logdevices[d].log_proc_nr;
m.REP_STATUS = logdevices[d].log_status;
m.REP_IO_GRANT = logdevices[d].log_user_vir_g;
send(m_ptr->m_source, &m);
logdevices[d].log_proc_nr = 0;
logdevices[d].log_revive_alerted = 0;
#if LOG_DEBUG
printf("revived %d with %d bytes\n",
m.REP_ENDPT, m.REP_STATUS);
#endif
return;
}
/* Check for select callback. */
if(logdevices[d].log_selected && logdevices[d].log_select_proc == m_ptr->m_source
&& logdevices[d].log_select_alerted) {
m.m_type = DEV_IO_READY;
m.DEV_SEL_OPS = logdevices[d].log_select_ready_ops;
m.DEV_MINOR = d;
#if LOG_DEBUG
printf("select sending sent\n");
#endif
send(m_ptr->m_source, &m);
logdevices[d].log_selected &= ~logdevices[d].log_select_ready_ops;
logdevices[d].log_select_alerted = 0;
#if LOG_DEBUG
printf("select send sent\n");
#endif
}
}
/* No event found. */
m.m_type = DEV_NO_STATUS;
send(m_ptr->m_source, &m);
return;
}
/*============================================================================*
* log_signal *
*============================================================================*/
PRIVATE void log_signal(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
sigset_t sigset = m_ptr->NOTIFY_ARG;
if (sigismember(&sigset, SIGKMESS)) {
do_new_kmess(m_ptr);
}
}
/*============================================================================*
* log_other *
*============================================================================*/
PRIVATE int log_other(dp, m_ptr, safe)
struct driver *dp;
message *m_ptr;
int safe;
{
int r;
/* This function gets messages that the generic driver doesn't
* understand.
*/
switch(m_ptr->m_type) {
case DIAGNOSTICS: {
r = do_diagnostics(m_ptr, 0);
break;
}
case DIAGNOSTICS_S: {
r = do_diagnostics(m_ptr, 1);
break;
}
case DEV_STATUS: {
do_status(m_ptr);
r = EDONTREPLY;
break;
}
case NOTIFY_FROM(TTY_PROC_NR):
do_new_kmess(m_ptr);
r = EDONTREPLY;
break;
default:
r = EINVAL;
break;
}
return r;
}
/*============================================================================*
* log_select *
*============================================================================*/
PRIVATE int log_select(dp, m_ptr)
struct driver *dp;
message *m_ptr;
{
int d, ready_ops = 0, ops = 0;
d = m_ptr->TTY_LINE;
if(d < 0 || d >= NR_DEVS) {
#if LOG_DEBUG
printf("line %d? EINVAL\n", d);
#endif
return EINVAL;
}
ops = m_ptr->IO_ENDPT & (SEL_RD|SEL_WR|SEL_ERR);
/* Read blocks when there is no log. */
if((m_ptr->IO_ENDPT & SEL_RD) && logdevices[d].log_size > 0) {
#if LOG_DEBUG
printf("log can read; size %d\n", logdevices[d].log_size);
#endif
ready_ops |= SEL_RD; /* writes never block */
}
/* Write never blocks. */
if(m_ptr->IO_ENDPT & SEL_WR) ready_ops |= SEL_WR;
/* Enable select calback if no operations were
* ready to go, but operations were requested,
* and notify was enabled.
*/
if((m_ptr->IO_ENDPT & SEL_NOTIFY) && ops && !ready_ops) {
logdevices[d].log_selected |= ops;
logdevices[d].log_select_proc = m_ptr->m_source;
#if LOG_DEBUG
printf("log setting selector.\n");
#endif
}
#if LOG_DEBUG
printf("log returning ops %d\n", ready_ops);
#endif
return(ready_ops);
}