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/dnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/iobuf.h>
19 #include <asm/uaccess.h>
21 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
23 int vfs_statfs(struct super_block *sb, struct statfs *buf)
29 if (sb->s_op && sb->s_op->statfs) {
30 memset(buf, 0, sizeof(struct statfs));
32 retval = sb->s_op->statfs(sb, buf);
40 asmlinkage long sys_statfs(const char * path, struct statfs * buf)
45 error = user_path_walk(path, &nd);
48 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
49 if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
56 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs * buf)
66 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
67 if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
75 * Install a file pointer in the fd array.
77 * The VFS is full of places where we drop the files lock between
78 * setting the open_fds bitmap and installing the file in the file
79 * array. At any such point, we are vulnerable to a dup2() race
80 * installing a file in the array before us. We need to detect this and
81 * fput() the struct file we are about to overwrite in this case.
83 * It should never happen - if we allow dup2() do it, _really_ bad things
87 void fd_install(unsigned int fd, struct file * file)
89 struct files_struct *files = current->files;
91 write_lock(&files->file_lock);
95 write_unlock(&files->file_lock);
98 int do_truncate(struct dentry *dentry, loff_t length)
100 struct inode *inode = dentry->d_inode;
102 struct iattr newattrs;
104 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
109 newattrs.ia_size = length;
110 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
111 error = notify_change(dentry, &newattrs);
116 static inline long do_sys_truncate(const char * path, loff_t length)
119 struct inode * inode;
123 if (length < 0) /* sorry, but loff_t says... */
126 error = user_path_walk(path, &nd);
129 inode = nd.dentry->d_inode;
131 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
133 if (S_ISDIR(inode->i_mode))
137 if (!S_ISREG(inode->i_mode))
140 error = permission(inode,MAY_WRITE);
145 if (IS_RDONLY(inode))
149 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
153 * Make sure that there are no leases.
155 error = get_lease(inode, FMODE_WRITE);
159 error = get_write_access(inode);
163 error = locks_verify_truncate(inode, NULL, length);
166 error = do_truncate(nd.dentry, length);
168 put_write_access(inode);
176 asmlinkage long sys_truncate(const char * path, unsigned long length)
178 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
179 return do_sys_truncate(path, (long)length);
182 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
184 struct inode * inode;
185 struct dentry *dentry;
197 /* explicitly opened as large or we are on 64-bit box */
198 if (file->f_flags & O_LARGEFILE)
201 dentry = file->f_dentry;
202 inode = dentry->d_inode;
204 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
208 /* Cannot ftruncate over 2^31 bytes without large file support */
209 if (small && length > MAX_NON_LFS)
213 if (IS_APPEND(inode))
216 error = locks_verify_truncate(inode, file, length);
218 error = do_truncate(dentry, length);
225 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
227 return do_sys_ftruncate(fd, length, 1);
230 /* LFS versions of truncate are only needed on 32 bit machines */
231 #if BITS_PER_LONG == 32
232 asmlinkage long sys_truncate64(const char * path, loff_t length)
234 return do_sys_truncate(path, length);
237 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
239 return do_sys_ftruncate(fd, length, 0);
243 #if !(defined(__alpha__) || defined(__ia64__))
246 * sys_utime() can be implemented in user-level using sys_utimes().
247 * Is this for backwards compatibility? If so, why not move it
248 * into the appropriate arch directory (for those architectures that
252 /* If times==NULL, set access and modification to current time,
253 * must be owner or have write permission.
254 * Else, update from *times, must be owner or super user.
256 asmlinkage long sys_utime(char * filename, struct utimbuf * times)
260 struct inode * inode;
261 struct iattr newattrs;
263 error = user_path_walk(filename, &nd);
266 inode = nd.dentry->d_inode;
269 if (IS_RDONLY(inode))
272 /* Don't worry, the checks are done in inode_change_ok() */
273 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
275 error = get_user(newattrs.ia_atime, ×->actime);
277 error = get_user(newattrs.ia_mtime, ×->modtime);
281 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
283 if (current->fsuid != inode->i_uid &&
284 (error = permission(inode,MAY_WRITE)) != 0)
287 error = notify_change(nd.dentry, &newattrs);
296 /* If times==NULL, set access and modification to current time,
297 * must be owner or have write permission.
298 * Else, update from *times, must be owner or super user.
300 asmlinkage long sys_utimes(char * filename, struct timeval * utimes)
304 struct inode * inode;
305 struct iattr newattrs;
307 error = user_path_walk(filename, &nd);
311 inode = nd.dentry->d_inode;
314 if (IS_RDONLY(inode))
317 /* Don't worry, the checks are done in inode_change_ok() */
318 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
320 struct timeval times[2];
322 if (copy_from_user(×, utimes, sizeof(times)))
324 newattrs.ia_atime = times[0].tv_sec;
325 newattrs.ia_mtime = times[1].tv_sec;
326 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
328 if (current->fsuid != inode->i_uid &&
329 (error = permission(inode,MAY_WRITE)) != 0)
332 error = notify_change(nd.dentry, &newattrs);
340 * access() needs to use the real uid/gid, not the effective uid/gid.
341 * We do this by temporarily clearing all FS-related capabilities and
342 * switching the fsuid/fsgid around to the real ones.
344 asmlinkage long sys_access(const char * filename, int mode)
347 int old_fsuid, old_fsgid;
348 kernel_cap_t old_cap;
351 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
354 old_fsuid = current->fsuid;
355 old_fsgid = current->fsgid;
356 old_cap = current->cap_effective;
358 current->fsuid = current->uid;
359 current->fsgid = current->gid;
361 /* Clear the capabilities if we switch to a non-root user */
363 cap_clear(current->cap_effective);
365 current->cap_effective = current->cap_permitted;
367 res = user_path_walk(filename, &nd);
369 res = permission(nd.dentry->d_inode, mode);
370 /* SuS v2 requires we report a read only fs too */
371 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
372 && !special_file(nd.dentry->d_inode->i_mode))
377 current->fsuid = old_fsuid;
378 current->fsgid = old_fsgid;
379 current->cap_effective = old_cap;
384 asmlinkage long sys_chdir(const char * filename)
389 error = __user_walk(filename,LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY,&nd);
393 error = permission(nd.dentry->d_inode,MAY_EXEC);
397 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
405 asmlinkage long sys_fchdir(unsigned int fd)
408 struct dentry *dentry;
410 struct vfsmount *mnt;
418 dentry = file->f_dentry;
419 mnt = file->f_vfsmnt;
420 inode = dentry->d_inode;
423 if (!S_ISDIR(inode->i_mode))
426 error = permission(inode, MAY_EXEC);
428 set_fs_pwd(current->fs, mnt, dentry);
435 asmlinkage long sys_chroot(const char * filename)
440 error = __user_walk(filename, LOOKUP_POSITIVE | LOOKUP_FOLLOW |
441 LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
445 error = permission(nd.dentry->d_inode,MAY_EXEC);
450 if (!capable(CAP_SYS_CHROOT))
453 set_fs_root(current->fs, nd.mnt, nd.dentry);
462 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
464 struct inode * inode;
465 struct dentry * dentry;
468 struct iattr newattrs;
474 dentry = file->f_dentry;
475 inode = dentry->d_inode;
478 if (IS_RDONLY(inode))
481 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
483 if (mode == (mode_t) -1)
484 mode = inode->i_mode;
485 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
486 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
487 err = notify_change(dentry, &newattrs);
495 asmlinkage long sys_chmod(const char * filename, mode_t mode)
498 struct inode * inode;
500 struct iattr newattrs;
502 error = user_path_walk(filename, &nd);
505 inode = nd.dentry->d_inode;
508 if (IS_RDONLY(inode))
512 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
515 if (mode == (mode_t) -1)
516 mode = inode->i_mode;
517 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
518 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
519 error = notify_change(nd.dentry, &newattrs);
527 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
529 struct inode * inode;
531 struct iattr newattrs;
534 if (!(inode = dentry->d_inode)) {
535 printk(KERN_ERR "chown_common: NULL inode\n");
539 if (IS_RDONLY(inode))
542 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
544 if (user == (uid_t) -1)
546 if (group == (gid_t) -1)
547 group = inode->i_gid;
548 newattrs.ia_mode = inode->i_mode;
549 newattrs.ia_uid = user;
550 newattrs.ia_gid = group;
551 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
553 * If the user or group of a non-directory has been changed by a
554 * non-root user, remove the setuid bit.
555 * 19981026 David C Niemi <niemi@tux.org>
557 * Changed this to apply to all users, including root, to avoid
558 * some races. This is the behavior we had in 2.0. The check for
559 * non-root was definitely wrong for 2.2 anyway, as it should
560 * have been using CAP_FSETID rather than fsuid -- 19990830 SD.
562 if ((inode->i_mode & S_ISUID) == S_ISUID &&
563 !S_ISDIR(inode->i_mode))
565 newattrs.ia_mode &= ~S_ISUID;
566 newattrs.ia_valid |= ATTR_MODE;
569 * Likewise, if the user or group of a non-directory has been changed
570 * by a non-root user, remove the setgid bit UNLESS there is no group
571 * execute bit (this would be a file marked for mandatory locking).
572 * 19981026 David C Niemi <niemi@tux.org>
574 * Removed the fsuid check (see the comment above) -- 19990830 SD.
576 if (((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
577 && !S_ISDIR(inode->i_mode))
579 newattrs.ia_mode &= ~S_ISGID;
580 newattrs.ia_valid |= ATTR_MODE;
582 error = notify_change(dentry, &newattrs);
587 asmlinkage long sys_chown(const char * filename, uid_t user, gid_t group)
592 error = user_path_walk(filename, &nd);
594 error = chown_common(nd.dentry, user, group);
600 asmlinkage long sys_lchown(const char * filename, uid_t user, gid_t group)
605 error = user_path_walk_link(filename, &nd);
607 error = chown_common(nd.dentry, user, group);
614 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
621 error = chown_common(file->f_dentry, user, group);
628 * Note that while the flag value (low two bits) for sys_open means:
634 * 00 - no permissions needed
635 * 01 - read-permission
636 * 10 - write-permission
638 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
641 struct file *filp_open(const char * filename, int flags, int mode)
643 int namei_flags, error;
647 if ((namei_flags+1) & O_ACCMODE)
649 if (namei_flags & O_TRUNC)
652 error = open_namei(filename, namei_flags, mode, &nd);
654 return dentry_open(nd.dentry, nd.mnt, flags);
656 return ERR_PTR(error);
659 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
663 static LIST_HEAD(kill_list);
667 f = get_empty_filp();
671 f->f_mode = (flags+1) & O_ACCMODE;
672 inode = dentry->d_inode;
673 if (f->f_mode & FMODE_WRITE) {
674 error = get_write_access(inode);
679 f->f_dentry = dentry;
683 f->f_op = fops_get(inode->i_fop);
684 file_move(f, &inode->i_sb->s_files);
686 /* preallocate kiobuf for O_DIRECT */
689 if (f->f_flags & O_DIRECT) {
690 error = alloc_kiovec(1, &f->f_iobuf);
695 if (f->f_op && f->f_op->open) {
696 error = f->f_op->open(inode,f);
700 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
706 free_kiovec(1, &f->f_iobuf);
708 if (f->f_mode & FMODE_WRITE)
709 put_write_access(inode);
710 file_move(f, &kill_list); /* out of the way.. */
718 return ERR_PTR(error);
722 * Find an empty file descriptor entry, and mark it busy.
724 int get_unused_fd(void)
726 struct files_struct * files = current->files;
730 write_lock(&files->file_lock);
733 fd = find_next_zero_bit(files->open_fds,
738 * N.B. For clone tasks sharing a files structure, this test
739 * will limit the total number of files that can be opened.
741 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
744 /* Do we need to expand the fdset array? */
745 if (fd >= files->max_fdset) {
746 error = expand_fdset(files, fd);
755 * Check whether we need to expand the fd array.
757 if (fd >= files->max_fds) {
758 error = expand_fd_array(files, fd);
766 FD_SET(fd, files->open_fds);
767 FD_CLR(fd, files->close_on_exec);
768 files->next_fd = fd + 1;
771 if (files->fd[fd] != NULL) {
772 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
773 files->fd[fd] = NULL;
779 write_unlock(&files->file_lock);
783 asmlinkage long sys_open(const char * filename, int flags, int mode)
788 #if BITS_PER_LONG != 32
789 flags |= O_LARGEFILE;
791 tmp = getname(filename);
794 fd = get_unused_fd();
796 struct file *f = filp_open(tmp, flags, mode);
816 * For backward compatibility? Maybe this should be moved
817 * into arch/i386 instead?
819 asmlinkage long sys_creat(const char * pathname, int mode)
821 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
827 * "id" is the POSIX thread ID. We use the
828 * files pointer for this..
830 int filp_close(struct file *filp, fl_owner_t id)
834 if (!file_count(filp)) {
835 printk(KERN_ERR "VFS: Close: file count is 0\n");
839 if (filp->f_op && filp->f_op->flush) {
841 retval = filp->f_op->flush(filp);
844 dnotify_flush(filp, id);
845 locks_remove_posix(filp, id);
851 * Careful here! We test whether the file pointer is NULL before
852 * releasing the fd. This ensures that one clone task can't release
853 * an fd while another clone is opening it.
855 asmlinkage long sys_close(unsigned int fd)
858 struct files_struct *files = current->files;
860 write_lock(&files->file_lock);
861 if (fd >= files->max_fds)
863 filp = files->fd[fd];
866 files->fd[fd] = NULL;
867 FD_CLR(fd, files->close_on_exec);
868 __put_unused_fd(files, fd);
869 write_unlock(&files->file_lock);
870 return filp_close(filp, files);
873 write_unlock(&files->file_lock);
878 * This routine simulates a hangup on the tty, to arrange that users
879 * are given clean terminals at login time.
881 asmlinkage long sys_vhangup(void)
883 if (capable(CAP_SYS_TTY_CONFIG)) {
884 tty_vhangup(current->tty);
891 * Called when an inode is about to be open.
892 * We use this to disallow opening RW large files on 32bit systems if
893 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
894 * on this flag in sys_open.
896 int generic_file_open(struct inode * inode, struct file * filp)
898 if (!(filp->f_flags & O_LARGEFILE) && inode->i_size > MAX_NON_LFS)
903 EXPORT_SYMBOL(generic_file_open);