[PATCH] x86_64: cpu_pda array to macro followup correction
[linux-2.6/suspend2-2.6.18.git] / fs / open.c
blob7d02d19bd0a28281a108353883902fae745e1f10
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/fsnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/capability.h>
20 #include <linux/security.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
25 #include <linux/fs.h>
26 #include <linux/personality.h>
27 #include <linux/pagemap.h>
28 #include <linux/syscalls.h>
29 #include <linux/rcupdate.h>
31 #include <asm/unistd.h>
33 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
35 int retval = -ENODEV;
37 if (sb) {
38 retval = -ENOSYS;
39 if (sb->s_op->statfs) {
40 memset(buf, 0, sizeof(*buf));
41 retval = security_sb_statfs(sb);
42 if (retval)
43 return retval;
44 retval = sb->s_op->statfs(sb, buf);
45 if (retval == 0 && buf->f_frsize == 0)
46 buf->f_frsize = buf->f_bsize;
49 return retval;
52 EXPORT_SYMBOL(vfs_statfs);
54 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
56 struct kstatfs st;
57 int retval;
59 retval = vfs_statfs(sb, &st);
60 if (retval)
61 return retval;
63 if (sizeof(*buf) == sizeof(st))
64 memcpy(buf, &st, sizeof(st));
65 else {
66 if (sizeof buf->f_blocks == 4) {
67 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
68 0xffffffff00000000ULL)
69 return -EOVERFLOW;
71 * f_files and f_ffree may be -1; it's okay to stuff
72 * that into 32 bits
74 if (st.f_files != -1 &&
75 (st.f_files & 0xffffffff00000000ULL))
76 return -EOVERFLOW;
77 if (st.f_ffree != -1 &&
78 (st.f_ffree & 0xffffffff00000000ULL))
79 return -EOVERFLOW;
82 buf->f_type = st.f_type;
83 buf->f_bsize = st.f_bsize;
84 buf->f_blocks = st.f_blocks;
85 buf->f_bfree = st.f_bfree;
86 buf->f_bavail = st.f_bavail;
87 buf->f_files = st.f_files;
88 buf->f_ffree = st.f_ffree;
89 buf->f_fsid = st.f_fsid;
90 buf->f_namelen = st.f_namelen;
91 buf->f_frsize = st.f_frsize;
92 memset(buf->f_spare, 0, sizeof(buf->f_spare));
94 return 0;
97 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
99 struct kstatfs st;
100 int retval;
102 retval = vfs_statfs(sb, &st);
103 if (retval)
104 return retval;
106 if (sizeof(*buf) == sizeof(st))
107 memcpy(buf, &st, sizeof(st));
108 else {
109 buf->f_type = st.f_type;
110 buf->f_bsize = st.f_bsize;
111 buf->f_blocks = st.f_blocks;
112 buf->f_bfree = st.f_bfree;
113 buf->f_bavail = st.f_bavail;
114 buf->f_files = st.f_files;
115 buf->f_ffree = st.f_ffree;
116 buf->f_fsid = st.f_fsid;
117 buf->f_namelen = st.f_namelen;
118 buf->f_frsize = st.f_frsize;
119 memset(buf->f_spare, 0, sizeof(buf->f_spare));
121 return 0;
124 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
126 struct nameidata nd;
127 int error;
129 error = user_path_walk(path, &nd);
130 if (!error) {
131 struct statfs tmp;
132 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
133 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
134 error = -EFAULT;
135 path_release(&nd);
137 return error;
141 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
143 struct nameidata nd;
144 long error;
146 if (sz != sizeof(*buf))
147 return -EINVAL;
148 error = user_path_walk(path, &nd);
149 if (!error) {
150 struct statfs64 tmp;
151 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
152 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
153 error = -EFAULT;
154 path_release(&nd);
156 return error;
160 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
162 struct file * file;
163 struct statfs tmp;
164 int error;
166 error = -EBADF;
167 file = fget(fd);
168 if (!file)
169 goto out;
170 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
171 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
172 error = -EFAULT;
173 fput(file);
174 out:
175 return error;
178 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
180 struct file * file;
181 struct statfs64 tmp;
182 int error;
184 if (sz != sizeof(*buf))
185 return -EINVAL;
187 error = -EBADF;
188 file = fget(fd);
189 if (!file)
190 goto out;
191 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
192 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
193 error = -EFAULT;
194 fput(file);
195 out:
196 return error;
199 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
200 struct file *filp)
202 int err;
203 struct iattr newattrs;
205 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
206 if (length < 0)
207 return -EINVAL;
209 newattrs.ia_size = length;
210 newattrs.ia_valid = ATTR_SIZE | time_attrs;
211 if (filp) {
212 newattrs.ia_file = filp;
213 newattrs.ia_valid |= ATTR_FILE;
216 mutex_lock(&dentry->d_inode->i_mutex);
217 err = notify_change(dentry, &newattrs);
218 mutex_unlock(&dentry->d_inode->i_mutex);
219 return err;
222 static long do_sys_truncate(const char __user * path, loff_t length)
224 struct nameidata nd;
225 struct inode * inode;
226 int error;
228 error = -EINVAL;
229 if (length < 0) /* sorry, but loff_t says... */
230 goto out;
232 error = user_path_walk(path, &nd);
233 if (error)
234 goto out;
235 inode = nd.dentry->d_inode;
237 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
238 error = -EISDIR;
239 if (S_ISDIR(inode->i_mode))
240 goto dput_and_out;
242 error = -EINVAL;
243 if (!S_ISREG(inode->i_mode))
244 goto dput_and_out;
246 error = vfs_permission(&nd, MAY_WRITE);
247 if (error)
248 goto dput_and_out;
250 error = -EROFS;
251 if (IS_RDONLY(inode))
252 goto dput_and_out;
254 error = -EPERM;
255 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
256 goto dput_and_out;
259 * Make sure that there are no leases.
261 error = break_lease(inode, FMODE_WRITE);
262 if (error)
263 goto dput_and_out;
265 error = get_write_access(inode);
266 if (error)
267 goto dput_and_out;
269 error = locks_verify_truncate(inode, NULL, length);
270 if (!error) {
271 DQUOT_INIT(inode);
272 error = do_truncate(nd.dentry, length, 0, NULL);
274 put_write_access(inode);
276 dput_and_out:
277 path_release(&nd);
278 out:
279 return error;
282 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
284 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
285 return do_sys_truncate(path, (long)length);
288 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
290 struct inode * inode;
291 struct dentry *dentry;
292 struct file * file;
293 int error;
295 error = -EINVAL;
296 if (length < 0)
297 goto out;
298 error = -EBADF;
299 file = fget(fd);
300 if (!file)
301 goto out;
303 /* explicitly opened as large or we are on 64-bit box */
304 if (file->f_flags & O_LARGEFILE)
305 small = 0;
307 dentry = file->f_dentry;
308 inode = dentry->d_inode;
309 error = -EINVAL;
310 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
311 goto out_putf;
313 error = -EINVAL;
314 /* Cannot ftruncate over 2^31 bytes without large file support */
315 if (small && length > MAX_NON_LFS)
316 goto out_putf;
318 error = -EPERM;
319 if (IS_APPEND(inode))
320 goto out_putf;
322 error = locks_verify_truncate(inode, file, length);
323 if (!error)
324 error = do_truncate(dentry, length, 0, file);
325 out_putf:
326 fput(file);
327 out:
328 return error;
331 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
333 return do_sys_ftruncate(fd, length, 1);
336 /* LFS versions of truncate are only needed on 32 bit machines */
337 #if BITS_PER_LONG == 32
338 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
340 return do_sys_truncate(path, length);
343 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
345 return do_sys_ftruncate(fd, length, 0);
347 #endif
349 #ifdef __ARCH_WANT_SYS_UTIME
352 * sys_utime() can be implemented in user-level using sys_utimes().
353 * Is this for backwards compatibility? If so, why not move it
354 * into the appropriate arch directory (for those architectures that
355 * need it).
358 /* If times==NULL, set access and modification to current time,
359 * must be owner or have write permission.
360 * Else, update from *times, must be owner or super user.
362 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
364 int error;
365 struct nameidata nd;
366 struct inode * inode;
367 struct iattr newattrs;
369 error = user_path_walk(filename, &nd);
370 if (error)
371 goto out;
372 inode = nd.dentry->d_inode;
374 error = -EROFS;
375 if (IS_RDONLY(inode))
376 goto dput_and_out;
378 /* Don't worry, the checks are done in inode_change_ok() */
379 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
380 if (times) {
381 error = -EPERM;
382 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
383 goto dput_and_out;
385 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
386 newattrs.ia_atime.tv_nsec = 0;
387 if (!error)
388 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
389 newattrs.ia_mtime.tv_nsec = 0;
390 if (error)
391 goto dput_and_out;
393 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
394 } else {
395 error = -EACCES;
396 if (IS_IMMUTABLE(inode))
397 goto dput_and_out;
399 if (current->fsuid != inode->i_uid &&
400 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
401 goto dput_and_out;
403 mutex_lock(&inode->i_mutex);
404 error = notify_change(nd.dentry, &newattrs);
405 mutex_unlock(&inode->i_mutex);
406 dput_and_out:
407 path_release(&nd);
408 out:
409 return error;
412 #endif
414 /* If times==NULL, set access and modification to current time,
415 * must be owner or have write permission.
416 * Else, update from *times, must be owner or super user.
418 long do_utimes(int dfd, char __user *filename, struct timeval *times)
420 int error;
421 struct nameidata nd;
422 struct inode * inode;
423 struct iattr newattrs;
425 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
427 if (error)
428 goto out;
429 inode = nd.dentry->d_inode;
431 error = -EROFS;
432 if (IS_RDONLY(inode))
433 goto dput_and_out;
435 /* Don't worry, the checks are done in inode_change_ok() */
436 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
437 if (times) {
438 error = -EPERM;
439 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
440 goto dput_and_out;
442 newattrs.ia_atime.tv_sec = times[0].tv_sec;
443 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
444 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
445 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
446 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
447 } else {
448 error = -EACCES;
449 if (IS_IMMUTABLE(inode))
450 goto dput_and_out;
452 if (current->fsuid != inode->i_uid &&
453 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
454 goto dput_and_out;
456 mutex_lock(&inode->i_mutex);
457 error = notify_change(nd.dentry, &newattrs);
458 mutex_unlock(&inode->i_mutex);
459 dput_and_out:
460 path_release(&nd);
461 out:
462 return error;
465 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
467 struct timeval times[2];
469 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
470 return -EFAULT;
471 return do_utimes(dfd, filename, utimes ? times : NULL);
474 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
476 return sys_futimesat(AT_FDCWD, filename, utimes);
481 * access() needs to use the real uid/gid, not the effective uid/gid.
482 * We do this by temporarily clearing all FS-related capabilities and
483 * switching the fsuid/fsgid around to the real ones.
485 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
487 struct nameidata nd;
488 int old_fsuid, old_fsgid;
489 kernel_cap_t old_cap;
490 int res;
492 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
493 return -EINVAL;
495 old_fsuid = current->fsuid;
496 old_fsgid = current->fsgid;
497 old_cap = current->cap_effective;
499 current->fsuid = current->uid;
500 current->fsgid = current->gid;
503 * Clear the capabilities if we switch to a non-root user
505 * FIXME: There is a race here against sys_capset. The
506 * capabilities can change yet we will restore the old
507 * value below. We should hold task_capabilities_lock,
508 * but we cannot because user_path_walk can sleep.
510 if (current->uid)
511 cap_clear(current->cap_effective);
512 else
513 current->cap_effective = current->cap_permitted;
515 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
516 if (!res) {
517 res = vfs_permission(&nd, mode);
518 /* SuS v2 requires we report a read only fs too */
519 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
520 && !special_file(nd.dentry->d_inode->i_mode))
521 res = -EROFS;
522 path_release(&nd);
525 current->fsuid = old_fsuid;
526 current->fsgid = old_fsgid;
527 current->cap_effective = old_cap;
529 return res;
532 asmlinkage long sys_access(const char __user *filename, int mode)
534 return sys_faccessat(AT_FDCWD, filename, mode);
537 asmlinkage long sys_chdir(const char __user * filename)
539 struct nameidata nd;
540 int error;
542 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
543 if (error)
544 goto out;
546 error = vfs_permission(&nd, MAY_EXEC);
547 if (error)
548 goto dput_and_out;
550 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
552 dput_and_out:
553 path_release(&nd);
554 out:
555 return error;
558 asmlinkage long sys_fchdir(unsigned int fd)
560 struct file *file;
561 struct dentry *dentry;
562 struct inode *inode;
563 struct vfsmount *mnt;
564 int error;
566 error = -EBADF;
567 file = fget(fd);
568 if (!file)
569 goto out;
571 dentry = file->f_dentry;
572 mnt = file->f_vfsmnt;
573 inode = dentry->d_inode;
575 error = -ENOTDIR;
576 if (!S_ISDIR(inode->i_mode))
577 goto out_putf;
579 error = file_permission(file, MAY_EXEC);
580 if (!error)
581 set_fs_pwd(current->fs, mnt, dentry);
582 out_putf:
583 fput(file);
584 out:
585 return error;
588 asmlinkage long sys_chroot(const char __user * filename)
590 struct nameidata nd;
591 int error;
593 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
594 if (error)
595 goto out;
597 error = vfs_permission(&nd, MAY_EXEC);
598 if (error)
599 goto dput_and_out;
601 error = -EPERM;
602 if (!capable(CAP_SYS_CHROOT))
603 goto dput_and_out;
605 set_fs_root(current->fs, nd.mnt, nd.dentry);
606 set_fs_altroot();
607 error = 0;
608 dput_and_out:
609 path_release(&nd);
610 out:
611 return error;
614 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
616 struct inode * inode;
617 struct dentry * dentry;
618 struct file * file;
619 int err = -EBADF;
620 struct iattr newattrs;
622 file = fget(fd);
623 if (!file)
624 goto out;
626 dentry = file->f_dentry;
627 inode = dentry->d_inode;
629 err = -EROFS;
630 if (IS_RDONLY(inode))
631 goto out_putf;
632 err = -EPERM;
633 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
634 goto out_putf;
635 mutex_lock(&inode->i_mutex);
636 if (mode == (mode_t) -1)
637 mode = inode->i_mode;
638 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
639 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
640 err = notify_change(dentry, &newattrs);
641 mutex_unlock(&inode->i_mutex);
643 out_putf:
644 fput(file);
645 out:
646 return err;
649 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
650 mode_t mode)
652 struct nameidata nd;
653 struct inode * inode;
654 int error;
655 struct iattr newattrs;
657 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
658 if (error)
659 goto out;
660 inode = nd.dentry->d_inode;
662 error = -EROFS;
663 if (IS_RDONLY(inode))
664 goto dput_and_out;
666 error = -EPERM;
667 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
668 goto dput_and_out;
670 mutex_lock(&inode->i_mutex);
671 if (mode == (mode_t) -1)
672 mode = inode->i_mode;
673 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
674 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
675 error = notify_change(nd.dentry, &newattrs);
676 mutex_unlock(&inode->i_mutex);
678 dput_and_out:
679 path_release(&nd);
680 out:
681 return error;
684 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
686 return sys_fchmodat(AT_FDCWD, filename, mode);
689 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
691 struct inode * inode;
692 int error;
693 struct iattr newattrs;
695 error = -ENOENT;
696 if (!(inode = dentry->d_inode)) {
697 printk(KERN_ERR "chown_common: NULL inode\n");
698 goto out;
700 error = -EROFS;
701 if (IS_RDONLY(inode))
702 goto out;
703 error = -EPERM;
704 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
705 goto out;
706 newattrs.ia_valid = ATTR_CTIME;
707 if (user != (uid_t) -1) {
708 newattrs.ia_valid |= ATTR_UID;
709 newattrs.ia_uid = user;
711 if (group != (gid_t) -1) {
712 newattrs.ia_valid |= ATTR_GID;
713 newattrs.ia_gid = group;
715 if (!S_ISDIR(inode->i_mode))
716 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
717 mutex_lock(&inode->i_mutex);
718 error = notify_change(dentry, &newattrs);
719 mutex_unlock(&inode->i_mutex);
720 out:
721 return error;
724 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
726 struct nameidata nd;
727 int error;
729 error = user_path_walk(filename, &nd);
730 if (!error) {
731 error = chown_common(nd.dentry, user, group);
732 path_release(&nd);
734 return error;
737 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
738 gid_t group, int flag)
740 struct nameidata nd;
741 int error = -EINVAL;
742 int follow;
744 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
745 goto out;
747 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
748 error = __user_walk_fd(dfd, filename, follow, &nd);
749 if (!error) {
750 error = chown_common(nd.dentry, user, group);
751 path_release(&nd);
753 out:
754 return error;
757 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
759 struct nameidata nd;
760 int error;
762 error = user_path_walk_link(filename, &nd);
763 if (!error) {
764 error = chown_common(nd.dentry, user, group);
765 path_release(&nd);
767 return error;
771 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
773 struct file * file;
774 int error = -EBADF;
776 file = fget(fd);
777 if (file) {
778 error = chown_common(file->f_dentry, user, group);
779 fput(file);
781 return error;
784 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
785 int flags, struct file *f,
786 int (*open)(struct inode *, struct file *))
788 struct inode *inode;
789 int error;
791 f->f_flags = flags;
792 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
793 FMODE_PREAD | FMODE_PWRITE;
794 inode = dentry->d_inode;
795 if (f->f_mode & FMODE_WRITE) {
796 error = get_write_access(inode);
797 if (error)
798 goto cleanup_file;
801 f->f_mapping = inode->i_mapping;
802 f->f_dentry = dentry;
803 f->f_vfsmnt = mnt;
804 f->f_pos = 0;
805 f->f_op = fops_get(inode->i_fop);
806 file_move(f, &inode->i_sb->s_files);
808 if (!open && f->f_op)
809 open = f->f_op->open;
810 if (open) {
811 error = open(inode, f);
812 if (error)
813 goto cleanup_all;
816 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
818 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
820 /* NB: we're sure to have correct a_ops only after f_op->open */
821 if (f->f_flags & O_DIRECT) {
822 if (!f->f_mapping->a_ops ||
823 ((!f->f_mapping->a_ops->direct_IO) &&
824 (!f->f_mapping->a_ops->get_xip_page))) {
825 fput(f);
826 f = ERR_PTR(-EINVAL);
830 return f;
832 cleanup_all:
833 fops_put(f->f_op);
834 if (f->f_mode & FMODE_WRITE)
835 put_write_access(inode);
836 file_kill(f);
837 f->f_dentry = NULL;
838 f->f_vfsmnt = NULL;
839 cleanup_file:
840 put_filp(f);
841 dput(dentry);
842 mntput(mnt);
843 return ERR_PTR(error);
847 * Note that while the flag value (low two bits) for sys_open means:
848 * 00 - read-only
849 * 01 - write-only
850 * 10 - read-write
851 * 11 - special
852 * it is changed into
853 * 00 - no permissions needed
854 * 01 - read-permission
855 * 10 - write-permission
856 * 11 - read-write
857 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
858 * used by symlinks.
860 static struct file *do_filp_open(int dfd, const char *filename, int flags,
861 int mode)
863 int namei_flags, error;
864 struct nameidata nd;
866 namei_flags = flags;
867 if ((namei_flags+1) & O_ACCMODE)
868 namei_flags++;
870 error = open_namei(dfd, filename, namei_flags, mode, &nd);
871 if (!error)
872 return nameidata_to_filp(&nd, flags);
874 return ERR_PTR(error);
877 struct file *filp_open(const char *filename, int flags, int mode)
879 return do_filp_open(AT_FDCWD, filename, flags, mode);
881 EXPORT_SYMBOL(filp_open);
884 * lookup_instantiate_filp - instantiates the open intent filp
885 * @nd: pointer to nameidata
886 * @dentry: pointer to dentry
887 * @open: open callback
889 * Helper for filesystems that want to use lookup open intents and pass back
890 * a fully instantiated struct file to the caller.
891 * This function is meant to be called from within a filesystem's
892 * lookup method.
893 * Beware of calling it for non-regular files! Those ->open methods might block
894 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
895 * leading to a deadlock, as nobody can open that fifo anymore, because
896 * another process to open fifo will block on locked parent when doing lookup).
897 * Note that in case of error, nd->intent.open.file is destroyed, but the
898 * path information remains valid.
899 * If the open callback is set to NULL, then the standard f_op->open()
900 * filesystem callback is substituted.
902 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
903 int (*open)(struct inode *, struct file *))
905 if (IS_ERR(nd->intent.open.file))
906 goto out;
907 if (IS_ERR(dentry))
908 goto out_err;
909 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
910 nd->intent.open.flags - 1,
911 nd->intent.open.file,
912 open);
913 out:
914 return nd->intent.open.file;
915 out_err:
916 release_open_intent(nd);
917 nd->intent.open.file = (struct file *)dentry;
918 goto out;
920 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
923 * nameidata_to_filp - convert a nameidata to an open filp.
924 * @nd: pointer to nameidata
925 * @flags: open flags
927 * Note that this function destroys the original nameidata
929 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
931 struct file *filp;
933 /* Pick up the filp from the open intent */
934 filp = nd->intent.open.file;
935 /* Has the filesystem initialised the file for us? */
936 if (filp->f_dentry == NULL)
937 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
938 else
939 path_release(nd);
940 return filp;
944 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
945 * error.
947 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
949 int error;
950 struct file *f;
952 error = -ENFILE;
953 f = get_empty_filp();
954 if (f == NULL) {
955 dput(dentry);
956 mntput(mnt);
957 return ERR_PTR(error);
960 return __dentry_open(dentry, mnt, flags, f, NULL);
962 EXPORT_SYMBOL(dentry_open);
965 * Find an empty file descriptor entry, and mark it busy.
967 int get_unused_fd(void)
969 struct files_struct * files = current->files;
970 int fd, error;
971 struct fdtable *fdt;
973 error = -EMFILE;
974 spin_lock(&files->file_lock);
976 repeat:
977 fdt = files_fdtable(files);
978 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
979 fdt->max_fdset,
980 files->next_fd);
983 * N.B. For clone tasks sharing a files structure, this test
984 * will limit the total number of files that can be opened.
986 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
987 goto out;
989 /* Do we need to expand the fd array or fd set? */
990 error = expand_files(files, fd);
991 if (error < 0)
992 goto out;
994 if (error) {
996 * If we needed to expand the fs array we
997 * might have blocked - try again.
999 error = -EMFILE;
1000 goto repeat;
1003 FD_SET(fd, fdt->open_fds);
1004 FD_CLR(fd, fdt->close_on_exec);
1005 files->next_fd = fd + 1;
1006 #if 1
1007 /* Sanity check */
1008 if (fdt->fd[fd] != NULL) {
1009 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1010 fdt->fd[fd] = NULL;
1012 #endif
1013 error = fd;
1015 out:
1016 spin_unlock(&files->file_lock);
1017 return error;
1020 EXPORT_SYMBOL(get_unused_fd);
1022 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1024 struct fdtable *fdt = files_fdtable(files);
1025 __FD_CLR(fd, fdt->open_fds);
1026 if (fd < files->next_fd)
1027 files->next_fd = fd;
1030 void fastcall put_unused_fd(unsigned int fd)
1032 struct files_struct *files = current->files;
1033 spin_lock(&files->file_lock);
1034 __put_unused_fd(files, fd);
1035 spin_unlock(&files->file_lock);
1038 EXPORT_SYMBOL(put_unused_fd);
1041 * Install a file pointer in the fd array.
1043 * The VFS is full of places where we drop the files lock between
1044 * setting the open_fds bitmap and installing the file in the file
1045 * array. At any such point, we are vulnerable to a dup2() race
1046 * installing a file in the array before us. We need to detect this and
1047 * fput() the struct file we are about to overwrite in this case.
1049 * It should never happen - if we allow dup2() do it, _really_ bad things
1050 * will follow.
1053 void fastcall fd_install(unsigned int fd, struct file * file)
1055 struct files_struct *files = current->files;
1056 struct fdtable *fdt;
1057 spin_lock(&files->file_lock);
1058 fdt = files_fdtable(files);
1059 BUG_ON(fdt->fd[fd] != NULL);
1060 rcu_assign_pointer(fdt->fd[fd], file);
1061 spin_unlock(&files->file_lock);
1064 EXPORT_SYMBOL(fd_install);
1066 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1068 char *tmp = getname(filename);
1069 int fd = PTR_ERR(tmp);
1071 if (!IS_ERR(tmp)) {
1072 fd = get_unused_fd();
1073 if (fd >= 0) {
1074 struct file *f = do_filp_open(dfd, tmp, flags, mode);
1075 if (IS_ERR(f)) {
1076 put_unused_fd(fd);
1077 fd = PTR_ERR(f);
1078 } else {
1079 fsnotify_open(f->f_dentry);
1080 fd_install(fd, f);
1083 putname(tmp);
1085 return fd;
1088 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1090 if (force_o_largefile())
1091 flags |= O_LARGEFILE;
1093 return do_sys_open(AT_FDCWD, filename, flags, mode);
1095 EXPORT_SYMBOL_GPL(sys_open);
1097 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1098 int mode)
1100 if (force_o_largefile())
1101 flags |= O_LARGEFILE;
1103 return do_sys_open(dfd, filename, flags, mode);
1105 EXPORT_SYMBOL_GPL(sys_openat);
1107 #ifndef __alpha__
1110 * For backward compatibility? Maybe this should be moved
1111 * into arch/i386 instead?
1113 asmlinkage long sys_creat(const char __user * pathname, int mode)
1115 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1118 #endif
1121 * "id" is the POSIX thread ID. We use the
1122 * files pointer for this..
1124 int filp_close(struct file *filp, fl_owner_t id)
1126 int retval = 0;
1128 if (!file_count(filp)) {
1129 printk(KERN_ERR "VFS: Close: file count is 0\n");
1130 return 0;
1133 if (filp->f_op && filp->f_op->flush)
1134 retval = filp->f_op->flush(filp);
1136 dnotify_flush(filp, id);
1137 locks_remove_posix(filp, id);
1138 fput(filp);
1139 return retval;
1142 EXPORT_SYMBOL(filp_close);
1145 * Careful here! We test whether the file pointer is NULL before
1146 * releasing the fd. This ensures that one clone task can't release
1147 * an fd while another clone is opening it.
1149 asmlinkage long sys_close(unsigned int fd)
1151 struct file * filp;
1152 struct files_struct *files = current->files;
1153 struct fdtable *fdt;
1155 spin_lock(&files->file_lock);
1156 fdt = files_fdtable(files);
1157 if (fd >= fdt->max_fds)
1158 goto out_unlock;
1159 filp = fdt->fd[fd];
1160 if (!filp)
1161 goto out_unlock;
1162 rcu_assign_pointer(fdt->fd[fd], NULL);
1163 FD_CLR(fd, fdt->close_on_exec);
1164 __put_unused_fd(files, fd);
1165 spin_unlock(&files->file_lock);
1166 return filp_close(filp, files);
1168 out_unlock:
1169 spin_unlock(&files->file_lock);
1170 return -EBADF;
1173 EXPORT_SYMBOL(sys_close);
1176 * This routine simulates a hangup on the tty, to arrange that users
1177 * are given clean terminals at login time.
1179 asmlinkage long sys_vhangup(void)
1181 if (capable(CAP_SYS_TTY_CONFIG)) {
1182 tty_vhangup(current->signal->tty);
1183 return 0;
1185 return -EPERM;
1189 * Called when an inode is about to be open.
1190 * We use this to disallow opening large files on 32bit systems if
1191 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1192 * on this flag in sys_open.
1194 int generic_file_open(struct inode * inode, struct file * filp)
1196 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1197 return -EFBIG;
1198 return 0;
1201 EXPORT_SYMBOL(generic_file_open);
1204 * This is used by subsystems that don't want seekable
1205 * file descriptors
1207 int nonseekable_open(struct inode *inode, struct file *filp)
1209 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1210 return 0;
1213 EXPORT_SYMBOL(nonseekable_open);