Kernel - Fix ASSERTRV macro supplying too many arguments to Warning
[tpg/acess2.git] / KernelLand / Kernel / vfs / io.c
index 1df3403..271e920 100644 (file)
@@ -1,6 +1,11 @@
 /*
- * AcessMicro VFS
- * - File IO Passthru's
+ * Acess2 Kernel
+ * - By John Hodge (thePowersGang)
+ *
+ * vfs/io.c
+ * - VFS IO Handling (Read/Write)
+ *
+ * TODO: VFS-level caching to support non-blocking IO?
  */
 #define DEBUG  0
 #include <acess.h>
@@ -18,40 +23,40 @@ size_t VFS_Read(int FD, size_t Length, void *Buffer)
        tVFS_Handle     *h;
        size_t  ret;
        
-       ENTER("xFD xLength pBuffer", FD, Length, Buffer);
-       
        h = VFS_GetHandle(FD);
        if(!h) {
-               LOG("Bad Handle");
-               LEAVE_RET('i', -1);
+               LOG("FD%i is a bad Handle", FD);
+               return -1;
        }
        
        if( !(h->Mode & VFS_OPENFLAG_READ) ) {
-               LOG("Bad mode");
-               LEAVE_RET('i', -1);
+               LOG("FD%i not open for reading", FD);
+               return -1;
        }
        if( (h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
-               LOG("Reading directory");
-               LEAVE_RET('i', -1);
+               LOG("FD%i is a directory", FD);
+               return -1;
        }
 
        if(!h->Node->Type || !h->Node->Type->Read) {
-               LOG("No read method");
-               LEAVE_RET('i', -1);
+               LOG("FD%i has no read method", FD);
+               return -1;
        }
 
        if( !MM_GetPhysAddr(h->Node->Type->Read) ) {
-               Log_Error("VFS", "Node type %p(%s) read method is junk %p", h->Node->Type, h->Node, h->Node->Type->TypeName,
+               Log_Error("VFS", "Node type %p(%s) read method is junk %p",
+                       h->Node->Type, h->Node, h->Node->Type->TypeName,
                        h->Node->Type->Read);
-               LEAVE_RET('i', -1);
+               return -1;
        }
        
-       LOG("Position=%llx", h->Position);
-       ret = h->Node->Type->Read(h->Node, h->Position, Length, Buffer);
-       if(ret == (size_t)-1)   LEAVE_RET('i', -1);
+       Uint    flags = 0;
+       flags |= (h->Mode & VFS_OPENFLAG_NONBLOCK) ? VFS_IOFLAG_NOBLOCK : 0;
+       ret = h->Node->Type->Read(h->Node, h->Position, Length, Buffer, flags);
+       if(ret != Length)       LOG("%i/%i read", ret, Length);
+       if(ret == (size_t)-1)   return -1;
        
        h->Position += ret;
-       LEAVE('x', ret);
        return ret;
 }
 
@@ -76,12 +81,15 @@ size_t VFS_ReadAt(int FD, Uint64 Offset, size_t Length, void *Buffer)
        }
 
        if( !MM_GetPhysAddr(h->Node->Type->Read) ) {
-               Log_Error("VFS", "Node type %p(%s) read method is junk %p", h->Node->Type, h->Node, h->Node->Type->TypeName,
+               Log_Error("VFS", "Node type %p(%s) read method is junk %p",
+                       h->Node->Type, h->Node->Type->TypeName,
                        h->Node->Type->Read);
-               LEAVE_RET('i', -1);
        }
        
-       ret = h->Node->Type->Read(h->Node, Offset, Length, Buffer);
+       Uint    flags = 0;
+       flags |= (h->Mode & VFS_OPENFLAG_NONBLOCK) ? VFS_IOFLAG_NOBLOCK : 0;
+       ret = h->Node->Type->Read(h->Node, Offset, Length, Buffer, flags);
+       if(ret != Length)       LOG("%i/%i read", ret, Length);
        if(ret == (size_t)-1)   return -1;
        return ret;
 }
