Import 2.3.12pre9
[davej-history.git] / fs / open.c
blob11e0855b1f45eb86595c9e41e2566e5460ee0a69
1 /*
2 * linux/fs/open.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/mm.h>
8 #include <linux/utime.h>
9 #include <linux/file.h>
10 #include <linux/smp_lock.h>
11 #include <linux/quotaops.h>
13 #include <asm/uaccess.h>
15 asmlinkage int sys_statfs(const char * path, struct statfs * buf)
17 struct dentry * dentry;
18 int error;
20 lock_kernel();
21 dentry = namei(path);
22 error = PTR_ERR(dentry);
23 if (!IS_ERR(dentry)) {
24 struct inode * inode = dentry->d_inode;
25 struct super_block * sb = inode->i_sb;
27 error = -ENODEV;
28 if (sb && sb->s_op && sb->s_op->statfs)
29 error = sb->s_op->statfs(sb, buf, sizeof(struct statfs));
31 dput(dentry);
33 unlock_kernel();
34 return error;
37 asmlinkage int sys_fstatfs(unsigned int fd, struct statfs * buf)
39 struct file * file;
40 struct super_block * sb;
41 int error;
43 error = -EBADF;
44 file = fget(fd);
45 if (!file)
46 goto out;
47 error = -ENODEV;
48 sb = file->f_dentry->d_inode->i_sb;
49 lock_kernel();
50 if (sb && sb->s_op && sb->s_op->statfs)
51 error = sb->s_op->statfs(sb, buf, sizeof(struct statfs));
52 unlock_kernel();
53 fput(file);
54 out:
55 return error;
58 int do_truncate(struct dentry *dentry, unsigned long length)
60 struct inode *inode = dentry->d_inode;
61 int error;
62 struct iattr newattrs;
64 /* Not pretty: "inode->i_size" shouldn't really be "off_t". But it is. */
65 if ((off_t) length < 0)
66 return -EINVAL;
68 down(&inode->i_sem);
69 newattrs.ia_size = length;
70 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
71 error = notify_change(dentry, &newattrs);
72 if (!error) {
73 /* truncate virtual mappings of this file */
74 vmtruncate(inode, length);
75 if (inode->i_op && inode->i_op->truncate)
76 inode->i_op->truncate(inode);
78 up(&inode->i_sem);
79 return error;
82 asmlinkage int sys_truncate(const char * path, unsigned long length)
84 struct dentry * dentry;
85 struct inode * inode;
86 int error;
88 lock_kernel();
89 dentry = namei(path);
91 error = PTR_ERR(dentry);
92 if (IS_ERR(dentry))
93 goto out;
94 inode = dentry->d_inode;
96 error = -EACCES;
97 if (S_ISDIR(inode->i_mode))
98 goto dput_and_out;
100 error = permission(inode,MAY_WRITE);
101 if (error)
102 goto dput_and_out;
104 error = -EROFS;
105 if (IS_RDONLY(inode))
106 goto dput_and_out;
108 error = -EPERM;
109 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
110 goto dput_and_out;
112 error = get_write_access(inode);
113 if (error)
114 goto dput_and_out;
116 error = locks_verify_area(FLOCK_VERIFY_WRITE, inode, NULL,
117 length < inode->i_size ? length : inode->i_size,
118 abs(inode->i_size - length));
119 if (!error) {
120 DQUOT_INIT(inode);
121 error = do_truncate(dentry, length);
123 put_write_access(inode);
124 dput_and_out:
125 dput(dentry);
126 out:
127 unlock_kernel();
128 return error;
131 asmlinkage int sys_ftruncate(unsigned int fd, unsigned long length)
133 struct inode * inode;
134 struct dentry *dentry;
135 struct file * file;
136 int error;
138 error = -EBADF;
139 file = fget(fd);
140 if (!file)
141 goto out;
142 error = -ENOENT;
143 if (!(dentry = file->f_dentry))
144 goto out_putf;
145 if (!(inode = dentry->d_inode))
146 goto out_putf;
147 error = -EACCES;
148 if (S_ISDIR(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
149 goto out_putf;
150 error = -EPERM;
151 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
152 goto out_putf;
153 error = locks_verify_area(FLOCK_VERIFY_WRITE, inode, file,
154 length<inode->i_size ? length : inode->i_size,
155 abs(inode->i_size - length));
156 lock_kernel();
157 if (!error)
158 error = do_truncate(dentry, length);
159 unlock_kernel();
160 out_putf:
161 fput(file);
162 out:
163 return error;
166 #if !(defined(__alpha__) || defined(__ia64__))
169 * sys_utime() can be implemented in user-level using sys_utimes().
170 * Is this for backwards compatibility? If so, why not move it
171 * into the appropriate arch directory (for those architectures that
172 * need it).
175 /* If times==NULL, set access and modification to current time,
176 * must be owner or have write permission.
177 * Else, update from *times, must be owner or super user.
179 asmlinkage int sys_utime(char * filename, struct utimbuf * times)
181 int error;
182 struct dentry * dentry;
183 struct inode * inode;
184 struct iattr newattrs;
186 lock_kernel();
187 dentry = namei(filename);
189 error = PTR_ERR(dentry);
190 if (IS_ERR(dentry))
191 goto out;
192 inode = dentry->d_inode;
194 error = -EROFS;
195 if (IS_RDONLY(inode))
196 goto dput_and_out;
198 /* Don't worry, the checks are done in inode_change_ok() */
199 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
200 if (times) {
201 error = get_user(newattrs.ia_atime, &times->actime);
202 if (!error)
203 error = get_user(newattrs.ia_mtime, &times->modtime);
204 if (error)
205 goto dput_and_out;
207 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
208 } else {
209 if (current->fsuid != inode->i_uid &&
210 (error = permission(inode,MAY_WRITE)) != 0)
211 goto dput_and_out;
213 error = notify_change(dentry, &newattrs);
214 dput_and_out:
215 dput(dentry);
216 out:
217 unlock_kernel();
218 return error;
221 #endif
223 /* If times==NULL, set access and modification to current time,
224 * must be owner or have write permission.
225 * Else, update from *times, must be owner or super user.
227 asmlinkage int sys_utimes(char * filename, struct timeval * utimes)
229 int error;
230 struct dentry * dentry;
231 struct inode * inode;
232 struct iattr newattrs;
234 lock_kernel();
235 dentry = namei(filename);
237 error = PTR_ERR(dentry);
238 if (IS_ERR(dentry))
239 goto out;
240 inode = dentry->d_inode;
242 error = -EROFS;
243 if (IS_RDONLY(inode))
244 goto dput_and_out;
246 /* Don't worry, the checks are done in inode_change_ok() */
247 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
248 if (utimes) {
249 struct timeval times[2];
250 error = -EFAULT;
251 if (copy_from_user(&times, utimes, sizeof(times)))
252 goto dput_and_out;
253 newattrs.ia_atime = times[0].tv_sec;
254 newattrs.ia_mtime = times[1].tv_sec;
255 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
256 } else {
257 if ((error = permission(inode,MAY_WRITE)) != 0)
258 goto dput_and_out;
260 error = notify_change(dentry, &newattrs);
261 dput_and_out:
262 dput(dentry);
263 out:
264 unlock_kernel();
265 return error;
269 * access() needs to use the real uid/gid, not the effective uid/gid.
270 * We do this by temporarily clearing all FS-related capabilities and
271 * switching the fsuid/fsgid around to the real ones.
273 asmlinkage int sys_access(const char * filename, int mode)
275 struct dentry * dentry;
276 int old_fsuid, old_fsgid;
277 kernel_cap_t old_cap;
278 int res = -EINVAL;
280 lock_kernel();
281 if (mode != (mode & S_IRWXO)) /* where's F_OK, X_OK, W_OK, R_OK? */
282 goto out;
283 old_fsuid = current->fsuid;
284 old_fsgid = current->fsgid;
285 old_cap = current->cap_effective;
287 current->fsuid = current->uid;
288 current->fsgid = current->gid;
290 /* Clear the capabilities if we switch to a non-root user */
291 if (current->uid)
292 cap_clear(current->cap_effective);
293 else
294 current->cap_effective = current->cap_permitted;
296 dentry = namei(filename);
297 res = PTR_ERR(dentry);
298 if (!IS_ERR(dentry)) {
299 res = permission(dentry->d_inode, mode);
300 /* SuS v2 requires we report a read only fs too */
301 if(!res && (mode & S_IWOTH) && IS_RDONLY(dentry->d_inode))
302 res = -EROFS;
303 dput(dentry);
306 current->fsuid = old_fsuid;
307 current->fsgid = old_fsgid;
308 current->cap_effective = old_cap;
309 out:
310 unlock_kernel();
311 return res;
314 asmlinkage int sys_chdir(const char * filename)
316 int error;
317 struct inode *inode;
318 struct dentry *dentry, *tmp;
320 lock_kernel();
322 dentry = namei(filename);
323 error = PTR_ERR(dentry);
324 if (IS_ERR(dentry))
325 goto out;
327 inode = dentry->d_inode;
329 error = -ENOTDIR;
330 if (!S_ISDIR(inode->i_mode))
331 goto dput_and_out;
333 error = permission(inode,MAY_EXEC);
334 if (error)
335 goto dput_and_out;
337 /* exchange dentries */
338 tmp = current->fs->pwd;
339 current->fs->pwd = dentry;
340 dentry = tmp;
342 dput_and_out:
343 dput(dentry);
344 out:
345 unlock_kernel();
346 return error;
349 asmlinkage int sys_fchdir(unsigned int fd)
351 struct file *file;
352 struct dentry *dentry;
353 struct inode *inode;
354 int error;
356 error = -EBADF;
357 file = fget(fd);
358 if (!file)
359 goto out;
361 error = -ENOENT;
362 if (!(dentry = file->f_dentry))
363 goto out_putf;
364 if (!(inode = dentry->d_inode))
365 goto out_putf;
367 error = -ENOTDIR;
368 if (!S_ISDIR(inode->i_mode))
369 goto out_putf;
371 lock_kernel();
372 error = permission(inode, MAY_EXEC);
373 if (!error) {
374 struct dentry *tmp = current->fs->pwd;
375 current->fs->pwd = dget(dentry);
376 dput(tmp);
378 unlock_kernel();
379 out_putf:
380 fput(file);
381 out:
382 return error;
385 asmlinkage int sys_chroot(const char * filename)
387 int error;
388 struct inode *inode;
389 struct dentry *dentry, *tmp;
391 lock_kernel();
393 dentry = namei(filename);
394 error = PTR_ERR(dentry);
395 if (IS_ERR(dentry))
396 goto out;
398 inode = dentry->d_inode;
400 error = -ENOTDIR;
401 if (!S_ISDIR(inode->i_mode))
402 goto dput_and_out;
404 error = permission(inode,MAY_EXEC);
405 if (error)
406 goto dput_and_out;
408 error = -EPERM;
409 if (!capable(CAP_SYS_CHROOT))
410 goto dput_and_out;
412 /* exchange dentries */
413 tmp = current->fs->root;
414 current->fs->root = dentry;
415 dentry = tmp;
416 error = 0;
418 dput_and_out:
419 dput(dentry);
420 out:
421 unlock_kernel();
422 return error;
425 asmlinkage int sys_fchmod(unsigned int fd, mode_t mode)
427 struct inode * inode;
428 struct dentry * dentry;
429 struct file * file;
430 int err = -EBADF;
431 struct iattr newattrs;
433 file = fget(fd);
434 if (!file)
435 goto out;
437 err = -ENOENT;
438 if (!(dentry = file->f_dentry))
439 goto out_putf;
440 if (!(inode = dentry->d_inode))
441 goto out_putf;
443 err = -EROFS;
444 if (IS_RDONLY(inode))
445 goto out_putf;
446 err = -EPERM;
447 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
448 goto out_putf;
449 if (mode == (mode_t) -1)
450 mode = inode->i_mode;
451 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
452 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
453 lock_kernel();
454 err = notify_change(dentry, &newattrs);
455 unlock_kernel();
457 out_putf:
458 fput(file);
459 out:
460 return err;
463 asmlinkage int sys_chmod(const char * filename, mode_t mode)
465 struct dentry * dentry;
466 struct inode * inode;
467 int error;
468 struct iattr newattrs;
470 lock_kernel();
471 dentry = namei(filename);
473 error = PTR_ERR(dentry);
474 if (IS_ERR(dentry))
475 goto out;
476 inode = dentry->d_inode;
478 error = -EROFS;
479 if (IS_RDONLY(inode))
480 goto dput_and_out;
482 error = -EPERM;
483 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
484 goto dput_and_out;
486 if (mode == (mode_t) -1)
487 mode = inode->i_mode;
488 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
489 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
490 error = notify_change(dentry, &newattrs);
492 dput_and_out:
493 dput(dentry);
494 out:
495 unlock_kernel();
496 return error;
499 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
501 struct inode * inode;
502 int error;
503 struct iattr newattrs;
505 error = -ENOENT;
506 if (!(inode = dentry->d_inode)) {
507 printk("chown_common: NULL inode\n");
508 goto out;
510 error = -EROFS;
511 if (IS_RDONLY(inode))
512 goto out;
513 error = -EPERM;
514 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
515 goto out;
516 if (user == (uid_t) -1)
517 user = inode->i_uid;
518 if (group == (gid_t) -1)
519 group = inode->i_gid;
520 newattrs.ia_mode = inode->i_mode;
521 newattrs.ia_uid = user;
522 newattrs.ia_gid = group;
523 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
525 * If the user or group of a non-directory has been changed by a
526 * non-root user, remove the setuid bit.
527 * 19981026 David C Niemi <niemi@tux.org>
530 if ((inode->i_mode & S_ISUID) == S_ISUID &&
531 !S_ISDIR(inode->i_mode)
532 && current->fsuid)
534 newattrs.ia_mode &= ~S_ISUID;
535 newattrs.ia_valid |= ATTR_MODE;
538 * Likewise, if the user or group of a non-directory has been changed
539 * by a non-root user, remove the setgid bit UNLESS there is no group
540 * execute bit (this would be a file marked for mandatory locking).
541 * 19981026 David C Niemi <niemi@tux.org>
543 if (((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
544 && !S_ISDIR(inode->i_mode) && current->fsuid)
546 newattrs.ia_mode &= ~S_ISGID;
547 newattrs.ia_valid |= ATTR_MODE;
549 error = DQUOT_TRANSFER(dentry, &newattrs);
550 out:
551 return error;
554 asmlinkage int sys_chown(const char * filename, uid_t user, gid_t group)
556 struct dentry * dentry;
557 int error;
559 lock_kernel();
560 dentry = namei(filename);
562 error = PTR_ERR(dentry);
563 if (!IS_ERR(dentry)) {
564 error = chown_common(dentry, user, group);
565 dput(dentry);
567 unlock_kernel();
568 return error;
571 asmlinkage int sys_lchown(const char * filename, uid_t user, gid_t group)
573 struct dentry * dentry;
574 int error;
576 lock_kernel();
577 dentry = lnamei(filename);
579 error = PTR_ERR(dentry);
580 if (!IS_ERR(dentry)) {
581 error = chown_common(dentry, user, group);
582 dput(dentry);
584 unlock_kernel();
585 return error;
589 asmlinkage int sys_fchown(unsigned int fd, uid_t user, gid_t group)
591 struct dentry * dentry;
592 struct file * file;
593 int error = -EBADF;
595 file = fget(fd);
596 if (!file)
597 goto out;
598 error = -ENOENT;
599 lock_kernel();
600 if ((dentry = file->f_dentry) != NULL)
601 error = chown_common(dentry, user, group);
602 unlock_kernel();
603 fput(file);
605 out:
606 return error;
610 * Note that while the flag value (low two bits) for sys_open means:
611 * 00 - read-only
612 * 01 - write-only
613 * 10 - read-write
614 * 11 - special
615 * it is changed into
616 * 00 - no permissions needed
617 * 01 - read-permission
618 * 10 - write-permission
619 * 11 - read-write
620 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
621 * used by symlinks.
623 struct file *filp_open(const char * filename, int flags, int mode)
625 struct inode * inode;
626 struct dentry * dentry;
627 struct file * f;
628 int flag,error;
630 error = -ENFILE;
631 f = get_empty_filp();
632 if (!f)
633 goto out;
634 f->f_flags = flag = flags;
635 f->f_mode = (flag+1) & O_ACCMODE;
636 if (f->f_mode)
637 flag++;
638 if (flag & O_TRUNC)
639 flag |= 2;
640 dentry = open_namei(filename,flag,mode);
641 error = PTR_ERR(dentry);
642 if (IS_ERR(dentry))
643 goto cleanup_file;
644 inode = dentry->d_inode;
645 if (f->f_mode & FMODE_WRITE) {
646 error = get_write_access(inode);
647 if (error)
648 goto cleanup_dentry;
651 f->f_dentry = dentry;
652 f->f_pos = 0;
653 f->f_reada = 0;
654 f->f_op = NULL;
655 if (inode->i_op)
656 f->f_op = inode->i_op->default_file_ops;
657 if (inode->i_sb)
658 file_move(f, &inode->i_sb->s_files);
659 if (f->f_op && f->f_op->open) {
660 error = f->f_op->open(inode,f);
661 if (error)
662 goto cleanup_all;
664 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
666 return f;
668 cleanup_all:
669 if (f->f_mode & FMODE_WRITE)
670 put_write_access(inode);
671 cleanup_dentry:
672 f->f_dentry = NULL;
673 dput(dentry);
674 cleanup_file:
675 put_filp(f);
676 out:
677 return ERR_PTR(error);
681 * Find an empty file descriptor entry, and mark it busy.
683 int get_unused_fd(void)
685 struct files_struct * files = current->files;
686 int fd, error;
688 error = -EMFILE;
689 write_lock(&files->file_lock);
691 repeat:
692 fd = find_next_zero_bit(files->open_fds,
693 current->files->max_fdset,
694 files->next_fd);
697 * N.B. For clone tasks sharing a files structure, this test
698 * will limit the total number of files that can be opened.
700 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
701 goto out;
703 /* Do we need to expand the fdset array? */
704 if (fd >= current->files->max_fdset) {
705 error = expand_fdset(files, 0);
706 if (!error) {
707 error = -EMFILE;
708 goto repeat;
710 goto out;
714 * Check whether we need to expand the fd array.
716 if (fd >= files->max_fds) {
717 error = expand_fd_array(files, 0);
718 if (!error) {
719 error = -EMFILE;
720 goto repeat;
722 goto out;
725 FD_SET(fd, files->open_fds);
726 FD_CLR(fd, files->close_on_exec);
727 files->next_fd = fd + 1;
728 #if 1
729 /* Sanity check */
730 if (files->fd[fd] != NULL) {
731 printk("get_unused_fd: slot %d not NULL!\n", fd);
732 files->fd[fd] = NULL;
734 #endif
735 error = fd;
737 out:
738 write_unlock(&files->file_lock);
739 return error;
742 inline void put_unused_fd(unsigned int fd)
744 write_lock(&current->files->file_lock);
745 FD_CLR(fd, current->files->open_fds);
746 if (fd < current->files->next_fd)
747 current->files->next_fd = fd;
748 write_unlock(&current->files->file_lock);
751 asmlinkage int sys_open(const char * filename, int flags, int mode)
753 char * tmp;
754 int fd, error;
756 tmp = getname(filename);
757 fd = PTR_ERR(tmp);
758 if (!IS_ERR(tmp)) {
759 fd = get_unused_fd();
760 if (fd >= 0) {
761 struct file * f;
762 lock_kernel();
763 f = filp_open(tmp, flags, mode);
764 unlock_kernel();
765 error = PTR_ERR(f);
766 if (IS_ERR(f))
767 goto out_error;
768 fd_install(fd, f);
770 out:
771 putname(tmp);
773 return fd;
775 out_error:
776 put_unused_fd(fd);
777 fd = error;
778 goto out;
781 #ifndef __alpha__
784 * For backward compatibility? Maybe this should be moved
785 * into arch/i386 instead?
787 asmlinkage int sys_creat(const char * pathname, int mode)
789 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
792 #endif
795 * "id" is the POSIX thread ID. We use the
796 * files pointer for this..
798 int filp_close(struct file *filp, fl_owner_t id)
800 int retval;
801 struct dentry *dentry = filp->f_dentry;
803 if (!file_count(filp)) {
804 printk("VFS: Close: file count is 0\n");
805 return 0;
807 retval = 0;
808 if (filp->f_op && filp->f_op->flush)
809 retval = filp->f_op->flush(filp);
810 if (dentry->d_inode)
811 locks_remove_posix(filp, id);
812 fput(filp);
813 return retval;
817 * Careful here! We test whether the file pointer is NULL before
818 * releasing the fd. This ensures that one clone task can't release
819 * an fd while another clone is opening it.
821 * The "release" argument tells us whether or not to mark the fd as free
822 * or not in the open-files bitmap. dup2 uses this to retain the fd
823 * without races.
825 int do_close(unsigned int fd, int release)
827 int error;
828 struct file * filp;
829 struct files_struct * files = current->files;
831 error = -EBADF;
832 write_lock(&files->file_lock);
833 filp = frip(fd);
834 if (!filp)
835 goto out_unlock;
836 FD_CLR(fd, files->close_on_exec);
837 write_unlock(&files->file_lock);
838 if (release)
839 put_unused_fd(fd);
840 lock_kernel();
841 error = filp_close(filp, files);
842 unlock_kernel();
843 out:
844 return error;
845 out_unlock:
846 write_unlock(&files->file_lock);
847 goto out;
850 asmlinkage int sys_close(unsigned int fd)
852 return do_close(fd, 1);
856 * This routine simulates a hangup on the tty, to arrange that users
857 * are given clean terminals at login time.
859 asmlinkage int sys_vhangup(void)
861 int ret = -EPERM;
863 if (!capable(CAP_SYS_TTY_CONFIG))
864 goto out;
865 /* If there is a controlling tty, hang it up */
866 lock_kernel();
867 if (current->tty)
868 tty_vhangup(current->tty);
869 unlock_kernel();
870 ret = 0;
871 out:
872 return ret;