1 
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <errno.h>
6 
7 #include <sys/kassert.h>
8 #include <sys/kdebug.h>
9 #include <sys/spinlock.h>
10 #include <sys/disk.h>
11 #include <sys/vfs.h>
12 #include <sys/handle.h>
13 #include <sys/vfsuio.h>
14 
15 static int
VFSUIO_Read(Handle * handle,void * buf,uint64_t len,uint64_t off)16 VFSUIO_Read(Handle *handle, void *buf, uint64_t len, uint64_t off)
17 {
18     ASSERT(handle->type == HANDLE_TYPE_FILE);
19 
20     // XXX: Need to pin memory
21 
22     return VFS_Read(handle->vnode, buf, len, off);
23 }
24 
25 static int
VFSUIO_Write(Handle * handle,void * buf,uint64_t len,uint64_t off)26 VFSUIO_Write(Handle *handle, void *buf, uint64_t len, uint64_t off)
27 {
28     ASSERT(handle->type == HANDLE_TYPE_FILE);
29 
30     // XXX: Need to pin memory
31 
32     return VFS_Write(handle->vnode, buf, len, off);
33 }
34 
35 static int
VFSUIO_Flush(Handle * handle)36 VFSUIO_Flush(Handle *handle)
37 {
38     ASSERT(handle->type == HANDLE_TYPE_FILE);
39     return -EINVAL;
40 }
41 
42 static int
VFSUIO_Close(Handle * handle)43 VFSUIO_Close(Handle *handle)
44 {
45     int status;
46 
47     ASSERT(handle->type == HANDLE_TYPE_FILE);
48 
49     status = VFS_Close(handle->vnode);
50     Handle_Free(handle);
51 
52     return status;
53 }
54 
55 int
VFSUIO_Open(const char * path,Handle ** handle)56 VFSUIO_Open(const char *path, Handle **handle)
57 {
58     int status;
59 
60     Handle *hdl = Handle_Alloc();
61     if (!hdl) {
62 	return -ENOMEM;
63     }
64 
65     VNode *vn = VFS_Lookup(path);
66     if (!vn) {
67 	Handle_Free(hdl);
68 	return -ENOENT;
69     }
70 
71     status = VFS_Open(vn);
72     if (status != 0) {
73 	// XXX: Release VNode
74 	Handle_Free(hdl);
75 	return status;
76     }
77 
78     hdl->vnode = vn;
79     hdl->type = HANDLE_TYPE_FILE;
80     hdl->read = VFSUIO_Read;
81     hdl->write = VFSUIO_Write;
82     hdl->flush = VFSUIO_Flush;
83     hdl->close = VFSUIO_Close;
84 
85     *handle = hdl;
86 
87     return 0;
88 }
89 
90 
91