Import 2.4.0-test2pre7
[davej-history.git] / fs / open.c
blobb2f6f6ffa0de24e302b7291218998fe99b7168d7
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/module.h>
15 #include <asm/uaccess.h>
17 int vfs_statfs(struct super_block *sb, struct statfs *buf)
19 int retval = -ENODEV;
21 if (sb) {
22 retval = -ENOSYS;
23 if (sb->s_op && sb->s_op->statfs) {
24 memset(buf, 0, sizeof(struct statfs));
25 lock_kernel();
26 retval = sb->s_op->statfs(sb, buf);
27 unlock_kernel();
30 return retval;
34 asmlinkage long sys_statfs(const char * path, struct statfs * buf)
36 struct nameidata nd;
37 int error;
39 error = user_path_walk(path, &nd);
40 if (!error) {
41 struct statfs tmp;
42 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
43 if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
44 error = -EFAULT;
45 path_release(&nd);
47 return error;
50 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs * buf)
52 struct file * file;
53 struct statfs tmp;
54 int error;
56 error = -EBADF;
57 file = fget(fd);
58 if (!file)
59 goto out;
60 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
61 if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
62 error = -EFAULT;
63 fput(file);
64 out:
65 return error;
68 int do_truncate(struct dentry *dentry, loff_t length)
70 struct inode *inode = dentry->d_inode;
71 int error;
72 struct iattr newattrs;
74 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
75 if (length < 0)
76 return -EINVAL;
78 down(&inode->i_sem);
79 newattrs.ia_size = length;
80 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
81 error = notify_change(dentry, &newattrs);
82 up(&inode->i_sem);
83 return error;
86 static inline long do_sys_truncate(const char * path, loff_t length)
88 struct nameidata nd;
89 struct inode * inode;
90 int error;
92 error = -EINVAL;
93 if (length < 0) /* sorry, but loff_t says... */
94 goto out;
96 error = user_path_walk(path, &nd);
97 if (error)
98 goto out;
99 inode = nd.dentry->d_inode;
101 error = -EACCES;
102 if (S_ISDIR(inode->i_mode))
103 goto dput_and_out;
105 error = permission(inode,MAY_WRITE);
106 if (error)
107 goto dput_and_out;
109 error = -EROFS;
110 if (IS_RDONLY(inode))
111 goto dput_and_out;
113 error = -EPERM;
114 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
115 goto dput_and_out;
117 lock_kernel();
118 error = get_write_access(inode);
119 if (error)
120 goto dput_and_out;
122 error = locks_verify_truncate(inode, NULL, length);
123 if (!error) {
124 DQUOT_INIT(inode);
125 error = do_truncate(nd.dentry, length);
127 put_write_access(inode);
128 unlock_kernel();
130 dput_and_out:
131 path_release(&nd);
132 out:
133 return error;
136 asmlinkage long sys_truncate(const char * path, unsigned long length)
138 return do_sys_truncate(path, length);
141 static inline long do_sys_ftruncate(unsigned int fd, loff_t length)
143 struct inode * inode;
144 struct dentry *dentry;
145 struct file * file;
146 int error;
148 error = -EINVAL;
149 if (length < 0)
150 goto out;
151 error = -EBADF;
152 file = fget(fd);
153 if (!file)
154 goto out;
155 dentry = file->f_dentry;
156 inode = dentry->d_inode;
157 error = -EACCES;
158 if (S_ISDIR(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
159 goto out_putf;
160 error = -EPERM;
161 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
162 goto out_putf;
164 lock_kernel();
165 error = locks_verify_truncate(inode, file, length);
166 if (!error)
167 error = do_truncate(dentry, length);
168 unlock_kernel();
169 out_putf:
170 fput(file);
171 out:
172 return error;
175 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
177 return do_sys_ftruncate(fd, length);
180 /* LFS versions of truncate are only needed on 32 bit machines */
181 #if BITS_PER_LONG == 32
182 asmlinkage long sys_truncate64(const char * path, loff_t length)
184 return do_sys_truncate(path, length);
187 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
189 return do_sys_ftruncate(fd, length);
191 #endif
193 #if !(defined(__alpha__) || defined(__ia64__))
196 * sys_utime() can be implemented in user-level using sys_utimes().
197 * Is this for backwards compatibility? If so, why not move it
198 * into the appropriate arch directory (for those architectures that
199 * need it).
202 /* If times==NULL, set access and modification to current time,
203 * must be owner or have write permission.
204 * Else, update from *times, must be owner or super user.
206 asmlinkage long sys_utime(char * filename, struct utimbuf * times)
208 int error;
209 struct nameidata nd;
210 struct inode * inode;
211 struct iattr newattrs;
213 error = user_path_walk(filename, &nd);
214 if (error)
215 goto out;
216 inode = nd.dentry->d_inode;
218 error = -EROFS;
219 if (IS_RDONLY(inode))
220 goto dput_and_out;
222 /* Don't worry, the checks are done in inode_change_ok() */
223 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
224 if (times) {
225 error = get_user(newattrs.ia_atime, &times->actime);
226 if (!error)
227 error = get_user(newattrs.ia_mtime, &times->modtime);
228 if (error)
229 goto dput_and_out;
231 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
232 } else {
233 if (current->fsuid != inode->i_uid &&
234 (error = permission(inode,MAY_WRITE)) != 0)
235 goto dput_and_out;
237 error = notify_change(nd.dentry, &newattrs);
238 dput_and_out:
239 path_release(&nd);
240 out:
241 return error;
244 #endif
246 /* If times==NULL, set access and modification to current time,
247 * must be owner or have write permission.
248 * Else, update from *times, must be owner or super user.
250 asmlinkage long sys_utimes(char * filename, struct timeval * utimes)
252 int error;
253 struct nameidata nd;
254 struct inode * inode;
255 struct iattr newattrs;
257 error = user_path_walk(filename, &nd);
259 if (error)
260 goto out;
261 inode = nd.dentry->d_inode;
263 error = -EROFS;
264 if (IS_RDONLY(inode))
265 goto dput_and_out;
267 /* Don't worry, the checks are done in inode_change_ok() */
268 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
269 if (utimes) {
270 struct timeval times[2];
271 error = -EFAULT;
272 if (copy_from_user(&times, utimes, sizeof(times)))
273 goto dput_and_out;
274 newattrs.ia_atime = times[0].tv_sec;
275 newattrs.ia_mtime = times[1].tv_sec;
276 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
277 } else {
278 if ((error = permission(inode,MAY_WRITE)) != 0)
279 goto dput_and_out;
281 error = notify_change(nd.dentry, &newattrs);
282 dput_and_out:
283 path_release(&nd);
284 out:
285 return error;
289 * access() needs to use the real uid/gid, not the effective uid/gid.
290 * We do this by temporarily clearing all FS-related capabilities and
291 * switching the fsuid/fsgid around to the real ones.
293 asmlinkage long sys_access(const char * filename, int mode)
295 struct nameidata nd;
296 int old_fsuid, old_fsgid;
297 kernel_cap_t old_cap;
298 int res;
300 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
301 return -EINVAL;
303 old_fsuid = current->fsuid;
304 old_fsgid = current->fsgid;
305 old_cap = current->cap_effective;
307 current->fsuid = current->uid;
308 current->fsgid = current->gid;
310 /* Clear the capabilities if we switch to a non-root user */
311 if (current->uid)
312 cap_clear(current->cap_effective);
313 else
314 current->cap_effective = current->cap_permitted;
316 res = user_path_walk(filename, &nd);
317 if (!res) {
318 res = permission(nd.dentry->d_inode, mode);
319 /* SuS v2 requires we report a read only fs too */
320 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode))
321 res = -EROFS;
322 path_release(&nd);
325 current->fsuid = old_fsuid;
326 current->fsgid = old_fsgid;
327 current->cap_effective = old_cap;
329 return res;
332 asmlinkage long sys_chdir(const char * filename)
334 int error;
335 struct nameidata nd;
336 char *name;
338 lock_kernel();
340 name = getname(filename);
341 error = PTR_ERR(name);
342 if (IS_ERR(name))
343 goto out;
345 error = 0;
346 if (path_init(name,LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY,&nd))
347 error = path_walk(name, &nd);
348 putname(name);
349 if (error)
350 goto out;
352 error = permission(nd.dentry->d_inode,MAY_EXEC);
353 if (error)
354 goto dput_and_out;
356 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
358 dput_and_out:
359 path_release(&nd);
360 out:
361 unlock_kernel();
362 return error;
365 asmlinkage long sys_fchdir(unsigned int fd)
367 struct file *file;
368 struct dentry *dentry;
369 struct inode *inode;
370 struct vfsmount *mnt;
371 int error;
373 error = -EBADF;
374 file = fget(fd);
375 if (!file)
376 goto out;
378 dentry = file->f_dentry;
379 mnt = file->f_vfsmnt;
380 inode = dentry->d_inode;
382 error = -ENOTDIR;
383 if (!S_ISDIR(inode->i_mode))
384 goto out_putf;
386 lock_kernel();
387 error = permission(inode, MAY_EXEC);
388 if (!error)
389 set_fs_pwd(current->fs, mnt, dentry);
390 unlock_kernel();
391 out_putf:
392 fput(file);
393 out:
394 return error;
397 asmlinkage long sys_chroot(const char * filename)
399 int error;
400 struct nameidata nd;
401 char *name;
403 lock_kernel();
405 name = getname(filename);
406 error = PTR_ERR(name);
407 if (IS_ERR(name))
408 goto out;
410 path_init(name, LOOKUP_POSITIVE | LOOKUP_FOLLOW |
411 LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
412 error = path_walk(name, &nd);
413 putname(name);
414 if (error)
415 goto out;
417 error = permission(nd.dentry->d_inode,MAY_EXEC);
418 if (error)
419 goto dput_and_out;
421 error = -EPERM;
422 if (!capable(CAP_SYS_CHROOT))
423 goto dput_and_out;
425 set_fs_root(current->fs, nd.mnt, nd.dentry);
426 set_fs_altroot();
427 error = 0;
428 dput_and_out:
429 path_release(&nd);
430 out:
431 unlock_kernel();
432 return error;
435 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
437 struct inode * inode;
438 struct dentry * dentry;
439 struct file * file;
440 int err = -EBADF;
441 struct iattr newattrs;
443 file = fget(fd);
444 if (!file)
445 goto out;
447 dentry = file->f_dentry;
448 inode = dentry->d_inode;
450 err = -EROFS;
451 if (IS_RDONLY(inode))
452 goto out_putf;
453 err = -EPERM;
454 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
455 goto out_putf;
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 lock_kernel();
461 err = notify_change(dentry, &newattrs);
462 unlock_kernel();
464 out_putf:
465 fput(file);
466 out:
467 return err;
470 asmlinkage long sys_chmod(const char * filename, mode_t mode)
472 struct nameidata nd;
473 struct inode * inode;
474 int error;
475 struct iattr newattrs;
477 error = user_path_walk(filename, &nd);
478 if (error)
479 goto out;
480 inode = nd.dentry->d_inode;
482 error = -EROFS;
483 if (IS_RDONLY(inode))
484 goto dput_and_out;
486 error = -EPERM;
487 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
488 goto dput_and_out;
490 if (mode == (mode_t) -1)
491 mode = inode->i_mode;
492 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
493 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
494 error = notify_change(nd.dentry, &newattrs);
496 dput_and_out:
497 path_release(&nd);
498 out:
499 return error;
502 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
504 struct inode * inode;
505 int error;
506 struct iattr newattrs;
508 error = -ENOENT;
509 if (!(inode = dentry->d_inode)) {
510 printk("chown_common: NULL inode\n");
511 goto out;
513 error = -EROFS;
514 if (IS_RDONLY(inode))
515 goto out;
516 error = -EPERM;
517 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
518 goto out;
519 if (user == (uid_t) -1)
520 user = inode->i_uid;
521 if (group == (gid_t) -1)
522 group = inode->i_gid;
523 newattrs.ia_mode = inode->i_mode;
524 newattrs.ia_uid = user;
525 newattrs.ia_gid = group;
526 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
528 * If the user or group of a non-directory has been changed by a
529 * non-root user, remove the setuid bit.
530 * 19981026 David C Niemi <niemi@tux.org>
532 * Changed this to apply to all users, including root, to avoid
533 * some races. This is the behavior we had in 2.0. The check for
534 * non-root was definitely wrong for 2.2 anyway, as it should
535 * have been using CAP_FSETID rather than fsuid -- 19990830 SD.
537 if ((inode->i_mode & S_ISUID) == S_ISUID &&
538 !S_ISDIR(inode->i_mode))
540 newattrs.ia_mode &= ~S_ISUID;
541 newattrs.ia_valid |= ATTR_MODE;
544 * Likewise, if the user or group of a non-directory has been changed
545 * by a non-root user, remove the setgid bit UNLESS there is no group
546 * execute bit (this would be a file marked for mandatory locking).
547 * 19981026 David C Niemi <niemi@tux.org>
549 * Removed the fsuid check (see the comment above) -- 19990830 SD.
551 if (((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
552 && !S_ISDIR(inode->i_mode))
554 newattrs.ia_mode &= ~S_ISGID;
555 newattrs.ia_valid |= ATTR_MODE;
557 error = DQUOT_TRANSFER(dentry, &newattrs);
558 out:
559 return error;
562 asmlinkage long sys_chown(const char * filename, uid_t user, gid_t group)
564 struct nameidata nd;
565 int error;
567 error = user_path_walk(filename, &nd);
568 if (!error) {
569 error = chown_common(nd.dentry, user, group);
570 path_release(&nd);
572 return error;
575 asmlinkage long sys_lchown(const char * filename, uid_t user, gid_t group)
577 struct nameidata nd;
578 int error;
580 error = user_path_walk_link(filename, &nd);
581 if (!error) {
582 error = chown_common(nd.dentry, user, group);
583 path_release(&nd);
585 return error;
589 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
591 struct file * file;
592 int error = -EBADF;
594 file = fget(fd);
595 if (file) {
596 error = chown_common(file->f_dentry, user, group);
597 fput(file);
599 return error;
603 * Note that while the flag value (low two bits) for sys_open means:
604 * 00 - read-only
605 * 01 - write-only
606 * 10 - read-write
607 * 11 - special
608 * it is changed into
609 * 00 - no permissions needed
610 * 01 - read-permission
611 * 10 - write-permission
612 * 11 - read-write
613 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
614 * used by symlinks.
616 struct file *filp_open(const char * filename, int flags, int mode)
618 int namei_flags, error;
619 struct nameidata nd;
621 namei_flags = flags;
622 if ((namei_flags+1) & O_ACCMODE)
623 namei_flags++;
624 if (namei_flags & O_TRUNC)
625 namei_flags |= 2;
627 error = open_namei(filename, namei_flags, mode, &nd);
628 if (!error)
629 return dentry_open(nd.dentry, nd.mnt, flags);
631 return ERR_PTR(error);
634 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
636 struct file * f;
637 struct inode *inode;
638 int error;
640 error = -ENFILE;
641 f = get_empty_filp();
642 if (!f)
643 goto cleanup_dentry;
644 f->f_flags = flags;
645 f->f_mode = (flags+1) & O_ACCMODE;
646 inode = dentry->d_inode;
647 if (f->f_mode & FMODE_WRITE) {
648 error = get_write_access(inode);
649 if (error)
650 goto cleanup_file;
653 f->f_dentry = dentry;
654 f->f_vfsmnt = mnt;
655 f->f_pos = 0;
656 f->f_reada = 0;
657 f->f_op = fops_get(inode->i_fop);
658 if (inode->i_sb)
659 file_move(f, &inode->i_sb->s_files);
660 if (f->f_op && f->f_op->open) {
661 error = f->f_op->open(inode,f);
662 if (error)
663 goto cleanup_all;
665 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
667 return f;
669 cleanup_all:
670 fops_put(f->f_op);
671 if (f->f_mode & FMODE_WRITE)
672 put_write_access(inode);
673 f->f_dentry = NULL;
674 f->f_vfsmnt = NULL;
675 cleanup_file:
676 put_filp(f);
677 cleanup_dentry:
678 dput(dentry);
679 mntput(mnt);
680 return ERR_PTR(error);
684 * Find an empty file descriptor entry, and mark it busy.
686 int get_unused_fd(void)
688 struct files_struct * files = current->files;
689 int fd, error;
691 error = -EMFILE;
692 write_lock(&files->file_lock);
694 repeat:
695 fd = find_next_zero_bit(files->open_fds,
696 current->files->max_fdset,
697 files->next_fd);
700 * N.B. For clone tasks sharing a files structure, this test
701 * will limit the total number of files that can be opened.
703 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
704 goto out;
706 /* Do we need to expand the fdset array? */
707 if (fd >= current->files->max_fdset) {
708 error = expand_fdset(files, fd);
709 if (!error) {
710 error = -EMFILE;
711 goto repeat;
713 goto out;
717 * Check whether we need to expand the fd array.
719 if (fd >= files->max_fds) {
720 error = expand_fd_array(files, fd);
721 if (!error) {
722 error = -EMFILE;
723 goto repeat;
725 goto out;
728 FD_SET(fd, files->open_fds);
729 FD_CLR(fd, files->close_on_exec);
730 files->next_fd = fd + 1;
731 #if 1
732 /* Sanity check */
733 if (files->fd[fd] != NULL) {
734 printk("get_unused_fd: slot %d not NULL!\n", fd);
735 files->fd[fd] = NULL;
737 #endif
738 error = fd;
740 out:
741 write_unlock(&files->file_lock);
742 return error;
745 asmlinkage long sys_open(const char * filename, int flags, int mode)
747 char * tmp;
748 int fd, error;
750 #if BITS_PER_LONG != 32
751 flags |= O_LARGEFILE;
752 #endif
753 tmp = getname(filename);
754 fd = PTR_ERR(tmp);
755 if (!IS_ERR(tmp)) {
756 fd = get_unused_fd();
757 if (fd >= 0) {
758 struct file * f;
759 lock_kernel();
760 f = filp_open(tmp, flags, mode);
761 unlock_kernel();
762 error = PTR_ERR(f);
763 if (IS_ERR(f))
764 goto out_error;
765 fd_install(fd, f);
767 out:
768 putname(tmp);
770 return fd;
772 out_error:
773 put_unused_fd(fd);
774 fd = error;
775 goto out;
778 #ifndef __alpha__
781 * For backward compatibility? Maybe this should be moved
782 * into arch/i386 instead?
784 asmlinkage long sys_creat(const char * pathname, int mode)
786 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
789 #endif
792 * "id" is the POSIX thread ID. We use the
793 * files pointer for this..
795 int filp_close(struct file *filp, fl_owner_t id)
797 int retval;
799 if (!file_count(filp)) {
800 printk("VFS: Close: file count is 0\n");
801 return 0;
803 retval = 0;
804 if (filp->f_op && filp->f_op->flush)
805 retval = filp->f_op->flush(filp);
806 locks_remove_posix(filp, id);
807 fput(filp);
808 return retval;
812 * Careful here! We test whether the file pointer is NULL before
813 * releasing the fd. This ensures that one clone task can't release
814 * an fd while another clone is opening it.
816 * The "release" argument tells us whether or not to mark the fd as free
817 * or not in the open-files bitmap. dup2 uses this to retain the fd
818 * without races.
820 int do_close(unsigned int fd, int release)
822 int error;
823 struct file * filp;
824 struct files_struct * files = current->files;
826 error = -EBADF;
827 write_lock(&files->file_lock);
828 filp = frip(files, fd);
829 if (!filp)
830 goto out_unlock;
831 FD_CLR(fd, files->close_on_exec);
832 if (release)
833 __put_unused_fd(files, fd);
834 write_unlock(&files->file_lock);
835 lock_kernel();
836 error = filp_close(filp, files);
837 unlock_kernel();
838 out:
839 return error;
840 out_unlock:
841 write_unlock(&files->file_lock);
842 goto out;
845 asmlinkage long sys_close(unsigned int fd)
847 return do_close(fd, 1);
851 * This routine simulates a hangup on the tty, to arrange that users
852 * are given clean terminals at login time.
854 asmlinkage long sys_vhangup(void)
856 if (capable(CAP_SYS_TTY_CONFIG)) {
857 tty_vhangup(current->tty);
858 return 0;
860 return -EPERM;