This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / fcntl.c
1 /*
2  *  linux/fs/fcntl.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/fs.h>
10 #include <linux/file.h>
11 #include <linux/dnotify.h>
12 #include <linux/smp_lock.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/security.h>
16 #include <linux/ptrace.h>
17 #include <linux/vs_limit.h>
18
19 #include <asm/poll.h>
20 #include <asm/siginfo.h>
21 #include <asm/uaccess.h>
22
23 void fastcall set_close_on_exec(unsigned int fd, int flag)
24 {
25         struct files_struct *files = current->files;
26         spin_lock(&files->file_lock);
27         if (flag)
28                 FD_SET(fd, files->close_on_exec);
29         else
30                 FD_CLR(fd, files->close_on_exec);
31         spin_unlock(&files->file_lock);
32 }
33
34 static inline int get_close_on_exec(unsigned int fd)
35 {
36         struct files_struct *files = current->files;
37         int res;
38         spin_lock(&files->file_lock);
39         res = FD_ISSET(fd, files->close_on_exec);
40         spin_unlock(&files->file_lock);
41         return res;
42 }
43
44
45 /* Expand files.  Return <0 on error; 0 nothing done; 1 files expanded,
46  * we may have blocked. 
47  *
48  * Should be called with the files->file_lock spinlock held for write.
49  */
50 static int expand_files(struct files_struct *files, int nr)
51 {
52         int err, expand = 0;
53 #ifdef FDSET_DEBUG      
54         printk (KERN_ERR "%s %d: nr = %d\n", __FUNCTION__, current->pid, nr);
55 #endif
56         
57         if (nr >= files->max_fdset) {
58                 expand = 1;
59                 if ((err = expand_fdset(files, nr)))
60                         goto out;
61         }
62         if (nr >= files->max_fds) {
63                 expand = 1;
64                 if ((err = expand_fd_array(files, nr)))
65                         goto out;
66         }
67         err = expand;
68  out:
69 #ifdef FDSET_DEBUG      
70         if (err)
71                 printk (KERN_ERR "%s %d: return %d\n", __FUNCTION__, current->pid, err);
72 #endif
73         return err;
74 }
75
76 /*
77  * locate_fd finds a free file descriptor in the open_fds fdset,
78  * expanding the fd arrays if necessary.  Must be called with the
79  * file_lock held for write.
80  */
81
82 static int locate_fd(struct files_struct *files, 
83                             struct file *file, unsigned int orig_start)
84 {
85         unsigned int newfd;
86         unsigned int start;
87         int error;
88
89         error = -EINVAL;
90         if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
91                 goto out;
92
93 repeat:
94         /*
95          * Someone might have closed fd's in the range
96          * orig_start..files->next_fd
97          */
98         start = orig_start;
99         if (start < files->next_fd)
100                 start = files->next_fd;
101
102         newfd = start;
103         if (start < files->max_fdset) {
104                 newfd = find_next_zero_bit(files->open_fds->fds_bits,
105                         files->max_fdset, start);
106         }
107         
108         error = -EMFILE;
109         if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
110                 goto out;
111         if (!vx_files_avail(1))
112                 goto out;
113
114         error = expand_files(files, newfd);
115         if (error < 0)
116                 goto out;
117
118         /*
119          * If we needed to expand the fs array we
120          * might have blocked - try again.
121          */
122         if (error)
123                 goto repeat;
124
125         if (start <= files->next_fd)
126                 files->next_fd = newfd + 1;
127         
128         error = newfd;
129         
130 out:
131         return error;
132 }
133
134 static int dupfd(struct file *file, unsigned int start)
135 {
136         struct files_struct * files = current->files;
137         int fd;
138
139         spin_lock(&files->file_lock);
140         fd = locate_fd(files, file, start);
141         if (fd >= 0) {
142                 FD_SET(fd, files->open_fds);
143                 FD_CLR(fd, files->close_on_exec);
144                 spin_unlock(&files->file_lock);
145                 vx_openfd_inc(fd);
146                 fd_install(fd, file);
147         } else {
148                 spin_unlock(&files->file_lock);
149                 fput(file);
150         }
151
152         return fd;
153 }
154
155 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
156 {
157         int err = -EBADF;
158         struct file * file, *tofree;
159         struct files_struct * files = current->files;
160
161         spin_lock(&files->file_lock);
162         if (!(file = fcheck(oldfd)))
163                 goto out_unlock;
164         err = newfd;
165         if (newfd == oldfd)
166                 goto out_unlock;
167         err = -EBADF;
168         if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
169                 goto out_unlock;
170         get_file(file);                 /* We are now finished with oldfd */
171
172         err = expand_files(files, newfd);
173         if (err < 0)
174                 goto out_fput;
175
176         /* To avoid races with open() and dup(), we will mark the fd as
177          * in-use in the open-file bitmap throughout the entire dup2()
178          * process.  This is quite safe: do_close() uses the fd array
179          * entry, not the bitmap, to decide what work needs to be
180          * done.  --sct */
181         /* Doesn't work. open() might be there first. --AV */
182
183         /* Yes. It's a race. In user space. Nothing sane to do */
184         err = -EBUSY;
185         tofree = files->fd[newfd];
186         if (!tofree && FD_ISSET(newfd, files->open_fds))
187                 goto out_fput;
188
189         files->fd[newfd] = file;
190         FD_SET(newfd, files->open_fds);
191         FD_CLR(newfd, files->close_on_exec);
192         spin_unlock(&files->file_lock);
193         vx_openfd_inc(newfd);
194
195         if (tofree)
196                 filp_close(tofree, files);
197         err = newfd;
198 out:
199         return err;
200 out_unlock:
201         spin_unlock(&files->file_lock);
202         goto out;
203
204 out_fput:
205         spin_unlock(&files->file_lock);
206         fput(file);
207         goto out;
208 }
209
210 asmlinkage long sys_dup(unsigned int fildes)
211 {
212         int ret = -EBADF;
213         struct file * file = fget(fildes);
214
215         if (file)
216                 ret = dupfd(file, 0);
217         return ret;
218 }
219
220 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
221
222 static int setfl(int fd, struct file * filp, unsigned long arg)
223 {
224         struct inode * inode = filp->f_dentry->d_inode;
225         int error = 0;
226
227         /* O_APPEND cannot be cleared if the file is marked as append-only */
228         if (!(arg & O_APPEND) && IS_APPEND(inode))
229                 return -EPERM;
230
231         /* O_NOATIME can only be set by the owner or superuser */
232         if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
233                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
234                         return -EPERM;
235
236         /* required for strict SunOS emulation */
237         if (O_NONBLOCK != O_NDELAY)
238                if (arg & O_NDELAY)
239                    arg |= O_NONBLOCK;
240
241         if (arg & O_DIRECT) {
242                 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
243                         !filp->f_mapping->a_ops->direct_IO)
244                                 return -EINVAL;
245         }
246
247         lock_kernel();
248         if ((arg ^ filp->f_flags) & FASYNC) {
249                 if (filp->f_op && filp->f_op->fasync) {
250                         error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
251                         if (error < 0)
252                                 goto out;
253                 }
254         }
255
256         filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
257  out:
258         unlock_kernel();
259         return error;
260 }
261
262 static void f_modown(struct file *filp, unsigned long pid,
263                      uid_t uid, uid_t euid, int force)
264 {
265         write_lock_irq(&filp->f_owner.lock);
266         if (force || !filp->f_owner.pid) {
267                 filp->f_owner.pid = pid;
268                 filp->f_owner.uid = uid;
269                 filp->f_owner.euid = euid;
270         }
271         write_unlock_irq(&filp->f_owner.lock);
272 }
273
274 int f_setown(struct file *filp, unsigned long arg, int force)
275 {
276         int err;
277         
278         err = security_file_set_fowner(filp);
279         if (err)
280                 return err;
281
282         f_modown(filp, arg, current->uid, current->euid, force);
283         return 0;
284 }
285
286 EXPORT_SYMBOL(f_setown);
287
288 void f_delown(struct file *filp)
289 {
290         f_modown(filp, 0, 0, 0, 1);
291 }
292
293 EXPORT_SYMBOL(f_delown);
294
295 long generic_file_fcntl(int fd, unsigned int cmd,
296                         unsigned long arg, struct file *filp)
297 {
298         long err = -EINVAL;
299
300         switch (cmd) {
301         case F_DUPFD:
302                 get_file(filp);
303                 err = dupfd(filp, arg);
304                 break;
305         case F_GETFD:
306                 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
307                 break;
308         case F_SETFD:
309                 err = 0;
310                 set_close_on_exec(fd, arg & FD_CLOEXEC);
311                 break;
312         case F_GETFL:
313                 err = filp->f_flags;
314                 break;
315         case F_SETFL:
316                 err = setfl(fd, filp, arg);
317                 break;
318         case F_GETLK:
319                 err = fcntl_getlk(filp, (struct flock __user *) arg);
320                 break;
321         case F_SETLK:
322         case F_SETLKW:
323                 err = fcntl_setlk(filp, cmd, (struct flock __user *) arg);
324                 break;
325         case F_GETOWN:
326                 /*
327                  * XXX If f_owner is a process group, the
328                  * negative return value will get converted
329                  * into an error.  Oops.  If we keep the
330                  * current syscall conventions, the only way
331                  * to fix this will be in libc.
332                  */
333                 err = filp->f_owner.pid;
334                 force_successful_syscall_return();
335                 break;
336         case F_SETOWN:
337                 err = f_setown(filp, arg, 1);
338                 break;
339         case F_GETSIG:
340                 err = filp->f_owner.signum;
341                 break;
342         case F_SETSIG:
343                 /* arg == 0 restores default behaviour. */
344                 if (arg < 0 || arg > _NSIG) {
345                         break;
346                 }
347                 err = 0;
348                 filp->f_owner.signum = arg;
349                 break;
350         case F_GETLEASE:
351                 err = fcntl_getlease(filp);
352                 break;
353         case F_SETLEASE:
354                 err = fcntl_setlease(fd, filp, arg);
355                 break;
356         case F_NOTIFY:
357                 err = fcntl_dirnotify(fd, filp, arg);
358                 break;
359         default:
360                 break;
361         }
362         return err;
363 }
364 EXPORT_SYMBOL(generic_file_fcntl);
365
366 static long do_fcntl(int fd, unsigned int cmd,
367                         unsigned long arg, struct file *filp)
368 {
369         if (filp->f_op && filp->f_op->fcntl)
370                 return filp->f_op->fcntl(fd, cmd, arg, filp);
371         return generic_file_fcntl(fd, cmd, arg, filp);
372 }
373
374 asmlinkage long sys_fcntl(int fd, unsigned int cmd, unsigned long arg)
375 {       
376         struct file *filp;
377         long err = -EBADF;
378
379         filp = fget(fd);
380         if (!filp)
381                 goto out;
382
383         err = security_file_fcntl(filp, cmd, arg);
384         if (err) {
385                 fput(filp);
386                 return err;
387         }
388
389         err = do_fcntl(fd, cmd, arg, filp);
390
391         fput(filp);
392 out:
393         return err;
394 }
395
396 #if BITS_PER_LONG == 32
397 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
398 {       
399         struct file * filp;
400         long err;
401
402         err = -EBADF;
403         filp = fget(fd);
404         if (!filp)
405                 goto out;
406
407         err = security_file_fcntl(filp, cmd, arg);
408         if (err) {
409                 fput(filp);
410                 return err;
411         }
412         err = -EBADF;
413         
414         switch (cmd) {
415                 case F_GETLK64:
416                         err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
417                         break;
418                 case F_SETLK64:
419                 case F_SETLKW64:
420                         err = fcntl_setlk64(filp, cmd, (struct flock64 __user *) arg);
421                         break;
422                 default:
423                         err = do_fcntl(fd, cmd, arg, filp);
424                         break;
425         }
426         fput(filp);
427 out:
428         return err;
429 }
430 #endif
431
432 /* Table to convert sigio signal codes into poll band bitmaps */
433
434 static long band_table[NSIGPOLL] = {
435         POLLIN | POLLRDNORM,                    /* POLL_IN */
436         POLLOUT | POLLWRNORM | POLLWRBAND,      /* POLL_OUT */
437         POLLIN | POLLRDNORM | POLLMSG,          /* POLL_MSG */
438         POLLERR,                                /* POLL_ERR */
439         POLLPRI | POLLRDBAND,                   /* POLL_PRI */
440         POLLHUP | POLLERR                       /* POLL_HUP */
441 };
442
443 static inline int sigio_perm(struct task_struct *p,
444                              struct fown_struct *fown)
445 {
446         return ((fown->euid == 0) ||
447                 (fown->euid == p->suid) || (fown->euid == p->uid) ||
448                 (fown->uid == p->suid) || (fown->uid == p->uid));
449 }
450
451 static void send_sigio_to_task(struct task_struct *p,
452                                struct fown_struct *fown, 
453                                int fd,
454                                int reason)
455 {
456         if (!sigio_perm(p, fown))
457                 return;
458
459         if (security_file_send_sigiotask(p, fown, fd, reason))
460                 return;
461
462         switch (fown->signum) {
463                 siginfo_t si;
464                 default:
465                         /* Queue a rt signal with the appropriate fd as its
466                            value.  We use SI_SIGIO as the source, not 
467                            SI_KERNEL, since kernel signals always get 
468                            delivered even if we can't queue.  Failure to
469                            queue in this case _should_ be reported; we fall
470                            back to SIGIO in that case. --sct */
471                         si.si_signo = fown->signum;
472                         si.si_errno = 0;
473                         si.si_code  = reason;
474                         /* Make sure we are called with one of the POLL_*
475                            reasons, otherwise we could leak kernel stack into
476                            userspace.  */
477                         if ((reason & __SI_MASK) != __SI_POLL)
478                                 BUG();
479                         if (reason - POLL_IN >= NSIGPOLL)
480                                 si.si_band  = ~0L;
481                         else
482                                 si.si_band = band_table[reason - POLL_IN];
483                         si.si_fd    = fd;
484                         if (!send_sig_info(fown->signum, &si, p))
485                                 break;
486                 /* fall-through: fall back on the old plain SIGIO signal */
487                 case 0:
488                         send_group_sig_info(SIGIO, SEND_SIG_PRIV, p);
489         }
490 }
491
492 void send_sigio(struct fown_struct *fown, int fd, int band)
493 {
494         struct task_struct *p;
495         int pid;
496         
497         read_lock(&fown->lock);
498         pid = fown->pid;
499         if (!pid)
500                 goto out_unlock_fown;
501         
502         read_lock(&tasklist_lock);
503         if (pid > 0) {
504                 p = find_task_by_pid(pid);
505                 if (p) {
506                         send_sigio_to_task(p, fown, fd, band);
507                 }
508         } else {
509                 struct list_head *l;
510                 struct pid *pidptr;
511                 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
512                         send_sigio_to_task(p, fown, fd, band);
513                 }
514         }
515         read_unlock(&tasklist_lock);
516  out_unlock_fown:
517         read_unlock(&fown->lock);
518 }
519
520 static void send_sigurg_to_task(struct task_struct *p,
521                                 struct fown_struct *fown)
522 {
523         if (sigio_perm(p, fown))
524                 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
525 }
526
527 int send_sigurg(struct fown_struct *fown)
528 {
529         struct task_struct *p;
530         int pid, ret = 0;
531         
532         read_lock(&fown->lock);
533         pid = fown->pid;
534         if (!pid)
535                 goto out_unlock_fown;
536
537         ret = 1;
538         
539         read_lock(&tasklist_lock);
540         if (pid > 0) {
541                 p = find_task_by_pid(pid);
542                 if (p) {
543                         send_sigurg_to_task(p, fown);
544                 }
545         } else {
546                 struct list_head *l;
547                 struct pid *pidptr;
548                 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
549                         send_sigurg_to_task(p, fown);
550                 }
551         }
552         read_unlock(&tasklist_lock);
553  out_unlock_fown:
554         read_unlock(&fown->lock);
555         return ret;
556 }
557
558 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
559 static kmem_cache_t *fasync_cache;
560
561 /*
562  * fasync_helper() is used by some character device drivers (mainly mice)
563  * to set up the fasync queue. It returns negative on error, 0 if it did
564  * no changes and positive if it added/deleted the entry.
565  */
566 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
567 {
568         struct fasync_struct *fa, **fp;
569         struct fasync_struct *new = NULL;
570         int result = 0;
571
572         if (on) {
573                 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
574                 if (!new)
575                         return -ENOMEM;
576         }
577         write_lock_irq(&fasync_lock);
578         for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
579                 if (fa->fa_file == filp) {
580                         if(on) {
581                                 fa->fa_fd = fd;
582                                 kmem_cache_free(fasync_cache, new);
583                         } else {
584                                 *fp = fa->fa_next;
585                                 kmem_cache_free(fasync_cache, fa);
586                                 result = 1;
587                         }
588                         goto out;
589                 }
590         }
591
592         if (on) {
593                 new->magic = FASYNC_MAGIC;
594                 new->fa_file = filp;
595                 new->fa_fd = fd;
596                 new->fa_next = *fapp;
597                 *fapp = new;
598                 result = 1;
599         }
600 out:
601         write_unlock_irq(&fasync_lock);
602         return result;
603 }
604
605 EXPORT_SYMBOL(fasync_helper);
606
607 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
608 {
609         while (fa) {
610                 struct fown_struct * fown;
611                 if (fa->magic != FASYNC_MAGIC) {
612                         printk(KERN_ERR "kill_fasync: bad magic number in "
613                                "fasync_struct!\n");
614                         return;
615                 }
616                 fown = &fa->fa_file->f_owner;
617                 /* Don't send SIGURG to processes which have not set a
618                    queued signum: SIGURG has its own default signalling
619                    mechanism. */
620                 if (!(sig == SIGURG && fown->signum == 0))
621                         send_sigio(fown, fa->fa_fd, band);
622                 fa = fa->fa_next;
623         }
624 }
625
626 EXPORT_SYMBOL(__kill_fasync);
627
628 void kill_fasync(struct fasync_struct **fp, int sig, int band)
629 {
630         /* First a quick test without locking: usually
631          * the list is empty.
632          */
633         if (*fp) {
634                 read_lock(&fasync_lock);
635                 /* reread *fp after obtaining the lock */
636                 __kill_fasync(*fp, sig, band);
637                 read_unlock(&fasync_lock);
638         }
639 }
640 EXPORT_SYMBOL(kill_fasync);
641
642 static int __init fasync_init(void)
643 {
644         fasync_cache = kmem_cache_create("fasync_cache",
645                 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL, NULL);
646         return 0;
647 }
648
649 module_init(fasync_init)