Replace extern inline with static inline.
[linux-2.6/linux-mips.git] / fs / open.c
blobb6476e054ed91d76a58d9b9366d2386d4a3adac9
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>
24 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
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 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
47 struct kstatfs st;
48 int retval;
50 retval = vfs_statfs(sb, &st);
51 if (retval)
52 return retval;
54 if (sizeof(*buf) == sizeof(st))
55 memcpy(buf, &st, sizeof(st));
56 else {
57 if (sizeof buf->f_blocks == 4) {
58 if ((st.f_blocks | st.f_bfree |
59 st.f_bavail | st.f_files | st.f_ffree) &
60 0xffffffff00000000ULL)
61 return -EOVERFLOW;
64 buf->f_type = st.f_type;
65 buf->f_bsize = st.f_bsize;
66 buf->f_blocks = st.f_blocks;
67 buf->f_bfree = st.f_bfree;
68 buf->f_bavail = st.f_bavail;
69 buf->f_files = st.f_files;
70 buf->f_ffree = st.f_ffree;
71 buf->f_fsid = st.f_fsid;
72 buf->f_namelen = st.f_namelen;
73 buf->f_frsize = st.f_frsize;
74 memset(buf->f_spare, 0, sizeof(buf->f_spare));
76 return 0;
79 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
81 struct kstatfs st;
82 int retval;
84 retval = vfs_statfs(sb, &st);
85 if (retval)
86 return retval;
88 if (sizeof(*buf) == sizeof(st))
89 memcpy(buf, &st, sizeof(st));
90 else {
91 buf->f_type = st.f_type;
92 buf->f_bsize = st.f_bsize;
93 buf->f_blocks = st.f_blocks;
94 buf->f_bfree = st.f_bfree;
95 buf->f_bavail = st.f_bavail;
96 buf->f_files = st.f_files;
97 buf->f_ffree = st.f_ffree;
98 buf->f_fsid = st.f_fsid;
99 buf->f_namelen = st.f_namelen;
100 buf->f_frsize = st.f_frsize;
101 memset(buf->f_spare, 0, sizeof(buf->f_spare));
103 return 0;
106 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
108 struct nameidata nd;
109 int error;
111 error = user_path_walk(path, &nd);
112 if (!error) {
113 struct statfs tmp;
114 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
115 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
116 error = -EFAULT;
117 path_release(&nd);
119 return error;
123 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
125 struct nameidata nd;
126 long error;
128 if (sz != sizeof(*buf))
129 return -EINVAL;
130 error = user_path_walk(path, &nd);
131 if (!error) {
132 struct statfs64 tmp;
133 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
134 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
135 error = -EFAULT;
136 path_release(&nd);
138 return error;
142 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
144 struct file * file;
145 struct statfs tmp;
146 int error;
148 error = -EBADF;
149 file = fget(fd);
150 if (!file)
151 goto out;
152 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
153 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
154 error = -EFAULT;
155 fput(file);
156 out:
157 return error;
160 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
162 struct file * file;
163 struct statfs64 tmp;
164 int error;
166 if (sz != sizeof(*buf))
167 return -EINVAL;
169 error = -EBADF;
170 file = fget(fd);
171 if (!file)
172 goto out;
173 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
174 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
175 error = -EFAULT;
176 fput(file);
177 out:
178 return error;
181 int do_truncate(struct dentry *dentry, loff_t length)
183 int err;
184 struct iattr newattrs;
186 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
187 if (length < 0)
188 return -EINVAL;
190 newattrs.ia_size = length;
191 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
192 down(&dentry->d_inode->i_sem);
193 err = notify_change(dentry, &newattrs);
194 up(&dentry->d_inode->i_sem);
195 return err;
198 static inline long do_sys_truncate(const char __user * path, loff_t length)
200 struct nameidata nd;
201 struct inode * inode;
202 int error;
204 error = -EINVAL;
205 if (length < 0) /* sorry, but loff_t says... */
206 goto out;
208 error = user_path_walk(path, &nd);
209 if (error)
210 goto out;
211 inode = nd.dentry->d_inode;
213 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
214 error = -EISDIR;
215 if (S_ISDIR(inode->i_mode))
216 goto dput_and_out;
218 error = -EINVAL;
219 if (!S_ISREG(inode->i_mode))
220 goto dput_and_out;
222 error = permission(inode,MAY_WRITE,&nd);
223 if (error)
224 goto dput_and_out;
226 error = -EROFS;
227 if (IS_RDONLY(inode))
228 goto dput_and_out;
230 error = -EPERM;
231 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
232 goto dput_and_out;
235 * Make sure that there are no leases.
237 error = break_lease(inode, FMODE_WRITE);
238 if (error)
239 goto dput_and_out;
241 error = get_write_access(inode);
242 if (error)
243 goto dput_and_out;
245 error = locks_verify_truncate(inode, NULL, length);
246 if (!error) {
247 DQUOT_INIT(inode);
248 error = do_truncate(nd.dentry, length);
250 put_write_access(inode);
252 dput_and_out:
253 path_release(&nd);
254 out:
255 return error;
258 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
260 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
261 return do_sys_truncate(path, (long)length);
264 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
266 struct inode * inode;
267 struct dentry *dentry;
268 struct file * file;
269 int error;
271 error = -EINVAL;
272 if (length < 0)
273 goto out;
274 error = -EBADF;
275 file = fget(fd);
276 if (!file)
277 goto out;
279 /* explicitly opened as large or we are on 64-bit box */
280 if (file->f_flags & O_LARGEFILE)
281 small = 0;
283 dentry = file->f_dentry;
284 inode = dentry->d_inode;
285 error = -EINVAL;
286 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
287 goto out_putf;
289 error = -EINVAL;
290 /* Cannot ftruncate over 2^31 bytes without large file support */
291 if (small && length > MAX_NON_LFS)
292 goto out_putf;
294 error = -EPERM;
295 if (IS_APPEND(inode))
296 goto out_putf;
298 error = locks_verify_truncate(inode, file, length);
299 if (!error)
300 error = do_truncate(dentry, length);
301 out_putf:
302 fput(file);
303 out:
304 return error;
307 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
309 return do_sys_ftruncate(fd, length, 1);
312 /* LFS versions of truncate are only needed on 32 bit machines */
313 #if BITS_PER_LONG == 32
314 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
316 return do_sys_truncate(path, length);
319 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
321 return do_sys_ftruncate(fd, length, 0);
323 #endif
325 #if !(defined(__alpha__) || defined(__ia64__))
328 * sys_utime() can be implemented in user-level using sys_utimes().
329 * Is this for backwards compatibility? If so, why not move it
330 * into the appropriate arch directory (for those architectures that
331 * need it).
334 /* If times==NULL, set access and modification to current time,
335 * must be owner or have write permission.
336 * Else, update from *times, must be owner or super user.
338 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
340 int error;
341 struct nameidata nd;
342 struct inode * inode;
343 struct iattr newattrs;
345 error = user_path_walk(filename, &nd);
346 if (error)
347 goto out;
348 inode = nd.dentry->d_inode;
350 error = -EROFS;
351 if (IS_RDONLY(inode))
352 goto dput_and_out;
354 /* Don't worry, the checks are done in inode_change_ok() */
355 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
356 if (times) {
357 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
358 newattrs.ia_atime.tv_nsec = 0;
359 if (!error)
360 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
361 newattrs.ia_mtime.tv_nsec = 0;
362 if (error)
363 goto dput_and_out;
365 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
366 } else {
367 if (current->fsuid != inode->i_uid &&
368 (error = permission(inode,MAY_WRITE,&nd)) != 0)
369 goto dput_and_out;
371 down(&inode->i_sem);
372 error = notify_change(nd.dentry, &newattrs);
373 up(&inode->i_sem);
374 dput_and_out:
375 path_release(&nd);
376 out:
377 return error;
380 #endif
382 /* If times==NULL, set access and modification to current time,
383 * must be owner or have write permission.
384 * Else, update from *times, must be owner or super user.
386 long do_utimes(char __user * filename, struct timeval * times)
388 int error;
389 struct nameidata nd;
390 struct inode * inode;
391 struct iattr newattrs;
393 error = user_path_walk(filename, &nd);
395 if (error)
396 goto out;
397 inode = nd.dentry->d_inode;
399 error = -EROFS;
400 if (IS_RDONLY(inode))
401 goto dput_and_out;
403 /* Don't worry, the checks are done in inode_change_ok() */
404 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
405 if (times) {
406 newattrs.ia_atime.tv_sec = times[0].tv_sec;
407 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
408 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
409 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
410 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
411 } else {
412 if (current->fsuid != inode->i_uid &&
413 (error = permission(inode,MAY_WRITE,&nd)) != 0)
414 goto dput_and_out;
416 down(&inode->i_sem);
417 error = notify_change(nd.dentry, &newattrs);
418 up(&inode->i_sem);
419 dput_and_out:
420 path_release(&nd);
421 out:
422 return error;
425 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
427 struct timeval times[2];
429 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
430 return -EFAULT;
431 return do_utimes(filename, utimes ? times : NULL);
436 * access() needs to use the real uid/gid, not the effective uid/gid.
437 * We do this by temporarily clearing all FS-related capabilities and
438 * switching the fsuid/fsgid around to the real ones.
440 asmlinkage long sys_access(const char __user * filename, int mode)
442 struct nameidata nd;
443 int old_fsuid, old_fsgid;
444 kernel_cap_t old_cap;
445 int res;
447 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
448 return -EINVAL;
450 old_fsuid = current->fsuid;
451 old_fsgid = current->fsgid;
452 old_cap = current->cap_effective;
454 current->fsuid = current->uid;
455 current->fsgid = current->gid;
458 * Clear the capabilities if we switch to a non-root user
460 * FIXME: There is a race here against sys_capset. The
461 * capabilities can change yet we will restore the old
462 * value below. We should hold task_capabilities_lock,
463 * but we cannot because user_path_walk can sleep.
465 if (current->uid)
466 cap_clear(current->cap_effective);
467 else
468 current->cap_effective = current->cap_permitted;
470 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
471 if (!res) {
472 res = permission(nd.dentry->d_inode, mode, &nd);
473 /* SuS v2 requires we report a read only fs too */
474 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
475 && !special_file(nd.dentry->d_inode->i_mode))
476 res = -EROFS;
477 path_release(&nd);
480 current->fsuid = old_fsuid;
481 current->fsgid = old_fsgid;
482 current->cap_effective = old_cap;
484 return res;
487 asmlinkage long sys_chdir(const char __user * filename)
489 struct nameidata nd;
490 int error;
492 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
493 if (error)
494 goto out;
496 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
497 if (error)
498 goto dput_and_out;
500 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
502 dput_and_out:
503 path_release(&nd);
504 out:
505 return error;
508 asmlinkage long sys_fchdir(unsigned int fd)
510 struct file *file;
511 struct dentry *dentry;
512 struct inode *inode;
513 struct vfsmount *mnt;
514 int error;
516 error = -EBADF;
517 file = fget(fd);
518 if (!file)
519 goto out;
521 dentry = file->f_dentry;
522 mnt = file->f_vfsmnt;
523 inode = dentry->d_inode;
525 error = -ENOTDIR;
526 if (!S_ISDIR(inode->i_mode))
527 goto out_putf;
529 error = permission(inode, MAY_EXEC, NULL);
530 if (!error)
531 set_fs_pwd(current->fs, mnt, dentry);
532 out_putf:
533 fput(file);
534 out:
535 return error;
538 asmlinkage long sys_chroot(const char __user * filename)
540 struct nameidata nd;
541 int error;
543 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
544 if (error)
545 goto out;
547 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
548 if (error)
549 goto dput_and_out;
551 error = -EPERM;
552 if (!capable(CAP_SYS_CHROOT))
553 goto dput_and_out;
555 set_fs_root(current->fs, nd.mnt, nd.dentry);
556 set_fs_altroot();
557 error = 0;
558 dput_and_out:
559 path_release(&nd);
560 out:
561 return error;
564 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
566 struct inode * inode;
567 struct dentry * dentry;
568 struct file * file;
569 int err = -EBADF;
570 struct iattr newattrs;
572 file = fget(fd);
573 if (!file)
574 goto out;
576 dentry = file->f_dentry;
577 inode = dentry->d_inode;
579 err = -EROFS;
580 if (IS_RDONLY(inode))
581 goto out_putf;
582 err = -EPERM;
583 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
584 goto out_putf;
585 down(&inode->i_sem);
586 if (mode == (mode_t) -1)
587 mode = inode->i_mode;
588 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
589 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
590 err = notify_change(dentry, &newattrs);
591 up(&inode->i_sem);
593 out_putf:
594 fput(file);
595 out:
596 return err;
599 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
601 struct nameidata nd;
602 struct inode * inode;
603 int error;
604 struct iattr newattrs;
606 error = user_path_walk(filename, &nd);
607 if (error)
608 goto out;
609 inode = nd.dentry->d_inode;
611 error = -EROFS;
612 if (IS_RDONLY(inode))
613 goto dput_and_out;
615 error = -EPERM;
616 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
617 goto dput_and_out;
619 down(&inode->i_sem);
620 if (mode == (mode_t) -1)
621 mode = inode->i_mode;
622 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
623 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
624 error = notify_change(nd.dentry, &newattrs);
625 up(&inode->i_sem);
627 dput_and_out:
628 path_release(&nd);
629 out:
630 return error;
633 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
635 struct inode * inode;
636 int error;
637 struct iattr newattrs;
639 error = -ENOENT;
640 if (!(inode = dentry->d_inode)) {
641 printk(KERN_ERR "chown_common: NULL inode\n");
642 goto out;
644 error = -EROFS;
645 if (IS_RDONLY(inode))
646 goto out;
647 error = -EPERM;
648 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
649 goto out;
650 newattrs.ia_valid = ATTR_CTIME;
651 if (user != (uid_t) -1) {
652 newattrs.ia_valid |= ATTR_UID;
653 newattrs.ia_uid = user;
655 if (group != (gid_t) -1) {
656 newattrs.ia_valid |= ATTR_GID;
657 newattrs.ia_gid = group;
659 if (!S_ISDIR(inode->i_mode))
660 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
661 down(&inode->i_sem);
662 error = notify_change(dentry, &newattrs);
663 up(&inode->i_sem);
664 out:
665 return error;
668 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
670 struct nameidata nd;
671 int error;
673 error = user_path_walk(filename, &nd);
674 if (!error) {
675 error = chown_common(nd.dentry, user, group);
676 path_release(&nd);
678 return error;
681 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
683 struct nameidata nd;
684 int error;
686 error = user_path_walk_link(filename, &nd);
687 if (!error) {
688 error = chown_common(nd.dentry, user, group);
689 path_release(&nd);
691 return error;
695 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
697 struct file * file;
698 int error = -EBADF;
700 file = fget(fd);
701 if (file) {
702 error = chown_common(file->f_dentry, user, group);
703 fput(file);
705 return error;
709 * Note that while the flag value (low two bits) for sys_open means:
710 * 00 - read-only
711 * 01 - write-only
712 * 10 - read-write
713 * 11 - special
714 * it is changed into
715 * 00 - no permissions needed
716 * 01 - read-permission
717 * 10 - write-permission
718 * 11 - read-write
719 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
720 * used by symlinks.
722 struct file *filp_open(const char * filename, int flags, int mode)
724 int namei_flags, error;
725 struct nameidata nd;
727 namei_flags = flags;
728 if ((namei_flags+1) & O_ACCMODE)
729 namei_flags++;
730 if (namei_flags & O_TRUNC)
731 namei_flags |= 2;
733 error = open_namei(filename, namei_flags, mode, &nd);
734 if (!error)
735 return dentry_open(nd.dentry, nd.mnt, flags);
737 return ERR_PTR(error);
740 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
742 struct file * f;
743 struct inode *inode;
744 int error;
746 error = -ENFILE;
747 f = get_empty_filp();
748 if (!f)
749 goto cleanup_dentry;
750 f->f_flags = flags;
751 f->f_mode = (flags+1) & O_ACCMODE;
752 inode = dentry->d_inode;
753 if (f->f_mode & FMODE_WRITE) {
754 error = get_write_access(inode);
755 if (error)
756 goto cleanup_file;
759 file_ra_state_init(&f->f_ra, inode->i_mapping);
760 f->f_dentry = dentry;
761 f->f_vfsmnt = mnt;
762 f->f_pos = 0;
763 f->f_op = fops_get(inode->i_fop);
764 file_move(f, &inode->i_sb->s_files);
766 if (f->f_op && f->f_op->open) {
767 error = f->f_op->open(inode,f);
768 if (error)
769 goto cleanup_all;
771 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
773 /* NB: we're sure to have correct a_ops only after f_op->open */
774 if (f->f_flags & O_DIRECT) {
775 if (!inode->i_mapping || !inode->i_mapping->a_ops ||
776 !inode->i_mapping->a_ops->direct_IO) {
777 fput(f);
778 f = ERR_PTR(-EINVAL);
782 return f;
784 cleanup_all:
785 fops_put(f->f_op);
786 if (f->f_mode & FMODE_WRITE)
787 put_write_access(inode);
788 file_kill(f);
789 f->f_dentry = NULL;
790 f->f_vfsmnt = NULL;
791 cleanup_file:
792 put_filp(f);
793 cleanup_dentry:
794 dput(dentry);
795 mntput(mnt);
796 return ERR_PTR(error);
800 * Find an empty file descriptor entry, and mark it busy.
802 int get_unused_fd(void)
804 struct files_struct * files = current->files;
805 int fd, error;
807 error = -EMFILE;
808 spin_lock(&files->file_lock);
810 repeat:
811 fd = find_next_zero_bit(files->open_fds->fds_bits,
812 files->max_fdset,
813 files->next_fd);
816 * N.B. For clone tasks sharing a files structure, this test
817 * will limit the total number of files that can be opened.
819 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
820 goto out;
822 /* Do we need to expand the fdset array? */
823 if (fd >= files->max_fdset) {
824 error = expand_fdset(files, fd);
825 if (!error) {
826 error = -EMFILE;
827 goto repeat;
829 goto out;
833 * Check whether we need to expand the fd array.
835 if (fd >= files->max_fds) {
836 error = expand_fd_array(files, fd);
837 if (!error) {
838 error = -EMFILE;
839 goto repeat;
841 goto out;
844 FD_SET(fd, files->open_fds);
845 FD_CLR(fd, files->close_on_exec);
846 files->next_fd = fd + 1;
847 #if 1
848 /* Sanity check */
849 if (files->fd[fd] != NULL) {
850 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
851 files->fd[fd] = NULL;
853 #endif
854 error = fd;
856 out:
857 spin_unlock(&files->file_lock);
858 return error;
861 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
863 __FD_CLR(fd, files->open_fds);
864 if (fd < files->next_fd)
865 files->next_fd = fd;
868 void put_unused_fd(unsigned int fd)
870 struct files_struct *files = current->files;
871 spin_lock(&files->file_lock);
872 __put_unused_fd(files, fd);
873 spin_unlock(&files->file_lock);
877 * Install a file pointer in the fd array.
879 * The VFS is full of places where we drop the files lock between
880 * setting the open_fds bitmap and installing the file in the file
881 * array. At any such point, we are vulnerable to a dup2() race
882 * installing a file in the array before us. We need to detect this and
883 * fput() the struct file we are about to overwrite in this case.
885 * It should never happen - if we allow dup2() do it, _really_ bad things
886 * will follow.
889 void fd_install(unsigned int fd, struct file * file)
891 struct files_struct *files = current->files;
892 spin_lock(&files->file_lock);
893 if (unlikely(files->fd[fd] != NULL))
894 BUG();
895 files->fd[fd] = file;
896 spin_unlock(&files->file_lock);
899 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
901 char * tmp;
902 int fd, error;
904 #if BITS_PER_LONG != 32
905 flags |= O_LARGEFILE;
906 #endif
907 tmp = getname(filename);
908 fd = PTR_ERR(tmp);
909 if (!IS_ERR(tmp)) {
910 fd = get_unused_fd();
911 if (fd >= 0) {
912 struct file *f = filp_open(tmp, flags, mode);
913 error = PTR_ERR(f);
914 if (IS_ERR(f))
915 goto out_error;
916 fd_install(fd, f);
918 out:
919 putname(tmp);
921 return fd;
923 out_error:
924 put_unused_fd(fd);
925 fd = error;
926 goto out;
929 #ifndef __alpha__
932 * For backward compatibility? Maybe this should be moved
933 * into arch/i386 instead?
935 asmlinkage long sys_creat(const char __user * pathname, int mode)
937 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
940 #endif
943 * "id" is the POSIX thread ID. We use the
944 * files pointer for this..
946 int filp_close(struct file *filp, fl_owner_t id)
948 int retval;
950 if (!file_count(filp)) {
951 printk(KERN_ERR "VFS: Close: file count is 0\n");
952 return 0;
954 retval = 0;
955 if (filp->f_op && filp->f_op->flush)
956 retval = filp->f_op->flush(filp);
957 dnotify_flush(filp, id);
958 locks_remove_posix(filp, id);
959 fput(filp);
960 return retval;
964 * Careful here! We test whether the file pointer is NULL before
965 * releasing the fd. This ensures that one clone task can't release
966 * an fd while another clone is opening it.
968 asmlinkage long sys_close(unsigned int fd)
970 struct file * filp;
971 struct files_struct *files = current->files;
973 spin_lock(&files->file_lock);
974 if (fd >= files->max_fds)
975 goto out_unlock;
976 filp = files->fd[fd];
977 if (!filp)
978 goto out_unlock;
979 files->fd[fd] = NULL;
980 FD_CLR(fd, files->close_on_exec);
981 __put_unused_fd(files, fd);
982 spin_unlock(&files->file_lock);
983 return filp_close(filp, files);
985 out_unlock:
986 spin_unlock(&files->file_lock);
987 return -EBADF;
991 * This routine simulates a hangup on the tty, to arrange that users
992 * are given clean terminals at login time.
994 asmlinkage long sys_vhangup(void)
996 if (capable(CAP_SYS_TTY_CONFIG)) {
997 tty_vhangup(current->tty);
998 return 0;
1000 return -EPERM;
1004 * Called when an inode is about to be open.
1005 * We use this to disallow opening large files on 32bit systems if
1006 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1007 * on this flag in sys_open.
1009 int generic_file_open(struct inode * inode, struct file * filp)
1011 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1012 return -EFBIG;
1013 return 0;
1016 EXPORT_SYMBOL(generic_file_open);