file.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 timer_list timer;
  23. struct fdtable *next;
  24. };
  25. /*
  26. * We use this list to defer free fdtables that have vmalloced
  27. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  28. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  29. * this per-task structure.
  30. */
  31. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  32. /*
  33. * Allocate an fd array, using kmalloc or vmalloc.
  34. * Note: the array isn't cleared at allocation time.
  35. */
  36. struct file ** alloc_fd_array(int num)
  37. {
  38. struct file **new_fds;
  39. int size = num * sizeof(struct file *);
  40. if (size <= PAGE_SIZE)
  41. new_fds = (struct file **) kmalloc(size, GFP_KERNEL);
  42. else
  43. new_fds = (struct file **) vmalloc(size);
  44. return new_fds;
  45. }
  46. void free_fd_array(struct file **array, int num)
  47. {
  48. int size = num * sizeof(struct file *);
  49. if (!array) {
  50. printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
  51. return;
  52. }
  53. if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
  54. return;
  55. else if (size <= PAGE_SIZE)
  56. kfree(array);
  57. else
  58. vfree(array);
  59. }
  60. static void __free_fdtable(struct fdtable *fdt)
  61. {
  62. free_fdset(fdt->open_fds, fdt->max_fdset);
  63. free_fdset(fdt->close_on_exec, fdt->max_fdset);
  64. free_fd_array(fdt->fd, fdt->max_fds);
  65. kfree(fdt);
  66. }
  67. static void fdtable_timer(unsigned long data)
  68. {
  69. struct fdtable_defer *fddef = (struct fdtable_defer *)data;
  70. spin_lock(&fddef->lock);
  71. /*
  72. * If someone already emptied the queue return.
  73. */
  74. if (!fddef->next)
  75. goto out;
  76. if (!schedule_work(&fddef->wq))
  77. mod_timer(&fddef->timer, 5);
  78. out:
  79. spin_unlock(&fddef->lock);
  80. }
  81. static void free_fdtable_work(struct fdtable_defer *f)
  82. {
  83. struct fdtable *fdt;
  84. spin_lock_bh(&f->lock);
  85. fdt = f->next;
  86. f->next = NULL;
  87. spin_unlock_bh(&f->lock);
  88. while(fdt) {
  89. struct fdtable *next = fdt->next;
  90. __free_fdtable(fdt);
  91. fdt = next;
  92. }
  93. }
  94. static void free_fdtable_rcu(struct rcu_head *rcu)
  95. {
  96. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  97. int fdset_size, fdarray_size;
  98. struct fdtable_defer *fddef;
  99. BUG_ON(!fdt);
  100. fdset_size = fdt->max_fdset / 8;
  101. fdarray_size = fdt->max_fds * sizeof(struct file *);
  102. if (fdt->free_files) {
  103. /*
  104. * The this fdtable was embedded in the files structure
  105. * and the files structure itself was getting destroyed.
  106. * It is now safe to free the files structure.
  107. */
  108. kmem_cache_free(files_cachep, fdt->free_files);
  109. return;
  110. }
  111. if (fdt->max_fdset <= __FD_SETSIZE && fdt->max_fds <= NR_OPEN_DEFAULT) {
  112. /*
  113. * The fdtable was embedded
  114. */
  115. return;
  116. }
  117. if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
  118. kfree(fdt->open_fds);
  119. kfree(fdt->close_on_exec);
  120. kfree(fdt->fd);
  121. kfree(fdt);
  122. } else {
  123. fddef = &get_cpu_var(fdtable_defer_list);
  124. spin_lock(&fddef->lock);
  125. fdt->next = fddef->next;
  126. fddef->next = fdt;
  127. /*
  128. * vmallocs are handled from the workqueue context.
  129. * If the per-cpu workqueue is running, then we
  130. * defer work scheduling through a timer.
  131. */
  132. if (!schedule_work(&fddef->wq))
  133. mod_timer(&fddef->timer, 5);
  134. spin_unlock(&fddef->lock);
  135. put_cpu_var(fdtable_defer_list);
  136. }
  137. }
  138. void free_fdtable(struct fdtable *fdt)
  139. {
  140. if (fdt->free_files || fdt->max_fdset > __FD_SETSIZE ||
  141. fdt->max_fds > NR_OPEN_DEFAULT)
  142. call_rcu(&fdt->rcu, free_fdtable_rcu);
  143. }
  144. /*
  145. * Expand the fdset in the files_struct. Called with the files spinlock
  146. * held for write.
  147. */
  148. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
  149. {
  150. int i;
  151. int count;
  152. BUG_ON(nfdt->max_fdset < fdt->max_fdset);
  153. BUG_ON(nfdt->max_fds < fdt->max_fds);
  154. /* Copy the existing tables and install the new pointers */
  155. i = fdt->max_fdset / (sizeof(unsigned long) * 8);
  156. count = (nfdt->max_fdset - fdt->max_fdset) / 8;
  157. /*
  158. * Don't copy the entire array if the current fdset is
  159. * not yet initialised.
  160. */
  161. if (i) {
  162. memcpy (nfdt->open_fds, fdt->open_fds,
  163. fdt->max_fdset/8);
  164. memcpy (nfdt->close_on_exec, fdt->close_on_exec,
  165. fdt->max_fdset/8);
  166. memset (&nfdt->open_fds->fds_bits[i], 0, count);
  167. memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
  168. }
  169. /* Don't copy/clear the array if we are creating a new
  170. fd array for fork() */
  171. if (fdt->max_fds) {
  172. memcpy(nfdt->fd, fdt->fd,
  173. fdt->max_fds * sizeof(struct file *));
  174. /* clear the remainder of the array */
  175. memset(&nfdt->fd[fdt->max_fds], 0,
  176. (nfdt->max_fds - fdt->max_fds) *
  177. sizeof(struct file *));
  178. }
  179. nfdt->next_fd = fdt->next_fd;
  180. }
  181. /*
  182. * Allocate an fdset array, using kmalloc or vmalloc.
  183. * Note: the array isn't cleared at allocation time.
  184. */
  185. fd_set * alloc_fdset(int num)
  186. {
  187. fd_set *new_fdset;
  188. int size = num / 8;
  189. if (size <= PAGE_SIZE)
  190. new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
  191. else
  192. new_fdset = (fd_set *) vmalloc(size);
  193. return new_fdset;
  194. }
  195. void free_fdset(fd_set *array, int num)
  196. {
  197. int size = num / 8;
  198. if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
  199. return;
  200. else if (size <= PAGE_SIZE)
  201. kfree(array);
  202. else
  203. vfree(array);
  204. }
  205. static struct fdtable *alloc_fdtable(int nr)
  206. {
  207. struct fdtable *fdt = NULL;
  208. int nfds = 0;
  209. fd_set *new_openset = NULL, *new_execset = NULL;
  210. struct file **new_fds;
  211. fdt = kmalloc(sizeof(*fdt), GFP_KERNEL);
  212. if (!fdt)
  213. goto out;
  214. memset(fdt, 0, sizeof(*fdt));
  215. nfds = __FD_SETSIZE;
  216. /* Expand to the max in easy steps */
  217. do {
  218. if (nfds < (PAGE_SIZE * 8))
  219. nfds = PAGE_SIZE * 8;
  220. else {
  221. nfds = nfds * 2;
  222. if (nfds > NR_OPEN)
  223. nfds = NR_OPEN;
  224. }
  225. } while (nfds <= nr);
  226. new_openset = alloc_fdset(nfds);
  227. new_execset = alloc_fdset(nfds);
  228. if (!new_openset || !new_execset)
  229. goto out;
  230. fdt->open_fds = new_openset;
  231. fdt->close_on_exec = new_execset;
  232. fdt->max_fdset = nfds;
  233. nfds = NR_OPEN_DEFAULT;
  234. /*
  235. * Expand to the max in easy steps, and keep expanding it until
  236. * we have enough for the requested fd array size.
  237. */
  238. do {
  239. #if NR_OPEN_DEFAULT < 256
  240. if (nfds < 256)
  241. nfds = 256;
  242. else
  243. #endif
  244. if (nfds < (PAGE_SIZE / sizeof(struct file *)))
  245. nfds = PAGE_SIZE / sizeof(struct file *);
  246. else {
  247. nfds = nfds * 2;
  248. if (nfds > NR_OPEN)
  249. nfds = NR_OPEN;
  250. }
  251. } while (nfds <= nr);
  252. new_fds = alloc_fd_array(nfds);
  253. if (!new_fds)
  254. goto out;
  255. fdt->fd = new_fds;
  256. fdt->max_fds = nfds;
  257. fdt->free_files = NULL;
  258. return fdt;
  259. out:
  260. if (new_openset)
  261. free_fdset(new_openset, nfds);
  262. if (new_execset)
  263. free_fdset(new_execset, nfds);
  264. kfree(fdt);
  265. return NULL;
  266. }
  267. /*
  268. * Expands the file descriptor table - it will allocate a new fdtable and
  269. * both fd array and fdset. It is expected to be called with the
  270. * files_lock held.
  271. */
  272. static int expand_fdtable(struct files_struct *files, int nr)
  273. __releases(files->file_lock)
  274. __acquires(files->file_lock)
  275. {
  276. int error = 0;
  277. struct fdtable *fdt;
  278. struct fdtable *nfdt = NULL;
  279. spin_unlock(&files->file_lock);
  280. nfdt = alloc_fdtable(nr);
  281. if (!nfdt) {
  282. error = -ENOMEM;
  283. spin_lock(&files->file_lock);
  284. goto out;
  285. }
  286. spin_lock(&files->file_lock);
  287. fdt = files_fdtable(files);
  288. /*
  289. * Check again since another task may have expanded the
  290. * fd table while we dropped the lock
  291. */
  292. if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
  293. copy_fdtable(nfdt, fdt);
  294. } else {
  295. /* Somebody expanded while we dropped file_lock */
  296. spin_unlock(&files->file_lock);
  297. __free_fdtable(nfdt);
  298. spin_lock(&files->file_lock);
  299. goto out;
  300. }
  301. rcu_assign_pointer(files->fdt, nfdt);
  302. free_fdtable(fdt);
  303. out:
  304. return error;
  305. }
  306. /*
  307. * Expand files.
  308. * Return <0 on error; 0 nothing done; 1 files expanded, we may have blocked.
  309. * Should be called with the files->file_lock spinlock held for write.
  310. */
  311. int expand_files(struct files_struct *files, int nr)
  312. {
  313. int err, expand = 0;
  314. struct fdtable *fdt;
  315. fdt = files_fdtable(files);
  316. if (nr >= fdt->max_fdset || nr >= fdt->max_fds) {
  317. if (fdt->max_fdset >= NR_OPEN ||
  318. fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
  319. err = -EMFILE;
  320. goto out;
  321. }
  322. expand = 1;
  323. if ((err = expand_fdtable(files, nr)))
  324. goto out;
  325. }
  326. err = expand;
  327. out:
  328. return err;
  329. }
  330. static void __devinit fdtable_defer_list_init(int cpu)
  331. {
  332. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  333. spin_lock_init(&fddef->lock);
  334. INIT_WORK(&fddef->wq, (void (*)(void *))free_fdtable_work, fddef);
  335. init_timer(&fddef->timer);
  336. fddef->timer.data = (unsigned long)fddef;
  337. fddef->timer.function = fdtable_timer;
  338. fddef->next = NULL;
  339. }
  340. void __init files_defer_init(void)
  341. {
  342. int i;
  343. for_each_cpu(i)
  344. fdtable_defer_list_init(i);
  345. }