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 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 EXPORT_SYMBOL_GPL(dupfd);
156
157 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
158 {
159         int err = -EBADF;
160         struct file * file, *tofree;
161         struct files_struct * files = current->files;
162
163         spin_lock(&files->file_lock);
164         if (!(file = fcheck(oldfd)))
165                 goto out_unlock;
166         err = newfd;
167         if (newfd == oldfd)
168                 goto out_unlock;
169         err = -EBADF;
170         if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
171                 goto out_unlock;
172         get_file(file);                 /* We are now finished with oldfd */
173
174         err = expand_files(files, newfd);
175         if (err < 0)
176                 goto out_fput;
177
178         /* To avoid races with open() and dup(), we will mark the fd as
179          * in-use in the open-file bitmap throughout the entire dup2()
180          * process.  This is quite safe: do_close() uses the fd array
181          * entry, not the bitmap, to decide what work needs to be
182          * done.  --sct */
183         /* Doesn't work. open() might be there first. --AV */
184
185         /* Yes. It's a race. In user space. Nothing sane to do */
186         err = -EBUSY;
187         tofree = files->fd[newfd];
188         if (!tofree && FD_ISSET(newfd, files->open_fds))
189                 goto out_fput;
190
191         files->fd[newfd] = file;
192         FD_SET(newfd, files->open_fds);
193         FD_CLR(newfd, files->close_on_exec);
194         spin_unlock(&files->file_lock);
195         // vx_openfd_inc(newfd);
196
197         if (tofree)
198                 filp_close(tofree, files);
199         err = newfd;
200 out:
201         return err;
202 out_unlock:
203         spin_unlock(&files->file_lock);
204         goto out;
205
206 out_fput:
207         spin_unlock(&files->file_lock);
208         fput(file);
209         goto out;
210 }
211
212 asmlinkage long sys_dup(unsigned int fildes)
213 {
214         int ret = -EBADF;
215         struct file * file = fget(fildes);
216
217         if (file)
218                 ret = dupfd(file, 0);
219         return ret;
220 }
221
222 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
223
224 static int setfl(int fd, struct file * filp, unsigned long arg)
225 {
226         struct inode * inode = filp->f_dentry->d_inode;
227         int error = 0;
228
229         /* O_APPEND cannot be cleared if the file is marked as append-only */
230         if (!(arg & O_APPEND) && IS_APPEND(inode))
231                 return -EPERM;
232
233         /* O_NOATIME can only be set by the owner or superuser */
234         if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
235                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
236                         return -EPERM;
237
238         /* required for strict SunOS emulation */
239         if (O_NONBLOCK != O_NDELAY)
240                if (arg & O_NDELAY)
241                    arg |= O_NONBLOCK;
242
243         if (arg & O_DIRECT) {
244                 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
245                         !filp->f_mapping->a_ops->direct_IO)
246                                 return -EINVAL;
247         }
248
249         if (filp->f_op && filp->f_op->check_flags)
250                 error = filp->f_op->check_flags(arg);
251         if (error)
252                 return error;
253
254         lock_kernel();
255         if ((arg ^ filp->f_flags) & FASYNC) {
256                 if (filp->f_op && filp->f_op->fasync) {
257                         error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
258                         if (error < 0)
259                                 goto out;
260                 }
261         }
262
263         filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
264  out:
265         unlock_kernel();
266         return error;
267 }
268
269 static void f_modown(struct file *filp, unsigned long pid,
270                      uid_t uid, uid_t euid, int force)
271 {
272         write_lock_irq(&filp->f_owner.lock);
273         if (force || !filp->f_owner.pid) {
274                 filp->f_owner.pid = pid;
275                 filp->f_owner.uid = uid;
276                 filp->f_owner.euid = euid;
277         }
278         write_unlock_irq(&filp->f_owner.lock);
279 }
280
281 int f_setown(struct file *filp, unsigned long arg, int force)
282 {
283         int err;
284         
285         err = security_file_set_fowner(filp);
286         if (err)
287                 return err;
288
289         f_modown(filp, arg, current->uid, current->euid, force);
290         return 0;
291 }
292
293 EXPORT_SYMBOL(f_setown);
294
295 void f_delown(struct file *filp)
296 {
297         f_modown(filp, 0, 0, 0, 1);
298 }
299
300 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
301                 struct file *filp)
302 {
303         long err = -EINVAL;
304
305         switch (cmd) {
306         case F_DUPFD:
307                 get_file(filp);
308                 err = dupfd(filp, arg);
309                 break;
310         case F_GETFD:
311                 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
312                 break;
313         case F_SETFD:
314                 err = 0;
315                 set_close_on_exec(fd, arg & FD_CLOEXEC);
316                 break;
317         case F_GETFL:
318                 err = filp->f_flags;
319                 break;
320         case F_SETFL:
321                 err = setfl(fd, filp, arg);
322                 break;
323         case F_GETLK:
324                 err = fcntl_getlk(filp, (struct flock __user *) arg);
325                 break;
326         case F_SETLK:
327         case F_SETLKW:
328                 err = fcntl_setlk(filp, cmd, (struct flock __user *) arg);
329                 break;
330         case F_GETOWN:
331                 /*
332                  * XXX If f_owner is a process group, the
333                  * negative return value will get converted
334                  * into an error.  Oops.  If we keep the
335                  * current syscall conventions, the only way
336                  * to fix this will be in libc.
337                  */
338                 err = filp->f_owner.pid;
339                 force_successful_syscall_return();
340                 break;
341         case F_SETOWN:
342                 err = f_setown(filp, arg, 1);
343                 break;
344         case F_GETSIG:
345                 err = filp->f_owner.signum;
346                 break;
347         case F_SETSIG:
348                 /* arg == 0 restores default behaviour. */
349                 if (arg < 0 || arg > _NSIG) {
350                         break;
351                 }
352                 err = 0;
353                 filp->f_owner.signum = arg;
354                 break;
355         case F_GETLEASE:
356                 err = fcntl_getlease(filp);
357                 break;
358         case F_SETLEASE:
359                 err = fcntl_setlease(fd, filp, arg);
360                 break;
361         case F_NOTIFY:
362                 err = fcntl_dirnotify(fd, filp, arg);
363                 break;
364         default:
365                 break;
366         }
367         return err;
368 }
369
370 asmlinkage long sys_fcntl(int fd, unsigned int cmd, unsigned long arg)
371 {       
372         struct file *filp;
373         long err = -EBADF;
374
375         filp = fget(fd);
376         if (!filp)
377                 goto out;
378
379         err = security_file_fcntl(filp, cmd, arg);
380         if (err) {
381                 fput(filp);
382                 return err;
383         }
384
385         err = do_fcntl(fd, cmd, arg, filp);
386
387         fput(filp);
388 out:
389         return err;
390 }
391
392 #if BITS_PER_LONG == 32
393 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
394 {       
395         struct file * filp;
396         long err;
397
398         err = -EBADF;
399         filp = fget(fd);
400         if (!filp)
401                 goto out;
402
403         err = security_file_fcntl(filp, cmd, arg);
404         if (err) {
405                 fput(filp);
406                 return err;
407         }
408         err = -EBADF;
409         
410         switch (cmd) {
411                 case F_GETLK64:
412                         err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
413                         break;
414                 case F_SETLK64:
415                 case F_SETLKW64:
416                         err = fcntl_setlk64(filp, cmd, (struct flock64 __user *) arg);
417                         break;
418                 default:
419                         err = do_fcntl(fd, cmd, arg, filp);
420                         break;
421         }
422         fput(filp);
423 out:
424         return err;
425 }
426 #endif
427
428 /* Table to convert sigio signal codes into poll band bitmaps */
429
430 static long band_table[NSIGPOLL] = {
431         POLLIN | POLLRDNORM,                    /* POLL_IN */
432         POLLOUT | POLLWRNORM | POLLWRBAND,      /* POLL_OUT */
433         POLLIN | POLLRDNORM | POLLMSG,          /* POLL_MSG */
434         POLLERR,                                /* POLL_ERR */
435         POLLPRI | POLLRDBAND,                   /* POLL_PRI */
436         POLLHUP | POLLERR                       /* POLL_HUP */
437 };
438
439 static inline int sigio_perm(struct task_struct *p,
440                              struct fown_struct *fown, int sig)
441 {
442         return (((fown->euid == 0) ||
443                 (fown->euid == p->suid) || (fown->euid == p->uid) ||
444                 (fown->uid == p->suid) || (fown->uid == p->uid)) &&
445                 !security_file_send_sigiotask(p, fown, sig));
446 }
447
448 static void send_sigio_to_task(struct task_struct *p,
449                                struct fown_struct *fown, 
450                                int fd,
451                                int reason)
452 {
453         if (!sigio_perm(p, fown, fown->signum))
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_real_pid(pid);
499                 if (p) {
500                         send_sigio_to_task(p, fown, fd, band);
501                 }
502         } else {
503                 do_each_task_pid(-pid, PIDTYPE_PGID, p) {
504                         send_sigio_to_task(p, fown, fd, band);
505                 } while_each_task_pid(-pid, PIDTYPE_PGID, p);
506         }
507         read_unlock(&tasklist_lock);
508  out_unlock_fown:
509         read_unlock(&fown->lock);
510 }
511
512 static void send_sigurg_to_task(struct task_struct *p,
513                                 struct fown_struct *fown)
514 {
515         if (sigio_perm(p, fown, SIGURG))
516                 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
517 }
518
519 int send_sigurg(struct fown_struct *fown)
520 {
521         struct task_struct *p;
522         int pid, ret = 0;
523         
524         read_lock(&fown->lock);
525         pid = fown->pid;
526         if (!pid)
527                 goto out_unlock_fown;
528
529         ret = 1;
530         
531         read_lock(&tasklist_lock);
532         if (pid > 0) {
533                 p = find_task_by_real_pid(pid);
534                 if (p) {
535                         send_sigurg_to_task(p, fown);
536                 }
537         } else {
538                 do_each_task_pid(-pid, PIDTYPE_PGID, p) {
539                         send_sigurg_to_task(p, fown);
540                 } while_each_task_pid(-pid, PIDTYPE_PGID, p);
541         }
542         read_unlock(&tasklist_lock);
543  out_unlock_fown:
544         read_unlock(&fown->lock);
545         return ret;
546 }
547
548 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
549 static kmem_cache_t *fasync_cache;
550
551 /*
552  * fasync_helper() is used by some character device drivers (mainly mice)
553  * to set up the fasync queue. It returns negative on error, 0 if it did
554  * no changes and positive if it added/deleted the entry.
555  */
556 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
557 {
558         struct fasync_struct *fa, **fp;
559         struct fasync_struct *new = NULL;
560         int result = 0;
561
562         if (on) {
563                 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
564                 if (!new)
565                         return -ENOMEM;
566         }
567         write_lock_irq(&fasync_lock);
568         for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
569                 if (fa->fa_file == filp) {
570                         if(on) {
571                                 fa->fa_fd = fd;
572                                 kmem_cache_free(fasync_cache, new);
573                         } else {
574                                 *fp = fa->fa_next;
575                                 kmem_cache_free(fasync_cache, fa);
576                                 result = 1;
577                         }
578                         goto out;
579                 }
580         }
581
582         if (on) {
583                 new->magic = FASYNC_MAGIC;
584                 new->fa_file = filp;
585                 new->fa_fd = fd;
586                 new->fa_next = *fapp;
587                 *fapp = new;
588                 result = 1;
589         }
590 out:
591         write_unlock_irq(&fasync_lock);
592         return result;
593 }
594
595 EXPORT_SYMBOL(fasync_helper);
596
597 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
598 {
599         while (fa) {
600                 struct fown_struct * fown;
601                 if (fa->magic != FASYNC_MAGIC) {
602                         printk(KERN_ERR "kill_fasync: bad magic number in "
603                                "fasync_struct!\n");
604                         return;
605                 }
606                 fown = &fa->fa_file->f_owner;
607                 /* Don't send SIGURG to processes which have not set a
608                    queued signum: SIGURG has its own default signalling
609                    mechanism. */
610                 if (!(sig == SIGURG && fown->signum == 0))
611                         send_sigio(fown, fa->fa_fd, band);
612                 fa = fa->fa_next;
613         }
614 }
615
616 EXPORT_SYMBOL(__kill_fasync);
617
618 void kill_fasync(struct fasync_struct **fp, int sig, int band)
619 {
620         /* First a quick test without locking: usually
621          * the list is empty.
622          */
623         if (*fp) {
624                 read_lock(&fasync_lock);
625                 /* reread *fp after obtaining the lock */
626                 __kill_fasync(*fp, sig, band);
627                 read_unlock(&fasync_lock);
628         }
629 }
630 EXPORT_SYMBOL(kill_fasync);
631
632 static int __init fasync_init(void)
633 {
634         fasync_cache = kmem_cache_create("fasync_cache",
635                 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL, NULL);
636         return 0;
637 }
638
639 module_init(fasync_init)