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