Open系统调用
:这一步是需要找到一个没有使用的文件描述符fd
看一下sys_open函数中调用的这个函数get_unused_fd:
738 /*
739 * Find an empty file descriptor entry, and mark it busy.
740 */
741 int get_unused_fd(void)
742 {
743 struct files_struct * files = current->files; /* (1)获得当前进程的文件打开表,这个前面已经说过了 */
744 int fd, error;
745
746 error = -EMFILE;
747 write_lock(&files->file_lock);
748
749 repeat:
750 fd = find_next_zero_bit(files->open_fds, /* (2)在这个进程文件打开表中寻找还没有使用的fd */
751 files->max_fdset,
752 files->next_fd);
753
754 /*
755 * N.B. For clone tasks sharing a files structure, this test
756 * will limit the total number of files that can be opened.
757 */
758 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur) /* 不能超过限制(考虑到fork进程情况,共享fd情况) */
759 goto out;
760
761 /* Do we need to expand the fdset array? */
762 if (fd >= files->max_fdset) { /* 如果当前进程的fd集合中最大fd比当前申请的fd小,那么需要扩大fdset,需要扩容:具体见http://blog.csdn.net/shanshanpt/article/details/38943731中files_struct结构体描述 */
763 error = expand_fdset(files, fd); /* 扩容函数1 */
764 if (!error) {
765 error = -EMFILE;
766 goto repeat;
767 }
768 goto out;
769 }
770
771 /*
772 * Check whether we need to expand the fd array.
773 */
774 if (fd >= files->max_fds) { /* 类似于上面,这里是需要扩大fd-array数组,具体见上面链接 */
775 error = expand_fd_array(files, fd); /* 扩容函数2 */
776 if (!error) {
777 error = -EMFILE;
778 goto repeat;
779 }
780 goto out;
781 }
782
783 FD_SET(fd, files->open_fds); /* 将fd加入到打开文件描述符中 */
784 FD_CLR(fd, files->close_on_exec);/* 从close-on-exec中清除 */
785 files->next_fd = fd + 1; /* 当前描述符是最大的fd,所有next就是fd+1 */
786 #if 1
787 /* Sanity check */
788 if (files->fd[fd] != NULL) {
789 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
790 files->fd[fd] = NULL;
791 }
792 #endif
793 error = fd;
794
795 out:
796 write_unlock(&files->file_lock);
797 return error;
798 }
799
ATTENTION:注意上面的max_fdset和max_fds的区别,前者是当前可以容纳的最大的文件描述符的数量大小!后者是当前可以容纳的文件对象的数量大小!对于一个文件对象而言,可以存在多个文件描述符指向这一个文件对象!所以对于申请到的fd,要分别和这两个进行判断!
主要看上面的三个函数,第一个是寻找fd函数find_next_zero_bit:
这个函数的意义就是找到open_fds打开的文件描述符中第一个bit位=0的那一位,作为新的fd返回。(前面也说过,fd的管理是使用位管理的),反正是一堆位运算,慢慢看吧:
254 static inline unsigned long find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
255 {
256 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
257 unsigned long result = offset & ~31UL;
258 unsigned long tmp;
259
260 if (offset >= size)
261 return size;
262 size -= result;
263 offset &= 31UL;
264 if (offset) {
265 tmp = *(p++);
266 tmp |= ~0UL >> (32-offset);
267 if (size < 32)
268 goto found_first;
269 if (~tmp)
270 goto found_middle;
271 size -= 32;
272 result += 32;
273 }
274 while (size & ~31UL) {
275 if (~(tmp = *(p++)))
276 goto found_middle;
277 result += 32;
278 size -= 32;
279 }
280 if (!size)
281 return result;
282 tmp = *p;
283
284 found_first:
285 tmp |= ~0UL << size;
286 if (tmp == ~0UL) /* Are any bits zero? */
287 return result + size; /* Nope. */
288 found_middle:
289 return result + ffz(tmp);
290 }
扩大fdset集合函数:expand_fdset
162 /*
163 * Expand the fdset in the files_struct. Called with the files spinlock
164 * held for write.
165 */
166 int expand_fdset(struct files_struct *files, int nr)
167 {
168 fd_set *new_openset = 0, *new_execset = 0;
169 int error, nfds = 0;
170
171 error = -EMFILE;
172 if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN) /* 不能大于系统最大限制 */
173 goto out;
174
175 nfds = files->max_fdset; /* 当前最大文件描述符 */
176 write_unlock(&files->file_lock);
177
178 /* Expand to the max in easy steps,下面是一个 简单的扩展过程 */
179 do {
180 if (nfds < (PAGE_SIZE * 8))
181 nfds = PAGE_SIZE * 8;
182 else {
183 nfds = nfds * 2;
184 if (nfds > NR_OPEN)
185 nfds = NR_OPEN;
186 }
187 } while (nfds <= nr);
188
189 error = -ENOMEM;
190 new_openset = alloc_fdset(nfds); /* 根据新的大小分配新的打开set集合 */
191 new_execset = alloc_fdset(nfds); /* 根据新的大小分配新的执行时候需要close的set集合 */
192 write_lock(&files->file_lock);
193 if (!new_openset || !new_execset)
194 goto out;
195
196 error = 0;
197
198 /* Copy the existing tables and install the new pointers:将老的数据拷贝到新的内存中来 */
199 if (nfds > files->max_fdset) {
200 int i = files->max_fdset / (sizeof(unsigned long) * 8);
201 int count = (nfds - files->max_fdset) / 8;
202
203 /*
204 * Don't copy the entire array if the current fdset is
205 * not yet initialised.
206 */
207 if (i) {
208 memcpy (new_openset, files->open_fds, files->max_fdset/8);
209 memcpy (new_execset, files->close_on_exec, files->max_fdset/8);
210 memset (&new_openset->fds_bits[i], 0, count);
211 memset (&new_execset->fds_bits[i], 0, count);
212 }
213 /* 下面几步骤很重要,将新分配的挂载到files结构体中去 */
214 nfds = xchg(&files->max_fdset, nfds);
215 new_openset = xchg(&files->open_fds, new_openset);
216 new_execset = xchg(&files->close_on_exec, new_execset);
217 write_unlock(&files->file_lock);
218 free_fdset (new_openset, nfds);
219 free_fdset (new_execset, nfds);
220 write_lock(&files->file_lock);
221 return 0;
222 }
223 /* Somebody expanded the array while we slept ... */
224
225 out:
226 write_unlock(&files->file_lock);
227 if (new_openset)
228 free_fdset(new_openset, nfds);
229 if (new_execset)
230 free_fdset(new_execset, nfds);
231 write_lock(&files->file_lock);
232 return error;
233 }
再看一下具体的alloc_fdset函数:
128 /*
129 * Allocate an fdset array, using kmalloc or vmalloc.
130 * Note: the array isn't cleared at allocation time.
131 */
132 fd_set * alloc_fdset(int num)
133 {
134 fd_set *new_fdset;
135 int size = num / 8;
136
137 if (size <= PAGE_SIZE)
138 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
139 else
140 new_fdset = (fd_set *) vmalloc(size);
141 return new_fdset;
142 }
回到上面,看一下扩大fd数组的函数expand_fd_array:
52 /*
53 * Expand the fd array in the files_struct. Called with the files
54 * spinlock held for write.
55 */
56
57 int expand_fd_array(struct files_struct *files, int nr)
58 {
59 struct file **new_fds;
60 int error, nfds;
61
62
63 error = -EMFILE;
64 if (files->max_fds >= NR_OPEN || nr >= NR_OPEN) /* 不能超过最大系统限制 */
65 goto out;
66
67 nfds = files->max_fds; /* 当前进程中最大的fd */
68 write_unlock(&files->file_lock);
69
70 /*
71 * Expand to the max in easy steps, and keep expanding it until
72 * we have enough for the requested fd array size.
73 */
74 /* 简单的扩展策略 */
75 do {
76 #if NR_OPEN_DEFAULT < 256
77 if (nfds < 256)
78 nfds = 256;
79 else
80 #endif
81 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
82 nfds = PAGE_SIZE / sizeof(struct file *);
83 else {
84 nfds = nfds * 2;
85 if (nfds > NR_OPEN)
86 nfds = NR_OPEN;
87 }
88 } while (nfds <= nr);
89
90 error = -ENOMEM;
91 new_fds = alloc_fd_array(nfds); /* 分配新的fd_array数组 */
92 write_lock(&files->file_lock);
93 if (!new_fds)
94 goto out;
95
96 /* Copy the existing array and install the new pointer */
97
98 if (nfds > files->max_fds) {
99 struct file **old_fds;
100 int i;
101 /* 将当前进行的文件数组指针指向新申请的fd数组! */
102 old_fds = xchg(&files->fd, new_fds);
103 i = xchg(&files->max_fds, nfds);
104
105 /* Don't copy/clear the array if we are creating a new
106 fd array for fork() */
107 if (i) { /* 下面将老的数据拷贝过去 */
108 memcpy(new_fds, old_fds, i * sizeof(struct file *));
109 /* clear the remainder of the array */
110 memset(&new_fds[i], 0,
111 (nfds-i) * sizeof(struct file *));
112
113 write_unlock(&files->file_lock);
114 free_fd_array(old_fds, i);
115 write_lock(&files->file_lock);
116 }
117 } else {
118 /* Somebody expanded the array while we slept ... */
119 write_unlock(&files->file_lock);
120 free_fd_array(new_fds, nfds);
121 write_lock(&files->file_lock);
122 }
123 error = 0;
124 out:
125 return error;
126 }