file.c 24 KB

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