Add mkpartition(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-06-01 13:24:27 +02:00
parent 94cd049473
commit 5d1fe2620b
8 changed files with 231 additions and 1 deletions

View File

@ -260,6 +260,7 @@ lstat.o \
memstat.o \
mkdirat.o \
mkdir.o \
mkpartition.o \
mktemp.o \
netdb/endhostent.o \
netdb/endnetent.o \

View File

@ -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);

34
libc/mkpartition.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
mkpartition.cpp
Creates a block device representating a partition of another block device.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
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);
}

View File

@ -113,6 +113,7 @@ memorymanagement.o \
mtable.o \
net/fs.o \
panic.o \
partition.o \
pci.o \
pipe.o \
poll.o \

View File

@ -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

View File

@ -33,6 +33,7 @@
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/process.h>
#include <sortix/kernel/thread.h>
#include <sortix/kernel/vnode.h>
#include <sortix/seek.h>
#include <sortix/dirent.h>
@ -45,6 +46,7 @@
#include <errno.h>
#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<Descriptor> desc = CurrentProcess()->GetDescriptor(fd);
if ( !desc )
return -1;
int dflags = desc->dflags;
Ref<Inode> inner_inode = desc->vnode->inode;
desc.Reset();
Ref<Inode> partition(new Partition(inner_inode, start, length));
if ( !partition )
return -1;
inner_inode.Reset();
Ref<Vnode> partition_vnode(new Vnode(partition, Ref<Vnode>(NULL), 0, 0));
if ( !partition_vnode )
return -1;
partition.Reset();
Ref<Descriptor> 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);

101
sortix/partition.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
partition.cpp
Block device representing a partition of another block device.
*******************************************************************************/
#include <sys/types.h>
#include <errno.h>
#include <sortix/seek.h>
#include <sortix/kernel/platform.h>
#include <sortix/kernel/refcount.h>
#include <sortix/kernel/inode.h>
#include "partition.h"
namespace Sortix {
Partition::Partition(Ref<Inode> 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);
}
}

56
sortix/partition.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
partition.h
Block device representing a partition of another block device.
*******************************************************************************/
#ifndef PARTITION_H
#define PARTITION_H
#include <sortix/kernel/refcount.h>
#include <sortix/kernel/inode.h>
namespace Sortix {
class Partition : public AbstractInode
{
public:
Partition(Ref<Inode> inner_inode, off_t start, off_t length);
virtual ~Partition();
private:
Ref<Inode> 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