file.c 25 KB

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