file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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/syscalls.h>
  9. #include <linux/export.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <linux/mmzone.h>
  13. #include <linux/time.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/file.h>
  18. #include <linux/fdtable.h>
  19. #include <linux/bitops.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/workqueue.h>
  24. int sysctl_nr_open __read_mostly = 1024*1024;
  25. int sysctl_nr_open_min = BITS_PER_LONG;
  26. /* our max() is unusable in constant expressions ;-/ */
  27. #define __const_max(x, y) ((x) < (y) ? (x) : (y))
  28. int sysctl_nr_open_max = __const_max(INT_MAX, ~(size_t)0/sizeof(void *)) &
  29. -BITS_PER_LONG;
  30. static void *alloc_fdmem(size_t size)
  31. {
  32. /*
  33. * Very large allocations can stress page reclaim, so fall back to
  34. * vmalloc() if the allocation size will be considered "large" by the VM.
  35. */
  36. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  37. void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY);
  38. if (data != NULL)
  39. return data;
  40. }
  41. return vmalloc(size);
  42. }
  43. static void __free_fdtable(struct fdtable *fdt)
  44. {
  45. kvfree(fdt->fd);
  46. kvfree(fdt->open_fds);
  47. kfree(fdt);
  48. }
  49. static void free_fdtable_rcu(struct rcu_head *rcu)
  50. {
  51. __free_fdtable(container_of(rcu, struct fdtable, rcu));
  52. }
  53. #define BITBIT_NR(nr) BITS_TO_LONGS(BITS_TO_LONGS(nr))
  54. #define BITBIT_SIZE(nr) (BITBIT_NR(nr) * sizeof(long))
  55. /*
  56. * Copy 'count' fd bits from the old table to the new table and clear the extra
  57. * space if any. This does not copy the file pointers. Called with the files
  58. * spinlock held for write.
  59. */
  60. static void copy_fd_bitmaps(struct fdtable *nfdt, struct fdtable *ofdt,
  61. unsigned int count)
  62. {
  63. unsigned int cpy, set;
  64. cpy = count / BITS_PER_BYTE;
  65. set = (nfdt->max_fds - count) / BITS_PER_BYTE;
  66. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  67. memset((char *)nfdt->open_fds + cpy, 0, set);
  68. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  69. memset((char *)nfdt->close_on_exec + cpy, 0, set);
  70. cpy = BITBIT_SIZE(count);
  71. set = BITBIT_SIZE(nfdt->max_fds) - cpy;
  72. memcpy(nfdt->full_fds_bits, ofdt->full_fds_bits, cpy);
  73. memset((char *)nfdt->full_fds_bits + cpy, 0, set);
  74. }
  75. /*
  76. * Copy all file descriptors from the old table to the new, expanded table and
  77. * clear the extra space. Called with the files spinlock held for write.
  78. */
  79. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  80. {
  81. unsigned int cpy, set;
  82. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  83. cpy = ofdt->max_fds * sizeof(struct file *);
  84. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  85. memcpy(nfdt->fd, ofdt->fd, cpy);
  86. memset((char *)nfdt->fd + cpy, 0, set);
  87. copy_fd_bitmaps(nfdt, ofdt, ofdt->max_fds);
  88. }
  89. static struct fdtable * alloc_fdtable(unsigned int nr)
  90. {
  91. struct fdtable *fdt;
  92. void *data;
  93. /*
  94. * Figure out how many fds we actually want to support in this fdtable.
  95. * Allocation steps are keyed to the size of the fdarray, since it
  96. * grows far faster than any of the other dynamic data. We try to fit
  97. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  98. * and growing in powers of two from there on.
  99. */
  100. nr /= (1024 / sizeof(struct file *));
  101. nr = roundup_pow_of_two(nr + 1);
  102. nr *= (1024 / sizeof(struct file *));
  103. /*
  104. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  105. * had been set lower between the check in expand_files() and here. Deal
  106. * with that in caller, it's cheaper that way.
  107. *
  108. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  109. * bitmaps handling below becomes unpleasant, to put it mildly...
  110. */
  111. if (unlikely(nr > sysctl_nr_open))
  112. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  113. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  114. if (!fdt)
  115. goto out;
  116. fdt->max_fds = nr;
  117. data = alloc_fdmem(nr * sizeof(struct file *));
  118. if (!data)
  119. goto out_fdt;
  120. fdt->fd = data;
  121. data = alloc_fdmem(max_t(size_t,
  122. 2 * nr / BITS_PER_BYTE + BITBIT_SIZE(nr), L1_CACHE_BYTES));
  123. if (!data)
  124. goto out_arr;
  125. fdt->open_fds = data;
  126. data += nr / BITS_PER_BYTE;
  127. fdt->close_on_exec = data;
  128. data += nr / BITS_PER_BYTE;
  129. fdt->full_fds_bits = data;
  130. return fdt;
  131. out_arr:
  132. kvfree(fdt->fd);
  133. out_fdt:
  134. kfree(fdt);
  135. out:
  136. return NULL;
  137. }
  138. /*
  139. * Expand the file descriptor table.
  140. * This function will allocate a new fdtable and both fd array and fdset, of
  141. * the given size.
  142. * Return <0 error code on error; 1 on successful completion.
  143. * The files->file_lock should be held on entry, and will be held on exit.
  144. */
  145. static int expand_fdtable(struct files_struct *files, int nr)
  146. __releases(files->file_lock)
  147. __acquires(files->file_lock)
  148. {
  149. struct fdtable *new_fdt, *cur_fdt;
  150. spin_unlock(&files->file_lock);
  151. new_fdt = alloc_fdtable(nr);
  152. /* make sure all __fd_install() have seen resize_in_progress
  153. * or have finished their rcu_read_lock_sched() section.
  154. */
  155. if (atomic_read(&files->count) > 1)
  156. synchronize_sched();
  157. spin_lock(&files->file_lock);
  158. if (!new_fdt)
  159. return -ENOMEM;
  160. /*
  161. * extremely unlikely race - sysctl_nr_open decreased between the check in
  162. * caller and alloc_fdtable(). Cheaper to catch it here...
  163. */
  164. if (unlikely(new_fdt->max_fds <= nr)) {
  165. __free_fdtable(new_fdt);
  166. return -EMFILE;
  167. }
  168. cur_fdt = files_fdtable(files);
  169. BUG_ON(nr < cur_fdt->max_fds);
  170. copy_fdtable(new_fdt, cur_fdt);
  171. rcu_assign_pointer(files->fdt, new_fdt);
  172. if (cur_fdt != &files->fdtab)
  173. call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
  174. /* coupled with smp_rmb() in __fd_install() */
  175. smp_wmb();
  176. return 1;
  177. }
  178. /*
  179. * Expand files.
  180. * This function will expand the file structures, if the requested size exceeds
  181. * the current capacity and there is room for expansion.
  182. * Return <0 error code on error; 0 when nothing done; 1 when files were
  183. * expanded and execution may have blocked.
  184. * The files->file_lock should be held on entry, and will be held on exit.
  185. */
  186. static int expand_files(struct files_struct *files, int nr)
  187. __releases(files->file_lock)
  188. __acquires(files->file_lock)
  189. {
  190. struct fdtable *fdt;
  191. int expanded = 0;
  192. repeat:
  193. fdt = files_fdtable(files);
  194. /* Do we need to expand? */
  195. if (nr < fdt->max_fds)
  196. return expanded;
  197. /* Can we expand? */
  198. if (nr >= sysctl_nr_open)
  199. return -EMFILE;
  200. if (unlikely(files->resize_in_progress)) {
  201. spin_unlock(&files->file_lock);
  202. expanded = 1;
  203. wait_event(files->resize_wait, !files->resize_in_progress);
  204. spin_lock(&files->file_lock);
  205. goto repeat;
  206. }
  207. /* All good, so we try */
  208. files->resize_in_progress = true;
  209. expanded = expand_fdtable(files, nr);
  210. files->resize_in_progress = false;
  211. wake_up_all(&files->resize_wait);
  212. return expanded;
  213. }
  214. static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
  215. {
  216. __set_bit(fd, fdt->close_on_exec);
  217. }
  218. static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
  219. {
  220. if (test_bit(fd, fdt->close_on_exec))
  221. __clear_bit(fd, fdt->close_on_exec);
  222. }
  223. static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt)
  224. {
  225. __set_bit(fd, fdt->open_fds);
  226. fd /= BITS_PER_LONG;
  227. if (!~fdt->open_fds[fd])
  228. __set_bit(fd, fdt->full_fds_bits);
  229. }
  230. static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt)
  231. {
  232. __clear_bit(fd, fdt->open_fds);
  233. __clear_bit(fd / BITS_PER_LONG, fdt->full_fds_bits);
  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 / BITS_PER_LONG; i > 0; ) {
  241. if (fdt->open_fds[--i])
  242. break;
  243. }
  244. i = (i + 1) * BITS_PER_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, 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->resize_in_progress = false;
  265. init_waitqueue_head(&newf->resize_wait);
  266. newf->next_fd = 0;
  267. new_fdt = &newf->fdtab;
  268. new_fdt->max_fds = NR_OPEN_DEFAULT;
  269. new_fdt->close_on_exec = newf->close_on_exec_init;
  270. new_fdt->open_fds = newf->open_fds_init;
  271. new_fdt->full_fds_bits = newf->full_fds_bits_init;
  272. new_fdt->fd = &newf->fd_array[0];
  273. spin_lock(&oldf->file_lock);
  274. old_fdt = files_fdtable(oldf);
  275. open_files = count_open_files(old_fdt);
  276. /*
  277. * Check whether we need to allocate a larger fd array and fd set.
  278. */
  279. while (unlikely(open_files > new_fdt->max_fds)) {
  280. spin_unlock(&oldf->file_lock);
  281. if (new_fdt != &newf->fdtab)
  282. __free_fdtable(new_fdt);
  283. new_fdt = alloc_fdtable(open_files - 1);
  284. if (!new_fdt) {
  285. *errorp = -ENOMEM;
  286. goto out_release;
  287. }
  288. /* beyond sysctl_nr_open; nothing to do */
  289. if (unlikely(new_fdt->max_fds < open_files)) {
  290. __free_fdtable(new_fdt);
  291. *errorp = -EMFILE;
  292. goto out_release;
  293. }
  294. /*
  295. * Reacquire the oldf lock and a pointer to its fd table
  296. * who knows it may have a new bigger fd table. We need
  297. * the latest pointer.
  298. */
  299. spin_lock(&oldf->file_lock);
  300. old_fdt = files_fdtable(oldf);
  301. open_files = count_open_files(old_fdt);
  302. }
  303. copy_fd_bitmaps(new_fdt, old_fdt, open_files);
  304. old_fds = old_fdt->fd;
  305. new_fds = new_fdt->fd;
  306. for (i = open_files; i != 0; i--) {
  307. struct file *f = *old_fds++;
  308. if (f) {
  309. get_file(f);
  310. } else {
  311. /*
  312. * The fd may be claimed in the fd bitmap but not yet
  313. * instantiated in the files array if a sibling thread
  314. * is partway through open(). So make sure that this
  315. * fd is available to the new process.
  316. */
  317. __clear_open_fd(open_files - i, new_fdt);
  318. }
  319. rcu_assign_pointer(*new_fds++, f);
  320. }
  321. spin_unlock(&oldf->file_lock);
  322. /* clear the remainder */
  323. memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));
  324. rcu_assign_pointer(newf->fdt, new_fdt);
  325. return newf;
  326. out_release:
  327. kmem_cache_free(files_cachep, newf);
  328. out:
  329. return NULL;
  330. }
  331. static struct fdtable *close_files(struct files_struct * files)
  332. {
  333. /*
  334. * It is safe to dereference the fd table without RCU or
  335. * ->file_lock because this is the last reference to the
  336. * files structure.
  337. */
  338. struct fdtable *fdt = rcu_dereference_raw(files->fdt);
  339. int i, j = 0;
  340. for (;;) {
  341. unsigned long set;
  342. i = j * BITS_PER_LONG;
  343. if (i >= fdt->max_fds)
  344. break;
  345. set = fdt->open_fds[j++];
  346. while (set) {
  347. if (set & 1) {
  348. struct file * file = xchg(&fdt->fd[i], NULL);
  349. if (file) {
  350. filp_close(file, files);
  351. cond_resched_rcu_qs();
  352. }
  353. }
  354. i++;
  355. set >>= 1;
  356. }
  357. }
  358. return fdt;
  359. }
  360. struct files_struct *get_files_struct(struct task_struct *task)
  361. {
  362. struct files_struct *files;
  363. task_lock(task);
  364. files = task->files;
  365. if (files)
  366. atomic_inc(&files->count);
  367. task_unlock(task);
  368. return files;
  369. }
  370. void put_files_struct(struct files_struct *files)
  371. {
  372. if (atomic_dec_and_test(&files->count)) {
  373. struct fdtable *fdt = close_files(files);
  374. /* free the arrays if they are not embedded */
  375. if (fdt != &files->fdtab)
  376. __free_fdtable(fdt);
  377. kmem_cache_free(files_cachep, files);
  378. }
  379. }
  380. void reset_files_struct(struct files_struct *files)
  381. {
  382. struct task_struct *tsk = current;
  383. struct files_struct *old;
  384. old = tsk->files;
  385. task_lock(tsk);
  386. tsk->files = files;
  387. task_unlock(tsk);
  388. put_files_struct(old);
  389. }
  390. void exit_files(struct task_struct *tsk)
  391. {
  392. struct files_struct * files = tsk->files;
  393. if (files) {
  394. task_lock(tsk);
  395. tsk->files = NULL;
  396. task_unlock(tsk);
  397. put_files_struct(files);
  398. }
  399. }
  400. struct files_struct init_files = {
  401. .count = ATOMIC_INIT(1),
  402. .fdt = &init_files.fdtab,
  403. .fdtab = {
  404. .max_fds = NR_OPEN_DEFAULT,
  405. .fd = &init_files.fd_array[0],
  406. .close_on_exec = init_files.close_on_exec_init,
  407. .open_fds = init_files.open_fds_init,
  408. .full_fds_bits = init_files.full_fds_bits_init,
  409. },
  410. .file_lock = __SPIN_LOCK_UNLOCKED(init_files.file_lock),
  411. };
  412. static unsigned long find_next_fd(struct fdtable *fdt, unsigned long start)
  413. {
  414. unsigned long maxfd = fdt->max_fds;
  415. unsigned long maxbit = maxfd / BITS_PER_LONG;
  416. unsigned long bitbit = start / BITS_PER_LONG;
  417. bitbit = find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG;
  418. if (bitbit > maxfd)
  419. return maxfd;
  420. if (bitbit > start)
  421. start = bitbit;
  422. return find_next_zero_bit(fdt->open_fds, maxfd, start);
  423. }
  424. /*
  425. * allocate a file descriptor, mark it busy.
  426. */
  427. int __alloc_fd(struct files_struct *files,
  428. unsigned start, unsigned end, unsigned flags)
  429. {
  430. unsigned int fd;
  431. int error;
  432. struct fdtable *fdt;
  433. spin_lock(&files->file_lock);
  434. repeat:
  435. fdt = files_fdtable(files);
  436. fd = start;
  437. if (fd < files->next_fd)
  438. fd = files->next_fd;
  439. if (fd < fdt->max_fds)
  440. fd = find_next_fd(fdt, fd);
  441. /*
  442. * N.B. For clone tasks sharing a files structure, this test
  443. * will limit the total number of files that can be opened.
  444. */
  445. error = -EMFILE;
  446. if (fd >= end)
  447. goto out;
  448. error = expand_files(files, fd);
  449. if (error < 0)
  450. goto out;
  451. /*
  452. * If we needed to expand the fs array we
  453. * might have blocked - try again.
  454. */
  455. if (error)
  456. goto repeat;
  457. if (start <= files->next_fd)
  458. files->next_fd = fd + 1;
  459. __set_open_fd(fd, fdt);
  460. if (flags & O_CLOEXEC)
  461. __set_close_on_exec(fd, fdt);
  462. else
  463. __clear_close_on_exec(fd, fdt);
  464. error = fd;
  465. #if 1
  466. /* Sanity check */
  467. if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
  468. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  469. rcu_assign_pointer(fdt->fd[fd], NULL);
  470. }
  471. #endif
  472. out:
  473. spin_unlock(&files->file_lock);
  474. return error;
  475. }
  476. static int alloc_fd(unsigned start, unsigned flags)
  477. {
  478. return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
  479. }
  480. int get_unused_fd_flags(unsigned flags)
  481. {
  482. return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
  483. }
  484. EXPORT_SYMBOL(get_unused_fd_flags);
  485. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  486. {
  487. struct fdtable *fdt = files_fdtable(files);
  488. __clear_open_fd(fd, fdt);
  489. if (fd < files->next_fd)
  490. files->next_fd = fd;
  491. }
  492. void put_unused_fd(unsigned int fd)
  493. {
  494. struct files_struct *files = current->files;
  495. spin_lock(&files->file_lock);
  496. __put_unused_fd(files, fd);
  497. spin_unlock(&files->file_lock);
  498. }
  499. EXPORT_SYMBOL(put_unused_fd);
  500. /*
  501. * Install a file pointer in the fd array.
  502. *
  503. * The VFS is full of places where we drop the files lock between
  504. * setting the open_fds bitmap and installing the file in the file
  505. * array. At any such point, we are vulnerable to a dup2() race
  506. * installing a file in the array before us. We need to detect this and
  507. * fput() the struct file we are about to overwrite in this case.
  508. *
  509. * It should never happen - if we allow dup2() do it, _really_ bad things
  510. * will follow.
  511. *
  512. * NOTE: __fd_install() variant is really, really low-level; don't
  513. * use it unless you are forced to by truly lousy API shoved down
  514. * your throat. 'files' *MUST* be either current->files or obtained
  515. * by get_files_struct(current) done by whoever had given it to you,
  516. * or really bad things will happen. Normally you want to use
  517. * fd_install() instead.
  518. */
  519. void __fd_install(struct files_struct *files, unsigned int fd,
  520. struct file *file)
  521. {
  522. struct fdtable *fdt;
  523. might_sleep();
  524. rcu_read_lock_sched();
  525. while (unlikely(files->resize_in_progress)) {
  526. rcu_read_unlock_sched();
  527. wait_event(files->resize_wait, !files->resize_in_progress);
  528. rcu_read_lock_sched();
  529. }
  530. /* coupled with smp_wmb() in expand_fdtable() */
  531. smp_rmb();
  532. fdt = rcu_dereference_sched(files->fdt);
  533. BUG_ON(fdt->fd[fd] != NULL);
  534. rcu_assign_pointer(fdt->fd[fd], file);
  535. rcu_read_unlock_sched();
  536. }
  537. void fd_install(unsigned int fd, struct file *file)
  538. {
  539. __fd_install(current->files, fd, file);
  540. }
  541. EXPORT_SYMBOL(fd_install);
  542. /*
  543. * The same warnings as for __alloc_fd()/__fd_install() apply here...
  544. */
  545. int __close_fd(struct files_struct *files, unsigned fd)
  546. {
  547. struct file *file;
  548. struct fdtable *fdt;
  549. spin_lock(&files->file_lock);
  550. fdt = files_fdtable(files);
  551. if (fd >= fdt->max_fds)
  552. goto out_unlock;
  553. file = fdt->fd[fd];
  554. if (!file)
  555. goto out_unlock;
  556. rcu_assign_pointer(fdt->fd[fd], NULL);
  557. __clear_close_on_exec(fd, fdt);
  558. __put_unused_fd(files, fd);
  559. spin_unlock(&files->file_lock);
  560. return filp_close(file, files);
  561. out_unlock:
  562. spin_unlock(&files->file_lock);
  563. return -EBADF;
  564. }
  565. void do_close_on_exec(struct files_struct *files)
  566. {
  567. unsigned i;
  568. struct fdtable *fdt;
  569. /* exec unshares first */
  570. spin_lock(&files->file_lock);
  571. for (i = 0; ; i++) {
  572. unsigned long set;
  573. unsigned fd = i * BITS_PER_LONG;
  574. fdt = files_fdtable(files);
  575. if (fd >= fdt->max_fds)
  576. break;
  577. set = fdt->close_on_exec[i];
  578. if (!set)
  579. continue;
  580. fdt->close_on_exec[i] = 0;
  581. for ( ; set ; fd++, set >>= 1) {
  582. struct file *file;
  583. if (!(set & 1))
  584. continue;
  585. file = fdt->fd[fd];
  586. if (!file)
  587. continue;
  588. rcu_assign_pointer(fdt->fd[fd], NULL);
  589. __put_unused_fd(files, fd);
  590. spin_unlock(&files->file_lock);
  591. filp_close(file, files);
  592. cond_resched();
  593. spin_lock(&files->file_lock);
  594. }
  595. }
  596. spin_unlock(&files->file_lock);
  597. }
  598. static struct file *__fget(unsigned int fd, fmode_t mask)
  599. {
  600. struct files_struct *files = current->files;
  601. struct file *file;
  602. rcu_read_lock();
  603. loop:
  604. file = fcheck_files(files, fd);
  605. if (file) {
  606. /* File object ref couldn't be taken.
  607. * dup2() atomicity guarantee is the reason
  608. * we loop to catch the new file (or NULL pointer)
  609. */
  610. if (file->f_mode & mask)
  611. file = NULL;
  612. else if (!get_file_rcu(file))
  613. goto loop;
  614. }
  615. rcu_read_unlock();
  616. return file;
  617. }
  618. struct file *fget(unsigned int fd)
  619. {
  620. return __fget(fd, FMODE_PATH);
  621. }
  622. EXPORT_SYMBOL(fget);
  623. struct file *fget_raw(unsigned int fd)
  624. {
  625. return __fget(fd, 0);
  626. }
  627. EXPORT_SYMBOL(fget_raw);
  628. /*
  629. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  630. *
  631. * You can use this instead of fget if you satisfy all of the following
  632. * conditions:
  633. * 1) You must call fput_light before exiting the syscall and returning control
  634. * to userspace (i.e. you cannot remember the returned struct file * after
  635. * returning to userspace).
  636. * 2) You must not call filp_close on the returned struct file * in between
  637. * calls to fget_light and fput_light.
  638. * 3) You must not clone the current task in between the calls to fget_light
  639. * and fput_light.
  640. *
  641. * The fput_needed flag returned by fget_light should be passed to the
  642. * corresponding fput_light.
  643. */
  644. static unsigned long __fget_light(unsigned int fd, fmode_t mask)
  645. {
  646. struct files_struct *files = current->files;
  647. struct file *file;
  648. if (atomic_read(&files->count) == 1) {
  649. file = __fcheck_files(files, fd);
  650. if (!file || unlikely(file->f_mode & mask))
  651. return 0;
  652. return (unsigned long)file;
  653. } else {
  654. file = __fget(fd, mask);
  655. if (!file)
  656. return 0;
  657. return FDPUT_FPUT | (unsigned long)file;
  658. }
  659. }
  660. unsigned long __fdget(unsigned int fd)
  661. {
  662. return __fget_light(fd, FMODE_PATH);
  663. }
  664. EXPORT_SYMBOL(__fdget);
  665. unsigned long __fdget_raw(unsigned int fd)
  666. {
  667. return __fget_light(fd, 0);
  668. }
  669. unsigned long __fdget_pos(unsigned int fd)
  670. {
  671. unsigned long v = __fdget(fd);
  672. struct file *file = (struct file *)(v & ~3);
  673. if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
  674. if (file_count(file) > 1) {
  675. v |= FDPUT_POS_UNLOCK;
  676. mutex_lock(&file->f_pos_lock);
  677. }
  678. }
  679. return v;
  680. }
  681. /*
  682. * We only lock f_pos if we have threads or if the file might be
  683. * shared with another process. In both cases we'll have an elevated
  684. * file count (done either by fdget() or by fork()).
  685. */
  686. void set_close_on_exec(unsigned int fd, int flag)
  687. {
  688. struct files_struct *files = current->files;
  689. struct fdtable *fdt;
  690. spin_lock(&files->file_lock);
  691. fdt = files_fdtable(files);
  692. if (flag)
  693. __set_close_on_exec(fd, fdt);
  694. else
  695. __clear_close_on_exec(fd, fdt);
  696. spin_unlock(&files->file_lock);
  697. }
  698. bool get_close_on_exec(unsigned int fd)
  699. {
  700. struct files_struct *files = current->files;
  701. struct fdtable *fdt;
  702. bool res;
  703. rcu_read_lock();
  704. fdt = files_fdtable(files);
  705. res = close_on_exec(fd, fdt);
  706. rcu_read_unlock();
  707. return res;
  708. }
  709. static int do_dup2(struct files_struct *files,
  710. struct file *file, unsigned fd, unsigned flags)
  711. __releases(&files->file_lock)
  712. {
  713. struct file *tofree;
  714. struct fdtable *fdt;
  715. /*
  716. * We need to detect attempts to do dup2() over allocated but still
  717. * not finished descriptor. NB: OpenBSD avoids that at the price of
  718. * extra work in their equivalent of fget() - they insert struct
  719. * file immediately after grabbing descriptor, mark it larval if
  720. * more work (e.g. actual opening) is needed and make sure that
  721. * fget() treats larval files as absent. Potentially interesting,
  722. * but while extra work in fget() is trivial, locking implications
  723. * and amount of surgery on open()-related paths in VFS are not.
  724. * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
  725. * deadlocks in rather amusing ways, AFAICS. All of that is out of
  726. * scope of POSIX or SUS, since neither considers shared descriptor
  727. * tables and this condition does not arise without those.
  728. */
  729. fdt = files_fdtable(files);
  730. tofree = fdt->fd[fd];
  731. if (!tofree && fd_is_open(fd, fdt))
  732. goto Ebusy;
  733. get_file(file);
  734. rcu_assign_pointer(fdt->fd[fd], file);
  735. __set_open_fd(fd, fdt);
  736. if (flags & O_CLOEXEC)
  737. __set_close_on_exec(fd, fdt);
  738. else
  739. __clear_close_on_exec(fd, fdt);
  740. spin_unlock(&files->file_lock);
  741. if (tofree)
  742. filp_close(tofree, files);
  743. return fd;
  744. Ebusy:
  745. spin_unlock(&files->file_lock);
  746. return -EBUSY;
  747. }
  748. int replace_fd(unsigned fd, struct file *file, unsigned flags)
  749. {
  750. int err;
  751. struct files_struct *files = current->files;
  752. if (!file)
  753. return __close_fd(files, fd);
  754. if (fd >= rlimit(RLIMIT_NOFILE))
  755. return -EBADF;
  756. spin_lock(&files->file_lock);
  757. err = expand_files(files, fd);
  758. if (unlikely(err < 0))
  759. goto out_unlock;
  760. return do_dup2(files, file, fd, flags);
  761. out_unlock:
  762. spin_unlock(&files->file_lock);
  763. return err;
  764. }
  765. SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
  766. {
  767. int err = -EBADF;
  768. struct file *file;
  769. struct files_struct *files = current->files;
  770. if ((flags & ~O_CLOEXEC) != 0)
  771. return -EINVAL;
  772. if (unlikely(oldfd == newfd))
  773. return -EINVAL;
  774. if (newfd >= rlimit(RLIMIT_NOFILE))
  775. return -EBADF;
  776. spin_lock(&files->file_lock);
  777. err = expand_files(files, newfd);
  778. file = fcheck(oldfd);
  779. if (unlikely(!file))
  780. goto Ebadf;
  781. if (unlikely(err < 0)) {
  782. if (err == -EMFILE)
  783. goto Ebadf;
  784. goto out_unlock;
  785. }
  786. return do_dup2(files, file, newfd, flags);
  787. Ebadf:
  788. err = -EBADF;
  789. out_unlock:
  790. spin_unlock(&files->file_lock);
  791. return err;
  792. }
  793. SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
  794. {
  795. if (unlikely(newfd == oldfd)) { /* corner case */
  796. struct files_struct *files = current->files;
  797. int retval = oldfd;
  798. rcu_read_lock();
  799. if (!fcheck_files(files, oldfd))
  800. retval = -EBADF;
  801. rcu_read_unlock();
  802. return retval;
  803. }
  804. return sys_dup3(oldfd, newfd, 0);
  805. }
  806. SYSCALL_DEFINE1(dup, unsigned int, fildes)
  807. {
  808. int ret = -EBADF;
  809. struct file *file = fget_raw(fildes);
  810. if (file) {
  811. ret = get_unused_fd_flags(0);
  812. if (ret >= 0)
  813. fd_install(ret, file);
  814. else
  815. fput(file);
  816. }
  817. return ret;
  818. }
  819. int f_dupfd(unsigned int from, struct file *file, unsigned flags)
  820. {
  821. int err;
  822. if (from >= rlimit(RLIMIT_NOFILE))
  823. return -EINVAL;
  824. err = alloc_fd(from, flags);
  825. if (err >= 0) {
  826. get_file(file);
  827. fd_install(err, file);
  828. }
  829. return err;
  830. }
  831. int iterate_fd(struct files_struct *files, unsigned n,
  832. int (*f)(const void *, struct file *, unsigned),
  833. const void *p)
  834. {
  835. struct fdtable *fdt;
  836. int res = 0;
  837. if (!files)
  838. return 0;
  839. spin_lock(&files->file_lock);
  840. for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
  841. struct file *file;
  842. file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
  843. if (!file)
  844. continue;
  845. res = f(p, file, n);
  846. if (res)
  847. break;
  848. }
  849. spin_unlock(&files->file_lock);
  850. return res;
  851. }
  852. EXPORT_SYMBOL(iterate_fd);