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