RAMFS: Properly check that the Lockers are actually locked.

They pretty much always will be, but better to be safe. Also
de-indent one level and use an early return for this.
This commit is contained in:
Augustin Cavalier 2024-11-22 18:07:26 -05:00
parent 374d7a1eb1
commit e2e7d84d21

View File

@ -1509,16 +1509,17 @@ private:
};
// ramfs_create_attr
static status_t
ramfs_create_attr(fs_volume* _volume, fs_vnode* _node, const char *name,
uint32 type, int openMode, void** _cookie)
{
Volume* volume = (Volume*)_volume->private_volume;
Node* node = (Node*)_node->private_node;
if (VolumeWriteLocker locker = volume) {
VolumeWriteLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// try to find the attribute
Attribute *attribute = NULL;
node->FindAttribute(name, &attribute);
@ -1573,26 +1574,26 @@ ramfs_create_attr(fs_volume* _volume, fs_vnode* _node, const char *name,
// success
cookieDeleter.Detach();
*_cookie = cookie;
} else
RETURN_ERROR(B_ERROR);
return B_OK;
}
// ramfs_open_attr
static status_t
ramfs_open_attr(fs_volume* _volume, fs_vnode* _node, const char *name,
int openMode, void** _cookie)
{
// FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
Node* node = (Node*)_node->private_node;
FUNCTION(("node: %lld\n", node->GetID()));
status_t error = B_OK;
if (VolumeWriteLocker locker = volume) {
VolumeWriteLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// find the attribute
Attribute *attribute = NULL;
if (error == B_OK)
@ -1634,33 +1635,29 @@ ramfs_open_attr(fs_volume* _volume, fs_vnode* _node, const char *name,
*_cookie = cookie;
else if (cookie)
delete cookie;
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_close_attr
static status_t
ramfs_close_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
// FUNCTION_START();
FUNCTION(("node: %lld\n", node->GetID()));
Volume* volume = (Volume*)_volume->private_volume;
Node* node = (Node*)_node->private_node;
FUNCTION(("node: %lld\n", node->GetID()));
status_t error = B_OK;
VolumeReadLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// notify listeners
if (VolumeReadLocker locker = volume) {
notify_if_stat_changed(volume, node);
} else
SET_ERROR(error, B_ERROR);
return error;
return B_OK;
}
// ramfs_free_attr_cookie
static status_t
ramfs_free_attr_cookie(fs_volume* /*fs*/, fs_vnode* /*_node*/, void* _cookie)
{
@ -1671,19 +1668,23 @@ ramfs_free_attr_cookie(fs_volume* /*fs*/, fs_vnode* /*_node*/, void* _cookie)
}
// ramfs_read_attr
static status_t
ramfs_read_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie, off_t pos,
void *buffer, size_t *bufferSize)
{
// FUNCTION_START();
FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
Node* node = (Node*)_node->private_node;
AttributeCookie *cookie = (AttributeCookie*)_cookie;
VolumeReadLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
status_t error = B_OK;
if (VolumeReadLocker locker = volume) {
// find the attribute
Attribute *attribute = NULL;
if (error == B_OK)
@ -1697,18 +1698,17 @@ ramfs_read_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie, off_t pos,
// read
if (error == B_OK)
error = attribute->ReadAt(pos, buffer, *bufferSize, bufferSize);
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_write_attr
static status_t
ramfs_write_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie,
off_t pos, const void *buffer, size_t *bufferSize)
{
// FUNCTION_START();
FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
Node* node = (Node*)_node->private_node;
AttributeCookie *cookie = (AttributeCookie*)_cookie;
@ -1723,7 +1723,10 @@ ramfs_write_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie,
RETURN_ERROR(B_NOT_ALLOWED);
}
if (VolumeWriteLocker locker = volume) {
VolumeWriteLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
NodeMTimeUpdater mTimeUpdater(node);
// find the attribute
@ -1745,25 +1748,26 @@ ramfs_write_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie,
notify_attribute_changed(volume->GetID(), -1, node->GetID(), name,
B_ATTR_CHANGED);
}
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_read_attr_stat
static status_t
ramfs_read_attr_stat(fs_volume* _volume, fs_vnode* _node, void* _cookie,
struct stat *st)
{
// FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
Node* node = (Node*)_node->private_node;
AttributeCookie *cookie = (AttributeCookie*)_cookie;
status_t error = B_OK;
if (VolumeReadLocker locker = volume) {
VolumeReadLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// find the attribute
Attribute *attribute = NULL;
if (error == B_OK)
@ -1779,13 +1783,11 @@ ramfs_read_attr_stat(fs_volume* _volume, fs_vnode* _node, void* _cookie,
st->st_type = attribute->GetType();
st->st_size = attribute->GetSize();
}
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_rename_attr
static status_t
ramfs_rename_attr(fs_volume* /*fs*/, fs_vnode* /*_fromNode*/,
const char */*fromName*/, fs_vnode* /*_toNode*/, const char */*toName*/)
@ -1795,7 +1797,6 @@ ramfs_rename_attr(fs_volume* /*fs*/, fs_vnode* /*_fromNode*/,
}
// ramfs_remove_attr
static status_t
ramfs_remove_attr(fs_volume* _volume, fs_vnode* _node, const char *name)
{
@ -1804,7 +1805,10 @@ ramfs_remove_attr(fs_volume* _volume, fs_vnode* _node, const char *name)
Node* node = (Node*)_node->private_node;
status_t error = B_OK;
if (VolumeWriteLocker locker = volume) {
VolumeWriteLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
NodeMTimeUpdater mTimeUpdater(node);
// check permissions
@ -1824,8 +1828,6 @@ ramfs_remove_attr(fs_volume* _volume, fs_vnode* _node, const char *name)
notify_attribute_changed(volume->GetID(), -1, node->GetID(), name,
B_ATTR_REMOVED);
}
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
@ -1843,14 +1845,17 @@ public:
};
// ramfs_open_index_dir
static status_t
ramfs_open_index_dir(fs_volume* _volume, void** _cookie)
{
FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
VolumeReadLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
status_t error = B_OK;
if (VolumeReadLocker locker = volume) {
// check whether an index directory exists
if (volume->GetIndexDirectory()) {
IndexDirCookie *cookie = new(nothrow) IndexDirCookie;
@ -1860,13 +1865,11 @@ ramfs_open_index_dir(fs_volume* _volume, void** _cookie)
SET_ERROR(error, B_NO_MEMORY);
} else
SET_ERROR(error, B_ENTRY_NOT_FOUND);
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_close_index_dir
static status_t
ramfs_close_index_dir(fs_volume* /*fs*/, void* /*_cookie*/)
{
@ -1875,7 +1878,6 @@ ramfs_close_index_dir(fs_volume* /*fs*/, void* /*_cookie*/)
}
// ramfs_free_index_dir_cookie
static status_t
ramfs_free_index_dir_cookie(fs_volume* /*fs*/, void* _cookie)
{
@ -1886,7 +1888,6 @@ ramfs_free_index_dir_cookie(fs_volume* /*fs*/, void* _cookie)
}
// ramfs_read_index_dir
static status_t
ramfs_read_index_dir(fs_volume* _volume, void* _cookie,
struct dirent *buffer, size_t bufferSize, uint32 *count)
@ -1896,7 +1897,10 @@ ramfs_read_index_dir(fs_volume* _volume, void* _cookie,
IndexDirCookie *cookie = (IndexDirCookie*)_cookie;
status_t error = B_OK;
if (VolumeReadLocker locker = volume) {
VolumeReadLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// get the next index
Index *index = volume->GetIndexDirectory()->IndexAt(
cookie->index_index++);
@ -1918,14 +1922,11 @@ ramfs_read_index_dir(fs_volume* _volume, void* _cookie,
}
} else
*count = 0;
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_rewind_index_dir
static status_t
ramfs_rewind_index_dir(fs_volume* /*fs*/, void* _cookie)
{
@ -1936,7 +1937,6 @@ ramfs_rewind_index_dir(fs_volume* /*fs*/, void* _cookie)
}
// ramfs_create_index
static status_t
ramfs_create_index(fs_volume* _volume, const char *name, uint32 type,
uint32 /*flags*/)
@ -1946,9 +1946,13 @@ ramfs_create_index(fs_volume* _volume, const char *name, uint32 type,
status_t error = B_OK;
// only root is allowed to manipulate the indices
if (geteuid() != 0) {
SET_ERROR(error, B_NOT_ALLOWED);
} else if (VolumeWriteLocker locker = volume) {
if (geteuid() != 0)
RETURN_ERROR(B_NOT_ALLOWED);
VolumeWriteLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// get the index directory
if (IndexDirectory *indexDir = volume->GetIndexDirectory()) {
// check whether an index with that name does already exist
@ -1961,24 +1965,26 @@ ramfs_create_index(fs_volume* _volume, const char *name, uint32 type,
}
} else
SET_ERROR(error, B_ENTRY_NOT_FOUND);
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_remove_index
static status_t
ramfs_remove_index(fs_volume* _volume, const char *name)
{
FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
status_t error = B_OK;
// only root is allowed to manipulate the indices
if (geteuid() != 0) {
SET_ERROR(error, B_NOT_ALLOWED);
} else if (VolumeWriteLocker locker = volume) {
if (geteuid() != 0)
RETURN_ERROR(B_NOT_ALLOWED);
VolumeWriteLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// get the index directory
if (IndexDirectory *indexDir = volume->GetIndexDirectory()) {
// check whether an index with that name does exist
@ -1992,20 +1998,22 @@ ramfs_remove_index(fs_volume* _volume, const char *name)
SET_ERROR(error, B_ENTRY_NOT_FOUND);
} else
SET_ERROR(error, B_ENTRY_NOT_FOUND);
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// ramfs_read_index_stat
static status_t
ramfs_read_index_stat(fs_volume* _volume, const char *name, struct stat *st)
{
FUNCTION_START();
Volume* volume = (Volume*)_volume->private_volume;
status_t error = B_OK;
if (VolumeReadLocker locker = volume) {
VolumeReadLocker locker(volume);
if (!locker.IsLocked())
RETURN_ERROR(B_ERROR);
// get the index directory
if (IndexDirectory *indexDir = volume->GetIndexDirectory()) {
// find the index
@ -2025,17 +2033,14 @@ ramfs_read_index_stat(fs_volume* _volume, const char *name, struct stat *st)
SET_ERROR(error, B_ENTRY_NOT_FOUND);
} else
SET_ERROR(error, B_ENTRY_NOT_FOUND);
} else
SET_ERROR(error, B_ERROR);
RETURN_ERROR(error);
}
// #pragma mark - Queries
// Query implementation by Axel Dörfler. Slightly adjusted.
// ramfs_open_query
static status_t
ramfs_open_query(fs_volume* _volume, const char *queryString, uint32 flags,
port_id port, uint32 token, void** _cookie)
@ -2064,7 +2069,6 @@ ramfs_open_query(fs_volume* _volume, const char *queryString, uint32 flags,
}
// ramfs_close_query
static status_t
ramfs_close_query(fs_volume* /*fs*/, void* /*cookie*/)
{
@ -2073,7 +2077,6 @@ ramfs_close_query(fs_volume* /*fs*/, void* /*cookie*/)
}
// ramfs_free_query_cookie
static status_t
ramfs_free_query_cookie(fs_volume* _volume, void* _cookie)
{
@ -2093,7 +2096,6 @@ ramfs_free_query_cookie(fs_volume* _volume, void* _cookie)
}
// ramfs_read_query
static status_t
ramfs_read_query(fs_volume* _volume, void* _cookie, struct dirent *buffer,
size_t bufferSize, uint32 *count)