xref: /minix3/minix/lib/libbdev/minor.c (revision 7c48de6cc4c6d56f2277d378dba01dbac8a8c3b9)
1433d6423SLionel Sambuc /* libbdev - tracking and reopening of opened minor devices */
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc #include <minix/drivers.h>
4433d6423SLionel Sambuc #include <minix/bdev.h>
5433d6423SLionel Sambuc #include <assert.h>
6433d6423SLionel Sambuc 
7433d6423SLionel Sambuc #include "const.h"
8433d6423SLionel Sambuc #include "type.h"
9433d6423SLionel Sambuc #include "proto.h"
10433d6423SLionel Sambuc 
11433d6423SLionel Sambuc static struct {
12433d6423SLionel Sambuc   dev_t dev;
13433d6423SLionel Sambuc   int count;
14433d6423SLionel Sambuc   int access;
15433d6423SLionel Sambuc } open_dev[NR_OPEN_DEVS] = { { NO_DEV, 0, 0 } };
16433d6423SLionel Sambuc 
bdev_minor_reopen(dev_t dev)17433d6423SLionel Sambuc int bdev_minor_reopen(dev_t dev)
18433d6423SLionel Sambuc {
19433d6423SLionel Sambuc /* Reopen all minor devices on a major device. This function duplicates some
20433d6423SLionel Sambuc  * code from elsewhere, because in this case we must avoid performing recovery.
21433d6423SLionel Sambuc  * FIXME: if reopening fails with a non-IPC error, we should attempt to close
22433d6423SLionel Sambuc  * all minors that we did manage to reopen so far, or they might stay open
23433d6423SLionel Sambuc  * forever.
24433d6423SLionel Sambuc  */
25433d6423SLionel Sambuc   endpoint_t endpt;
26433d6423SLionel Sambuc   message m;
27433d6423SLionel Sambuc   int i, j, r, major;
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc   major = major(dev);
30433d6423SLionel Sambuc   endpt = bdev_driver_get(dev);
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc   assert(endpt != NONE);
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc   for (i = 0; i < NR_OPEN_DEVS; i++) {
35433d6423SLionel Sambuc 	if (major(open_dev[i].dev) != major)
36433d6423SLionel Sambuc 		continue;
37433d6423SLionel Sambuc 
38433d6423SLionel Sambuc 	/* Each minor device may have been opened multiple times. Send an open
39433d6423SLionel Sambuc 	 * request for each time that it was opened before. We could reopen it
40433d6423SLionel Sambuc 	 * just once, but then we'd have to keep a shadow open count as well.
41433d6423SLionel Sambuc 	 */
42433d6423SLionel Sambuc 	for (j = 0; j < open_dev[i].count; j++) {
43433d6423SLionel Sambuc 		memset(&m, 0, sizeof(m));
44433d6423SLionel Sambuc 		m.m_type = BDEV_OPEN;
45433d6423SLionel Sambuc 		m.m_lbdev_lblockdriver_msg.minor = minor(open_dev[i].dev);
46433d6423SLionel Sambuc 		m.m_lbdev_lblockdriver_msg.access = open_dev[i].access;
47433d6423SLionel Sambuc 		m.m_lbdev_lblockdriver_msg.id = NO_ID;
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc 		if ((r = ipc_sendrec(endpt, &m)) != OK) {
50433d6423SLionel Sambuc 			printf("bdev: IPC to driver (%d) failed (%d)\n",
51433d6423SLionel Sambuc 				endpt, r);
52433d6423SLionel Sambuc 			return r;
53433d6423SLionel Sambuc 		}
54433d6423SLionel Sambuc 
55433d6423SLionel Sambuc 		if (m.m_type != BDEV_REPLY) {
56433d6423SLionel Sambuc 			printf("bdev: driver (%d) sent weird response (%d)\n",
57433d6423SLionel Sambuc 				endpt, m.m_type);
58433d6423SLionel Sambuc 			return EINVAL;
59433d6423SLionel Sambuc 		}
60433d6423SLionel Sambuc 
61433d6423SLionel Sambuc 		if (m.m_lblockdriver_lbdev_reply.id != NO_ID) {
6265f76edbSDavid van Moolenbroek 			printf("bdev: driver (%d) sent invalid ID (%d)\n",
63433d6423SLionel Sambuc 				endpt, m.m_lblockdriver_lbdev_reply.id);
64433d6423SLionel Sambuc 			return EINVAL;
65433d6423SLionel Sambuc 		}
66433d6423SLionel Sambuc 
67433d6423SLionel Sambuc 		if ((r = m.m_lblockdriver_lbdev_reply.status) != OK) {
68433d6423SLionel Sambuc 			printf("bdev: driver (%d) failed device reopen (%d)\n",
69433d6423SLionel Sambuc 				endpt, r);
70433d6423SLionel Sambuc 			return r;
71433d6423SLionel Sambuc 		}
72433d6423SLionel Sambuc 	}
73433d6423SLionel Sambuc   }
74433d6423SLionel Sambuc 
75433d6423SLionel Sambuc   return OK;
76433d6423SLionel Sambuc }
77433d6423SLionel Sambuc 
bdev_minor_add(dev_t dev,int bits)78*7c48de6cSDavid van Moolenbroek void bdev_minor_add(dev_t dev, int bits)
79433d6423SLionel Sambuc {
80433d6423SLionel Sambuc /* Increase the reference count of the given minor device.
81433d6423SLionel Sambuc  */
82*7c48de6cSDavid van Moolenbroek   int i, ifree = -1;
83433d6423SLionel Sambuc 
84433d6423SLionel Sambuc   for (i = 0; i < NR_OPEN_DEVS; i++) {
85433d6423SLionel Sambuc 	if (open_dev[i].dev == dev) {
86433d6423SLionel Sambuc 		open_dev[i].count++;
87*7c48de6cSDavid van Moolenbroek 		open_dev[i].access |= bits;
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc 		return;
90433d6423SLionel Sambuc 	}
91433d6423SLionel Sambuc 
92*7c48de6cSDavid van Moolenbroek 	if (ifree < 0 && open_dev[i].dev == NO_DEV)
93*7c48de6cSDavid van Moolenbroek 		ifree = i;
94433d6423SLionel Sambuc   }
95433d6423SLionel Sambuc 
96*7c48de6cSDavid van Moolenbroek   if (ifree < 0) {
97433d6423SLionel Sambuc 	printf("bdev: too many open devices, increase NR_OPEN_DEVS\n");
98433d6423SLionel Sambuc 	return;
99433d6423SLionel Sambuc   }
100433d6423SLionel Sambuc 
101*7c48de6cSDavid van Moolenbroek   open_dev[ifree].dev = dev;
102*7c48de6cSDavid van Moolenbroek   open_dev[ifree].count = 1;
103*7c48de6cSDavid van Moolenbroek   open_dev[ifree].access = bits;
104433d6423SLionel Sambuc }
105433d6423SLionel Sambuc 
bdev_minor_del(dev_t dev)106433d6423SLionel Sambuc void bdev_minor_del(dev_t dev)
107433d6423SLionel Sambuc {
108433d6423SLionel Sambuc /* Decrease the reference count of the given minor device, if present.
109433d6423SLionel Sambuc  */
110433d6423SLionel Sambuc   int i;
111433d6423SLionel Sambuc 
112433d6423SLionel Sambuc   for (i = 0; i < NR_OPEN_DEVS; i++) {
113433d6423SLionel Sambuc 	if (open_dev[i].dev == dev) {
114433d6423SLionel Sambuc 		if (!--open_dev[i].count)
115433d6423SLionel Sambuc 			open_dev[i].dev = NO_DEV;
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc 		break;
118433d6423SLionel Sambuc 	}
119433d6423SLionel Sambuc   }
120433d6423SLionel Sambuc }
121433d6423SLionel Sambuc 
bdev_minor_is_open(dev_t dev)122433d6423SLionel Sambuc int bdev_minor_is_open(dev_t dev)
123433d6423SLionel Sambuc {
124433d6423SLionel Sambuc /* Return whether any minor is open for the major of the given device.
125433d6423SLionel Sambuc  */
126433d6423SLionel Sambuc   int i, major;
127433d6423SLionel Sambuc 
128433d6423SLionel Sambuc   major = major(dev);
129433d6423SLionel Sambuc 
130433d6423SLionel Sambuc   for (i = 0; i < NR_OPEN_DEVS; i++) {
131433d6423SLionel Sambuc 	if (major(open_dev[i].dev) == major)
132433d6423SLionel Sambuc 		return TRUE;
133433d6423SLionel Sambuc   }
134433d6423SLionel Sambuc 
135433d6423SLionel Sambuc   return FALSE;
136433d6423SLionel Sambuc }
137