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>
17 #include <asm/uaccess.h>
19 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
21 int vfs_statfs(struct super_block
*sb
, struct statfs
*buf
)
27 if (sb
->s_op
&& sb
->s_op
->statfs
) {
28 memset(buf
, 0, sizeof(struct statfs
));
30 retval
= sb
->s_op
->statfs(sb
, buf
);
38 asmlinkage
long sys_statfs(const char * path
, struct statfs
* buf
)
43 error
= user_path_walk(path
, &nd
);
46 error
= vfs_statfs(nd
.dentry
->d_inode
->i_sb
, &tmp
);
47 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(struct statfs
)))
54 asmlinkage
long sys_fstatfs(unsigned int fd
, struct statfs
* buf
)
64 error
= vfs_statfs(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
65 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(struct statfs
)))
72 int do_truncate(struct dentry
*dentry
, loff_t length
)
74 struct inode
*inode
= dentry
->d_inode
;
76 struct iattr newattrs
;
78 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
83 newattrs
.ia_size
= length
;
84 newattrs
.ia_valid
= ATTR_SIZE
| ATTR_CTIME
;
85 error
= notify_change(dentry
, &newattrs
);
90 static inline long do_sys_truncate(const char * path
, loff_t length
)
97 if (length
< 0) /* sorry, but loff_t says... */
100 error
= user_path_walk(path
, &nd
);
103 inode
= nd
.dentry
->d_inode
;
106 if (!S_ISREG(inode
->i_mode
))
109 error
= permission(inode
,MAY_WRITE
);
114 if (IS_RDONLY(inode
))
118 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
122 * Make sure that there are no leases.
124 error
= get_lease(inode
, FMODE_WRITE
);
128 error
= get_write_access(inode
);
132 error
= locks_verify_truncate(inode
, NULL
, length
);
135 error
= do_truncate(nd
.dentry
, length
);
137 put_write_access(inode
);
145 asmlinkage
long sys_truncate(const char * path
, unsigned long length
)
147 return do_sys_truncate(path
, length
);
150 static inline long do_sys_ftruncate(unsigned int fd
, loff_t length
)
152 struct inode
* inode
;
153 struct dentry
*dentry
;
164 dentry
= file
->f_dentry
;
165 inode
= dentry
->d_inode
;
167 if (!S_ISREG(inode
->i_mode
) || !(file
->f_mode
& FMODE_WRITE
))
170 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
173 error
= locks_verify_truncate(inode
, file
, length
);
175 error
= do_truncate(dentry
, length
);
182 asmlinkage
long sys_ftruncate(unsigned int fd
, unsigned long length
)
184 return do_sys_ftruncate(fd
, length
);
187 /* LFS versions of truncate are only needed on 32 bit machines */
188 #if BITS_PER_LONG == 32
189 asmlinkage
long sys_truncate64(const char * path
, loff_t length
)
191 return do_sys_truncate(path
, length
);
194 asmlinkage
long sys_ftruncate64(unsigned int fd
, loff_t length
)
196 return do_sys_ftruncate(fd
, length
);
200 #if !(defined(__alpha__) || defined(__ia64__))
203 * sys_utime() can be implemented in user-level using sys_utimes().
204 * Is this for backwards compatibility? If so, why not move it
205 * into the appropriate arch directory (for those architectures that
209 /* If times==NULL, set access and modification to current time,
210 * must be owner or have write permission.
211 * Else, update from *times, must be owner or super user.
213 asmlinkage
long sys_utime(char * filename
, struct utimbuf
* times
)
217 struct inode
* inode
;
218 struct iattr newattrs
;
220 error
= user_path_walk(filename
, &nd
);
223 inode
= nd
.dentry
->d_inode
;
226 if (IS_RDONLY(inode
))
229 /* Don't worry, the checks are done in inode_change_ok() */
230 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
232 error
= get_user(newattrs
.ia_atime
, ×
->actime
);
234 error
= get_user(newattrs
.ia_mtime
, ×
->modtime
);
238 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
240 if (current
->fsuid
!= inode
->i_uid
&&
241 (error
= permission(inode
,MAY_WRITE
)) != 0)
244 error
= notify_change(nd
.dentry
, &newattrs
);
253 /* If times==NULL, set access and modification to current time,
254 * must be owner or have write permission.
255 * Else, update from *times, must be owner or super user.
257 asmlinkage
long sys_utimes(char * filename
, struct timeval
* utimes
)
261 struct inode
* inode
;
262 struct iattr newattrs
;
264 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
;
277 struct timeval times
[2];
279 if (copy_from_user(×
, utimes
, sizeof(times
)))
281 newattrs
.ia_atime
= times
[0].tv_sec
;
282 newattrs
.ia_mtime
= times
[1].tv_sec
;
283 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
285 if ((error
= permission(inode
,MAY_WRITE
)) != 0)
288 error
= notify_change(nd
.dentry
, &newattrs
);
296 * access() needs to use the real uid/gid, not the effective uid/gid.
297 * We do this by temporarily clearing all FS-related capabilities and
298 * switching the fsuid/fsgid around to the real ones.
300 asmlinkage
long sys_access(const char * filename
, int mode
)
303 int old_fsuid
, old_fsgid
;
304 kernel_cap_t old_cap
;
307 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
310 old_fsuid
= current
->fsuid
;
311 old_fsgid
= current
->fsgid
;
312 old_cap
= current
->cap_effective
;
314 current
->fsuid
= current
->uid
;
315 current
->fsgid
= current
->gid
;
317 /* Clear the capabilities if we switch to a non-root user */
319 cap_clear(current
->cap_effective
);
321 current
->cap_effective
= current
->cap_permitted
;
323 res
= user_path_walk(filename
, &nd
);
325 res
= permission(nd
.dentry
->d_inode
, mode
);
326 /* SuS v2 requires we report a read only fs too */
327 if(!res
&& (mode
& S_IWOTH
) && IS_RDONLY(nd
.dentry
->d_inode
)
328 && !special_file(nd
.dentry
->d_inode
->i_mode
))
333 current
->fsuid
= old_fsuid
;
334 current
->fsgid
= old_fsgid
;
335 current
->cap_effective
= old_cap
;
340 asmlinkage
long sys_chdir(const char * filename
)
346 name
= getname(filename
);
347 error
= PTR_ERR(name
);
352 if (path_init(name
,LOOKUP_POSITIVE
|LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
,&nd
))
353 error
= path_walk(name
, &nd
);
358 error
= permission(nd
.dentry
->d_inode
,MAY_EXEC
);
362 set_fs_pwd(current
->fs
, nd
.mnt
, nd
.dentry
);
370 asmlinkage
long sys_fchdir(unsigned int fd
)
373 struct dentry
*dentry
;
375 struct vfsmount
*mnt
;
383 dentry
= file
->f_dentry
;
384 mnt
= file
->f_vfsmnt
;
385 inode
= dentry
->d_inode
;
388 if (!S_ISDIR(inode
->i_mode
))
391 error
= permission(inode
, MAY_EXEC
);
393 set_fs_pwd(current
->fs
, mnt
, dentry
);
400 asmlinkage
long sys_chroot(const char * filename
)
406 name
= getname(filename
);
407 error
= PTR_ERR(name
);
411 path_init(name
, LOOKUP_POSITIVE
| LOOKUP_FOLLOW
|
412 LOOKUP_DIRECTORY
| LOOKUP_NOALT
, &nd
);
413 error
= path_walk(name
, &nd
);
418 error
= permission(nd
.dentry
->d_inode
,MAY_EXEC
);
423 if (!capable(CAP_SYS_CHROOT
))
426 set_fs_root(current
->fs
, nd
.mnt
, nd
.dentry
);
435 asmlinkage
long sys_fchmod(unsigned int fd
, mode_t mode
)
437 struct inode
* inode
;
438 struct dentry
* dentry
;
441 struct iattr newattrs
;
447 dentry
= file
->f_dentry
;
448 inode
= dentry
->d_inode
;
451 if (IS_RDONLY(inode
))
454 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
456 if (mode
== (mode_t
) -1)
457 mode
= inode
->i_mode
;
458 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
459 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
460 err
= notify_change(dentry
, &newattrs
);
468 asmlinkage
long sys_chmod(const char * filename
, mode_t mode
)
471 struct inode
* inode
;
473 struct iattr newattrs
;
475 error
= user_path_walk(filename
, &nd
);
478 inode
= nd
.dentry
->d_inode
;
481 if (IS_RDONLY(inode
))
485 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
488 if (mode
== (mode_t
) -1)
489 mode
= inode
->i_mode
;
490 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
491 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
492 error
= notify_change(nd
.dentry
, &newattrs
);
500 static int chown_common(struct dentry
* dentry
, uid_t user
, gid_t group
)
502 struct inode
* inode
;
504 struct iattr newattrs
;
507 if (!(inode
= dentry
->d_inode
)) {
508 printk("chown_common: NULL inode\n");
512 if (IS_RDONLY(inode
))
515 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
517 if (user
== (uid_t
) -1)
519 if (group
== (gid_t
) -1)
520 group
= inode
->i_gid
;
521 newattrs
.ia_mode
= inode
->i_mode
;
522 newattrs
.ia_uid
= user
;
523 newattrs
.ia_gid
= group
;
524 newattrs
.ia_valid
= ATTR_UID
| ATTR_GID
| ATTR_CTIME
;
526 * If the user or group of a non-directory has been changed by a
527 * non-root user, remove the setuid bit.
528 * 19981026 David C Niemi <niemi@tux.org>
530 * Changed this to apply to all users, including root, to avoid
531 * some races. This is the behavior we had in 2.0. The check for
532 * non-root was definitely wrong for 2.2 anyway, as it should
533 * have been using CAP_FSETID rather than fsuid -- 19990830 SD.
535 if ((inode
->i_mode
& S_ISUID
) == S_ISUID
&&
536 !S_ISDIR(inode
->i_mode
))
538 newattrs
.ia_mode
&= ~S_ISUID
;
539 newattrs
.ia_valid
|= ATTR_MODE
;
542 * Likewise, if the user or group of a non-directory has been changed
543 * by a non-root user, remove the setgid bit UNLESS there is no group
544 * execute bit (this would be a file marked for mandatory locking).
545 * 19981026 David C Niemi <niemi@tux.org>
547 * Removed the fsuid check (see the comment above) -- 19990830 SD.
549 if (((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
550 && !S_ISDIR(inode
->i_mode
))
552 newattrs
.ia_mode
&= ~S_ISGID
;
553 newattrs
.ia_valid
|= ATTR_MODE
;
555 error
= DQUOT_TRANSFER(dentry
, &newattrs
);
560 asmlinkage
long sys_chown(const char * filename
, uid_t user
, gid_t group
)
565 error
= user_path_walk(filename
, &nd
);
567 error
= chown_common(nd
.dentry
, user
, group
);
573 asmlinkage
long sys_lchown(const char * filename
, uid_t user
, gid_t group
)
578 error
= user_path_walk_link(filename
, &nd
);
580 error
= chown_common(nd
.dentry
, user
, group
);
587 asmlinkage
long sys_fchown(unsigned int fd
, uid_t user
, gid_t group
)
594 error
= chown_common(file
->f_dentry
, user
, group
);
601 * Note that while the flag value (low two bits) for sys_open means:
607 * 00 - no permissions needed
608 * 01 - read-permission
609 * 10 - write-permission
611 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
614 struct file
*filp_open(const char * filename
, int flags
, int mode
)
616 int namei_flags
, error
;
620 if ((namei_flags
+1) & O_ACCMODE
)
622 if (namei_flags
& O_TRUNC
)
625 error
= open_namei(filename
, namei_flags
, mode
, &nd
);
627 return dentry_open(nd
.dentry
, nd
.mnt
, flags
);
629 return ERR_PTR(error
);
632 struct file
*dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
, int flags
)
639 f
= get_empty_filp();
643 f
->f_mode
= (flags
+1) & O_ACCMODE
;
644 inode
= dentry
->d_inode
;
645 if (f
->f_mode
& FMODE_WRITE
) {
646 error
= get_write_access(inode
);
651 f
->f_dentry
= dentry
;
655 f
->f_op
= fops_get(inode
->i_fop
);
657 file_move(f
, &inode
->i_sb
->s_files
);
658 if (f
->f_op
&& f
->f_op
->open
) {
659 error
= f
->f_op
->open(inode
,f
);
663 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
669 if (f
->f_mode
& FMODE_WRITE
)
670 put_write_access(inode
);
678 return ERR_PTR(error
);
682 * Find an empty file descriptor entry, and mark it busy.
684 int get_unused_fd(void)
686 struct files_struct
* files
= current
->files
;
690 write_lock(&files
->file_lock
);
693 fd
= find_next_zero_bit(files
->open_fds
,
698 * N.B. For clone tasks sharing a files structure, this test
699 * will limit the total number of files that can be opened.
701 if (fd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
704 /* Do we need to expand the fdset array? */
705 if (fd
>= files
->max_fdset
) {
706 error
= expand_fdset(files
, fd
);
715 * Check whether we need to expand the fd array.
717 if (fd
>= files
->max_fds
) {
718 error
= expand_fd_array(files
, fd
);
726 FD_SET(fd
, files
->open_fds
);
727 FD_CLR(fd
, files
->close_on_exec
);
728 files
->next_fd
= fd
+ 1;
731 if (files
->fd
[fd
] != NULL
) {
732 printk("get_unused_fd: slot %d not NULL!\n", fd
);
733 files
->fd
[fd
] = NULL
;
739 write_unlock(&files
->file_lock
);
743 asmlinkage
long sys_open(const char * filename
, int flags
, int mode
)
748 #if BITS_PER_LONG != 32
749 flags
|= O_LARGEFILE
;
751 tmp
= getname(filename
);
754 fd
= get_unused_fd();
756 struct file
*f
= filp_open(tmp
, flags
, mode
);
776 * For backward compatibility? Maybe this should be moved
777 * into arch/i386 instead?
779 asmlinkage
long sys_creat(const char * pathname
, int mode
)
781 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
787 * "id" is the POSIX thread ID. We use the
788 * files pointer for this..
790 int filp_close(struct file
*filp
, fl_owner_t id
)
794 if (!file_count(filp
)) {
795 printk("VFS: Close: file count is 0\n");
799 if (filp
->f_op
&& filp
->f_op
->flush
) {
801 retval
= filp
->f_op
->flush(filp
);
804 fcntl_dirnotify(0, filp
, 0);
805 locks_remove_posix(filp
, id
);
811 * Careful here! We test whether the file pointer is NULL before
812 * releasing the fd. This ensures that one clone task can't release
813 * an fd while another clone is opening it.
815 asmlinkage
long sys_close(unsigned int fd
)
818 struct files_struct
*files
= current
->files
;
820 write_lock(&files
->file_lock
);
821 if (fd
>= files
->max_fds
)
823 filp
= files
->fd
[fd
];
826 files
->fd
[fd
] = NULL
;
827 FD_CLR(fd
, files
->close_on_exec
);
828 __put_unused_fd(files
, fd
);
829 write_unlock(&files
->file_lock
);
830 return filp_close(filp
, files
);
833 write_unlock(&files
->file_lock
);
838 * This routine simulates a hangup on the tty, to arrange that users
839 * are given clean terminals at login time.
841 asmlinkage
long sys_vhangup(void)
843 if (capable(CAP_SYS_TTY_CONFIG
)) {
844 tty_vhangup(current
->tty
);