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