file.c 22 KB

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