file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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/export.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/mmzone.h>
  12. #include <linux/time.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/file.h>
  17. #include <linux/fdtable.h>
  18. #include <linux/bitops.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/workqueue.h>
  23. struct fdtable_defer {
  24. spinlock_t lock;
  25. struct work_struct wq;
  26. struct fdtable *next;
  27. };
  28. int sysctl_nr_open __read_mostly = 1024*1024;
  29. int sysctl_nr_open_min = BITS_PER_LONG;
  30. int sysctl_nr_open_max = 1024 * 1024; /* raised later */
  31. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  32. extern void sec_debug_EMFILE_error_proc(unsigned long files_addr);
  33. #endif
  34. /*
  35. * We use this list to defer free fdtables that have vmalloced
  36. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  37. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  38. * this per-task structure.
  39. */
  40. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  41. static void *alloc_fdmem(size_t size)
  42. {
  43. /*
  44. * Very large allocations can stress page reclaim, so fall back to
  45. * vmalloc() if the allocation size will be considered "large" by the VM.
  46. */
  47. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  48. void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY);
  49. if (data != NULL)
  50. return data;
  51. }
  52. return vmalloc(size);
  53. }
  54. static void free_fdmem(void *ptr)
  55. {
  56. is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
  57. }
  58. static void __free_fdtable(struct fdtable *fdt)
  59. {
  60. free_fdmem(fdt->fd);
  61. free_fdmem(fdt->open_fds);
  62. kfree(fdt);
  63. }
  64. static void free_fdtable_work(struct work_struct *work)
  65. {
  66. struct fdtable_defer *f =
  67. container_of(work, struct fdtable_defer, wq);
  68. struct fdtable *fdt;
  69. spin_lock_bh(&f->lock);
  70. fdt = f->next;
  71. f->next = NULL;
  72. spin_unlock_bh(&f->lock);
  73. while(fdt) {
  74. struct fdtable *next = fdt->next;
  75. __free_fdtable(fdt);
  76. fdt = next;
  77. }
  78. }
  79. void free_fdtable_rcu(struct rcu_head *rcu)
  80. {
  81. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  82. struct fdtable_defer *fddef;
  83. BUG_ON(!fdt);
  84. if (fdt->max_fds <= NR_OPEN_DEFAULT) {
  85. /*
  86. * This fdtable is embedded in the files structure and that
  87. * structure itself is getting destroyed.
  88. */
  89. kmem_cache_free(files_cachep,
  90. container_of(fdt, struct files_struct, fdtab));
  91. return;
  92. }
  93. if (!is_vmalloc_addr(fdt->fd) && !is_vmalloc_addr(fdt->open_fds)) {
  94. kfree(fdt->fd);
  95. kfree(fdt->open_fds);
  96. kfree(fdt);
  97. } else {
  98. fddef = &get_cpu_var(fdtable_defer_list);
  99. spin_lock(&fddef->lock);
  100. fdt->next = fddef->next;
  101. fddef->next = fdt;
  102. /* vmallocs are handled from the workqueue context */
  103. schedule_work(&fddef->wq);
  104. spin_unlock(&fddef->lock);
  105. put_cpu_var(fdtable_defer_list);
  106. }
  107. }
  108. /*
  109. * Expand the fdset in the files_struct. Called with the files spinlock
  110. * held for write.
  111. */
  112. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  113. {
  114. size_t cpy, set;
  115. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  116. cpy = ofdt->max_fds * sizeof(struct file *);
  117. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  118. memcpy(nfdt->fd, ofdt->fd, cpy);
  119. memset((char *)(nfdt->fd) + cpy, 0, set);
  120. cpy = ofdt->max_fds / BITS_PER_BYTE;
  121. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  122. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  123. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  124. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  125. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  126. }
  127. static struct fdtable * alloc_fdtable(unsigned int nr)
  128. {
  129. struct fdtable *fdt;
  130. void *data;
  131. /*
  132. * Figure out how many fds we actually want to support in this fdtable.
  133. * Allocation steps are keyed to the size of the fdarray, since it
  134. * grows far faster than any of the other dynamic data. We try to fit
  135. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  136. * and growing in powers of two from there on.
  137. */
  138. nr /= (1024 / sizeof(struct file *));
  139. nr = roundup_pow_of_two(nr + 1);
  140. nr *= (1024 / sizeof(struct file *));
  141. /*
  142. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  143. * had been set lower between the check in expand_files() and here. Deal
  144. * with that in caller, it's cheaper that way.
  145. *
  146. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  147. * bitmaps handling below becomes unpleasant, to put it mildly...
  148. */
  149. if (unlikely(nr > sysctl_nr_open))
  150. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  151. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  152. if (!fdt)
  153. goto out;
  154. fdt->max_fds = nr;
  155. data = alloc_fdmem(nr * sizeof(struct file *));
  156. if (!data)
  157. goto out_fdt;
  158. fdt->fd = data;
  159. data = alloc_fdmem(max_t(size_t,
  160. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  161. if (!data)
  162. goto out_arr;
  163. fdt->open_fds = data;
  164. data += nr / BITS_PER_BYTE;
  165. fdt->close_on_exec = data;
  166. fdt->next = NULL;
  167. return fdt;
  168. out_arr:
  169. free_fdmem(fdt->fd);
  170. out_fdt:
  171. kfree(fdt);
  172. out:
  173. return NULL;
  174. }
  175. /*
  176. * Expand the file descriptor table.
  177. * This function will allocate a new fdtable and both fd array and fdset, of
  178. * the given size.
  179. * Return <0 error code on error; 1 on successful completion.
  180. * The files->file_lock should be held on entry, and will be held on exit.
  181. */
  182. static int expand_fdtable(struct files_struct *files, int nr)
  183. __releases(files->file_lock)
  184. __acquires(files->file_lock)
  185. {
  186. struct fdtable *new_fdt, *cur_fdt;
  187. spin_unlock(&files->file_lock);
  188. new_fdt = alloc_fdtable(nr);
  189. spin_lock(&files->file_lock);
  190. if (!new_fdt)
  191. return -ENOMEM;
  192. /*
  193. * extremely unlikely race - sysctl_nr_open decreased between the check in
  194. * caller and alloc_fdtable(). Cheaper to catch it here...
  195. */
  196. if (unlikely(new_fdt->max_fds <= nr)) {
  197. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  198. sec_debug_EMFILE_error_proc((unsigned long)files);
  199. #endif
  200. __free_fdtable(new_fdt);
  201. return -EMFILE;
  202. }
  203. /*
  204. * Check again since another task may have expanded the fd table while
  205. * we dropped the lock
  206. */
  207. cur_fdt = files_fdtable(files);
  208. if (nr >= cur_fdt->max_fds) {
  209. /* Continue as planned */
  210. copy_fdtable(new_fdt, cur_fdt);
  211. rcu_assign_pointer(files->fdt, new_fdt);
  212. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  213. free_fdtable(cur_fdt);
  214. } else {
  215. /* Somebody else expanded, so undo our attempt */
  216. __free_fdtable(new_fdt);
  217. }
  218. return 1;
  219. }
  220. /*
  221. * Expand files.
  222. * This function will expand the file structures, if the requested size exceeds
  223. * the current capacity and there is room for expansion.
  224. * Return <0 error code on error; 0 when nothing done; 1 when files were
  225. * expanded and execution may have blocked.
  226. * The files->file_lock should be held on entry, and will be held on exit.
  227. */
  228. int expand_files(struct files_struct *files, int nr)
  229. {
  230. struct fdtable *fdt;
  231. fdt = files_fdtable(files);
  232. /* Do we need to expand? */
  233. if (nr < fdt->max_fds)
  234. return 0;
  235. /* Can we expand? */
  236. if (nr >= sysctl_nr_open) {
  237. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  238. sec_debug_EMFILE_error_proc((unsigned long)files);
  239. #endif
  240. return -EMFILE;
  241. }
  242. /* All good, so we try */
  243. return expand_fdtable(files, nr);
  244. }
  245. static int count_open_files(struct fdtable *fdt)
  246. {
  247. int size = fdt->max_fds;
  248. int i;
  249. /* Find the last open fd */
  250. for (i = size / BITS_PER_LONG; i > 0; ) {
  251. if (fdt->open_fds[--i])
  252. break;
  253. }
  254. i = (i + 1) * BITS_PER_LONG;
  255. return i;
  256. }
  257. /*
  258. * Allocate a new files structure and copy contents from the
  259. * passed in files structure.
  260. * errorp will be valid only when the returned files_struct is NULL.
  261. */
  262. struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  263. {
  264. struct files_struct *newf;
  265. struct file **old_fds, **new_fds;
  266. int open_files, size, i;
  267. struct fdtable *old_fdt, *new_fdt;
  268. *errorp = -ENOMEM;
  269. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  270. if (!newf)
  271. goto out;
  272. atomic_set(&newf->count, 1);
  273. spin_lock_init(&newf->file_lock);
  274. newf->next_fd = 0;
  275. new_fdt = &newf->fdtab;
  276. new_fdt->max_fds = NR_OPEN_DEFAULT;
  277. new_fdt->close_on_exec = newf->close_on_exec_init;
  278. new_fdt->open_fds = newf->open_fds_init;
  279. new_fdt->fd = &newf->fd_array[0];
  280. new_fdt->next = NULL;
  281. spin_lock(&oldf->file_lock);
  282. old_fdt = files_fdtable(oldf);
  283. open_files = count_open_files(old_fdt);
  284. /*
  285. * Check whether we need to allocate a larger fd array and fd set.
  286. */
  287. while (unlikely(open_files > new_fdt->max_fds)) {
  288. spin_unlock(&oldf->file_lock);
  289. if (new_fdt != &newf->fdtab)
  290. __free_fdtable(new_fdt);
  291. new_fdt = alloc_fdtable(open_files - 1);
  292. if (!new_fdt) {
  293. *errorp = -ENOMEM;
  294. goto out_release;
  295. }
  296. /* beyond sysctl_nr_open; nothing to do */
  297. if (unlikely(new_fdt->max_fds < open_files)) {
  298. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  299. sec_debug_EMFILE_error_proc((unsigned long)oldf);
  300. #endif
  301. __free_fdtable(new_fdt);
  302. *errorp = -EMFILE;
  303. goto out_release;
  304. }
  305. /*
  306. * Reacquire the oldf lock and a pointer to its fd table
  307. * who knows it may have a new bigger fd table. We need
  308. * the latest pointer.
  309. */
  310. spin_lock(&oldf->file_lock);
  311. old_fdt = files_fdtable(oldf);
  312. open_files = count_open_files(old_fdt);
  313. }
  314. old_fds = old_fdt->fd;
  315. new_fds = new_fdt->fd;
  316. memcpy(new_fdt->open_fds, old_fdt->open_fds, open_files / 8);
  317. memcpy(new_fdt->close_on_exec, old_fdt->close_on_exec, open_files / 8);
  318. for (i = open_files; i != 0; i--) {
  319. struct file *f = *old_fds++;
  320. if (f) {
  321. get_file(f);
  322. } else {
  323. /*
  324. * The fd may be claimed in the fd bitmap but not yet
  325. * instantiated in the files array if a sibling thread
  326. * is partway through open(). So make sure that this
  327. * fd is available to the new process.
  328. */
  329. __clear_open_fd(open_files - i, new_fdt);
  330. }
  331. rcu_assign_pointer(*new_fds++, f);
  332. }
  333. spin_unlock(&oldf->file_lock);
  334. /* compute the remainder to be cleared */
  335. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  336. /* This is long word aligned thus could use a optimized version */
  337. memset(new_fds, 0, size);
  338. if (new_fdt->max_fds > open_files) {
  339. int left = (new_fdt->max_fds - open_files) / 8;
  340. int start = open_files / BITS_PER_LONG;
  341. memset(&new_fdt->open_fds[start], 0, left);
  342. memset(&new_fdt->close_on_exec[start], 0, left);
  343. }
  344. rcu_assign_pointer(newf->fdt, new_fdt);
  345. return newf;
  346. out_release:
  347. kmem_cache_free(files_cachep, newf);
  348. out:
  349. return NULL;
  350. }
  351. static void __devinit fdtable_defer_list_init(int cpu)
  352. {
  353. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  354. spin_lock_init(&fddef->lock);
  355. INIT_WORK(&fddef->wq, free_fdtable_work);
  356. fddef->next = NULL;
  357. }
  358. void __init files_defer_init(void)
  359. {
  360. int i;
  361. for_each_possible_cpu(i)
  362. fdtable_defer_list_init(i);
  363. sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
  364. -BITS_PER_LONG;
  365. }
  366. struct files_struct init_files = {
  367. .count = ATOMIC_INIT(1),
  368. .fdt = &init_files.fdtab,
  369. .fdtab = {
  370. .max_fds = NR_OPEN_DEFAULT,
  371. .fd = &init_files.fd_array[0],
  372. .close_on_exec = init_files.close_on_exec_init,
  373. .open_fds = init_files.open_fds_init,
  374. },
  375. .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
  376. };
  377. /*
  378. * allocate a file descriptor, mark it busy.
  379. */
  380. int __alloc_fd(struct files_struct *files,
  381. unsigned start, unsigned end, unsigned flags)
  382. {
  383. unsigned int fd;
  384. int error;
  385. struct fdtable *fdt;
  386. spin_lock(&files->file_lock);
  387. repeat:
  388. fdt = files_fdtable(files);
  389. fd = start;
  390. if (fd < files->next_fd)
  391. fd = files->next_fd;
  392. if (fd < fdt->max_fds)
  393. fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);
  394. /*
  395. * N.B. For clone tasks sharing a files structure, this test
  396. * will limit the total number of files that can be opened.
  397. */
  398. error = -EMFILE;
  399. if (fd >= end) {
  400. #ifdef CONFIG_SEC_FILE_LEAK_DEBUG
  401. sec_debug_EMFILE_error_proc((unsigned long)files);
  402. #endif
  403. goto out;
  404. }
  405. error = expand_files(files, fd);
  406. if (error < 0)
  407. goto out;
  408. /*
  409. * If we needed to expand the fs array we
  410. * might have blocked - try again.
  411. */
  412. if (error)
  413. goto repeat;
  414. if (start <= files->next_fd)
  415. files->next_fd = fd + 1;
  416. __set_open_fd(fd, fdt);
  417. if (flags & O_CLOEXEC)
  418. __set_close_on_exec(fd, fdt);
  419. else
  420. __clear_close_on_exec(fd, fdt);
  421. error = fd;
  422. #if 1
  423. /* Sanity check */
  424. if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
  425. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  426. rcu_assign_pointer(fdt->fd[fd], NULL);
  427. }
  428. #endif
  429. out:
  430. spin_unlock(&files->file_lock);
  431. return error;
  432. }
  433. EXPORT_SYMBOL(alloc_fd);
  434. int alloc_fd(unsigned start, unsigned flags)
  435. {
  436. return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
  437. }
  438. int get_unused_fd_flags(unsigned flags)
  439. {
  440. return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
  441. }
  442. EXPORT_SYMBOL(get_unused_fd_flags);
  443. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  444. {
  445. struct fdtable *fdt = files_fdtable(files);
  446. __clear_open_fd(fd, fdt);
  447. if (fd < files->next_fd)
  448. files->next_fd = fd;
  449. }
  450. void put_unused_fd(unsigned int fd)
  451. {
  452. struct files_struct *files = current->files;
  453. spin_lock(&files->file_lock);
  454. __put_unused_fd(files, fd);
  455. spin_unlock(&files->file_lock);
  456. }
  457. EXPORT_SYMBOL(put_unused_fd);
  458. /*
  459. * Install a file pointer in the fd array.
  460. *
  461. * The VFS is full of places where we drop the files lock between
  462. * setting the open_fds bitmap and installing the file in the file
  463. * array. At any such point, we are vulnerable to a dup2() race
  464. * installing a file in the array before us. We need to detect this and
  465. * fput() the struct file we are about to overwrite in this case.
  466. *
  467. * It should never happen - if we allow dup2() do it, _really_ bad things
  468. * will follow.
  469. *
  470. * NOTE: __fd_install() variant is really, really low-level; don't
  471. * use it unless you are forced to by truly lousy API shoved down
  472. * your throat. 'files' *MUST* be either current->files or obtained
  473. * by get_files_struct(current) done by whoever had given it to you,
  474. * or really bad things will happen. Normally you want to use
  475. * fd_install() instead.
  476. */
  477. void __fd_install(struct files_struct *files, unsigned int fd,
  478. struct file *file)
  479. {
  480. struct fdtable *fdt;
  481. spin_lock(&files->file_lock);
  482. fdt = files_fdtable(files);
  483. BUG_ON(fdt->fd[fd] != NULL);
  484. rcu_assign_pointer(fdt->fd[fd], file);
  485. spin_unlock(&files->file_lock);
  486. }
  487. void fd_install(unsigned int fd, struct file *file)
  488. {
  489. __fd_install(current->files, fd, file);
  490. }
  491. EXPORT_SYMBOL(fd_install);
  492. /*
  493. * The same warnings as for __alloc_fd()/__fd_install() apply here...
  494. */
  495. int __close_fd(struct files_struct *files, unsigned fd)
  496. {
  497. struct file *file;
  498. struct fdtable *fdt;
  499. spin_lock(&files->file_lock);
  500. fdt = files_fdtable(files);
  501. if (fd >= fdt->max_fds)
  502. goto out_unlock;
  503. file = fdt->fd[fd];
  504. if (!file)
  505. goto out_unlock;
  506. rcu_assign_pointer(fdt->fd[fd], NULL);
  507. __clear_close_on_exec(fd, fdt);
  508. __put_unused_fd(files, fd);
  509. spin_unlock(&files->file_lock);
  510. return filp_close(file, files);
  511. out_unlock:
  512. spin_unlock(&files->file_lock);
  513. return -EBADF;
  514. }
  515. struct file *fget(unsigned int fd)
  516. {
  517. struct file *file;
  518. struct files_struct *files = current->files;
  519. rcu_read_lock();
  520. file = fcheck_files(files, fd);
  521. if (file) {
  522. /* File object ref couldn't be taken */
  523. if (file->f_mode & FMODE_PATH ||
  524. !atomic_long_inc_not_zero(&file->f_count))
  525. file = NULL;
  526. }
  527. rcu_read_unlock();
  528. return file;
  529. }
  530. EXPORT_SYMBOL(fget);
  531. struct file *fget_raw(unsigned int fd)
  532. {
  533. struct file *file;
  534. struct files_struct *files = current->files;
  535. rcu_read_lock();
  536. file = fcheck_files(files, fd);
  537. if (file) {
  538. /* File object ref couldn't be taken */
  539. if (!atomic_long_inc_not_zero(&file->f_count))
  540. file = NULL;
  541. }
  542. rcu_read_unlock();
  543. return file;
  544. }
  545. EXPORT_SYMBOL(fget_raw);
  546. /*
  547. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  548. *
  549. * You can use this instead of fget if you satisfy all of the following
  550. * conditions:
  551. * 1) You must call fput_light before exiting the syscall and returning control
  552. * to userspace (i.e. you cannot remember the returned struct file * after
  553. * returning to userspace).
  554. * 2) You must not call filp_close on the returned struct file * in between
  555. * calls to fget_light and fput_light.
  556. * 3) You must not clone the current task in between the calls to fget_light
  557. * and fput_light.
  558. *
  559. * The fput_needed flag returned by fget_light should be passed to the
  560. * corresponding fput_light.
  561. */
  562. struct file *fget_light(unsigned int fd, int *fput_needed)
  563. {
  564. struct file *file;
  565. struct files_struct *files = current->files;
  566. *fput_needed = 0;
  567. if (atomic_read(&files->count) == 1) {
  568. file = fcheck_files(files, fd);
  569. if (file && (file->f_mode & FMODE_PATH))
  570. file = NULL;
  571. } else {
  572. rcu_read_lock();
  573. file = fcheck_files(files, fd);
  574. if (file) {
  575. if (!(file->f_mode & FMODE_PATH) &&
  576. atomic_long_inc_not_zero(&file->f_count))
  577. *fput_needed = 1;
  578. else
  579. /* Didn't get the reference, someone's freed */
  580. file = NULL;
  581. }
  582. rcu_read_unlock();
  583. }
  584. return file;
  585. }
  586. EXPORT_SYMBOL(fget_light);
  587. struct file *fget_raw_light(unsigned int fd, int *fput_needed)
  588. {
  589. struct file *file;
  590. struct files_struct *files = current->files;
  591. *fput_needed = 0;
  592. if (atomic_read(&files->count) == 1) {
  593. file = fcheck_files(files, fd);
  594. } else {
  595. rcu_read_lock();
  596. file = fcheck_files(files, fd);
  597. if (file) {
  598. if (atomic_long_inc_not_zero(&file->f_count))
  599. *fput_needed = 1;
  600. else
  601. /* Didn't get the reference, someone's freed */
  602. file = NULL;
  603. }
  604. rcu_read_unlock();
  605. }
  606. return file;
  607. }