source: branches/minix3-book/servers/fs/device.c@ 4

Last change on this file since 4 was 4, checked in by Mattia Monga, 13 years ago

Importazione sorgenti libro

File size: 13.5 KB
Line 
1/* When a needed block is not in the cache, it must be fetched from the disk.
2 * Special character files also require I/O. The routines for these are here.
3 *
4 * The entry points in this file are:
5 * dev_open: FS opens a device
6 * dev_close: FS closes a device
7 * dev_io: FS does a read or write on a device
8 * dev_status: FS processes callback request alert
9 * gen_opcl: generic call to a task to perform an open/close
10 * gen_io: generic call to a task to perform an I/O operation
11 * no_dev: open/close processing for devices that don't exist
12 * tty_opcl: perform tty-specific processing for open/close
13 * ctty_opcl: perform controlling-tty-specific processing for open/close
14 * ctty_io: perform controlling-tty-specific processing for I/O
15 * do_ioctl: perform the IOCTL system call
16 * do_setsid: perform the SETSID system call (FS side)
17 */
18
19#include "fs.h"
20#include <fcntl.h>
21#include <minix/callnr.h>
22#include <minix/com.h>
23#include "file.h"
24#include "fproc.h"
25#include "inode.h"
26#include "param.h"
27
28#define ELEMENTS(a) (sizeof(a)/sizeof((a)[0]))
29
30extern int dmap_size;
31
32/*===========================================================================*
33 * dev_open *
34 *===========================================================================*/
35PUBLIC int dev_open(dev, proc, flags)
36dev_t dev; /* device to open */
37int proc; /* process to open for */
38int flags; /* mode bits and flags */
39{
40 int major, r;
41 struct dmap *dp;
42
43 /* Determine the major device number call the device class specific
44 * open/close routine. (This is the only routine that must check the
45 * device number for being in range. All others can trust this check.)
46 */
47 major = (dev >> MAJOR) & BYTE;
48 if (major >= NR_DEVICES) major = 0;
49 dp = &dmap[major];
50 r = (*dp->dmap_opcl)(DEV_OPEN, dev, proc, flags);
51 if (r == SUSPEND) panic(__FILE__,"suspend on open from", dp->dmap_driver);
52 return(r);
53}
54
55/*===========================================================================*
56 * dev_close *
57 *===========================================================================*/
58PUBLIC void dev_close(dev)
59dev_t dev; /* device to close */
60{
61 (void) (*dmap[(dev >> MAJOR) & BYTE].dmap_opcl)(DEV_CLOSE, dev, 0, 0);
62}
63
64/*===========================================================================*
65 * dev_status *
66 *===========================================================================*/
67PUBLIC void dev_status(message *m)
68{
69 message st;
70 int d, get_more = 1;
71
72 for(d = 0; d < NR_DEVICES; d++)
73 if (dmap[d].dmap_driver == m->m_source)
74 break;
75
76 if (d >= NR_DEVICES)
77 return;
78
79 do {
80 int r;
81 st.m_type = DEV_STATUS;
82 if ((r=sendrec(m->m_source, &st)) != OK)
83 panic(__FILE__,"couldn't sendrec for DEV_STATUS", r);
84
85 switch(st.m_type) {
86 case DEV_REVIVE:
87 revive(st.REP_PROC_NR, st.REP_STATUS);
88 break;
89 case DEV_IO_READY:
90 select_notified(d, st.DEV_MINOR, st.DEV_SEL_OPS);
91 break;
92 default:
93 printf("FS: unrecognized reply %d to DEV_STATUS\n", st.m_type);
94 /* Fall through. */
95 case DEV_NO_STATUS:
96 get_more = 0;
97 break;
98 }
99 } while(get_more);
100
101 return;
102}
103
104/*===========================================================================*
105 * dev_io *
106 *===========================================================================*/
107PUBLIC int dev_io(op, dev, proc, buf, pos, bytes, flags)
108int op; /* DEV_READ, DEV_WRITE, DEV_IOCTL, etc. */
109dev_t dev; /* major-minor device number */
110int proc; /* in whose address space is buf? */
111void *buf; /* virtual address of the buffer */
112off_t pos; /* byte position */
113int bytes; /* how many bytes to transfer */
114int flags; /* special flags, like O_NONBLOCK */
115{
116/* Read or write from a device. The parameter 'dev' tells which one. */
117 struct dmap *dp;
118 message dev_mess;
119
120 /* Determine task dmap. */
121 dp = &dmap[(dev >> MAJOR) & BYTE];
122
123 /* Set up the message passed to task. */
124 dev_mess.m_type = op;
125 dev_mess.DEVICE = (dev >> MINOR) & BYTE;
126 dev_mess.POSITION = pos;
127 dev_mess.PROC_NR = proc;
128 dev_mess.ADDRESS = buf;
129 dev_mess.COUNT = bytes;
130 dev_mess.TTY_FLAGS = flags;
131
132 /* Call the task. */
133 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
134
135 /* Task has completed. See if call completed. */
136 if (dev_mess.REP_STATUS == SUSPEND) {
137 if (flags & O_NONBLOCK) {
138 /* Not supposed to block. */
139 dev_mess.m_type = CANCEL;
140 dev_mess.PROC_NR = proc;
141 dev_mess.DEVICE = (dev >> MINOR) & BYTE;
142 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
143 if (dev_mess.REP_STATUS == EINTR) dev_mess.REP_STATUS = EAGAIN;
144 } else {
145 /* Suspend user. */
146 suspend(dp->dmap_driver);
147 return(SUSPEND);
148 }
149 }
150 return(dev_mess.REP_STATUS);
151}
152
153/*===========================================================================*
154 * gen_opcl *
155 *===========================================================================*/
156PUBLIC int gen_opcl(op, dev, proc, flags)
157int op; /* operation, DEV_OPEN or DEV_CLOSE */
158dev_t dev; /* device to open or close */
159int proc; /* process to open/close for */
160int flags; /* mode bits and flags */
161{
162/* Called from the dmap struct in table.c on opens & closes of special files.*/
163 struct dmap *dp;
164 message dev_mess;
165
166 /* Determine task dmap. */
167 dp = &dmap[(dev >> MAJOR) & BYTE];
168
169 dev_mess.m_type = op;
170 dev_mess.DEVICE = (dev >> MINOR) & BYTE;
171 dev_mess.PROC_NR = proc;
172 dev_mess.COUNT = flags;
173
174 /* Call the task. */
175 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
176
177 return(dev_mess.REP_STATUS);
178}
179
180/*===========================================================================*
181 * tty_opcl *
182 *===========================================================================*/
183PUBLIC int tty_opcl(op, dev, proc, flags)
184int op; /* operation, DEV_OPEN or DEV_CLOSE */
185dev_t dev; /* device to open or close */
186int proc; /* process to open/close for */
187int flags; /* mode bits and flags */
188{
189/* This procedure is called from the dmap struct on tty open/close. */
190
191 int r;
192 register struct fproc *rfp;
193
194 /* Add O_NOCTTY to the flags if this process is not a session leader, or
195 * if it already has a controlling tty, or if it is someone elses
196 * controlling tty.
197 */
198 if (!fp->fp_sesldr || fp->fp_tty != 0) {
199 flags |= O_NOCTTY;
200 } else {
201 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
202 if (rfp->fp_tty == dev) flags |= O_NOCTTY;
203 }
204 }
205
206 r = gen_opcl(op, dev, proc, flags);
207
208 /* Did this call make the tty the controlling tty? */
209 if (r == 1) {
210 fp->fp_tty = dev;
211 r = OK;
212 }
213 return(r);
214}
215
216/*===========================================================================*
217 * ctty_opcl *
218 *===========================================================================*/
219PUBLIC int ctty_opcl(op, dev, proc, flags)
220int op; /* operation, DEV_OPEN or DEV_CLOSE */
221dev_t dev; /* device to open or close */
222int proc; /* process to open/close for */
223int flags; /* mode bits and flags */
224{
225/* This procedure is called from the dmap struct in table.c on opening/closing
226 * /dev/tty, the magic device that translates to the controlling tty.
227 */
228
229 return(fp->fp_tty == 0 ? ENXIO : OK);
230}
231
232/*===========================================================================*
233 * do_setsid *
234 *===========================================================================*/
235PUBLIC int do_setsid()
236{
237/* Perform the FS side of the SETSID call, i.e. get rid of the controlling
238 * terminal of a process, and make the process a session leader.
239 */
240 register struct fproc *rfp;
241
242 /* Only MM may do the SETSID call directly. */
243 if (who != PM_PROC_NR) return(ENOSYS);
244
245 /* Make the process a session leader with no controlling tty. */
246 rfp = &fproc[m_in.slot1];
247 rfp->fp_sesldr = TRUE;
248 rfp->fp_tty = 0;
249 return(OK);
250}
251
252/*===========================================================================*
253 * do_ioctl *
254 *===========================================================================*/
255PUBLIC int do_ioctl()
256{
257/* Perform the ioctl(ls_fd, request, argx) system call (uses m2 fmt). */
258
259 struct filp *f;
260 register struct inode *rip;
261 dev_t dev;
262
263 if ( (f = get_filp(m_in.ls_fd)) == NIL_FILP) return(err_code);
264 rip = f->filp_ino; /* get inode pointer */
265 if ( (rip->i_mode & I_TYPE) != I_CHAR_SPECIAL
266 && (rip->i_mode & I_TYPE) != I_BLOCK_SPECIAL) return(ENOTTY);
267 dev = (dev_t) rip->i_zone[0];
268
269 return(dev_io(DEV_IOCTL, dev, who, m_in.ADDRESS, 0L,
270 m_in.REQUEST, f->filp_flags));
271}
272
273/*===========================================================================*
274 * gen_io *
275 *===========================================================================*/
276PUBLIC void gen_io(task_nr, mess_ptr)
277int task_nr; /* which task to call */
278message *mess_ptr; /* pointer to message for task */
279{
280/* All file system I/O ultimately comes down to I/O on major/minor device
281 * pairs. These lead to calls on the following routines via the dmap table.
282 */
283
284 int r, proc_nr;
285 message local_m;
286
287 proc_nr = mess_ptr->PROC_NR;
288 if (! isokprocnr(proc_nr)) {
289 printf("FS: warning, got illegal process number (%d) from %d\n",
290 mess_ptr->PROC_NR, mess_ptr->m_source);
291 return;
292 }
293
294 while ((r = sendrec(task_nr, mess_ptr)) == ELOCKED) {
295 /* sendrec() failed to avoid deadlock. The task 'task_nr' is
296 * trying to send a REVIVE message for an earlier request.
297 * Handle it and go try again.
298 */
299 if ((r = receive(task_nr, &local_m)) != OK) {
300 break;
301 }
302
303 /* If we're trying to send a cancel message to a task which has just
304 * sent a completion reply, ignore the reply and abort the cancel
305 * request. The caller will do the revive for the process.
306 */
307 if (mess_ptr->m_type == CANCEL && local_m.REP_PROC_NR == proc_nr) {
308 return;
309 }
310
311 /* Otherwise it should be a REVIVE. */
312 if (local_m.m_type != REVIVE) {
313 printf(
314 "fs: strange device reply from %d, type = %d, proc = %d (1)\n",
315 local_m.m_source,
316 local_m.m_type, local_m.REP_PROC_NR);
317 continue;
318 }
319
320 revive(local_m.REP_PROC_NR, local_m.REP_STATUS);
321 }
322
323 /* The message received may be a reply to this call, or a REVIVE for some
324 * other process.
325 */
326 for (;;) {
327 if (r != OK) {
328 if (r == EDEADDST) return; /* give up */
329 else panic(__FILE__,"call_task: can't send/receive", r);
330 }
331
332 /* Did the process we did the sendrec() for get a result? */
333 if (mess_ptr->REP_PROC_NR == proc_nr) {
334 break;
335 } else if (mess_ptr->m_type == REVIVE) {
336 /* Otherwise it should be a REVIVE. */
337 revive(mess_ptr->REP_PROC_NR, mess_ptr->REP_STATUS);
338 } else {
339 printf(
340 "fs: strange device reply from %d, type = %d, proc = %d (2)\n",
341 mess_ptr->m_source,
342 mess_ptr->m_type, mess_ptr->REP_PROC_NR);
343 return;
344 }
345
346 r = receive(task_nr, mess_ptr);
347 }
348}
349
350/*===========================================================================*
351 * ctty_io *
352 *===========================================================================*/
353PUBLIC void ctty_io(task_nr, mess_ptr)
354int task_nr; /* not used - for compatibility with dmap_t */
355message *mess_ptr; /* pointer to message for task */
356{
357/* This routine is only called for one device, namely /dev/tty. Its job
358 * is to change the message to use the controlling terminal, instead of the
359 * major/minor pair for /dev/tty itself.
360 */
361
362 struct dmap *dp;
363
364 if (fp->fp_tty == 0) {
365 /* No controlling tty present anymore, return an I/O error. */
366 mess_ptr->REP_STATUS = EIO;
367 } else {
368 /* Substitute the controlling terminal device. */
369 dp = &dmap[(fp->fp_tty >> MAJOR) & BYTE];
370 mess_ptr->DEVICE = (fp->fp_tty >> MINOR) & BYTE;
371 (*dp->dmap_io)(dp->dmap_driver, mess_ptr);
372 }
373}
374
375/*===========================================================================*
376 * no_dev *
377 *===========================================================================*/
378PUBLIC int no_dev(op, dev, proc, flags)
379int op; /* operation, DEV_OPEN or DEV_CLOSE */
380dev_t dev; /* device to open or close */
381int proc; /* process to open/close for */
382int flags; /* mode bits and flags */
383{
384/* Called when opening a nonexistent device. */
385
386 return(ENODEV);
387}
388
389/*===========================================================================*
390 * clone_opcl *
391 *===========================================================================*/
392PUBLIC int clone_opcl(op, dev, proc, flags)
393int op; /* operation, DEV_OPEN or DEV_CLOSE */
394dev_t dev; /* device to open or close */
395int proc; /* process to open/close for */
396int flags; /* mode bits and flags */
397{
398/* Some devices need special processing upon open. Such a device is "cloned",
399 * i.e. on a succesful open it is replaced by a new device with a new unique
400 * minor device number. This new device number identifies a new object (such
401 * as a new network connection) that has been allocated within a task.
402 */
403 struct dmap *dp;
404 int minor;
405 message dev_mess;
406
407 /* Determine task dmap. */
408 dp = &dmap[(dev >> MAJOR) & BYTE];
409 minor = (dev >> MINOR) & BYTE;
410
411 dev_mess.m_type = op;
412 dev_mess.DEVICE = minor;
413 dev_mess.PROC_NR = proc;
414 dev_mess.COUNT = flags;
415
416 /* Call the task. */
417 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
418
419 if (op == DEV_OPEN && dev_mess.REP_STATUS >= 0) {
420 if (dev_mess.REP_STATUS != minor) {
421 /* A new minor device number has been returned. Create a
422 * temporary device file to hold it.
423 */
424 struct inode *ip;
425
426 /* Device number of the new device. */
427 dev = (dev & ~(BYTE << MINOR)) | (dev_mess.REP_STATUS << MINOR);
428
429 ip = alloc_inode(root_dev, ALL_MODES | I_CHAR_SPECIAL);
430 if (ip == NIL_INODE) {
431 /* Oops, that didn't work. Undo open. */
432 (void) clone_opcl(DEV_CLOSE, dev, proc, 0);
433 return(err_code);
434 }
435 ip->i_zone[0] = dev;
436
437 put_inode(fp->fp_filp[m_in.fd]->filp_ino);
438 fp->fp_filp[m_in.fd]->filp_ino = ip;
439 }
440 dev_mess.REP_STATUS = OK;
441 }
442 return(dev_mess.REP_STATUS);
443}
444
Note: See TracBrowser for help on using the repository browser.