Blame view

fs/fcntl.c 23 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
  /*
   *  linux/fs/fcntl.c
   *
   *  Copyright (C) 1991, 1992  Linus Torvalds
   */
  
  #include <linux/syscalls.h>
  #include <linux/init.h>
  #include <linux/mm.h>
299300258   Ingo Molnar   sched/headers: Pr...
11
  #include <linux/sched/task.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
  #include <linux/fs.h>
  #include <linux/file.h>
9f3acc314   Al Viro   [PATCH] split lin...
14
  #include <linux/fdtable.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
15
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #include <linux/dnotify.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  #include <linux/slab.h>
  #include <linux/module.h>
35f3d14db   Jens Axboe   pipe: add support...
19
  #include <linux/pipe_fs_i.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include <linux/security.h>
  #include <linux/ptrace.h>
7ed20e1ad   Jesper Juhl   [PATCH] convert t...
22
  #include <linux/signal.h>
ab2af1f50   Dipankar Sarma   [PATCH] files: fi...
23
  #include <linux/rcupdate.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
24
  #include <linux/pid_namespace.h>
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
25
  #include <linux/user_namespace.h>
40e041a2c   David Herrmann   shm: add sealing API
26
  #include <linux/shmem_fs.h>
80f0cce6a   Al Viro   fcntl: move compa...
27
  #include <linux/compat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
  
  #include <asm/poll.h>
  #include <asm/siginfo.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
31
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32

