file.c 8.9 KB

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