diff --git a/libc/Makefile b/libc/Makefile
index 895aa547..cdf88916 100644
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -260,6 +260,7 @@ lstat.o \
memstat.o \
mkdirat.o \
mkdir.o \
+mkpartition.o \
mktemp.o \
netdb/endhostent.o \
netdb/endnetent.o \
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 3dcdd5bd..43454961 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -368,6 +368,7 @@ int execvpe(const char*, char* const [], char* const []);
int getdtablesize(void);
size_t getpagesize(void);
int memstat(size_t* memused, size_t* memtotal);
+int mkpartition(int fd, off_t start, off_t length);
size_t preadall(int fd, void* buf, size_t count, off_t off);
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off);
size_t pwriteall(int fd, const void* buf, size_t count, off_t off);
diff --git a/libc/mkpartition.cpp b/libc/mkpartition.cpp
new file mode 100644
index 00000000..83497316
--- /dev/null
+++ b/libc/mkpartition.cpp
@@ -0,0 +1,34 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2013.
+
+ This file is part of the Sortix C Library.
+
+ The Sortix C Library is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or (at your
+ option) any later version.
+
+ The Sortix C Library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with the Sortix C Library. If not, see .
+
+ mkpartition.cpp
+ Creates a block device representating a partition of another block device.
+
+*******************************************************************************/
+
+#include
+
+#include
+
+DEFN_SYSCALL3(int, sys_mkpartition, SYSCALL_MKPARTITION, int, off_t, off_t);
+
+extern "C" int mkpartition(int fd, off_t start, off_t length)
+{
+ return sys_mkpartition(fd, start, length);
+}
diff --git a/sortix/Makefile b/sortix/Makefile
index 3707280d..329e27f6 100644
--- a/sortix/Makefile
+++ b/sortix/Makefile
@@ -113,6 +113,7 @@ memorymanagement.o \
mtable.o \
net/fs.o \
panic.o \
+partition.o \
pci.o \
pipe.o \
poll.o \
diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h
index 0bff873b..2442a619 100644
--- a/sortix/include/sortix/syscallnum.h
+++ b/sortix/include/sortix/syscallnum.h
@@ -132,6 +132,7 @@
#define SYSCALL_FCHDIRAT 108
#define SYSCALL_FCHROOT 109
#define SYSCALL_FCHROOTAT 110
-#define SYSCALL_MAX_NUM 111 /* index of highest constant + 1 */
+#define SYSCALL_MKPARTITION 111
+#define SYSCALL_MAX_NUM 112 /* index of highest constant + 1 */
#endif
diff --git a/sortix/io.cpp b/sortix/io.cpp
index 34f3fbba..031c5c1a 100644
--- a/sortix/io.cpp
+++ b/sortix/io.cpp
@@ -33,6 +33,7 @@
#include
#include
#include
+#include
#include
#include
@@ -45,6 +46,7 @@
#include
#include "io.h"
+#include "partition.h"
namespace Sortix {
namespace IO {
@@ -886,6 +888,38 @@ static ssize_t sys_pwritev(int fd, const struct iovec* user_iov, int iovcnt,
return so_far;
}
+static int sys_mkpartition(int fd, off_t start, off_t length, int flags)
+{
+ int fdflags = 0;
+ if ( flags & O_CLOEXEC ) fdflags |= FD_CLOEXEC;
+ if ( flags & O_CLOFORK ) fdflags |= FD_CLOFORK;
+
+ Ref desc = CurrentProcess()->GetDescriptor(fd);
+ if ( !desc )
+ return -1;
+
+ int dflags = desc->dflags;
+ Ref inner_inode = desc->vnode->inode;
+ desc.Reset();
+
+ Ref partition(new Partition(inner_inode, start, length));
+ if ( !partition )
+ return -1;
+ inner_inode.Reset();
+
+ Ref partition_vnode(new Vnode(partition, Ref(NULL), 0, 0));
+ if ( !partition_vnode )
+ return -1;
+ partition.Reset();
+
+ Ref partition_desc(new Descriptor(partition_vnode, dflags));
+ if ( !partition_desc )
+ return -1;
+ partition_vnode.Reset();
+
+ return CurrentProcess()->GetDTable()->Allocate(partition_desc, fdflags);
+}
+
void Init()
{
Syscall::Register(SYSCALL_ACCEPT4, (void*) sys_accept4);
@@ -921,6 +955,7 @@ void Init()
Syscall::Register(SYSCALL_LISTEN, (void*) sys_listen);
Syscall::Register(SYSCALL_MKDIRAT, (void*) sys_mkdirat);
Syscall::Register(SYSCALL_MKDIR, (void*) sys_mkdir);
+ Syscall::Register(SYSCALL_MKPARTITION, (void*) sys_mkpartition);
Syscall::Register(SYSCALL_OPENAT, (void*) sys_openat);
Syscall::Register(SYSCALL_OPEN, (void*) sys_open);
Syscall::Register(SYSCALL_PREAD, (void*) sys_pread);
diff --git a/sortix/partition.cpp b/sortix/partition.cpp
new file mode 100644
index 00000000..562bf251
--- /dev/null
+++ b/sortix/partition.cpp
@@ -0,0 +1,101 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2013.
+
+ This file is part of Sortix.
+
+ Sortix is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ Sortix. If not, see .
+
+ partition.cpp
+ Block device representing a partition of another block device.
+
+*******************************************************************************/
+
+#include
+
+#include
+
+#include
+
+#include
+#include
+#include
+
+#include "partition.h"
+
+namespace Sortix {
+
+Partition::Partition(Ref inner_inode, off_t start, off_t length)
+{
+ this->dev = (dev_t) this;
+ this->ino = (ino_t) this;
+ this->inode_type = INODE_TYPE_FILE;
+ this->inner_inode = inner_inode;
+ this->start = start;
+ this->length = length;
+ this->stat_size = length;
+}
+
+Partition::~Partition()
+{
+}
+
+int Partition::sync(ioctx_t* ctx)
+{
+ return inner_inode->sync(ctx);
+}
+
+int Partition::truncate(ioctx_t* /*ctx*/, off_t new_length)
+{
+ if ( new_length != length )
+ return errno = EPERM, -1;
+ return 0;
+}
+
+off_t Partition::lseek(ioctx_t* /*ctx*/, off_t offset, int whence)
+{
+ if ( whence == SEEK_SET )
+ return offset;
+ if ( whence == SEEK_END )
+ // TODO: Avoid underflow and overflow!
+ return length + offset;
+ return errno = EINVAL, -1;
+}
+
+ssize_t Partition::pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off)
+{
+ // TODO: Avoid underflow and overflow!
+ off_t end_at = off + count;
+ if ( length <= end_at )
+ return 0;
+ off_t available = length - off;
+ if ( (uintmax_t) available < (uintmax_t) count )
+ count = available;
+ return inner_inode->pread(ctx, buf, count, start + off);
+}
+
+ssize_t Partition::pwrite(ioctx_t* ctx, const uint8_t* buf, size_t count,
+ off_t off)
+{
+ // TODO: Avoid underflow and overflow!
+ off_t end_at = off + count;
+ if ( length <= end_at )
+ return 0;
+ off_t available = length - off;
+ if ( (uintmax_t) available < (uintmax_t) count )
+ count = available;
+ return inner_inode->pwrite(ctx, buf, count, start + off);
+}
+
+}
diff --git a/sortix/partition.h b/sortix/partition.h
new file mode 100644
index 00000000..eec26e1d
--- /dev/null
+++ b/sortix/partition.h
@@ -0,0 +1,56 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2013.
+
+ This file is part of Sortix.
+
+ Sortix is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ Sortix. If not, see .
+
+ partition.h
+ Block device representing a partition of another block device.
+
+*******************************************************************************/
+
+#ifndef PARTITION_H
+#define PARTITION_H
+
+#include
+#include
+
+namespace Sortix {
+
+class Partition : public AbstractInode
+{
+public:
+ Partition(Ref inner_inode, off_t start, off_t length);
+ virtual ~Partition();
+
+private:
+ Ref inner_inode;
+ off_t start;
+ off_t length;
+
+public:
+ virtual int sync(ioctx_t* ctx);
+ virtual int truncate(ioctx_t* ctx, off_t length);
+ virtual off_t lseek(ioctx_t* ctx, off_t offset, int whence);
+ virtual ssize_t pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off);
+ virtual ssize_t pwrite(ioctx_t* ctx, const uint8_t* buf, size_t count,
+ off_t off);
+
+};
+
+} // namespace Sortix
+
+#endif