file.c 24 KB

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