Annotation of prex-old/usr/server/fs/devfs/devfs_vnops.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2005-2007, Kohsuke Ohtani
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #include <prex/prex.h>
31: #include <sys/stat.h>
32: #include <sys/vnode.h>
33: #include <sys/file.h>
34: #include <sys/mount.h>
35: #include <sys/syslog.h>
36:
37: #include <ctype.h>
38: #include <unistd.h>
39: #include <errno.h>
40: #include <string.h>
41: #include <stdlib.h>
42: #include <limits.h>
43: #include <fcntl.h>
44:
45: #include "devfs.h"
46:
47:
48: #define devfs_mount ((vfsop_mount_t)vfs_nullop)
49: #define devfs_unmount ((vfsop_umount_t)vfs_nullop)
50: #define devfs_sync ((vfsop_sync_t)vfs_nullop)
51: #define devfs_vget ((vfsop_vget_t)vfs_nullop)
52: #define devfs_statfs ((vfsop_statfs_t)vfs_nullop)
53:
54: static int devfs_open(vnode_t, mode_t);
55: static int devfs_close(vnode_t, file_t);
56: static int devfs_read(vnode_t, file_t, void *, size_t, size_t *);
57: static int devfs_write(vnode_t, file_t, void *, size_t, size_t *);
58: #define devfs_seek ((vnop_seek_t)vop_nullop)
59: static int devfs_ioctl(vnode_t, file_t, int, u_long);
60: #define devfs_fsync ((vnop_fsync_t)vop_nullop)
61: static int devfs_readdir(vnode_t, file_t, struct dirent *);
62: static int devfs_lookup(vnode_t, char *, vnode_t);
63: #define devfs_create ((vnop_create_t)vop_einval)
64: #define devfs_remove ((vnop_remove_t)vop_einval)
65: #define devfs_rename ((vnop_rename_t)vop_einval)
66: #define devfs_mkdir ((vnop_mkdir_t)vop_einval)
67: #define devfs_rmdir ((vnop_rmdir_t)vop_einval)
68: #define devfs_getattr ((vnop_getattr_t)vop_nullop)
69: #define devfs_setattr ((vnop_setattr_t)vop_nullop)
70: #define devfs_inactive ((vnop_inactive_t)vop_nullop)
71: #define devfs_truncate ((vnop_truncate_t)vop_nullop)
72:
73: struct vnops devfs_vnops;
74:
75: /*
76: * File system operations
77: */
78: struct vfsops devfs_vfsops = {
79: devfs_mount, /* mount */
80: devfs_unmount, /* unmount */
81: devfs_sync, /* sync */
82: devfs_vget, /* vget */
83: devfs_statfs, /* statfs */
84: &devfs_vnops, /* vnops */
85: };
86:
87: /*
88: * vnode operations
89: */
90: struct vnops devfs_vnops = {
91: devfs_open, /* open */
92: devfs_close, /* close */
93: devfs_read, /* read */
94: devfs_write, /* write */
95: devfs_seek, /* seek */
96: devfs_ioctl, /* ioctl */
97: devfs_fsync, /* fsync */
98: devfs_readdir, /* readdir */
99: devfs_lookup, /* lookup */
100: devfs_create, /* create */
101: devfs_remove, /* remove */
102: devfs_rename, /* remame */
103: devfs_mkdir, /* mkdir */
104: devfs_rmdir, /* rmdir */
105: devfs_getattr, /* getattr */
106: devfs_setattr, /* setattr */
107: devfs_inactive, /* inactive */
108: devfs_truncate, /* truncate */
109: };
110:
111: static int
112: devfs_open(vnode_t vp, mode_t mode)
113: {
114: char *path;
115: device_t dev;
116: int err;
117:
118: dprintf("devfs_open: path=%s\n", vp->v_path);
119:
120: path = vp->v_path;
121: if (!strcmp(path, "/")) /* root ? */
122: return 0;
123:
124: if (*path == '/')
125: path++;
126: err = device_open(path, mode & DO_RWMASK, &dev);
127: if (err) {
128: dprintf("devfs_open: can not open device = %s error=%d\n",
129: path, err);
130: return err;
131: }
132: vp->v_data = (void *)dev; /* Store private data */
133: return 0;
134: }
135:
136: static int
137: devfs_close(vnode_t vp, file_t fp)
138: {
139:
140: dprintf("devfs_close: fp=%x\n", fp);
141:
142: if (!strcmp(vp->v_path, "/")) /* root ? */
143: return 0;
144:
145: return device_close((device_t)vp->v_data);
146: }
147:
148: static int
149: devfs_read(vnode_t vp, file_t fp, void *buf, size_t size, size_t *result)
150: {
151: int err;
152: size_t len;
153:
154: len = size;
155: err = device_read((device_t)vp->v_data, buf, &len, fp->f_offset);
156: if (!err)
157: *result = len;
158: return err;
159: }
160:
161: static int
162: devfs_write(vnode_t vp, file_t fp, void *buf, size_t size, size_t *result)
163: {
164: int err;
165: size_t len;
166:
167: len = size;
168: err = device_write((device_t)vp->v_data, buf, &len, fp->f_offset);
169: if (!err)
170: *result = len;
171: dprintf("devfs_write: err=%d len=%d\n", err, len);
172: return err;
173: }
174:
175: static int
176: devfs_ioctl(vnode_t vp, file_t fp, int cmd, u_long arg)
177: {
178: dprintf("devfs_ioctl\n");
179: return EINVAL;
180: }
181:
182: static int
183: devfs_lookup(vnode_t dvp, char *name, vnode_t vp)
184: {
185: struct info_device info;
186: int err, i;
187:
188: dprintf("devfs_lookup:%s\n", name);
189:
190: if (*name == '\0')
191: return ENOENT;
192:
193: i = 0;
194: err = 0;
195: info.cookie = 0;
196: for (;;) {
197: err = sys_info(INFO_DEVICE, &info);
198: if (err)
199: return ENOENT;
200: if (!strncmp(info.name, name, MAXDEVNAME))
201: break;
202: i++;
203: }
204: vp->v_type = (info.flags & DF_CHR) ? VCHR : VBLK;
205: vp->v_mode = (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
206: | S_IROTH | S_IWOTH);
207: return 0;
208: }
209:
210: /*
211: * @vp: vnode of the directory.
212: */
213: static int
214: devfs_readdir(vnode_t vp, file_t fp, struct dirent *dir)
215: {
216: struct info_device info;
217: int err, i;
218:
219: dprintf("devfs_readdir offset=%d\n", fp->f_offset);
220:
221: i = 0;
222: err = 0;
223: info.cookie = 0;
224: do {
225: err = sys_info(INFO_DEVICE, &info);
226: if (err)
227: return ENOENT;
228: } while (i++ != fp->f_offset);
229:
230: dir->d_type = 0;
231: if (info.flags & DF_CHR)
232: dir->d_type = DT_CHR;
233: else if (info.flags & DF_BLK)
234: dir->d_type = DT_BLK;
235: strcpy((char *)&dir->d_name, info.name);
236: dir->d_fileno = fp->f_offset;
237: dir->d_namlen = strlen(dir->d_name);
238:
239: dprintf("devfs_readdir: %s\n", dir->d_name);
240: fp->f_offset++;
241: return 0;
242: }
243:
244: int
245: devfs_init(void)
246: {
247: return 0;
248: }
CVSweb