VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100    1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    2) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    3)  *  linux/fs/file.c
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    4)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    5)  *  Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    6)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    7)  *  Manage the dynamic fd arrays in the process files_struct.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    8)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    9) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400   10) #include <linux/syscalls.h>
630d9c47274aa (Paul Gortmaker     2011-11-16 23:57:37 -0500   11) #include <linux/export.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   12) #include <linux/fs.h>
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200   13) #include <linux/kernel.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   14) #include <linux/mm.h>
3f07c0144132e (Ingo Molnar        2017-02-08 18:51:30 +0100   15) #include <linux/sched/signal.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   16) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   17) #include <linux/file.h>
9f3acc3140444 (Al Viro            2008-04-24 07:44:08 -0400   18) #include <linux/fdtable.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   19) #include <linux/bitops.h>
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   20) #include <linux/spinlock.h>
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   21) #include <linux/rcupdate.h>
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200   22) #include <linux/close_range.h>
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700   23) #include <net/sock.h>
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   24) 
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700   25) #include "internal.h"
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700   26) 
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300   27) unsigned int sysctl_nr_open __read_mostly = 1024*1024;
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300   28) unsigned int sysctl_nr_open_min = BITS_PER_LONG;
752343be63d90 (Rasmus Villemoes   2015-10-29 12:01:41 +0100   29) /* our min() is unusable in constant expressions ;-/ */
752343be63d90 (Rasmus Villemoes   2015-10-29 12:01:41 +0100   30) #define __const_min(x, y) ((x) < (y) ? (x) : (y))
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300   31) unsigned int sysctl_nr_open_max =
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300   32) 	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
9cfe015aa424b (Eric Dumazet       2008-02-06 01:37:16 -0800   33) 
a892e2d7dcdfa (Changli Gao        2010-08-10 18:01:35 -0700   34) static void __free_fdtable(struct fdtable *fdt)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   35) {
f6c0a1920e018 (Al Viro            2014-04-23 10:18:46 -0400   36) 	kvfree(fdt->fd);
f6c0a1920e018 (Al Viro            2014-04-23 10:18:46 -0400   37) 	kvfree(fdt->open_fds);
a892e2d7dcdfa (Changli Gao        2010-08-10 18:01:35 -0700   38) 	kfree(fdt);
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   39) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   40) 
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400   41) static void free_fdtable_rcu(struct rcu_head *rcu)
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   42) {
ac3e3c5b11643 (Al Viro            2013-04-28 21:42:33 -0400   43) 	__free_fdtable(container_of(rcu, struct fdtable, rcu));
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   44) }
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   45) 
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700   46) #define BITBIT_NR(nr)	BITS_TO_LONGS(BITS_TO_LONGS(nr))
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700   47) #define BITBIT_SIZE(nr)	(BITBIT_NR(nr) * sizeof(long))
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700   48) 
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   49) /*
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   50)  * Copy 'count' fd bits from the old table to the new table and clear the extra
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   51)  * space if any.  This does not copy the file pointers.  Called with the files
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   52)  * spinlock held for write.
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   53)  */
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   54) static void copy_fd_bitmaps(struct fdtable *nfdt, struct fdtable *ofdt,
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   55) 			    unsigned int count)
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   56) {
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   57) 	unsigned int cpy, set;
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   58) 
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   59) 	cpy = count / BITS_PER_BYTE;
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   60) 	set = (nfdt->max_fds - count) / BITS_PER_BYTE;
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   61) 	memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   62) 	memset((char *)nfdt->open_fds + cpy, 0, set);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   63) 	memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   64) 	memset((char *)nfdt->close_on_exec + cpy, 0, set);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   65) 
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   66) 	cpy = BITBIT_SIZE(count);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   67) 	set = BITBIT_SIZE(nfdt->max_fds) - cpy;
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   68) 	memcpy(nfdt->full_fds_bits, ofdt->full_fds_bits, cpy);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   69) 	memset((char *)nfdt->full_fds_bits + cpy, 0, set);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   70) }
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   71) 
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   72) /*
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   73)  * Copy all file descriptors from the old table to the new, expanded table and
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   74)  * clear the extra space.  Called with the files spinlock held for write.
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   75)  */
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   76) static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   77) {
4e89b7210403f (Al Viro            2020-05-19 17:48:52 -0400   78) 	size_t cpy, set;
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   79) 
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   80) 	BUG_ON(nfdt->max_fds < ofdt->max_fds);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   81) 
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   82) 	cpy = ofdt->max_fds * sizeof(struct file *);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   83) 	set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   84) 	memcpy(nfdt->fd, ofdt->fd, cpy);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   85) 	memset((char *)nfdt->fd + cpy, 0, set);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   86) 
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600   87) 	copy_fd_bitmaps(nfdt, ofdt, ofdt->max_fds);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   88) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   89) 
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   90) static struct fdtable * alloc_fdtable(unsigned int nr)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   91) {
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   92) 	struct fdtable *fdt;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000   93) 	void *data;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   94) 
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700   95) 	/*
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   96) 	 * Figure out how many fds we actually want to support in this fdtable.
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   97) 	 * Allocation steps are keyed to the size of the fdarray, since it
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   98) 	 * grows far faster than any of the other dynamic data. We try to fit
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800   99) 	 * the fdarray into comfortable page-tuned chunks: starting at 1024B
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  100) 	 * and growing in powers of two from there on.
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  101) 	 */
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  102) 	nr /= (1024 / sizeof(struct file *));
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  103) 	nr = roundup_pow_of_two(nr + 1);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  104) 	nr *= (1024 / sizeof(struct file *));
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  105) 	/*
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  106) 	 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  107) 	 * had been set lower between the check in expand_files() and here.  Deal
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  108) 	 * with that in caller, it's cheaper that way.
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  109) 	 *
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  110) 	 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  111) 	 * bitmaps handling below becomes unpleasant, to put it mildly...
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  112) 	 */
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  113) 	if (unlikely(nr > sysctl_nr_open))
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  114) 		nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
bbea9f69668a3 (Vadim Lobanov      2006-12-10 02:21:12 -0800  115) 
5d097056c9a01 (Vladimir Davydov   2016-01-14 15:18:21 -0800  116) 	fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL_ACCOUNT);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  117) 	if (!fdt)
bbea9f69668a3 (Vadim Lobanov      2006-12-10 02:21:12 -0800  118) 		goto out;
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  119) 	fdt->max_fds = nr;
c823bd9244337 (Michal Hocko       2017-07-06 15:36:19 -0700  120) 	data = kvmalloc_array(nr, sizeof(struct file *), GFP_KERNEL_ACCOUNT);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  121) 	if (!data)
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  122) 		goto out_fdt;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  123) 	fdt->fd = data;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  124) 
c823bd9244337 (Michal Hocko       2017-07-06 15:36:19 -0700  125) 	data = kvmalloc(max_t(size_t,
c823bd9244337 (Michal Hocko       2017-07-06 15:36:19 -0700  126) 				 2 * nr / BITS_PER_BYTE + BITBIT_SIZE(nr), L1_CACHE_BYTES),
c823bd9244337 (Michal Hocko       2017-07-06 15:36:19 -0700  127) 				 GFP_KERNEL_ACCOUNT);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  128) 	if (!data)
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  129) 		goto out_arr;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  130) 	fdt->open_fds = data;
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  131) 	data += nr / BITS_PER_BYTE;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  132) 	fdt->close_on_exec = data;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  133) 	data += nr / BITS_PER_BYTE;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  134) 	fdt->full_fds_bits = data;
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  135) 
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  136) 	return fdt;
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  137) 
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  138) out_arr:
f6c0a1920e018 (Al Viro            2014-04-23 10:18:46 -0400  139) 	kvfree(fdt->fd);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  140) out_fdt:
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  141) 	kfree(fdt);
5466b456ed674 (Vadim Lobanov      2006-12-10 02:21:22 -0800  142) out:
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  143) 	return NULL;
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  144) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  145) 
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  146) /*
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  147)  * Expand the file descriptor table.
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  148)  * This function will allocate a new fdtable and both fd array and fdset, of
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  149)  * the given size.
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  150)  * Return <0 error code on error; 1 on successful completion.
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  151)  * The files->file_lock should be held on entry, and will be held on exit.
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  152)  */
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  153) static int expand_fdtable(struct files_struct *files, unsigned int nr)
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  154) 	__releases(files->file_lock)
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  155) 	__acquires(files->file_lock)
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  156) {
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  157) 	struct fdtable *new_fdt, *cur_fdt;
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  158) 
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  159) 	spin_unlock(&files->file_lock);
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  160) 	new_fdt = alloc_fdtable(nr);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  161) 
d74ba04d919eb (Eric W. Biederman  2020-11-20 17:14:35 -0600  162) 	/* make sure all fd_install() have seen resize_in_progress
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  163) 	 * or have finished their rcu_read_lock_sched() section.
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  164) 	 */
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  165) 	if (atomic_read(&files->count) > 1)
c93ffc15cceb0 (Paul E. McKenney   2018-11-05 17:31:31 -0800  166) 		synchronize_rcu();
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  167) 
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  168) 	spin_lock(&files->file_lock);
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  169) 	if (!new_fdt)
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  170) 		return -ENOMEM;
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  171) 	/*
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  172) 	 * extremely unlikely race - sysctl_nr_open decreased between the check in
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  173) 	 * caller and alloc_fdtable().  Cheaper to catch it here...
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  174) 	 */
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  175) 	if (unlikely(new_fdt->max_fds <= nr)) {
a892e2d7dcdfa (Changli Gao        2010-08-10 18:01:35 -0700  176) 		__free_fdtable(new_fdt);
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  177) 		return -EMFILE;
5c598b3428c37 (Al Viro            2008-04-27 20:04:15 -0400  178) 	}
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  179) 	cur_fdt = files_fdtable(files);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  180) 	BUG_ON(nr < cur_fdt->max_fds);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  181) 	copy_fdtable(new_fdt, cur_fdt);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  182) 	rcu_assign_pointer(files->fdt, new_fdt);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  183) 	if (cur_fdt != &files->fdtab)
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  184) 		call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
d74ba04d919eb (Eric W. Biederman  2020-11-20 17:14:35 -0600  185) 	/* coupled with smp_rmb() in fd_install() */
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  186) 	smp_wmb();
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  187) 	return 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  188) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  189) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  190) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  191)  * Expand files.
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  192)  * This function will expand the file structures, if the requested size exceeds
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  193)  * the current capacity and there is room for expansion.
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  194)  * Return <0 error code on error; 0 when nothing done; 1 when files were
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  195)  * expanded and execution may have blocked.
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  196)  * The files->file_lock should be held on entry, and will be held on exit.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  197)  */
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  198) static int expand_files(struct files_struct *files, unsigned int nr)
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  199) 	__releases(files->file_lock)
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  200) 	__acquires(files->file_lock)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  201) {
badf16621c1f9 (Dipankar Sarma     2005-09-09 13:04:10 -0700  202) 	struct fdtable *fdt;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  203) 	int expanded = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  204) 
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  205) repeat:
badf16621c1f9 (Dipankar Sarma     2005-09-09 13:04:10 -0700  206) 	fdt = files_fdtable(files);
4e1e018ecc6f7 (Al Viro            2008-07-26 16:01:20 -0400  207) 
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  208) 	/* Do we need to expand? */
bbea9f69668a3 (Vadim Lobanov      2006-12-10 02:21:12 -0800  209) 	if (nr < fdt->max_fds)
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  210) 		return expanded;
4e1e018ecc6f7 (Al Viro            2008-07-26 16:01:20 -0400  211) 
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  212) 	/* Can we expand? */
9cfe015aa424b (Eric Dumazet       2008-02-06 01:37:16 -0800  213) 	if (nr >= sysctl_nr_open)
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  214) 		return -EMFILE;
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  215) 
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  216) 	if (unlikely(files->resize_in_progress)) {
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  217) 		spin_unlock(&files->file_lock);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  218) 		expanded = 1;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  219) 		wait_event(files->resize_wait, !files->resize_in_progress);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  220) 		spin_lock(&files->file_lock);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  221) 		goto repeat;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  222) 	}
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  223) 
74d392aaabfc8 (Vadim Lobanov      2006-09-29 02:01:43 -0700  224) 	/* All good, so we try */
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  225) 	files->resize_in_progress = true;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  226) 	expanded = expand_fdtable(files, nr);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  227) 	files->resize_in_progress = false;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  228) 
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  229) 	wake_up_all(&files->resize_wait);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  230) 	return expanded;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  231) }
ab2af1f500506 (Dipankar Sarma     2005-09-09 13:04:13 -0700  232) 
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  233) static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt)
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  234) {
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  235) 	__set_bit(fd, fdt->close_on_exec);
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  236) }
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  237) 
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  238) static inline void __clear_close_on_exec(unsigned int fd, struct fdtable *fdt)
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  239) {
fc90888d07b8e (Linus Torvalds     2015-10-31 16:06:40 -0700  240) 	if (test_bit(fd, fdt->close_on_exec))
fc90888d07b8e (Linus Torvalds     2015-10-31 16:06:40 -0700  241) 		__clear_bit(fd, fdt->close_on_exec);
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  242) }
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  243) 
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  244) static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt)
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  245) {
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  246) 	__set_bit(fd, fdt->open_fds);
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  247) 	fd /= BITS_PER_LONG;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  248) 	if (!~fdt->open_fds[fd])
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  249) 		__set_bit(fd, fdt->full_fds_bits);
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  250) }
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  251) 
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  252) static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt)
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  253) {
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  254) 	__clear_bit(fd, fdt->open_fds);
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  255) 	__clear_bit(fd / BITS_PER_LONG, fdt->full_fds_bits);
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  256) }
b8318b01a8f7f (Al Viro            2012-08-21 20:09:42 -0400  257) 
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  258) static unsigned int count_open_files(struct fdtable *fdt)
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  259) {
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  260) 	unsigned int size = fdt->max_fds;
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  261) 	unsigned int i;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  262) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  263) 	/* Find the last open fd */
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  264) 	for (i = size / BITS_PER_LONG; i > 0; ) {
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  265) 		if (fdt->open_fds[--i])
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  266) 			break;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  267) 	}
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  268) 	i = (i + 1) * BITS_PER_LONG;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  269) 	return i;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  270) }
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  271) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  272) static unsigned int sane_fdtable_size(struct fdtable *fdt, unsigned int max_fds)
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  273) {
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  274) 	unsigned int count;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  275) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  276) 	count = count_open_files(fdt);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  277) 	if (max_fds < NR_OPEN_DEFAULT)
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  278) 		max_fds = NR_OPEN_DEFAULT;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  279) 	return min(count, max_fds);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  280) }
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  281) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  282) /*
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  283)  * Allocate a new files structure and copy contents from the
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  284)  * passed in files structure.
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  285)  * errorp will be valid only when the returned files_struct is NULL.
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  286)  */
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  287) struct files_struct *dup_fd(struct files_struct *oldf, unsigned int max_fds, int *errorp)
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  288) {
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  289) 	struct files_struct *newf;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  290) 	struct file **old_fds, **new_fds;
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  291) 	unsigned int open_files, i;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  292) 	struct fdtable *old_fdt, *new_fdt;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  293) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  294) 	*errorp = -ENOMEM;
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  295) 	newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  296) 	if (!newf)
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  297) 		goto out;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  298) 
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  299) 	atomic_set(&newf->count, 1);
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  300) 
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  301) 	spin_lock_init(&newf->file_lock);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  302) 	newf->resize_in_progress = false;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  303) 	init_waitqueue_head(&newf->resize_wait);
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  304) 	newf->next_fd = 0;
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  305) 	new_fdt = &newf->fdtab;
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  306) 	new_fdt->max_fds = NR_OPEN_DEFAULT;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  307) 	new_fdt->close_on_exec = newf->close_on_exec_init;
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  308) 	new_fdt->open_fds = newf->open_fds_init;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  309) 	new_fdt->full_fds_bits = newf->full_fds_bits_init;
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  310) 	new_fdt->fd = &newf->fd_array[0];
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  311) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  312) 	spin_lock(&oldf->file_lock);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  313) 	old_fdt = files_fdtable(oldf);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  314) 	open_files = sane_fdtable_size(old_fdt, max_fds);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  315) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  316) 	/*
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  317) 	 * Check whether we need to allocate a larger fd array and fd set.
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  318) 	 */
adbecb128cd2c (Al Viro            2008-05-08 21:19:42 -0400  319) 	while (unlikely(open_files > new_fdt->max_fds)) {
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  320) 		spin_unlock(&oldf->file_lock);
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  321) 
a892e2d7dcdfa (Changli Gao        2010-08-10 18:01:35 -0700  322) 		if (new_fdt != &newf->fdtab)
a892e2d7dcdfa (Changli Gao        2010-08-10 18:01:35 -0700  323) 			__free_fdtable(new_fdt);
adbecb128cd2c (Al Viro            2008-05-08 21:19:42 -0400  324) 
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  325) 		new_fdt = alloc_fdtable(open_files - 1);
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  326) 		if (!new_fdt) {
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  327) 			*errorp = -ENOMEM;
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  328) 			goto out_release;
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  329) 		}
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  330) 
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  331) 		/* beyond sysctl_nr_open; nothing to do */
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  332) 		if (unlikely(new_fdt->max_fds < open_files)) {
a892e2d7dcdfa (Changli Gao        2010-08-10 18:01:35 -0700  333) 			__free_fdtable(new_fdt);
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  334) 			*errorp = -EMFILE;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  335) 			goto out_release;
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  336) 		}
9dec3c4d306b0 (Al Viro            2008-05-08 21:02:45 -0400  337) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  338) 		/*
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  339) 		 * Reacquire the oldf lock and a pointer to its fd table
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  340) 		 * who knows it may have a new bigger fd table. We need
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  341) 		 * the latest pointer.
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  342) 		 */
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  343) 		spin_lock(&oldf->file_lock);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  344) 		old_fdt = files_fdtable(oldf);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  345) 		open_files = sane_fdtable_size(old_fdt, max_fds);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  346) 	}
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  347) 
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600  348) 	copy_fd_bitmaps(new_fdt, old_fdt, open_files);
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600  349) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  350) 	old_fds = old_fdt->fd;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  351) 	new_fds = new_fdt->fd;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  352) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  353) 	for (i = open_files; i != 0; i--) {
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  354) 		struct file *f = *old_fds++;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  355) 		if (f) {
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  356) 			get_file(f);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  357) 		} else {
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  358) 			/*
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  359) 			 * The fd may be claimed in the fd bitmap but not yet
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  360) 			 * instantiated in the files array if a sibling thread
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  361) 			 * is partway through open().  So make sure that this
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  362) 			 * fd is available to the new process.
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  363) 			 */
1dce27c5aa677 (David Howells      2012-02-16 17:49:42 +0000  364) 			__clear_open_fd(open_files - i, new_fdt);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  365) 		}
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  366) 		rcu_assign_pointer(*new_fds++, f);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  367) 	}
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  368) 	spin_unlock(&oldf->file_lock);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  369) 
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600  370) 	/* clear the remainder */
ea5c58e70c3a1 (Eric Biggers       2015-11-06 00:32:04 -0600  371) 	memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  372) 
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  373) 	rcu_assign_pointer(newf->fdt, new_fdt);
afbec7fff4928 (Al Viro            2008-05-08 21:11:17 -0400  374) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  375) 	return newf;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  376) 
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  377) out_release:
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  378) 	kmem_cache_free(files_cachep, newf);
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  379) out:
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  380) 	return NULL;
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  381) }
02afc6267f6d5 (Al Viro            2008-05-08 19:42:56 -0400  382) 
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  383) static struct fdtable *close_files(struct files_struct * files)
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  384) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  385) 	/*
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  386) 	 * It is safe to dereference the fd table without RCU or
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  387) 	 * ->file_lock because this is the last reference to the
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  388) 	 * files structure.
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  389) 	 */
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  390) 	struct fdtable *fdt = rcu_dereference_raw(files->fdt);
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  391) 	unsigned int i, j = 0;
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  392) 
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  393) 	for (;;) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  394) 		unsigned long set;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  395) 		i = j * BITS_PER_LONG;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  396) 		if (i >= fdt->max_fds)
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  397) 			break;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  398) 		set = fdt->open_fds[j++];
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  399) 		while (set) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  400) 			if (set & 1) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  401) 				struct file * file = xchg(&fdt->fd[i], NULL);
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  402) 				if (file) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  403) 					filp_close(file, files);
388a4c88064e7 (Paul E. McKenney   2017-10-24 08:39:34 -0700  404) 					cond_resched();
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  405) 				}
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  406) 			}
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  407) 			i++;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  408) 			set >>= 1;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  409) 		}
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  410) 	}
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  411) 
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  412) 	return fdt;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  413) }
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  414) 
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  415) void put_files_struct(struct files_struct *files)
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  416) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  417) 	if (atomic_dec_and_test(&files->count)) {
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  418) 		struct fdtable *fdt = close_files(files);
ce08b62d18b3f (Oleg Nesterov      2014-01-11 19:19:53 +0100  419) 
b9e02af0ae078 (Al Viro            2012-08-15 20:00:58 -0400  420) 		/* free the arrays if they are not embedded */
b9e02af0ae078 (Al Viro            2012-08-15 20:00:58 -0400  421) 		if (fdt != &files->fdtab)
b9e02af0ae078 (Al Viro            2012-08-15 20:00:58 -0400  422) 			__free_fdtable(fdt);
b9e02af0ae078 (Al Viro            2012-08-15 20:00:58 -0400  423) 		kmem_cache_free(files_cachep, files);
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  424) 	}
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  425) }
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  426) 
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  427) void exit_files(struct task_struct *tsk)
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  428) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  429) 	struct files_struct * files = tsk->files;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  430) 
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  431) 	if (files) {
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  432) 		task_lock(tsk);
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  433) 		tsk->files = NULL;
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  434) 		task_unlock(tsk);
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  435) 		put_files_struct(files);
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  436) 	}
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  437) }
7cf4dc3c8dbfd (Al Viro            2012-08-15 19:56:12 -0400  438) 
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  439) struct files_struct init_files = {
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  440) 	.count		= ATOMIC_INIT(1),
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  441) 	.fdt		= &init_files.fdtab,
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  442) 	.fdtab		= {
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  443) 		.max_fds	= NR_OPEN_DEFAULT,
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  444) 		.fd		= &init_files.fd_array[0],
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  445) 		.close_on_exec	= init_files.close_on_exec_init,
1fd36adcd98c1 (David Howells      2012-02-16 17:49:54 +0000  446) 		.open_fds	= init_files.open_fds_init,
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  447) 		.full_fds_bits	= init_files.full_fds_bits_init,
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  448) 	},
eece09ec213e9 (Thomas Gleixner    2011-07-17 21:25:03 +0200  449) 	.file_lock	= __SPIN_LOCK_UNLOCKED(init_files.file_lock),
5704a06810682 (Shuriyc Chu        2019-03-05 15:41:56 -0800  450) 	.resize_wait	= __WAIT_QUEUE_HEAD_INITIALIZER(init_files.resize_wait),
f52111b154694 (Al Viro            2008-05-08 18:19:16 -0400  451) };
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  452) 
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  453) static unsigned int find_next_fd(struct fdtable *fdt, unsigned int start)
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  454) {
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  455) 	unsigned int maxfd = fdt->max_fds;
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  456) 	unsigned int maxbit = maxfd / BITS_PER_LONG;
9b80a184eaadc (Alexey Dobriyan    2016-09-02 00:38:52 +0300  457) 	unsigned int bitbit = start / BITS_PER_LONG;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  458) 
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  459) 	bitbit = find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  460) 	if (bitbit > maxfd)
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  461) 		return maxfd;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  462) 	if (bitbit > start)
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  463) 		start = bitbit;
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  464) 	return find_next_zero_bit(fdt->open_fds, maxfd, start);
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  465) }
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  466) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  467) /*
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  468)  * allocate a file descriptor, mark it busy.
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  469)  */
aa384d10f3d06 (Eric W. Biederman  2020-11-20 17:14:37 -0600  470) static int alloc_fd(unsigned start, unsigned end, unsigned flags)
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  471) {
aa384d10f3d06 (Eric W. Biederman  2020-11-20 17:14:37 -0600  472) 	struct files_struct *files = current->files;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  473) 	unsigned int fd;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  474) 	int error;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  475) 	struct fdtable *fdt;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  476) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  477) 	spin_lock(&files->file_lock);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  478) repeat:
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  479) 	fdt = files_fdtable(files);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  480) 	fd = start;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  481) 	if (fd < files->next_fd)
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  482) 		fd = files->next_fd;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  483) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  484) 	if (fd < fdt->max_fds)
f3f86e33dc3da (Linus Torvalds     2015-10-30 16:53:57 -0700  485) 		fd = find_next_fd(fdt, fd);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  486) 
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  487) 	/*
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  488) 	 * N.B. For clone tasks sharing a files structure, this test
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  489) 	 * will limit the total number of files that can be opened.
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  490) 	 */
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  491) 	error = -EMFILE;
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  492) 	if (fd >= end)
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  493) 		goto out;
f33ff9927f420 (Al Viro            2012-08-12 16:17:59 -0400  494) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  495) 	error = expand_files(files, fd);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  496) 	if (error < 0)
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  497) 		goto out;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  498) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  499) 	/*
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  500) 	 * If we needed to expand the fs array we
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  501) 	 * might have blocked - try again.
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  502) 	 */
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  503) 	if (error)
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  504) 		goto repeat;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  505) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  506) 	if (start <= files->next_fd)
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  507) 		files->next_fd = fd + 1;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  508) 
1dce27c5aa677 (David Howells      2012-02-16 17:49:42 +0000  509) 	__set_open_fd(fd, fdt);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  510) 	if (flags & O_CLOEXEC)
1dce27c5aa677 (David Howells      2012-02-16 17:49:42 +0000  511) 		__set_close_on_exec(fd, fdt);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  512) 	else
1dce27c5aa677 (David Howells      2012-02-16 17:49:42 +0000  513) 		__clear_close_on_exec(fd, fdt);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  514) 	error = fd;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  515) #if 1
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  516) 	/* Sanity check */
add1f09954543 (Paul E. McKenney   2014-02-12 12:51:09 -0800  517) 	if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  518) 		printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  519) 		rcu_assign_pointer(fdt->fd[fd], NULL);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  520) 	}
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  521) #endif
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  522) 
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  523) out:
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  524) 	spin_unlock(&files->file_lock);
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  525) 	return error;
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  526) }
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  527) 
4022e7af86be2 (Jens Axboe         2020-03-19 19:23:18 -0600  528) int __get_unused_fd_flags(unsigned flags, unsigned long nofile)
4022e7af86be2 (Jens Axboe         2020-03-19 19:23:18 -0600  529) {
aa384d10f3d06 (Eric W. Biederman  2020-11-20 17:14:37 -0600  530) 	return alloc_fd(0, nofile, flags);
4022e7af86be2 (Jens Axboe         2020-03-19 19:23:18 -0600  531) }
4022e7af86be2 (Jens Axboe         2020-03-19 19:23:18 -0600  532) 
1a7bd2265fc57 (Al Viro            2012-08-12 17:18:05 -0400  533) int get_unused_fd_flags(unsigned flags)
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  534) {
4022e7af86be2 (Jens Axboe         2020-03-19 19:23:18 -0600  535) 	return __get_unused_fd_flags(flags, rlimit(RLIMIT_NOFILE));
1027abe8827b4 (Al Viro            2008-07-30 04:13:04 -0400  536) }
1a7bd2265fc57 (Al Viro            2012-08-12 17:18:05 -0400  537) EXPORT_SYMBOL(get_unused_fd_flags);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  538) 
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  539) static void __put_unused_fd(struct files_struct *files, unsigned int fd)
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  540) {
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  541) 	struct fdtable *fdt = files_fdtable(files);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  542) 	__clear_open_fd(fd, fdt);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  543) 	if (fd < files->next_fd)
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  544) 		files->next_fd = fd;
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  545) }
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  546) 
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  547) void put_unused_fd(unsigned int fd)
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  548) {
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  549) 	struct files_struct *files = current->files;
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  550) 	spin_lock(&files->file_lock);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  551) 	__put_unused_fd(files, fd);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  552) 	spin_unlock(&files->file_lock);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  553) }
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  554) 
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  555) EXPORT_SYMBOL(put_unused_fd);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  556) 
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  557) /*
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  558)  * Install a file pointer in the fd array.
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  559)  *
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  560)  * The VFS is full of places where we drop the files lock between
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  561)  * setting the open_fds bitmap and installing the file in the file
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  562)  * array.  At any such point, we are vulnerable to a dup2() race
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  563)  * installing a file in the array before us.  We need to detect this and
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  564)  * fput() the struct file we are about to overwrite in this case.
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  565)  *
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  566)  * It should never happen - if we allow dup2() do it, _really_ bad things
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  567)  * will follow.
f869e8a7f753e (Al Viro            2012-08-15 21:06:33 -0400  568)  *
d74ba04d919eb (Eric W. Biederman  2020-11-20 17:14:35 -0600  569)  * This consumes the "file" refcount, so callers should treat it
d74ba04d919eb (Eric W. Biederman  2020-11-20 17:14:35 -0600  570)  * as if they had called fput(file).
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  571)  */
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  572) 
d74ba04d919eb (Eric W. Biederman  2020-11-20 17:14:35 -0600  573) void fd_install(unsigned int fd, struct file *file)
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  574) {
d74ba04d919eb (Eric W. Biederman  2020-11-20 17:14:35 -0600  575) 	struct files_struct *files = current->files;
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  576) 	struct fdtable *fdt;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  577) 
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  578) 	rcu_read_lock_sched();
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  579) 
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  580) 	if (unlikely(files->resize_in_progress)) {
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  581) 		rcu_read_unlock_sched();
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  582) 		spin_lock(&files->file_lock);
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  583) 		fdt = files_fdtable(files);
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  584) 		BUG_ON(fdt->fd[fd] != NULL);
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  585) 		rcu_assign_pointer(fdt->fd[fd], file);
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  586) 		spin_unlock(&files->file_lock);
c02b1a9b41c2e (Mateusz Guzik      2017-10-03 12:58:15 +0200  587) 		return;
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  588) 	}
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  589) 	/* coupled with smp_wmb() in expand_fdtable() */
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  590) 	smp_rmb();
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  591) 	fdt = rcu_dereference_sched(files->fdt);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  592) 	BUG_ON(fdt->fd[fd] != NULL);
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  593) 	rcu_assign_pointer(fdt->fd[fd], file);
8a81252b774b5 (Eric Dumazet       2015-06-30 15:54:08 +0200  594) 	rcu_read_unlock_sched();
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  595) }
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  596) 
56007cae94f34 (Al Viro            2012-08-15 21:03:26 -0400  597) EXPORT_SYMBOL(fd_install);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  598) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  599) static struct file *pick_file(struct files_struct *files, unsigned fd)
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  600) {
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  601) 	struct file *file = NULL;
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  602) 	struct fdtable *fdt;
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  603) 
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  604) 	spin_lock(&files->file_lock);
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  605) 	fdt = files_fdtable(files);
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  606) 	if (fd >= fdt->max_fds)
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  607) 		goto out_unlock;
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  608) 	file = fdt->fd[fd];
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  609) 	if (!file)
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  610) 		goto out_unlock;
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  611) 	rcu_assign_pointer(fdt->fd[fd], NULL);
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  612) 	__put_unused_fd(files, fd);
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  613) 
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  614) out_unlock:
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  615) 	spin_unlock(&files->file_lock);
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  616) 	return file;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  617) }
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  618) 
8760c909f54a8 (Eric W. Biederman  2020-11-20 17:14:38 -0600  619) int close_fd(unsigned fd)
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  620) {
8760c909f54a8 (Eric W. Biederman  2020-11-20 17:14:38 -0600  621) 	struct files_struct *files = current->files;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  622) 	struct file *file;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  623) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  624) 	file = pick_file(files, fd);
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  625) 	if (!file)
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  626) 		return -EBADF;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  627) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  628) 	return filp_close(file, files);
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  629) }
8760c909f54a8 (Eric W. Biederman  2020-11-20 17:14:38 -0600  630) EXPORT_SYMBOL(close_fd); /* for ksys_close() */
483ce1d4b8c3b (Al Viro            2012-08-19 12:04:24 -0400  631) 
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  632) /**
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  633)  * last_fd - return last valid index into fd table
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  634)  * @cur_fds: files struct
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  635)  *
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  636)  * Context: Either rcu read lock or files_lock must be held.
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  637)  *
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  638)  * Returns: Last valid index into fdtable.
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  639)  */
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  640) static inline unsigned last_fd(struct fdtable *fdt)
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  641) {
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  642) 	return fdt->max_fds - 1;
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  643) }
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  644) 
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  645) static inline void __range_cloexec(struct files_struct *cur_fds,
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  646) 				   unsigned int fd, unsigned int max_fd)
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  647) {
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  648) 	struct fdtable *fdt;
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  649) 
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  650) 	/* make sure we're using the correct maximum value */
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  651) 	spin_lock(&cur_fds->file_lock);
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  652) 	fdt = files_fdtable(cur_fds);
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  653) 	max_fd = min(last_fd(fdt), max_fd);
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  654) 	if (fd <= max_fd)
9b5b872215fe6 (Christian Brauner  2021-04-02 10:29:36 +0200  655) 		bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1);
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  656) 	spin_unlock(&cur_fds->file_lock);
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  657) }
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  658) 
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  659) static inline void __range_close(struct files_struct *cur_fds, unsigned int fd,
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  660) 				 unsigned int max_fd)
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  661) {
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  662) 	while (fd <= max_fd) {
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  663) 		struct file *file;
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  664) 
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  665) 		file = pick_file(cur_fds, fd++);
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  666) 		if (!file)
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  667) 			continue;
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  668) 
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  669) 		filp_close(file, cur_fds);
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  670) 		cond_resched();
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  671) 	}
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  672) }
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  673) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  674) /**
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  675)  * __close_range() - Close all file descriptors in a given range.
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  676)  *
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  677)  * @fd:     starting file descriptor to close
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  678)  * @max_fd: last file descriptor to close
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  679)  *
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  680)  * This closes a range of file descriptors. All file descriptors
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  681)  * from @fd up to and including @max_fd are closed.
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  682)  */
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  683) int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  684) {
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  685) 	unsigned int cur_max;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  686) 	struct task_struct *me = current;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  687) 	struct files_struct *cur_fds = me->files, *fds = NULL;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  688) 
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  689) 	if (flags & ~(CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC))
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  690) 		return -EINVAL;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  691) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  692) 	if (fd > max_fd)
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  693) 		return -EINVAL;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  694) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  695) 	rcu_read_lock();
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  696) 	cur_max = files_fdtable(cur_fds)->max_fds;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  697) 	rcu_read_unlock();
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  698) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  699) 	/* cap to last valid index into fdtable */
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  700) 	cur_max--;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  701) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  702) 	if (flags & CLOSE_RANGE_UNSHARE) {
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  703) 		int ret;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  704) 		unsigned int max_unshare_fds = NR_OPEN_MAX;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  705) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  706) 		/*
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  707) 		 * If the requested range is greater than the current maximum,
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  708) 		 * we're closing everything so only copy all file descriptors
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  709) 		 * beneath the lowest file descriptor.
fec8a6a691033 (Christian Brauner  2020-12-17 22:33:03 +0100  710) 		 * If the caller requested all fds to be made cloexec copy all
fec8a6a691033 (Christian Brauner  2020-12-17 22:33:03 +0100  711) 		 * of the file descriptors since they still want to use them.
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  712) 		 */
fec8a6a691033 (Christian Brauner  2020-12-17 22:33:03 +0100  713) 		if (!(flags & CLOSE_RANGE_CLOEXEC) && (max_fd >= cur_max))
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  714) 			max_unshare_fds = fd;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  715) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  716) 		ret = unshare_fd(CLONE_FILES, max_unshare_fds, &fds);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  717) 		if (ret)
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  718) 			return ret;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  719) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  720) 		/*
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  721) 		 * We used to share our file descriptor table, and have now
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  722) 		 * created a private one, make sure we're using it below.
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  723) 		 */
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  724) 		if (fds)
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  725) 			swap(cur_fds, fds);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  726) 	}
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  727) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  728) 	max_fd = min(max_fd, cur_max);
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  729) 
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  730) 	if (flags & CLOSE_RANGE_CLOEXEC)
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  731) 		__range_cloexec(cur_fds, fd, max_fd);
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  732) 	else
582f1fb6b721f (Giuseppe Scrivano  2020-11-18 11:47:45 +0100  733) 		__range_close(cur_fds, fd, max_fd);
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  734) 
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  735) 	if (fds) {
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  736) 		/*
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  737) 		 * We're done closing the files we were supposed to. Time to install
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  738) 		 * the new file descriptor table and drop the old one.
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  739) 		 */
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  740) 		task_lock(me);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  741) 		me->files = cur_fds;
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  742) 		task_unlock(me);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  743) 		put_files_struct(fds);
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  744) 	}
60997c3d45d9a (Christian Brauner  2020-06-03 21:48:55 +0200  745) 
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  746) 	return 0;
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  747) }
278a5fbaed89d (Christian Brauner  2019-05-24 11:30:34 +0200  748) 
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  749) /*
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  750)  * See close_fd_get_file() below, this variant assumes current->files->file_lock
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  751)  * is held.
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  752)  */
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  753) int __close_fd_get_file(unsigned int fd, struct file **res)
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  754) {
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  755) 	struct files_struct *files = current->files;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  756) 	struct file *file;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  757) 	struct fdtable *fdt;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  758) 
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  759) 	fdt = files_fdtable(files);
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  760) 	if (fd >= fdt->max_fds)
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  761) 		goto out_err;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  762) 	file = fdt->fd[fd];
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  763) 	if (!file)
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  764) 		goto out_err;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  765) 	rcu_assign_pointer(fdt->fd[fd], NULL);
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  766) 	__put_unused_fd(files, fd);
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  767) 	get_file(file);
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  768) 	*res = file;
6e802a4ba056a (Jens Axboe         2019-12-11 14:10:35 -0700  769) 	return 0;
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  770) out_err:
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  771) 	*res = NULL;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  772) 	return -ENOENT;
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  773) }
80cd795630d65 (Todd Kjos          2018-12-14 15:58:21 -0800  774) 
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  775) /*
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  776)  * variant of close_fd that gets a ref on the file for later fput.
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  777)  * The caller must ensure that filp_close() called on the file, and then
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  778)  * an fput().
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  779)  */
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  780) int close_fd_get_file(unsigned int fd, struct file **res)
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  781) {
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  782) 	struct files_struct *files = current->files;
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  783) 	int ret;
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  784) 
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  785) 	spin_lock(&files->file_lock);
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  786) 	ret = __close_fd_get_file(fd, res);
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  787) 	spin_unlock(&files->file_lock);
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  788) 
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  789) 	return ret;
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  790) }
53dec2ea74f2e (Jens Axboe         2021-01-19 15:41:52 -0700  791) 
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  792) void do_close_on_exec(struct files_struct *files)
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  793) {
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  794) 	unsigned i;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  795) 	struct fdtable *fdt;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  796) 
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  797) 	/* exec unshares first */
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  798) 	spin_lock(&files->file_lock);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  799) 	for (i = 0; ; i++) {
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  800) 		unsigned long set;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  801) 		unsigned fd = i * BITS_PER_LONG;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  802) 		fdt = files_fdtable(files);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  803) 		if (fd >= fdt->max_fds)
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  804) 			break;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  805) 		set = fdt->close_on_exec[i];
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  806) 		if (!set)
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  807) 			continue;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  808) 		fdt->close_on_exec[i] = 0;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  809) 		for ( ; set ; fd++, set >>= 1) {
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  810) 			struct file *file;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  811) 			if (!(set & 1))
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  812) 				continue;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  813) 			file = fdt->fd[fd];
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  814) 			if (!file)
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  815) 				continue;
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  816) 			rcu_assign_pointer(fdt->fd[fd], NULL);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  817) 			__put_unused_fd(files, fd);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  818) 			spin_unlock(&files->file_lock);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  819) 			filp_close(file, files);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  820) 			cond_resched();
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  821) 			spin_lock(&files->file_lock);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  822) 		}
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  823) 
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  824) 	}
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  825) 	spin_unlock(&files->file_lock);
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  826) }
6a6d27de340c8 (Al Viro            2012-08-21 09:56:33 -0400  827) 
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  828) static struct file *__fget_files(struct files_struct *files, unsigned int fd,
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  829) 				 fmode_t mask, unsigned int refs)
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  830) {
1deb46e256256 (Oleg Nesterov      2014-01-13 16:48:19 +0100  831) 	struct file *file;
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  832) 
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  833) 	rcu_read_lock();
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  834) loop:
f36c294327419 (Eric W. Biederman  2020-11-20 17:14:26 -0600  835) 	file = files_lookup_fd_rcu(files, fd);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  836) 	if (file) {
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  837) 		/* File object ref couldn't be taken.
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  838) 		 * dup2() atomicity guarantee is the reason
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  839) 		 * we loop to catch the new file (or NULL pointer)
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  840) 		 */
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  841) 		if (file->f_mode & mask)
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  842) 			file = NULL;
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  843) 		else if (!get_file_rcu_many(file, refs))
5ba97d2832f87 (Eric Dumazet       2015-06-29 17:10:30 +0200  844) 			goto loop;
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  845) 	}
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  846) 	rcu_read_unlock();
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  847) 
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  848) 	return file;
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  849) }
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  850) 
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  851) static inline struct file *__fget(unsigned int fd, fmode_t mask,
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  852) 				  unsigned int refs)
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  853) {
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  854) 	return __fget_files(current->files, fd, mask, refs);
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  855) }
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  856) 
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  857) struct file *fget_many(unsigned int fd, unsigned int refs)
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  858) {
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  859) 	return __fget(fd, FMODE_PATH, refs);
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  860) }
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  861) 
1deb46e256256 (Oleg Nesterov      2014-01-13 16:48:19 +0100  862) struct file *fget(unsigned int fd)
1deb46e256256 (Oleg Nesterov      2014-01-13 16:48:19 +0100  863) {
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  864) 	return __fget(fd, FMODE_PATH, 1);
1deb46e256256 (Oleg Nesterov      2014-01-13 16:48:19 +0100  865) }
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  866) EXPORT_SYMBOL(fget);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  867) 
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  868) struct file *fget_raw(unsigned int fd)
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  869) {
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  870) 	return __fget(fd, 0, 1);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  871) }
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  872) EXPORT_SYMBOL(fget_raw);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  873) 
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  874) struct file *fget_task(struct task_struct *task, unsigned int fd)
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  875) {
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  876) 	struct file *file = NULL;
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  877) 
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  878) 	task_lock(task);
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  879) 	if (task->files)
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  880) 		file = __fget_files(task->files, fd, 0, 1);
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  881) 	task_unlock(task);
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  882) 
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  883) 	return file;
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  884) }
5e876fb43dbf2 (Sargun Dhillon     2020-01-07 09:59:24 -0800  885) 
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  886) struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd)
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  887) {
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  888) 	/* Must be called with rcu_read_lock held */
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  889) 	struct files_struct *files;
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  890) 	struct file *file = NULL;
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  891) 
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  892) 	task_lock(task);
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  893) 	files = task->files;
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  894) 	if (files)
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  895) 		file = files_lookup_fd_rcu(files, fd);
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  896) 	task_unlock(task);
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  897) 
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  898) 	return file;
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  899) }
3a879fb380821 (Eric W. Biederman  2020-11-20 17:14:28 -0600  900) 
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  901) struct file *task_lookup_next_fd_rcu(struct task_struct *task, unsigned int *ret_fd)
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  902) {
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  903) 	/* Must be called with rcu_read_lock held */
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  904) 	struct files_struct *files;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  905) 	unsigned int fd = *ret_fd;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  906) 	struct file *file = NULL;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  907) 
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  908) 	task_lock(task);
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  909) 	files = task->files;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  910) 	if (files) {
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  911) 		for (; fd < files_fdtable(files)->max_fds; fd++) {
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  912) 			file = files_lookup_fd_rcu(files, fd);
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  913) 			if (file)
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  914) 				break;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  915) 		}
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  916) 	}
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  917) 	task_unlock(task);
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  918) 	*ret_fd = fd;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  919) 	return file;
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  920) }
e9a53aeb5e0a8 (Eric W. Biederman  2020-11-20 17:14:31 -0600  921) 
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  922) /*
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  923)  * Lightweight file lookup - no refcnt increment if fd table isn't shared.
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  924)  *
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  925)  * You can use this instead of fget if you satisfy all of the following
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  926)  * conditions:
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  927)  * 1) You must call fput_light before exiting the syscall and returning control
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  928)  *    to userspace (i.e. you cannot remember the returned struct file * after
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  929)  *    returning to userspace).
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  930)  * 2) You must not call filp_close on the returned struct file * in between
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  931)  *    calls to fget_light and fput_light.
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  932)  * 3) You must not clone the current task in between the calls to fget_light
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  933)  *    and fput_light.
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  934)  *
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  935)  * The fput_needed flag returned by fget_light should be passed to the
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  936)  * corresponding fput_light.
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  937)  */
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  938) static unsigned long __fget_light(unsigned int fd, fmode_t mask)
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  939) {
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  940) 	struct files_struct *files = current->files;
ad46183445043 (Oleg Nesterov      2014-01-13 16:48:40 +0100  941) 	struct file *file;
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  942) 
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  943) 	if (atomic_read(&files->count) == 1) {
bebf684bf3309 (Eric W. Biederman  2020-11-20 17:14:24 -0600  944) 		file = files_lookup_fd_raw(files, fd);
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  945) 		if (!file || unlikely(file->f_mode & mask))
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  946) 			return 0;
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  947) 		return (unsigned long)file;
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  948) 	} else {
091141a42e15f (Jens Axboe         2018-11-21 10:32:39 -0700  949) 		file = __fget(fd, mask, 1);
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  950) 		if (!file)
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  951) 			return 0;
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  952) 		return FDPUT_FPUT | (unsigned long)file;
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  953) 	}
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  954) }
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  955) unsigned long __fdget(unsigned int fd)
ad46183445043 (Oleg Nesterov      2014-01-13 16:48:40 +0100  956) {
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  957) 	return __fget_light(fd, FMODE_PATH);
ad46183445043 (Oleg Nesterov      2014-01-13 16:48:40 +0100  958) }
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  959) EXPORT_SYMBOL(__fdget);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  960) 
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  961) unsigned long __fdget_raw(unsigned int fd)
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  962) {
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  963) 	return __fget_light(fd, 0);
0ee8cdfe6af05 (Al Viro            2012-08-15 21:12:10 -0400  964) }
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  965) 
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  966) unsigned long __fdget_pos(unsigned int fd)
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  967) {
99aea68134f3c (Eric Biggers       2014-03-16 15:47:48 -0500  968) 	unsigned long v = __fdget(fd);
99aea68134f3c (Eric Biggers       2014-03-16 15:47:48 -0500  969) 	struct file *file = (struct file *)(v & ~3);
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  970) 
2be7d348fe924 (Linus Torvalds     2019-11-26 11:34:06 -0800  971) 	if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  972) 		if (file_count(file) > 1) {
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  973) 			v |= FDPUT_POS_UNLOCK;
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  974) 			mutex_lock(&file->f_pos_lock);
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  975) 		}
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  976) 	}
99aea68134f3c (Eric Biggers       2014-03-16 15:47:48 -0500  977) 	return v;
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  978) }
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  979) 
63b6df14134dd (Al Viro            2016-04-20 17:08:21 -0400  980) void __f_unlock_pos(struct file *f)
63b6df14134dd (Al Viro            2016-04-20 17:08:21 -0400  981) {
63b6df14134dd (Al Viro            2016-04-20 17:08:21 -0400  982) 	mutex_unlock(&f->f_pos_lock);
63b6df14134dd (Al Viro            2016-04-20 17:08:21 -0400  983) }
63b6df14134dd (Al Viro            2016-04-20 17:08:21 -0400  984) 
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  985) /*
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  986)  * We only lock f_pos if we have threads or if the file might be
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  987)  * shared with another process. In both cases we'll have an elevated
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  988)  * file count (done either by fdget() or by fork()).
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  989)  */
bd2a31d522344 (Al Viro            2014-03-04 14:54:22 -0500  990) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  991) void set_close_on_exec(unsigned int fd, int flag)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  992) {
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  993) 	struct files_struct *files = current->files;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  994) 	struct fdtable *fdt;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  995) 	spin_lock(&files->file_lock);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  996) 	fdt = files_fdtable(files);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  997) 	if (flag)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  998) 		__set_close_on_exec(fd, fdt);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400  999) 	else
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1000) 		__clear_close_on_exec(fd, fdt);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1001) 	spin_unlock(&files->file_lock);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1002) }
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1003) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1004) bool get_close_on_exec(unsigned int fd)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1005) {
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1006) 	struct files_struct *files = current->files;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1007) 	struct fdtable *fdt;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1008) 	bool res;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1009) 	rcu_read_lock();
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1010) 	fdt = files_fdtable(files);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1011) 	res = close_on_exec(fd, fdt);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1012) 	rcu_read_unlock();
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1013) 	return res;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1014) }
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1015) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1016) static int do_dup2(struct files_struct *files,
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1017) 	struct file *file, unsigned fd, unsigned flags)
e983094d6dce5 (Al Viro            2014-08-31 14:12:09 -0400 1018) __releases(&files->file_lock)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1019) {
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1020) 	struct file *tofree;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1021) 	struct fdtable *fdt;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1022) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1023) 	/*
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1024) 	 * We need to detect attempts to do dup2() over allocated but still
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1025) 	 * not finished descriptor.  NB: OpenBSD avoids that at the price of
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1026) 	 * extra work in their equivalent of fget() - they insert struct
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1027) 	 * file immediately after grabbing descriptor, mark it larval if
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1028) 	 * more work (e.g. actual opening) is needed and make sure that
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1029) 	 * fget() treats larval files as absent.  Potentially interesting,
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1030) 	 * but while extra work in fget() is trivial, locking implications
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1031) 	 * and amount of surgery on open()-related paths in VFS are not.
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1032) 	 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1033) 	 * deadlocks in rather amusing ways, AFAICS.  All of that is out of
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1034) 	 * scope of POSIX or SUS, since neither considers shared descriptor
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1035) 	 * tables and this condition does not arise without those.
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1036) 	 */
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1037) 	fdt = files_fdtable(files);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1038) 	tofree = fdt->fd[fd];
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1039) 	if (!tofree && fd_is_open(fd, fdt))
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1040) 		goto Ebusy;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1041) 	get_file(file);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1042) 	rcu_assign_pointer(fdt->fd[fd], file);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1043) 	__set_open_fd(fd, fdt);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1044) 	if (flags & O_CLOEXEC)
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1045) 		__set_close_on_exec(fd, fdt);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1046) 	else
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1047) 		__clear_close_on_exec(fd, fdt);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1048) 	spin_unlock(&files->file_lock);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1049) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1050) 	if (tofree)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1051) 		filp_close(tofree, files);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1052) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1053) 	return fd;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1054) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1055) Ebusy:
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1056) 	spin_unlock(&files->file_lock);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1057) 	return -EBUSY;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1058) }
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1059) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1060) int replace_fd(unsigned fd, struct file *file, unsigned flags)
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1061) {
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1062) 	int err;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1063) 	struct files_struct *files = current->files;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1064) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1065) 	if (!file)
8760c909f54a8 (Eric W. Biederman  2020-11-20 17:14:38 -0600 1066) 		return close_fd(fd);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1067) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1068) 	if (fd >= rlimit(RLIMIT_NOFILE))
08f05c49749ee (Al Viro            2012-10-31 03:37:48 +0000 1069) 		return -EBADF;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1070) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1071) 	spin_lock(&files->file_lock);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1072) 	err = expand_files(files, fd);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1073) 	if (unlikely(err < 0))
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1074) 		goto out_unlock;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1075) 	return do_dup2(files, file, fd, flags);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1076) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1077) out_unlock:
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1078) 	spin_unlock(&files->file_lock);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1079) 	return err;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1080) }
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1081) 
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1082) /**
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1083)  * __receive_fd() - Install received file into file descriptor table
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1084)  * @file: struct file that was received from another process
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1085)  * @ufd: __user pointer to write new fd number to
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1086)  * @o_flags: the O_* flags to apply to the new fd entry
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1087)  *
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1088)  * Installs a received file into the file descriptor table, with appropriate
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1089)  * checks and count updates. Optionally writes the fd number to userspace, if
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1090)  * @ufd is non-NULL.
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1091)  *
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1092)  * This helper handles its own reference counting of the incoming
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1093)  * struct file.
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1094)  *
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1095)  * Returns newly install fd or -ve on error.
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1096)  */
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1097) int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1098) {
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1099) 	int new_fd;
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1100) 	int error;
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1101) 
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1102) 	error = security_file_receive(file);
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1103) 	if (error)
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1104) 		return error;
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1105) 
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1106) 	new_fd = get_unused_fd_flags(o_flags);
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1107) 	if (new_fd < 0)
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1108) 		return new_fd;
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1109) 
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1110) 	if (ufd) {
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1111) 		error = put_user(new_fd, ufd);
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1112) 		if (error) {
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1113) 			put_unused_fd(new_fd);
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1114) 			return error;
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1115) 		}
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1116) 	}
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1117) 
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1118) 	fd_install(new_fd, get_file(file));
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1119) 	__receive_sock(file);
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1120) 	return new_fd;
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1121) }
173817151b15d (Kees Cook          2020-06-10 08:46:58 -0700 1122) 
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1123) int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags)
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1124) {
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1125) 	int error;
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1126) 
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1127) 	error = security_file_receive(file);
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1128) 	if (error)
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1129) 		return error;
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1130) 	error = replace_fd(new_fd, file, o_flags);
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1131) 	if (error)
42eb0d54c08a0 (Christoph Hellwig  2021-03-25 09:22:09 +0100 1132) 		return error;
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1133) 	__receive_sock(file);
deefa7f3505ae (Kees Cook          2020-06-10 20:47:45 -0700 1134) 	return new_fd;
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1135) }
6659061045cc9 (Kees Cook          2020-06-10 08:20:05 -0700 1136) 
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1137) static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1138) {
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1139) 	int err = -EBADF;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1140) 	struct file *file;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1141) 	struct files_struct *files = current->files;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1142) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1143) 	if ((flags & ~O_CLOEXEC) != 0)
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1144) 		return -EINVAL;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1145) 
aed976475bff9 (Richard W.M. Jones 2012-10-09 15:27:43 +0100 1146) 	if (unlikely(oldfd == newfd))
aed976475bff9 (Richard W.M. Jones 2012-10-09 15:27:43 +0100 1147) 		return -EINVAL;
aed976475bff9 (Richard W.M. Jones 2012-10-09 15:27:43 +0100 1148) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1149) 	if (newfd >= rlimit(RLIMIT_NOFILE))
08f05c49749ee (Al Viro            2012-10-31 03:37:48 +0000 1150) 		return -EBADF;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1151) 
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1152) 	spin_lock(&files->file_lock);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1153) 	err = expand_files(files, newfd);
120ce2b0cd52a (Eric W. Biederman  2020-11-20 17:14:25 -0600 1154) 	file = files_lookup_fd_locked(files, oldfd);
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1155) 	if (unlikely(!file))
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1156) 		goto Ebadf;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1157) 	if (unlikely(err < 0)) {
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1158) 		if (err == -EMFILE)
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1159) 			goto Ebadf;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1160) 		goto out_unlock;
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1161) 	}
8280d16172243 (Al Viro            2012-08-21 12:11:46 -0400 1162) 	return do_dup2(files, file, newfd, flags);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1163) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1164) Ebadf:
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1165) 	err = -EBADF;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1166) out_unlock:
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1167) 	spin_unlock(&files->file_lock);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1168) 	return err;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1169) }
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1170) 
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1171) SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1172) {
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1173) 	return ksys_dup3(oldfd, newfd, flags);
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1174) }
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1175) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1176) SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1177) {
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1178) 	if (unlikely(newfd == oldfd)) { /* corner case */
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1179) 		struct files_struct *files = current->files;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1180) 		int retval = oldfd;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1181) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1182) 		rcu_read_lock();
f36c294327419 (Eric W. Biederman  2020-11-20 17:14:26 -0600 1183) 		if (!files_lookup_fd_rcu(files, oldfd))
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1184) 			retval = -EBADF;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1185) 		rcu_read_unlock();
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1186) 		return retval;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1187) 	}
c7248321a3d42 (Dominik Brodowski  2018-03-11 11:34:40 +0100 1188) 	return ksys_dup3(oldfd, newfd, 0);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1189) }
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1190) 
bc1cd99a9ad7e (Christoph Hellwig  2020-07-14 08:58:49 +0200 1191) SYSCALL_DEFINE1(dup, unsigned int, fildes)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1192) {
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1193) 	int ret = -EBADF;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1194) 	struct file *file = fget_raw(fildes);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1195) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1196) 	if (file) {
8d10a035829ca (Yann Droneaud      2014-12-10 15:45:44 -0800 1197) 		ret = get_unused_fd_flags(0);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1198) 		if (ret >= 0)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1199) 			fd_install(ret, file);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1200) 		else
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1201) 			fput(file);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1202) 	}
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1203) 	return ret;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1204) }
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1205) 
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1206) int f_dupfd(unsigned int from, struct file *file, unsigned flags)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1207) {
e06b53c22f31e (Eric W. Biederman  2020-11-20 17:14:36 -0600 1208) 	unsigned long nofile = rlimit(RLIMIT_NOFILE);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1209) 	int err;
e06b53c22f31e (Eric W. Biederman  2020-11-20 17:14:36 -0600 1210) 	if (from >= nofile)
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1211) 		return -EINVAL;
e06b53c22f31e (Eric W. Biederman  2020-11-20 17:14:36 -0600 1212) 	err = alloc_fd(from, nofile, flags);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1213) 	if (err >= 0) {
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1214) 		get_file(file);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1215) 		fd_install(err, file);
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1216) 	}
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1217) 	return err;
fe17f22d7fd0e (Al Viro            2012-08-21 11:48:11 -0400 1218) }
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1219) 
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1220) int iterate_fd(struct files_struct *files, unsigned n,
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1221) 		int (*f)(const void *, struct file *, unsigned),
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1222) 		const void *p)
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1223) {
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1224) 	struct fdtable *fdt;
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1225) 	int res = 0;
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1226) 	if (!files)
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1227) 		return 0;
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1228) 	spin_lock(&files->file_lock);
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1229) 	for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1230) 		struct file *file;
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1231) 		file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1232) 		if (!file)
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1233) 			continue;
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1234) 		res = f(p, file, n);
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1235) 		if (res)
a77cfcb429ed9 (Al Viro            2012-11-29 22:57:33 -0500 1236) 			break;
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1237) 	}
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1238) 	spin_unlock(&files->file_lock);
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1239) 	return res;
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1240) }
c3c073f808b22 (Al Viro            2012-08-21 22:32:06 -0400 1241) EXPORT_SYMBOL(iterate_fd);