@@ -96,20 +104,41 @@ size_t VFS_Write(int FD, size_t Length, const void *Buffer)
        size_t  ret;
        
        h = VFS_GetHandle(FD);
-       if(!h)  return -1;
+       if(!h) {
+               LOG("FD%i is not open", FD);
+               errno = EBADF;
+               return -1;
+       }
        
-       if( !(h->Mode & VFS_OPENFLAG_WRITE) )   return -1;
-       if( h->Node->Flags & VFS_FFLAG_DIRECTORY )      return -1;
+       if( !(h->Mode & VFS_OPENFLAG_WRITE) ) {
+               LOG("FD%i not opened for writing", FD);
+               errno = EBADF;
+               return -1;
+       }
+       if( h->Node->Flags & VFS_FFLAG_DIRECTORY ) {
+               LOG("FD%i is a directory", FD);
+               errno = EISDIR;
+               return -1;
+       }
 
-       if( !h->Node->Type || !h->Node->Type->Write )   return 0;
+       if( !h->Node->Type || !h->Node->Type->Write ) {
+               LOG("FD%i has no write method", FD);
+               errno = EINTERNAL;
+               return 0;
+       }
 
        if( !MM_GetPhysAddr(h->Node->Type->Write) ) {
-               Log_Error("VFS", "Node type %p(%s) write method is junk %p", h->Node->Type, h->Node, h->Node->Type->TypeName,
+               Log_Error("VFS", "Node type %p(%s) write method is junk %p",
+                       h->Node->Type, h->Node->Type->TypeName,
                        h->Node->Type->Write);
+               errno = EINTERNAL;
                return -1;
        }
        
-       ret = h->Node->Type->Write(h->Node, h->Position, Length, Buffer);
+       Uint flags = 0;
+       flags |= (h->Mode & VFS_OPENFLAG_NONBLOCK) ? VFS_IOFLAG_NOBLOCK : 0;
+       ret = h->Node->Type->Write(h->Node, h->Position, Length, Buffer, flags);
+       if(ret != Length)       LOG("%i/%i written", ret, Length);
        if(ret == (size_t)-1)   return -1;
 
        h->Position += ret;
@@ -134,11 +163,14 @@ size_t VFS_WriteAt(int FD, Uint64 Offset, size_t Length, const void *Buffer)
        if(!h->Node->Type || !h->Node->Type->Write)     return 0;
 
        if( !MM_GetPhysAddr(h->Node->Type->Write) ) {
-               Log_Error("VFS", "Node type %p(%s) write method is junk %p", h->Node->Type, h->Node, h->Node->Type->TypeName,
+               Log_Error("VFS", "Node type %p(%s) write method is junk %p",
+                       h->Node->Type, h->Node->Type->TypeName,
                        h->Node->Type->Write);
                return -1;
        }
-       ret = h->Node->Type->Write(h->Node, Offset, Length, Buffer);
+       Uint flags = 0;
+       flags |= (h->Mode & VFS_OPENFLAG_NONBLOCK) ? VFS_IOFLAG_NOBLOCK : 0;
+       ret = h->Node->Type->Write(h->Node, Offset, Length, Buffer, flags);
        if(ret == (size_t)-1)   return -1;
        return ret;
 }
@@ -202,9 +234,17 @@ int VFS_IOCtl(int FD, int ID, void *Buffer)
        tVFS_Handle     *h;
        
        h = VFS_GetHandle(FD);
-       if(!h)  return -1;
+       if(!h) {
+               LOG("FD%i is invalid", FD);
+               errno = EINVAL;
+               return -1;
+       }
 
-       if(!h->Node->Type || !h->Node->Type->IOCtl)     return -1;
+       if(!h->Node->Type || !h->Node->Type->IOCtl) {
+               LOG("FD%i does not have an IOCtl method");
+               errno = EINVAL;
+               return -1;
+       }
        return h->Node->Type->IOCtl(h->Node, ID, Buffer);
 }
 

UCC git Repository :: git.ucc.asn.au