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 }