Kernel version:2.6.14
CPU architecture:ARM920T
Author:ce123(http://blog.csdn.net/ce123)
咱们先贴出expand_files函数的源码:
[plain]view plaincopy
print?
- intexpand_files(structfiles_struct*files,intnr)
- {
- interr,expand=0;
- structfdtable*fdt;
- fdt=files_fdtable(files);
- if(nr>=fdt->max_fdset||nr>=fdt->max_fds){//咱们在前面的文章中现已剖析过,初始时max_fdset=1024,max_fds=32
- if(fdt->max_fdset>=NR_OPEN||//#defineNR_OPEN(1024*1024)/*Absoluteupperlimitonfdnum*/
- fdt->max_fds>=NR_OPEN||nr>=NR_OPEN){
- err=-EMFILE;//max_fdset和max_fds都不能大于NR_OPEN,不然回来-EMFILE,即翻开太多的文件
- gotoout;
- }
- expand=1;
- if((err=expand_fdtable(files,nr)))//真实进行扩展
- gotoout;
- }
- err=expand;
- out:
- returnerr;
- }
expand_files函数进行一些查看后调用expand_fdtable进行文件描述符表的扩展,下面剖析expand_fdtable函数。
[plain]view plaincopy
print?
- staticintexpand_fdtable(structfiles_struct*files,intnr)
- __releases(files->file_lock)
- __acquires(files->file_lock)
- {
- interror=0;
- structfdtable*fdt;
- structfdtable*nfdt=NULL;
- spin_unlock(&files->file_lock);
- nfdt=alloc_fdtable(nr);//依据nr从头创立一个新的fdtable
- if(!nfdt){
- error=-ENOMEM;
- spin_lock(&files->file_lock);
- gotoout;
- }
- spin_lock(&files->file_lock);
- fdt=files_fdtable(files);
- /*
- *Checkagainsinceanothertaskmayhaveexpandedthe
- *fdtablewhilewedroppedthelock
- */
- if(nr>=fdt->max_fds||nr>=fdt->max_fdset){//nr值有必要大于max_fds和max_fdset值,这儿再次进行查看是避免另一个进程进行了expand
- copy_fdtable(nfdt,fdt);//将旧的fdtable中的内容复制至新的fdtable
- }else{
- /*Somebodyexpandedwhilewedroppedfile_lock*/
- spin_unlock(&files->file_lock);
- __free_fdtable(nfdt);
- spin_lock(&files->file_lock);
- gotoout;
- }
- rcu_assign_pointer(files->fdt,nfdt);//用新的fdtable替换旧的fdtable
- free_fdtable(fdt);//开释旧的fdtable
- out:
- returnerror;
- }
咱们再来看一下扩展文件描述符表的要害函数alloc_fdtable,其界说如下:
[plain]view plaincopy
print?
- staticstructfdtable*alloc_fdtable(intnr)
- {
- structfdtable*fdt=NULL;
- intnfds=0;
- fd_set*new_openset=NULL,*new_execset=NULL;
- structfile**new_fds;
- fdt=kmalloc(sizeof(*fdt),GFP_KERNEL);
- if(!fdt)
- gotoout;
- memset(fdt,0,sizeof(*fdt));
- nfds=__FD_SETSIZE;//#define__FD_SETSIZE1024
- //#definePAGE_SHIFT12
- //#definePAGE_SIZE(1UL<
- /*Expandtothemaxineasysteps*/
- do{
- if(nfds<(PAGE_SIZE*8))//dfds=1024
- nfds=PAGE_SIZE*8;
- else{
- nfds=nfds*2;
- if(nfds>NR_OPEN)
- nfds=NR_OPEN;
- }
- }while(nfds<=nr);//第一次expand时,nr应该等于32
- new_openset=alloc_fdset(nfds);//分配翻开文件位图
- new_execset=alloc_fdset(nfds);
- if(!new_openset||!new_execset)
- gotoout;
- fdt->open_fds=new_openset;
- fdt->close_on_exec=new_execset;
- fdt->max_fdset=nfds;//更新max_fdset值,此刻这个值为32k
- nfds=NR_OPEN_DEFAULT;//nfds=32
- /*
- *Expandtothemaxineasysteps,andkeepexpandingituntil
- *wehaveenoughfortherequestedfdarraysize.
- */
- do{
- #ifNR_OPEN_DEFAULT<256
- if(nfds<256)
- nfds=256;//nfds=256(32->256->1024)
- //无法超越1024,由于在最开端的就进行了查看,一定要小于current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
- else
- #endif
- if(nfds<(PAGE_SIZE/sizeof(structfile*)))
- nfds=PAGE_SIZE/sizeof(structfile*);
- else{
- nfds=nfds*2;
- if(nfds>NR_OPEN)
- nfds=NR_OPEN;
- }
- }while(nfds<=nr);
- new_fds=alloc_fd_array(nfds);//分配文件描述符数组
- if(!new_fds)
- gotoout;
- fdt->fd=new_fds;
- fdt->max_fds=nfds;//更新max_fds
- fdt->free_files=NULL;
- returnfdt;
- out:
- if(new_openset)
- free_fdset(new_openset,nfds);
- if(new_execset)
- free_fdset(new_execset,nfds);
- kfree(fdt);
- returnNULL;
- }
alloc_fd_array和alloc_fdset选用kmalloc或许vmalloc进行内存分配。