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