//用 bitmap 数组来记录文件描述符的分配情况,一位代表一个SYS FD
static unsigned int bitmap[CONFIG_NFILE_DESCRIPTORS / 32 + 1] = {0};
//设置指定位值为 1
static void set_bit(int i, void *addr)
{
unsigned int tem = (unsigned int)i >> 5; /* Get the bitmap subscript */
unsigned int *addri = (unsigned int *)addr + tem;
unsigned int old = *addri;
old = old | (1UL << ((unsigned int)i & 0x1f)); /* set the new map bit */
*addri = old;
}
//获取指定位,看是否已经被分配
bool get_bit(int i)
{
unsigned int *p = NULL;
unsigned int mask;
p = ((unsigned int *)bitmap) + (i >> 5); /* Gets the location in the bitmap */
mask = 1 << (i & 0x1f); /* Gets the mask for the current bit int bitmap */
if (!(~(*p) & mask)){
return true;
}
return false;
}
ssize_t file_write(struct file *filep, const void *buf, size_t nbytes)
{
int ret;
int err;
if (buf == NULL)
{
err = EFAULT;
goto errout;
}
/* Was this file opened for write access? */
if ((((unsigned int)(filep->f_oflags)) & O_ACCMODE) == O_RDONLY)
{
err = EACCES;
goto errout;
}
/* Is a driver registered? Does it support the write method? */
if (!filep->ops || !filep->ops->write)
{
err = EBADF;
goto errout;
}
/* Yes, then let the driver perform the write */
ret = filep->ops->write(filep, (const char *)buf, nbytes);
if (ret < 0)
{
err = -ret;
goto errout;
}
return ret;
errout:
set_errno(err);
return VFS_ERROR;
}
close
//关闭文件句柄
int SysClose(int fd)
{
int ret;
/* Process fd convert to system global fd */
int sysfd = DisassociateProcessFd(fd);//先解除关联
ret = close(sysfd);//关闭文件,个人认为应该先 close - > DisassociateProcessFd
if (ret < 0) {//关闭失败时
AssociateSystemFd(fd, sysfd);//继续关联
return -get_errno();
}
FreeProcessFd(fd);//释放进程fd
return ret;
}
解除进程fd和系统fd的绑定关系
close时会有个判断,这个文件的引用数是否为0,只有为0才会真正的执行_files_close
int files_close_internal(int fd, LosProcessCB *processCB)
{
//...
list->fl_files[fd].f_refcount--;
if (list->fl_files[fd].f_refcount == 0)
{
#ifdef LOSCFG_KERNEL_VM
dec_mapping_nolock(filep->f_mapping);
#endif
ret = _files_close(&list->fl_files[fd]);
if (ret == OK)
{
clear_bit(fd, bitmap);
}
}
// ...
}
static int _files_close(struct file *filep)
{
struct Vnode *vnode = filep->f_vnode;
int ret = OK;
/* Check if the struct file is open (i.e., assigned an vnode) */
if (filep->f_oflags & O_DIRECTORY)
{
ret = closedir(filep->f_dir);
if (ret != OK)
{
return ret;
}
}
else
{
/* Close the file, driver, or mountpoint. */
if (filep->ops && filep->ops->close)
{
/* Perform the close operation */
ret = filep->ops->close(filep);
if (ret != OK)
{
return ret;
}
}
VnodeHold();
vnode->useCount--;
/* Block char device is removed when close */
if (vnode->type == VNODE_TYPE_BCHR)
{
ret = VnodeFree(vnode);
if (ret < 0)
{
PRINTK("Removing bchar device %s failed\n", filep->f_path);
}
}
VnodeDrop();
}
/* Release the path of file */
free(filep->f_path);
/* Release the file descriptor */
filep->f_magicnum = 0;
filep->f_oflags = 0;
filep->f_pos = 0;
filep->f_path = NULL;
filep->f_priv = NULL;
filep->f_vnode = NULL;
filep->f_refcount = 0;
filep->f_mapping = NULL;
filep->f_dir = NULL;
return ret;
}