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