- pre4:
[davej-history.git] / fs / open.c
blob0f1787025c0f023933e74b1b7d55becb7b17ef05
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>
14 #include <linux/slab.h>
16 #include <asm/uaccess.h>
18 int vfs_statfs(struct super_block *sb, struct statfs *buf)
20 int retval = -ENODEV;
22 if (sb) {
23 retval = -ENOSYS;
24 if (sb->s_op && sb->s_op->statfs) {
25 memset(buf, 0, sizeof(struct statfs));
26 lock_kernel();
27 retval = sb->s_op->statfs(sb, buf);
28 unlock_kernel();
31 return retval;
35 asmlinkage long sys_statfs(const char * path, struct statfs * buf)
37 struct nameidata nd;
38 int error;
40 error = user_path_walk(path, &nd);
41 if (!error) {
42 struct statfs tmp;
43 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
44 if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
45 error = -EFAULT;
46 path_release(&nd);
48 return error;
51 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs * buf)
53 struct file * file;
54 struct statfs tmp;
55 int error;
57 error = -EBADF;
58 file = fget(fd);
59 if (!file)
60 goto out;
61 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
62 if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
63 error = -EFAULT;
64 fput(file);
65 out:
66 return error;
69 int do_truncate(struct dentry *dentry, loff_t length)
71 struct inode *inode = dentry->d_inode;
72 int error;
73 struct iattr newattrs;
75 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
76 if (length < 0)
77 return -EINVAL;
79 down(&inode->i_sem);
80 newattrs.ia_size = length;
81 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
82 error = notify_change(dentry, &newattrs);
83 up(&inode->i_sem);
84 return error;
87 static inline long do_sys_truncate(const char * path, loff_t length)
89 struct nameidata nd;
90 struct inode * inode;
91 int error;
93 error = -EINVAL;
94 if (length < 0) /* sorry, but loff_t says... */
95 goto out;
97 error = user_path_walk(path, &nd);
98 if (error)
99 goto out;
100 inode = nd.dentry->d_inode;
102 error = -EACCES;
103 if (S_ISDIR(inode->i_mode))
104 goto dput_and_out;
106 error = permission(inode,MAY_WRITE);
107 if (error)
108 goto dput_and_out;
110 error = -EROFS;
111 if (IS_RDONLY(inode))
112 goto dput_and_out;
114 error = -EPERM;
115 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
116 goto dput_and_out;
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);
129 dput_and_out:
130 path_release(&nd);
131 out:
132 return error;
135 asmlinkage long sys_truncate(const char * path, unsigned long length)
137 return do_sys_truncate(path, length);
140 static inline long do_sys_ftruncate(unsigned int fd, loff_t length)
142 struct inode * inode;
143 struct dentry *dentry;
144 struct file * file;
145 int error;
147 error = -EINVAL;
148 if (length < 0)
149 goto out;
150 error = -EBADF;
151 file = fget(fd);
152 if (!file)
153 goto out;
154 dentry = file->f_dentry;
155 inode = dentry->d_inode;
156 error = -EACCES;
157 if (S_ISDIR(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
158 goto out_putf;
159 error = -EPERM;
160 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
161 goto out_putf;
163 error = locks_verify_truncate(inode, file, length);
164 if (!error)
165 error = do_truncate(dentry, length);
166 out_putf:
167 fput(file);
168 out:
169 return error;
172 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
174 return do_sys_ftruncate(fd, length);
177 /* LFS versions of truncate are only needed on 32 bit machines */
178 #if BITS_PER_LONG == 32
179 asmlinkage long sys_truncate64(const char * path, loff_t length)
181 return do_sys_truncate(path, length);
184 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
186 return do_sys_ftruncate(fd, length);
188 #endif
190 #if !(defined(__alpha__) || defined(__ia64__))
193 * sys_utime() can be implemented in user-level using sys_utimes().
194 * Is this for backwards compatibility? If so, why not move it
195 * into the appropriate arch directory (for those architectures that
196 * need it).
199 /* If times==NULL, set access and modification to current time,
200 * must be owner or have write permission.
201 * Else, update from *times, must be owner or super user.
203 asmlinkage long sys_utime(char * filename, struct utimbuf * times)
205 int error;
206 struct nameidata nd;
207 struct inode * inode;
208 struct iattr newattrs;
210 error = user_path_walk(filename, &nd);
211 if (error)
212 goto out;
213 inode = nd.dentry->d_inode;
215 error = -EROFS;
216 if (IS_RDONLY(inode))
217 goto dput_and_out;
219 /* Don't worry, the checks are done in inode_change_ok() */
220 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
221 if (times) {
222 error = get_user(newattrs.ia_atime, &times->actime);
223 if (!error)
224 error = get_user(newattrs.ia_mtime, &times->modtime);
225 if (error)
226 goto dput_and_out;
228 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
229 } else {
230 if (current->fsuid != inode->i_uid &&
231 (error = permission(inode,MAY_WRITE)) != 0)
232 goto dput_and_out;
234 error = notify_change(nd.dentry, &newattrs);
235 dput_and_out:
236 path_release(&nd);
237 out:
238 return error;
241 #endif
243 /* If times==NULL, set access and modification to current time,
244 * must be owner or have write permission.
245 * Else, update from *times, must be owner or super user.
247 asmlinkage long sys_utimes(char * filename, struct timeval * utimes)
249 int error;
250 struct nameidata nd;
251 struct inode * inode;
252 struct iattr newattrs;
254 error = user_path_walk(filename, &nd);
256 if (error)
257 goto out;
258 inode = nd.dentry->d_inode;
260 error = -EROFS;
261 if (IS_RDONLY(inode))
262 goto dput_and_out;
264 /* Don't worry, the checks are done in inode_change_ok() */
265 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
266 if (utimes) {
267 struct timeval times[2];
268 error = -EFAULT;
269 if (copy_from_user(&times, utimes, sizeof(times)))
270 goto dput_and_out;
271 newattrs.ia_atime = times[0].tv_sec;
272 newattrs.ia_mtime = times[1].tv_sec;
273 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
274 } else {
275 if ((error = permission(inode,MAY_WRITE)) != 0)
276 goto dput_and_out;
278 error = notify_change(nd.dentry, &newattrs);
279 dput_and_out:
280 path_release(&nd);
281 out:
282 return error;
286 * access() needs to use the real uid/gid, not the effective uid/gid.
287 * We do this by temporarily clearing all FS-related capabilities and
288 * switching the fsuid/fsgid around to the real ones.
290 asmlinkage long sys_access(const char * filename, int mode)
292 struct nameidata nd;
293 int old_fsuid, old_fsgid;
294 kernel_cap_t old_cap;
295 int res;
297 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
298 return -EINVAL;
300 old_fsuid = current->fsuid;
301 old_fsgid = current->fsgid;
302 old_cap = current->cap_effective;
304 current->fsuid = current->uid;
305 current->fsgid = current->gid;
307 /* Clear the capabilities if we switch to a non-root user */
308 if (current->uid)
309 cap_clear(current->cap_effective);
310 else
311 current->cap_effective = current->cap_permitted;
313 res = user_path_walk(filename, &nd);
314 if (!res) {
315 res = permission(nd.dentry->d_inode, mode);
316 /* SuS v2 requires we report a read only fs too */
317 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode))
318 res = -EROFS;
319 path_release(&nd);
322 current->fsuid = old_fsuid;
323 current->fsgid = old_fsgid;
324 current->cap_effective = old_cap;
326 return res;
329 asmlinkage long sys_chdir(const char * filename)
331 int error;
332 struct nameidata nd;
333 char *name;
335 name = getname(filename);
336 error = PTR_ERR(name);
337 if (IS_ERR(name))
338 goto out;
340 error = 0;
341 if (path_init(name,LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY,&nd))
342 error = path_walk(name, &nd);
343 putname(name);
344 if (error)
345 goto out;
347 error = permission(nd.dentry->d_inode,MAY_EXEC);
348 if (error)
349 goto dput_and_out;
351 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
353 dput_and_out:
354 path_release(&nd);
355 out:
356 return error;
359 asmlinkage long sys_fchdir(unsigned int fd)
361 struct file *file;
362 struct dentry *dentry;
363 struct inode *inode;
364 struct vfsmount *mnt;
365 int error;
367 error = -EBADF;
368 file = fget(fd);
369 if (!file)
370 goto out;
372 dentry = file->f_dentry;
373 mnt = file->f_vfsmnt;
374 inode = dentry->d_inode;
376 error = -ENOTDIR;
377 if (!S_ISDIR(inode->i_mode))
378 goto out_putf;
380 error = permission(inode, MAY_EXEC);
381 if (!error)
382 set_fs_pwd(current->fs, mnt, dentry);
383 out_putf:
384 fput(file);
385 out:
386 return error;
389 asmlinkage long sys_chroot(const char * filename)
391 int error;
392 struct nameidata nd;
393 char *name;
395 name = getname(filename);
396 error = PTR_ERR(name);
397 if (IS_ERR(name))
398 goto out;
400 path_init(name, LOOKUP_POSITIVE | LOOKUP_FOLLOW |
401 LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
402 error = path_walk(name, &nd);
403 putname(name);
404 if (error)
405 goto out;
407 error = permission(nd.dentry->d_inode,MAY_EXEC);
408 if (error)
409 goto dput_and_out;
411 error = -EPERM;
412 if (!capable(CAP_SYS_CHROOT))
413 goto dput_and_out;
415 set_fs_root(current->fs, nd.mnt, nd.dentry);
416 set_fs_altroot();
417 error = 0;
418 dput_and_out:
419 path_release(&nd);
420 out:
421 return error;
424 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
426 struct inode * inode;
427 struct dentry * dentry;
428 struct file * file;
429 int err = -EBADF;
430 struct iattr newattrs;
432 file = fget(fd);
433 if (!file)
434 goto out;
436 dentry = file->f_dentry;
437 inode = dentry->d_inode;
439 err = -EROFS;
440 if (IS_RDONLY(inode))
441 goto out_putf;
442 err = -EPERM;
443 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
444 goto out_putf;
445 if (mode == (mode_t) -1)
446 mode = inode->i_mode;
447 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
448 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
449 err = notify_change(dentry, &newattrs);
451 out_putf:
452 fput(file);
453 out:
454 return err;
457 asmlinkage long sys_chmod(const char * filename, mode_t mode)
459 struct nameidata nd;
460 struct inode * inode;
461 int error;
462 struct iattr newattrs;
464 error = user_path_walk(filename, &nd);
465 if (error)
466 goto out;
467 inode = nd.dentry->d_inode;
469 error = -EROFS;
470 if (IS_RDONLY(inode))
471 goto dput_and_out;
473 error = -EPERM;
474 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
475 goto dput_and_out;
477 if (mode == (mode_t) -1)
478 mode = inode->i_mode;
479 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
480 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
481 error = notify_change(nd.dentry, &newattrs);
483 dput_and_out:
484 path_release(&nd);
485 out:
486 return error;
489 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
491 struct inode * inode;
492 int error;
493 struct iattr newattrs;
495 error = -ENOENT;
496 if (!(inode = dentry->d_inode)) {
497 printk("chown_common: NULL inode\n");
498 goto out;
500 error = -EROFS;
501 if (IS_RDONLY(inode))
502 goto out;
503 error = -EPERM;
504 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
505 goto out;
506 if (user == (uid_t) -1)
507 user = inode->i_uid;
508 if (group == (gid_t) -1)
509 group = inode->i_gid;
510 newattrs.ia_mode = inode->i_mode;
511 newattrs.ia_uid = user;
512 newattrs.ia_gid = group;
513 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
515 * If the user or group of a non-directory has been changed by a
516 * non-root user, remove the setuid bit.
517 * 19981026 David C Niemi <niemi@tux.org>
519 * Changed this to apply to all users, including root, to avoid
520 * some races. This is the behavior we had in 2.0. The check for
521 * non-root was definitely wrong for 2.2 anyway, as it should
522 * have been using CAP_FSETID rather than fsuid -- 19990830 SD.
524 if ((inode->i_mode & S_ISUID) == S_ISUID &&
525 !S_ISDIR(inode->i_mode))
527 newattrs.ia_mode &= ~S_ISUID;
528 newattrs.ia_valid |= ATTR_MODE;
531 * Likewise, if the user or group of a non-directory has been changed
532 * by a non-root user, remove the setgid bit UNLESS there is no group
533 * execute bit (this would be a file marked for mandatory locking).
534 * 19981026 David C Niemi <niemi@tux.org>
536 * Removed the fsuid check (see the comment above) -- 19990830 SD.
538 if (((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
539 && !S_ISDIR(inode->i_mode))
541 newattrs.ia_mode &= ~S_ISGID;
542 newattrs.ia_valid |= ATTR_MODE;
544 error = DQUOT_TRANSFER(dentry, &newattrs);
545 out:
546 return error;
549 asmlinkage long sys_chown(const char * filename, uid_t user, gid_t group)
551 struct nameidata nd;
552 int error;
554 error = user_path_walk(filename, &nd);
555 if (!error) {
556 error = chown_common(nd.dentry, user, group);
557 path_release(&nd);
559 return error;
562 asmlinkage long sys_lchown(const char * filename, uid_t user, gid_t group)
564 struct nameidata nd;
565 int error;
567 error = user_path_walk_link(filename, &nd);
568 if (!error) {
569 error = chown_common(nd.dentry, user, group);
570 path_release(&nd);
572 return error;
576 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
578 struct file * file;
579 int error = -EBADF;
581 file = fget(fd);
582 if (file) {
583 error = chown_common(file->f_dentry, user, group);
584 fput(file);
586 return error;
590 * Note that while the flag value (low two bits) for sys_open means:
591 * 00 - read-only
592 * 01 - write-only
593 * 10 - read-write
594 * 11 - special
595 * it is changed into
596 * 00 - no permissions needed
597 * 01 - read-permission
598 * 10 - write-permission
599 * 11 - read-write
600 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
601 * used by symlinks.
603 struct file *filp_open(const char * filename, int flags, int mode)
605 int namei_flags, error;
606 struct nameidata nd;
608 namei_flags = flags;
609 if ((namei_flags+1) & O_ACCMODE)
610 namei_flags++;
611 if (namei_flags & O_TRUNC)
612 namei_flags |= 2;
614 error = open_namei(filename, namei_flags, mode, &nd);
615 if (!error)
616 return dentry_open(nd.dentry, nd.mnt, flags);
618 return ERR_PTR(error);
621 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
623 struct file * f;
624 struct inode *inode;
625 int error;
627 error = -ENFILE;
628 f = get_empty_filp();
629 if (!f)
630 goto cleanup_dentry;
631 f->f_flags = flags;
632 f->f_mode = (flags+1) & O_ACCMODE;
633 inode = dentry->d_inode;
634 if (f->f_mode & FMODE_WRITE) {
635 error = get_write_access(inode);
636 if (error)
637 goto cleanup_file;
640 f->f_dentry = dentry;
641 f->f_vfsmnt = mnt;
642 f->f_pos = 0;
643 f->f_reada = 0;
644 f->f_op = fops_get(inode->i_fop);
645 if (inode->i_sb)
646 file_move(f, &inode->i_sb->s_files);
647 if (f->f_op && f->f_op->open) {
648 error = f->f_op->open(inode,f);
649 if (error)
650 goto cleanup_all;
652 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
654 return f;
656 cleanup_all:
657 fops_put(f->f_op);
658 if (f->f_mode & FMODE_WRITE)
659 put_write_access(inode);
660 f->f_dentry = NULL;
661 f->f_vfsmnt = NULL;
662 cleanup_file:
663 put_filp(f);
664 cleanup_dentry:
665 dput(dentry);
666 mntput(mnt);
667 return ERR_PTR(error);
671 * Find an empty file descriptor entry, and mark it busy.
673 int get_unused_fd(void)
675 struct files_struct * files = current->files;
676 int fd, error;
678 error = -EMFILE;
679 write_lock(&files->file_lock);
681 repeat:
682 fd = find_next_zero_bit(files->open_fds,
683 files->max_fdset,
684 files->next_fd);
687 * N.B. For clone tasks sharing a files structure, this test
688 * will limit the total number of files that can be opened.
690 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
691 goto out;
693 /* Do we need to expand the fdset array? */
694 if (fd >= files->max_fdset) {
695 error = expand_fdset(files, fd);
696 if (!error) {
697 error = -EMFILE;
698 goto repeat;
700 goto out;
704 * Check whether we need to expand the fd array.
706 if (fd >= files->max_fds) {
707 error = expand_fd_array(files, fd);
708 if (!error) {
709 error = -EMFILE;
710 goto repeat;
712 goto out;
715 FD_SET(fd, files->open_fds);
716 FD_CLR(fd, files->close_on_exec);
717 files->next_fd = fd + 1;
718 #if 1
719 /* Sanity check */
720 if (files->fd[fd] != NULL) {
721 printk("get_unused_fd: slot %d not NULL!\n", fd);
722 files->fd[fd] = NULL;
724 #endif
725 error = fd;
727 out:
728 write_unlock(&files->file_lock);
729 return error;
732 asmlinkage long sys_open(const char * filename, int flags, int mode)
734 char * tmp;
735 int fd, error;
737 #if BITS_PER_LONG != 32
738 flags |= O_LARGEFILE;
739 #endif
740 tmp = getname(filename);
741 fd = PTR_ERR(tmp);
742 if (!IS_ERR(tmp)) {
743 fd = get_unused_fd();
744 if (fd >= 0) {
745 struct file *f = filp_open(tmp, flags, mode);
746 error = PTR_ERR(f);
747 if (IS_ERR(f))
748 goto out_error;
749 fd_install(fd, f);
751 out:
752 putname(tmp);
754 return fd;
756 out_error:
757 put_unused_fd(fd);
758 fd = error;
759 goto out;
762 #ifndef __alpha__
765 * For backward compatibility? Maybe this should be moved
766 * into arch/i386 instead?
768 asmlinkage long sys_creat(const char * pathname, int mode)
770 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
773 #endif
776 * "id" is the POSIX thread ID. We use the
777 * files pointer for this..
779 int filp_close(struct file *filp, fl_owner_t id)
781 int retval;
783 if (!file_count(filp)) {
784 printk("VFS: Close: file count is 0\n");
785 return 0;
787 retval = 0;
788 if (filp->f_op && filp->f_op->flush) {
789 lock_kernel();
790 retval = filp->f_op->flush(filp);
791 unlock_kernel();
793 locks_remove_posix(filp, id);
794 fput(filp);
795 return retval;
799 * Careful here! We test whether the file pointer is NULL before
800 * releasing the fd. This ensures that one clone task can't release
801 * an fd while another clone is opening it.
803 asmlinkage long sys_close(unsigned int fd)
805 struct file * filp;
806 struct files_struct *files = current->files;
808 write_lock(&files->file_lock);
809 if (fd >= files->max_fds)
810 goto out_unlock;
811 filp = files->fd[fd];
812 if (!filp)
813 goto out_unlock;
814 files->fd[fd] = NULL;
815 FD_CLR(fd, files->close_on_exec);
816 __put_unused_fd(files, fd);
817 write_unlock(&files->file_lock);
818 return filp_close(filp, files);
820 out_unlock:
821 write_unlock(&files->file_lock);
822 return -EBADF;
826 * This routine simulates a hangup on the tty, to arrange that users
827 * are given clean terminals at login time.
829 asmlinkage long sys_vhangup(void)
831 if (capable(CAP_SYS_TTY_CONFIG)) {
832 tty_vhangup(current->tty);
833 return 0;
835 return -EPERM;