]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/fcntl.c
drbd: Drop unnecessary static
[mirror_ubuntu-artful-kernel.git] / fs / fcntl.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/fcntl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/syscalls.h>
8#include <linux/init.h>
9#include <linux/mm.h>
29930025 10#include <linux/sched/task.h>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/file.h>
9f3acc31 13#include <linux/fdtable.h>
16f7e0fe 14#include <linux/capability.h>
1da177e4 15#include <linux/dnotify.h>
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/module.h>
35f3d14d 18#include <linux/pipe_fs_i.h>
1da177e4
LT
19#include <linux/security.h>
20#include <linux/ptrace.h>
7ed20e1a 21#include <linux/signal.h>
ab2af1f5 22#include <linux/rcupdate.h>
b488893a 23#include <linux/pid_namespace.h>
1d151c33 24#include <linux/user_namespace.h>
40e041a2 25#include <linux/shmem_fs.h>
80f0cce6 26#include <linux/compat.h>
1da177e4
LT
27
28#include <asm/poll.h>
29#include <asm/siginfo.h>
7c0f6ba6 30#include <linux/uaccess.h>
1da177e4 31
76398425 32#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
1da177e4
LT
33
34static int setfl(int fd, struct file * filp, unsigned long arg)
35{
496ad9aa 36 struct inode * inode = file_inode(filp);
1da177e4
LT
37 int error = 0;
38
7d95c8f2 39 /*
40 * O_APPEND cannot be cleared if the file is marked as append-only
41 * and the file is open for write.
42 */
43 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
1da177e4
LT
44 return -EPERM;
45
46 /* O_NOATIME can only be set by the owner or superuser */
47 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
2e149670 48 if (!inode_owner_or_capable(inode))
1da177e4
LT
49 return -EPERM;
50
51 /* required for strict SunOS emulation */
52 if (O_NONBLOCK != O_NDELAY)
53 if (arg & O_NDELAY)
54 arg |= O_NONBLOCK;
55
0dbf5f20 56 /* Pipe packetized mode is controlled by O_DIRECT flag */
45063097 57 if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) {
1da177e4
LT
58 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
59 !filp->f_mapping->a_ops->direct_IO)
60 return -EINVAL;
61 }
62
72c2d531 63 if (filp->f_op->check_flags)
1da177e4
LT
64 error = filp->f_op->check_flags(arg);
65 if (error)
66 return error;
67
218d11a8 68 /*
76398425 69 * ->fasync() is responsible for setting the FASYNC bit.
218d11a8 70 */
72c2d531 71 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
76398425
JC
72 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
73 if (error < 0)
74 goto out;
60aa4924
JC
75 if (error > 0)
76 error = 0;
1da177e4 77 }
db1dd4d3 78 spin_lock(&filp->f_lock);
1da177e4 79 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
db1dd4d3 80 spin_unlock(&filp->f_lock);
76398425 81
1da177e4 82 out:
1da177e4
LT
83 return error;
84}
85
609d7fa9 86static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
2f38d70f 87 int force)
1da177e4 88{
80e1e823 89 write_lock_irq(&filp->f_owner.lock);
1da177e4 90 if (force || !filp->f_owner.pid) {
609d7fa9
EB
91 put_pid(filp->f_owner.pid);
92 filp->f_owner.pid = get_pid(pid);
93 filp->f_owner.pid_type = type;
2f38d70f
ON
94
95 if (pid) {
96 const struct cred *cred = current_cred();
97 filp->f_owner.uid = cred->uid;
98 filp->f_owner.euid = cred->euid;
99 }
1da177e4 100 }
80e1e823 101 write_unlock_irq(&filp->f_owner.lock);
1da177e4
LT
102}
103
e0b93edd 104void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
609d7fa9 105 int force)
1da177e4 106{
e0b93edd 107 security_file_set_fowner(filp);
2f38d70f 108 f_modown(filp, pid, type, force);
1da177e4 109}
609d7fa9 110EXPORT_SYMBOL(__f_setown);
1da177e4 111
e0b93edd 112void f_setown(struct file *filp, unsigned long arg, int force)
609d7fa9
EB
113{
114 enum pid_type type;
115 struct pid *pid;
116 int who = arg;
609d7fa9
EB
117 type = PIDTYPE_PID;
118 if (who < 0) {
119 type = PIDTYPE_PGID;
120 who = -who;
121 }
122 rcu_read_lock();
b488893a 123 pid = find_vpid(who);
e0b93edd 124 __f_setown(filp, pid, type, force);
609d7fa9 125 rcu_read_unlock();
609d7fa9 126}
1da177e4
LT
127EXPORT_SYMBOL(f_setown);
128
129void f_delown(struct file *filp)
130{
2f38d70f 131 f_modown(filp, NULL, PIDTYPE_PID, 1);
609d7fa9
EB
132}
133
134pid_t f_getown(struct file *filp)
135{
136 pid_t pid;
43fa1adb 137 read_lock(&filp->f_owner.lock);
6c5f3e7b 138 pid = pid_vnr(filp->f_owner.pid);
609d7fa9
EB
139 if (filp->f_owner.pid_type == PIDTYPE_PGID)
140 pid = -pid;
43fa1adb 141 read_unlock(&filp->f_owner.lock);
609d7fa9 142 return pid;
1da177e4
LT
143}
144
ba0a6c9f
PZ
145static int f_setown_ex(struct file *filp, unsigned long arg)
146{
63784dd0 147 struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f
PZ
148 struct f_owner_ex owner;
149 struct pid *pid;
150 int type;
151 int ret;
152
153 ret = copy_from_user(&owner, owner_p, sizeof(owner));
154 if (ret)
5b54470d 155 return -EFAULT;
ba0a6c9f
PZ
156
157 switch (owner.type) {
158 case F_OWNER_TID:
159 type = PIDTYPE_MAX;
160 break;
161
162 case F_OWNER_PID:
163 type = PIDTYPE_PID;
164 break;
165
978b4053 166 case F_OWNER_PGRP:
ba0a6c9f
PZ
167 type = PIDTYPE_PGID;
168 break;
169
170 default:
171 return -EINVAL;
172 }
173
174 rcu_read_lock();
175 pid = find_vpid(owner.pid);
176 if (owner.pid && !pid)
177 ret = -ESRCH;
178 else
e0b93edd 179 __f_setown(filp, pid, type, 1);
ba0a6c9f
PZ
180 rcu_read_unlock();
181
182 return ret;
183}
184
185static int f_getown_ex(struct file *filp, unsigned long arg)
186{
63784dd0 187 struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f
PZ
188 struct f_owner_ex owner;
189 int ret = 0;
190
191 read_lock(&filp->f_owner.lock);
192 owner.pid = pid_vnr(filp->f_owner.pid);
193 switch (filp->f_owner.pid_type) {
194 case PIDTYPE_MAX:
195 owner.type = F_OWNER_TID;
196 break;
197
198 case PIDTYPE_PID:
199 owner.type = F_OWNER_PID;
200 break;
201
202 case PIDTYPE_PGID:
978b4053 203 owner.type = F_OWNER_PGRP;
ba0a6c9f
PZ
204 break;
205
206 default:
207 WARN_ON(1);
208 ret = -EINVAL;
209 break;
210 }
211 read_unlock(&filp->f_owner.lock);
212
5b54470d 213 if (!ret) {
ba0a6c9f 214 ret = copy_to_user(owner_p, &owner, sizeof(owner));
5b54470d
DC
215 if (ret)
216 ret = -EFAULT;
217 }
ba0a6c9f
PZ
218 return ret;
219}
220
1d151c33
CG
221#ifdef CONFIG_CHECKPOINT_RESTORE
222static int f_getowner_uids(struct file *filp, unsigned long arg)
223{
224 struct user_namespace *user_ns = current_user_ns();
63784dd0 225 uid_t __user *dst = (void __user *)arg;
1d151c33
CG
226 uid_t src[2];
227 int err;
228
229 read_lock(&filp->f_owner.lock);
230 src[0] = from_kuid(user_ns, filp->f_owner.uid);
231 src[1] = from_kuid(user_ns, filp->f_owner.euid);
232 read_unlock(&filp->f_owner.lock);
233
234 err = put_user(src[0], &dst[0]);
235 err |= put_user(src[1], &dst[1]);
236
237 return err;
238}
239#else
240static int f_getowner_uids(struct file *filp, unsigned long arg)
241{
242 return -EINVAL;
243}
244#endif
245
c75b1d94
JA
246static bool rw_hint_valid(enum rw_hint hint)
247{
248 switch (hint) {
249 case RWF_WRITE_LIFE_NOT_SET:
250 case RWH_WRITE_LIFE_NONE:
251 case RWH_WRITE_LIFE_SHORT:
252 case RWH_WRITE_LIFE_MEDIUM:
253 case RWH_WRITE_LIFE_LONG:
254 case RWH_WRITE_LIFE_EXTREME:
255 return true;
256 default:
257 return false;
258 }
259}
260
261static long fcntl_rw_hint(struct file *file, unsigned int cmd,
262 unsigned long arg)
263{
264 struct inode *inode = file_inode(file);
265 u64 *argp = (u64 __user *)arg;
266 enum rw_hint hint;
267
268 switch (cmd) {
269 case F_GET_FILE_RW_HINT:
270 if (put_user(file_write_hint(file), argp))
271 return -EFAULT;
272 return 0;
273 case F_SET_FILE_RW_HINT:
274 if (get_user(hint, argp))
275 return -EFAULT;
276 if (!rw_hint_valid(hint))
277 return -EINVAL;
278
279 spin_lock(&file->f_lock);
280 file->f_write_hint = hint;
281 spin_unlock(&file->f_lock);
282 return 0;
283 case F_GET_RW_HINT:
284 if (put_user(inode->i_write_hint, argp))
285 return -EFAULT;
286 return 0;
287 case F_SET_RW_HINT:
288 if (get_user(hint, argp))
289 return -EFAULT;
290 if (!rw_hint_valid(hint))
291 return -EINVAL;
292
293 inode_lock(inode);
294 inode->i_write_hint = hint;
295 inode_unlock(inode);
296 return 0;
297 default:
298 return -EINVAL;
299 }
300}
301
1da177e4
LT
302static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
303 struct file *filp)
304{
305 long err = -EINVAL;
306
307 switch (cmd) {
308 case F_DUPFD:
fe17f22d
AV
309 err = f_dupfd(arg, filp, 0);
310 break;
22d2b35b 311 case F_DUPFD_CLOEXEC:
12197718 312 err = f_dupfd(arg, filp, O_CLOEXEC);
1da177e4
LT
313 break;
314 case F_GETFD:
315 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
316 break;
317 case F_SETFD:
318 err = 0;
319 set_close_on_exec(fd, arg & FD_CLOEXEC);
320 break;
321 case F_GETFL:
322 err = filp->f_flags;
323 break;
324 case F_SETFL:
325 err = setfl(fd, filp, arg);
326 break;
5d50ffd7
JL
327#if BITS_PER_LONG != 32
328 /* 32-bit arches must use fcntl64() */
0d3f7a2d 329 case F_OFD_GETLK:
5d50ffd7 330#endif
1da177e4 331 case F_GETLK:
c1e62b8f 332 err = fcntl_getlk(filp, cmd, (struct flock __user *) arg);
1da177e4 333 break;
5d50ffd7
JL
334#if BITS_PER_LONG != 32
335 /* 32-bit arches must use fcntl64() */
0d3f7a2d
JL
336 case F_OFD_SETLK:
337 case F_OFD_SETLKW:
5d50ffd7
JL
338#endif
339 /* Fallthrough */
1da177e4
LT
340 case F_SETLK:
341 case F_SETLKW:
c293621b 342 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
1da177e4
LT
343 break;
344 case F_GETOWN:
345 /*
346 * XXX If f_owner is a process group, the
347 * negative return value will get converted
348 * into an error. Oops. If we keep the
349 * current syscall conventions, the only way
350 * to fix this will be in libc.
351 */
609d7fa9 352 err = f_getown(filp);
1da177e4
LT
353 force_successful_syscall_return();
354 break;
355 case F_SETOWN:
e0b93edd
JL
356 f_setown(filp, arg, 1);
357 err = 0;
1da177e4 358 break;
ba0a6c9f
PZ
359 case F_GETOWN_EX:
360 err = f_getown_ex(filp, arg);
361 break;
362 case F_SETOWN_EX:
363 err = f_setown_ex(filp, arg);
364 break;
1d151c33
CG
365 case F_GETOWNER_UIDS:
366 err = f_getowner_uids(filp, arg);
367 break;
1da177e4
LT
368 case F_GETSIG:
369 err = filp->f_owner.signum;
370 break;
371 case F_SETSIG:
372 /* arg == 0 restores default behaviour. */
7ed20e1a 373 if (!valid_signal(arg)) {
1da177e4
LT
374 break;
375 }
376 err = 0;
377 filp->f_owner.signum = arg;
378 break;
379 case F_GETLEASE:
380 err = fcntl_getlease(filp);
381 break;
382 case F_SETLEASE:
383 err = fcntl_setlease(fd, filp, arg);
384 break;
385 case F_NOTIFY:
386 err = fcntl_dirnotify(fd, filp, arg);
387 break;
35f3d14d
JA
388 case F_SETPIPE_SZ:
389 case F_GETPIPE_SZ:
390 err = pipe_fcntl(filp, cmd, arg);
391 break;
40e041a2
DH
392 case F_ADD_SEALS:
393 case F_GET_SEALS:
394 err = shmem_fcntl(filp, cmd, arg);
395 break;
c75b1d94
JA
396 case F_GET_RW_HINT:
397 case F_SET_RW_HINT:
398 case F_GET_FILE_RW_HINT:
399 case F_SET_FILE_RW_HINT:
400 err = fcntl_rw_hint(filp, cmd, arg);
401 break;
1da177e4
LT
402 default:
403 break;
404 }
405 return err;
406}
407
1abf0c71
AV
408static int check_fcntl_cmd(unsigned cmd)
409{
410 switch (cmd) {
411 case F_DUPFD:
412 case F_DUPFD_CLOEXEC:
413 case F_GETFD:
414 case F_SETFD:
415 case F_GETFL:
416 return 1;
417 }
418 return 0;
419}
420
a26eab24 421SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
1da177e4 422{
2903ff01 423 struct fd f = fdget_raw(fd);
1da177e4
LT
424 long err = -EBADF;
425
2903ff01 426 if (!f.file)
1da177e4
LT
427 goto out;
428
2903ff01 429 if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c7
AV
430 if (!check_fcntl_cmd(cmd))
431 goto out1;
1abf0c71
AV
432 }
433
2903ff01 434 err = security_file_fcntl(f.file, cmd, arg);
545ec2c7 435 if (!err)
2903ff01 436 err = do_fcntl(fd, cmd, arg, f.file);
1da177e4 437
545ec2c7 438out1:
2903ff01 439 fdput(f);
1da177e4
LT
440out:
441 return err;
442}
443
444#if BITS_PER_LONG == 32
a26eab24
HC
445SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
446 unsigned long, arg)
1da177e4 447{
2903ff01 448 struct fd f = fdget_raw(fd);
545ec2c7 449 long err = -EBADF;
1da177e4 450
2903ff01 451 if (!f.file)
1da177e4
LT
452 goto out;
453
2903ff01 454 if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c7
AV
455 if (!check_fcntl_cmd(cmd))
456 goto out1;
1abf0c71
AV
457 }
458
2903ff01 459 err = security_file_fcntl(f.file, cmd, arg);
545ec2c7
AV
460 if (err)
461 goto out1;
1da177e4
LT
462
463 switch (cmd) {
5d50ffd7 464 case F_GETLK64:
0d3f7a2d 465 case F_OFD_GETLK:
5d50ffd7
JL
466 err = fcntl_getlk64(f.file, cmd, (struct flock64 __user *) arg);
467 break;
468 case F_SETLK64:
469 case F_SETLKW64:
0d3f7a2d
JL
470 case F_OFD_SETLK:
471 case F_OFD_SETLKW:
5d50ffd7
JL
472 err = fcntl_setlk64(fd, f.file, cmd,
473 (struct flock64 __user *) arg);
474 break;
475 default:
476 err = do_fcntl(fd, cmd, arg, f.file);
477 break;
1da177e4 478 }
545ec2c7 479out1:
2903ff01 480 fdput(f);
1da177e4
LT
481out:
482 return err;
483}
484#endif
485
80f0cce6
AV
486#ifdef CONFIG_COMPAT
487static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
488{
489 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
490 __get_user(kfl->l_type, &ufl->l_type) ||
491 __get_user(kfl->l_whence, &ufl->l_whence) ||
492 __get_user(kfl->l_start, &ufl->l_start) ||
493 __get_user(kfl->l_len, &ufl->l_len) ||
494 __get_user(kfl->l_pid, &ufl->l_pid))
495 return -EFAULT;
496 return 0;
497}
498
499static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
500{
501 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
502 __put_user(kfl->l_type, &ufl->l_type) ||
503 __put_user(kfl->l_whence, &ufl->l_whence) ||
504 __put_user(kfl->l_start, &ufl->l_start) ||
505 __put_user(kfl->l_len, &ufl->l_len) ||
506 __put_user(kfl->l_pid, &ufl->l_pid))
507 return -EFAULT;
508 return 0;
509}
510
511#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
512static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
513{
514 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
515 __get_user(kfl->l_type, &ufl->l_type) ||
516 __get_user(kfl->l_whence, &ufl->l_whence) ||
517 __get_user(kfl->l_start, &ufl->l_start) ||
518 __get_user(kfl->l_len, &ufl->l_len) ||
519 __get_user(kfl->l_pid, &ufl->l_pid))
520 return -EFAULT;
521 return 0;
522}
523#endif
524
525#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
526static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
527{
528 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
529 __put_user(kfl->l_type, &ufl->l_type) ||
530 __put_user(kfl->l_whence, &ufl->l_whence) ||
531 __put_user(kfl->l_start, &ufl->l_start) ||
532 __put_user(kfl->l_len, &ufl->l_len) ||
533 __put_user(kfl->l_pid, &ufl->l_pid))
534 return -EFAULT;
535 return 0;
536}
537#endif
538
539static unsigned int
540convert_fcntl_cmd(unsigned int cmd)
541{
542 switch (cmd) {
543 case F_GETLK64:
544 return F_GETLK;
545 case F_SETLK64:
546 return F_SETLK;
547 case F_SETLKW64:
548 return F_SETLKW;
549 }
550
551 return cmd;
552}
553
554COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
555 compat_ulong_t, arg)
556{
557 mm_segment_t old_fs;
558 struct flock f;
559 long ret;
560 unsigned int conv_cmd;
561
562 switch (cmd) {
563 case F_GETLK:
564 case F_SETLK:
565 case F_SETLKW:
566 ret = get_compat_flock(&f, compat_ptr(arg));
567 if (ret != 0)
568 break;
569 old_fs = get_fs();
570 set_fs(KERNEL_DS);
571 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
572 set_fs(old_fs);
573 if (cmd == F_GETLK && ret == 0) {
574 /* GETLK was successful and we need to return the data...
575 * but it needs to fit in the compat structure.
576 * l_start shouldn't be too big, unless the original
577 * start + end is greater than COMPAT_OFF_T_MAX, in which
578 * case the app was asking for trouble, so we return
579 * -EOVERFLOW in that case.
580 * l_len could be too big, in which case we just truncate it,
581 * and only allow the app to see that part of the conflicting
582 * lock that might make sense to it anyway
583 */
584
585 if (f.l_start > COMPAT_OFF_T_MAX)
586 ret = -EOVERFLOW;
587 if (f.l_len > COMPAT_OFF_T_MAX)
588 f.l_len = COMPAT_OFF_T_MAX;
589 if (ret == 0)
590 ret = put_compat_flock(&f, compat_ptr(arg));
591 }
592 break;
593
594 case F_GETLK64:
595 case F_SETLK64:
596 case F_SETLKW64:
597 case F_OFD_GETLK:
598 case F_OFD_SETLK:
599 case F_OFD_SETLKW:
600 ret = get_compat_flock64(&f, compat_ptr(arg));
601 if (ret != 0)
602 break;
603 old_fs = get_fs();
604 set_fs(KERNEL_DS);
605 conv_cmd = convert_fcntl_cmd(cmd);
606 ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
607 set_fs(old_fs);
608 if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
609 /* need to return lock information - see above for commentary */
610 if (f.l_start > COMPAT_LOFF_T_MAX)
611 ret = -EOVERFLOW;
612 if (f.l_len > COMPAT_LOFF_T_MAX)
613 f.l_len = COMPAT_LOFF_T_MAX;
614 if (ret == 0)
615 ret = put_compat_flock64(&f, compat_ptr(arg));
616 }
617 break;
618
619 default:
620 ret = sys_fcntl(fd, cmd, arg);
621 break;
622 }
623 return ret;
624}
625
626COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
627 compat_ulong_t, arg)
628{
629 switch (cmd) {
630 case F_GETLK64:
631 case F_SETLK64:
632 case F_SETLKW64:
633 case F_OFD_GETLK:
634 case F_OFD_SETLK:
635 case F_OFD_SETLKW:
636 return -EINVAL;
637 }
638 return compat_sys_fcntl64(fd, cmd, arg);
639}
640#endif
641
1da177e4
LT
642/* Table to convert sigio signal codes into poll band bitmaps */
643
fa3536cc 644static const long band_table[NSIGPOLL] = {
1da177e4
LT
645 POLLIN | POLLRDNORM, /* POLL_IN */
646 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
647 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
648 POLLERR, /* POLL_ERR */
649 POLLPRI | POLLRDBAND, /* POLL_PRI */
650 POLLHUP | POLLERR /* POLL_HUP */
651};
652
653static inline int sigio_perm(struct task_struct *p,
654 struct fown_struct *fown, int sig)
655{
c69e8d9c
DH
656 const struct cred *cred;
657 int ret;
658
659 rcu_read_lock();
660 cred = __task_cred(p);
8e96e3b7
EB
661 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
662 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
663 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
c69e8d9c
DH
664 !security_file_send_sigiotask(p, fown, sig));
665 rcu_read_unlock();
666 return ret;
1da177e4
LT
667}
668
669static void send_sigio_to_task(struct task_struct *p,
8eeee4e2 670 struct fown_struct *fown,
ba0a6c9f 671 int fd, int reason, int group)
1da177e4 672{
8eeee4e2
ON
673 /*
674 * F_SETSIG can change ->signum lockless in parallel, make
675 * sure we read it once and use the same value throughout.
676 */
677 int signum = ACCESS_ONCE(fown->signum);
678
679 if (!sigio_perm(p, fown, signum))
1da177e4
LT
680 return;
681
8eeee4e2 682 switch (signum) {
1da177e4
LT
683 siginfo_t si;
684 default:
685 /* Queue a rt signal with the appropriate fd as its
686 value. We use SI_SIGIO as the source, not
687 SI_KERNEL, since kernel signals always get
688 delivered even if we can't queue. Failure to
689 queue in this case _should_ be reported; we fall
690 back to SIGIO in that case. --sct */
8eeee4e2 691 si.si_signo = signum;
1da177e4
LT
692 si.si_errno = 0;
693 si.si_code = reason;
694 /* Make sure we are called with one of the POLL_*
695 reasons, otherwise we could leak kernel stack into
696 userspace. */
f6298aab 697 BUG_ON((reason & __SI_MASK) != __SI_POLL);
1da177e4
LT
698 if (reason - POLL_IN >= NSIGPOLL)
699 si.si_band = ~0L;
700 else
701 si.si_band = band_table[reason - POLL_IN];
702 si.si_fd = fd;
ba0a6c9f 703 if (!do_send_sig_info(signum, &si, p, group))
1da177e4
LT
704 break;
705 /* fall-through: fall back on the old plain SIGIO signal */
706 case 0:
ba0a6c9f 707 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
1da177e4
LT
708 }
709}
710
711void send_sigio(struct fown_struct *fown, int fd, int band)
712{
713 struct task_struct *p;
609d7fa9
EB
714 enum pid_type type;
715 struct pid *pid;
ba0a6c9f 716 int group = 1;
1da177e4
LT
717
718 read_lock(&fown->lock);
ba0a6c9f 719
609d7fa9 720 type = fown->pid_type;
ba0a6c9f
PZ
721 if (type == PIDTYPE_MAX) {
722 group = 0;
723 type = PIDTYPE_PID;
724 }
725
1da177e4
LT
726 pid = fown->pid;
727 if (!pid)
728 goto out_unlock_fown;
729
730 read_lock(&tasklist_lock);
609d7fa9 731 do_each_pid_task(pid, type, p) {
ba0a6c9f 732 send_sigio_to_task(p, fown, fd, band, group);
609d7fa9 733 } while_each_pid_task(pid, type, p);
1da177e4
LT
734 read_unlock(&tasklist_lock);
735 out_unlock_fown:
736 read_unlock(&fown->lock);
737}
738
739static void send_sigurg_to_task(struct task_struct *p,
ba0a6c9f 740 struct fown_struct *fown, int group)
1da177e4
LT
741{
742 if (sigio_perm(p, fown, SIGURG))
ba0a6c9f 743 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
1da177e4
LT
744}
745
746int send_sigurg(struct fown_struct *fown)
747{
748 struct task_struct *p;
609d7fa9
EB
749 enum pid_type type;
750 struct pid *pid;
ba0a6c9f 751 int group = 1;
609d7fa9 752 int ret = 0;
1da177e4
LT
753
754 read_lock(&fown->lock);
ba0a6c9f 755
609d7fa9 756 type = fown->pid_type;
ba0a6c9f
PZ
757 if (type == PIDTYPE_MAX) {
758 group = 0;
759 type = PIDTYPE_PID;
760 }
761
1da177e4
LT
762 pid = fown->pid;
763 if (!pid)
764 goto out_unlock_fown;
765
766 ret = 1;
767
768 read_lock(&tasklist_lock);
609d7fa9 769 do_each_pid_task(pid, type, p) {
ba0a6c9f 770 send_sigurg_to_task(p, fown, group);
609d7fa9 771 } while_each_pid_task(pid, type, p);
1da177e4
LT
772 read_unlock(&tasklist_lock);
773 out_unlock_fown:
774 read_unlock(&fown->lock);
775 return ret;
776}
777
989a2979 778static DEFINE_SPINLOCK(fasync_lock);
e18b890b 779static struct kmem_cache *fasync_cache __read_mostly;
1da177e4 780
989a2979
ED
781static void fasync_free_rcu(struct rcu_head *head)
782{
783 kmem_cache_free(fasync_cache,
784 container_of(head, struct fasync_struct, fa_rcu));
785}
786
1da177e4 787/*
53281b6d
LT
788 * Remove a fasync entry. If successfully removed, return
789 * positive and clear the FASYNC flag. If no entry exists,
790 * do nothing and return 0.
791 *
792 * NOTE! It is very important that the FASYNC flag always
793 * match the state "is the filp on a fasync list".
794 *
1da177e4 795 */
f7347ce4 796int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
1da177e4
LT
797{
798 struct fasync_struct *fa, **fp;
1da177e4
LT
799 int result = 0;
800
53281b6d 801 spin_lock(&filp->f_lock);
989a2979 802 spin_lock(&fasync_lock);
53281b6d
LT
803 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
804 if (fa->fa_file != filp)
805 continue;
989a2979
ED
806
807 spin_lock_irq(&fa->fa_lock);
808 fa->fa_file = NULL;
809 spin_unlock_irq(&fa->fa_lock);
810
53281b6d 811 *fp = fa->fa_next;
989a2979 812 call_rcu(&fa->fa_rcu, fasync_free_rcu);
53281b6d
LT
813 filp->f_flags &= ~FASYNC;
814 result = 1;
815 break;
1da177e4 816 }
989a2979 817 spin_unlock(&fasync_lock);
53281b6d
LT
818 spin_unlock(&filp->f_lock);
819 return result;
820}
821
f7347ce4
LT
822struct fasync_struct *fasync_alloc(void)
823{
824 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
825}
826
53281b6d 827/*
f7347ce4
LT
828 * NOTE! This can be used only for unused fasync entries:
829 * entries that actually got inserted on the fasync list
830 * need to be released by rcu - see fasync_remove_entry.
53281b6d 831 */
f7347ce4 832void fasync_free(struct fasync_struct *new)
53281b6d 833{
f7347ce4
LT
834 kmem_cache_free(fasync_cache, new);
835}
53281b6d 836
f7347ce4
LT
837/*
838 * Insert a new entry into the fasync list. Return the pointer to the
839 * old one if we didn't use the new one.
55f335a8
LT
840 *
841 * NOTE! It is very important that the FASYNC flag always
842 * match the state "is the filp on a fasync list".
f7347ce4
LT
843 */
844struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
845{
846 struct fasync_struct *fa, **fp;
4a6a4499 847
4a6a4499 848 spin_lock(&filp->f_lock);
989a2979 849 spin_lock(&fasync_lock);
1da177e4 850 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
53281b6d
LT
851 if (fa->fa_file != filp)
852 continue;
989a2979
ED
853
854 spin_lock_irq(&fa->fa_lock);
53281b6d 855 fa->fa_fd = fd;
989a2979 856 spin_unlock_irq(&fa->fa_lock);
53281b6d 857 goto out;
1da177e4
LT
858 }
859
989a2979 860 spin_lock_init(&new->fa_lock);
53281b6d
LT
861 new->magic = FASYNC_MAGIC;
862 new->fa_file = filp;
863 new->fa_fd = fd;
864 new->fa_next = *fapp;
989a2979 865 rcu_assign_pointer(*fapp, new);
53281b6d
LT
866 filp->f_flags |= FASYNC;
867
1da177e4 868out:
989a2979 869 spin_unlock(&fasync_lock);
4a6a4499 870 spin_unlock(&filp->f_lock);
f7347ce4
LT
871 return fa;
872}
873
874/*
875 * Add a fasync entry. Return negative on error, positive if
876 * added, and zero if did nothing but change an existing one.
f7347ce4
LT
877 */
878static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
879{
880 struct fasync_struct *new;
881
882 new = fasync_alloc();
883 if (!new)
884 return -ENOMEM;
885
886 /*
887 * fasync_insert_entry() returns the old (update) entry if
888 * it existed.
889 *
890 * So free the (unused) new entry and return 0 to let the
891 * caller know that we didn't add any new fasync entries.
892 */
893 if (fasync_insert_entry(fd, filp, fapp, new)) {
894 fasync_free(new);
895 return 0;
896 }
897
898 return 1;
1da177e4
LT
899}
900
53281b6d
LT
901/*
902 * fasync_helper() is used by almost all character device drivers
903 * to set up the fasync queue, and for regular files by the file
904 * lease code. It returns negative on error, 0 if it did no changes
905 * and positive if it added/deleted the entry.
906 */
907int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
908{
909 if (!on)
910 return fasync_remove_entry(filp, fapp);
911 return fasync_add_entry(fd, filp, fapp);
912}
913
1da177e4
LT
914EXPORT_SYMBOL(fasync_helper);
915
989a2979
ED
916/*
917 * rcu_read_lock() is held
918 */
919static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1da177e4
LT
920{
921 while (fa) {
989a2979 922 struct fown_struct *fown;
f4985dc7
AM
923 unsigned long flags;
924
1da177e4
LT
925 if (fa->magic != FASYNC_MAGIC) {
926 printk(KERN_ERR "kill_fasync: bad magic number in "
927 "fasync_struct!\n");
928 return;
929 }
f4985dc7 930 spin_lock_irqsave(&fa->fa_lock, flags);
989a2979
ED
931 if (fa->fa_file) {
932 fown = &fa->fa_file->f_owner;
933 /* Don't send SIGURG to processes which have not set a
934 queued signum: SIGURG has its own default signalling
935 mechanism. */
936 if (!(sig == SIGURG && fown->signum == 0))
937 send_sigio(fown, fa->fa_fd, band);
938 }
f4985dc7 939 spin_unlock_irqrestore(&fa->fa_lock, flags);
989a2979 940 fa = rcu_dereference(fa->fa_next);
1da177e4
LT
941 }
942}
943
1da177e4
LT
944void kill_fasync(struct fasync_struct **fp, int sig, int band)
945{
946 /* First a quick test without locking: usually
947 * the list is empty.
948 */
949 if (*fp) {
989a2979
ED
950 rcu_read_lock();
951 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
952 rcu_read_unlock();
1da177e4
LT
953 }
954}
955EXPORT_SYMBOL(kill_fasync);
956
454eedb8 957static int __init fcntl_init(void)
1da177e4 958{
3ab04d5c
JB
959 /*
960 * Please add new bits here to ensure allocation uniqueness.
961 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
962 * is defined as O_NONBLOCK on some platforms and not on others.
963 */
80f18379
CH
964 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
965 HWEIGHT32(
966 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
967 __FMODE_EXEC | __FMODE_NONOTIFY));
454eedb8 968
1da177e4 969 fasync_cache = kmem_cache_create("fasync_cache",
20c2df83 970 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1da177e4
LT
971 return 0;
972}
973
454eedb8 974module_init(fcntl_init)