file.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * linux/fs/file.c
  3. *
  4. * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
  5. *
  6. * Manage the dynamic fd arrays in the process files_struct.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/time.h>
  11. #include <linux/slab.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/file.h>
  14. #include <linux/bitops.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/workqueue.h>
  19. struct fdtable_defer {
  20. spinlock_t lock;
  21. struct work_struct wq;
  22. struct fdtable *next;
  23. };
  24. /*
  25. * We use this list to defer free fdtables that have vmalloced
  26. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  27. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  28. * this per-task structure.
  29. */
  30. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  31. /*
  32. * Allocate an fd array, using kmalloc or vmalloc.
  33. * Note: the array isn't cleared at allocation time.
  34. */
  35. struct file ** alloc_fd_array(int num)
  36. {
  37. struct file **new_fds;
  38. int size = num * sizeof(struct file *);
  39. if (size <= PAGE_SIZE)
  40. new_fds = (struct file **) kmalloc(size, GFP_KERNEL);
  41. else
  42. new_fds = (struct file **) vmalloc(size);
  43. return new_fds;
  44. }
  45. void free_fd_array(struct file **array, int num)
  46. {
  47. int size = num * sizeof(struct file *);
  48. if (!array) {
  49. printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
  50. return;
  51. }
  52. if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
  53. return;
  54. else if (size <= PAGE_SIZE)
  55. kfree(array);
  56. else
  57. vfree(array);
  58. }
  59. static void __free_fdtable(struct fdtable *fdt)
  60. {
  61. free_fdset(fdt->open_fds, fdt->max_fds);
  62. free_fdset(fdt->close_on_exec, fdt->max_fds);
  63. free_fd_array(fdt->fd, fdt->max_fds);
  64. kfree(fdt);
  65. }
  66. static void free_fdtable_work(struct work_struct *work)
  67. {
  68. struct fdtable_defer *f =
  69. container_of(work, struct fdtable_defer, wq);
  70. struct fdtable *fdt;
  71. spin_lock_bh(&f->lock);
  72. fdt = f->next;
  73. f->next = NULL;
  74. spin_unlock_bh(&f->lock);
  75. while(fdt) {
  76. struct fdtable *next = fdt->next;
  77. __free_fdtable(fdt);
  78. fdt = next;
  79. }
  80. }
  81. void free_fdtable_rcu(struct rcu_head *rcu)
  82. {
  83. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  84. int fdset_size, fdarray_size;
  85. struct fdtable_defer *fddef;
  86. BUG_ON(!fdt);
  87. fdset_size = fdt->max_fds / 8;
  88. fdarray_size = fdt->max_fds * sizeof(struct file *);
  89. if (fdt->max_fds <= NR_OPEN_DEFAULT) {
  90. /*
  91. * This fdtable is embedded in the files structure and that
  92. * structure itself is getting destroyed.
  93. */
  94. kmem_cache_free(files_cachep,
  95. container_of(fdt, struct files_struct, fdtab));
  96. return;
  97. }
  98. if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
  99. kfree(fdt->open_fds);
  100. kfree(fdt->close_on_exec);
  101. kfree(fdt->fd);
  102. kfree(fdt);
  103. } else {
  104. fddef = &get_cpu_var(fdtable_defer_list);
  105. spin_lock(&fddef->lock);
  106. fdt->next = fddef->next;
  107. fddef->next = fdt;
  108. /* vmallocs are handled from the workqueue context */
  109. schedule_work(&fddef->wq);
  110. spin_unlock(&fddef->lock);
  111. put_cpu_var(fdtable_defer_list);
  112. }
  113. }
  114. /*
  115. * Expand the fdset in the files_struct. Called with the files spinlock
  116. * held for write.
  117. */
  118. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
  119. {
  120. int i;
  121. int count;
  122. BUG_ON(nfdt->max_fds < fdt->max_fds);
  123. /* Copy the existing tables and install the new pointers */
  124. i = fdt->max_fds / (sizeof(unsigned long) * 8);
  125. count = (nfdt->max_fds - fdt->max_fds) / 8;
  126. /*
  127. * Don't copy the entire array if the current fdset is
  128. * not yet initialised.
  129. */
  130. if (i) {
  131. memcpy (nfdt->open_fds, fdt->open_fds,
  132. fdt->max_fds/8);
  133. memcpy (nfdt->close_on_exec, fdt->close_on_exec,
  134. fdt->max_fds/8);
  135. memset (&nfdt->open_fds->fds_bits[i], 0, count);
  136. memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
  137. }
  138. /* Don't copy/clear the array if we are creating a new
  139. fd array for fork() */
  140. if (fdt->max_fds) {
  141. memcpy(nfdt->fd, fdt->fd,
  142. fdt->max_fds * sizeof(struct file *));
  143. /* clear the remainder of the array */
  144. memset(&nfdt->fd[fdt->max_fds], 0,
  145. (nfdt->max_fds - fdt->max_fds) *
  146. sizeof(struct file *));
  147. }
  148. }
  149. /*
  150. * Allocate an fdset array, using kmalloc or vmalloc.
  151. * Note: the array isn't cleared at allocation time.
  152. */
  153. fd_set * alloc_fdset(int num)
  154. {
  155. fd_set *new_fdset;
  156. int size = num / 8;
  157. if (size <= PAGE_SIZE)
  158. new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
  159. else
  160. new_fdset = (fd_set *) vmalloc(size);
  161. return new_fdset;
  162. }
  163. void free_fdset(fd_set *array, int num)
  164. {
  165. if (num <= NR_OPEN_DEFAULT) /* Don't free an embedded fdset */
  166. return;
  167. else if (num <= 8 * PAGE_SIZE)
  168. kfree(array);
  169. else
  170. vfree(array);
  171. }
  172. static struct fdtable *alloc_fdtable(int nr)
  173. {
  174. struct fdtable *fdt = NULL;
  175. int nfds = 0;
  176. fd_set *new_openset = NULL, *new_execset = NULL;
  177. struct file **new_fds;
  178. fdt = kzalloc(sizeof(*fdt), GFP_KERNEL);
  179. if (!fdt)
  180. goto out;
  181. nfds = NR_OPEN_DEFAULT;
  182. /*
  183. * Expand to the max in easy steps, and keep expanding it until
  184. * we have enough for the requested fd array size.
  185. */
  186. do {
  187. #if NR_OPEN_DEFAULT < 256
  188. if (nfds < 256)
  189. nfds = 256;
  190. else
  191. #endif
  192. if (nfds < (PAGE_SIZE / sizeof(struct file *)))
  193. nfds = PAGE_SIZE / sizeof(struct file *);
  194. else {
  195. nfds = nfds * 2;
  196. if (nfds > NR_OPEN)
  197. nfds = NR_OPEN;
  198. }
  199. } while (nfds <= nr);
  200. new_openset = alloc_fdset(nfds);
  201. new_execset = alloc_fdset(nfds);
  202. if (!new_openset || !new_execset)
  203. goto out;
  204. fdt->open_fds = new_openset;
  205. fdt->close_on_exec = new_execset;
  206. new_fds = alloc_fd_array(nfds);
  207. if (!new_fds)
  208. goto out;
  209. fdt->fd = new_fds;
  210. fdt->max_fds = nfds;
  211. return fdt;
  212. out:
  213. free_fdset(new_openset, nfds);
  214. free_fdset(new_execset, nfds);
  215. kfree(fdt);
  216. return NULL;
  217. }
  218. /*
  219. * Expand the file descriptor table.
  220. * This function will allocate a new fdtable and both fd array and fdset, of
  221. * the given size.
  222. * Return <0 error code on error; 1 on successful completion.
  223. * The files->file_lock should be held on entry, and will be held on exit.
  224. */
  225. static int expand_fdtable(struct files_struct *files, int nr)
  226. __releases(files->file_lock)
  227. __acquires(files->file_lock)
  228. {
  229. struct fdtable *new_fdt, *cur_fdt;
  230. spin_unlock(&files->file_lock);
  231. new_fdt = alloc_fdtable(nr);
  232. spin_lock(&files->file_lock);
  233. if (!new_fdt)
  234. return -ENOMEM;
  235. /*
  236. * Check again since another task may have expanded the fd table while
  237. * we dropped the lock
  238. */
  239. cur_fdt = files_fdtable(files);
  240. if (nr >= cur_fdt->max_fds) {
  241. /* Continue as planned */
  242. copy_fdtable(new_fdt, cur_fdt);
  243. rcu_assign_pointer(files->fdt, new_fdt);
  244. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  245. call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
  246. } else {
  247. /* Somebody else expanded, so undo our attempt */
  248. __free_fdtable(new_fdt);
  249. }
  250. return 1;
  251. }
  252. /*
  253. * Expand files.
  254. * This function will expand the file structures, if the requested size exceeds
  255. * the current capacity and there is room for expansion.
  256. * Return <0 error code on error; 0 when nothing done; 1 when files were
  257. * expanded and execution may have blocked.
  258. * The files->file_lock should be held on entry, and will be held on exit.
  259. */
  260. int expand_files(struct files_struct *files, int nr)
  261. {
  262. struct fdtable *fdt;
  263. fdt = files_fdtable(files);
  264. /* Do we need to expand? */
  265. if (nr < fdt->max_fds)
  266. return 0;
  267. /* Can we expand? */
  268. if (nr >= NR_OPEN)
  269. return -EMFILE;
  270. /* All good, so we try */
  271. return expand_fdtable(files, nr);
  272. }
  273. static void __devinit fdtable_defer_list_init(int cpu)
  274. {
  275. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  276. spin_lock_init(&fddef->lock);
  277. INIT_WORK(&fddef->wq, free_fdtable_work);
  278. fddef->next = NULL;
  279. }
  280. void __init files_defer_init(void)
  281. {
  282. int i;
  283. for_each_possible_cpu(i)
  284. fdtable_defer_list_init(i);
  285. }