file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/time.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/file.h>
  16. #include <linux/fdtable.h>
  17. #include <linux/bitops.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/workqueue.h>
  22. struct fdtable_defer {
  23. spinlock_t lock;
  24. struct work_struct wq;
  25. struct fdtable *next;
  26. };
  27. int sysctl_nr_open __read_mostly = 1024*1024;
  28. int sysctl_nr_open_min = BITS_PER_LONG;
  29. int sysctl_nr_open_max = 1024 * 1024; /* raised later */
  30. /*
  31. * We use this list to defer free fdtables that have vmalloced
  32. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  33. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  34. * this per-task structure.
  35. */
  36. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  37. static inline void *alloc_fdmem(unsigned int size)
  38. {
  39. void *data;
  40. data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN);
  41. if (data != NULL)
  42. return data;
  43. return vmalloc(size);
  44. }
  45. static void free_fdmem(void *ptr)
  46. {
  47. is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
  48. }
  49. static void __free_fdtable(struct fdtable *fdt)
  50. {
  51. free_fdmem(fdt->fd);
  52. free_fdmem(fdt->open_fds);
  53. kfree(fdt);
  54. }
  55. static void free_fdtable_work(struct work_struct *work)
  56. {
  57. struct fdtable_defer *f =
  58. container_of(work, struct fdtable_defer, wq);
  59. struct fdtable *fdt;
  60. spin_lock_bh(&f->lock);
  61. fdt = f->next;
  62. f->next = NULL;
  63. spin_unlock_bh(&f->lock);
  64. while(fdt) {
  65. struct fdtable *next = fdt->next;
  66. __free_fdtable(fdt);
  67. fdt = next;
  68. }
  69. }
  70. void free_fdtable_rcu(struct rcu_head *rcu)
  71. {
  72. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  73. struct fdtable_defer *fddef;
  74. BUG_ON(!fdt);
  75. if (fdt->max_fds <= NR_OPEN_DEFAULT) {
  76. /*
  77. * This fdtable is embedded in the files structure and that
  78. * structure itself is getting destroyed.
  79. */
  80. kmem_cache_free(files_cachep,
  81. container_of(fdt, struct files_struct, fdtab));
  82. return;
  83. }
  84. if (!is_vmalloc_addr(fdt->fd) && !is_vmalloc_addr(fdt->open_fds)) {
  85. kfree(fdt->fd);
  86. kfree(fdt->open_fds);
  87. kfree(fdt);
  88. } else {
  89. fddef = &get_cpu_var(fdtable_defer_list);
  90. spin_lock(&fddef->lock);
  91. fdt->next = fddef->next;
  92. fddef->next = fdt;
  93. /* vmallocs are handled from the workqueue context */
  94. schedule_work(&fddef->wq);
  95. spin_unlock(&fddef->lock);
  96. put_cpu_var(fdtable_defer_list);
  97. }
  98. }
  99. /*
  100. * Expand the fdset in the files_struct. Called with the files spinlock
  101. * held for write.
  102. */
  103. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  104. {
  105. unsigned int cpy, set;
  106. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  107. cpy = ofdt->max_fds * sizeof(struct file *);
  108. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  109. memcpy(nfdt->fd, ofdt->fd, cpy);
  110. memset((char *)(nfdt->fd) + cpy, 0, set);
  111. cpy = ofdt->max_fds / BITS_PER_BYTE;
  112. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  113. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  114. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  115. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  116. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  117. }
  118. static struct fdtable * alloc_fdtable(unsigned int nr)
  119. {
  120. struct fdtable *fdt;
  121. char *data;
  122. /*
  123. * Figure out how many fds we actually want to support in this fdtable.
  124. * Allocation steps are keyed to the size of the fdarray, since it
  125. * grows far faster than any of the other dynamic data. We try to fit
  126. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  127. * and growing in powers of two from there on.
  128. */
  129. nr /= (1024 / sizeof(struct file *));
  130. nr = roundup_pow_of_two(nr + 1);
  131. nr *= (1024 / sizeof(struct file *));
  132. /*
  133. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  134. * had been set lower between the check in expand_files() and here. Deal
  135. * with that in caller, it's cheaper that way.
  136. *
  137. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  138. * bitmaps handling below becomes unpleasant, to put it mildly...
  139. */
  140. if (unlikely(nr > sysctl_nr_open))
  141. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  142. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  143. if (!fdt)
  144. goto out;
  145. fdt->max_fds = nr;
  146. data = alloc_fdmem(nr * sizeof(struct file *));
  147. if (!data)
  148. goto out_fdt;
  149. fdt->fd = (struct file **)data;
  150. data = alloc_fdmem(max_t(unsigned int,
  151. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  152. if (!data)
  153. goto out_arr;
  154. fdt->open_fds = (fd_set *)data;
  155. data += nr / BITS_PER_BYTE;
  156. fdt->close_on_exec = (fd_set *)data;
  157. fdt->next = NULL;
  158. return fdt;
  159. out_arr:
  160. free_fdmem(fdt->fd);
  161. out_fdt:
  162. kfree(fdt);
  163. out:
  164. return NULL;
  165. }
  166. /*
  167. * Expand the file descriptor table.
  168. * This function will allocate a new fdtable and both fd array and fdset, of
  169. * the given size.
  170. * Return <0 error code on error; 1 on successful completion.
  171. * The files->file_lock should be held on entry, and will be held on exit.
  172. */
  173. static int expand_fdtable(struct files_struct *files, int nr)
  174. __releases(files->file_lock)
  175. __acquires(files->file_lock)
  176. {
  177. struct fdtable *new_fdt, *cur_fdt;
  178. spin_unlock(&files->file_lock);
  179. new_fdt = alloc_fdtable(nr);
  180. spin_lock(&files->file_lock);
  181. if (!new_fdt)
  182. return -ENOMEM;
  183. /*
  184. * extremely unlikely race - sysctl_nr_open decreased between the check in
  185. * caller and alloc_fdtable(). Cheaper to catch it here...
  186. */
  187. if (unlikely(new_fdt->max_fds <= nr)) {
  188. __free_fdtable(new_fdt);
  189. return -EMFILE;
  190. }
  191. /*
  192. * Check again since another task may have expanded the fd table while
  193. * we dropped the lock
  194. */
  195. cur_fdt = files_fdtable(files);
  196. if (nr >= cur_fdt->max_fds) {
  197. /* Continue as planned */
  198. copy_fdtable(new_fdt, cur_fdt);
  199. rcu_assign_pointer(files->fdt, new_fdt);
  200. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  201. free_fdtable(cur_fdt);
  202. } else {
  203. /* Somebody else expanded, so undo our attempt */
  204. __free_fdtable(new_fdt);
  205. }
  206. return 1;
  207. }
  208. /*
  209. * Expand files.
  210. * This function will expand the file structures, if the requested size exceeds
  211. * the current capacity and there is room for expansion.
  212. * Return <0 error code on error; 0 when nothing done; 1 when files were
  213. * expanded and execution may have blocked.
  214. * The files->file_lock should be held on entry, and will be held on exit.
  215. */
  216. int expand_files(struct files_struct *files, int nr)
  217. {
  218. struct fdtable *fdt;
  219. fdt = files_fdtable(files);
  220. /*
  221. * N.B. For clone tasks sharing a files structure, this test
  222. * will limit the total number of files that can be opened.
  223. */
  224. if (nr >= rlimit(RLIMIT_NOFILE))
  225. return -EMFILE;
  226. /* Do we need to expand? */
  227. if (nr < fdt->max_fds)
  228. return 0;
  229. /* Can we expand? */
  230. if (nr >= sysctl_nr_open)
  231. return -EMFILE;
  232. /* All good, so we try */
  233. return expand_fdtable(files, nr);
  234. }
  235. static int count_open_files(struct fdtable *fdt)
  236. {
  237. int size = fdt->max_fds;
  238. int i;
  239. /* Find the last open fd */
  240. for (i = size/(8*sizeof(long)); i > 0; ) {
  241. if (fdt->open_fds->fds_bits[--i])
  242. break;
  243. }
  244. i = (i+1) * 8 * sizeof(long);
  245. return i;
  246. }
  247. /*
  248. * Allocate a new files structure and copy contents from the
  249. * passed in files structure.
  250. * errorp will be valid only when the returned files_struct is NULL.
  251. */
  252. struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  253. {
  254. struct files_struct *newf;
  255. struct file **old_fds, **new_fds;
  256. int open_files, size, i;
  257. struct fdtable *old_fdt, *new_fdt;
  258. *errorp = -ENOMEM;
  259. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  260. if (!newf)
  261. goto out;
  262. atomic_set(&newf->count, 1);
  263. spin_lock_init(&newf->file_lock);
  264. newf->next_fd = 0;
  265. new_fdt = &newf->fdtab;
  266. new_fdt->max_fds = NR_OPEN_DEFAULT;
  267. new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
  268. new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
  269. new_fdt->fd = &newf->fd_array[0];
  270. new_fdt->next = NULL;
  271. spin_lock(&oldf->file_lock);
  272. old_fdt = files_fdtable(oldf);
  273. open_files = count_open_files(old_fdt);
  274. /*
  275. * Check whether we need to allocate a larger fd array and fd set.
  276. */
  277. while (unlikely(open_files > new_fdt->max_fds)) {
  278. spin_unlock(&oldf->file_lock);
  279. if (new_fdt != &newf->fdtab)
  280. __free_fdtable(new_fdt);
  281. new_fdt = alloc_fdtable(open_files - 1);
  282. if (!new_fdt) {
  283. *errorp = -ENOMEM;
  284. goto out_release;
  285. }
  286. /* beyond sysctl_nr_open; nothing to do */
  287. if (unlikely(new_fdt->max_fds < open_files)) {
  288. __free_fdtable(new_fdt);
  289. *errorp = -EMFILE;
  290. goto out_release;
  291. }
  292. /*
  293. * Reacquire the oldf lock and a pointer to its fd table
  294. * who knows it may have a new bigger fd table. We need
  295. * the latest pointer.
  296. */
  297. spin_lock(&oldf->file_lock);
  298. old_fdt = files_fdtable(oldf);
  299. open_files = count_open_files(old_fdt);
  300. }
  301. old_fds = old_fdt->fd;
  302. new_fds = new_fdt->fd;
  303. memcpy(new_fdt->open_fds->fds_bits,
  304. old_fdt->open_fds->fds_bits, open_files/8);
  305. memcpy(new_fdt->close_on_exec->fds_bits,
  306. old_fdt->close_on_exec->fds_bits, open_files/8);
  307. for (i = open_files; i != 0; i--) {
  308. struct file *f = *old_fds++;
  309. if (f) {
  310. get_file(f);
  311. } else {
  312. /*
  313. * The fd may be claimed in the fd bitmap but not yet
  314. * instantiated in the files array if a sibling thread
  315. * is partway through open(). So make sure that this
  316. * fd is available to the new process.
  317. */
  318. FD_CLR(open_files - i, new_fdt->open_fds);
  319. }
  320. rcu_assign_pointer(*new_fds++, f);
  321. }
  322. spin_unlock(&oldf->file_lock);
  323. /* compute the remainder to be cleared */
  324. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  325. /* This is long word aligned thus could use a optimized version */
  326. memset(new_fds, 0, size);
  327. if (new_fdt->max_fds > open_files) {
  328. int left = (new_fdt->max_fds-open_files)/8;
  329. int start = open_files / (8 * sizeof(unsigned long));
  330. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  331. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  332. }
  333. rcu_assign_pointer(newf->fdt, new_fdt);
  334. return newf;
  335. out_release:
  336. kmem_cache_free(files_cachep, newf);
  337. out:
  338. return NULL;
  339. }
  340. static void __devinit fdtable_defer_list_init(int cpu)
  341. {
  342. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  343. spin_lock_init(&fddef->lock);
  344. INIT_WORK(&fddef->wq, free_fdtable_work);
  345. fddef->next = NULL;
  346. }
  347. void __init files_defer_init(void)
  348. {
  349. int i;
  350. for_each_possible_cpu(i)
  351. fdtable_defer_list_init(i);
  352. sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
  353. -BITS_PER_LONG;
  354. }
  355. struct files_struct init_files = {
  356. .count = ATOMIC_INIT(1),
  357. .fdt = &init_files.fdtab,
  358. .fdtab = {
  359. .max_fds = NR_OPEN_DEFAULT,
  360. .fd = &init_files.fd_array[0],
  361. .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
  362. .open_fds = (fd_set *)&init_files.open_fds_init,
  363. },
  364. .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
  365. };
  366. /*
  367. * allocate a file descriptor, mark it busy.
  368. */
  369. int alloc_fd(unsigned start, unsigned flags)
  370. {
  371. struct files_struct *files = current->files;
  372. unsigned int fd;
  373. int error;
  374. struct fdtable *fdt;
  375. spin_lock(&files->file_lock);
  376. repeat:
  377. fdt = files_fdtable(files);
  378. fd = start;
  379. if (fd < files->next_fd)
  380. fd = files->next_fd;
  381. if (fd < fdt->max_fds)
  382. fd = find_next_zero_bit(fdt->open_fds->fds_bits,
  383. fdt->max_fds, fd);
  384. error = expand_files(files, fd);
  385. if (error < 0)
  386. goto out;
  387. /*
  388. * If we needed to expand the fs array we
  389. * might have blocked - try again.
  390. */
  391. if (error)
  392. goto repeat;
  393. if (start <= files->next_fd)
  394. files->next_fd = fd + 1;
  395. FD_SET(fd, fdt->open_fds);
  396. if (flags & O_CLOEXEC)
  397. FD_SET(fd, fdt->close_on_exec);
  398. else
  399. FD_CLR(fd, fdt->close_on_exec);
  400. error = fd;
  401. #if 1
  402. /* Sanity check */
  403. if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
  404. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  405. rcu_assign_pointer(fdt->fd[fd], NULL);
  406. }
  407. #endif
  408. out:
  409. spin_unlock(&files->file_lock);
  410. return error;
  411. }
  412. int get_unused_fd(void)
  413. {
  414. return alloc_fd(0, 0);
  415. }
  416. EXPORT_SYMBOL(get_unused_fd);