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. */
108 down_write(&inode->i_alloc_sem);
110 newattrs.ia_size = length;
111 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
112 error = notify_change(dentry, &newattrs);
114 up_write(&inode->i_alloc_sem);
118 static inline long do_sys_truncate(const char * path, loff_t length)
121 struct inode * inode;
125 if (length < 0) /* sorry, but loff_t says... */
128 error = user_path_walk(path, &nd);
131 inode = nd.dentry->d_inode;
133 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
135 if (S_ISDIR(inode->i_mode))
139 if (!S_ISREG(inode->i_mode))
142 error = permission(inode,MAY_WRITE);
147 if (IS_RDONLY(inode))
151 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
155 * Make sure that there are no leases.
157 error = get_lease(inode, FMODE_WRITE);
161 error = get_write_access(inode);
165 error = locks_verify_truncate(inode, NULL, length);
168 error = do_truncate(nd.dentry, length);
170 put_write_access(inode);
178 asmlinkage long sys_truncate(const char * path, unsigned long length)
180 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
181 return do_sys_truncate(path, (long)length);
184 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
186 struct inode * inode;
187 struct dentry *dentry;
199 /* explicitly opened as large or we are on 64-bit box */
200 if (file->f_flags & O_LARGEFILE)
203 dentry = file->f_dentry;
204 inode = dentry->d_inode;
206 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
210 /* Cannot ftruncate over 2^31 bytes without large file support */
211 if (small && length > MAX_NON_LFS)
215 if (IS_APPEND(inode))
218 error = locks_verify_truncate(inode, file, length);
220 error = do_truncate(dentry, length);
227 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
229 return do_sys_ftruncate(fd, length, 1);
232 /* LFS versions of truncate are only needed on 32 bit machines */
233 #if BITS_PER_LONG == 32
234 asmlinkage long sys_truncate64(const char * path, loff_t length)
236 return do_sys_truncate(path, length);
239 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
241 return do_sys_ftruncate(fd, length, 0);
245 #if !(defined(__alpha__) || defined(__ia64__))
248 * sys_utime() can be implemented in user-level using sys_utimes().
249 * Is this for backwards compatibility? If so, why not move it
250 * into the appropriate arch directory (for those architectures that
254 /* If times==NULL, set access and modification to current time,
255 * must be owner or have write permission.
256 * Else, update from *times, must be owner or super user.
258 asmlinkage long sys_utime(char * filename, struct utimbuf * times)
262 struct inode * inode;
263 struct iattr newattrs;
265 error = user_path_walk(filename, &nd);
268 inode = nd.dentry->d_inode;
271 if (IS_RDONLY(inode))
274 /* Don't worry, the checks are done in inode_change_ok() */
275 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
278 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
280 error = get_user(newattrs.ia_atime, ×->actime);
282 error = get_user(newattrs.ia_mtime, ×->modtime);
286 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
289 if (IS_IMMUTABLE(inode))
291 if (current->fsuid != inode->i_uid &&
292 (error = permission(inode,MAY_WRITE)) != 0)
295 error = notify_change(nd.dentry, &newattrs);
304 /* If times==NULL, set access and modification to current time,
305 * must be owner or have write permission.
306 * Else, update from *times, must be owner or super user.
308 asmlinkage long sys_utimes(char * filename, struct timeval * utimes)
312 struct inode * inode;
313 struct iattr newattrs;
315 error = user_path_walk(filename, &nd);
319 inode = nd.dentry->d_inode;
322 if (IS_RDONLY(inode))
325 /* Don't worry, the checks are done in inode_change_ok() */
326 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
328 struct timeval times[2];
330 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
333 if (copy_from_user(×, utimes, sizeof(times)))
335 newattrs.ia_atime = times[0].tv_sec;
336 newattrs.ia_mtime = times[1].tv_sec;
337 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
340 if (IS_IMMUTABLE(inode))
343 if (current->fsuid != inode->i_uid &&
344 (error = permission(inode,MAY_WRITE)) != 0)
347 error = notify_change(nd.dentry, &newattrs);
355 * access() needs to use the real uid/gid, not the effective uid/gid.
356 * We do this by temporarily clearing all FS-related capabilities and
357 * switching the fsuid/fsgid around to the real ones.
359 asmlinkage long sys_access(const char * filename, int mode)
362 int old_fsuid, old_fsgid;
363 kernel_cap_t old_cap;
366 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
369 old_fsuid = current->fsuid;
370 old_fsgid = current->fsgid;
371 old_cap = current->cap_effective;
373 current->fsuid = current->uid;
374 current->fsgid = current->gid;
376 /* Clear the capabilities if we switch to a non-root user */
378 cap_clear(current->cap_effective);
380 current->cap_effective = current->cap_permitted;
382 res = user_path_walk(filename, &nd);
384 res = permission(nd.dentry->d_inode, mode);
385 /* SuS v2 requires we report a read only fs too */
386 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
387 && !special_file(nd.dentry->d_inode->i_mode))
392 current->fsuid = old_fsuid;
393 current->fsgid = old_fsgid;
394 current->cap_effective = old_cap;
399 asmlinkage long sys_chdir(const char * filename)
404 error = __user_walk(filename,LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY,&nd);
408 error = permission(nd.dentry->d_inode,MAY_EXEC);
412 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
420 asmlinkage long sys_fchdir(unsigned int fd)
423 struct dentry *dentry;
425 struct vfsmount *mnt;
433 dentry = file->f_dentry;
434 mnt = file->f_vfsmnt;
435 inode = dentry->d_inode;
438 if (!S_ISDIR(inode->i_mode))
441 error = permission(inode, MAY_EXEC);
443 set_fs_pwd(current->fs, mnt, dentry);
450 asmlinkage long sys_chroot(const char * filename)
455 error = __user_walk(filename, LOOKUP_POSITIVE | LOOKUP_FOLLOW |
456 LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
460 error = permission(nd.dentry->d_inode,MAY_EXEC);
465 if (!capable(CAP_SYS_CHROOT))
468 set_fs_root(current->fs, nd.mnt, nd.dentry);
477 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
479 struct inode * inode;
480 struct dentry * dentry;
483 struct iattr newattrs;
489 dentry = file->f_dentry;
490 inode = dentry->d_inode;
493 if (IS_RDONLY(inode))
496 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
498 if (mode == (mode_t) -1)
499 mode = inode->i_mode;
500 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
501 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
502 err = notify_change(dentry, &newattrs);
510 asmlinkage long sys_chmod(const char * filename, mode_t mode)
513 struct inode * inode;
515 struct iattr newattrs;
517 error = user_path_walk(filename, &nd);
520 inode = nd.dentry->d_inode;
523 if (IS_RDONLY(inode))
527 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
530 if (mode == (mode_t) -1)
531 mode = inode->i_mode;
532 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
533 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
534 error = notify_change(nd.dentry, &newattrs);
542 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
544 struct inode * inode;
546 struct iattr newattrs;
549 if (!(inode = dentry->d_inode)) {
550 printk(KERN_ERR "chown_common: NULL inode\n");
554 if (IS_RDONLY(inode))
557 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
559 if (user == (uid_t) -1)
561 if (group == (gid_t) -1)
562 group = inode->i_gid;
563 newattrs.ia_mode = inode->i_mode;
564 newattrs.ia_uid = user;
565 newattrs.ia_gid = group;
566 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
568 * If the user or group of a non-directory has been changed by a
569 * non-root user, remove the setuid bit.
570 * 19981026 David C Niemi <niemi@tux.org>
572 * Changed this to apply to all users, including root, to avoid
573 * some races. This is the behavior we had in 2.0. The check for
574 * non-root was definitely wrong for 2.2 anyway, as it should
575 * have been using CAP_FSETID rather than fsuid -- 19990830 SD.
577 if ((inode->i_mode & S_ISUID) == S_ISUID &&
578 !S_ISDIR(inode->i_mode))
580 newattrs.ia_mode &= ~S_ISUID;
581 newattrs.ia_valid |= ATTR_MODE;
584 * Likewise, if the user or group of a non-directory has been changed
585 * by a non-root user, remove the setgid bit UNLESS there is no group
586 * execute bit (this would be a file marked for mandatory locking).
587 * 19981026 David C Niemi <niemi@tux.org>
589 * Removed the fsuid check (see the comment above) -- 19990830 SD.
591 if (((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
592 && !S_ISDIR(inode->i_mode))
594 newattrs.ia_mode &= ~S_ISGID;
595 newattrs.ia_valid |= ATTR_MODE;
597 error = notify_change(dentry, &newattrs);
602 asmlinkage long sys_chown(const char * filename, uid_t user, gid_t group)
607 error = user_path_walk(filename, &nd);
609 error = chown_common(nd.dentry, user, group);
615 asmlinkage long sys_lchown(const char * filename, uid_t user, gid_t group)
620 error = user_path_walk_link(filename, &nd);
622 error = chown_common(nd.dentry, user, group);
629 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
636 error = chown_common(file->f_dentry, user, group);
643 * Note that while the flag value (low two bits) for sys_open means:
649 * 00 - no permissions needed
650 * 01 - read-permission
651 * 10 - write-permission
653 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
656 struct file *filp_open(const char * filename, int flags, int mode)
658 int namei_flags, error;
662 if ((namei_flags+1) & O_ACCMODE)
664 if (namei_flags & O_TRUNC)
667 error = open_namei(filename, namei_flags, mode, &nd);
669 return dentry_open(nd.dentry, nd.mnt, flags);
671 return ERR_PTR(error);
674 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
678 static LIST_HEAD(kill_list);
682 f = get_empty_filp();
686 f->f_mode = (flags+1) & O_ACCMODE;
687 inode = dentry->d_inode;
688 if (f->f_mode & FMODE_WRITE) {
689 error = get_write_access(inode);
694 f->f_dentry = dentry;
698 f->f_op = fops_get(inode->i_fop);
699 file_move(f, &inode->i_sb->s_files);
701 /* preallocate kiobuf for O_DIRECT */
704 if (f->f_flags & O_DIRECT) {
705 error = alloc_kiovec(1, &f->f_iobuf);
710 if (f->f_op && f->f_op->open) {
711 error = f->f_op->open(inode,f);
715 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
721 free_kiovec(1, &f->f_iobuf);
723 if (f->f_mode & FMODE_WRITE)
724 put_write_access(inode);
725 file_move(f, &kill_list); /* out of the way.. */
733 return ERR_PTR(error);
737 * Find an empty file descriptor entry, and mark it busy.
739 int get_unused_fd(void)
741 struct files_struct * files = current->files;
745 write_lock(&files->file_lock);
748 fd = find_next_zero_bit(files->open_fds,
753 * N.B. For clone tasks sharing a files structure, this test
754 * will limit the total number of files that can be opened.
756 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
759 /* Do we need to expand the fdset array? */
760 if (fd >= files->max_fdset) {
761 error = expand_fdset(files, fd);
770 * Check whether we need to expand the fd array.
772 if (fd >= files->max_fds) {
773 error = expand_fd_array(files, fd);
781 FD_SET(fd, files->open_fds);
782 FD_CLR(fd, files->close_on_exec);
783 files->next_fd = fd + 1;
786 if (files->fd[fd] != NULL) {
787 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
788 files->fd[fd] = NULL;
794 write_unlock(&files->file_lock);
798 asmlinkage long sys_open(const char * filename, int flags, int mode)
803 #if BITS_PER_LONG != 32
804 flags |= O_LARGEFILE;
806 tmp = getname(filename);
809 fd = get_unused_fd();
811 struct file *f = filp_open(tmp, flags, mode);
831 * For backward compatibility? Maybe this should be moved
832 * into arch/i386 instead?
834 asmlinkage long sys_creat(const char * pathname, int mode)
836 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
842 * "id" is the POSIX thread ID. We use the
843 * files pointer for this..
845 int filp_close(struct file *filp, fl_owner_t id)
849 if (!file_count(filp)) {
850 printk(KERN_ERR "VFS: Close: file count is 0\n");
854 if (filp->f_op && filp->f_op->flush) {
856 retval = filp->f_op->flush(filp);
859 dnotify_flush(filp, id);
860 locks_remove_posix(filp, id);
866 * Careful here! We test whether the file pointer is NULL before
867 * releasing the fd. This ensures that one clone task can't release
868 * an fd while another clone is opening it.
870 asmlinkage long sys_close(unsigned int fd)
873 struct files_struct *files = current->files;
875 write_lock(&files->file_lock);
876 if (fd >= files->max_fds)
878 filp = files->fd[fd];
881 files->fd[fd] = NULL;
882 FD_CLR(fd, files->close_on_exec);
883 __put_unused_fd(files, fd);
884 write_unlock(&files->file_lock);
885 return filp_close(filp, files);
888 write_unlock(&files->file_lock);
893 * This routine simulates a hangup on the tty, to arrange that users
894 * are given clean terminals at login time.
896 asmlinkage long sys_vhangup(void)
898 if (capable(CAP_SYS_TTY_CONFIG)) {
899 tty_vhangup(current->tty);
906 * Called when an inode is about to be open.
907 * We use this to disallow opening RW large files on 32bit systems if
908 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
909 * on this flag in sys_open.
911 int generic_file_open(struct inode * inode, struct file * filp)
913 if (!(filp->f_flags & O_LARGEFILE) && inode->i_size > MAX_NON_LFS)
918 EXPORT_SYMBOL(generic_file_open);