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