file.c 21 KB

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