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. };
  413. static unsigned int find_next_fd(struct fdtable *fdt, unsigned int start)
  414. {
  415. unsigned int maxfd = fdt->max_fds;
  416. unsigned int maxbit = maxfd / BITS_PER_LONG;
  417. unsigned int bitbit = start / BITS_PER_LONG;
  418. bitbit = find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG;
  419. if (bitbit > maxfd)
  420. return maxfd;
  421. if (bitbit > start)
  422. start = bitbit;
  423. return find_next_zero_bit(fdt->open_fds, maxfd, start);
  424. }
  425. /*
  426. * allocate a file descriptor, mark it busy.
  427. */
  428. int __alloc_fd(struct files_struct *files,
  429. unsigned start, unsigned end, unsigned flags)
  430. {
  431. unsigned int fd;
  432. int error;
  433. struct fdtable *fdt;
  434. spin_lock(&files->file_lock);
  435. repeat:
  436. fdt = files_fdtable(files);
  437. fd = start;
  438. if (fd < files->next_fd)
  439. fd = files->next_fd;
  440. if (fd < fdt->max_fds)
  441. fd = find_next_fd(fdt, fd);
  442. /*
  443. * N.B. For clone tasks sharing a files structure, this test
  444. * will limit the total number of files that can be opened.
  445. */
  446. error = -EMFILE;
  447. if (fd >= end)
  448. goto out;
  449. error = expand_files(files, fd);
  450. if (error < 0)
  451. goto out;
  452. /*
  453. * If we needed to expand the fs array we
  454. * might have blocked - try again.
  455. */
  456. if (error)
  457. goto repeat;
  458. if (start <= files->next_fd)
  459. files->next_fd = fd + 1;
  460. __set_open_fd(fd, fdt);
  461. if (flags & O_CLOEXEC)
  462. __set_close_on_exec(fd, fdt);
  463. else
  464. __clear_close_on_exec(fd, fdt);
  465. error = fd;
  466. #if 1
  467. /* Sanity check */
  468. if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
  469. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  470. rcu_assign_pointer(fdt->fd[fd], NULL);
  471. }
  472. #endif
  473. out:
  474. spin_unlock(&files->file_lock);
  475. return error;
  476. }
  477. static int alloc_fd(unsigned start, unsigned flags)
  478. {
  479. return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
  480. }
  481. int get_unused_fd_flags(unsigned flags)
  482. {
  483. return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
  484. }
  485. EXPORT_SYMBOL(get_unused_fd_flags);
  486. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  487. {
  488. struct fdtable *fdt = files_fdtable(files);
  489. __clear_open_fd(fd, fdt);
  490. if (fd < files->next_fd)
  491. files->next_fd = fd;
  492. }
  493. void put_unused_fd(unsigned int fd)
  494. {
  495. struct files_struct *files = current->files;
  496. spin_lock(&files->file_lock);
  497. __put_unused_fd(files, fd);
  498. spin_unlock(&files->file_lock);
  499. }
  500. EXPORT_SYMBOL(put_unused_fd);
  501. /*
  502. * Install a file pointer in the fd array.
  503. *
  504. * The VFS is full of places where we drop the files lock between
  505. * setting the open_fds bitmap and installing the file in the file
  506. * array. At any such point, we are vulnerable to a dup2() race
  507. * installing a file in the array before us. We need to detect this and
  508. * fput() the struct file we are about to overwrite in this case.
  509. *
  510. * It should never happen - if we allow dup2() do it, _really_ bad things
  511. * will follow.
  512. *
  513. * NOTE: __fd_install() variant is really, really low-level; don't
  514. * use it unless you are forced to by truly lousy API shoved down
  515. * your throat. 'files' *MUST* be either current->files or obtained
  516. * by get_files_struct(current) done by whoever had given it to you,
  517. * or really bad things will happen. Normally you want to use
  518. * fd_install() instead.
  519. */
  520. void __fd_install(struct files_struct *files, unsigned int fd,
  521. struct file *file)
  522. {
  523. struct fdtable *fdt;
  524. might_sleep();
  525. rcu_read_lock_sched();
  526. while (unlikely(files->resize_in_progress)) {
  527. rcu_read_unlock_sched();
  528. wait_event(files->resize_wait, !files->resize_in_progress);
  529. rcu_read_lock_sched();
  530. }
  531. /* coupled with smp_wmb() in expand_fdtable() */
  532. smp_rmb();
  533. fdt = rcu_dereference_sched(files->fdt);
  534. BUG_ON(fdt->fd[fd] != NULL);
  535. rcu_assign_pointer(fdt->fd[fd], file);
  536. rcu_read_unlock_sched();
  537. }
  538. void fd_install(unsigned int fd, struct file *file)
  539. {
  540. __fd_install(current->files, fd, file);
  541. }
  542. EXPORT_SYMBOL(fd_install);
  543. /*
  544. * The same warnings as for __alloc_fd()/__fd_install() apply here...
  545. */
  546. int __close_fd(struct files_struct *files, unsigned fd)
  547. {
  548. struct file *file;
  549. struct fdtable *fdt;
  550. spin_lock(&files->file_lock);
  551. fdt = files_fdtable(files);
  552. if (fd >= fdt->max_fds)
  553. goto out_unlock;
  554. file = fdt->fd[fd];
  555. if (!file)
  556. goto out_unlock;
  557. rcu_assign_pointer(fdt->fd[fd], NULL);
  558. __clear_close_on_exec(fd, fdt);
  559. __put_unused_fd(files, fd);
  560. spin_unlock(&files->file_lock);
  561. return filp_close(file, files);
  562. out_unlock:
  563. spin_unlock(&files->file_lock);
  564. return -EBADF;
  565. }
  566. void do_close_on_exec(struct files_struct *files)
  567. {
  568. unsigned i;
  569. struct fdtable *fdt;
  570. /* exec unshares first */
  571. spin_lock(&files->file_lock);
  572. for (i = 0; ; i++) {
  573. unsigned long set;
  574. unsigned fd = i * BITS_PER_LONG;
  575. fdt = files_fdtable(files);
  576. if (fd >= fdt->max_fds)
  577. break;
  578. set = fdt->close_on_exec[i];
  579. if (!set)
  580. continue;
  581. fdt->close_on_exec[i] = 0;
  582. for ( ; set ; fd++, set >>= 1) {
  583. struct file *file;
  584. if (!(set & 1))
  585. continue;
  586. file = fdt->fd[fd];
  587. if (!file)
  588. continue;
  589. rcu_assign_pointer(fdt->fd[fd], NULL);
  590. __put_unused_fd(files, fd);
  591. spin_unlock(&files->file_lock);
  592. filp_close(file, files);
  593. cond_resched();
  594. spin_lock(&files->file_lock);
  595. }
  596. }
  597. spin_unlock(&files->file_lock);
  598. }
  599. static struct file *__fget(unsigned int fd, fmode_t mask)
  600. {
  601. struct files_struct *files = current->files;
  602. struct file *file;
  603. rcu_read_lock();
  604. loop:
  605. file = fcheck_files(files, fd);
  606. if (file) {
  607. /* File object ref couldn't be taken.
  608. * dup2() atomicity guarantee is the reason
  609. * we loop to catch the new file (or NULL pointer)
  610. */
  611. if (file->f_mode & mask)
  612. file = NULL;
  613. else if (!get_file_rcu(file))
  614. goto loop;
  615. }
  616. rcu_read_unlock();
  617. return file;
  618. }
  619. struct file *fget(unsigned int fd)
  620. {
  621. return __fget(fd, FMODE_PATH);
  622. }
  623. EXPORT_SYMBOL(fget);
  624. struct file *fget_raw(unsigned int fd)
  625. {
  626. return __fget(fd, 0);
  627. }
  628. EXPORT_SYMBOL(fget_raw);
  629. /*
  630. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  631. *
  632. * You can use this instead of fget if you satisfy all of the following
  633. * conditions:
  634. * 1) You must call fput_light before exiting the syscall and returning control
  635. * to userspace (i.e. you cannot remember the returned struct file * after
  636. * returning to userspace).
  637. * 2) You must not call filp_close on the returned struct file * in between
  638. * calls to fget_light and fput_light.
  639. * 3) You must not clone the current task in between the calls to fget_light
  640. * and fput_light.
  641. *
  642. * The fput_needed flag returned by fget_light should be passed to the
  643. * corresponding fput_light.
  644. */
  645. static unsigned long __fget_light(unsigned int fd, fmode_t mask)
  646. {
  647. struct files_struct *files = current->files;
  648. struct file *file;
  649. if (atomic_read(&files->count) == 1) {
  650. file = __fcheck_files(files, fd);
  651. if (!file || unlikely(file->f_mode & mask))
  652. return 0;
  653. return (unsigned long)file;
  654. } else {
  655. file = __fget(fd, mask);
  656. if (!file)
  657. return 0;
  658. return FDPUT_FPUT | (unsigned long)file;
  659. }
  660. }
  661. unsigned long __fdget(unsigned int fd)
  662. {
  663. return __fget_light(fd, FMODE_PATH);
  664. }
  665. EXPORT_SYMBOL(__fdget);
  666. unsigned long __fdget_raw(unsigned int fd)
  667. {
  668. return __fget_light(fd, 0);
  669. }
  670. unsigned long __fdget_pos(unsigned int fd)
  671. {
  672. unsigned long v = __fdget(fd);
  673. struct file *file = (struct file *)(v & ~3);
  674. if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
  675. if (file_count(file) > 1) {
  676. v |= FDPUT_POS_UNLOCK;
  677. mutex_lock(&file->f_pos_lock);
  678. }
  679. }
  680. return v;
  681. }
  682. void __f_unlock_pos(struct file *f)
  683. {
  684. mutex_unlock(&f->f_pos_lock);
  685. }
  686. /*
  687. * We only lock f_pos if we have threads or if the file might be
  688. * shared with another process. In both cases we'll have an elevated
  689. * file count (done either by fdget() or by fork()).
  690. */
  691. void set_close_on_exec(unsigned int fd, int flag)
  692. {
  693. struct files_struct *files = current->files;
  694. struct fdtable *fdt;
  695. spin_lock(&files->file_lock);
  696. fdt = files_fdtable(files);
  697. if (flag)
  698. __set_close_on_exec(fd, fdt);
  699. else
  700. __clear_close_on_exec(fd, fdt);
  701. spin_unlock(&files->file_lock);
  702. }
  703. bool get_close_on_exec(unsigned int fd)
  704. {
  705. struct files_struct *files = current->files;
  706. struct fdtable *fdt;
  707. bool res;
  708. rcu_read_lock();
  709. fdt = files_fdtable(files);
  710. res = close_on_exec(fd, fdt);
  711. rcu_read_unlock();
  712. return res;
  713. }
  714. static int do_dup2(struct files_struct *files,
  715. struct file *file, unsigned fd, unsigned flags)
  716. __releases(&files->file_lock)
  717. {
  718. struct file *tofree;
  719. struct fdtable *fdt;
  720. /*
  721. * We need to detect attempts to do dup2() over allocated but still
  722. * not finished descriptor. NB: OpenBSD avoids that at the price of
  723. * extra work in their equivalent of fget() - they insert struct
  724. * file immediately after grabbing descriptor, mark it larval if
  725. * more work (e.g. actual opening) is needed and make sure that
  726. * fget() treats larval files as absent. Potentially interesting,
  727. * but while extra work in fget() is trivial, locking implications
  728. * and amount of surgery on open()-related paths in VFS are not.
  729. * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
  730. * deadlocks in rather amusing ways, AFAICS. All of that is out of
  731. * scope of POSIX or SUS, since neither considers shared descriptor
  732. * tables and this condition does not arise without those.
  733. */
  734. fdt = files_fdtable(files);
  735. tofree = fdt->fd[fd];
  736. if (!tofree && fd_is_open(fd, fdt))
  737. goto Ebusy;
  738. get_file(file);
  739. rcu_assign_pointer(fdt->fd[fd], file);
  740. __set_open_fd(fd, fdt);
  741. if (flags & O_CLOEXEC)
  742. __set_close_on_exec(fd, fdt);
  743. else
  744. __clear_close_on_exec(fd, fdt);
  745. spin_unlock(&files->file_lock);
  746. if (tofree)
  747. filp_close(tofree, files);
  748. return fd;
  749. Ebusy:
  750. spin_unlock(&files->file_lock);
  751. return -EBUSY;
  752. }
  753. int replace_fd(unsigned fd, struct file *file, unsigned flags)
  754. {
  755. int err;
  756. struct files_struct *files = current->files;
  757. if (!file)
  758. return __close_fd(files, fd);
  759. if (fd >= rlimit(RLIMIT_NOFILE))
  760. return -EBADF;
  761. spin_lock(&files->file_lock);
  762. err = expand_files(files, fd);
  763. if (unlikely(err < 0))
  764. goto out_unlock;
  765. return do_dup2(files, file, fd, flags);
  766. out_unlock:
  767. spin_unlock(&files->file_lock);
  768. return err;
  769. }
  770. SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
  771. {
  772. int err = -EBADF;
  773. struct file *file;
  774. struct files_struct *files = current->files;
  775. if ((flags & ~O_CLOEXEC) != 0)
  776. return -EINVAL;
  777. if (unlikely(oldfd == newfd))
  778. return -EINVAL;
  779. if (newfd >= rlimit(RLIMIT_NOFILE))
  780. return -EBADF;
  781. spin_lock(&files->file_lock);
  782. err = expand_files(files, newfd);
  783. file = fcheck(oldfd);
  784. if (unlikely(!file))
  785. goto Ebadf;
  786. if (unlikely(err < 0)) {
  787. if (err == -EMFILE)
  788. goto Ebadf;
  789. goto out_unlock;
  790. }
  791. return do_dup2(files, file, newfd, flags);
  792. Ebadf:
  793. err = -EBADF;
  794. out_unlock:
  795. spin_unlock(&files->file_lock);
  796. return err;
  797. }
  798. SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
  799. {
  800. if (unlikely(newfd == oldfd)) { /* corner case */
  801. struct files_struct *files = current->files;
  802. int retval = oldfd;
  803. rcu_read_lock();
  804. if (!fcheck_files(files, oldfd))
  805. retval = -EBADF;
  806. rcu_read_unlock();
  807. return retval;
  808. }
  809. return sys_dup3(oldfd, newfd, 0);
  810. }
  811. SYSCALL_DEFINE1(dup, unsigned int, fildes)
  812. {
  813. int ret = -EBADF;
  814. struct file *file = fget_raw(fildes);
  815. if (file) {
  816. ret = get_unused_fd_flags(0);
  817. if (ret >= 0)
  818. fd_install(ret, file);
  819. else
  820. fput(file);
  821. }
  822. return ret;
  823. }
  824. int f_dupfd(unsigned int from, struct file *file, unsigned flags)
  825. {
  826. int err;
  827. if (from >= rlimit(RLIMIT_NOFILE))
  828. return -EINVAL;
  829. err = alloc_fd(from, flags);
  830. if (err >= 0) {
  831. get_file(file);
  832. fd_install(err, file);
  833. }
  834. return err;
  835. }
  836. int iterate_fd(struct files_struct *files, unsigned n,
  837. int (*f)(const void *, struct file *, unsigned),
  838. const void *p)
  839. {
  840. struct fdtable *fdt;
  841. int res = 0;
  842. if (!files)
  843. return 0;
  844. spin_lock(&files->file_lock);
  845. for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
  846. struct file *file;
  847. file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
  848. if (!file)
  849. continue;
  850. res = f(p, file, n);
  851. if (res)
  852. break;
  853. }
  854. spin_unlock(&files->file_lock);
  855. return res;
  856. }
  857. EXPORT_SYMBOL(iterate_fd);