76398425b   Jonathan Corbet   Move FASYNC bit h...
33
  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
  
  static int setfl(int fd, struct file * filp, unsigned long arg)
  {
496ad9aa8   Al Viro   new helper: file_...
37
  	struct inode * inode = file_inode(filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  	int error = 0;
7d95c8f27   dean gaudet   [PATCH] fcntl F_S...
39
40
41
42
43
  	/*
  	 * O_APPEND cannot be cleared if the file is marked as append-only
  	 * and the file is open for write.
  	 */
  	if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
  		return -EPERM;
  
  	/* O_NOATIME can only be set by the owner or superuser */
  	if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
2e1496707   Serge E. Hallyn   userns: rename is...
48
  		if (!inode_owner_or_capable(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
  			return -EPERM;
  
  	/* required for strict SunOS emulation */
  	if (O_NONBLOCK != O_NDELAY)
  	       if (arg & O_NDELAY)
  		   arg |= O_NONBLOCK;
0dbf5f206   Stanislav Kinsburskiy   fcntl: allow to s...
55
  	/* Pipe packetized mode is controlled by O_DIRECT flag */
450630975   Al Viro   don't open-code f...
56
  	if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
  		if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  			!filp->f_mapping->a_ops->direct_IO)
  				return -EINVAL;
  	}
72c2d5319   Al Viro   file->f_op is nev...
61
  	if (filp->f_op->check_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
  		error = filp->f_op->check_flags(arg);
  	if (error)
  		return error;
218d11a8b   Jonathan Corbet   Fix a race condit...
65
  	/*
76398425b   Jonathan Corbet   Move FASYNC bit h...
66
  	 * ->fasync() is responsible for setting the FASYNC bit.
218d11a8b   Jonathan Corbet   Fix a race condit...
67
  	 */
72c2d5319   Al Viro   file->f_op is nev...
68
  	if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
76398425b   Jonathan Corbet   Move FASYNC bit h...
69
70
71
  		error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  		if (error < 0)
  			goto out;
60aa49243   Jonathan Corbet   Rationalize fasyn...
72
73
  		if (error > 0)
  			error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
  	}
db1dd4d37   Jonathan Corbet   Use f_lock to pro...
75
  	spin_lock(&filp->f_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  	filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
db1dd4d37   Jonathan Corbet   Use f_lock to pro...
77
  	spin_unlock(&filp->f_lock);
76398425b   Jonathan Corbet   Move FASYNC bit h...
78

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
   out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
  	return error;
  }
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
82
  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
2f38d70fb   Oleg Nesterov   shift current_cre...
83
                       int force)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
  {
80e1e8239   Linus Torvalds   Fix race in tty_f...
85
  	write_lock_irq(&filp->f_owner.lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  	if (force || !filp->f_owner.pid) {
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
87
88
89
  		put_pid(filp->f_owner.pid);
  		filp->f_owner.pid = get_pid(pid);
  		filp->f_owner.pid_type = type;
2f38d70fb   Oleg Nesterov   shift current_cre...
90
91
92
93
94
95
  
  		if (pid) {
  			const struct cred *cred = current_cred();
  			filp->f_owner.uid = cred->uid;
  			filp->f_owner.euid = cred->euid;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  	}
80e1e8239   Linus Torvalds   Fix race in tty_f...
97
  	write_unlock_irq(&filp->f_owner.lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  }
e0b93eddf   Jeff Layton   security: make se...
99
  void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
100
  		int force)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
  {
e0b93eddf   Jeff Layton   security: make se...
102
  	security_file_set_fowner(filp);
2f38d70fb   Oleg Nesterov   shift current_cre...
103
  	f_modown(filp, pid, type, force);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  }
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
105
  EXPORT_SYMBOL(__f_setown);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

393cc3f51   Jiri Slaby   fs/fcntl: f_setow...
107
  int f_setown(struct file *filp, unsigned long arg, int force)
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
108
109
  {
  	enum pid_type type;
f73127356   Jeff Layton   fs/fcntl: return ...
110
111
  	struct pid *pid = NULL;
  	int who = arg, ret = 0;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
112
113
  	type = PIDTYPE_PID;
  	if (who < 0) {
fc3dc6747   Jiri Slaby   fs/fcntl: f_setow...
114
115
116
  		/* avoid overflow below */
  		if (who == INT_MIN)
  			return -EINVAL;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
117
118
119
  		type = PIDTYPE_PGID;
  		who = -who;
  	}
f73127356   Jeff Layton   fs/fcntl: return ...
120

609d7fa95   Eric W. Biederman   [PATCH] file: mod...
121
  	rcu_read_lock();
f73127356   Jeff Layton   fs/fcntl: return ...
122
123
124
125
126
127
128
129
  	if (who) {
  		pid = find_vpid(who);
  		if (!pid)
  			ret = -ESRCH;
  	}
  
  	if (!ret)
  		__f_setown(filp, pid, type, force);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
130
  	rcu_read_unlock();
393cc3f51   Jiri Slaby   fs/fcntl: f_setow...
131

f73127356   Jeff Layton   fs/fcntl: return ...
132
  	return ret;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
133
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
137
  EXPORT_SYMBOL(f_setown);
  
  void f_delown(struct file *filp)
  {
2f38d70fb   Oleg Nesterov   shift current_cre...
138
  	f_modown(filp, NULL, PIDTYPE_PID, 1);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
139
140
141
142
143
  }
  
  pid_t f_getown(struct file *filp)
  {
  	pid_t pid;
43fa1adb9   Eric W. Biederman   [PATCH] file: Add...
144
  	read_lock(&filp->f_owner.lock);
6c5f3e7b4   Pavel Emelyanov   Pidns: make full ...
145
  	pid = pid_vnr(filp->f_owner.pid);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
146
147
  	if (filp->f_owner.pid_type == PIDTYPE_PGID)
  		pid = -pid;
43fa1adb9   Eric W. Biederman   [PATCH] file: Add...
148
  	read_unlock(&filp->f_owner.lock);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
149
  	return pid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  }
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
151
152
  static int f_setown_ex(struct file *filp, unsigned long arg)
  {
63784dd02   Al Viro   fcntl: fix misann...
153
  	struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
154
155
156
157
158
159
160
  	struct f_owner_ex owner;
  	struct pid *pid;
  	int type;
  	int ret;
  
  	ret = copy_from_user(&owner, owner_p, sizeof(owner));
  	if (ret)
5b54470da   Dan Carpenter   fcntl: return -EF...
161
  		return -EFAULT;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
162
163
164
165
166
167
168
169
170
  
  	switch (owner.type) {
  	case F_OWNER_TID:
  		type = PIDTYPE_MAX;
  		break;
  
  	case F_OWNER_PID:
  		type = PIDTYPE_PID;
  		break;
978b4053a   Peter Zijlstra   fcntl: rename F_O...
171
  	case F_OWNER_PGRP:
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
172
173
174
175
176
177
178
179
180
181
182
183
  		type = PIDTYPE_PGID;
  		break;
  
  	default:
  		return -EINVAL;
  	}
  
  	rcu_read_lock();
  	pid = find_vpid(owner.pid);
  	if (owner.pid && !pid)
  		ret = -ESRCH;
  	else
e0b93eddf   Jeff Layton   security: make se...
184
  		 __f_setown(filp, pid, type, 1);
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
185
186
187
188
189
190
191
  	rcu_read_unlock();
  
  	return ret;
  }
  
  static int f_getown_ex(struct file *filp, unsigned long arg)
  {
63784dd02   Al Viro   fcntl: fix misann...
192
  	struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  	struct f_owner_ex owner;
  	int ret = 0;
  
  	read_lock(&filp->f_owner.lock);
  	owner.pid = pid_vnr(filp->f_owner.pid);
  	switch (filp->f_owner.pid_type) {
  	case PIDTYPE_MAX:
  		owner.type = F_OWNER_TID;
  		break;
  
  	case PIDTYPE_PID:
  		owner.type = F_OWNER_PID;
  		break;
  
  	case PIDTYPE_PGID:
978b4053a   Peter Zijlstra   fcntl: rename F_O...
208
  		owner.type = F_OWNER_PGRP;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
209
210
211
212
213
214
215
216
  		break;
  
  	default:
  		WARN_ON(1);
  		ret = -EINVAL;
  		break;
  	}
  	read_unlock(&filp->f_owner.lock);
5b54470da   Dan Carpenter   fcntl: return -EF...
217
  	if (!ret) {
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
218
  		ret = copy_to_user(owner_p, &owner, sizeof(owner));
5b54470da   Dan Carpenter   fcntl: return -EF...
219
220
221
  		if (ret)
  			ret = -EFAULT;
  	}
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
222
223
  	return ret;
  }
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
224
225
226
227
  #ifdef CONFIG_CHECKPOINT_RESTORE
  static int f_getowner_uids(struct file *filp, unsigned long arg)
  {
  	struct user_namespace *user_ns = current_user_ns();
63784dd02   Al Viro   fcntl: fix misann...
228
  	uid_t __user *dst = (void __user *)arg;
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  	uid_t src[2];
  	int err;
  
  	read_lock(&filp->f_owner.lock);
  	src[0] = from_kuid(user_ns, filp->f_owner.uid);
  	src[1] = from_kuid(user_ns, filp->f_owner.euid);
  	read_unlock(&filp->f_owner.lock);
  
  	err  = put_user(src[0], &dst[0]);
  	err |= put_user(src[1], &dst[1]);
  
  	return err;
  }
  #else
  static int f_getowner_uids(struct file *filp, unsigned long arg)
  {
  	return -EINVAL;
  }
  #endif
c75b1d942   Jens Axboe   fs: add fcntl() i...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  static bool rw_hint_valid(enum rw_hint hint)
  {
  	switch (hint) {
  	case RWF_WRITE_LIFE_NOT_SET:
  	case RWH_WRITE_LIFE_NONE:
  	case RWH_WRITE_LIFE_SHORT:
  	case RWH_WRITE_LIFE_MEDIUM:
  	case RWH_WRITE_LIFE_LONG:
  	case RWH_WRITE_LIFE_EXTREME:
  		return true;
  	default:
  		return false;
  	}
  }
  
  static long fcntl_rw_hint(struct file *file, unsigned int cmd,
  			  unsigned long arg)
  {
  	struct inode *inode = file_inode(file);
  	u64 *argp = (u64 __user *)arg;
  	enum rw_hint hint;
5657cb079   Jens Axboe   fs/fcntl: use cop...
269
  	u64 h;
c75b1d942   Jens Axboe   fs: add fcntl() i...
270
271
272
  
  	switch (cmd) {
  	case F_GET_FILE_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
273
274
  		h = file_write_hint(file);
  		if (copy_to_user(argp, &h, sizeof(*argp)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
275
276
277
  			return -EFAULT;
  		return 0;
  	case F_SET_FILE_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
278
  		if (copy_from_user(&h, argp, sizeof(h)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
279
  			return -EFAULT;
5657cb079   Jens Axboe   fs/fcntl: use cop...
280
  		hint = (enum rw_hint) h;
c75b1d942   Jens Axboe   fs: add fcntl() i...
281
282
283
284
285
286
287
288
  		if (!rw_hint_valid(hint))
  			return -EINVAL;
  
  		spin_lock(&file->f_lock);
  		file->f_write_hint = hint;
  		spin_unlock(&file->f_lock);
  		return 0;
  	case F_GET_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
289
290
  		h = inode->i_write_hint;
  		if (copy_to_user(argp, &h, sizeof(*argp)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
291
292
293
  			return -EFAULT;
  		return 0;
  	case F_SET_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
294
  		if (copy_from_user(&h, argp, sizeof(h)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
295
  			return -EFAULT;
5657cb079   Jens Axboe   fs/fcntl: use cop...
296
  		hint = (enum rw_hint) h;
c75b1d942   Jens Axboe   fs: add fcntl() i...
297
298
299
300
301
302
303
304
305
306
307
  		if (!rw_hint_valid(hint))
  			return -EINVAL;
  
  		inode_lock(inode);
  		inode->i_write_hint = hint;
  		inode_unlock(inode);
  		return 0;
  	default:
  		return -EINVAL;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
  static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  		struct file *filp)
  {
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
311
312
  	void __user *argp = (void __user *)arg;
  	struct flock flock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
314
315
316
  	long err = -EINVAL;
  
  	switch (cmd) {
  	case F_DUPFD:
fe17f22d7   Al Viro   take purely descr...
317
318
  		err = f_dupfd(arg, filp, 0);
  		break;
22d2b35b2   Ulrich Drepper   F_DUPFD_CLOEXEC i...
319
  	case F_DUPFD_CLOEXEC:
121977187   Al Viro   Fix F_DUPFD_CLOEX...
320
  		err = f_dupfd(arg, filp, O_CLOEXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  		break;
  	case F_GETFD:
  		err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  		break;
  	case F_SETFD:
  		err = 0;
  		set_close_on_exec(fd, arg & FD_CLOEXEC);
  		break;
  	case F_GETFL:
  		err = filp->f_flags;
  		break;
  	case F_SETFL:
  		err = setfl(fd, filp, arg);
  		break;
5d50ffd7c   Jeff Layton   locks: add new fc...
335
336
  #if BITS_PER_LONG != 32
  	/* 32-bit arches must use fcntl64() */
0d3f7a2dd   Jeff Layton   locks: rename fil...
337
  	case F_OFD_GETLK:
5d50ffd7c   Jeff Layton   locks: add new fc...
338
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
  	case F_GETLK:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
340
341
342
343
344
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			return -EFAULT;
  		err = fcntl_getlk(filp, cmd, &flock);
  		if (!err && copy_to_user(argp, &flock, sizeof(flock)))
  			return -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
  		break;
5d50ffd7c   Jeff Layton   locks: add new fc...
346
347
  #if BITS_PER_LONG != 32
  	/* 32-bit arches must use fcntl64() */
0d3f7a2dd   Jeff Layton   locks: rename fil...
348
349
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
5d50ffd7c   Jeff Layton   locks: add new fc...
350
351
  #endif
  		/* Fallthrough */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
353
  	case F_SETLK:
  	case F_SETLKW:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
354
355
356
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			return -EFAULT;
  		err = fcntl_setlk(fd, filp, cmd, &flock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
361
362
363
364
365
  		break;
  	case F_GETOWN:
  		/*
  		 * XXX If f_owner is a process group, the
  		 * negative return value will get converted
  		 * into an error.  Oops.  If we keep the
  		 * current syscall conventions, the only way
  		 * to fix this will be in libc.
  		 */
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
366
  		err = f_getown(filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
  		force_successful_syscall_return();
  		break;
  	case F_SETOWN:
393cc3f51   Jiri Slaby   fs/fcntl: f_setow...
370
  		err = f_setown(filp, arg, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
  		break;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
372
373
374
375
376
377
  	case F_GETOWN_EX:
  		err = f_getown_ex(filp, arg);
  		break;
  	case F_SETOWN_EX:
  		err = f_setown_ex(filp, arg);
  		break;
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
378
379
380
  	case F_GETOWNER_UIDS:
  		err = f_getowner_uids(filp, arg);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
  	case F_GETSIG:
  		err = filp->f_owner.signum;
  		break;
  	case F_SETSIG:
  		/* arg == 0 restores default behaviour. */
7ed20e1ad   Jesper Juhl   [PATCH] convert t...
386
  		if (!valid_signal(arg)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  			break;
  		}
  		err = 0;
  		filp->f_owner.signum = arg;
  		break;
  	case F_GETLEASE:
  		err = fcntl_getlease(filp);
  		break;
  	case F_SETLEASE:
  		err = fcntl_setlease(fd, filp, arg);
  		break;
  	case F_NOTIFY:
  		err = fcntl_dirnotify(fd, filp, arg);
  		break;
35f3d14db   Jens Axboe   pipe: add support...
401
402
403
404
  	case F_SETPIPE_SZ:
  	case F_GETPIPE_SZ:
  		err = pipe_fcntl(filp, cmd, arg);
  		break;
40e041a2c   David Herrmann   shm: add sealing API
405
406
407
408
  	case F_ADD_SEALS:
  	case F_GET_SEALS:
  		err = shmem_fcntl(filp, cmd, arg);
  		break;
c75b1d942   Jens Axboe   fs: add fcntl() i...
409
410
411
412
413
414
  	case F_GET_RW_HINT:
  	case F_SET_RW_HINT:
  	case F_GET_FILE_RW_HINT:
  	case F_SET_FILE_RW_HINT:
  		err = fcntl_rw_hint(filp, cmd, arg);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
418
419
  	default:
  		break;
  	}
  	return err;
  }
1abf0c718   Al Viro   New kind of open ...
420
421
422
423
424
425
426
427
428
429
430
431
  static int check_fcntl_cmd(unsigned cmd)
  {
  	switch (cmd) {
  	case F_DUPFD:
  	case F_DUPFD_CLOEXEC:
  	case F_GETFD:
  	case F_SETFD:
  	case F_GETFL:
  		return 1;
  	}
  	return 0;
  }
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
432
  SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
  {	
2903ff019   Al Viro   switch simple cas...
434
  	struct fd f = fdget_raw(fd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
  	long err = -EBADF;
2903ff019   Al Viro   switch simple cas...
436
  	if (!f.file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
  		goto out;
2903ff019   Al Viro   switch simple cas...
438
  	if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c79   Al Viro   switch fcntl to f...
439
440
  		if (!check_fcntl_cmd(cmd))
  			goto out1;
1abf0c718   Al Viro   New kind of open ...
441
  	}
2903ff019   Al Viro   switch simple cas...
442
  	err = security_file_fcntl(f.file, cmd, arg);
545ec2c79   Al Viro   switch fcntl to f...
443
  	if (!err)
2903ff019   Al Viro   switch simple cas...
444
  		err = do_fcntl(fd, cmd, arg, f.file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445

545ec2c79   Al Viro   switch fcntl to f...
446
  out1:
2903ff019   Al Viro   switch simple cas...
447
   	fdput(f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
450
451
452
  out:
  	return err;
  }
  
  #if BITS_PER_LONG == 32
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
453
454
  SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  		unsigned long, arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  {	
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
456
  	void __user *argp = (void __user *)arg;
2903ff019   Al Viro   switch simple cas...
457
  	struct fd f = fdget_raw(fd);
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
458
  	struct flock64 flock;
545ec2c79   Al Viro   switch fcntl to f...
459
  	long err = -EBADF;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460

2903ff019   Al Viro   switch simple cas...
461
  	if (!f.file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
  		goto out;
2903ff019   Al Viro   switch simple cas...
463
  	if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c79   Al Viro   switch fcntl to f...
464
465
  		if (!check_fcntl_cmd(cmd))
  			goto out1;
1abf0c718   Al Viro   New kind of open ...
466
  	}
2903ff019   Al Viro   switch simple cas...
467
  	err = security_file_fcntl(f.file, cmd, arg);
545ec2c79   Al Viro   switch fcntl to f...
468
469
  	if (err)
  		goto out1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
  	
  	switch (cmd) {
5d50ffd7c   Jeff Layton   locks: add new fc...
472
  	case F_GETLK64:
0d3f7a2dd   Jeff Layton   locks: rename fil...
473
  	case F_OFD_GETLK:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
474
475
476
477
478
479
  		err = -EFAULT;
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			break;
  		err = fcntl_getlk64(f.file, cmd, &flock);
  		if (!err && copy_to_user(argp, &flock, sizeof(flock)))
  			err = -EFAULT;
5d50ffd7c   Jeff Layton   locks: add new fc...
480
481
482
  		break;
  	case F_SETLK64:
  	case F_SETLKW64:
0d3f7a2dd   Jeff Layton   locks: rename fil...
483
484
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
485
486
487
488
  		err = -EFAULT;
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			break;
  		err = fcntl_setlk64(fd, f.file, cmd, &flock);
5d50ffd7c   Jeff Layton   locks: add new fc...
489
490
491
492
  		break;
  	default:
  		err = do_fcntl(fd, cmd, arg, f.file);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
  	}
545ec2c79   Al Viro   switch fcntl to f...
494
  out1:
2903ff019   Al Viro   switch simple cas...
495
  	fdput(f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
497
498
499
  out:
  	return err;
  }
  #endif
80f0cce6a   Al Viro   fcntl: move compa...
500
  #ifdef CONFIG_COMPAT
8c6657cb5   Al Viro   Switch flock copy...
501
  /* careful - don't use anywhere else */
b59eea554   Linus Torvalds   vfs: fix flock co...
502
503
504
505
506
507
508
509
  #define copy_flock_fields(dst, src)		\
  	(dst)->l_type = (src)->l_type;		\
  	(dst)->l_whence = (src)->l_whence;	\
  	(dst)->l_start = (src)->l_start;	\
  	(dst)->l_len = (src)->l_len;		\
  	(dst)->l_pid = (src)->l_pid;
  
  static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
510
  {
8c6657cb5   Al Viro   Switch flock copy...
511
512
513
  	struct compat_flock fl;
  
  	if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
80f0cce6a   Al Viro   fcntl: move compa...
514
  		return -EFAULT;
b59eea554   Linus Torvalds   vfs: fix flock co...
515
  	copy_flock_fields(kfl, &fl);
80f0cce6a   Al Viro   fcntl: move compa...
516
517
  	return 0;
  }
b59eea554   Linus Torvalds   vfs: fix flock co...
518
  static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
519
  {
8c6657cb5   Al Viro   Switch flock copy...
520
521
522
  	struct compat_flock64 fl;
  
  	if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
80f0cce6a   Al Viro   fcntl: move compa...
523
  		return -EFAULT;
b59eea554   Linus Torvalds   vfs: fix flock co...
524
  	copy_flock_fields(kfl, &fl);
80f0cce6a   Al Viro   fcntl: move compa...
525
526
  	return 0;
  }
b59eea554   Linus Torvalds   vfs: fix flock co...
527
  static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
528
  {
8c6657cb5   Al Viro   Switch flock copy...
529
530
531
  	struct compat_flock fl;
  
  	memset(&fl, 0, sizeof(struct compat_flock));
b59eea554   Linus Torvalds   vfs: fix flock co...
532
  	copy_flock_fields(&fl, kfl);
8c6657cb5   Al Viro   Switch flock copy...
533
  	if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
80f0cce6a   Al Viro   fcntl: move compa...
534
535
536
  		return -EFAULT;
  	return 0;
  }
80f0cce6a   Al Viro   fcntl: move compa...
537

b59eea554   Linus Torvalds   vfs: fix flock co...
538
  static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
539
  {
8c6657cb5   Al Viro   Switch flock copy...
540
  	struct compat_flock64 fl;
fc2f80219   Jeff Layton   fcntl: don't cap ...
541
542
  	BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
  	BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
8c6657cb5   Al Viro   Switch flock copy...
543
  	memset(&fl, 0, sizeof(struct compat_flock64));
b59eea554   Linus Torvalds   vfs: fix flock co...
544
  	copy_flock_fields(&fl, kfl);
8c6657cb5   Al Viro   Switch flock copy...
545
  	if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
80f0cce6a   Al Viro   fcntl: move compa...
546
547
548
  		return -EFAULT;
  	return 0;
  }
8c6657cb5   Al Viro   Switch flock copy...
549
  #undef copy_flock_fields
80f0cce6a   Al Viro   fcntl: move compa...
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  
  static unsigned int
  convert_fcntl_cmd(unsigned int cmd)
  {
  	switch (cmd) {
  	case F_GETLK64:
  		return F_GETLK;
  	case F_SETLK64:
  		return F_SETLK;
  	case F_SETLKW64:
  		return F_SETLKW;
  	}
  
  	return cmd;
  }
94073ad77   Christoph Hellwig   fs/locks: don't m...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
  /*
   * GETLK was successful and we need to return the data, but it needs to fit in
   * the compat structure.
   * l_start shouldn't be too big, unless the original start + end is greater than
   * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
   * -EOVERFLOW in that case.  l_len could be too big, in which case we just
   * truncate it, and only allow the app to see that part of the conflicting lock
   * that might make sense to it anyway
   */
  static int fixup_compat_flock(struct flock *flock)
  {
  	if (flock->l_start > COMPAT_OFF_T_MAX)
  		return -EOVERFLOW;
  	if (flock->l_len > COMPAT_OFF_T_MAX)
  		flock->l_len = COMPAT_OFF_T_MAX;
  	return 0;
  }
80f0cce6a   Al Viro   fcntl: move compa...
582
583
584
  COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  		       compat_ulong_t, arg)
  {
94073ad77   Christoph Hellwig   fs/locks: don't m...
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  	struct fd f = fdget_raw(fd);
  	struct flock flock;
  	long err = -EBADF;
  
  	if (!f.file)
  		return err;
  
  	if (unlikely(f.file->f_mode & FMODE_PATH)) {
  		if (!check_fcntl_cmd(cmd))
  			goto out_put;
  	}
  
  	err = security_file_fcntl(f.file, cmd, arg);
  	if (err)
  		goto out_put;
80f0cce6a   Al Viro   fcntl: move compa...
600
601
602
  
  	switch (cmd) {
  	case F_GETLK:
94073ad77   Christoph Hellwig   fs/locks: don't m...
603
604
605
606
607
608
609
  		err = get_compat_flock(&flock, compat_ptr(arg));
  		if (err)
  			break;
  		err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
  		if (err)
  			break;
  		err = fixup_compat_flock(&flock);
6e9c2a05c   Jeff Layton   fcntl: don't leak...
610
611
  		if (!err)
  			err = put_compat_flock(&flock, compat_ptr(arg));
94073ad77   Christoph Hellwig   fs/locks: don't m...
612
613
614
615
616
617
618
  		break;
  	case F_GETLK64:
  	case F_OFD_GETLK:
  		err = get_compat_flock64(&flock, compat_ptr(arg));
  		if (err)
  			break;
  		err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
fc2f80219   Jeff Layton   fcntl: don't cap ...
619
620
  		if (!err)
  			err = put_compat_flock64(&flock, compat_ptr(arg));
94073ad77   Christoph Hellwig   fs/locks: don't m...
621
  		break;
80f0cce6a   Al Viro   fcntl: move compa...
622
623
  	case F_SETLK:
  	case F_SETLKW:
94073ad77   Christoph Hellwig   fs/locks: don't m...
624
625
  		err = get_compat_flock(&flock, compat_ptr(arg));
  		if (err)
80f0cce6a   Al Viro   fcntl: move compa...
626
  			break;
94073ad77   Christoph Hellwig   fs/locks: don't m...
627
  		err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
80f0cce6a   Al Viro   fcntl: move compa...
628
  		break;
80f0cce6a   Al Viro   fcntl: move compa...
629
630
  	case F_SETLK64:
  	case F_SETLKW64:
80f0cce6a   Al Viro   fcntl: move compa...
631
632
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
94073ad77   Christoph Hellwig   fs/locks: don't m...
633
634
  		err = get_compat_flock64(&flock, compat_ptr(arg));
  		if (err)
80f0cce6a   Al Viro   fcntl: move compa...
635
  			break;
94073ad77   Christoph Hellwig   fs/locks: don't m...
636
  		err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
80f0cce6a   Al Viro   fcntl: move compa...
637
  		break;
80f0cce6a   Al Viro   fcntl: move compa...
638
  	default:
94073ad77   Christoph Hellwig   fs/locks: don't m...
639
  		err = do_fcntl(fd, cmd, arg, f.file);
80f0cce6a   Al Viro   fcntl: move compa...
640
641
  		break;
  	}
94073ad77   Christoph Hellwig   fs/locks: don't m...
642
643
644
  out_put:
  	fdput(f);
  	return err;
80f0cce6a   Al Viro   fcntl: move compa...
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
  }
  
  COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
  		       compat_ulong_t, arg)
  {
  	switch (cmd) {
  	case F_GETLK64:
  	case F_SETLK64:
  	case F_SETLKW64:
  	case F_OFD_GETLK:
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
  		return -EINVAL;
  	}
  	return compat_sys_fcntl64(fd, cmd, arg);
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
  /* Table to convert sigio signal codes into poll band bitmaps */
fa3536cc1   Eric Dumazet   [PATCH] Use __rea...
663
  static const long band_table[NSIGPOLL] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
669
670
671
672
673
674
  	POLLIN | POLLRDNORM,			/* POLL_IN */
  	POLLOUT | POLLWRNORM | POLLWRBAND,	/* POLL_OUT */
  	POLLIN | POLLRDNORM | POLLMSG,		/* POLL_MSG */
  	POLLERR,				/* POLL_ERR */
  	POLLPRI | POLLRDBAND,			/* POLL_PRI */
  	POLLHUP | POLLERR			/* POLL_HUP */
  };
  
  static inline int sigio_perm(struct task_struct *p,
                               struct fown_struct *fown, int sig)
  {
c69e8d9c0   David Howells   CRED: Use RCU to ...
675
676
677
678
679
  	const struct cred *cred;
  	int ret;
  
  	rcu_read_lock();
  	cred = __task_cred(p);
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
680
681
682
  	ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
  		uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
  		uid_eq(fown->uid,  cred->suid) || uid_eq(fown->uid,  cred->uid)) &&
c69e8d9c0   David Howells   CRED: Use RCU to ...
683
684
685
  	       !security_file_send_sigiotask(p, fown, sig));
  	rcu_read_unlock();
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
687
688
  }
  
  static void send_sigio_to_task(struct task_struct *p,
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
689
  			       struct fown_struct *fown,
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
690
  			       int fd, int reason, int group)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
691
  {
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
692
693
694
695
696
697
698
  	/*
  	 * F_SETSIG can change ->signum lockless in parallel, make
  	 * sure we read it once and use the same value throughout.
  	 */
  	int signum = ACCESS_ONCE(fown->signum);
  
  	if (!sigio_perm(p, fown, signum))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
  		return;
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
700
  	switch (signum) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
703
704
705
706
707
708
  		siginfo_t si;
  		default:
  			/* Queue a rt signal with the appropriate fd as its
  			   value.  We use SI_SIGIO as the source, not 
  			   SI_KERNEL, since kernel signals always get 
  			   delivered even if we can't queue.  Failure to
  			   queue in this case _should_ be reported; we fall
  			   back to SIGIO in that case. --sct */
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
709
  			si.si_signo = signum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
711
  			si.si_errno = 0;
  		        si.si_code  = reason;
d08477aa9   Eric W. Biederman   fcntl: Don't use ...
712
713
714
715
716
717
718
719
  			/*
  			 * Posix definies POLL_IN and friends to be signal
  			 * specific si_codes for SIG_POLL.  Linux extended
  			 * these si_codes to other signals in a way that is
  			 * ambiguous if other signals also have signal
  			 * specific si_codes.  In that case use SI_SIGIO instead
  			 * to remove the ambiguity.
  			 */
54640d238   Eric W. Biederman   fcntl: Don't set ...
720
  			if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
d08477aa9   Eric W. Biederman   fcntl: Don't use ...
721
  				si.si_code = SI_SIGIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
722
723
724
  			/* Make sure we are called with one of the POLL_*
  			   reasons, otherwise we could leak kernel stack into
  			   userspace.  */
d08477aa9   Eric W. Biederman   fcntl: Don't use ...
725
  			BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
730
  			if (reason - POLL_IN >= NSIGPOLL)
  				si.si_band  = ~0L;
  			else
  				si.si_band = band_table[reason - POLL_IN];
  			si.si_fd    = fd;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
731
  			if (!do_send_sig_info(signum, &si, p, group))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
734
  				break;
  		/* fall-through: fall back on the old plain SIGIO signal */
  		case 0:
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
735
  			do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
740
741
  	}
  }
  
  void send_sigio(struct fown_struct *fown, int fd, int band)
  {
  	struct task_struct *p;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
742
743
  	enum pid_type type;
  	struct pid *pid;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
744
  	int group = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
  	
  	read_lock(&fown->lock);
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
747

609d7fa95   Eric W. Biederman   [PATCH] file: mod...
748
  	type = fown->pid_type;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
749
750
751
752
  	if (type == PIDTYPE_MAX) {
  		group = 0;
  		type = PIDTYPE_PID;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
  	pid = fown->pid;
  	if (!pid)
  		goto out_unlock_fown;
  	
  	read_lock(&tasklist_lock);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
758
  	do_each_pid_task(pid, type, p) {
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
759
  		send_sigio_to_task(p, fown, fd, band, group);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
760
  	} while_each_pid_task(pid, type, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
766
  	read_unlock(&tasklist_lock);
   out_unlock_fown:
  	read_unlock(&fown->lock);
  }
  
  static void send_sigurg_to_task(struct task_struct *p,
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
767
  				struct fown_struct *fown, int group)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
769
  {
  	if (sigio_perm(p, fown, SIGURG))
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
770
  		do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
772
773
774
775
  }
  
  int send_sigurg(struct fown_struct *fown)
  {
  	struct task_struct *p;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
776
777
  	enum pid_type type;
  	struct pid *pid;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
778
  	int group = 1;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
779
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
781
  	
  	read_lock(&fown->lock);
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
782

609d7fa95   Eric W. Biederman   [PATCH] file: mod...
783
  	type = fown->pid_type;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
784
785
786
787
  	if (type == PIDTYPE_MAX) {
  		group = 0;
  		type = PIDTYPE_PID;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
791
792
793
794
  	pid = fown->pid;
  	if (!pid)
  		goto out_unlock_fown;
  
  	ret = 1;
  	
  	read_lock(&tasklist_lock);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
795
  	do_each_pid_task(pid, type, p) {
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
796
  		send_sigurg_to_task(p, fown, group);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
797
  	} while_each_pid_task(pid, type, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
799
800
801
802
  	read_unlock(&tasklist_lock);
   out_unlock_fown:
  	read_unlock(&fown->lock);
  	return ret;
  }
989a29792   Eric Dumazet   fasync: RCU and f...
803
  static DEFINE_SPINLOCK(fasync_lock);
e18b890bb   Christoph Lameter   [PATCH] slab: rem...
804
  static struct kmem_cache *fasync_cache __read_mostly;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805

989a29792   Eric Dumazet   fasync: RCU and f...
806
807
808
809
810
  static void fasync_free_rcu(struct rcu_head *head)
  {
  	kmem_cache_free(fasync_cache,
  			container_of(head, struct fasync_struct, fa_rcu));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
  /*
53281b6d3   Linus Torvalds   fasync: split 'fa...
812
813
814
815
816
817
818
   * Remove a fasync entry. If successfully removed, return
   * positive and clear the FASYNC flag. If no entry exists,
   * do nothing and return 0.
   *
   * NOTE! It is very important that the FASYNC flag always
   * match the state "is the filp on a fasync list".
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
   */
f7347ce4e   Linus Torvalds   fasync: re-organi...
820
  int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
822
  {
  	struct fasync_struct *fa, **fp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
  	int result = 0;
53281b6d3   Linus Torvalds   fasync: split 'fa...
824
  	spin_lock(&filp->f_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
825
  	spin_lock(&fasync_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
826
827
828
  	for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  		if (fa->fa_file != filp)
  			continue;
989a29792   Eric Dumazet   fasync: RCU and f...
829
830
831
832
  
  		spin_lock_irq(&fa->fa_lock);
  		fa->fa_file = NULL;
  		spin_unlock_irq(&fa->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
833
  		*fp = fa->fa_next;
989a29792   Eric Dumazet   fasync: RCU and f...
834
  		call_rcu(&fa->fa_rcu, fasync_free_rcu);
53281b6d3   Linus Torvalds   fasync: split 'fa...
835
836
837
  		filp->f_flags &= ~FASYNC;
  		result = 1;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
838
  	}
989a29792   Eric Dumazet   fasync: RCU and f...
839
  	spin_unlock(&fasync_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
840
841
842
  	spin_unlock(&filp->f_lock);
  	return result;
  }
f7347ce4e   Linus Torvalds   fasync: re-organi...
843
844
845
846
  struct fasync_struct *fasync_alloc(void)
  {
  	return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  }
53281b6d3   Linus Torvalds   fasync: split 'fa...
847
  /*
f7347ce4e   Linus Torvalds   fasync: re-organi...
848
849
850
   * NOTE! This can be used only for unused fasync entries:
   * entries that actually got inserted on the fasync list
   * need to be released by rcu - see fasync_remove_entry.
53281b6d3   Linus Torvalds   fasync: split 'fa...
851
   */
f7347ce4e   Linus Torvalds   fasync: re-organi...
852
  void fasync_free(struct fasync_struct *new)
53281b6d3   Linus Torvalds   fasync: split 'fa...
853
  {
f7347ce4e   Linus Torvalds   fasync: re-organi...
854
855
  	kmem_cache_free(fasync_cache, new);
  }
53281b6d3   Linus Torvalds   fasync: split 'fa...
856

f7347ce4e   Linus Torvalds   fasync: re-organi...
857
858
859
  /*
   * Insert a new entry into the fasync list.  Return the pointer to the
   * old one if we didn't use the new one.
55f335a88   Linus Torvalds   fasync: Fix place...
860
861
862
   *
   * NOTE! It is very important that the FASYNC flag always
   * match the state "is the filp on a fasync list".
f7347ce4e   Linus Torvalds   fasync: re-organi...
863
864
865
866
   */
  struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
  {
          struct fasync_struct *fa, **fp;
4a6a44996   Jonathan Corbet   Fix a lockdep war...
867

4a6a44996   Jonathan Corbet   Fix a lockdep war...
868
  	spin_lock(&filp->f_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
869
  	spin_lock(&fasync_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
870
  	for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
53281b6d3   Linus Torvalds   fasync: split 'fa...
871
872
  		if (fa->fa_file != filp)
  			continue;
989a29792   Eric Dumazet   fasync: RCU and f...
873
874
  
  		spin_lock_irq(&fa->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
875
  		fa->fa_fd = fd;
989a29792   Eric Dumazet   fasync: RCU and f...
876
  		spin_unlock_irq(&fa->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
877
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
  	}
989a29792   Eric Dumazet   fasync: RCU and f...
879
  	spin_lock_init(&new->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
880
881
882
883
  	new->magic = FASYNC_MAGIC;
  	new->fa_file = filp;
  	new->fa_fd = fd;
  	new->fa_next = *fapp;
989a29792   Eric Dumazet   fasync: RCU and f...
884
  	rcu_assign_pointer(*fapp, new);
53281b6d3   Linus Torvalds   fasync: split 'fa...
885
  	filp->f_flags |= FASYNC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
886
  out:
989a29792   Eric Dumazet   fasync: RCU and f...
887
  	spin_unlock(&fasync_lock);
4a6a44996   Jonathan Corbet   Fix a lockdep war...
888
  	spin_unlock(&filp->f_lock);
f7347ce4e   Linus Torvalds   fasync: re-organi...
889
890
891
892
893
894
  	return fa;
  }
  
  /*
   * Add a fasync entry. Return negative on error, positive if
   * added, and zero if did nothing but change an existing one.
f7347ce4e   Linus Torvalds   fasync: re-organi...
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
   */
  static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  {
  	struct fasync_struct *new;
  
  	new = fasync_alloc();
  	if (!new)
  		return -ENOMEM;
  
  	/*
  	 * fasync_insert_entry() returns the old (update) entry if
  	 * it existed.
  	 *
  	 * So free the (unused) new entry and return 0 to let the
  	 * caller know that we didn't add any new fasync entries.
  	 */
  	if (fasync_insert_entry(fd, filp, fapp, new)) {
  		fasync_free(new);
  		return 0;
  	}
  
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
  }
53281b6d3   Linus Torvalds   fasync: split 'fa...
918
919
920
921
922
923
924
925
926
927
928
929
  /*
   * fasync_helper() is used by almost all character device drivers
   * to set up the fasync queue, and for regular files by the file
   * lease code. It returns negative on error, 0 if it did no changes
   * and positive if it added/deleted the entry.
   */
  int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  {
  	if (!on)
  		return fasync_remove_entry(filp, fapp);
  	return fasync_add_entry(fd, filp, fapp);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930
  EXPORT_SYMBOL(fasync_helper);
989a29792   Eric Dumazet   fasync: RCU and f...
931
932
933
934
  /*
   * rcu_read_lock() is held
   */
  static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
936
  {
  	while (fa) {
989a29792   Eric Dumazet   fasync: RCU and f...
937
  		struct fown_struct *fown;
f4985dc71   Andrew Morton   fs/fcntl.c:kill_f...
938
  		unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
940
941
942
943
944
  		if (fa->magic != FASYNC_MAGIC) {
  			printk(KERN_ERR "kill_fasync: bad magic number in "
  			       "fasync_struct!
  ");
  			return;
  		}
f4985dc71   Andrew Morton   fs/fcntl.c:kill_f...
945
  		spin_lock_irqsave(&fa->fa_lock, flags);
989a29792   Eric Dumazet   fasync: RCU and f...
946
947
948
949
950
951
952
953
  		if (fa->fa_file) {
  			fown = &fa->fa_file->f_owner;
  			/* Don't send SIGURG to processes which have not set a
  			   queued signum: SIGURG has its own default signalling
  			   mechanism. */
  			if (!(sig == SIGURG && fown->signum == 0))
  				send_sigio(fown, fa->fa_fd, band);
  		}
f4985dc71   Andrew Morton   fs/fcntl.c:kill_f...
954
  		spin_unlock_irqrestore(&fa->fa_lock, flags);
989a29792   Eric Dumazet   fasync: RCU and f...
955
  		fa = rcu_dereference(fa->fa_next);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
956
957
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
958
959
960
961
962
963
  void kill_fasync(struct fasync_struct **fp, int sig, int band)
  {
  	/* First a quick test without locking: usually
  	 * the list is empty.
  	 */
  	if (*fp) {
989a29792   Eric Dumazet   fasync: RCU and f...
964
965
966
  		rcu_read_lock();
  		kill_fasync_rcu(rcu_dereference(*fp), sig, band);
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
968
969
  	}
  }
  EXPORT_SYMBOL(kill_fasync);
454eedb89   Wu Fengguang   vfs: O_* bit numb...
970
  static int __init fcntl_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
  {
3ab04d5cf   James Bottomley   vfs: take O_NONBL...
972
973
974
975
976
  	/*
  	 * Please add new bits here to ensure allocation uniqueness.
  	 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
  	 * is defined as O_NONBLOCK on some platforms and not on others.
  	 */
80f18379a   Christoph Hellwig   fs: add a VALID_O...
977
978
979
980
  	BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
  		HWEIGHT32(
  			(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
  			__FMODE_EXEC | __FMODE_NONOTIFY));
454eedb89   Wu Fengguang   vfs: O_* bit numb...
981

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
  	fasync_cache = kmem_cache_create("fasync_cache",
20c2df83d   Paul Mundt   mm: Remove slab d...
983
  		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
984
985
  	return 0;
  }
454eedb89   Wu Fengguang   vfs: O_* bit numb...
986
  module_init(fcntl_init)