file.c 23 KB

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