Linux内核中的文件描述符(五)--fd的分配--locate_fd


Kernel version:2.6.14

CPU architecture:ARM920T

继续上一篇文章的内容,分析另一个文件描述符fd的分配函数locate_fd。dup系统调用用于复制一个文件描述符对应的文件,返回值是个文件描述符。在前面的文章中,我们已经分析过了dup的源码(),在这里我们深入分析locate_fd函数,其定义如下:

static int locate_fd(struct files_struct *files,
      struct file *file, unsigned int orig_start)//从orig_start位开始分配fd
{
 unsigned int newfd;
 unsigned int start;
 int error;
 struct fdtable *fdt;

 error = -EINVAL;
 if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//检查orig_start是大于进程最大可以打开文件的数量
  goto out;

repeat:
 fdt = files_fdtable(files);//文件描述符位图
 /*
  * Someone might have closed fd's in the range
  * orig_start..fdt->next_fd
  */
 start = orig_start;
 if (start < fdt->next_fd)
  start = fdt->next_fd;//如果orig_start小于next_fd,那就从next_fd开始分配

 newfd = start;
 if (start < fdt->max_fdset) {//max_fdset是描述符问题的位数,下面会具体讲解
  newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
   fdt->max_fdset, start);//分配fd
 }
 
 error = -EMFILE;
 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//进行判断,分配的fd不能大于进程最大可以打开的文件数量
  goto out;

 error = expand_files(files, newfd);//文件描述符表的扩展,这个我们留在下一篇文章中详细讲解
 if (error < 0)
  goto out;

 /*
  * If we needed to expand the fs array we
  * might have blocked - try again.
  */
 if (error)
  goto repeat;

 /*
  * We reacquired files_lock, so we are safe as long as
  * we reacquire the fdtable pointer and use it while holding
  * the lock, no one can free it during that time.
  */
 fdt = files_fdtable(files);
 if (start <= fdt->next_fd)
  fdt->next_fd = newfd + 1;//更新next_fd值

 error = newfd;
 
out:
 return error;
}

max_fdset值的分析和rlim_cur差不多,最初的值时从父进程继承过来的。

linux/arch/arm/kernel/init_task.c

struct task_struct init_task = INIT_TASK(init_task);

#define INIT_TASK(tsk) \
{         \
 ...
 .files  = &init_files,     \
 ...   
}

init_files的定义如下:

static struct files_struct init_files = INIT_FILES;

linux/init_task.h

#define INIT_FDTABLE \
{       \
 .max_fds = NR_OPEN_DEFAULT,   \
 .max_fdset = __FD_SETSIZE,   \
 .next_fd = 0,     \
 .fd  = &init_files.fd_array[0],  \
 .close_on_exec = &init_files.close_on_exec_init, \
 .open_fds = &init_files.open_fds_init,  \
 .rcu  = RCU_HEAD_INIT,   \
 .free_files = NULL,    \
 .next  = NULL,    \
}

#define NR_OPEN_DEFAULT BITS_PER_LONG
#define __FD_SETSIZE 1024

#define INIT_FILES \
{        \
 .count  = ATOMIC_INIT(1),   \
 .file_lock = SPIN_LOCK_UNLOCKED,   \
 .fdt  = &init_files.fdtab,   \
 .fdtab  = INIT_FDTABLE,   \
 .close_on_exec_init = { { 0, } },   \
 .open_fds_init = { { 0, } },    \
 .fd_array = { NULL, }    \
}

BITS_PER_LONG是long型数据的字节数,即4*8=3,也就是说max_fds = 32。max_fdset为1024。max_fdset是进程打开文件描述符位图open_fds的大小。open_fds是fd_set的指针。

typedef __kernel_fd_set  fd_set;

#undef __NFDBITS
#define __NFDBITS (8 * sizeof(unsigned long))

#undef __FD_SETSIZE
#define __FD_SETSIZE 1024

#undef __FDSET_LONGS
#define __FDSET_LONGS (__FD_SETSIZE/__NFDBITS)

#undef __FDELT
#define __FDELT(d) ((d) / __NFDBITS)

#undef __FDMASK
#define __FDMASK(d) (1UL << ((d) % __NFDBITS))

typedef struct {
 unsigned long fds_bits [__FDSET_LONGS];
} __kernel_fd_set;

相关内容