2003-06-27 07:44:28 +04:00
|
|
|
/*
|
|
|
|
** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
|
|
|
** Distributed under the terms of the OpenBeOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <KernelExport.h>
|
|
|
|
#include <NodeMonitor.h>
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
#include <util/kernel_cpp.h>
|
|
|
|
#include <cbuf.h>
|
2003-06-27 07:44:28 +04:00
|
|
|
#include <vfs.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <khash.h>
|
|
|
|
#include <lock.h>
|
|
|
|
#include <vm.h>
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "builtin_fs.h"
|
|
|
|
|
|
|
|
// ToDo: handles file names suboptimally - it has all file names
|
|
|
|
// in a single linked list, no hash lookups or whatever.
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
#define PIPEFS_TRACE 0
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
#if PIPEFS_TRACE
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x)
|
|
|
|
#endif
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
|
|
|
|
namespace pipefs {
|
|
|
|
|
|
|
|
class Volume;
|
|
|
|
class Inode;
|
|
|
|
struct dir_cookie;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
struct read_request {
|
2003-06-28 08:38:04 +04:00
|
|
|
read_request *prev;
|
2003-06-28 03:04:36 +04:00
|
|
|
read_request *next;
|
|
|
|
void *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
size_t bytes_read;
|
2003-06-29 22:29:36 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
size_t SpaceLeft() const { return buffer_size - bytes_read; }
|
2003-06-29 22:29:36 +04:00
|
|
|
void Fill(Inode *inode);
|
2003-06-28 08:38:04 +04:00
|
|
|
status_t PutBuffer(const void **_buffer, size_t *_bufferSize);
|
2003-06-28 03:04:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ReadRequests {
|
|
|
|
public:
|
|
|
|
ReadRequests();
|
|
|
|
~ReadRequests();
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2003-06-28 03:04:36 +04:00
|
|
|
|
|
|
|
status_t Add(read_request &request);
|
|
|
|
status_t Remove(read_request &request);
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
read_request *GetCurrent() const { return fCurrent; }
|
|
|
|
void SkipCurrent() { if (fCurrent) fCurrent = fCurrent->next; }
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
private:
|
2003-06-28 08:38:04 +04:00
|
|
|
mutex fLock;
|
2003-06-28 03:04:36 +04:00
|
|
|
read_request *fFirst, *fCurrent, *fLast;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Volume {
|
|
|
|
public:
|
|
|
|
Volume(mount_id id);
|
|
|
|
~Volume();
|
|
|
|
|
|
|
|
status_t InitCheck();
|
|
|
|
mount_id ID() const { return fID; }
|
|
|
|
Inode &RootNode() const { return *fRootNode; }
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
|
|
|
|
|
|
|
Inode *Lookup(vnode_id id);
|
|
|
|
Inode *FindNode(const char *name);
|
|
|
|
Inode *CreateNode(const char *name, int32 type);
|
|
|
|
status_t DeleteNode(Inode *inode, bool forceDelete = false);
|
|
|
|
status_t RemoveNode(Inode *directory, const char *name);
|
|
|
|
|
|
|
|
vnode_id GetNextNodeID() { return fNextNodeID++; }
|
|
|
|
ReadRequests &GetReadRequests() { return fReadRequests; }
|
|
|
|
|
|
|
|
Inode *FirstEntry() const { return fFirstEntry; }
|
|
|
|
|
|
|
|
dir_cookie *FirstDirCookie() const { return fFirstDirCookie; }
|
|
|
|
void InsertCookie(dir_cookie *cookie);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RemoveCookie(dir_cookie *cookie);
|
|
|
|
void UpdateDirCookies(Inode *inode);
|
|
|
|
|
|
|
|
status_t RemoveNode(Inode *inode);
|
|
|
|
status_t InsertNode(Inode *inode);
|
|
|
|
|
|
|
|
ReadRequests fReadRequests;
|
|
|
|
mutex fLock;
|
|
|
|
mount_id fID;
|
|
|
|
Inode *fRootNode;
|
|
|
|
vnode_id fNextNodeID;
|
|
|
|
hash_table *fNodeHash;
|
|
|
|
|
|
|
|
// root directory contents - we don't support other directories
|
|
|
|
Inode *fFirstEntry;
|
|
|
|
dir_cookie *fFirstDirCookie;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Inode {
|
|
|
|
public:
|
|
|
|
Inode(Volume *volume, const char *name, int32 type);
|
|
|
|
~Inode();
|
|
|
|
|
|
|
|
status_t InitCheck();
|
|
|
|
vnode_id ID() const { return fID; }
|
|
|
|
const char *Name() const { return fName; }
|
|
|
|
int32 Type() const { return fType; }
|
|
|
|
|
|
|
|
Inode *Next() const { return fNext; }
|
|
|
|
void SetNext(Inode *inode) { fNext = inode; }
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
sem_id ReadLock() { return fReadLock; }
|
|
|
|
mutex *WriteMutex() { return &fWriteMutex; }
|
2003-06-28 03:04:36 +04:00
|
|
|
|
2003-06-29 22:29:36 +04:00
|
|
|
status_t WriteBufferToChain(const void *buffer, size_t bufferSize);
|
|
|
|
status_t ReadBufferFromChain(void *buffer, size_t *_bufferSize);
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
static int32 HashNextOffset();
|
|
|
|
static uint32 hash_func(void *_node, const void *_key, uint32 range);
|
|
|
|
static int compare_func(void *_node, const void *_key);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Inode *fNext;
|
|
|
|
Inode *fHashNext;
|
|
|
|
vnode_id fID;
|
|
|
|
int32 fType;
|
|
|
|
const char *fName;
|
|
|
|
|
2003-06-29 22:29:36 +04:00
|
|
|
cbuf *fBufferChain;
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
sem_id fReadLock;
|
|
|
|
mutex fWriteMutex;
|
2003-06-27 07:44:28 +04:00
|
|
|
};
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
|
2003-06-27 07:44:28 +04:00
|
|
|
struct dir_cookie {
|
|
|
|
dir_cookie *next;
|
|
|
|
dir_cookie *prev;
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *current;
|
2003-06-27 07:44:28 +04:00
|
|
|
};
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
struct file_cookie {
|
|
|
|
int open_mode;
|
2003-06-27 07:44:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define PIPEFS_HASH_SIZE 16
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume::Volume(mount_id id)
|
|
|
|
:
|
|
|
|
fID(id),
|
|
|
|
fRootNode(NULL),
|
|
|
|
fNextNodeID(0),
|
|
|
|
fNodeHash(NULL),
|
|
|
|
fFirstEntry(NULL),
|
|
|
|
fFirstDirCookie(NULL)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
if (mutex_init(&fLock, "pipefs_mutex") < B_OK)
|
|
|
|
return;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
fNodeHash = hash_init(PIPEFS_HASH_SIZE, Inode::HashNextOffset(),
|
|
|
|
&Inode::compare_func, &Inode::hash_func);
|
|
|
|
if (fNodeHash == NULL)
|
|
|
|
return;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
// create the root vnode
|
|
|
|
fRootNode = CreateNode("", S_IFDIR);
|
|
|
|
if (fRootNode == NULL)
|
|
|
|
return;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume::~Volume()
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
// put_vnode on the root to release the ref to it
|
|
|
|
if (fRootNode)
|
|
|
|
vfs_put_vnode(ID(), fRootNode->ID());
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (fNodeHash != NULL) {
|
|
|
|
// delete all of the inodes
|
|
|
|
struct hash_iterator i;
|
|
|
|
hash_open(fNodeHash, &i);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode;
|
|
|
|
while ((inode = (Inode *)hash_next(fNodeHash, &i)) != NULL) {
|
|
|
|
DeleteNode(inode, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_close(fNodeHash, &i, false);
|
|
|
|
|
|
|
|
hash_uninit(fNodeHash);
|
|
|
|
}
|
|
|
|
mutex_destroy(&fLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
Volume::InitCheck()
|
|
|
|
{
|
|
|
|
if (fLock.sem < B_OK
|
|
|
|
|| fRootNode == NULL)
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Volume::Lock()
|
|
|
|
{
|
|
|
|
mutex_lock(&fLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Volume::Unlock()
|
|
|
|
{
|
|
|
|
mutex_unlock(&fLock);
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *
|
|
|
|
Volume::CreateNode(const char *name, int32 type)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = new Inode(this, name, type);
|
|
|
|
if (inode == NULL)
|
2003-06-27 07:44:28 +04:00
|
|
|
return NULL;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (inode->InitCheck() != B_OK) {
|
|
|
|
delete inode;
|
2003-06-27 07:44:28 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (type == S_IFIFO)
|
|
|
|
InsertNode(inode);
|
2003-06-28 08:38:04 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
hash_insert(fNodeHash, inode);
|
2003-06-28 08:38:04 +04:00
|
|
|
vfs_new_vnode(ID(), inode->ID(), inode);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return inode;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status_t
|
|
|
|
Volume::DeleteNode(Inode *inode, bool forceDelete)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
// remove it from the global hash table
|
2003-06-28 03:04:36 +04:00
|
|
|
hash_remove(fNodeHash, inode);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
delete inode;
|
2003-06-27 07:44:28 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
void
|
|
|
|
Volume::InsertCookie(dir_cookie *cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
cookie->next = fFirstDirCookie;
|
|
|
|
fFirstDirCookie = cookie;
|
2003-06-27 07:44:28 +04:00
|
|
|
cookie->prev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
void
|
|
|
|
Volume::RemoveCookie(dir_cookie *cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
if (cookie->next)
|
|
|
|
cookie->next->prev = cookie->prev;
|
|
|
|
|
|
|
|
if (cookie->prev)
|
|
|
|
cookie->prev->next = cookie->next;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (fFirstDirCookie == cookie)
|
|
|
|
fFirstDirCookie = cookie->next;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
cookie->prev = cookie->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* makes sure none of the dircookies point to the vnode passed in */
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
void
|
|
|
|
Volume::UpdateDirCookies(Inode *inode)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
dir_cookie *cookie;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
for (cookie = fFirstDirCookie; cookie; cookie = cookie->next) {
|
|
|
|
if (cookie->current == inode)
|
|
|
|
cookie->current = inode->Next();
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *
|
|
|
|
Volume::Lookup(vnode_id id)
|
|
|
|
{
|
|
|
|
return (Inode *)hash_lookup(fNodeHash, &id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Inode *
|
|
|
|
Volume::FindNode(const char *name)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
if (!strcmp(name, ".")
|
|
|
|
|| !strcmp(name, ".."))
|
2003-06-28 03:04:36 +04:00
|
|
|
return fRootNode;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
for (Inode *inode = fFirstEntry; inode; inode = inode->Next()) {
|
|
|
|
if (!strcmp(inode->Name(), name))
|
|
|
|
return inode;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status_t
|
|
|
|
Volume::InsertNode(Inode *inode)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
inode->SetNext(fFirstEntry);
|
|
|
|
fFirstEntry = inode;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status_t
|
|
|
|
Volume::RemoveNode(Inode *findNode)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode, *last;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
for (inode = fFirstEntry, last = NULL; inode; last = inode, inode = inode->Next()) {
|
|
|
|
if (inode == findNode) {
|
|
|
|
// make sure no dir cookies point to this node
|
|
|
|
UpdateDirCookies(inode);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
if (last)
|
2003-06-28 03:04:36 +04:00
|
|
|
last->SetNext(inode->Next());
|
2003-06-27 07:44:28 +04:00
|
|
|
else
|
2003-06-28 03:04:36 +04:00
|
|
|
fFirstEntry = inode->Next();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
inode->SetNext(NULL);
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status_t
|
|
|
|
Volume::RemoveNode(Inode *directory, const char *name)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status_t status = B_OK;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = FindNode(name);
|
|
|
|
if (inode == NULL)
|
2003-06-27 07:44:28 +04:00
|
|
|
status = B_ENTRY_NOT_FOUND;
|
2003-06-28 03:04:36 +04:00
|
|
|
else if (inode->Type() == S_IFDIR)
|
2003-06-27 07:44:28 +04:00
|
|
|
status = B_NOT_ALLOWED;
|
|
|
|
|
|
|
|
if (status < B_OK)
|
|
|
|
goto err;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
RemoveNode(inode);
|
|
|
|
notify_listener(B_ENTRY_REMOVED, ID(), directory->ID(), 0, inode->ID(), name);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
// schedule this vnode to be removed when it's ref goes to zero
|
2003-06-28 03:04:36 +04:00
|
|
|
vfs_remove_vnode(ID(), inode->ID());
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
err:
|
2003-06-28 03:04:36 +04:00
|
|
|
Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode::Inode(Volume *volume, const char *name, int32 type)
|
|
|
|
:
|
2003-06-28 08:38:04 +04:00
|
|
|
fNext(NULL),
|
2003-06-29 22:29:36 +04:00
|
|
|
fHashNext(NULL),
|
|
|
|
fBufferChain(NULL)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
fName = strdup(name);
|
|
|
|
if (fName == NULL)
|
|
|
|
return;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
fID = volume->GetNextNodeID();
|
|
|
|
fType = type;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
if (type == S_IFIFO) {
|
|
|
|
fReadLock = create_sem(0, "pipe read");
|
|
|
|
mutex_init(&fWriteMutex, "pipe write");
|
|
|
|
}
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode::~Inode()
|
|
|
|
{
|
|
|
|
free(const_cast<char *>(fName));
|
2003-06-28 08:38:04 +04:00
|
|
|
|
|
|
|
if (fType == S_IFIFO) {
|
|
|
|
delete_sem(fReadLock);
|
|
|
|
mutex_destroy(&fWriteMutex);
|
|
|
|
}
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status_t
|
|
|
|
Inode::InitCheck()
|
|
|
|
{
|
|
|
|
if (fName == NULL
|
2003-06-28 08:38:04 +04:00
|
|
|
|| fType == S_IFIFO && (fReadLock < B_OK || fWriteMutex.sem < B_OK))
|
2003-06-28 03:04:36 +04:00
|
|
|
return B_ERROR;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
|
2003-06-29 22:29:36 +04:00
|
|
|
/** Adds the specified buffer to the inode's buffer chain by copying
|
|
|
|
* its contents. The Inode::WriteLock() must be held when calling
|
|
|
|
* this method.
|
|
|
|
* Releases the reader sem if necessary, so that blocking
|
|
|
|
* readers will get started.
|
|
|
|
* Returns B_NO_MEMORY if the chain couldn't be allocated, B_BAD_ADDRESS
|
|
|
|
* if copying from the buffer failed, and B_OK on success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
|
|
|
Inode::WriteBufferToChain(const void *buffer, size_t bufferSize)
|
|
|
|
{
|
|
|
|
cbuf *chain = cbuf_get_chain(bufferSize);
|
|
|
|
if (chain == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
if (cbuf_user_memcpy_to_chain(chain, 0, buffer, bufferSize) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
// join this chain with our already existing chain (if any)
|
|
|
|
|
|
|
|
chain = cbuf_merge_chains(fBufferChain, chain);
|
|
|
|
|
|
|
|
// let waiting readers go on
|
|
|
|
if (fBufferChain == NULL)
|
|
|
|
release_sem(ReadLock());
|
|
|
|
|
|
|
|
fBufferChain = chain;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Reads the contents of the buffer chain to the specified
|
|
|
|
* buffer, if any.
|
|
|
|
* Unblocks other waiting readers if there would be still
|
|
|
|
* some bytes left in the stream.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
|
|
|
Inode::ReadBufferFromChain(void *buffer, size_t *_bufferSize)
|
|
|
|
{
|
|
|
|
if (fBufferChain == NULL) {
|
|
|
|
*_bufferSize = 0;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length = cbuf_get_length(fBufferChain);
|
|
|
|
|
|
|
|
// we read *_bufferSize bytes at maximum - but never
|
|
|
|
// more than there are in the chain
|
|
|
|
if (*_bufferSize > length)
|
|
|
|
*_bufferSize = length;
|
|
|
|
else {
|
|
|
|
length = *_bufferSize;
|
|
|
|
|
|
|
|
// we unlock another reader here, so that it can read
|
|
|
|
// the rest of the pending bytes
|
|
|
|
release_sem(ReadLock());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cbuf_user_memcpy_from_chain(buffer, fBufferChain, 0, length) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
if (cbuf_truncate_head(fBufferChain, length) < B_OK) {
|
|
|
|
// if that call fails, the next read will duplicate the input
|
|
|
|
dprintf("pipefs: cbuf_truncate_head() failed for inode %Ld\n", ID());
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
int32
|
|
|
|
Inode::HashNextOffset()
|
|
|
|
{
|
|
|
|
Inode *inode;
|
|
|
|
return (addr)&inode->fHashNext - (addr)inode;
|
|
|
|
}
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
uint32
|
|
|
|
Inode::hash_func(void *_node, const void *_key, uint32 range)
|
|
|
|
{
|
|
|
|
Inode *inode = (Inode *)_node;
|
|
|
|
const vnode_id *key = (const vnode_id *)_key;
|
|
|
|
|
|
|
|
if (inode != NULL)
|
|
|
|
return inode->ID() % range;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return (*key) % range;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
int
|
|
|
|
Inode::compare_func(void *_node, const void *_key)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = (Inode *)_node;
|
|
|
|
const vnode_id *key = (const vnode_id *)_key;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (inode->ID() == *key)
|
|
|
|
return 0;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2003-06-29 22:29:36 +04:00
|
|
|
void
|
|
|
|
read_request::Fill(Inode *inode)
|
2003-06-28 08:38:04 +04:00
|
|
|
{
|
2003-06-29 22:29:36 +04:00
|
|
|
size_t length = SpaceLeft();
|
|
|
|
|
|
|
|
if (inode->ReadBufferFromChain(buffer, &length) < B_OK) {
|
|
|
|
// ToDo: should add status to read_request
|
|
|
|
dprintf("reading from chain failed!");
|
|
|
|
} else
|
|
|
|
bytes_read += length;
|
2003-06-28 08:38:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
read_request::PutBuffer(const void **_buffer, size_t *_bufferSize)
|
|
|
|
{
|
|
|
|
TRACE(("pipefs: read_request::PutUserBuffer(buffer = %p, size = %lu)\n", *_buffer, *_bufferSize));
|
|
|
|
|
|
|
|
size_t bytes = *_bufferSize;
|
|
|
|
if (bytes > SpaceLeft())
|
|
|
|
bytes = SpaceLeft();
|
|
|
|
|
|
|
|
uint8 *source = (uint8 *)*_buffer;
|
|
|
|
|
|
|
|
if (user_memcpy((uint8 *)buffer + bytes_read, source, bytes) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
bytes_read += bytes;
|
|
|
|
*_buffer = (void *)(source + bytes);
|
|
|
|
*_bufferSize -= bytes;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
ReadRequests::ReadRequests()
|
|
|
|
:
|
|
|
|
fFirst(NULL),
|
|
|
|
fCurrent(NULL),
|
|
|
|
fLast(NULL)
|
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
mutex_init(&fLock, "pipefs read requests");
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReadRequests::~ReadRequests()
|
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
mutex_destroy(&fLock);
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
void
|
2003-06-28 03:04:36 +04:00
|
|
|
ReadRequests::Lock()
|
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
mutex_lock(&fLock);
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
void
|
2003-06-28 03:04:36 +04:00
|
|
|
ReadRequests::Unlock()
|
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
mutex_unlock(&fLock);
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
ReadRequests::Add(read_request &request)
|
|
|
|
{
|
|
|
|
if (fLast != NULL)
|
|
|
|
fLast->next = &request;
|
|
|
|
|
|
|
|
if (fFirst == NULL)
|
|
|
|
fFirst = &request;
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
// ToDo: could directly skip full requests
|
|
|
|
if (fCurrent == NULL)
|
|
|
|
fCurrent = &request;
|
|
|
|
|
|
|
|
request.prev = fLast;
|
2003-06-28 03:04:36 +04:00
|
|
|
request.next = NULL;
|
2003-06-28 08:38:04 +04:00
|
|
|
fLast = &request;
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
ReadRequests::Remove(read_request &request)
|
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
if (request.next != NULL)
|
|
|
|
request.next->prev = request.prev;
|
|
|
|
if (request.prev != NULL)
|
|
|
|
request.prev->next = request.next;
|
|
|
|
|
|
|
|
// update pointers
|
|
|
|
|
|
|
|
if (fCurrent == &request)
|
|
|
|
fCurrent = fCurrent->next;
|
|
|
|
|
|
|
|
if (fLast == &request)
|
|
|
|
fLast = request.prev;
|
|
|
|
|
|
|
|
if (fFirst == &request)
|
|
|
|
fFirst = request.next;
|
2003-06-28 03:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
pipefs_mount(mount_id id, const char *device, void *args, fs_volume *_volume, vnode_id *_rootVnodeID)
|
|
|
|
{
|
|
|
|
TRACE(("pipefs_mount: entry\n"));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = new Volume(id);
|
|
|
|
if (volume == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
status_t status = volume->InitCheck();
|
|
|
|
if (status < B_OK) {
|
|
|
|
delete volume;
|
|
|
|
return status;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
*_rootVnodeID = volume->RootNode().ID();
|
|
|
|
*_volume = volume;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
pipefs_unmount(fs_volume _volume)
|
|
|
|
{
|
|
|
|
struct Volume *volume = (struct Volume *)_volume;
|
|
|
|
|
|
|
|
TRACE(("pipefs_unmount: entry fs = %p\n", _volume));
|
|
|
|
delete volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
pipefs_sync(fs_volume fs)
|
|
|
|
{
|
|
|
|
TRACE(("pipefs_sync: entry\n"));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_lookup(fs_volume _volume, fs_vnode _dir, const char *name, vnode_id *_id, int *_type)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
status_t status;
|
|
|
|
|
|
|
|
TRACE(("pipefs_lookup: entry dir %p, name '%s'\n", _dir, name));
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *directory = (Inode *)_dir;
|
|
|
|
if (directory->Type() != S_IFDIR)
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_NOT_A_DIRECTORY;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
// look it up
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = volume->FindNode(name);
|
|
|
|
if (inode == NULL) {
|
2003-06-27 07:44:28 +04:00
|
|
|
status = B_ENTRY_NOT_FOUND;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *dummy;
|
|
|
|
status = vfs_get_vnode(volume->ID(), inode->ID(), (fs_vnode *)&dummy);
|
2003-06-27 07:44:28 +04:00
|
|
|
if (status < B_OK)
|
|
|
|
goto err;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
*_id = inode->ID();
|
|
|
|
*_type = inode->Type();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
err:
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_get_vnode_name(fs_volume _volume, fs_vnode _node, char *buffer, size_t bufferSize)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = (Inode *)_node;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_get_vnode_name(): inode = %p\n", inode));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
strlcpy(buffer, inode->Name(), bufferSize);
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_get_vnode(fs_volume _volume, vnode_id id, fs_vnode *_inode, bool reenter)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
TRACE(("pipefs_getvnode: asking for vnode 0x%Lx, r %d\n", id, reenter));
|
|
|
|
|
|
|
|
if (!reenter)
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
*_inode = volume->Lookup(id);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
if (!reenter)
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_getnvnode: looked it up at %p\n", *_inode));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (*_inode)
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_put_vnode(fs_volume _volume, fs_vnode _node, bool reenter)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-09-04 07:29:13 +04:00
|
|
|
#if TRACE_PIPEFS
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = (Inode *)_node;
|
|
|
|
TRACE(("pipefs_putvnode: entry on vnode 0x%Lx, r %d\n", inode->ID(), reenter));
|
2003-09-04 07:29:13 +04:00
|
|
|
#endif
|
2003-06-28 08:38:04 +04:00
|
|
|
|
|
|
|
// ToDo: delete pipe - it isn't needed anymore!
|
|
|
|
|
|
|
|
//delete inode;
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_remove_vnode(fs_volume _volume, fs_vnode _node, bool reenter)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
|
|
|
Inode *inode = (Inode *)_node;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_removevnode: remove %p (0x%Lx), r %d\n", inode, inode->ID(), reenter));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
if (!reenter)
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (inode->Next()) {
|
2003-06-27 07:44:28 +04:00
|
|
|
// can't remove node if it's linked to the dir
|
2003-06-28 03:04:36 +04:00
|
|
|
panic("pipefs_remove_vnode(): vnode %p asked to be removed is present in dir\n", inode);
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->DeleteNode(inode);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
if (!reenter)
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 08:38:04 +04:00
|
|
|
pipefs_create(fs_volume _volume, fs_vnode _dir, const char *name, int openMode, int perms,
|
2003-06-28 03:04:36 +04:00
|
|
|
fs_cookie *_cookie, vnode_id *_newVnodeID)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
TRACE(("pipefs_create(): dir = %p, name = '%s', perms = %d, &id = %p\n",
|
2003-06-28 03:04:36 +04:00
|
|
|
_dir, name, perms, _newVnodeID));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
file_cookie *cookie = (file_cookie *)malloc(sizeof(file_cookie));
|
|
|
|
if (cookie == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *directory = (Inode *)_dir;
|
2003-06-27 07:44:28 +04:00
|
|
|
status_t status = B_OK;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = volume->FindNode(name);
|
|
|
|
if (inode != NULL) {
|
2003-06-27 07:44:28 +04:00
|
|
|
status = B_FILE_EXISTS;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
inode = volume->CreateNode(name, S_IFIFO);
|
|
|
|
if (inode == NULL) {
|
2003-06-27 07:44:28 +04:00
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
cookie->open_mode = openMode;
|
|
|
|
|
|
|
|
*_cookie = (void *)cookie;
|
|
|
|
*_newVnodeID = inode->ID();
|
2003-06-28 03:04:36 +04:00
|
|
|
notify_listener(B_ENTRY_CREATED, volume->ID(), directory->ID(), 0, inode->ID(), name);
|
2003-06-28 08:38:04 +04:00
|
|
|
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
err:
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-28 08:38:04 +04:00
|
|
|
free(cookie);
|
|
|
|
|
2003-06-27 07:44:28 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 08:38:04 +04:00
|
|
|
pipefs_open(fs_volume _volume, fs_vnode _node, int openMode, fs_cookie *_cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
// allow to open the file, but it can't be done anything with it
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
file_cookie *cookie = (file_cookie *)malloc(sizeof(file_cookie));
|
|
|
|
if (cookie == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
cookie->open_mode = openMode;
|
|
|
|
|
|
|
|
*_cookie = (void *)cookie;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_close(fs_volume _volume, fs_vnode _vnode, fs_cookie _cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
TRACE(("pipefs_close: entry vnode %p, cookie %p\n", _vnode, _cookie));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 08:38:04 +04:00
|
|
|
pipefs_free_cookie(fs_volume _volume, fs_vnode _node, fs_cookie _cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
file_cookie *cookie = (file_cookie *)_cookie;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
TRACE(("pipefs_freecookie: entry vnode %p, cookie %p\n", _node, _cookie));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
free(cookie);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_fsync(fs_volume _volume, fs_vnode _v)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_read(fs_volume _volume, fs_vnode _node, fs_cookie _cookie, off_t pos,
|
2003-06-27 07:44:28 +04:00
|
|
|
void *buffer, size_t *_length)
|
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
file_cookie *cookie = (file_cookie *)_cookie;
|
2003-06-28 08:38:04 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
(void)pos;
|
|
|
|
TRACE(("pipefs_read: vnode %p, cookie %p, pos 0x%Lx , len 0x%lx\n", _node, cookie, pos, *_length));
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
if ((cookie->open_mode & O_RWMASK) != O_RDONLY)
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
|
|
|
// issue read request
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
read_request request;
|
|
|
|
request.buffer = buffer;
|
|
|
|
request.buffer_size = *_length;
|
|
|
|
request.bytes_read = 0;
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
ReadRequests &requests = volume->GetReadRequests();
|
2003-06-28 03:04:36 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
requests.Lock();
|
2003-06-28 03:04:36 +04:00
|
|
|
requests.Add(request);
|
|
|
|
requests.Unlock();
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
// ToDo: here is the race condition that another reader issues
|
|
|
|
// its read request after this one, but locks earlier; this
|
|
|
|
// will currently lead to a dead-lock or failure - that could
|
|
|
|
// be solved by attaching a thread to the request
|
|
|
|
|
|
|
|
// wait for it to be filled
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = (Inode *)_node;
|
2003-06-28 08:38:04 +04:00
|
|
|
status_t status = acquire_sem_etc(inode->ReadLock(), 1,
|
|
|
|
(cookie->open_mode & O_NONBLOCK ? B_TIMEOUT : 0 ) | B_CAN_INTERRUPT, 0);
|
|
|
|
|
|
|
|
requests.Lock();
|
2003-06-28 03:04:36 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
if (status == B_OK)
|
2003-06-29 22:29:36 +04:00
|
|
|
request.Fill(inode);
|
2003-06-28 03:04:36 +04:00
|
|
|
|
|
|
|
requests.Remove(request);
|
|
|
|
requests.Unlock();
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
if (status == B_TIMED_OUT || B_INTERRUPTED && request.bytes_read > 0)
|
2003-06-28 03:04:36 +04:00
|
|
|
status = B_OK;
|
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
if (status == B_OK)
|
2003-06-28 03:04:36 +04:00
|
|
|
*_length = request.bytes_read;
|
2003-06-28 08:38:04 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return status;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
2003-06-28 08:38:04 +04:00
|
|
|
pipefs_write(fs_volume _volume, fs_vnode _node, fs_cookie _cookie, off_t pos,
|
2003-06-27 07:44:28 +04:00
|
|
|
const void *buffer, size_t *_length)
|
|
|
|
{
|
2003-06-28 08:38:04 +04:00
|
|
|
file_cookie *cookie = (file_cookie *)_cookie;
|
|
|
|
Volume *volume = (Volume *)_volume;
|
|
|
|
Inode *inode = (Inode *)_node;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_write: vnode %p, cookie %p, pos 0x%Lx , len 0x%lx\n", _node, cookie, pos, *_length));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
if ((cookie->open_mode & O_RWMASK) != O_WRONLY)
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
|
|
|
mutex_lock(inode->WriteMutex());
|
|
|
|
|
|
|
|
ReadRequests &requests = volume->GetReadRequests();
|
|
|
|
requests.Lock();
|
|
|
|
|
|
|
|
size_t bytesLeft = *_length;
|
|
|
|
read_request *request = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
request = requests.GetCurrent();
|
|
|
|
if (request != NULL) {
|
|
|
|
// fill this request
|
|
|
|
|
2003-06-29 22:29:36 +04:00
|
|
|
request->Fill(inode);
|
2003-06-28 08:38:04 +04:00
|
|
|
if (request->SpaceLeft() > 0) {
|
|
|
|
// place our data into that buffer
|
|
|
|
request->PutBuffer(&buffer, &bytesLeft);
|
|
|
|
if (bytesLeft == 0) {
|
|
|
|
release_sem(inode->ReadLock());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
requests.SkipCurrent();
|
|
|
|
}
|
|
|
|
} while (request != NULL);
|
|
|
|
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
if (request == NULL) {
|
|
|
|
// there is no read request pending, so we have to put
|
|
|
|
// our data in a temporary buffer
|
|
|
|
|
2003-06-29 22:29:36 +04:00
|
|
|
status = inode->WriteBufferToChain(buffer, bytesLeft);
|
|
|
|
if (status != B_OK)
|
|
|
|
*_length -= bytesLeft;
|
2003-06-28 08:38:04 +04:00
|
|
|
} else {
|
|
|
|
// could write everything without the need to copy!
|
|
|
|
status = B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
requests.Unlock();
|
|
|
|
mutex_unlock(inode->WriteMutex());
|
|
|
|
|
|
|
|
return status;
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_create_dir(fs_volume _volume, fs_vnode _dir, const char *name, int perms, vnode_id *_newID)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
return ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_remove_dir(fs_volume _volume, fs_vnode _dir, const char *name)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_open_dir(fs_volume _volume, fs_vnode _node, fs_cookie *_cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_open_dir(): vnode = %p\n", _node));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
Inode *inode = (Inode *)_node;
|
|
|
|
if (inode->Type() != S_IFDIR)
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (inode != &volume->RootNode())
|
2003-06-27 07:44:28 +04:00
|
|
|
panic("pipefs: found directory that's not the root!");
|
|
|
|
|
|
|
|
dir_cookie *cookie = (dir_cookie *)malloc(sizeof(dir_cookie));
|
|
|
|
if (cookie == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
cookie->current = volume->FirstEntry();
|
|
|
|
volume->InsertCookie(cookie);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 08:38:04 +04:00
|
|
|
*_cookie = (void *)cookie;
|
|
|
|
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_read_dir(fs_volume _volume, fs_vnode _node, fs_cookie _cookie,
|
2003-06-27 07:44:28 +04:00
|
|
|
struct dirent *dirent, size_t bufferSize, uint32 *_num)
|
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
2003-06-27 07:44:28 +04:00
|
|
|
status_t status = 0;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_read_dir: vnode %p, cookie %p, buffer = %p, bufferSize = %ld, num = %p\n", _node, _cookie, dirent, bufferSize,_num));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
if (_node != &volume->RootNode())
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
dir_cookie *cookie = (dir_cookie *)_cookie;
|
|
|
|
if (cookie->current == NULL) {
|
|
|
|
// we're at the end of the directory
|
|
|
|
*_num = 0;
|
|
|
|
status = B_OK;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
dirent->d_dev = volume->ID();
|
|
|
|
dirent->d_ino = cookie->current->ID();
|
|
|
|
dirent->d_reclen = strlen(cookie->current->Name()) + sizeof(struct dirent);
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
if (dirent->d_reclen > bufferSize) {
|
|
|
|
status = ENOBUFS;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
status = user_strlcpy(dirent->d_name, cookie->current->Name(), bufferSize);
|
2003-06-27 07:44:28 +04:00
|
|
|
if (status < 0)
|
|
|
|
goto err;
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
cookie->current = cookie->current->Next();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
err:
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_rewind_dir(fs_volume _volume, fs_vnode _vnode, fs_cookie _cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
|
|
|
volume->Lock();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
dir_cookie *cookie = (dir_cookie *)_cookie;
|
2003-06-28 03:04:36 +04:00
|
|
|
cookie->current = volume->FirstEntry();
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
volume->Unlock();
|
2003-06-27 07:44:28 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_close_dir(fs_volume _volume, fs_vnode _node, fs_cookie _cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_close: entry vnode %p, cookie %p\n", _node, _cookie));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_free_dir_cookie(fs_volume _volume, fs_vnode _vnode, fs_cookie _cookie)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
dir_cookie *cookie = (dir_cookie *)_cookie;
|
|
|
|
|
|
|
|
TRACE(("pipefs_freecookie: entry vnode %p, cookie %p\n", _vnode, cookie));
|
|
|
|
|
|
|
|
free(cookie);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_ioctl(fs_volume _volume, fs_vnode _vnode, fs_cookie _cookie, ulong op,
|
2003-06-27 07:44:28 +04:00
|
|
|
void *buffer, size_t length)
|
|
|
|
{
|
|
|
|
TRACE(("pipefs_ioctl: vnode %p, cookie %p, op %ld, buf %p, len %ld\n",
|
|
|
|
_vnode, _cookie, op, buffer, length));
|
|
|
|
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_can_page(fs_volume _volume, fs_vnode _v)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_read_pages(fs_volume _volume, fs_vnode _v, iovecs *vecs, off_t pos)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_write_pages(fs_volume _volume, fs_vnode _v, iovecs *vecs, off_t pos)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_unlink(fs_volume _volume, fs_vnode _dir, const char *name)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
|
|
|
Inode *directory = (Inode *)_dir;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_unlink: dir %p (0x%Lx), name '%s'\n", _dir, directory->ID(), name));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return volume->RemoveNode(directory, name);
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2003-06-28 03:04:36 +04:00
|
|
|
pipefs_read_stat(fs_volume _volume, fs_vnode _node, struct stat *stat)
|
2003-06-27 07:44:28 +04:00
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
Volume *volume = (Volume *)_volume;
|
|
|
|
Inode *inode = (Inode *)_node;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("pipefs_rstat: vnode %p (0x%Lx), stat %p\n", inode, inode->ID(), stat));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
stat->st_dev = volume->ID();
|
|
|
|
stat->st_ino = inode->ID();
|
2003-06-27 07:44:28 +04:00
|
|
|
stat->st_size = 0; // ToDo: oughta get me!
|
2003-06-28 03:04:36 +04:00
|
|
|
stat->st_mode = inode->Type() | 0777;
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
static struct fs_ops pipefs_ops = {
|
|
|
|
&pipefs_mount,
|
|
|
|
&pipefs_unmount,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&pipefs_sync,
|
|
|
|
|
|
|
|
&pipefs_lookup,
|
|
|
|
&pipefs_get_vnode_name,
|
|
|
|
|
|
|
|
&pipefs_get_vnode,
|
|
|
|
&pipefs_put_vnode,
|
|
|
|
&pipefs_remove_vnode,
|
|
|
|
|
|
|
|
&pipefs_can_page,
|
|
|
|
&pipefs_read_pages,
|
|
|
|
&pipefs_write_pages,
|
|
|
|
|
|
|
|
/* common */
|
|
|
|
&pipefs_ioctl,
|
|
|
|
&pipefs_fsync,
|
|
|
|
|
|
|
|
NULL, // fs_read_link()
|
|
|
|
NULL, // fs_write_link()
|
|
|
|
NULL, // fs_symlink()
|
|
|
|
NULL, // fs_link()
|
|
|
|
&pipefs_unlink,
|
|
|
|
NULL, // fs_rename()
|
|
|
|
|
|
|
|
NULL, // fs_access()
|
|
|
|
&pipefs_read_stat,
|
|
|
|
NULL, // fs_write_stat()
|
|
|
|
|
|
|
|
/* file */
|
|
|
|
&pipefs_create,
|
|
|
|
&pipefs_open,
|
|
|
|
&pipefs_close,
|
|
|
|
&pipefs_free_cookie,
|
|
|
|
&pipefs_read,
|
|
|
|
&pipefs_write,
|
|
|
|
|
|
|
|
/* directory */
|
|
|
|
&pipefs_create_dir,
|
|
|
|
&pipefs_remove_dir,
|
|
|
|
&pipefs_open_dir,
|
|
|
|
&pipefs_close_dir,
|
|
|
|
&pipefs_free_dir_cookie,
|
|
|
|
&pipefs_read_dir,
|
|
|
|
&pipefs_rewind_dir,
|
|
|
|
|
|
|
|
// the other operations are not supported (attributes, indices, queries)
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
} // namespace pipefs
|
2003-06-27 07:44:28 +04:00
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
bootstrap_pipefs(void)
|
|
|
|
{
|
2003-06-28 03:04:36 +04:00
|
|
|
TRACE(("bootstrap_pipefs: entry\n"));
|
2003-06-27 07:44:28 +04:00
|
|
|
|
2003-06-28 03:04:36 +04:00
|
|
|
return vfs_register_filesystem("pipefs", &pipefs::pipefs_ops);
|
2003-06-27 07:44:28 +04:00
|
|
|
}
|