Add to mips64 also for symmetry.
[linux-2.6/linux-mips.git] / fs / fcntl.c
blob9daa2a90764945e005d158d35c156126aac5d6b1
1 /*
2 * linux/fs/fcntl.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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 #include <linux/ptrace.h>
17 #include <asm/poll.h>
18 #include <asm/siginfo.h>
19 #include <asm/uaccess.h>
21 extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
22 extern int fcntl_getlease(struct file *filp);
24 void set_close_on_exec(unsigned int fd, int flag)
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);
35 static inline int get_close_on_exec(unsigned int fd)
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;
46 /* Expand files. Return <0 on error; 0 nothing done; 1 files expanded,
47 * we may have blocked.
49 * Should be called with the files->file_lock spinlock held for write.
51 static int expand_files(struct files_struct *files, int nr)
53 int err, expand = 0;
54 #ifdef FDSET_DEBUG
55 printk (KERN_ERR "%s %d: nr = %d\n", __FUNCTION__, current->pid, nr);
56 #endif
58 if (nr >= files->max_fdset) {
59 expand = 1;
60 if ((err = expand_fdset(files, nr)))
61 goto out;
63 if (nr >= files->max_fds) {
64 expand = 1;
65 if ((err = expand_fd_array(files, nr)))
66 goto out;
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;
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.
83 static int locate_fd(struct files_struct *files,
84 struct file *file, unsigned int orig_start)
86 unsigned int newfd;
87 unsigned int start;
88 int error;
90 error = -EINVAL;
91 if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
92 goto out;
94 repeat:
96 * Someone might have closed fd's in the range
97 * orig_start..files->next_fd
99 start = orig_start;
100 if (start < files->next_fd)
101 start = files->next_fd;
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);
109 error = -EMFILE;
110 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
111 goto out;
113 error = expand_files(files, newfd);
114 if (error < 0)
115 goto out;
118 * If we needed to expand the fs array we
119 * might have blocked - try again.
121 if (error)
122 goto repeat;
124 if (start <= files->next_fd)
125 files->next_fd = newfd + 1;
127 error = newfd;
129 out:
130 return error;
133 static int dupfd(struct file *file, unsigned int start)
135 struct files_struct * files = current->files;
136 int fd;
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 fd_install(fd, file);
145 } else {
146 spin_unlock(&files->file_lock);
147 fput(file);
150 return fd;
153 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
155 int err = -EBADF;
156 struct file * file, *tofree;
157 struct files_struct * files = current->files;
159 spin_lock(&files->file_lock);
160 if (!(file = fcheck(oldfd)))
161 goto out_unlock;
162 err = newfd;
163 if (newfd == oldfd)
164 goto out_unlock;
165 err = -EBADF;
166 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
167 goto out_unlock;
168 get_file(file); /* We are now finished with oldfd */
170 err = expand_files(files, newfd);
171 if (err < 0)
172 goto out_fput;
174 /* To avoid races with open() and dup(), we will mark the fd as
175 * in-use in the open-file bitmap throughout the entire dup2()
176 * process. This is quite safe: do_close() uses the fd array
177 * entry, not the bitmap, to decide what work needs to be
178 * done. --sct */
179 /* Doesn't work. open() might be there first. --AV */
181 /* Yes. It's a race. In user space. Nothing sane to do */
182 err = -EBUSY;
183 tofree = files->fd[newfd];
184 if (!tofree && FD_ISSET(newfd, files->open_fds))
185 goto out_fput;
187 files->fd[newfd] = file;
188 FD_SET(newfd, files->open_fds);
189 FD_CLR(newfd, files->close_on_exec);
190 spin_unlock(&files->file_lock);
192 if (tofree)
193 filp_close(tofree, files);
194 err = newfd;
195 out:
196 return err;
197 out_unlock:
198 spin_unlock(&files->file_lock);
199 goto out;
201 out_fput:
202 spin_unlock(&files->file_lock);
203 fput(file);
204 goto out;
207 asmlinkage long sys_dup(unsigned int fildes)
209 int ret = -EBADF;
210 struct file * file = fget(fildes);
212 if (file)
213 ret = dupfd(file, 0);
214 return ret;
217 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT)
219 static int setfl(int fd, struct file * filp, unsigned long arg)
221 struct inode * inode = filp->f_dentry->d_inode;
222 int error = 0;
224 /* O_APPEND cannot be cleared if the file is marked as append-only */
225 if (!(arg & O_APPEND) && IS_APPEND(inode))
226 return -EPERM;
228 /* required for strict SunOS emulation */
229 if (O_NONBLOCK != O_NDELAY)
230 if (arg & O_NDELAY)
231 arg |= O_NONBLOCK;
233 if (arg & O_DIRECT) {
234 if (!inode->i_mapping || !inode->i_mapping->a_ops ||
235 !inode->i_mapping->a_ops->direct_IO)
236 return -EINVAL;
239 lock_kernel();
240 if ((arg ^ filp->f_flags) & FASYNC) {
241 if (filp->f_op && filp->f_op->fasync) {
242 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
243 if (error < 0)
244 goto out;
248 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
249 out:
250 unlock_kernel();
251 return error;
254 static void f_modown(struct file *filp, unsigned long pid,
255 uid_t uid, uid_t euid, int force)
257 write_lock_irq(&filp->f_owner.lock);
258 if (force || !filp->f_owner.pid) {
259 filp->f_owner.pid = pid;
260 filp->f_owner.uid = uid;
261 filp->f_owner.euid = euid;
263 write_unlock_irq(&filp->f_owner.lock);
266 int f_setown(struct file *filp, unsigned long arg, int force)
268 int err;
270 err = security_file_set_fowner(filp);
271 if (err)
272 return err;
274 f_modown(filp, arg, current->uid, current->euid, force);
275 return 0;
278 void f_delown(struct file *filp)
280 f_modown(filp, 0, 0, 0, 1);
283 static long do_fcntl(unsigned int fd, unsigned int cmd,
284 unsigned long arg, struct file * filp)
286 long err = -EINVAL;
288 switch (cmd) {
289 case F_DUPFD:
290 get_file(filp);
291 err = dupfd(filp, arg);
292 break;
293 case F_GETFD:
294 err = get_close_on_exec(fd);
295 break;
296 case F_SETFD:
297 err = 0;
298 set_close_on_exec(fd, arg&1);
299 break;
300 case F_GETFL:
301 err = filp->f_flags;
302 break;
303 case F_SETFL:
304 err = setfl(fd, filp, arg);
305 break;
306 case F_GETLK:
307 err = fcntl_getlk(filp, (struct flock __user *) arg);
308 break;
309 case F_SETLK:
310 case F_SETLKW:
311 err = fcntl_setlk(filp, cmd, (struct flock __user *) arg);
312 break;
313 case F_GETOWN:
315 * XXX If f_owner is a process group, the
316 * negative return value will get converted
317 * into an error. Oops. If we keep the
318 * current syscall conventions, the only way
319 * to fix this will be in libc.
321 err = filp->f_owner.pid;
322 force_successful_syscall_return();
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;
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;
351 return err;
354 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
356 struct file * filp;
357 long err = -EBADF;
359 filp = fget(fd);
360 if (!filp)
361 goto out;
363 err = security_file_fcntl(filp, cmd, arg);
364 if (err) {
365 fput(filp);
366 return err;
369 err = do_fcntl(fd, cmd, arg, filp);
371 fput(filp);
372 out:
373 return err;
376 #if BITS_PER_LONG == 32
377 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
379 struct file * filp;
380 long err;
382 err = -EBADF;
383 filp = fget(fd);
384 if (!filp)
385 goto out;
387 err = security_file_fcntl(filp, cmd, arg);
388 if (err) {
389 fput(filp);
390 return err;
392 err = -EBADF;
394 switch (cmd) {
395 case F_GETLK64:
396 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
397 break;
398 case F_SETLK64:
399 case F_SETLKW64:
400 err = fcntl_setlk64(filp, cmd, (struct flock64 __user *) arg);
401 break;
402 default:
403 err = do_fcntl(fd, cmd, arg, filp);
404 break;
406 fput(filp);
407 out:
408 return err;
410 #endif
412 /* Table to convert sigio signal codes into poll band bitmaps */
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 */
423 static inline int sigio_perm(struct task_struct *p,
424 struct fown_struct *fown)
426 return ((fown->euid == 0) ||
427 (fown->euid == p->suid) || (fown->euid == p->uid) ||
428 (fown->uid == p->suid) || (fown->uid == p->uid));
431 static void send_sigio_to_task(struct task_struct *p,
432 struct fown_struct *fown,
433 int fd,
434 int reason)
436 if (!sigio_perm(p, fown))
437 return;
439 if (security_file_send_sigiotask(p, fown, fd, reason))
440 return;
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);
472 void send_sigio(struct fown_struct *fown, int fd, int band)
474 struct task_struct *p;
475 int pid;
477 read_lock(&fown->lock);
478 pid = fown->pid;
479 if (!pid)
480 goto out_unlock_fown;
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);
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);
495 read_unlock(&tasklist_lock);
496 out_unlock_fown:
497 read_unlock(&fown->lock);
500 static void send_sigurg_to_task(struct task_struct *p,
501 struct fown_struct *fown)
503 if (sigio_perm(p, fown))
504 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
507 int send_sigurg(struct fown_struct *fown)
509 struct task_struct *p;
510 int pid, ret = 0;
512 read_lock(&fown->lock);
513 pid = fown->pid;
514 if (!pid)
515 goto out_unlock_fown;
517 ret = 1;
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);
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);
532 read_unlock(&tasklist_lock);
533 out_unlock_fown:
534 read_unlock(&fown->lock);
535 return ret;
538 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
539 static kmem_cache_t *fasync_cache;
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.
546 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
548 struct fasync_struct *fa, **fp;
549 struct fasync_struct *new = NULL;
550 int result = 0;
552 if (on) {
553 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
554 if (!new)
555 return -ENOMEM;
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;
568 goto out;
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;
580 out:
581 write_unlock_irq(&fasync_lock);
582 return result;
585 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
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;
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;
604 void kill_fasync(struct fasync_struct **fp, int sig, int band)
606 read_lock(&fasync_lock);
607 __kill_fasync(*fp, sig, band);
608 read_unlock(&fasync_lock);
611 static int __init fasync_init(void)
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;
620 module_init(fasync_init)
622 EXPORT_SYMBOL(f_setown);
623 EXPORT_SYMBOL(f_delown);