Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define TMPFS_TYPE_FILE 0x00
#define TMPFS_TYPE_DIR 0x01
#define TMPFS_TYPE_DYN_DEV 0x02 /* dynamic device */
#define TMPFS_TYPE_SOCKET 0x03

struct devtmpfs_sb;

Expand Down Expand Up @@ -223,6 +224,7 @@ static struct devtmpfs_file *devtmpfs_file_lookup(struct devtmpfs_sb *superblock
if (rt_strcmp(file->name, filename) == 0)
{
rt_spin_unlock(&superblock->lock);
/* cppcheck-suppress uninitvar */
return file;
}
}
Expand Down Expand Up @@ -325,6 +327,10 @@ static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_
{
d->d_type = DT_DIR;
}
if (n_file->type == TMPFS_TYPE_SOCKET)
{
d->d_type = DT_SOCK;
}

d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, n_file->name, DIRENT_NAME_MAX);
Expand Down Expand Up @@ -540,6 +546,14 @@ static struct dfs_vnode *devtmpfs_create_vnode(struct dfs_dentry *dentry, int ty
vnode->mode &= ~S_IFMT;
vnode->mode |= S_IFDIR;
}
else if (type == FT_SOCKET ||
(type == FT_REGULAR && S_ISSOCK(mode)))
{
d_file->type = TMPFS_TYPE_SOCKET;
vnode->type = FT_SOCKET;
vnode->mode &= ~S_IFMT;
vnode->mode |= S_IFSOCK;
}
else
{
d_file->type = TMPFS_TYPE_FILE;
Expand Down Expand Up @@ -585,6 +599,10 @@ static struct dfs_vnode *devtmpfs_lookup(struct dfs_dentry *dentry)
{
vnode->type = FT_DIRECTORY;
}
else if (d_file->type == TMPFS_TYPE_SOCKET)
{
vnode->type = FT_SOCKET;
}
else if (d_file->link)
{
vnode->type = FT_SYMLINK;
Expand Down
16 changes: 16 additions & 0 deletions components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,10 @@ static int dfs_tmpfs_getdents(struct dfs_file *file,
{
d->d_type = DT_DIR;
}
if (n_file->type == TMPFS_TYPE_SOCKET)
{
d->d_type = DT_SOCK;
}
d->d_namlen = RT_NAME_MAX;
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
Expand Down Expand Up @@ -664,6 +668,11 @@ static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
vnode->type = FT_DIRECTORY;
}
else if (d_file->type == TMPFS_TYPE_SOCKET)
{
vnode->mode = S_IFSOCK | (S_IRWXU | S_IRWXG | S_IRWXO);
vnode->type = FT_SOCKET;
}
else
{
vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
Expand Down Expand Up @@ -750,6 +759,13 @@ static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int t
vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
vnode->type = FT_DIRECTORY;
}
else if (type == FT_SOCKET ||
(type == FT_REGULAR && S_ISSOCK(mode)))
{
d_file->type = TMPFS_TYPE_SOCKET;
vnode->mode = S_IFSOCK | (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
vnode->type = FT_SOCKET;
}
else
{
d_file->type = TMPFS_TYPE_FILE;
Expand Down
2 changes: 1 addition & 1 deletion components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#define TMPFS_TYPE_FILE 0x00
#define TMPFS_TYPE_DIR 0x01
#define TMPFS_TYPE_SOCKET 0x02

struct tmpfs_sb;

Expand Down Expand Up @@ -46,4 +47,3 @@ struct tmpfs_sb
int dfs_tmpfs_init(void);

#endif

6 changes: 6 additions & 0 deletions components/dfs/dfs_v2/include/dfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file
struct dfs_file *fd_get(int fd);
void fd_release(int fd);

/* Reference helpers used when an open file description crosses fd tables. */
int dfs_file_get_refs(const int *fds, size_t count, struct dfs_file **files);
/* Successful installation transfers the supplied references to the fd table. */
int dfs_file_install_refs(struct dfs_file **files, size_t count, int *fds);
void dfs_file_put_ref(struct dfs_file *file);

void fd_init(struct dfs_file *fd);

struct dfs_fdtable *dfs_fdtable_get(void);
Expand Down
1 change: 1 addition & 0 deletions components/dfs/dfs_v2/include/dfs_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void dfs_file_init(struct dfs_file *file);
void dfs_file_deinit(struct dfs_file *file);

int dfs_file_open(struct dfs_file *file, const char *path, int flags, mode_t mode);
int dfs_file_mknod(const char *path, int type, mode_t mode);
int dfs_file_close(struct dfs_file *file);

off_t dfs_file_get_fpos(struct dfs_file *file);
Expand Down
114 changes: 110 additions & 4 deletions components/dfs/dfs_v2/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,29 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
*/
void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
{
if (fd < fdt->maxfd)
if (fdt == RT_NULL || dfs_file_lock() != RT_EOK)
{
return;
}

if (fd >= 0 && fd < (int)fdt->maxfd)
{
struct dfs_file *file;

file = fdt_get_file(fdt, fd);

if (file && file->ref_count == 1)
if (file != RT_NULL && file->ref_count == 1)
{
dfs_file_destroy(file);
}
else
else if (file != RT_NULL)
{
rt_atomic_sub(&(file->ref_count), 1);
}

fdt->fds[fd] = RT_NULL;
}
dfs_file_unlock();
}

/**
Expand Down Expand Up @@ -493,6 +499,106 @@ struct dfs_file *fd_get(int fd)
return fdt_get_file(fdt, fd);
}

int dfs_file_get_refs(const int *fds, size_t count, struct dfs_file **files)
{
size_t index;
struct dfs_fdtable *fdt;

if ((count != 0 && (fds == RT_NULL || files == RT_NULL)) ||
dfs_file_lock() != RT_EOK)
{
return -EINVAL;
}

fdt = dfs_fdtable_get();
for (index = 0; index < count; index++)
{
files[index] = fdt_get_file(fdt, fds[index]);
if (files[index] == RT_NULL ||
(files[index]->dentry == RT_NULL && files[index]->vnode == RT_NULL))
{
dfs_file_unlock();
return -EBADF;
}
}

for (index = 0; index < count; index++)
{
rt_atomic_add(&files[index]->ref_count, 1);
}
dfs_file_unlock();
return 0;
}

int dfs_file_install_refs(struct dfs_file **files, size_t count, int *fds)
{
int fd;
int startfd;
size_t index;
struct dfs_fdtable *fdt;

if ((count != 0 && (files == RT_NULL || fds == RT_NULL)) ||
dfs_file_lock() != RT_EOK)
{
return -EINVAL;
}

fdt = dfs_fdtable_get();
startfd = (fdt == &_fdtab) ? DFS_STDIO_OFFSET : 0;
for (index = 0; index < count; index++)
{
if (files[index] == RT_NULL || files[index]->magic != DFS_FD_MAGIC)
{
break;
}

fd = _fdt_slot_alloc(fdt, startfd);
if (fd < 0)
{
break;
}
fdt->fds[fd] = files[index];
fds[index] = fd;
}

if (index != count)
{
while (index > 0)
{
index--;
fdt->fds[fds[index]] = RT_NULL;
}
dfs_file_unlock();
return -EMFILE;
}

dfs_file_unlock();
return 0;
}

void dfs_file_put_ref(struct dfs_file *file)
{
if (file == RT_NULL || dfs_file_lock() != RT_EOK)
{
return;
}

if (file->magic == DFS_FD_MAGIC &&
rt_atomic_load(&file->ref_count) > 0 &&
dfs_file_close(file) == 0)
{
if (rt_atomic_load(&file->ref_count) == 1)
{
dfs_file_destroy(file);
}
else
{
rt_atomic_sub(&file->ref_count, 1);
}
}
dfs_file_unlock();
}

/**
* This function will get the file descriptor table of current process.
*/
Expand Down Expand Up @@ -1238,4 +1344,4 @@ MSH_CMD_EXPORT(dfs_dlog, dfs dlog on|off);
#endif

#endif
/** @} */
/** @} */
Loading
Loading