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