file.c 22 KB

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