* Copyright 2017, Chế Vũ Gia Hy, cvghy116@gmail.com.
* Copyright 2011, Jérôme Duval, korli@users.berlios.de.
* Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
* This file may be used under the terms of the MIT License.
*/
#include "Attribute.h"
#include "AttributeIterator.h"
#include "btrfs.h"
#include "DirectoryIterator.h"
#include "Inode.h"
#include "Utility.h"
#ifdef TRACE_BTRFS
# define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
#else
# define TRACE(x...) ;
#endif
#define ERROR(x...) dprintf("\33[34mbtrfs:\33[0m " x)
#define BTRFS_IO_SIZE 65536
struct identify_cookie {
btrfs_super_block super_block;
};
static status_t
iterative_io_get_vecs_hook(void* cookie, io_request* request, off_t offset,
size_t size, struct file_io_vec* vecs, size_t* _count)
{
Inode* inode = (Inode*)cookie;
return file_map_translate(inode->Map(), offset, size, vecs, _count,
inode->GetVolume()->BlockSize());
}
static status_t
iterative_io_finished_hook(void* cookie, io_request* request, status_t status,
bool partialTransfer, size_t bytesTransferred)
{
Inode* inode = (Inode*)cookie;
rw_lock_read_unlock(inode->Lock());
return B_OK;
}
static float
btrfs_identify_partition(int fd, partition_data* partition, void** _cookie)
{
btrfs_super_block superBlock;
status_t status = Volume::Identify(fd, &superBlock);
if (status != B_OK)
return -1;
identify_cookie* cookie = new identify_cookie;
memcpy(&cookie->super_block, &superBlock, sizeof(btrfs_super_block));
*_cookie = cookie;
return 0.8f;
}
static status_t
btrfs_scan_partition(int fd, partition_data* partition, void* _cookie)
{
identify_cookie* cookie = (identify_cookie*)_cookie;
partition->status = B_PARTITION_VALID;
partition->flags |= B_PARTITION_FILE_SYSTEM;
partition->content_size = cookie->super_block.TotalSize();
partition->block_size = cookie->super_block.BlockSize();
partition->content_name = strdup(cookie->super_block.label);
if (partition->content_name == NULL)
return B_NO_MEMORY;
return B_OK;
}
static void
btrfs_free_identify_partition_cookie(partition_data* partition, void* _cookie)
{
delete (identify_cookie*)_cookie;
}
static status_t
btrfs_mount(fs_volume* _volume, const char* device, uint32 flags,
const char* args, ino_t* _rootID)
{
Volume* volume = new(std::nothrow) Volume(_volume);
if (volume == NULL)
return B_NO_MEMORY;
_volume->private_volume = volume;
_volume->ops = &gBtrfsVolumeOps;
status_t status = volume->Mount(device, flags);
if (status != B_OK) {
ERROR("Failed mounting the volume. Error: %s\n", strerror(status));
delete volume;
return status;
}
*_rootID = volume->RootNode()->ID();
return B_OK;
}
static status_t
btrfs_unmount(fs_volume* _volume)
{
Volume* volume = (Volume*)_volume->private_volume;
status_t status = volume->Unmount();
delete volume;
return status;
}
static status_t
btrfs_read_fs_info(fs_volume* _volume, struct fs_info* info)
{
Volume* volume = (Volume*)_volume->private_volume;
info->flags = B_FS_IS_PERSISTENT | B_FS_HAS_ATTR
| (volume->IsReadOnly() ? B_FS_IS_READONLY : 0);
info->io_size = BTRFS_IO_SIZE;
info->block_size = volume->BlockSize();
info->total_blocks = volume->SuperBlock().TotalSize() / volume->BlockSize();
info->free_blocks = 0;
strlcpy(info->volume_name, volume->Name(), sizeof(info->volume_name));
strlcpy(info->fsh_name, "btrfs", sizeof(info->fsh_name));
return B_OK;
}
static status_t
btrfs_get_vnode(fs_volume* _volume, ino_t id, fs_vnode* _node, int* _type,
uint32* _flags, bool reenter)
{
Volume* volume = (Volume*)_volume->private_volume;
Inode* inode = new(std::nothrow) Inode(volume, id);
if (inode == NULL)
return B_NO_MEMORY;
status_t status = inode->InitCheck();
if (status != B_OK) {
delete inode;
ERROR("get_vnode: InitCheck() failed. Error: %s\n", strerror(status));
return status;
}
_node->private_node = inode;
_node->ops = &gBtrfsVnodeOps;
*_type = inode->Mode();
*_flags = 0;
return B_OK;
}
static status_t
btrfs_put_vnode(fs_volume* _volume, fs_vnode* _node, bool reenter)
{
delete (Inode*)_node->private_node;
return B_OK;
}
static bool
btrfs_can_page(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
return true;
}
static status_t
btrfs_read_pages(fs_volume* _volume, fs_vnode* _node, void* _cookie,
off_t pos, const iovec* vecs, size_t count, size_t* _numBytes)
{
Volume* volume = (Volume*)_volume->private_volume;
Inode* inode = (Inode*)_node->private_node;
if (inode->FileCache() == NULL)
return B_BAD_VALUE;
rw_lock_read_lock(inode->Lock());
uint32 vecIndex = 0;
size_t vecOffset = 0;
size_t bytesLeft = *_numBytes;
status_t status;
while (true) {
file_io_vec fileVecs[8];
size_t fileVecCount = 8;
status = file_map_translate(inode->Map(), pos, bytesLeft, fileVecs,
&fileVecCount, 0);
if (status != B_OK && status != B_BUFFER_OVERFLOW)
break;
bool bufferOverflow = status == B_BUFFER_OVERFLOW;
size_t bytes = bytesLeft;
status = read_file_io_vec_pages(volume->Device(), fileVecs,
fileVecCount, vecs, count, &vecIndex, &vecOffset, &bytes);
if (status != B_OK || !bufferOverflow)
break;
pos += bytes;
bytesLeft -= bytes;
}
rw_lock_read_unlock(inode->Lock());
return status;
}
static status_t
btrfs_io(fs_volume* _volume, fs_vnode* _node, void* _cookie, io_request* request)
{
Volume* volume = (Volume*)_volume->private_volume;
Inode* inode = (Inode*)_node->private_node;
#ifndef FS_SHELL
if (io_request_is_write(request) && volume->IsReadOnly()) {
notify_io_request(request, B_READ_ONLY_DEVICE);
return B_READ_ONLY_DEVICE;
}
#endif
if (inode->FileCache() == NULL) {
#ifndef FS_SHELL
notify_io_request(request, B_BAD_VALUE);
#endif
return B_BAD_VALUE;
}
rw_lock_read_lock(inode->Lock());
return do_iterative_fd_io(volume->Device(), request,
iterative_io_get_vecs_hook, iterative_io_finished_hook, inode);
}
static status_t
btrfs_get_file_map(fs_volume* _volume, fs_vnode* _node, off_t offset,
size_t size, struct file_io_vec* vecs, size_t* _count)
{
TRACE("btrfs_get_file_map()\n");
Inode* inode = (Inode*)_node->private_node;
size_t index = 0, max = *_count;
while (true) {
off_t blockOffset;
off_t blockLength;
status_t status = inode->FindBlock(offset, blockOffset, &blockLength);
if (status != B_OK)
return status;
if (index > 0 && (vecs[index - 1].offset
== blockOffset - vecs[index - 1].length)) {
vecs[index - 1].length += blockLength;
} else {
if (index >= max) {
*_count = index;
return B_BUFFER_OVERFLOW;
}
vecs[index].offset = blockOffset;
vecs[index].length = blockLength;
index++;
}
offset += blockLength;
size -= blockLength;
if ((off_t)size <= vecs[index - 1].length || offset >= inode->Size()) {
*_count = index;
TRACE("btrfs_get_file_map for inode %" B_PRIdINO "\n", inode->ID());
return B_OK;
}
}
return B_ERROR;
}
static status_t
btrfs_lookup(fs_volume* _volume, fs_vnode* _directory, const char* name,
ino_t* _vnodeID)
{
TRACE("btrfs_lookup: name address: %p (%s)\n", name, name);
Volume* volume = (Volume*)_volume->private_volume;
Inode* directory = (Inode*)_directory->private_node;
status_t status = directory->CheckPermissions(X_OK);
if (status < B_OK)
return status;
status = DirectoryIterator(directory).Lookup(name, strlen(name), _vnodeID);
if (status != B_OK)
return status;
return get_vnode(volume->FSVolume(), *_vnodeID, NULL);
}
static status_t
btrfs_ioctl(fs_volume* _volume, fs_vnode* _node, void* _cookie, uint32 cmd,
void* buffer, size_t bufferLength)
{
TRACE("ioctl: %" B_PRIu32 "\n", cmd);
return B_DEV_INVALID_IOCTL;
}
static status_t
btrfs_read_stat(fs_volume* _volume, fs_vnode* _node, struct stat* stat)
{
Inode* inode = (Inode*)_node->private_node;
stat->st_dev = inode->GetVolume()->ID();
stat->st_ino = inode->ID();
stat->st_nlink = 1;
stat->st_blksize = BTRFS_IO_SIZE;
stat->st_uid = inode->UserID();
stat->st_gid = inode->GroupID();
stat->st_mode = inode->Mode();
stat->st_type = 0;
inode->GetAccessTime(stat->st_atim);
inode->GetModificationTime(stat->st_mtim);
inode->GetChangeTime(stat->st_ctim);
inode->GetCreationTime(stat->st_crtim);
stat->st_size = inode->Size();
stat->st_blocks = (inode->Size() + 511) / 512;
return B_OK;
}
static status_t
btrfs_open(fs_volume* , fs_vnode* _node, int openMode,
void** _cookie)
{
Inode* inode = (Inode*)_node->private_node;
if (inode->IsDirectory() && (openMode & O_RWMASK) != 0)
return B_IS_A_DIRECTORY;
status_t status = inode->CheckPermissions(open_mode_to_access(openMode)
| (openMode & O_TRUNC ? W_OK : 0));
if (status != B_OK)
return status;
file_cookie* cookie = new(std::nothrow) file_cookie;
if (cookie == NULL)
return B_NO_MEMORY;
ObjectDeleter<file_cookie> cookieDeleter(cookie);
cookie->open_mode = openMode & BTRFS_OPEN_MODE_USER_MASK;
cookie->last_size = inode->Size();
cookie->last_notification = system_time();
if ((openMode & O_NOCACHE) != 0 && inode->FileCache() != NULL) {
status = file_cache_disable(inode->FileCache());
if (status != B_OK)
return status;
}
cookieDeleter.Detach();
*_cookie = cookie;
return B_OK;
}
static status_t
btrfs_read(fs_volume* _volume, fs_vnode* _node, void* _cookie, off_t pos,
void* buffer, size_t* _length)
{
Inode* inode = (Inode*)_node->private_node;
if (!inode->IsFile()) {
*_length = 0;
return inode->IsDirectory() ? B_IS_A_DIRECTORY : B_BAD_VALUE;
}
return inode->ReadAt(pos, (uint8*)buffer, _length);
}
static status_t
btrfs_close(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
return B_OK;
}
static status_t
btrfs_free_cookie(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
file_cookie* cookie = (file_cookie*)_cookie;
Volume* volume = (Volume*)_volume->private_volume;
Inode* inode = (Inode*)_node->private_node;
if (inode->Size() != cookie->last_size)
notify_stat_changed(volume->ID(), -1, inode->ID(), B_STAT_SIZE);
delete cookie;
return B_OK;
}
static status_t
btrfs_access(fs_volume* _volume, fs_vnode* _node, int accessMode)
{
Inode* inode = (Inode*)_node->private_node;
return inode->CheckPermissions(accessMode);
}
static status_t
btrfs_read_link(fs_volume* _volume, fs_vnode* _node, char* buffer,
size_t* _bufferSize)
{
Inode* inode = (Inode*)_node->private_node;
return inode->ReadAt(0, (uint8*)buffer, _bufferSize);
}
static status_t
btrfs_create_dir(fs_volume* _volume, fs_vnode* _directory, const char* name,
int mode)
{
Volume* volume = (Volume*)_volume->private_volume;
Inode* directory = (Inode*)_directory->private_node;
BTree::Path path(volume->FSTree());
if (volume->IsReadOnly())
return B_READ_ONLY_DEVICE;
if (!directory->IsDirectory())
return B_NOT_A_DIRECTORY;
status_t status = directory->CheckPermissions(W_OK);
if (status < B_OK)
return status;
Transaction transaction(volume);
ino_t id = volume->GetNextInodeID();
mode = S_DIRECTORY | (mode & S_IUMSK);
Inode* inode = Inode::Create(transaction, id, directory, mode);
if (inode == NULL)
return B_NO_MEMORY;
status = inode->Insert(transaction, &path);
if (status != B_OK)
return status;
status = inode->MakeReference(transaction, &path, directory, name, mode);
if (status != B_OK)
return status;
put_vnode(volume->FSVolume(), inode->ID());
entry_cache_add(volume->ID(), directory->ID(), name, inode->ID());
status = transaction.Done();
if (status == B_OK)
notify_entry_created(volume->ID(), directory->ID(), name, inode->ID());
else
entry_cache_remove(volume->ID(), directory->ID(), name);
return status;
}
static status_t
btrfs_remove_dir(fs_volume* _volume, fs_vnode* _directory, const char* name)
{
Volume* volume = (Volume*)_volume->private_volume;
Inode* directory = (Inode*)_directory->private_node;
Transaction transaction(volume);
BTree::Path path(volume->FSTree());
ino_t id;
status_t status = DirectoryIterator(directory).Lookup(name, strlen(name),
&id);
if (status != B_OK)
return status;
Inode inode(volume, id);
status = inode.InitCheck();
if (status != B_OK)
return status;
status = inode.Remove(transaction, &path);
if (status != B_OK)
return status;
status = inode.Dereference(transaction, &path, directory->ID(), name);
if (status != B_OK)
return status;
entry_cache_remove(volume->ID(), directory->ID(), name);
entry_cache_remove(volume->ID(), id, "..");
status = transaction.Done();
if (status == B_OK)
notify_entry_removed(volume->ID(), directory->ID(), name, id);
else {
entry_cache_add(volume->ID(), directory->ID(), name, id);
entry_cache_add(volume->ID(), id, "..", id);
}
return status;
}
static status_t
btrfs_open_dir(fs_volume* , fs_vnode* _node, void** _cookie)
{
Inode* inode = (Inode*)_node->private_node;
status_t status = inode->CheckPermissions(R_OK);
if (status < B_OK)
return status;
if (!inode->IsDirectory())
return B_NOT_A_DIRECTORY;
DirectoryIterator* iterator = new(std::nothrow) DirectoryIterator(inode);
if (iterator == NULL || iterator->InitCheck() != B_OK) {
delete iterator;
return B_NO_MEMORY;
}
*_cookie = iterator;
return B_OK;
}
static status_t
btrfs_read_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie,
struct dirent* dirent, size_t bufferSize, uint32* _num)
{
DirectoryIterator* iterator = (DirectoryIterator*)_cookie;
Volume* volume = (Volume*)_volume->private_volume;
uint32 maxCount = *_num;
uint32 count = 0;
while (count < maxCount && bufferSize > sizeof(struct dirent)) {
ino_t id;
size_t length = bufferSize - sizeof(struct dirent) + 1;
status_t status = iterator->GetNext(dirent->d_name, &length,
&id);
if (status == B_ENTRY_NOT_FOUND)
break;
if (status == B_BUFFER_OVERFLOW) {
if (count == 0)
return B_BUFFER_OVERFLOW;
break;
}
if (status != B_OK)
return status;
dirent->d_dev = volume->ID();
dirent->d_ino = id;
dirent->d_reclen = sizeof(struct dirent) + length;
bufferSize -= dirent->d_reclen;
dirent = (struct dirent*)((uint8*)dirent + dirent->d_reclen);
count++;
}
*_num = count;
return B_OK;
}
static status_t
btrfs_rewind_dir(fs_volume* , fs_vnode* , void* _cookie)
{
DirectoryIterator* iterator = (DirectoryIterator*)_cookie;
return iterator->Rewind();
}
static status_t
btrfs_close_dir(fs_volume * , fs_vnode * , void * )
{
return B_OK;
}
static status_t
btrfs_free_dir_cookie(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
delete (DirectoryIterator*)_cookie;
return B_OK;
}
static status_t
btrfs_open_attr_dir(fs_volume* _volume, fs_vnode* _node, void** _cookie)
{
Inode* inode = (Inode*)_node->private_node;
TRACE("%s()\n", __FUNCTION__);
if (!inode->IsFile())
return EINVAL;
AttributeIterator* iterator = new(std::nothrow) AttributeIterator(inode);
if (iterator == NULL || iterator->InitCheck() != B_OK) {
delete iterator;
return B_NO_MEMORY;
}
*_cookie = iterator;
return B_OK;
}
static status_t
btrfs_close_attr_dir(fs_volume* _volume, fs_vnode* _node, void* cookie)
{
TRACE("%s()\n", __FUNCTION__);
return B_OK;
}
static status_t
btrfs_free_attr_dir_cookie(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
TRACE("%s()\n", __FUNCTION__);
delete (AttributeIterator*)_cookie;
return B_OK;
}
static status_t
btrfs_read_attr_dir(fs_volume* _volume, fs_vnode* _node,
void* _cookie, struct dirent* dirent, size_t bufferSize,
uint32* _num)
{
TRACE("%s()\n", __FUNCTION__);
AttributeIterator* iterator = (AttributeIterator*)_cookie;
size_t length = bufferSize;
status_t status = iterator->GetNext(dirent->d_name, &length);
if (status == B_ENTRY_NOT_FOUND) {
*_num = 0;
return B_OK;
}
if (status != B_OK)
return status;
Volume* volume = (Volume*)_volume->private_volume;
dirent->d_dev = volume->ID();
dirent->d_reclen = sizeof(struct dirent) + length;
*_num = 1;
return B_OK;
}
static status_t
btrfs_rewind_attr_dir(fs_volume* _volume, fs_vnode* _node, void* _cookie)
{
AttributeIterator* iterator = (AttributeIterator*)_cookie;
return iterator->Rewind();
}
static status_t
btrfs_create_attr(fs_volume* _volume, fs_vnode* _node,
const char* name, uint32 type, int openMode, void** _cookie)
{
return EROFS;
}
static status_t
btrfs_open_attr(fs_volume* _volume, fs_vnode* _node, const char* name,
int openMode, void** _cookie)
{
TRACE("%s()\n", __FUNCTION__);
Inode* inode = (Inode*)_node->private_node;
Attribute attribute(inode);
return attribute.Open(name, openMode, (attr_cookie**)_cookie);
}
static status_t
btrfs_close_attr(fs_volume* _volume, fs_vnode* _node,
void* cookie)
{
return B_OK;
}
static status_t
btrfs_free_attr_cookie(fs_volume* _volume, fs_vnode* _node,
void* cookie)
{
delete (attr_cookie*)cookie;
return B_OK;
}
static status_t
btrfs_read_attr(fs_volume* _volume, fs_vnode* _node, void* _cookie,
off_t pos, void* buffer, size_t* _length)
{
TRACE("%s()\n", __FUNCTION__);
attr_cookie* cookie = (attr_cookie*)_cookie;
Inode* inode = (Inode*)_node->private_node;
Attribute attribute(inode, cookie);
return attribute.Read(cookie, pos, (uint8*)buffer, _length);
}
static status_t
btrfs_write_attr(fs_volume* _volume, fs_vnode* _node, void* cookie,
off_t pos, const void* buffer, size_t* length)
{
return EROFS;
}
static status_t
btrfs_read_attr_stat(fs_volume* _volume, fs_vnode* _node,
void* _cookie, struct stat* stat)
{
attr_cookie* cookie = (attr_cookie*)_cookie;
Inode* inode = (Inode*)_node->private_node;
Attribute attribute(inode, cookie);
return attribute.Stat(*stat);
}
static status_t
btrfs_write_attr_stat(fs_volume* _volume, fs_vnode* _node,
void* cookie, const struct stat* stat, int statMask)
{
return EROFS;
}
static status_t
btrfs_rename_attr(fs_volume* _volume, fs_vnode* fromVnode,
const char* fromName, fs_vnode* toVnode, const char* toName)
{
return EROFS;
}
static status_t
btrfs_remove_attr(fs_volume* _volume, fs_vnode* vnode,
const char* name)
{
return EROFS;
}
static status_t
btrfs_std_ops(int32 op, ...)
{
switch (op) {
case B_MODULE_INIT:
return B_OK;
case B_MODULE_UNINIT:
return B_OK;
default:
return B_ERROR;
}
}
fs_volume_ops gBtrfsVolumeOps = {
&btrfs_unmount,
&btrfs_read_fs_info,
NULL,
NULL,
&btrfs_get_vnode,
};
fs_vnode_ops gBtrfsVnodeOps = {
&btrfs_lookup,
NULL,
&btrfs_put_vnode,
NULL,
&btrfs_can_page,
&btrfs_read_pages,
NULL,
NULL,
NULL,
&btrfs_get_file_map,
&btrfs_ioctl,
NULL,
NULL,
NULL,
NULL,
&btrfs_read_link,
NULL,
NULL,
NULL,
NULL,
&btrfs_access,
&btrfs_read_stat,
NULL,
NULL,
NULL,
&btrfs_open,
&btrfs_close,
&btrfs_free_cookie,
&btrfs_read,
NULL,
&btrfs_create_dir,
&btrfs_remove_dir,
&btrfs_open_dir,
&btrfs_close_dir,
&btrfs_free_dir_cookie,
&btrfs_read_dir,
&btrfs_rewind_dir,
&btrfs_open_attr_dir,
&btrfs_close_attr_dir,
&btrfs_free_attr_dir_cookie,
&btrfs_read_attr_dir,
&btrfs_rewind_attr_dir,
&btrfs_create_attr,
&btrfs_open_attr,
&btrfs_close_attr,
&btrfs_free_attr_cookie,
&btrfs_read_attr,
&btrfs_write_attr,
&btrfs_read_attr_stat,
&btrfs_write_attr_stat,
&btrfs_rename_attr,
&btrfs_remove_attr,
};
static file_system_module_info sBtrfsFileSystem = {
{
"file_systems/btrfs" B_CURRENT_FS_API_VERSION,
0,
btrfs_std_ops,
},
"btrfs",
"btrfs File System",
0,
btrfs_identify_partition,
btrfs_scan_partition,
btrfs_free_identify_partition_cookie,
NULL,
&btrfs_mount,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
module_info* modules[] = {
(module_info*)&sBtrfsFileSystem,
NULL,
};