2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2005-01-12 02:26:00 +03:00
|
|
|
* Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
#include "IOScheduler.h"
|
|
|
|
|
2004-06-07 05:18:56 +04:00
|
|
|
#include <KernelExport.h>
|
|
|
|
#include <Drivers.h>
|
|
|
|
#include <pnp_devfs.h>
|
2004-09-15 04:44:57 +04:00
|
|
|
#include <NodeMonitor.h>
|
2004-06-07 05:18:56 +04:00
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
#include <kdevice_manager.h>
|
2004-10-29 05:58:08 +04:00
|
|
|
#include <KPath.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <vfs.h>
|
|
|
|
#include <debug.h>
|
2005-06-02 22:32:21 +04:00
|
|
|
#include <util/khash.h>
|
|
|
|
#include <util/AutoLock.h>
|
|
|
|
#include <elf.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <lock.h>
|
|
|
|
#include <vm.h>
|
|
|
|
#include <arch/cpu.h>
|
2005-06-02 22:32:21 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <devfs.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2005-06-02 22:32:21 +04:00
|
|
|
#include <stdlib.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <stdio.h>
|
2005-06-02 22:32:21 +04:00
|
|
|
#include <errno.h>
|
2004-06-07 05:18:56 +04:00
|
|
|
#include <sys/stat.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
2004-06-07 05:18:56 +04:00
|
|
|
//#define TRACE_DEVFS
|
|
|
|
#ifdef TRACE_DEVFS
|
2002-07-17 11:55:51 +04:00
|
|
|
# define TRACE(x) dprintf x
|
2002-07-09 16:24:59 +04:00
|
|
|
#else
|
2002-07-17 11:55:51 +04:00
|
|
|
# define TRACE(x)
|
2002-07-09 16:24:59 +04:00
|
|
|
#endif
|
|
|
|
|
2002-07-28 22:41:07 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
struct devfs_partition {
|
|
|
|
struct devfs_vnode *raw_device;
|
|
|
|
partition_info info;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
struct driver_entry;
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
enum {
|
|
|
|
kNotScanned = 0,
|
|
|
|
kBootScan,
|
|
|
|
kNormalScan,
|
|
|
|
};
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs_stream {
|
2004-09-15 04:44:57 +04:00
|
|
|
mode_t type;
|
2002-07-09 16:24:59 +04:00
|
|
|
union {
|
|
|
|
struct stream_dir {
|
2005-06-02 22:32:21 +04:00
|
|
|
struct devfs_vnode *dir_head;
|
|
|
|
struct list cookies;
|
2005-06-03 04:51:17 +04:00
|
|
|
int32 scanned;
|
2002-07-09 16:24:59 +04:00
|
|
|
} dir;
|
|
|
|
struct stream_dev {
|
2005-06-02 22:32:21 +04:00
|
|
|
device_node_info *node;
|
|
|
|
pnp_devfs_driver_info *info;
|
|
|
|
device_hooks *ops;
|
2005-06-03 04:51:17 +04:00
|
|
|
struct devfs_partition *partition;
|
2005-06-02 22:32:21 +04:00
|
|
|
IOScheduler *scheduler;
|
|
|
|
driver_entry *driver;
|
2002-07-09 16:24:59 +04:00
|
|
|
} dev;
|
2002-07-28 22:41:07 +04:00
|
|
|
struct stream_symlink {
|
2005-06-02 22:32:21 +04:00
|
|
|
const char *path;
|
|
|
|
size_t length;
|
2002-07-28 22:41:07 +04:00
|
|
|
} symlink;
|
2002-07-09 16:24:59 +04:00
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct devfs_vnode {
|
|
|
|
struct devfs_vnode *all_next;
|
2004-09-15 04:44:57 +04:00
|
|
|
vnode_id id;
|
|
|
|
char *name;
|
|
|
|
time_t modification_time;
|
|
|
|
time_t creation_time;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs_vnode *parent;
|
|
|
|
struct devfs_vnode *dir_next;
|
|
|
|
struct devfs_stream stream;
|
|
|
|
};
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
#define DEVFS_HASH_SIZE 16
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs {
|
2005-01-18 19:51:21 +03:00
|
|
|
mount_id id;
|
2005-06-02 22:32:21 +04:00
|
|
|
recursive_lock lock;
|
2005-01-18 19:51:21 +03:00
|
|
|
int32 next_vnode_id;
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_table *vnode_hash;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs_vnode *root_vnode;
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_table *driver_hash;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_dir_cookie {
|
|
|
|
struct list_link link;
|
|
|
|
struct devfs_vnode *current;
|
|
|
|
int32 state; // iteration state
|
|
|
|
};
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs_cookie {
|
2005-05-27 04:43:01 +04:00
|
|
|
void *device_cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2004-09-12 04:00:29 +04:00
|
|
|
// directory iteration states
|
|
|
|
enum {
|
|
|
|
ITERATION_STATE_DOT = 0,
|
|
|
|
ITERATION_STATE_DOT_DOT = 1,
|
|
|
|
ITERATION_STATE_OTHERS = 2,
|
|
|
|
ITERATION_STATE_BEGIN = ITERATION_STATE_DOT,
|
|
|
|
};
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
struct driver_entry {
|
|
|
|
driver_entry *next;
|
|
|
|
const char *path;
|
|
|
|
mount_id device;
|
|
|
|
vnode_id node;
|
|
|
|
time_t last_modified;
|
|
|
|
image_id image;
|
|
|
|
};
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
|
|
|
|
static void get_device_name(struct devfs_vnode *vnode, char *buffer, size_t size);
|
2005-06-02 22:32:21 +04:00
|
|
|
static status_t publish_device(struct devfs *fs, const char *path,
|
|
|
|
device_node_info *deviceNode, pnp_devfs_driver_info *info,
|
|
|
|
driver_entry *driver, device_hooks *ops);
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
|
2005-01-18 19:51:21 +03:00
|
|
|
// The boot device, if already present
|
|
|
|
extern dev_t gBootDevice;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
/* the one and only allowed devfs instance */
|
2004-06-07 21:22:32 +04:00
|
|
|
static struct devfs *sDeviceFileSystem = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-06-07 05:18:56 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
// #pragma mark - driver private
|
|
|
|
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
driver_entry_hash(void *_driver, const void *_key, uint32 range)
|
|
|
|
{
|
|
|
|
driver_entry *driver = (driver_entry *)_driver;
|
|
|
|
const vnode_id *key = (const vnode_id *)_key;
|
|
|
|
|
|
|
|
if (driver != NULL)
|
|
|
|
return driver->node % range;
|
|
|
|
|
|
|
|
return (*key) % range;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
driver_entry_compare(void *_driver, const void *_key)
|
|
|
|
{
|
|
|
|
driver_entry *driver = (driver_entry *)_driver;
|
|
|
|
const vnode_id *key = (const vnode_id *)_key;
|
|
|
|
|
|
|
|
if (driver->node == *key)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
load_driver(driver_entry *driver)
|
|
|
|
{
|
|
|
|
status_t (*init_hardware)(void);
|
|
|
|
status_t (*init_driver)(void);
|
|
|
|
const char **devicePaths;
|
|
|
|
int32 exported = 0;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
// load the module
|
|
|
|
image_id image = load_kernel_add_on(driver->path);
|
|
|
|
if (image < 0)
|
|
|
|
return image;
|
|
|
|
|
|
|
|
// for prettier debug output
|
|
|
|
const char *name = strrchr(driver->path, '/');
|
|
|
|
if (name == NULL)
|
|
|
|
name = driver->path;
|
|
|
|
else
|
|
|
|
name++;
|
|
|
|
|
|
|
|
// For a valid device driver the following exports are required
|
|
|
|
|
|
|
|
uint32 *api_version;
|
|
|
|
if (get_image_symbol(image, "api_version", B_SYMBOL_TYPE_DATA, (void **)&api_version) != B_OK)
|
|
|
|
dprintf("%s: api_version missing\n", name);
|
|
|
|
|
|
|
|
device_hooks *(*find_device)(const char *);
|
|
|
|
const char **(*publish_devices)(void);
|
|
|
|
if (get_image_symbol(image, "publish_devices", B_SYMBOL_TYPE_TEXT, (void **)&publish_devices) != B_OK
|
|
|
|
|| get_image_symbol(image, "find_device", B_SYMBOL_TYPE_TEXT, (void **)&find_device) != B_OK) {
|
|
|
|
dprintf("%s: mandatory driver symbol(s) missing!\n", name);
|
|
|
|
status = B_BAD_VALUE;
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the driver
|
|
|
|
|
|
|
|
if (get_image_symbol(image, "init_hardware", B_SYMBOL_TYPE_TEXT,
|
|
|
|
(void **)&init_hardware) == B_OK
|
|
|
|
&& (status = init_hardware()) != B_OK) {
|
|
|
|
dprintf("%s: init_hardware() failed: %s\n", name, strerror(status));
|
|
|
|
status = ENXIO;
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_image_symbol(image, "init_driver", B_SYMBOL_TYPE_TEXT,
|
|
|
|
(void **)&init_driver) == B_OK
|
|
|
|
&& (status = init_driver()) != B_OK) {
|
|
|
|
dprintf("%s: init_driver() failed: %s\n", name, strerror(status));
|
|
|
|
status = ENXIO;
|
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The driver has successfully been initialized, now we can
|
|
|
|
// finally publish its device entries
|
|
|
|
|
|
|
|
// we keep the driver loaded if it exports at least a single interface
|
|
|
|
// ToDo: we could/should always unload drivers until they will be used for real
|
|
|
|
// ToDo: this function is probably better kept in devfs, so that it could remember
|
|
|
|
// the driver stuff (and even keep it loaded if there is enough memory)
|
|
|
|
devicePaths = publish_devices();
|
|
|
|
if (devicePaths == NULL) {
|
|
|
|
dprintf("%s: publish_devices() returned NULL.\n", name);
|
|
|
|
status = ENXIO;
|
|
|
|
goto error3;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; devicePaths[0]; devicePaths++) {
|
|
|
|
device_hooks *hooks = find_device(devicePaths[0]);
|
|
|
|
|
|
|
|
if (hooks != NULL
|
|
|
|
&& publish_device(sDeviceFileSystem, devicePaths[0],
|
|
|
|
NULL, NULL, driver, hooks) == B_OK)
|
|
|
|
exported++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're all done, driver will be kept loaded (for now, see above comment)
|
|
|
|
if (exported > 0) {
|
|
|
|
driver->image = image;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = B_ERROR; // whatever...
|
|
|
|
|
|
|
|
error3:
|
|
|
|
{
|
|
|
|
status_t (*uninit_driver)(void);
|
|
|
|
if (get_image_symbol(image, "uninit_driver", B_SYMBOL_TYPE_TEXT,
|
|
|
|
(void **)&uninit_driver) == B_OK)
|
|
|
|
uninit_driver();
|
|
|
|
}
|
|
|
|
|
|
|
|
error2:
|
|
|
|
{
|
|
|
|
status_t (*uninit_hardware)(void);
|
|
|
|
if (get_image_symbol(image, "uninit_hardware", B_SYMBOL_TYPE_TEXT,
|
|
|
|
(void **)&uninit_hardware) == B_OK)
|
|
|
|
uninit_hardware();
|
|
|
|
}
|
|
|
|
|
|
|
|
error1:
|
|
|
|
unload_kernel_add_on(image);
|
|
|
|
driver->image = status;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** This is no longer part of the public kernel API, so we just export the symbol */
|
|
|
|
|
|
|
|
status_t load_driver_symbols(const char *driverName);
|
|
|
|
status_t
|
|
|
|
load_driver_symbols(const char *driverName)
|
|
|
|
{
|
|
|
|
// This will be globally done for the whole kernel via the settings file.
|
|
|
|
// We don't have to do anything here.
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
static int32
|
|
|
|
scan_mode(void)
|
|
|
|
{
|
|
|
|
// We may scan every device twice:
|
|
|
|
// - once before there is a boot device,
|
|
|
|
// - and once when there is one
|
|
|
|
|
|
|
|
return gBootDevice >= 0 ? kNormalScan : kBootScan;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
scan_for_drivers(devfs_vnode *dir)
|
|
|
|
{
|
|
|
|
KPath path;
|
|
|
|
if (path.InitCheck() != B_OK)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
get_device_name(dir, path.LockBuffer(), path.BufferSize());
|
|
|
|
path.UnlockBuffer();
|
|
|
|
|
|
|
|
TRACE(("scan_for_drivers: mode %ld: %s\n", scan_mode(), path.Path()));
|
|
|
|
|
|
|
|
// scan for drivers at this path
|
|
|
|
probe_for_device_type(path.Path());
|
|
|
|
|
|
|
|
dir->stream.u.dir.scanned = scan_mode();
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
// #pragma mark - devfs private
|
2002-07-11 01:55:33 +04:00
|
|
|
|
|
|
|
|
2002-11-29 11:36:05 +03:00
|
|
|
static uint32
|
2005-06-02 22:32:21 +04:00
|
|
|
devfs_vnode_hash(void *_vnode, const void *_key, uint32 range)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
const vnode_id *key = (const vnode_id *)_key;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
if (vnode != NULL)
|
|
|
|
return vnode->id % range;
|
2002-07-17 11:55:51 +04:00
|
|
|
|
|
|
|
return (*key) % range;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
|
|
|
static int
|
2005-06-02 22:32:21 +04:00
|
|
|
devfs_vnode_compare(void *_vnode, const void *_key)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
const vnode_id *key = (const vnode_id *)_key;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
if (vnode->id == *key)
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
2002-07-17 11:55:51 +04:00
|
|
|
|
|
|
|
return -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
|
|
|
static struct devfs_vnode *
|
2004-09-15 04:44:57 +04:00
|
|
|
devfs_create_vnode(struct devfs *fs, devfs_vnode *parent, const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
vnode = (struct devfs_vnode *)malloc(sizeof(struct devfs_vnode));
|
|
|
|
if (vnode == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
memset(vnode, 0, sizeof(struct devfs_vnode));
|
|
|
|
vnode->id = fs->next_vnode_id++;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
vnode->name = strdup(name);
|
|
|
|
if (vnode->name == NULL) {
|
|
|
|
free(vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
vnode->creation_time = vnode->modification_time = time(NULL);
|
|
|
|
vnode->uid = geteuid();
|
|
|
|
vnode->gid = parent ? parent->gid : getegid();
|
|
|
|
// inherit group from parent if possible
|
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
return vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_delete_vnode(struct devfs *fs, struct devfs_vnode *vnode, bool force_delete)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
// cant delete it if it's in a directory or is a directory
|
|
|
|
// and has children
|
2002-07-17 11:55:51 +04:00
|
|
|
if (!force_delete
|
2004-09-15 04:44:57 +04:00
|
|
|
&& ((S_ISDIR(vnode->stream.type) && vnode->stream.u.dir.dir_head != NULL)
|
2004-08-27 21:44:08 +04:00
|
|
|
|| vnode->dir_next != NULL))
|
2002-07-09 16:24:59 +04:00
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
// remove it from the global hash table
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_remove(fs->vnode_hash, vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
2004-09-04 21:46:10 +04:00
|
|
|
// for partitions, we have to release the raw device
|
2005-06-03 04:51:17 +04:00
|
|
|
if (vnode->stream.u.dev.partition)
|
|
|
|
put_vnode(fs->id, vnode->stream.u.dev.partition->raw_device->id);
|
2004-09-04 21:46:10 +04:00
|
|
|
|
|
|
|
// remove API conversion from old to new drivers
|
|
|
|
if (vnode->stream.u.dev.node == NULL)
|
|
|
|
free(vnode->stream.u.dev.info);
|
|
|
|
}
|
2002-07-17 11:55:51 +04:00
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
free(vnode->name);
|
|
|
|
free(vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
/** makes sure none of the dircookies point to the vnode passed in */
|
2002-07-11 01:55:33 +04:00
|
|
|
|
|
|
|
static void
|
2005-05-27 04:43:01 +04:00
|
|
|
update_dir_cookies(struct devfs_vnode *dir, struct devfs_vnode *vnode)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_dir_cookie *cookie = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
while ((cookie = (devfs_dir_cookie *)list_get_next_item(&dir->stream.u.dir.cookies, cookie)) != NULL) {
|
|
|
|
if (cookie->current == vnode)
|
|
|
|
cookie->current = vnode->dir_next;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
static struct devfs_vnode *
|
|
|
|
devfs_find_in_dir(struct devfs_vnode *dir, const char *path)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-06-03 04:51:17 +04:00
|
|
|
struct devfs_vnode *vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISDIR(dir->stream.type))
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
if (!strcmp(path, "."))
|
2002-07-09 16:24:59 +04:00
|
|
|
return dir;
|
2002-07-17 11:55:51 +04:00
|
|
|
if (!strcmp(path, ".."))
|
2002-07-09 16:24:59 +04:00
|
|
|
return dir->parent;
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
for (vnode = dir->stream.u.dir.dir_head; vnode; vnode = vnode->dir_next) {
|
|
|
|
//TRACE(("devfs_find_in_dir: looking at entry '%s'\n", vnode->name));
|
|
|
|
if (strcmp(vnode->name, path) == 0) {
|
|
|
|
//TRACE(("devfs_find_in_dir: found it at %p\n", vnode));
|
|
|
|
return vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2005-05-12 07:27:44 +04:00
|
|
|
devfs_insert_in_dir(struct devfs_vnode *dir, struct devfs_vnode *vnode)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISDIR(dir->stream.type))
|
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-25 04:41:32 +04:00
|
|
|
// make sure the directory stays sorted alphabetically
|
|
|
|
|
|
|
|
devfs_vnode *node = dir->stream.u.dir.dir_head, *last = NULL;
|
|
|
|
while (node && strcmp(node->name, vnode->name) < 0) {
|
|
|
|
last = node;
|
|
|
|
node = node->dir_next;
|
|
|
|
}
|
|
|
|
if (last == NULL) {
|
|
|
|
// the new vnode is the first entry in the list
|
|
|
|
vnode->dir_next = dir->stream.u.dir.dir_head;
|
|
|
|
dir->stream.u.dir.dir_head = vnode;
|
|
|
|
} else {
|
|
|
|
// insert after that node
|
|
|
|
vnode->dir_next = last->dir_next;
|
|
|
|
last->dir_next = vnode;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
vnode->parent = dir;
|
2004-09-15 04:44:57 +04:00
|
|
|
dir->modification_time = time(NULL);
|
2005-05-12 07:27:44 +04:00
|
|
|
|
|
|
|
notify_entry_created(sDeviceFileSystem->id, dir->id, vnode->name, vnode->id);
|
|
|
|
notify_stat_changed(sDeviceFileSystem->id, dir->id, B_STAT_MODIFICATION_TIME);
|
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2005-05-12 07:27:44 +04:00
|
|
|
devfs_remove_from_dir(struct devfs_vnode *dir, struct devfs_vnode *removeNode)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-05-12 07:27:44 +04:00
|
|
|
struct devfs_vnode *vnode = dir->stream.u.dir.dir_head;
|
|
|
|
struct devfs_vnode *lastNode = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
for (; vnode != NULL; lastNode = vnode, vnode = vnode->dir_next) {
|
|
|
|
if (vnode == removeNode) {
|
|
|
|
// make sure no dircookies point to this vnode
|
2005-05-27 04:43:01 +04:00
|
|
|
update_dir_cookies(dir, vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
if (lastNode)
|
|
|
|
lastNode->dir_next = vnode->dir_next;
|
2002-07-09 16:24:59 +04:00
|
|
|
else
|
2005-05-12 07:27:44 +04:00
|
|
|
dir->stream.u.dir.dir_head = vnode->dir_next;
|
|
|
|
vnode->dir_next = NULL;
|
2004-09-15 04:44:57 +04:00
|
|
|
dir->modification_time = time(NULL);
|
2005-05-12 07:27:44 +04:00
|
|
|
|
|
|
|
notify_entry_removed(sDeviceFileSystem->id, dir->id, vnode->name, vnode->id);
|
|
|
|
notify_stat_changed(sDeviceFileSystem->id, dir->id, B_STAT_MODIFICATION_TIME);
|
2004-09-15 04:44:57 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
2004-09-15 04:44:57 +04:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2005-06-03 04:51:17 +04:00
|
|
|
devfs_get_partition_info(struct devfs *fs, struct devfs_vnode *vnode,
|
2004-10-27 19:21:11 +04:00
|
|
|
struct devfs_cookie *cookie, void *buffer, size_t length)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-06-03 04:51:17 +04:00
|
|
|
struct devfs_partition *partition = vnode->stream.u.dev.partition;
|
2002-07-17 11:55:51 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
if (!S_ISCHR(vnode->stream.type) || partition == NULL || length != sizeof(partition_info))
|
2004-10-27 19:21:11 +04:00
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
return user_memcpy(buffer, &partition->info, sizeof(partition_info));
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-19 08:20:07 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-10-27 19:21:11 +04:00
|
|
|
add_partition(struct devfs *fs, struct devfs_vnode *device,
|
|
|
|
const char *name, const partition_info &info)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-06-03 04:51:17 +04:00
|
|
|
struct devfs_vnode *partitionNode;
|
2004-08-28 17:33:31 +04:00
|
|
|
status_t status;
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2004-10-27 19:21:11 +04:00
|
|
|
if (!S_ISCHR(device->stream.type))
|
2004-08-28 17:33:31 +04:00
|
|
|
return B_BAD_VALUE;
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// we don't support nested partitions
|
2005-06-03 04:51:17 +04:00
|
|
|
if (device->stream.u.dev.partition)
|
2004-08-28 17:33:31 +04:00
|
|
|
return B_BAD_VALUE;
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// reduce checks to a minimum - things like negative offsets could be useful
|
2002-07-17 11:55:51 +04:00
|
|
|
if (info.size < 0)
|
2004-08-28 17:33:31 +04:00
|
|
|
return B_BAD_VALUE;
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
// create partition
|
|
|
|
struct devfs_partition *partition = (struct devfs_partition *)malloc(sizeof(struct devfs_partition));
|
|
|
|
if (partition == NULL)
|
2004-08-28 17:33:31 +04:00
|
|
|
return B_NO_MEMORY;
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
memcpy(&partition->info, &info, sizeof(partition_info));
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// you cannot change a partition once set
|
2004-10-27 19:21:11 +04:00
|
|
|
if (devfs_find_in_dir(device->parent, name)) {
|
2004-08-28 17:33:31 +04:00
|
|
|
status = B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err1;
|
|
|
|
}
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// increase reference count of raw device -
|
|
|
|
// the partition device really needs it
|
2005-06-03 04:51:17 +04:00
|
|
|
status = get_vnode(fs->id, device->id, (fs_vnode *)&partition->raw_device);
|
2004-08-28 17:33:31 +04:00
|
|
|
if (status < B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err1;
|
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
// now create the partition vnode
|
2005-06-03 04:51:17 +04:00
|
|
|
partitionNode = devfs_create_vnode(fs, device->parent, name);
|
|
|
|
if (partitionNode == NULL) {
|
2004-08-28 17:33:31 +04:00
|
|
|
status = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
partitionNode->stream.type = device->stream.type;
|
|
|
|
partitionNode->stream.u.dev.node = device->stream.u.dev.node;
|
|
|
|
partitionNode->stream.u.dev.info = device->stream.u.dev.info;
|
|
|
|
partitionNode->stream.u.dev.ops = device->stream.u.dev.ops;
|
|
|
|
partitionNode->stream.u.dev.partition = partition;
|
|
|
|
partitionNode->stream.u.dev.scheduler = device->stream.u.dev.scheduler;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
hash_insert(fs->vnode_hash, partitionNode);
|
|
|
|
devfs_insert_in_dir(device->parent, partitionNode);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
TRACE(("add_partition(name = %s, offset = %Ld, size = %Ld)\n",
|
|
|
|
name, info.offset, info.size));
|
2004-08-28 17:33:31 +04:00
|
|
|
return B_OK;
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
err2:
|
2004-10-27 19:21:11 +04:00
|
|
|
put_vnode(fs->id, device->id);
|
2005-06-03 04:51:17 +04:00
|
|
|
err1:
|
|
|
|
free(partition);
|
2004-08-28 17:33:31 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2005-06-02 22:32:21 +04:00
|
|
|
translate_partition_access(devfs_partition *partition, off_t &offset, size_t &size)
|
2004-08-28 17:33:31 +04:00
|
|
|
{
|
|
|
|
if (offset < 0)
|
|
|
|
offset = 0;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
if (offset > partition->info.size) {
|
2004-08-28 17:33:31 +04:00
|
|
|
size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
size = min_c(size, partition->info.size - offset);
|
|
|
|
offset += partition->info.offset;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
static pnp_devfs_driver_info *
|
|
|
|
create_new_driver_info(device_hooks *ops)
|
|
|
|
{
|
|
|
|
pnp_devfs_driver_info *info = (pnp_devfs_driver_info *)malloc(sizeof(pnp_devfs_driver_info));
|
|
|
|
if (info == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2005-01-12 02:26:00 +03:00
|
|
|
memset(info, 0, sizeof(driver_module_info));
|
2004-09-04 21:46:10 +04:00
|
|
|
|
|
|
|
info->open = NULL;
|
|
|
|
// ops->open is used directly for old devices
|
|
|
|
info->close = ops->close;
|
|
|
|
info->free = ops->free;
|
|
|
|
info->control = ops->control;
|
|
|
|
info->select = ops->select;
|
|
|
|
info->deselect = ops->deselect;
|
|
|
|
info->read = ops->read;
|
|
|
|
info->write = ops->write;
|
|
|
|
|
|
|
|
info->read_pages = NULL;
|
|
|
|
info->write_pages = NULL;
|
|
|
|
// old devices can't know to do physical page access
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-27 19:21:11 +04:00
|
|
|
static status_t
|
2004-10-29 06:24:30 +04:00
|
|
|
get_node_for_path(struct devfs *fs, const char *path, struct devfs_vnode **_node)
|
2004-10-27 19:21:11 +04:00
|
|
|
{
|
2004-10-29 06:24:30 +04:00
|
|
|
return vfs_get_fs_node_from_path(fs->id, path, true, (void **)_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
unpublish_node(struct devfs *fs, const char *path, int type)
|
|
|
|
{
|
|
|
|
devfs_vnode *node;
|
|
|
|
status_t status = get_node_for_path(fs, path, &node);
|
|
|
|
if (status != B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if ((type & S_IFMT) != type) {
|
|
|
|
status = B_BAD_TYPE;
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
recursive_lock_lock(&fs->lock);
|
2004-10-29 06:24:30 +04:00
|
|
|
|
|
|
|
status = devfs_remove_from_dir(node->parent, node);
|
|
|
|
if (status < B_OK)
|
|
|
|
goto err2;
|
|
|
|
|
|
|
|
status = remove_vnode(fs->id, node->id);
|
|
|
|
|
|
|
|
err2:
|
2005-06-02 22:32:21 +04:00
|
|
|
recursive_lock_unlock(&fs->lock);
|
2004-10-29 06:24:30 +04:00
|
|
|
err1:
|
|
|
|
put_vnode(fs->id, node->id);
|
|
|
|
return status;
|
2004-10-27 19:21:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-25 05:17:02 +03:00
|
|
|
static status_t
|
|
|
|
publish_directory(struct devfs *fs, const char *path)
|
|
|
|
{
|
2005-06-02 22:32:21 +04:00
|
|
|
ASSERT_LOCKED_RECURSIVE(&fs->lock);
|
2005-01-25 05:17:02 +03:00
|
|
|
|
|
|
|
// copy the path over to a temp buffer so we can munge it
|
|
|
|
KPath tempPath(path);
|
2005-05-13 03:31:04 +04:00
|
|
|
if (tempPath.InitCheck() != B_OK)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2005-01-25 05:17:02 +03:00
|
|
|
char *temp = tempPath.LockBuffer();
|
|
|
|
|
|
|
|
// create the path leading to the device
|
|
|
|
// parse the path passed in, stripping out '/'
|
|
|
|
|
|
|
|
struct devfs_vnode *dir = fs->root_vnode;
|
|
|
|
struct devfs_vnode *vnode = NULL;
|
|
|
|
status_t status = B_OK;
|
|
|
|
int32 i = 0, last = 0;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
while (temp[last]) {
|
2005-01-25 05:17:02 +03:00
|
|
|
if (temp[i] == '/') {
|
|
|
|
temp[i] = '\0';
|
|
|
|
i++;
|
|
|
|
} else if (temp[i] != '\0') {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("\tpath component '%s'\n", &temp[last]));
|
2005-01-25 05:17:02 +03:00
|
|
|
|
|
|
|
// we have a path component
|
|
|
|
vnode = devfs_find_in_dir(dir, &temp[last]);
|
|
|
|
if (vnode) {
|
|
|
|
if (S_ISDIR(vnode->stream.type)) {
|
|
|
|
last = i;
|
|
|
|
dir = vnode;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we hit something on our path that's not a directory
|
|
|
|
status = B_FILE_EXISTS;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
vnode = devfs_create_vnode(fs, dir, &temp[last]);
|
|
|
|
if (!vnode) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the new directory
|
|
|
|
vnode->stream.type = S_IFDIR | 0755;
|
|
|
|
vnode->stream.u.dir.dir_head = NULL;
|
2005-05-27 04:43:01 +04:00
|
|
|
list_init(&vnode->stream.u.dir.cookies);
|
2005-01-25 05:17:02 +03:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_insert(sDeviceFileSystem->vnode_hash, vnode);
|
2005-01-25 05:17:02 +03:00
|
|
|
devfs_insert_in_dir(dir, vnode);
|
|
|
|
|
|
|
|
last = i;
|
|
|
|
dir = vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
static status_t
|
2004-10-29 05:41:16 +04:00
|
|
|
publish_node(struct devfs *fs, const char *path, struct devfs_vnode **_node)
|
2004-09-04 21:46:10 +04:00
|
|
|
{
|
2004-10-29 05:58:08 +04:00
|
|
|
ASSERT_LOCKED_MUTEX(&fs->lock);
|
2004-09-04 21:46:10 +04:00
|
|
|
|
|
|
|
// copy the path over to a temp buffer so we can munge it
|
2004-10-29 05:58:08 +04:00
|
|
|
KPath tempPath(path);
|
2005-05-13 03:31:04 +04:00
|
|
|
if (tempPath.InitCheck() != B_OK)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2004-10-29 05:58:08 +04:00
|
|
|
char *temp = tempPath.LockBuffer();
|
2004-09-04 21:46:10 +04:00
|
|
|
|
|
|
|
// create the path leading to the device
|
|
|
|
// parse the path passed in, stripping out '/'
|
2004-10-29 05:58:08 +04:00
|
|
|
|
2004-10-29 05:41:16 +04:00
|
|
|
struct devfs_vnode *dir = fs->root_vnode;
|
2004-09-04 21:46:10 +04:00
|
|
|
struct devfs_vnode *vnode = NULL;
|
2004-10-29 05:58:08 +04:00
|
|
|
status_t status = B_OK;
|
2004-09-04 21:46:10 +04:00
|
|
|
int32 i = 0, last = 0;
|
|
|
|
bool atLeaf = false;
|
|
|
|
|
|
|
|
for (;;) {
|
2005-01-25 05:17:02 +03:00
|
|
|
if (temp[i] == '\0') {
|
2004-09-04 21:46:10 +04:00
|
|
|
atLeaf = true; // we'll be done after this one
|
|
|
|
} else if (temp[i] == '/') {
|
2005-01-25 05:17:02 +03:00
|
|
|
temp[i] = '\0';
|
2004-09-04 21:46:10 +04:00
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("\tpath component '%s'\n", &temp[last]));
|
2004-09-04 21:46:10 +04:00
|
|
|
|
|
|
|
// we have a path component
|
|
|
|
vnode = devfs_find_in_dir(dir, &temp[last]);
|
|
|
|
if (vnode) {
|
|
|
|
if (!atLeaf) {
|
|
|
|
// we are not at the leaf of the path, so as long as
|
|
|
|
// this is a dir we're okay
|
2004-09-15 04:44:57 +04:00
|
|
|
if (S_ISDIR(vnode->stream.type)) {
|
2004-09-04 21:46:10 +04:00
|
|
|
last = i;
|
|
|
|
dir = vnode;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we are at the leaf and hit another node
|
|
|
|
// or we aren't but hit a non-dir node.
|
|
|
|
// we're screwed
|
|
|
|
status = B_FILE_EXISTS;
|
|
|
|
goto out;
|
|
|
|
} else {
|
2004-10-29 05:41:16 +04:00
|
|
|
vnode = devfs_create_vnode(fs, dir, &temp[last]);
|
2004-09-04 21:46:10 +04:00
|
|
|
if (!vnode) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the new vnode
|
2004-10-29 05:41:16 +04:00
|
|
|
if (!atLeaf) {
|
2004-09-04 21:46:10 +04:00
|
|
|
// this is a dir
|
2004-09-15 04:44:57 +04:00
|
|
|
vnode->stream.type = S_IFDIR | 0755;
|
2004-09-04 21:46:10 +04:00
|
|
|
vnode->stream.u.dir.dir_head = NULL;
|
2005-05-27 04:43:01 +04:00
|
|
|
list_init(&vnode->stream.u.dir.cookies);
|
2004-10-29 05:41:16 +04:00
|
|
|
} else {
|
|
|
|
// this is the last component
|
|
|
|
*_node = vnode;
|
2004-09-04 21:46:10 +04:00
|
|
|
}
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_insert(sDeviceFileSystem->vnode_hash, vnode);
|
2004-09-04 21:46:10 +04:00
|
|
|
devfs_insert_in_dir(dir, vnode);
|
|
|
|
|
|
|
|
if (atLeaf)
|
|
|
|
break;
|
2004-10-29 05:41:16 +04:00
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
last = i;
|
|
|
|
dir = vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2004-10-29 05:41:16 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2005-01-12 02:26:00 +03:00
|
|
|
publish_device(struct devfs *fs, const char *path, device_node_info *deviceNode,
|
2005-06-02 22:32:21 +04:00
|
|
|
pnp_devfs_driver_info *info, driver_entry *driver, device_hooks *ops)
|
2004-10-29 05:41:16 +04:00
|
|
|
{
|
|
|
|
TRACE(("publish_device(path = \"%s\", node = %p, info = %p, hooks = %p)\n",
|
2005-01-12 02:26:00 +03:00
|
|
|
path, deviceNode, info, ops));
|
2004-10-29 05:41:16 +04:00
|
|
|
|
|
|
|
if (sDeviceFileSystem == NULL) {
|
|
|
|
panic("publish_device() called before devfs mounted\n");
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-01-12 02:26:00 +03:00
|
|
|
if ((ops == NULL && (deviceNode == NULL || info == NULL))
|
2004-10-29 05:41:16 +04:00
|
|
|
|| path == NULL || path[0] == '/')
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
// are the provided device hooks okay?
|
|
|
|
if ((ops != NULL && (ops->open == NULL || ops->close == NULL
|
|
|
|
|| ops->read == NULL || ops->write == NULL))
|
|
|
|
|| info != NULL && (info->open == NULL || info->close == NULL
|
|
|
|
|| info->read == NULL || info->write == NULL))
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
// mark disk devices - they might get an I/O scheduler
|
|
|
|
bool isDisk = false;
|
|
|
|
if (!strncmp(path, "disk/", 5))
|
|
|
|
isDisk = true;
|
|
|
|
|
|
|
|
struct devfs_vnode *node;
|
|
|
|
status_t status;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2004-10-29 05:41:16 +04:00
|
|
|
|
|
|
|
status = publish_node(fs, path, &node);
|
|
|
|
if (status != B_OK)
|
2005-06-02 22:32:21 +04:00
|
|
|
return status;
|
2004-10-29 05:41:16 +04:00
|
|
|
|
|
|
|
// all went fine, let's initialize the node
|
|
|
|
node->stream.type = S_IFCHR | 0644;
|
|
|
|
|
2005-01-12 02:26:00 +03:00
|
|
|
if (deviceNode != NULL)
|
2004-10-29 05:41:16 +04:00
|
|
|
node->stream.u.dev.info = info;
|
|
|
|
else
|
|
|
|
node->stream.u.dev.info = create_new_driver_info(ops);
|
|
|
|
|
2005-01-12 02:26:00 +03:00
|
|
|
node->stream.u.dev.node = deviceNode;
|
2005-06-02 22:32:21 +04:00
|
|
|
node->stream.u.dev.driver = driver;
|
2004-10-29 05:41:16 +04:00
|
|
|
node->stream.u.dev.ops = ops;
|
|
|
|
|
|
|
|
// every raw disk gets an I/O scheduler object attached
|
|
|
|
// ToDo: the driver should ask for a scheduler (ie. using its devfs node attributes)
|
|
|
|
if (isDisk && !strcmp(node->name, "raw"))
|
|
|
|
node->stream.u.dev.scheduler = new IOScheduler(path, info);
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-28 07:22:56 +04:00
|
|
|
/** Construct complete device name (as used for device_open()).
|
|
|
|
* This is safe to use only when the device is in use (and therefore
|
|
|
|
* cannot be unpublished during the iteration).
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_device_name(struct devfs_vnode *vnode, char *buffer, size_t size)
|
|
|
|
{
|
|
|
|
struct devfs_vnode *leaf = vnode;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
// count levels
|
|
|
|
|
|
|
|
for (; vnode->parent && vnode->parent != vnode; vnode = vnode->parent) {
|
|
|
|
offset += strlen(vnode->name) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct full path name
|
2005-06-02 22:32:21 +04:00
|
|
|
|
2004-09-28 07:22:56 +04:00
|
|
|
for (vnode = leaf; vnode->parent && vnode->parent != vnode; vnode = vnode->parent) {
|
|
|
|
size_t length = strlen(vnode->name);
|
|
|
|
size_t start = offset - length - 1;
|
|
|
|
|
|
|
|
if (size >= offset) {
|
|
|
|
strcpy(buffer + start, vnode->name);
|
|
|
|
if (vnode != leaf)
|
|
|
|
buffer[offset - 1] = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
// #pragma mark - file system interface
|
2002-07-11 01:55:33 +04:00
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-11-15 19:43:30 +03:00
|
|
|
devfs_mount(mount_id id, const char *devfs, uint32 flags, const char *args,
|
|
|
|
fs_volume *_fs, vnode_id *root_vnid)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_vnode *vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs *fs;
|
2002-09-03 06:19:22 +04:00
|
|
|
status_t err;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
TRACE(("devfs_mount: entry\n"));
|
|
|
|
|
2004-06-07 21:22:32 +04:00
|
|
|
if (sDeviceFileSystem) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("double mount of devfs attempted\n");
|
2004-02-23 06:21:59 +03:00
|
|
|
err = B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
fs = (struct devfs *)malloc(sizeof(struct devfs));
|
2002-07-14 09:13:20 +04:00
|
|
|
if (fs == NULL) {
|
2004-08-27 21:44:08 +04:00
|
|
|
err = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs->id = id;
|
|
|
|
fs->next_vnode_id = 0;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
err = recursive_lock_init(&fs->lock, "devfs lock");
|
2004-08-27 21:44:08 +04:00
|
|
|
if (err < B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err1;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
fs->vnode_hash = hash_init(DEVFS_HASH_SIZE, offsetof(devfs_vnode, all_next),
|
|
|
|
//(addr_t)&vnode->all_next - (addr_t)vnode,
|
|
|
|
&devfs_vnode_compare, &devfs_vnode_hash);
|
|
|
|
if (fs->vnode_hash == NULL) {
|
2004-08-27 21:44:08 +04:00
|
|
|
err = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
fs->driver_hash = hash_init(DEVFS_HASH_SIZE, offsetof(driver_entry, next),
|
|
|
|
&driver_entry_compare, &driver_entry_hash);
|
|
|
|
if (fs->driver_hash == NULL) {
|
|
|
|
err = B_NO_MEMORY;
|
|
|
|
goto err3;
|
|
|
|
}
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// create a vnode
|
2005-05-27 04:43:01 +04:00
|
|
|
vnode = devfs_create_vnode(fs, NULL, "");
|
|
|
|
if (vnode == NULL) {
|
2004-08-27 21:44:08 +04:00
|
|
|
err = B_NO_MEMORY;
|
2005-06-02 22:32:21 +04:00
|
|
|
goto err4;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set it up
|
2005-05-27 04:43:01 +04:00
|
|
|
vnode->parent = vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// create a dir stream for it to hold
|
2005-05-27 04:43:01 +04:00
|
|
|
vnode->stream.type = S_IFDIR | 0755;
|
|
|
|
vnode->stream.u.dir.dir_head = NULL;
|
|
|
|
list_init(&vnode->stream.u.dir.cookies);
|
|
|
|
fs->root_vnode = vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_insert(fs->vnode_hash, vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
*root_vnid = vnode->id;
|
2002-07-09 16:24:59 +04:00
|
|
|
*_fs = fs;
|
2004-06-07 21:22:32 +04:00
|
|
|
sDeviceFileSystem = fs;
|
2004-08-27 21:44:08 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
err4:
|
|
|
|
hash_uninit(fs->driver_hash);
|
2002-07-09 16:24:59 +04:00
|
|
|
err3:
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_uninit(fs->vnode_hash);
|
2002-07-09 16:24:59 +04:00
|
|
|
err2:
|
2005-06-02 22:32:21 +04:00
|
|
|
recursive_lock_destroy(&fs->lock);
|
2002-07-09 16:24:59 +04:00
|
|
|
err1:
|
2002-10-30 02:07:06 +03:00
|
|
|
free(fs);
|
2002-07-09 16:24:59 +04:00
|
|
|
err:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_unmount(fs_volume _fs)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_vnode *vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct hash_iterator i;
|
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_unmount: entry fs = %p\n", fs));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// delete all of the vnodes
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_open(fs->vnode_hash, &i);
|
|
|
|
while ((vnode = (devfs_vnode *)hash_next(fs->vnode_hash, &i)) != NULL) {
|
2005-05-27 04:43:01 +04:00
|
|
|
devfs_delete_vnode(fs, vnode, true);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_close(fs->vnode_hash, &i, false);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
hash_uninit(fs->vnode_hash);
|
|
|
|
hash_uninit(fs->driver_hash);
|
|
|
|
|
|
|
|
recursive_lock_destroy(&fs->lock);
|
2002-10-30 02:07:06 +03:00
|
|
|
free(fs);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_sync(fs_volume fs)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
TRACE(("devfs_sync: entry\n"));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_lookup(fs_volume _fs, fs_vnode _dir, const char *name, vnode_id *_id, int *_type)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
|
|
|
struct devfs_vnode *dir = (struct devfs_vnode *)_dir;
|
2002-07-28 22:41:07 +04:00
|
|
|
struct devfs_vnode *vnode, *vdummy;
|
2005-06-03 04:51:17 +04:00
|
|
|
status_t status;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_lookup: entry dir %p, name '%s'\n", dir, name));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISDIR(dir->stream.type))
|
2002-07-28 22:41:07 +04:00
|
|
|
return B_NOT_A_DIRECTORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2005-01-18 19:51:21 +03:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
if (dir->stream.u.dir.scanned < scan_mode())
|
|
|
|
scan_for_drivers(dir);
|
2005-01-18 19:51:21 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// look it up
|
2002-07-17 11:55:51 +04:00
|
|
|
vnode = devfs_find_in_dir(dir, name);
|
2005-01-18 19:51:21 +03:00
|
|
|
if (vnode == NULL) {
|
2005-06-03 04:51:17 +04:00
|
|
|
#if 0
|
2005-06-02 22:32:21 +04:00
|
|
|
// ToDo: with node monitoring in place, we *know* here that the file does not exist
|
|
|
|
// and don't have to scan for it again.
|
2005-01-18 19:51:21 +03:00
|
|
|
// scan for drivers in the given directory (that indicates the type of the driver)
|
|
|
|
KPath path;
|
2005-06-03 04:51:17 +04:00
|
|
|
if (path.InitCheck() != B_OK)
|
|
|
|
return B_NO_MEMORY;
|
2005-05-13 03:31:04 +04:00
|
|
|
|
2005-01-18 19:51:21 +03:00
|
|
|
get_device_name(dir, path.LockBuffer(), path.BufferSize());
|
|
|
|
path.UnlockBuffer();
|
|
|
|
path.Append(name);
|
2005-06-03 04:51:17 +04:00
|
|
|
dprintf("lookup: \"%s\"\n", path.Path());
|
2005-01-18 19:51:21 +03:00
|
|
|
|
|
|
|
// scan for drivers of this type
|
|
|
|
probe_for_device_type(path.Path());
|
|
|
|
|
|
|
|
vnode = devfs_find_in_dir(dir, name);
|
|
|
|
if (vnode == NULL) {
|
2005-06-03 04:51:17 +04:00
|
|
|
#endif
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
#if 0
|
2005-01-18 19:51:21 +03:00
|
|
|
}
|
2005-06-02 22:32:21 +04:00
|
|
|
|
|
|
|
if (S_ISDIR(vnode->stream.type) && gBootDevice >= 0)
|
|
|
|
vnode->stream.u.dir.scanned = true;
|
2005-06-03 04:51:17 +04:00
|
|
|
#endif
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
status = get_vnode(fs->id, vnode->id, (fs_vnode *)&vdummy);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-28 22:41:07 +04:00
|
|
|
*_id = vnode->id;
|
|
|
|
*_type = vnode->stream.type;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_get_vnode_name(fs_volume _fs, fs_vnode _vnode, char *buffer, size_t bufferSize)
|
2002-07-19 01:06:02 +04:00
|
|
|
{
|
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
TRACE(("devfs_get_vnode_name: vnode = %p\n", vnode));
|
2002-07-19 01:06:02 +04:00
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
strlcpy(buffer, vnode->name, bufferSize);
|
2002-07-19 01:06:02 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_get_vnode(fs_volume _fs, vnode_id id, fs_vnode *_vnode, bool reenter)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_get_vnode: asking for vnode id = %Ld, vnode = %p, r %d\n", id, _vnode, reenter));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
if (!reenter)
|
2005-06-02 22:32:21 +04:00
|
|
|
recursive_lock_lock(&fs->lock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
*_vnode = hash_lookup(fs->vnode_hash, &id);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
if (!reenter)
|
2005-06-02 22:32:21 +04:00
|
|
|
recursive_lock_unlock(&fs->lock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_get_vnode: looked it up at %p\n", *_vnode));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
if (*_vnode)
|
2005-05-12 07:27:44 +04:00
|
|
|
return B_OK;
|
2002-07-17 11:55:51 +04:00
|
|
|
|
2004-02-23 06:21:59 +03:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_put_vnode(fs_volume _fs, fs_vnode _v, bool reenter)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-06-07 05:18:56 +04:00
|
|
|
#ifdef TRACE_DEVFS
|
2002-07-17 11:55:51 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_v;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_put_vnode: entry on vnode %p, id = %Ld, reenter %d\n", vnode, vnode->id, reenter));
|
2002-07-09 16:24:59 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0; // whatever
|
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_remove_vnode(fs_volume _fs, fs_vnode _v, bool reenter)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
2002-07-17 11:55:51 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_v;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_removevnode: remove %p (%Ld), reenter %d\n", vnode, vnode->id, reenter));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
if (vnode->dir_next) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// can't remove node if it's linked to the dir
|
2002-07-17 11:55:51 +04:00
|
|
|
panic("devfs_removevnode: vnode %p asked to be removed is present in dir\n", vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
devfs_delete_vnode(fs, vnode, false);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-11-01 23:20:37 +03:00
|
|
|
devfs_create(fs_volume _fs, fs_vnode _dir, const char *name, int openMode, int perms,
|
|
|
|
fs_cookie *_cookie, vnode_id *_newVnodeID)
|
2002-07-14 09:13:20 +04:00
|
|
|
{
|
2004-11-01 23:20:37 +03:00
|
|
|
struct devfs_vnode *dir = (struct devfs_vnode *)_dir;
|
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
|
|
|
struct devfs_cookie *cookie;
|
|
|
|
struct devfs_vnode *vnode, *vdummy;
|
|
|
|
status_t status = B_OK;
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
TRACE(("devfs_create: dir %p, name \"%s\", openMode 0x%x, fs_cookie %p \n", dir, name, openMode, _cookie));
|
2004-11-01 23:20:37 +03:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2004-11-01 23:20:37 +03:00
|
|
|
|
|
|
|
// look it up
|
|
|
|
vnode = devfs_find_in_dir(dir, name);
|
|
|
|
if (!vnode) {
|
|
|
|
status = EROFS;
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (openMode & O_EXCL)
|
|
|
|
return B_FILE_EXISTS;
|
|
|
|
|
|
|
|
status = get_vnode(fs->id, vnode->id, (fs_vnode *)&vdummy);
|
|
|
|
if (status < B_OK)
|
|
|
|
goto err1;
|
|
|
|
|
|
|
|
*_newVnodeID = vnode->id;
|
|
|
|
|
|
|
|
cookie = (struct devfs_cookie *)malloc(sizeof(struct devfs_cookie));
|
|
|
|
if (cookie == NULL) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
|
|
|
if (vnode->stream.u.dev.node != NULL) {
|
|
|
|
status = vnode->stream.u.dev.info->open(
|
|
|
|
vnode->stream.u.dev.node->parent->cookie, openMode,
|
2005-05-27 04:43:01 +04:00
|
|
|
&cookie->device_cookie);
|
2004-11-01 23:20:37 +03:00
|
|
|
} else {
|
|
|
|
char buffer[B_FILE_NAME_LENGTH];
|
|
|
|
get_device_name(vnode, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
status = vnode->stream.u.dev.ops->open(buffer, openMode,
|
2005-05-27 04:43:01 +04:00
|
|
|
&cookie->device_cookie);
|
2004-11-01 23:20:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status < B_OK)
|
|
|
|
goto err3;
|
|
|
|
|
|
|
|
*_cookie = cookie;
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
err3:
|
|
|
|
free(cookie);
|
|
|
|
err2:
|
|
|
|
put_vnode(fs->id, vnode->id);
|
|
|
|
err1:
|
|
|
|
return status;
|
2002-07-14 09:13:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-09-04 21:46:10 +04:00
|
|
|
devfs_open(fs_volume _fs, fs_vnode _vnode, int openMode, fs_cookie *_cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct devfs_cookie *cookie;
|
2004-09-15 04:44:57 +04:00
|
|
|
status_t status = B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
TRACE(("devfs_open: vnode %p, openMode 0x%x, fs_cookie %p \n", vnode, openMode, _cookie));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-27 21:44:08 +04:00
|
|
|
cookie = (struct devfs_cookie *)malloc(sizeof(struct devfs_cookie));
|
2002-07-14 09:13:20 +04:00
|
|
|
if (cookie == NULL)
|
2004-08-27 21:44:08 +04:00
|
|
|
return B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
2004-09-10 03:06:04 +04:00
|
|
|
if (vnode->stream.u.dev.node != NULL) {
|
|
|
|
status = vnode->stream.u.dev.info->open(
|
|
|
|
vnode->stream.u.dev.node->parent->cookie, openMode,
|
2005-05-27 04:43:01 +04:00
|
|
|
&cookie->device_cookie);
|
2004-09-10 03:06:04 +04:00
|
|
|
} else {
|
2004-09-28 07:22:56 +04:00
|
|
|
char buffer[B_FILE_NAME_LENGTH];
|
|
|
|
get_device_name(vnode, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
status = vnode->stream.u.dev.ops->open(buffer, openMode,
|
2005-05-27 04:43:01 +04:00
|
|
|
&cookie->device_cookie);
|
2004-09-10 03:06:04 +04:00
|
|
|
}
|
|
|
|
}
|
2004-11-01 23:20:37 +03:00
|
|
|
if (status < B_OK)
|
|
|
|
free(cookie);
|
|
|
|
else
|
|
|
|
*_cookie = cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_close(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_close: entry vnode %p, cookie %p\n", vnode, cookie));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// pass the call through to the underlying device
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->close(cookie->device_cookie);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_free_cookie(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_freecookie: entry vnode %p, cookie %p\n", vnode, cookie));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// pass the call through to the underlying device
|
2005-05-27 04:43:01 +04:00
|
|
|
vnode->stream.u.dev.info->free(cookie->device_cookie);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
free(cookie);
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_fsync(fs_volume _fs, fs_vnode _v)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-10-29 05:58:08 +04:00
|
|
|
static status_t
|
2005-04-06 20:07:10 +04:00
|
|
|
devfs_read_link(fs_volume _fs, fs_vnode _link, char *buffer, size_t *_bufferSize)
|
2004-10-29 05:58:08 +04:00
|
|
|
{
|
|
|
|
struct devfs_vnode *link = (struct devfs_vnode *)_link;
|
2005-05-26 06:04:00 +04:00
|
|
|
size_t bufferSize = *_bufferSize;
|
2004-10-29 05:58:08 +04:00
|
|
|
|
|
|
|
if (!S_ISLNK(link->stream.type))
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2005-05-26 06:04:00 +04:00
|
|
|
*_bufferSize = link->stream.u.symlink.length + 1;
|
|
|
|
// we always need to return the number of bytes we intend to write!
|
|
|
|
|
|
|
|
if (bufferSize <= link->stream.u.symlink.length)
|
2005-04-06 20:07:10 +04:00
|
|
|
return B_BUFFER_OVERFLOW;
|
2004-10-29 05:58:08 +04:00
|
|
|
|
|
|
|
memcpy(buffer, link->stream.u.symlink.path, link->stream.u.symlink.length + 1);
|
2004-10-29 17:11:46 +04:00
|
|
|
return B_OK;
|
2004-10-29 05:58:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_read(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, off_t pos,
|
|
|
|
void *buffer, size_t *_length)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("devfs_read: vnode %p, cookie %p, pos %Ld, len %p\n",
|
|
|
|
// vnode, cookie, pos, _length));
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISCHR(vnode->stream.type))
|
2004-09-10 03:06:04 +04:00
|
|
|
return B_BAD_VALUE;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
if (vnode->stream.u.dev.partition)
|
|
|
|
translate_partition_access(vnode->stream.u.dev.partition, pos, *_length);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
if (*_length == 0)
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
// if this device has an I/O scheduler attached, the request must go through it
|
|
|
|
if (IOScheduler *scheduler = vnode->stream.u.dev.scheduler) {
|
2005-05-27 04:43:01 +04:00
|
|
|
IORequest request(cookie->device_cookie, pos, buffer, *_length);
|
2004-08-28 17:33:31 +04:00
|
|
|
|
|
|
|
status_t status = scheduler->Process(request);
|
|
|
|
if (status == B_OK)
|
|
|
|
*_length = request.Size();
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
// pass the call through to the device
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->read(cookie->device_cookie, pos, buffer, _length);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_write(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, off_t pos,
|
|
|
|
const void *buffer, size_t *_length)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("devfs_write: vnode %p, cookie %p, pos %Ld, len %p\n",
|
|
|
|
// vnode, cookie, pos, _length));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISCHR(vnode->stream.type))
|
2004-08-28 17:33:31 +04:00
|
|
|
return B_BAD_VALUE;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
if (vnode->stream.u.dev.partition)
|
|
|
|
translate_partition_access(vnode->stream.u.dev.partition, pos, *_length);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
if (*_length == 0)
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
if (IOScheduler *scheduler = vnode->stream.u.dev.scheduler) {
|
2005-05-27 04:43:01 +04:00
|
|
|
IORequest request(cookie->device_cookie, pos, buffer, *_length);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-08-28 17:33:31 +04:00
|
|
|
status_t status = scheduler->Process(request);
|
|
|
|
if (status == B_OK)
|
|
|
|
*_length = request.Size();
|
|
|
|
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-08-27 21:44:08 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->write(cookie->device_cookie, pos, buffer, _length);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2005-05-27 04:43:01 +04:00
|
|
|
devfs_create_dir(fs_volume _fs, fs_vnode _dir, const char *name,
|
|
|
|
int perms, vnode_id *_newVnodeID)
|
2002-07-14 09:13:20 +04:00
|
|
|
{
|
|
|
|
return EROFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_open_dir(fs_volume _fs, fs_vnode _vnode, fs_cookie *_cookie)
|
2002-07-14 09:13:20 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_dir_cookie *cookie;
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_open_dir: vnode %p\n", vnode));
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISDIR(vnode->stream.type))
|
2004-08-27 21:44:08 +04:00
|
|
|
return B_BAD_VALUE;
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
cookie = (devfs_dir_cookie *)malloc(sizeof(devfs_dir_cookie));
|
2002-07-14 09:13:20 +04:00
|
|
|
if (cookie == NULL)
|
2004-08-27 21:44:08 +04:00
|
|
|
return B_NO_MEMORY;
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
// make sure the directory has up-to-date contents
|
|
|
|
if (vnode->stream.u.dir.scanned < scan_mode())
|
|
|
|
scan_for_drivers(vnode);
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
cookie->current = vnode->stream.u.dir.dir_head;
|
|
|
|
cookie->state = ITERATION_STATE_BEGIN;
|
|
|
|
|
|
|
|
list_add_item(&vnode->stream.u.dir.cookies, cookie);
|
2002-07-14 09:13:20 +04:00
|
|
|
*_cookie = cookie;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2005-05-27 04:43:01 +04:00
|
|
|
devfs_free_dir_cookie(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-09-10 03:06:04 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_dir_cookie *cookie = (devfs_dir_cookie *)_cookie;
|
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
|
|
|
|
|
|
|
TRACE(("devfs_free_dir_cookie: entry vnode %p, cookie %p\n", vnode, cookie));
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2005-05-27 04:43:01 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
list_remove_item(&vnode->stream.u.dir.cookies, cookie);
|
2005-05-27 04:43:01 +04:00
|
|
|
free(cookie);
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
devfs_read_dir(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie,
|
|
|
|
struct dirent *dirent, size_t bufferSize, uint32 *_num)
|
|
|
|
{
|
|
|
|
struct devfs_vnode *vnode = (devfs_vnode *)_vnode;
|
|
|
|
struct devfs_dir_cookie *cookie = (devfs_dir_cookie *)_cookie;
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
2004-02-23 08:03:04 +03:00
|
|
|
status_t status = B_OK;
|
2004-09-12 04:00:29 +04:00
|
|
|
struct devfs_vnode *childNode = NULL;
|
|
|
|
const char *name = NULL;
|
|
|
|
struct devfs_vnode *nextChildNode = NULL;
|
2005-05-27 04:43:01 +04:00
|
|
|
int32 nextState = cookie->state;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
TRACE(("devfs_read_dir: vnode %p, cookie %p, buffer %p, size %ld\n", _vnode, cookie, dirent, bufferSize));
|
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISDIR(vnode->stream.type))
|
|
|
|
return B_BAD_VALUE;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
switch (cookie->state) {
|
2004-09-12 04:00:29 +04:00
|
|
|
case ITERATION_STATE_DOT:
|
|
|
|
childNode = vnode;
|
|
|
|
name = ".";
|
|
|
|
nextChildNode = vnode->stream.u.dir.dir_head;
|
2005-05-27 04:43:01 +04:00
|
|
|
nextState = cookie->state + 1;
|
2004-09-12 04:00:29 +04:00
|
|
|
break;
|
|
|
|
case ITERATION_STATE_DOT_DOT:
|
|
|
|
childNode = vnode->parent;
|
|
|
|
name = "..";
|
|
|
|
nextChildNode = vnode->stream.u.dir.dir_head;
|
2005-05-27 04:43:01 +04:00
|
|
|
nextState = cookie->state + 1;
|
2004-09-12 04:00:29 +04:00
|
|
|
break;
|
|
|
|
default:
|
2005-05-27 04:43:01 +04:00
|
|
|
childNode = cookie->current;
|
2004-09-12 04:00:29 +04:00
|
|
|
if (childNode) {
|
|
|
|
name = childNode->name;
|
|
|
|
nextChildNode = childNode->dir_next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!childNode) {
|
2002-07-11 01:55:33 +04:00
|
|
|
*_num = 0;
|
2005-06-02 22:32:21 +04:00
|
|
|
return B_OK;
|
2002-07-11 01:55:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dirent->d_dev = fs->id;
|
2004-09-12 04:00:29 +04:00
|
|
|
dirent->d_ino = childNode->id;
|
|
|
|
dirent->d_reclen = strlen(name) + sizeof(struct dirent);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
if (dirent->d_reclen > bufferSize)
|
|
|
|
return ENOBUFS;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-09-12 04:00:29 +04:00
|
|
|
status = user_strlcpy(dirent->d_name, name,
|
|
|
|
bufferSize - sizeof(struct dirent));
|
2004-02-23 08:03:04 +03:00
|
|
|
if (status < B_OK)
|
2005-06-02 22:32:21 +04:00
|
|
|
return status;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
cookie->current = nextChildNode;
|
|
|
|
cookie->state = nextState;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2002-09-24 03:24:12 +04:00
|
|
|
devfs_rewind_dir(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-09-10 03:06:04 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
2005-05-27 04:43:01 +04:00
|
|
|
struct devfs_dir_cookie *cookie = (devfs_dir_cookie *)_cookie;
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
2002-07-17 11:55:51 +04:00
|
|
|
|
|
|
|
TRACE(("devfs_rewind_dir: vnode %p, cookie %p\n", _vnode, _cookie));
|
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISDIR(vnode->stream.type))
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
cookie->current = vnode->stream.u.dir.dir_head;
|
|
|
|
cookie->state = ITERATION_STATE_BEGIN;
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_ioctl(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, ulong op, void *buffer, size_t length)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
|
|
|
|
|
|
|
TRACE(("devfs_ioctl: vnode %p, cookie %p, op %ld, buf %p, len %ld\n", _vnode, _cookie, op, buffer, length));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
2002-07-11 01:55:33 +04:00
|
|
|
switch (op) {
|
2004-02-23 06:21:59 +03:00
|
|
|
case B_GET_PARTITION_INFO:
|
2004-08-28 17:33:31 +04:00
|
|
|
return devfs_get_partition_info(fs, vnode, cookie, (partition_info *)buffer, length);
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-02-23 06:21:59 +03:00
|
|
|
case B_SET_PARTITION:
|
2004-10-27 19:21:11 +04:00
|
|
|
return B_NOT_ALLOWED;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->control(cookie->device_cookie, op, buffer, length);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-02-23 06:21:59 +03:00
|
|
|
|
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-10-05 17:24:12 +04:00
|
|
|
static status_t
|
|
|
|
devfs_set_flags(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, int flags)
|
|
|
|
{
|
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
|
|
|
|
|
|
|
// we need to pass the O_NONBLOCK flag to the underlying device
|
|
|
|
|
|
|
|
if (!S_ISCHR(vnode->stream.type))
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->control(cookie->device_cookie, flags & O_NONBLOCK ?
|
2004-10-05 17:24:12 +04:00
|
|
|
B_SET_NONBLOCKING_IO : B_SET_BLOCKING_IO, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-18 00:04:25 +03:00
|
|
|
static status_t
|
|
|
|
devfs_select(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, uint8 event,
|
|
|
|
uint32 ref, selectsync *sync)
|
|
|
|
{
|
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
|
|
|
|
|
|
|
if (!S_ISCHR(vnode->stream.type))
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
|
|
|
// If the device has no select() hook, notify select() now.
|
|
|
|
if (!vnode->stream.u.dev.info->select)
|
|
|
|
return notify_select_event((selectsync*)sync, ref, event);
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->select(cookie->device_cookie, event, ref,
|
2005-03-18 00:04:25 +03:00
|
|
|
(selectsync*)sync);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
devfs_deselect(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, uint8 event,
|
|
|
|
selectsync *sync)
|
|
|
|
{
|
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
|
|
|
|
|
|
|
if (!S_ISCHR(vnode->stream.type))
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
|
|
|
// If the device has no select() hook, notify select() now.
|
|
|
|
if (!vnode->stream.u.dev.info->deselect)
|
|
|
|
return B_OK;
|
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->deselect(cookie->device_cookie, event,
|
2005-03-18 00:04:25 +03:00
|
|
|
(selectsync*)sync);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
static bool
|
2004-09-07 02:48:58 +04:00
|
|
|
devfs_can_page(fs_volume _fs, fs_vnode _vnode, fs_cookie cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-09-04 21:46:10 +04:00
|
|
|
struct devfs_vnode *vnode = (devfs_vnode *)_vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("devfs_canpage: vnode %p\n", vnode));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISCHR(vnode->stream.type)
|
2004-09-07 19:39:03 +04:00
|
|
|
|| vnode->stream.u.dev.node == NULL
|
|
|
|
|| cookie == NULL)
|
2004-09-04 21:46:10 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return vnode->stream.u.dev.info->read_pages != NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
|
|
|
|
static status_t
|
2004-09-07 02:48:58 +04:00
|
|
|
devfs_read_pages(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, off_t pos, const iovec *vecs, size_t count, size_t *_numBytes)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-09-04 21:46:10 +04:00
|
|
|
struct devfs_vnode *vnode = (devfs_vnode *)_vnode;
|
2004-09-07 02:48:58 +04:00
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("devfs_read_pages: vnode %p, vecs %p, count = %lu, pos = %Ld, size = %lu\n", vnode, vecs, count, pos, *_numBytes));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISCHR(vnode->stream.type)
|
2004-09-07 19:39:03 +04:00
|
|
|
|| vnode->stream.u.dev.info->read_pages == NULL
|
|
|
|
|| cookie == NULL)
|
2004-09-04 21:46:10 +04:00
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
if (vnode->stream.u.dev.partition)
|
|
|
|
translate_partition_access(vnode->stream.u.dev.partition, pos, *_numBytes);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->read_pages(cookie->device_cookie, pos, vecs, count, _numBytes);
|
2004-09-04 21:46:10 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
static status_t
|
2004-09-07 02:48:58 +04:00
|
|
|
devfs_write_pages(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, off_t pos, const iovec *vecs, size_t count, size_t *_numBytes)
|
2004-09-04 21:46:10 +04:00
|
|
|
{
|
|
|
|
struct devfs_vnode *vnode = (devfs_vnode *)_vnode;
|
2004-09-07 02:48:58 +04:00
|
|
|
struct devfs_cookie *cookie = (struct devfs_cookie *)_cookie;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
//TRACE(("devfs_write_pages: vnode %p, vecs %p, count = %lu, pos = %Ld, size = %lu\n", vnode, vecs, count, pos, *_numBytes));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
if (!S_ISCHR(vnode->stream.type)
|
2004-09-07 19:39:03 +04:00
|
|
|
|| vnode->stream.u.dev.info->write_pages == NULL
|
|
|
|
|| cookie == NULL)
|
2004-09-04 21:46:10 +04:00
|
|
|
return B_NOT_ALLOWED;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-06-03 04:51:17 +04:00
|
|
|
if (vnode->stream.u.dev.partition)
|
|
|
|
translate_partition_access(vnode->stream.u.dev.partition, pos, *_numBytes);
|
2004-09-04 21:46:10 +04:00
|
|
|
|
2005-05-27 04:43:01 +04:00
|
|
|
return vnode->stream.u.dev.info->write_pages(cookie->device_cookie, pos, vecs, count, _numBytes);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-02-23 06:21:59 +03:00
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_read_stat(fs_volume _fs, fs_vnode _vnode, struct stat *stat)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
TRACE(("devfs_read_stat: vnode %p (%Ld), stat %p\n", vnode, vnode->id, stat));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 11:55:51 +04:00
|
|
|
stat->st_ino = vnode->id;
|
2002-07-09 16:24:59 +04:00
|
|
|
stat->st_size = 0;
|
2004-09-15 04:44:57 +04:00
|
|
|
stat->st_mode = vnode->stream.type;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
stat->st_nlink = 1;
|
|
|
|
stat->st_blksize = 65536;
|
|
|
|
|
|
|
|
stat->st_uid = vnode->uid;
|
|
|
|
stat->st_gid = vnode->gid;
|
|
|
|
|
|
|
|
stat->st_atime = time(NULL);
|
|
|
|
stat->st_mtime = stat->st_ctime = vnode->modification_time;
|
|
|
|
stat->st_crtime = vnode->creation_time;
|
|
|
|
|
2004-09-28 07:22:56 +04:00
|
|
|
// ToDo: this only works for partitions right now - if we should decide
|
|
|
|
// to keep this feature, we should have a better solution
|
|
|
|
if (S_ISCHR(vnode->stream.type)) {
|
|
|
|
//device_geometry geometry;
|
|
|
|
|
|
|
|
// if it's a real block device, then let's report a useful size
|
2005-06-03 04:51:17 +04:00
|
|
|
if (vnode->stream.u.dev.partition != NULL) {
|
|
|
|
stat->st_size = vnode->stream.u.dev.partition->info.size;
|
2004-09-28 07:22:56 +04:00
|
|
|
#if 0
|
2005-05-27 04:43:01 +04:00
|
|
|
} else if (vnode->stream.u.dev.info->control(cookie->device_cookie,
|
2004-09-28 07:22:56 +04:00
|
|
|
B_GET_GEOMETRY, &geometry, sizeof(struct device_geometry)) >= B_OK) {
|
|
|
|
stat->st_size = 1LL * geometry.head_count * geometry.cylinder_count
|
|
|
|
* geometry.sectors_per_track * geometry.bytes_per_sector;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this a real block device? then let's have it reported like that
|
|
|
|
if (stat->st_size != 0)
|
|
|
|
stat->st_mode = S_IFBLK | (vnode->stream.type & S_IUMSK);
|
|
|
|
}
|
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-03 06:19:22 +04:00
|
|
|
static status_t
|
2004-08-27 21:44:08 +04:00
|
|
|
devfs_write_stat(fs_volume _fs, fs_vnode _vnode, const struct stat *stat, uint32 statMask)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-09-15 04:44:57 +04:00
|
|
|
struct devfs *fs = (struct devfs *)_fs;
|
2004-08-27 21:44:08 +04:00
|
|
|
struct devfs_vnode *vnode = (struct devfs_vnode *)_vnode;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-15 04:44:57 +04:00
|
|
|
TRACE(("devfs_write_stat: vnode %p (0x%Lx), stat %p\n", vnode, vnode->id, stat));
|
|
|
|
|
|
|
|
// we cannot change the size of anything
|
|
|
|
if (statMask & FS_WRITE_STAT_SIZE)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&fs->lock);
|
2004-09-15 04:44:57 +04:00
|
|
|
|
|
|
|
if (statMask & FS_WRITE_STAT_MODE)
|
|
|
|
vnode->stream.type = (vnode->stream.type & ~S_IUMSK) | (stat->st_mode & S_IUMSK);
|
|
|
|
|
|
|
|
if (statMask & FS_WRITE_STAT_UID)
|
|
|
|
vnode->uid = stat->st_uid;
|
|
|
|
if (statMask & FS_WRITE_STAT_GID)
|
|
|
|
vnode->gid = stat->st_gid;
|
|
|
|
|
|
|
|
if (statMask & FS_WRITE_STAT_MTIME)
|
|
|
|
vnode->modification_time = stat->st_mtime;
|
|
|
|
if (statMask & FS_WRITE_STAT_CRTIME)
|
|
|
|
vnode->creation_time = stat->st_crtime;
|
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
notify_stat_changed(fs->id, vnode->id, statMask);
|
2004-09-15 04:44:57 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2004-06-07 21:22:32 +04:00
|
|
|
static status_t
|
|
|
|
devfs_std_ops(int32 op, ...)
|
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case B_MODULE_INIT:
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
case B_MODULE_UNINIT:
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-23 21:15:56 +04:00
|
|
|
file_system_module_info gDeviceFileSystem = {
|
2004-06-07 21:22:32 +04:00
|
|
|
{
|
|
|
|
"file_systems/devfs" B_CURRENT_FS_API_VERSION,
|
|
|
|
0,
|
|
|
|
devfs_std_ops,
|
|
|
|
},
|
|
|
|
|
2005-05-23 21:15:56 +04:00
|
|
|
"Device File System",
|
|
|
|
|
|
|
|
NULL, // identify_partition()
|
|
|
|
NULL, // scan_partition()
|
|
|
|
NULL, // free_identify_partition_cookie()
|
|
|
|
NULL, // free_partition_content_cookie()
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
&devfs_mount,
|
|
|
|
&devfs_unmount,
|
2002-07-23 18:10:12 +04:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2002-07-09 16:24:59 +04:00
|
|
|
&devfs_sync,
|
|
|
|
|
|
|
|
&devfs_lookup,
|
2002-07-19 01:06:02 +04:00
|
|
|
&devfs_get_vnode_name,
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-14 09:13:20 +04:00
|
|
|
&devfs_get_vnode,
|
|
|
|
&devfs_put_vnode,
|
|
|
|
&devfs_remove_vnode,
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
&devfs_can_page,
|
|
|
|
&devfs_read_pages,
|
|
|
|
&devfs_write_pages,
|
|
|
|
|
|
|
|
NULL, // get_file_map
|
2002-07-14 09:13:20 +04:00
|
|
|
|
|
|
|
/* common */
|
|
|
|
&devfs_ioctl,
|
2004-10-05 17:24:12 +04:00
|
|
|
&devfs_set_flags,
|
2005-03-18 00:04:25 +03:00
|
|
|
&devfs_select,
|
|
|
|
&devfs_deselect,
|
2002-07-09 16:24:59 +04:00
|
|
|
&devfs_fsync,
|
|
|
|
|
2004-10-29 05:58:08 +04:00
|
|
|
&devfs_read_link,
|
2002-08-05 09:37:17 +04:00
|
|
|
NULL, // write_link
|
2002-07-28 22:41:07 +04:00
|
|
|
NULL, // symlink
|
2002-08-14 00:39:25 +04:00
|
|
|
NULL, // link
|
2004-10-29 06:24:30 +04:00
|
|
|
NULL, // unlink
|
|
|
|
NULL, // rename
|
2002-07-14 09:13:20 +04:00
|
|
|
|
2002-08-13 17:51:36 +04:00
|
|
|
NULL, // access
|
2002-07-14 09:13:20 +04:00
|
|
|
&devfs_read_stat,
|
|
|
|
&devfs_write_stat,
|
|
|
|
|
|
|
|
/* file */
|
|
|
|
&devfs_create,
|
|
|
|
&devfs_open,
|
|
|
|
&devfs_close,
|
|
|
|
&devfs_free_cookie,
|
2002-07-09 16:24:59 +04:00
|
|
|
&devfs_read,
|
|
|
|
&devfs_write,
|
2002-07-14 09:13:20 +04:00
|
|
|
|
|
|
|
/* directory */
|
|
|
|
&devfs_create_dir,
|
2002-08-14 00:39:25 +04:00
|
|
|
NULL, // remove_dir
|
2002-07-14 09:13:20 +04:00
|
|
|
&devfs_open_dir,
|
2005-05-27 04:43:01 +04:00
|
|
|
&devfs_close, // same as for files - it does nothing for directories, anyway
|
|
|
|
&devfs_free_dir_cookie,
|
2002-07-09 16:24:59 +04:00
|
|
|
&devfs_read_dir,
|
|
|
|
&devfs_rewind_dir,
|
2002-09-24 03:24:12 +04:00
|
|
|
|
|
|
|
// the other operations are not supported (attributes, indices, queries)
|
|
|
|
NULL,
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
// #pragma mark - device node
|
2004-06-07 05:18:56 +04:00
|
|
|
// temporary hack to get it to work with the current device manager
|
|
|
|
|
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
static device_manager_info *sDeviceManager;
|
2004-06-07 05:18:56 +04:00
|
|
|
|
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
static const device_attr pnp_devfs_attrs[] = {
|
|
|
|
{ B_DRIVER_MODULE, B_STRING_TYPE, { string: PNP_DEVFS_MODULE_NAME }},
|
2004-06-07 05:18:56 +04:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** someone registered a device */
|
|
|
|
|
|
|
|
static status_t
|
2005-01-18 19:51:21 +03:00
|
|
|
pnp_devfs_register_device(device_node_handle parent)
|
2004-06-07 05:18:56 +04:00
|
|
|
{
|
2005-05-12 07:27:44 +04:00
|
|
|
char *filename = NULL;
|
2005-01-12 02:26:00 +03:00
|
|
|
device_node_handle node;
|
2004-09-04 21:46:10 +04:00
|
|
|
status_t status;
|
2004-06-07 05:18:56 +04:00
|
|
|
|
|
|
|
TRACE(("pnp_devfs_probe()\n"));
|
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
if (sDeviceManager->get_attr_string(parent, PNP_DEVFS_FILENAME, &filename, true) != B_OK) {
|
2004-09-04 21:46:10 +04:00
|
|
|
dprintf("devfs: Item containing file name is missing\n");
|
|
|
|
status = B_ERROR;
|
|
|
|
goto err1;
|
2004-06-07 05:18:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(("Adding %s\n", filename));
|
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
status = sDeviceManager->register_device(parent, pnp_devfs_attrs, NULL, &node);
|
|
|
|
if (status != B_OK || node == NULL)
|
2004-09-04 21:46:10 +04:00
|
|
|
goto err1;
|
2004-06-07 05:18:56 +04:00
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
// ToDo: this is a hack to get things working (init_driver() only works for registered nodes)
|
|
|
|
parent->registered = true;
|
|
|
|
|
|
|
|
pnp_devfs_driver_info *info;
|
|
|
|
status = sDeviceManager->init_driver(parent, NULL, (driver_module_info **)&info, NULL);
|
|
|
|
if (status != B_OK)
|
2004-06-07 05:18:56 +04:00
|
|
|
goto err2;
|
|
|
|
|
|
|
|
//add_device(device);
|
2005-06-02 22:32:21 +04:00
|
|
|
status = publish_device(sDeviceFileSystem, filename, node, info, NULL, NULL);
|
2004-10-29 05:41:16 +04:00
|
|
|
if (status != B_OK)
|
|
|
|
goto err3;
|
2004-06-07 05:18:56 +04:00
|
|
|
//nudge();
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
|
2004-10-29 05:41:16 +04:00
|
|
|
err3:
|
2005-05-12 07:27:44 +04:00
|
|
|
sDeviceManager->uninit_driver(parent);
|
2004-06-07 05:18:56 +04:00
|
|
|
err2:
|
2005-05-12 07:27:44 +04:00
|
|
|
sDeviceManager->unregister_device(node);
|
2004-09-04 21:46:10 +04:00
|
|
|
err1:
|
2004-06-07 05:18:56 +04:00
|
|
|
free(filename);
|
|
|
|
|
2004-09-04 21:46:10 +04:00
|
|
|
return status;
|
2004-06-07 05:18:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// remove device from public list and add it to unpublish list
|
|
|
|
// (devices_lock must be hold)
|
|
|
|
static void
|
|
|
|
pnp_devfs_remove_device(device_info *device)
|
|
|
|
{
|
|
|
|
TRACE(("removing device %s from public list\n", device->name));
|
|
|
|
|
|
|
|
--num_devices;
|
|
|
|
REMOVE_DL_LIST( device, devices, );
|
|
|
|
|
|
|
|
++num_unpublished_devices;
|
|
|
|
ADD_DL_LIST_HEAD( device, devices_to_unpublish, );
|
|
|
|
|
|
|
|
// (don't free it even if no handle is open - the device
|
|
|
|
// info block contains the hook list which may just got passed
|
|
|
|
// to the devfs layer; we better wait until next
|
|
|
|
// publish_devices, so we are sure that devfs won't access
|
|
|
|
// the hook list anymore)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// device got removed
|
|
|
|
static void
|
2005-01-12 02:26:00 +03:00
|
|
|
pnp_devfs_device_removed(device_node_handle node, void *cookie)
|
2004-06-07 05:18:56 +04:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
device_info *device;
|
2005-01-12 02:26:00 +03:00
|
|
|
device_node_handle parent;
|
2004-06-07 05:18:56 +04:00
|
|
|
status_t res = B_OK;
|
|
|
|
#endif
|
|
|
|
TRACE(("pnp_devfs_device_removed()\n"));
|
|
|
|
#if 0
|
2005-05-12 07:27:44 +04:00
|
|
|
parent = sDeviceManager->get_parent(node);
|
2004-06-07 05:18:56 +04:00
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
// don't use cookie - we don't use sDeviceManager loading scheme but
|
2004-06-07 05:18:56 +04:00
|
|
|
// global data and keep care of everything ourself!
|
|
|
|
ACQUIRE_BEN( &device_list_lock );
|
|
|
|
|
|
|
|
for( device = devices; device; device = device->next ) {
|
|
|
|
if( device->parent == parent )
|
|
|
|
break;
|
|
|
|
}
|
2005-05-12 07:27:44 +04:00
|
|
|
|
2004-06-07 05:18:56 +04:00
|
|
|
if( device != NULL ) {
|
2005-05-12 07:27:44 +04:00
|
|
|
pnp_devfs_remove_device(device);
|
2004-06-07 05:18:56 +04:00
|
|
|
} else {
|
|
|
|
SHOW_ERROR( 0, "bug: node %p couldn't been found", node );
|
|
|
|
res = B_NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE_BEN( &device_list_lock );
|
|
|
|
|
|
|
|
//nudge();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2004-06-07 21:22:32 +04:00
|
|
|
pnp_devfs_std_ops(int32 op, ...)
|
2004-06-07 05:18:56 +04:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case B_MODULE_INIT:
|
2005-05-12 07:27:44 +04:00
|
|
|
return get_module(B_DEVICE_MANAGER_MODULE_NAME, (module_info **)&sDeviceManager);
|
2004-06-07 05:18:56 +04:00
|
|
|
|
|
|
|
case B_MODULE_UNINIT:
|
2005-05-12 07:27:44 +04:00
|
|
|
put_module(B_DEVICE_MANAGER_MODULE_NAME);
|
2004-06-07 05:18:56 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-12 02:26:00 +03:00
|
|
|
driver_module_info gDeviceForDriversModule = {
|
2004-06-07 05:18:56 +04:00
|
|
|
{
|
|
|
|
PNP_DEVFS_MODULE_NAME,
|
|
|
|
0 /*B_KEEP_LOADED*/,
|
2004-06-07 21:22:32 +04:00
|
|
|
pnp_devfs_std_ops
|
2004-06-07 05:18:56 +04:00
|
|
|
},
|
|
|
|
|
2005-05-12 07:27:44 +04:00
|
|
|
NULL, // supports device
|
2005-01-18 19:51:21 +03:00
|
|
|
pnp_devfs_register_device,
|
2005-05-12 07:27:44 +04:00
|
|
|
NULL, // init driver
|
|
|
|
NULL, // uninit driver
|
|
|
|
pnp_devfs_device_removed,
|
|
|
|
NULL, // cleanup
|
|
|
|
NULL, // get paths
|
2004-06-07 05:18:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
// #pragma mark - kernel private API
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
devfs_add_driver(const char *path)
|
|
|
|
{
|
|
|
|
// see if we already know this driver
|
|
|
|
|
|
|
|
struct stat stat;
|
|
|
|
if (::stat(path, &stat) != 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
RecursiveLocker locker(&sDeviceFileSystem->lock);
|
|
|
|
|
|
|
|
driver_entry *driver = (driver_entry *)hash_lookup(
|
|
|
|
sDeviceFileSystem->driver_hash, &stat.st_ino);
|
|
|
|
if (driver != NULL) {
|
|
|
|
// we know this driver
|
|
|
|
// ToDo: test for changes here? Although node monitoring should be enough.
|
|
|
|
if (driver->image < B_OK)
|
|
|
|
return driver->image;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't know this driver, create a new entry for it
|
|
|
|
|
|
|
|
driver = (driver_entry *)malloc(sizeof(driver_entry));
|
|
|
|
if (driver == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
driver->path = strdup(path);
|
|
|
|
if (driver->path == NULL) {
|
|
|
|
free(driver);
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
driver->device = stat.st_dev;
|
|
|
|
driver->node = stat.st_ino;
|
|
|
|
driver->last_modified = stat.st_mtime;
|
|
|
|
|
|
|
|
hash_insert(sDeviceFileSystem->driver_hash, driver);
|
|
|
|
|
|
|
|
// Even if loading the driver fails - its entry will stay with us
|
|
|
|
// so that we don't have to go through it again
|
|
|
|
return load_driver(driver);
|
|
|
|
}
|
2004-06-07 05:18:56 +04:00
|
|
|
|
|
|
|
|
2004-10-29 05:41:16 +04:00
|
|
|
extern "C" status_t
|
|
|
|
devfs_unpublish_file_device(const char *path)
|
|
|
|
{
|
2004-10-29 06:24:30 +04:00
|
|
|
return unpublish_node(sDeviceFileSystem, path, S_IFLNK);
|
2004-10-29 05:41:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
devfs_publish_file_device(const char *path, const char *filePath)
|
|
|
|
{
|
2004-10-29 05:58:08 +04:00
|
|
|
struct devfs_vnode *node;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
filePath = strdup(filePath);
|
|
|
|
if (filePath == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&sDeviceFileSystem->lock);
|
2004-10-29 05:58:08 +04:00
|
|
|
|
|
|
|
status = publish_node(sDeviceFileSystem, path, &node);
|
|
|
|
if (status != B_OK)
|
2005-06-02 22:32:21 +04:00
|
|
|
return status;
|
2004-10-29 05:58:08 +04:00
|
|
|
|
|
|
|
// all went fine, let's initialize the node
|
|
|
|
node->stream.type = S_IFLNK | 0644;
|
|
|
|
node->stream.u.symlink.path = filePath;
|
|
|
|
node->stream.u.symlink.length = strlen(filePath);
|
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
return B_OK;
|
2004-10-29 05:41:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-27 21:57:43 +04:00
|
|
|
extern "C" status_t
|
2004-06-15 19:32:22 +04:00
|
|
|
devfs_unpublish_partition(const char *path)
|
|
|
|
{
|
2004-10-29 06:24:30 +04:00
|
|
|
return unpublish_node(sDeviceFileSystem, path, S_IFCHR);
|
2004-06-15 19:32:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-27 21:57:43 +04:00
|
|
|
extern "C" status_t
|
2004-06-15 19:32:22 +04:00
|
|
|
devfs_publish_partition(const char *path, const partition_info *info)
|
|
|
|
{
|
2004-10-27 19:21:11 +04:00
|
|
|
if (path == NULL || info == NULL)
|
2004-06-15 19:32:22 +04:00
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2004-10-27 19:21:11 +04:00
|
|
|
TRACE(("publish partition: %s (device \"%s\", offset %Ld, size %Ld)\n",
|
|
|
|
path, info->device, info->offset, info->size));
|
|
|
|
|
|
|
|
// the partition and device paths must be the same until the leaves
|
|
|
|
const char *lastPath = strrchr(path, '/');
|
|
|
|
const char *lastDevice = strrchr(info->device, '/');
|
|
|
|
if (lastPath == NULL || lastDevice == NULL
|
|
|
|
|| lastPath - path != lastDevice - info->device
|
|
|
|
|| strncmp(path, info->device, lastPath - path))
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
devfs_vnode *device;
|
2004-10-29 06:24:30 +04:00
|
|
|
status_t status = get_node_for_path(sDeviceFileSystem, info->device, &device);
|
2004-10-27 19:21:11 +04:00
|
|
|
if (status != B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
status = add_partition(sDeviceFileSystem, device, lastPath + 1, *info);
|
|
|
|
|
|
|
|
put_vnode(sDeviceFileSystem->id, device->id);
|
|
|
|
return status;
|
2004-06-15 19:32:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-27 21:57:43 +04:00
|
|
|
extern "C" status_t
|
2004-09-04 21:46:10 +04:00
|
|
|
devfs_publish_device(const char *path, void *obsolete, device_hooks *ops)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-06-02 22:32:21 +04:00
|
|
|
return publish_device(sDeviceFileSystem, path, NULL, NULL, NULL, ops);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-07-11 01:55:33 +04:00
|
|
|
|
2005-01-25 05:17:02 +03:00
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
devfs_publish_directory(const char *path)
|
|
|
|
{
|
2005-06-02 22:32:21 +04:00
|
|
|
RecursiveLocker locker(&sDeviceFileSystem->lock);
|
2005-01-25 05:17:02 +03:00
|
|
|
|
2005-06-02 22:32:21 +04:00
|
|
|
return publish_directory(sDeviceFileSystem, path);
|
2005-01-25 05:17:02 +03:00
|
|
|
}
|
|
|
|
|