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