[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / open.c
blob9a9ce5be4dbc2d681cc596c177fc49e4aeb41ce7
1 /*
2 * linux/fs/open.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/string.h>
8 #include <linux/mm.h>
9 #include <linux/utime.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/quotaops.h>
13 #include <linux/dnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/vfs.h>
22 #include <asm/uaccess.h>
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
26 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
28 int retval = -ENODEV;
30 if (sb) {
31 retval = -ENOSYS;
32 if (sb->s_op->statfs) {
33 memset(buf, 0, sizeof(*buf));
34 retval = security_sb_statfs(sb);
35 if (retval)
36 return retval;
37 retval = sb->s_op->statfs(sb, buf);
38 if (retval == 0 && buf->f_frsize == 0)
39 buf->f_frsize = buf->f_bsize;
42 return retval;
45 EXPORT_SYMBOL(vfs_statfs);
47 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
49 struct kstatfs st;
50 int retval;
52 retval = vfs_statfs(sb, &st);
53 if (retval)
54 return retval;
56 if (sizeof(*buf) == sizeof(st))
57 memcpy(buf, &st, sizeof(st));
58 else {
59 if (sizeof buf->f_blocks == 4) {
60 if ((st.f_blocks | st.f_bfree |
61 st.f_bavail | st.f_files | st.f_ffree) &
62 0xffffffff00000000ULL)
63 return -EOVERFLOW;
66 buf->f_type = st.f_type;
67 buf->f_bsize = st.f_bsize;
68 buf->f_blocks = st.f_blocks;
69 buf->f_bfree = st.f_bfree;
70 buf->f_bavail = st.f_bavail;
71 buf->f_files = st.f_files;
72 buf->f_ffree = st.f_ffree;
73 buf->f_fsid = st.f_fsid;
74 buf->f_namelen = st.f_namelen;
75 buf->f_frsize = st.f_frsize;
76 memset(buf->f_spare, 0, sizeof(buf->f_spare));
78 return 0;
81 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
83 struct kstatfs st;
84 int retval;
86 retval = vfs_statfs(sb, &st);
87 if (retval)
88 return retval;
90 if (sizeof(*buf) == sizeof(st))
91 memcpy(buf, &st, sizeof(st));
92 else {
93 buf->f_type = st.f_type;
94 buf->f_bsize = st.f_bsize;
95 buf->f_blocks = st.f_blocks;
96 buf->f_bfree = st.f_bfree;
97 buf->f_bavail = st.f_bavail;
98 buf->f_files = st.f_files;
99 buf->f_ffree = st.f_ffree;
100 buf->f_fsid = st.f_fsid;
101 buf->f_namelen = st.f_namelen;
102 buf->f_frsize = st.f_frsize;
103 memset(buf->f_spare, 0, sizeof(buf->f_spare));
105 return 0;
108 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
110 struct nameidata nd;
111 int error;
113 error = user_path_walk(path, &nd);
114 if (!error) {
115 struct statfs tmp;
116 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
117 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
118 error = -EFAULT;
119 path_release(&nd);
121 return error;
125 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
127 struct nameidata nd;
128 long error;
130 if (sz != sizeof(*buf))
131 return -EINVAL;
132 error = user_path_walk(path, &nd);
133 if (!error) {
134 struct statfs64 tmp;
135 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
136 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
137 error = -EFAULT;
138 path_release(&nd);
140 return error;
144 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
146 struct file * file;
147 struct statfs tmp;
148 int error;
150 error = -EBADF;
151 file = fget(fd);
152 if (!file)
153 goto out;
154 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
155 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
156 error = -EFAULT;
157 fput(file);
158 out:
159 return error;
162 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
164 struct file * file;
165 struct statfs64 tmp;
166 int error;
168 if (sz != sizeof(*buf))
169 return -EINVAL;
171 error = -EBADF;
172 file = fget(fd);
173 if (!file)
174 goto out;
175 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
176 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
177 error = -EFAULT;
178 fput(file);
179 out:
180 return error;
183 int do_truncate(struct dentry *dentry, loff_t length)
185 int err;
186 struct iattr newattrs;
188 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
189 if (length < 0)
190 return -EINVAL;
192 newattrs.ia_size = length;
193 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
194 down(&dentry->d_inode->i_sem);
195 err = notify_change(dentry, &newattrs);
196 up(&dentry->d_inode->i_sem);
197 return err;
200 static inline long do_sys_truncate(const char __user * path, loff_t length)
202 struct nameidata nd;
203 struct inode * inode;
204 int error;
206 error = -EINVAL;
207 if (length < 0) /* sorry, but loff_t says... */
208 goto out;
210 error = user_path_walk(path, &nd);
211 if (error)
212 goto out;
213 inode = nd.dentry->d_inode;
215 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
216 error = -EISDIR;
217 if (S_ISDIR(inode->i_mode))
218 goto dput_and_out;
220 error = -EINVAL;
221 if (!S_ISREG(inode->i_mode))
222 goto dput_and_out;
224 error = permission(inode,MAY_WRITE,&nd);
225 if (error)
226 goto dput_and_out;
228 error = -EROFS;
229 if (IS_RDONLY(inode))
230 goto dput_and_out;
232 error = -EPERM;
233 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 goto dput_and_out;
237 * Make sure that there are no leases.
239 error = break_lease(inode, FMODE_WRITE);
240 if (error)
241 goto dput_and_out;
243 error = get_write_access(inode);
244 if (error)
245 goto dput_and_out;
247 error = locks_verify_truncate(inode, NULL, length);
248 if (!error) {
249 DQUOT_INIT(inode);
250 error = do_truncate(nd.dentry, length);
252 put_write_access(inode);
254 dput_and_out:
255 path_release(&nd);
256 out:
257 return error;
260 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
262 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
263 return do_sys_truncate(path, (long)length);
266 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
268 struct inode * inode;
269 struct dentry *dentry;
270 struct file * file;
271 int error;
273 error = -EINVAL;
274 if (length < 0)
275 goto out;
276 error = -EBADF;
277 file = fget(fd);
278 if (!file)
279 goto out;
281 /* explicitly opened as large or we are on 64-bit box */
282 if (file->f_flags & O_LARGEFILE)
283 small = 0;
285 dentry = file->f_dentry;
286 inode = dentry->d_inode;
287 error = -EINVAL;
288 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
289 goto out_putf;
291 error = -EINVAL;
292 /* Cannot ftruncate over 2^31 bytes without large file support */
293 if (small && length > MAX_NON_LFS)
294 goto out_putf;
296 error = -EPERM;
297 if (IS_APPEND(inode))
298 goto out_putf;
300 error = locks_verify_truncate(inode, file, length);
301 if (!error)
302 error = do_truncate(dentry, length);
303 out_putf:
304 fput(file);
305 out:
306 return error;
309 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
311 return do_sys_ftruncate(fd, length, 1);
314 /* LFS versions of truncate are only needed on 32 bit machines */
315 #if BITS_PER_LONG == 32
316 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
318 return do_sys_truncate(path, length);
321 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
323 return do_sys_ftruncate(fd, length, 0);
325 #endif
327 #if !(defined(__alpha__) || defined(__ia64__))
330 * sys_utime() can be implemented in user-level using sys_utimes().
331 * Is this for backwards compatibility? If so, why not move it
332 * into the appropriate arch directory (for those architectures that
333 * need it).
336 /* If times==NULL, set access and modification to current time,
337 * must be owner or have write permission.
338 * Else, update from *times, must be owner or super user.
340 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
342 int error;
343 struct nameidata nd;
344 struct inode * inode;
345 struct iattr newattrs;
347 error = user_path_walk(filename, &nd);
348 if (error)
349 goto out;
350 inode = nd.dentry->d_inode;
352 error = -EROFS;
353 if (IS_RDONLY(inode))
354 goto dput_and_out;
356 /* Don't worry, the checks are done in inode_change_ok() */
357 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
358 if (times) {
359 error = -EPERM;
360 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
361 goto dput_and_out;
363 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
364 newattrs.ia_atime.tv_nsec = 0;
365 if (!error)
366 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
367 newattrs.ia_mtime.tv_nsec = 0;
368 if (error)
369 goto dput_and_out;
371 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
372 } else {
373 error = -EACCES;
374 if (IS_IMMUTABLE(inode))
375 goto dput_and_out;
377 if (current->fsuid != inode->i_uid &&
378 (error = permission(inode,MAY_WRITE,&nd)) != 0)
379 goto dput_and_out;
381 down(&inode->i_sem);
382 error = notify_change(nd.dentry, &newattrs);
383 up(&inode->i_sem);
384 dput_and_out:
385 path_release(&nd);
386 out:
387 return error;
390 #endif
392 /* If times==NULL, set access and modification to current time,
393 * must be owner or have write permission.
394 * Else, update from *times, must be owner or super user.
396 long do_utimes(char __user * filename, struct timeval * times)
398 int error;
399 struct nameidata nd;
400 struct inode * inode;
401 struct iattr newattrs;
403 error = user_path_walk(filename, &nd);
405 if (error)
406 goto out;
407 inode = nd.dentry->d_inode;
409 error = -EROFS;
410 if (IS_RDONLY(inode))
411 goto dput_and_out;
413 /* Don't worry, the checks are done in inode_change_ok() */
414 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
415 if (times) {
416 error = -EPERM;
417 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
418 goto dput_and_out;
420 newattrs.ia_atime.tv_sec = times[0].tv_sec;
421 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
422 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
423 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
424 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
425 } else {
426 error = -EACCES;
427 if (IS_IMMUTABLE(inode))
428 goto dput_and_out;
430 if (current->fsuid != inode->i_uid &&
431 (error = permission(inode,MAY_WRITE,&nd)) != 0)
432 goto dput_and_out;
434 down(&inode->i_sem);
435 error = notify_change(nd.dentry, &newattrs);
436 up(&inode->i_sem);
437 dput_and_out:
438 path_release(&nd);
439 out:
440 return error;
443 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
445 struct timeval times[2];
447 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
448 return -EFAULT;
449 return do_utimes(filename, utimes ? times : NULL);
454 * access() needs to use the real uid/gid, not the effective uid/gid.
455 * We do this by temporarily clearing all FS-related capabilities and
456 * switching the fsuid/fsgid around to the real ones.
458 asmlinkage long sys_access(const char __user * filename, int mode)
460 struct nameidata nd;
461 int old_fsuid, old_fsgid;
462 kernel_cap_t old_cap;
463 int res;
465 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
466 return -EINVAL;
468 old_fsuid = current->fsuid;
469 old_fsgid = current->fsgid;
470 old_cap = current->cap_effective;
472 current->fsuid = current->uid;
473 current->fsgid = current->gid;
476 * Clear the capabilities if we switch to a non-root user
478 * FIXME: There is a race here against sys_capset. The
479 * capabilities can change yet we will restore the old
480 * value below. We should hold task_capabilities_lock,
481 * but we cannot because user_path_walk can sleep.
483 if (current->uid)
484 cap_clear(current->cap_effective);
485 else
486 current->cap_effective = current->cap_permitted;
488 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
489 if (!res) {
490 res = permission(nd.dentry->d_inode, mode, &nd);
491 /* SuS v2 requires we report a read only fs too */
492 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
493 && !special_file(nd.dentry->d_inode->i_mode))
494 res = -EROFS;
495 path_release(&nd);
498 current->fsuid = old_fsuid;
499 current->fsgid = old_fsgid;
500 current->cap_effective = old_cap;
502 return res;
505 asmlinkage long sys_chdir(const char __user * filename)
507 struct nameidata nd;
508 int error;
510 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
511 if (error)
512 goto out;
514 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
515 if (error)
516 goto dput_and_out;
518 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
520 dput_and_out:
521 path_release(&nd);
522 out:
523 return error;
526 asmlinkage long sys_fchdir(unsigned int fd)
528 struct file *file;
529 struct dentry *dentry;
530 struct inode *inode;
531 struct vfsmount *mnt;
532 int error;
534 error = -EBADF;
535 file = fget(fd);
536 if (!file)
537 goto out;
539 dentry = file->f_dentry;
540 mnt = file->f_vfsmnt;
541 inode = dentry->d_inode;
543 error = -ENOTDIR;
544 if (!S_ISDIR(inode->i_mode))
545 goto out_putf;
547 error = permission(inode, MAY_EXEC, NULL);
548 if (!error)
549 set_fs_pwd(current->fs, mnt, dentry);
550 out_putf:
551 fput(file);
552 out:
553 return error;
556 asmlinkage long sys_chroot(const char __user * filename)
558 struct nameidata nd;
559 int error;
561 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
562 if (error)
563 goto out;
565 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
566 if (error)
567 goto dput_and_out;
569 error = -EPERM;
570 if (!capable(CAP_SYS_CHROOT))
571 goto dput_and_out;
573 set_fs_root(current->fs, nd.mnt, nd.dentry);
574 set_fs_altroot();
575 error = 0;
576 dput_and_out:
577 path_release(&nd);
578 out:
579 return error;
582 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
584 struct inode * inode;
585 struct dentry * dentry;
586 struct file * file;
587 int err = -EBADF;
588 struct iattr newattrs;
590 file = fget(fd);
591 if (!file)
592 goto out;
594 dentry = file->f_dentry;
595 inode = dentry->d_inode;
597 err = -EROFS;
598 if (IS_RDONLY(inode))
599 goto out_putf;
600 err = -EPERM;
601 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
602 goto out_putf;
603 down(&inode->i_sem);
604 if (mode == (mode_t) -1)
605 mode = inode->i_mode;
606 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
607 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
608 err = notify_change(dentry, &newattrs);
609 up(&inode->i_sem);
611 out_putf:
612 fput(file);
613 out:
614 return err;
617 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
619 struct nameidata nd;
620 struct inode * inode;
621 int error;
622 struct iattr newattrs;
624 error = user_path_walk(filename, &nd);
625 if (error)
626 goto out;
627 inode = nd.dentry->d_inode;
629 error = -EROFS;
630 if (IS_RDONLY(inode))
631 goto dput_and_out;
633 error = -EPERM;
634 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
635 goto dput_and_out;
637 down(&inode->i_sem);
638 if (mode == (mode_t) -1)
639 mode = inode->i_mode;
640 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
641 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
642 error = notify_change(nd.dentry, &newattrs);
643 up(&inode->i_sem);
645 dput_and_out:
646 path_release(&nd);
647 out:
648 return error;
651 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
653 struct inode * inode;
654 int error;
655 struct iattr newattrs;
657 error = -ENOENT;
658 if (!(inode = dentry->d_inode)) {
659 printk(KERN_ERR "chown_common: NULL inode\n");
660 goto out;
662 error = -EROFS;
663 if (IS_RDONLY(inode))
664 goto out;
665 error = -EPERM;
666 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
667 goto out;
668 newattrs.ia_valid = ATTR_CTIME;
669 if (user != (uid_t) -1) {
670 newattrs.ia_valid |= ATTR_UID;
671 newattrs.ia_uid = user;
673 if (group != (gid_t) -1) {
674 newattrs.ia_valid |= ATTR_GID;
675 newattrs.ia_gid = group;
677 if (!S_ISDIR(inode->i_mode))
678 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
679 down(&inode->i_sem);
680 error = notify_change(dentry, &newattrs);
681 up(&inode->i_sem);
682 out:
683 return error;
686 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
688 struct nameidata nd;
689 int error;
691 error = user_path_walk(filename, &nd);
692 if (!error) {
693 error = chown_common(nd.dentry, user, group);
694 path_release(&nd);
696 return error;
699 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
701 struct nameidata nd;
702 int error;
704 error = user_path_walk_link(filename, &nd);
705 if (!error) {
706 error = chown_common(nd.dentry, user, group);
707 path_release(&nd);
709 return error;
713 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
715 struct file * file;
716 int error = -EBADF;
718 file = fget(fd);
719 if (file) {
720 error = chown_common(file->f_dentry, user, group);
721 fput(file);
723 return error;
727 * Note that while the flag value (low two bits) for sys_open means:
728 * 00 - read-only
729 * 01 - write-only
730 * 10 - read-write
731 * 11 - special
732 * it is changed into
733 * 00 - no permissions needed
734 * 01 - read-permission
735 * 10 - write-permission
736 * 11 - read-write
737 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
738 * used by symlinks.
740 struct file *filp_open(const char * filename, int flags, int mode)
742 int namei_flags, error;
743 struct nameidata nd;
745 namei_flags = flags;
746 if ((namei_flags+1) & O_ACCMODE)
747 namei_flags++;
748 if (namei_flags & O_TRUNC)
749 namei_flags |= 2;
751 error = open_namei(filename, namei_flags, mode, &nd);
752 if (!error)
753 return dentry_open(nd.dentry, nd.mnt, flags);
755 return ERR_PTR(error);
758 EXPORT_SYMBOL(filp_open);
760 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
762 struct file * f;
763 struct inode *inode;
764 int error;
766 error = -ENFILE;
767 f = get_empty_filp();
768 if (!f)
769 goto cleanup_dentry;
770 f->f_flags = flags;
771 f->f_mode = (flags+1) & O_ACCMODE;
772 inode = dentry->d_inode;
773 if (f->f_mode & FMODE_WRITE) {
774 error = get_write_access(inode);
775 if (error)
776 goto cleanup_file;
779 f->f_mapping = inode->i_mapping;
780 file_ra_state_init(&f->f_ra, f->f_mapping);
781 f->f_dentry = dentry;
782 f->f_vfsmnt = mnt;
783 f->f_pos = 0;
784 f->f_op = fops_get(inode->i_fop);
785 file_move(f, &inode->i_sb->s_files);
787 if (f->f_op && f->f_op->open) {
788 error = f->f_op->open(inode,f);
789 if (error)
790 goto cleanup_all;
792 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
794 /* NB: we're sure to have correct a_ops only after f_op->open */
795 if (f->f_flags & O_DIRECT) {
796 if (!f->f_mapping || !f->f_mapping->a_ops ||
797 !f->f_mapping->a_ops->direct_IO) {
798 fput(f);
799 f = ERR_PTR(-EINVAL);
803 return f;
805 cleanup_all:
806 fops_put(f->f_op);
807 if (f->f_mode & FMODE_WRITE)
808 put_write_access(inode);
809 file_kill(f);
810 f->f_dentry = NULL;
811 f->f_vfsmnt = NULL;
812 cleanup_file:
813 put_filp(f);
814 cleanup_dentry:
815 dput(dentry);
816 mntput(mnt);
817 return ERR_PTR(error);
820 EXPORT_SYMBOL(dentry_open);
823 * Find an empty file descriptor entry, and mark it busy.
825 int get_unused_fd(void)
827 struct files_struct * files = current->files;
828 int fd, error;
830 error = -EMFILE;
831 spin_lock(&files->file_lock);
833 repeat:
834 fd = find_next_zero_bit(files->open_fds->fds_bits,
835 files->max_fdset,
836 files->next_fd);
839 * N.B. For clone tasks sharing a files structure, this test
840 * will limit the total number of files that can be opened.
842 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
843 goto out;
845 /* Do we need to expand the fdset array? */
846 if (fd >= files->max_fdset) {
847 error = expand_fdset(files, fd);
848 if (!error) {
849 error = -EMFILE;
850 goto repeat;
852 goto out;
856 * Check whether we need to expand the fd array.
858 if (fd >= files->max_fds) {
859 error = expand_fd_array(files, fd);
860 if (!error) {
861 error = -EMFILE;
862 goto repeat;
864 goto out;
867 FD_SET(fd, files->open_fds);
868 FD_CLR(fd, files->close_on_exec);
869 files->next_fd = fd + 1;
870 #if 1
871 /* Sanity check */
872 if (files->fd[fd] != NULL) {
873 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
874 files->fd[fd] = NULL;
876 #endif
877 error = fd;
879 out:
880 spin_unlock(&files->file_lock);
881 return error;
884 EXPORT_SYMBOL(get_unused_fd);
886 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
888 __FD_CLR(fd, files->open_fds);
889 if (fd < files->next_fd)
890 files->next_fd = fd;
893 void fastcall put_unused_fd(unsigned int fd)
895 struct files_struct *files = current->files;
896 spin_lock(&files->file_lock);
897 __put_unused_fd(files, fd);
898 spin_unlock(&files->file_lock);
901 EXPORT_SYMBOL(put_unused_fd);
904 * Install a file pointer in the fd array.
906 * The VFS is full of places where we drop the files lock between
907 * setting the open_fds bitmap and installing the file in the file
908 * array. At any such point, we are vulnerable to a dup2() race
909 * installing a file in the array before us. We need to detect this and
910 * fput() the struct file we are about to overwrite in this case.
912 * It should never happen - if we allow dup2() do it, _really_ bad things
913 * will follow.
916 void fastcall fd_install(unsigned int fd, struct file * file)
918 struct files_struct *files = current->files;
919 spin_lock(&files->file_lock);
920 if (unlikely(files->fd[fd] != NULL))
921 BUG();
922 files->fd[fd] = file;
923 spin_unlock(&files->file_lock);
926 EXPORT_SYMBOL(fd_install);
928 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
930 char * tmp;
931 int fd, error;
933 #if BITS_PER_LONG != 32
934 flags |= O_LARGEFILE;
935 #endif
936 tmp = getname(filename);
937 fd = PTR_ERR(tmp);
938 if (!IS_ERR(tmp)) {
939 fd = get_unused_fd();
940 if (fd >= 0) {
941 struct file *f = filp_open(tmp, flags, mode);
942 error = PTR_ERR(f);
943 if (IS_ERR(f))
944 goto out_error;
945 fd_install(fd, f);
947 out:
948 putname(tmp);
950 return fd;
952 out_error:
953 put_unused_fd(fd);
954 fd = error;
955 goto out;
957 EXPORT_SYMBOL_GPL(sys_open);
959 #ifndef __alpha__
962 * For backward compatibility? Maybe this should be moved
963 * into arch/i386 instead?
965 asmlinkage long sys_creat(const char __user * pathname, int mode)
967 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
970 #endif
973 * "id" is the POSIX thread ID. We use the
974 * files pointer for this..
976 int filp_close(struct file *filp, fl_owner_t id)
978 int retval;
980 /* Report and clear outstanding errors */
981 retval = filp->f_error;
982 if (retval)
983 filp->f_error = 0;
985 if (!file_count(filp)) {
986 printk(KERN_ERR "VFS: Close: file count is 0\n");
987 return retval;
990 if (filp->f_op && filp->f_op->flush) {
991 int err = filp->f_op->flush(filp);
992 if (!retval)
993 retval = err;
996 dnotify_flush(filp, id);
997 locks_remove_posix(filp, id);
998 fput(filp);
999 return retval;
1002 EXPORT_SYMBOL(filp_close);
1005 * Careful here! We test whether the file pointer is NULL before
1006 * releasing the fd. This ensures that one clone task can't release
1007 * an fd while another clone is opening it.
1009 asmlinkage long sys_close(unsigned int fd)
1011 struct file * filp;
1012 struct files_struct *files = current->files;
1014 spin_lock(&files->file_lock);
1015 if (fd >= files->max_fds)
1016 goto out_unlock;
1017 filp = files->fd[fd];
1018 if (!filp)
1019 goto out_unlock;
1020 files->fd[fd] = NULL;
1021 FD_CLR(fd, files->close_on_exec);
1022 __put_unused_fd(files, fd);
1023 spin_unlock(&files->file_lock);
1024 return filp_close(filp, files);
1026 out_unlock:
1027 spin_unlock(&files->file_lock);
1028 return -EBADF;
1031 EXPORT_SYMBOL(sys_close);
1034 * This routine simulates a hangup on the tty, to arrange that users
1035 * are given clean terminals at login time.
1037 asmlinkage long sys_vhangup(void)
1039 if (capable(CAP_SYS_TTY_CONFIG)) {
1040 tty_vhangup(current->tty);
1041 return 0;
1043 return -EPERM;
1047 * Called when an inode is about to be open.
1048 * We use this to disallow opening large files on 32bit systems if
1049 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1050 * on this flag in sys_open.
1052 int generic_file_open(struct inode * inode, struct file * filp)
1054 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1055 return -EFBIG;
1056 return 0;
1059 EXPORT_SYMBOL(generic_file_open);