4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/string.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/fsnotify.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/capability.h>
20 #include <linux/security.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
26 #include <linux/personality.h>
27 #include <linux/pagemap.h>
28 #include <linux/syscalls.h>
29 #include <linux/rcupdate.h>
30 #include <linux/audit.h>
32 #include <asm/unistd.h>
34 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
40 if (sb->s_op->statfs) {
41 memset(buf, 0, sizeof(*buf));
42 retval = security_sb_statfs(sb);
45 retval = sb->s_op->statfs(sb, buf);
46 if (retval == 0 && buf->f_frsize == 0)
47 buf->f_frsize = buf->f_bsize;
53 EXPORT_SYMBOL(vfs_statfs);
55 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
60 retval = vfs_statfs(sb, &st);
64 if (sizeof(*buf) == sizeof(st))
65 memcpy(buf, &st, sizeof(st));
67 if (sizeof buf->f_blocks == 4) {
68 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
69 0xffffffff00000000ULL)
72 * f_files and f_ffree may be -1; it's okay to stuff
75 if (st.f_files != -1 &&
76 (st.f_files & 0xffffffff00000000ULL))
78 if (st.f_ffree != -1 &&
79 (st.f_ffree & 0xffffffff00000000ULL))
83 buf->f_type = st.f_type;
84 buf->f_bsize = st.f_bsize;
85 buf->f_blocks = st.f_blocks;
86 buf->f_bfree = st.f_bfree;
87 buf->f_bavail = st.f_bavail;
88 buf->f_files = st.f_files;
89 buf->f_ffree = st.f_ffree;
90 buf->f_fsid = st.f_fsid;
91 buf->f_namelen = st.f_namelen;
92 buf->f_frsize = st.f_frsize;
93 memset(buf->f_spare, 0, sizeof(buf->f_spare));
98 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
103 retval = vfs_statfs(sb, &st);
107 if (sizeof(*buf) == sizeof(st))
108 memcpy(buf, &st, sizeof(st));
110 buf->f_type = st.f_type;
111 buf->f_bsize = st.f_bsize;
112 buf->f_blocks = st.f_blocks;
113 buf->f_bfree = st.f_bfree;
114 buf->f_bavail = st.f_bavail;
115 buf->f_files = st.f_files;
116 buf->f_ffree = st.f_ffree;
117 buf->f_fsid = st.f_fsid;
118 buf->f_namelen = st.f_namelen;
119 buf->f_frsize = st.f_frsize;
120 memset(buf->f_spare, 0, sizeof(buf->f_spare));
125 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
130 error = user_path_walk(path, &nd);
133 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
134 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
142 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
147 if (sz != sizeof(*buf))
149 error = user_path_walk(path, &nd);
152 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
153 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
161 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
171 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
172 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
179 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
185 if (sz != sizeof(*buf))
192 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
193 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
200 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
204 struct iattr newattrs;
206 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
210 newattrs.ia_size = length;
211 newattrs.ia_valid = ATTR_SIZE | time_attrs;
213 newattrs.ia_file = filp;
214 newattrs.ia_valid |= ATTR_FILE;
217 mutex_lock(&dentry->d_inode->i_mutex);
218 err = notify_change(dentry, &newattrs);
219 mutex_unlock(&dentry->d_inode->i_mutex);
223 static long do_sys_truncate(const char __user * path, loff_t length)
226 struct inode * inode;
230 if (length < 0) /* sorry, but loff_t says... */
233 error = user_path_walk(path, &nd);
236 inode = nd.dentry->d_inode;
238 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
240 if (S_ISDIR(inode->i_mode))
244 if (!S_ISREG(inode->i_mode))
247 error = vfs_permission(&nd, MAY_WRITE);
252 if (IS_RDONLY(inode))
256 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
260 * Make sure that there are no leases.
262 error = break_lease(inode, FMODE_WRITE);
266 error = get_write_access(inode);
270 error = locks_verify_truncate(inode, NULL, length);
273 error = do_truncate(nd.dentry, length, 0, NULL);
275 put_write_access(inode);
283 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
285 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
286 return do_sys_truncate(path, (long)length);
289 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
291 struct inode * inode;
292 struct dentry *dentry;
304 /* explicitly opened as large or we are on 64-bit box */
305 if (file->f_flags & O_LARGEFILE)
308 dentry = file->f_dentry;
309 inode = dentry->d_inode;
311 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
315 /* Cannot ftruncate over 2^31 bytes without large file support */
316 if (small && length > MAX_NON_LFS)
320 if (IS_APPEND(inode))
323 error = locks_verify_truncate(inode, file, length);
325 error = do_truncate(dentry, length, 0, file);
332 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
334 long ret = do_sys_ftruncate(fd, length, 1);
335 prevent_tail_call(ret);
339 /* LFS versions of truncate are only needed on 32 bit machines */
340 #if BITS_PER_LONG == 32
341 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
343 return do_sys_truncate(path, length);
346 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
348 long ret = do_sys_ftruncate(fd, length, 0);
349 prevent_tail_call(ret);
354 #ifdef __ARCH_WANT_SYS_UTIME
357 * sys_utime() can be implemented in user-level using sys_utimes().
358 * Is this for backwards compatibility? If so, why not move it
359 * into the appropriate arch directory (for those architectures that
363 /* If times==NULL, set access and modification to current time,
364 * must be owner or have write permission.
365 * Else, update from *times, must be owner or super user.
367 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
371 struct inode * inode;
372 struct iattr newattrs;
374 error = user_path_walk(filename, &nd);
377 inode = nd.dentry->d_inode;
380 if (IS_RDONLY(inode))
383 /* Don't worry, the checks are done in inode_change_ok() */
384 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
387 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
390 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
391 newattrs.ia_atime.tv_nsec = 0;
393 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
394 newattrs.ia_mtime.tv_nsec = 0;
398 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
401 if (IS_IMMUTABLE(inode))
404 if (current->fsuid != inode->i_uid &&
405 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
408 mutex_lock(&inode->i_mutex);
409 error = notify_change(nd.dentry, &newattrs);
410 mutex_unlock(&inode->i_mutex);
419 /* If times==NULL, set access and modification to current time,
420 * must be owner or have write permission.
421 * Else, update from *times, must be owner or super user.
423 long do_utimes(int dfd, char __user *filename, struct timeval *times)
427 struct inode * inode;
428 struct iattr newattrs;
430 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
434 inode = nd.dentry->d_inode;
437 if (IS_RDONLY(inode))
440 /* Don't worry, the checks are done in inode_change_ok() */
441 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
444 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
447 newattrs.ia_atime.tv_sec = times[0].tv_sec;
448 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
449 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
450 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
451 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
454 if (IS_IMMUTABLE(inode))
457 if (current->fsuid != inode->i_uid &&
458 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
461 mutex_lock(&inode->i_mutex);
462 error = notify_change(nd.dentry, &newattrs);
463 mutex_unlock(&inode->i_mutex);
470 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
472 struct timeval times[2];
474 if (utimes && copy_from_user(×, utimes, sizeof(times)))
476 return do_utimes(dfd, filename, utimes ? times : NULL);
479 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
481 return sys_futimesat(AT_FDCWD, filename, utimes);
486 * access() needs to use the real uid/gid, not the effective uid/gid.
487 * We do this by temporarily clearing all FS-related capabilities and
488 * switching the fsuid/fsgid around to the real ones.
490 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
493 int old_fsuid, old_fsgid;
494 kernel_cap_t old_cap;
497 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
500 old_fsuid = current->fsuid;
501 old_fsgid = current->fsgid;
502 old_cap = current->cap_effective;
504 current->fsuid = current->uid;
505 current->fsgid = current->gid;
508 * Clear the capabilities if we switch to a non-root user
510 * FIXME: There is a race here against sys_capset. The
511 * capabilities can change yet we will restore the old
512 * value below. We should hold task_capabilities_lock,
513 * but we cannot because user_path_walk can sleep.
516 cap_clear(current->cap_effective);
518 current->cap_effective = current->cap_permitted;
520 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
522 res = vfs_permission(&nd, mode);
523 /* SuS v2 requires we report a read only fs too */
524 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
525 && !special_file(nd.dentry->d_inode->i_mode))
530 current->fsuid = old_fsuid;
531 current->fsgid = old_fsgid;
532 current->cap_effective = old_cap;
537 asmlinkage long sys_access(const char __user *filename, int mode)
539 return sys_faccessat(AT_FDCWD, filename, mode);
542 asmlinkage long sys_chdir(const char __user * filename)
547 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
551 error = vfs_permission(&nd, MAY_EXEC);
555 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
563 asmlinkage long sys_fchdir(unsigned int fd)
566 struct dentry *dentry;
568 struct vfsmount *mnt;
576 dentry = file->f_dentry;
577 mnt = file->f_vfsmnt;
578 inode = dentry->d_inode;
581 if (!S_ISDIR(inode->i_mode))
584 error = file_permission(file, MAY_EXEC);
586 set_fs_pwd(current->fs, mnt, dentry);
593 asmlinkage long sys_chroot(const char __user * filename)
598 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
602 error = vfs_permission(&nd, MAY_EXEC);
607 if (!capable(CAP_SYS_CHROOT))
610 set_fs_root(current->fs, nd.mnt, nd.dentry);
619 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
621 struct inode * inode;
622 struct dentry * dentry;
625 struct iattr newattrs;
631 dentry = file->f_dentry;
632 inode = dentry->d_inode;
634 audit_inode(NULL, inode, 0);
637 if (IS_RDONLY(inode))
640 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
642 mutex_lock(&inode->i_mutex);
643 if (mode == (mode_t) -1)
644 mode = inode->i_mode;
645 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
646 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
647 err = notify_change(dentry, &newattrs);
648 mutex_unlock(&inode->i_mutex);
656 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
660 struct inode * inode;
662 struct iattr newattrs;
664 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
667 inode = nd.dentry->d_inode;
670 if (IS_RDONLY(inode))
674 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
677 mutex_lock(&inode->i_mutex);
678 if (mode == (mode_t) -1)
679 mode = inode->i_mode;
680 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
681 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
682 error = notify_change(nd.dentry, &newattrs);
683 mutex_unlock(&inode->i_mutex);
691 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
693 return sys_fchmodat(AT_FDCWD, filename, mode);
696 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
698 struct inode * inode;
700 struct iattr newattrs;
703 if (!(inode = dentry->d_inode)) {
704 printk(KERN_ERR "chown_common: NULL inode\n");
708 if (IS_RDONLY(inode))
711 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
713 newattrs.ia_valid = ATTR_CTIME;
714 if (user != (uid_t) -1) {
715 newattrs.ia_valid |= ATTR_UID;
716 newattrs.ia_uid = user;
718 if (group != (gid_t) -1) {
719 newattrs.ia_valid |= ATTR_GID;
720 newattrs.ia_gid = group;
722 if (!S_ISDIR(inode->i_mode))
723 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
724 mutex_lock(&inode->i_mutex);
725 error = notify_change(dentry, &newattrs);
726 mutex_unlock(&inode->i_mutex);
731 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
736 error = user_path_walk(filename, &nd);
738 error = chown_common(nd.dentry, user, group);
744 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
745 gid_t group, int flag)
751 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
754 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
755 error = __user_walk_fd(dfd, filename, follow, &nd);
757 error = chown_common(nd.dentry, user, group);
764 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
769 error = user_path_walk_link(filename, &nd);
771 error = chown_common(nd.dentry, user, group);
778 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
785 struct dentry * dentry;
786 dentry = file->f_dentry;
787 audit_inode(NULL, dentry->d_inode, 0);
788 error = chown_common(dentry, user, group);
794 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
795 int flags, struct file *f,
796 int (*open)(struct inode *, struct file *))
802 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
803 FMODE_PREAD | FMODE_PWRITE;
804 inode = dentry->d_inode;
805 if (f->f_mode & FMODE_WRITE) {
806 error = get_write_access(inode);
811 f->f_mapping = inode->i_mapping;
812 f->f_dentry = dentry;
815 f->f_op = fops_get(inode->i_fop);
816 file_move(f, &inode->i_sb->s_files);
818 if (!open && f->f_op)
819 open = f->f_op->open;
821 error = open(inode, f);
826 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
828 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
830 /* NB: we're sure to have correct a_ops only after f_op->open */
831 if (f->f_flags & O_DIRECT) {
832 if (!f->f_mapping->a_ops ||
833 ((!f->f_mapping->a_ops->direct_IO) &&
834 (!f->f_mapping->a_ops->get_xip_page))) {
836 f = ERR_PTR(-EINVAL);
844 if (f->f_mode & FMODE_WRITE)
845 put_write_access(inode);
853 return ERR_PTR(error);
857 * Note that while the flag value (low two bits) for sys_open means:
863 * 00 - no permissions needed
864 * 01 - read-permission
865 * 10 - write-permission
867 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
870 static struct file *do_filp_open(int dfd, const char *filename, int flags,
873 int namei_flags, error;
877 if ((namei_flags+1) & O_ACCMODE)
880 error = open_namei(dfd, filename, namei_flags, mode, &nd);
882 return nameidata_to_filp(&nd, flags);
884 return ERR_PTR(error);
887 struct file *filp_open(const char *filename, int flags, int mode)
889 return do_filp_open(AT_FDCWD, filename, flags, mode);
891 EXPORT_SYMBOL(filp_open);
894 * lookup_instantiate_filp - instantiates the open intent filp
895 * @nd: pointer to nameidata
896 * @dentry: pointer to dentry
897 * @open: open callback
899 * Helper for filesystems that want to use lookup open intents and pass back
900 * a fully instantiated struct file to the caller.
901 * This function is meant to be called from within a filesystem's
903 * Beware of calling it for non-regular files! Those ->open methods might block
904 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
905 * leading to a deadlock, as nobody can open that fifo anymore, because
906 * another process to open fifo will block on locked parent when doing lookup).
907 * Note that in case of error, nd->intent.open.file is destroyed, but the
908 * path information remains valid.
909 * If the open callback is set to NULL, then the standard f_op->open()
910 * filesystem callback is substituted.
912 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
913 int (*open)(struct inode *, struct file *))
915 if (IS_ERR(nd->intent.open.file))
919 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
920 nd->intent.open.flags - 1,
921 nd->intent.open.file,
924 return nd->intent.open.file;
926 release_open_intent(nd);
927 nd->intent.open.file = (struct file *)dentry;
930 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
933 * nameidata_to_filp - convert a nameidata to an open filp.
934 * @nd: pointer to nameidata
937 * Note that this function destroys the original nameidata
939 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
943 /* Pick up the filp from the open intent */
944 filp = nd->intent.open.file;
945 /* Has the filesystem initialised the file for us? */
946 if (filp->f_dentry == NULL)
947 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
954 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
957 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
963 f = get_empty_filp();
967 return ERR_PTR(error);
970 return __dentry_open(dentry, mnt, flags, f, NULL);
972 EXPORT_SYMBOL(dentry_open);
975 * Find an empty file descriptor entry, and mark it busy.
977 int get_unused_fd(void)
979 struct files_struct * files = current->files;
984 spin_lock(&files->file_lock);
987 fdt = files_fdtable(files);
988 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
993 * N.B. For clone tasks sharing a files structure, this test
994 * will limit the total number of files that can be opened.
996 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
999 /* Do we need to expand the fd array or fd set? */
1000 error = expand_files(files, fd);
1006 * If we needed to expand the fs array we
1007 * might have blocked - try again.
1013 FD_SET(fd, fdt->open_fds);
1014 FD_CLR(fd, fdt->close_on_exec);
1015 files->next_fd = fd + 1;
1018 if (fdt->fd[fd] != NULL) {
1019 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1026 spin_unlock(&files->file_lock);
1030 EXPORT_SYMBOL(get_unused_fd);
1032 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1034 struct fdtable *fdt = files_fdtable(files);
1035 __FD_CLR(fd, fdt->open_fds);
1036 if (fd < files->next_fd)
1037 files->next_fd = fd;
1040 void fastcall put_unused_fd(unsigned int fd)
1042 struct files_struct *files = current->files;
1043 spin_lock(&files->file_lock);
1044 __put_unused_fd(files, fd);
1045 spin_unlock(&files->file_lock);
1048 EXPORT_SYMBOL(put_unused_fd);
1051 * Install a file pointer in the fd array.
1053 * The VFS is full of places where we drop the files lock between
1054 * setting the open_fds bitmap and installing the file in the file
1055 * array. At any such point, we are vulnerable to a dup2() race
1056 * installing a file in the array before us. We need to detect this and
1057 * fput() the struct file we are about to overwrite in this case.
1059 * It should never happen - if we allow dup2() do it, _really_ bad things
1063 void fastcall fd_install(unsigned int fd, struct file * file)
1065 struct files_struct *files = current->files;
1066 struct fdtable *fdt;
1067 spin_lock(&files->file_lock);
1068 fdt = files_fdtable(files);
1069 BUG_ON(fdt->fd[fd] != NULL);
1070 rcu_assign_pointer(fdt->fd[fd], file);
1071 spin_unlock(&files->file_lock);
1074 EXPORT_SYMBOL(fd_install);
1076 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1078 char *tmp = getname(filename);
1079 int fd = PTR_ERR(tmp);
1082 fd = get_unused_fd();
1084 struct file *f = do_filp_open(dfd, tmp, flags, mode);
1089 fsnotify_open(f->f_dentry);
1098 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1100 if (force_o_largefile())
1101 flags |= O_LARGEFILE;
1103 return do_sys_open(AT_FDCWD, filename, flags, mode);
1105 EXPORT_SYMBOL_GPL(sys_open);
1107 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1110 if (force_o_largefile())
1111 flags |= O_LARGEFILE;
1113 return do_sys_open(dfd, filename, flags, mode);
1115 EXPORT_SYMBOL_GPL(sys_openat);
1120 * For backward compatibility? Maybe this should be moved
1121 * into arch/i386 instead?
1123 asmlinkage long sys_creat(const char __user * pathname, int mode)
1125 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1131 * "id" is the POSIX thread ID. We use the
1132 * files pointer for this..
1134 int filp_close(struct file *filp, fl_owner_t id)
1138 if (!file_count(filp)) {
1139 printk(KERN_ERR "VFS: Close: file count is 0\n");
1143 if (filp->f_op && filp->f_op->flush)
1144 retval = filp->f_op->flush(filp);
1146 dnotify_flush(filp, id);
1147 locks_remove_posix(filp, id);
1152 EXPORT_SYMBOL(filp_close);
1155 * Careful here! We test whether the file pointer is NULL before
1156 * releasing the fd. This ensures that one clone task can't release
1157 * an fd while another clone is opening it.
1159 asmlinkage long sys_close(unsigned int fd)
1162 struct files_struct *files = current->files;
1163 struct fdtable *fdt;
1165 spin_lock(&files->file_lock);
1166 fdt = files_fdtable(files);
1167 if (fd >= fdt->max_fds)
1172 rcu_assign_pointer(fdt->fd[fd], NULL);
1173 FD_CLR(fd, fdt->close_on_exec);
1174 __put_unused_fd(files, fd);
1175 spin_unlock(&files->file_lock);
1176 return filp_close(filp, files);
1179 spin_unlock(&files->file_lock);
1183 EXPORT_SYMBOL(sys_close);
1186 * This routine simulates a hangup on the tty, to arrange that users
1187 * are given clean terminals at login time.
1189 asmlinkage long sys_vhangup(void)
1191 if (capable(CAP_SYS_TTY_CONFIG)) {
1192 tty_vhangup(current->signal->tty);
1199 * Called when an inode is about to be open.
1200 * We use this to disallow opening large files on 32bit systems if
1201 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1202 * on this flag in sys_open.
1204 int generic_file_open(struct inode * inode, struct file * filp)
1206 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1211 EXPORT_SYMBOL(generic_file_open);
1214 * This is used by subsystems that don't want seekable
1217 int nonseekable_open(struct inode *inode, struct file *filp)
1219 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1223 EXPORT_SYMBOL(nonseekable_open);