IB/mthca: Fix off-by-one in FMR handling on memfree
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / open.c
blobf697914649e3af9c40e037130301cefd0ae2a28e
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 long ret = do_sys_ftruncate(fd, length, 1);
334 /* avoid REGPARM breakage on x86: */
335 prevent_tail_call(ret);
336 return ret;
339 /* LFS versions of truncate are only needed on 32 bit machines */
340 #if BITS_PER_LONG == 32
341 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
343 return do_sys_truncate(path, length);
346 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
348 long ret = do_sys_ftruncate(fd, length, 0);
349 /* avoid REGPARM breakage on x86: */
350 prevent_tail_call(ret);
351 return ret;
353 #endif
355 #ifdef __ARCH_WANT_SYS_UTIME
358 * sys_utime() can be implemented in user-level using sys_utimes().
359 * Is this for backwards compatibility? If so, why not move it
360 * into the appropriate arch directory (for those architectures that
361 * need it).
364 /* If times==NULL, set access and modification to current time,
365 * must be owner or have write permission.
366 * Else, update from *times, must be owner or super user.
368 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
370 int error;
371 struct nameidata nd;
372 struct inode * inode;
373 struct iattr newattrs;
375 error = user_path_walk(filename, &nd);
376 if (error)
377 goto out;
378 inode = nd.dentry->d_inode;
380 error = -EROFS;
381 if (IS_RDONLY(inode))
382 goto dput_and_out;
384 /* Don't worry, the checks are done in inode_change_ok() */
385 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
386 if (times) {
387 error = -EPERM;
388 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
389 goto dput_and_out;
391 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
392 newattrs.ia_atime.tv_nsec = 0;
393 if (!error)
394 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
395 newattrs.ia_mtime.tv_nsec = 0;
396 if (error)
397 goto dput_and_out;
399 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
400 } else {
401 error = -EACCES;
402 if (IS_IMMUTABLE(inode))
403 goto dput_and_out;
405 if (current->fsuid != inode->i_uid &&
406 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
407 goto dput_and_out;
409 mutex_lock(&inode->i_mutex);
410 error = notify_change(nd.dentry, &newattrs);
411 mutex_unlock(&inode->i_mutex);
412 dput_and_out:
413 path_release(&nd);
414 out:
415 return error;
418 #endif
420 /* If times==NULL, set access and modification to current time,
421 * must be owner or have write permission.
422 * Else, update from *times, must be owner or super user.
424 long do_utimes(int dfd, char __user *filename, struct timeval *times)
426 int error;
427 struct nameidata nd;
428 struct inode * inode;
429 struct iattr newattrs;
431 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
433 if (error)
434 goto out;
435 inode = nd.dentry->d_inode;
437 error = -EROFS;
438 if (IS_RDONLY(inode))
439 goto dput_and_out;
441 /* Don't worry, the checks are done in inode_change_ok() */
442 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
443 if (times) {
444 error = -EPERM;
445 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
446 goto dput_and_out;
448 newattrs.ia_atime.tv_sec = times[0].tv_sec;
449 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
450 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
451 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
452 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
453 } else {
454 error = -EACCES;
455 if (IS_IMMUTABLE(inode))
456 goto dput_and_out;
458 if (current->fsuid != inode->i_uid &&
459 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
460 goto dput_and_out;
462 mutex_lock(&inode->i_mutex);
463 error = notify_change(nd.dentry, &newattrs);
464 mutex_unlock(&inode->i_mutex);
465 dput_and_out:
466 path_release(&nd);
467 out:
468 return error;
471 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
473 struct timeval times[2];
475 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
476 return -EFAULT;
477 return do_utimes(dfd, filename, utimes ? times : NULL);
480 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
482 return sys_futimesat(AT_FDCWD, filename, utimes);
487 * access() needs to use the real uid/gid, not the effective uid/gid.
488 * We do this by temporarily clearing all FS-related capabilities and
489 * switching the fsuid/fsgid around to the real ones.
491 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
493 struct nameidata nd;
494 int old_fsuid, old_fsgid;
495 kernel_cap_t old_cap;
496 int res;
498 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
499 return -EINVAL;
501 old_fsuid = current->fsuid;
502 old_fsgid = current->fsgid;
503 old_cap = current->cap_effective;
505 current->fsuid = current->uid;
506 current->fsgid = current->gid;
509 * Clear the capabilities if we switch to a non-root user
511 * FIXME: There is a race here against sys_capset. The
512 * capabilities can change yet we will restore the old
513 * value below. We should hold task_capabilities_lock,
514 * but we cannot because user_path_walk can sleep.
516 if (current->uid)
517 cap_clear(current->cap_effective);
518 else
519 current->cap_effective = current->cap_permitted;
521 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
522 if (!res) {
523 res = vfs_permission(&nd, mode);
524 /* SuS v2 requires we report a read only fs too */
525 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
526 && !special_file(nd.dentry->d_inode->i_mode))
527 res = -EROFS;
528 path_release(&nd);
531 current->fsuid = old_fsuid;
532 current->fsgid = old_fsgid;
533 current->cap_effective = old_cap;
535 return res;
538 asmlinkage long sys_access(const char __user *filename, int mode)
540 return sys_faccessat(AT_FDCWD, filename, mode);
543 asmlinkage long sys_chdir(const char __user * filename)
545 struct nameidata nd;
546 int error;
548 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
549 if (error)
550 goto out;
552 error = vfs_permission(&nd, MAY_EXEC);
553 if (error)
554 goto dput_and_out;
556 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
558 dput_and_out:
559 path_release(&nd);
560 out:
561 return error;
564 asmlinkage long sys_fchdir(unsigned int fd)
566 struct file *file;
567 struct dentry *dentry;
568 struct inode *inode;
569 struct vfsmount *mnt;
570 int error;
572 error = -EBADF;
573 file = fget(fd);
574 if (!file)
575 goto out;
577 dentry = file->f_dentry;
578 mnt = file->f_vfsmnt;
579 inode = dentry->d_inode;
581 error = -ENOTDIR;
582 if (!S_ISDIR(inode->i_mode))
583 goto out_putf;
585 error = file_permission(file, MAY_EXEC);
586 if (!error)
587 set_fs_pwd(current->fs, mnt, dentry);
588 out_putf:
589 fput(file);
590 out:
591 return error;
594 asmlinkage long sys_chroot(const char __user * filename)
596 struct nameidata nd;
597 int error;
599 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
600 if (error)
601 goto out;
603 error = vfs_permission(&nd, MAY_EXEC);
604 if (error)
605 goto dput_and_out;
607 error = -EPERM;
608 if (!capable(CAP_SYS_CHROOT))
609 goto dput_and_out;
611 set_fs_root(current->fs, nd.mnt, nd.dentry);
612 set_fs_altroot();
613 error = 0;
614 dput_and_out:
615 path_release(&nd);
616 out:
617 return error;
620 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
622 struct inode * inode;
623 struct dentry * dentry;
624 struct file * file;
625 int err = -EBADF;
626 struct iattr newattrs;
628 file = fget(fd);
629 if (!file)
630 goto out;
632 dentry = file->f_dentry;
633 inode = dentry->d_inode;
635 err = -EROFS;
636 if (IS_RDONLY(inode))
637 goto out_putf;
638 err = -EPERM;
639 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
640 goto out_putf;
641 mutex_lock(&inode->i_mutex);
642 if (mode == (mode_t) -1)
643 mode = inode->i_mode;
644 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
645 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
646 err = notify_change(dentry, &newattrs);
647 mutex_unlock(&inode->i_mutex);
649 out_putf:
650 fput(file);
651 out:
652 return err;
655 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
656 mode_t mode)
658 struct nameidata nd;
659 struct inode * inode;
660 int error;
661 struct iattr newattrs;
663 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
664 if (error)
665 goto out;
666 inode = nd.dentry->d_inode;
668 error = -EROFS;
669 if (IS_RDONLY(inode))
670 goto dput_and_out;
672 error = -EPERM;
673 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
674 goto dput_and_out;
676 mutex_lock(&inode->i_mutex);
677 if (mode == (mode_t) -1)
678 mode = inode->i_mode;
679 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
680 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
681 error = notify_change(nd.dentry, &newattrs);
682 mutex_unlock(&inode->i_mutex);
684 dput_and_out:
685 path_release(&nd);
686 out:
687 return error;
690 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
692 return sys_fchmodat(AT_FDCWD, filename, mode);
695 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
697 struct inode * inode;
698 int error;
699 struct iattr newattrs;
701 error = -ENOENT;
702 if (!(inode = dentry->d_inode)) {
703 printk(KERN_ERR "chown_common: NULL inode\n");
704 goto out;
706 error = -EROFS;
707 if (IS_RDONLY(inode))
708 goto out;
709 error = -EPERM;
710 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
711 goto out;
712 newattrs.ia_valid = ATTR_CTIME;
713 if (user != (uid_t) -1) {
714 newattrs.ia_valid |= ATTR_UID;
715 newattrs.ia_uid = user;
717 if (group != (gid_t) -1) {
718 newattrs.ia_valid |= ATTR_GID;
719 newattrs.ia_gid = group;
721 if (!S_ISDIR(inode->i_mode))
722 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
723 mutex_lock(&inode->i_mutex);
724 error = notify_change(dentry, &newattrs);
725 mutex_unlock(&inode->i_mutex);
726 out:
727 return error;
730 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
732 struct nameidata nd;
733 int error;
735 error = user_path_walk(filename, &nd);
736 if (!error) {
737 error = chown_common(nd.dentry, user, group);
738 path_release(&nd);
740 return error;
743 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
744 gid_t group, int flag)
746 struct nameidata nd;
747 int error = -EINVAL;
748 int follow;
750 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
751 goto out;
753 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
754 error = __user_walk_fd(dfd, filename, follow, &nd);
755 if (!error) {
756 error = chown_common(nd.dentry, user, group);
757 path_release(&nd);
759 out:
760 return error;
763 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
765 struct nameidata nd;
766 int error;
768 error = user_path_walk_link(filename, &nd);
769 if (!error) {
770 error = chown_common(nd.dentry, user, group);
771 path_release(&nd);
773 return error;
777 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
779 struct file * file;
780 int error = -EBADF;
782 file = fget(fd);
783 if (file) {
784 error = chown_common(file->f_dentry, user, group);
785 fput(file);
787 return error;
790 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
791 int flags, struct file *f,
792 int (*open)(struct inode *, struct file *))
794 struct inode *inode;
795 int error;
797 f->f_flags = flags;
798 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
799 FMODE_PREAD | FMODE_PWRITE;
800 inode = dentry->d_inode;
801 if (f->f_mode & FMODE_WRITE) {
802 error = get_write_access(inode);
803 if (error)
804 goto cleanup_file;
807 f->f_mapping = inode->i_mapping;
808 f->f_dentry = dentry;
809 f->f_vfsmnt = mnt;
810 f->f_pos = 0;
811 f->f_op = fops_get(inode->i_fop);
812 file_move(f, &inode->i_sb->s_files);
814 if (!open && f->f_op)
815 open = f->f_op->open;
816 if (open) {
817 error = open(inode, f);
818 if (error)
819 goto cleanup_all;
822 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
824 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
826 /* NB: we're sure to have correct a_ops only after f_op->open */
827 if (f->f_flags & O_DIRECT) {
828 if (!f->f_mapping->a_ops ||
829 ((!f->f_mapping->a_ops->direct_IO) &&
830 (!f->f_mapping->a_ops->get_xip_page))) {
831 fput(f);
832 f = ERR_PTR(-EINVAL);
836 return f;
838 cleanup_all:
839 fops_put(f->f_op);
840 if (f->f_mode & FMODE_WRITE)
841 put_write_access(inode);
842 file_kill(f);
843 f->f_dentry = NULL;
844 f->f_vfsmnt = NULL;
845 cleanup_file:
846 put_filp(f);
847 dput(dentry);
848 mntput(mnt);
849 return ERR_PTR(error);
853 * Note that while the flag value (low two bits) for sys_open means:
854 * 00 - read-only
855 * 01 - write-only
856 * 10 - read-write
857 * 11 - special
858 * it is changed into
859 * 00 - no permissions needed
860 * 01 - read-permission
861 * 10 - write-permission
862 * 11 - read-write
863 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
864 * used by symlinks.
866 static struct file *do_filp_open(int dfd, const char *filename, int flags,
867 int mode)
869 int namei_flags, error;
870 struct nameidata nd;
872 namei_flags = flags;
873 if ((namei_flags+1) & O_ACCMODE)
874 namei_flags++;
876 error = open_namei(dfd, filename, namei_flags, mode, &nd);
877 if (!error)
878 return nameidata_to_filp(&nd, flags);
880 return ERR_PTR(error);
883 struct file *filp_open(const char *filename, int flags, int mode)
885 return do_filp_open(AT_FDCWD, filename, flags, mode);
887 EXPORT_SYMBOL(filp_open);
890 * lookup_instantiate_filp - instantiates the open intent filp
891 * @nd: pointer to nameidata
892 * @dentry: pointer to dentry
893 * @open: open callback
895 * Helper for filesystems that want to use lookup open intents and pass back
896 * a fully instantiated struct file to the caller.
897 * This function is meant to be called from within a filesystem's
898 * lookup method.
899 * Note that in case of error, nd->intent.open.file is destroyed, but the
900 * path information remains valid.
901 * If the open callback is set to NULL, then the standard f_op->open()
902 * filesystem callback is substituted.
904 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
905 int (*open)(struct inode *, struct file *))
907 if (IS_ERR(nd->intent.open.file))
908 goto out;
909 if (IS_ERR(dentry))
910 goto out_err;
911 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
912 nd->intent.open.flags - 1,
913 nd->intent.open.file,
914 open);
915 out:
916 return nd->intent.open.file;
917 out_err:
918 release_open_intent(nd);
919 nd->intent.open.file = (struct file *)dentry;
920 goto out;
922 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
925 * nameidata_to_filp - convert a nameidata to an open filp.
926 * @nd: pointer to nameidata
927 * @flags: open flags
929 * Note that this function destroys the original nameidata
931 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
933 struct file *filp;
935 /* Pick up the filp from the open intent */
936 filp = nd->intent.open.file;
937 /* Has the filesystem initialised the file for us? */
938 if (filp->f_dentry == NULL)
939 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
940 else
941 path_release(nd);
942 return filp;
946 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
947 * error.
949 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
951 int error;
952 struct file *f;
954 error = -ENFILE;
955 f = get_empty_filp();
956 if (f == NULL) {
957 dput(dentry);
958 mntput(mnt);
959 return ERR_PTR(error);
962 return __dentry_open(dentry, mnt, flags, f, NULL);
964 EXPORT_SYMBOL(dentry_open);
967 * Find an empty file descriptor entry, and mark it busy.
969 int get_unused_fd(void)
971 struct files_struct * files = current->files;
972 int fd, error;
973 struct fdtable *fdt;
975 error = -EMFILE;
976 spin_lock(&files->file_lock);
978 repeat:
979 fdt = files_fdtable(files);
980 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
981 fdt->max_fdset,
982 fdt->next_fd);
985 * N.B. For clone tasks sharing a files structure, this test
986 * will limit the total number of files that can be opened.
988 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
989 goto out;
991 /* Do we need to expand the fd array or fd set? */
992 error = expand_files(files, fd);
993 if (error < 0)
994 goto out;
996 if (error) {
998 * If we needed to expand the fs array we
999 * might have blocked - try again.
1001 error = -EMFILE;
1002 goto repeat;
1005 FD_SET(fd, fdt->open_fds);
1006 FD_CLR(fd, fdt->close_on_exec);
1007 fdt->next_fd = fd + 1;
1008 #if 1
1009 /* Sanity check */
1010 if (fdt->fd[fd] != NULL) {
1011 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1012 fdt->fd[fd] = NULL;
1014 #endif
1015 error = fd;
1017 out:
1018 spin_unlock(&files->file_lock);
1019 return error;
1022 EXPORT_SYMBOL(get_unused_fd);
1024 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1026 struct fdtable *fdt = files_fdtable(files);
1027 __FD_CLR(fd, fdt->open_fds);
1028 if (fd < fdt->next_fd)
1029 fdt->next_fd = fd;
1032 void fastcall put_unused_fd(unsigned int fd)
1034 struct files_struct *files = current->files;
1035 spin_lock(&files->file_lock);
1036 __put_unused_fd(files, fd);
1037 spin_unlock(&files->file_lock);
1040 EXPORT_SYMBOL(put_unused_fd);
1043 * Install a file pointer in the fd array.
1045 * The VFS is full of places where we drop the files lock between
1046 * setting the open_fds bitmap and installing the file in the file
1047 * array. At any such point, we are vulnerable to a dup2() race
1048 * installing a file in the array before us. We need to detect this and
1049 * fput() the struct file we are about to overwrite in this case.
1051 * It should never happen - if we allow dup2() do it, _really_ bad things
1052 * will follow.
1055 void fastcall fd_install(unsigned int fd, struct file * file)
1057 struct files_struct *files = current->files;
1058 struct fdtable *fdt;
1059 spin_lock(&files->file_lock);
1060 fdt = files_fdtable(files);
1061 BUG_ON(fdt->fd[fd] != NULL);
1062 rcu_assign_pointer(fdt->fd[fd], file);
1063 spin_unlock(&files->file_lock);
1066 EXPORT_SYMBOL(fd_install);
1068 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1070 char *tmp = getname(filename);
1071 int fd = PTR_ERR(tmp);
1073 if (!IS_ERR(tmp)) {
1074 fd = get_unused_fd();
1075 if (fd >= 0) {
1076 struct file *f = do_filp_open(dfd, tmp, flags, mode);
1077 if (IS_ERR(f)) {
1078 put_unused_fd(fd);
1079 fd = PTR_ERR(f);
1080 } else {
1081 fsnotify_open(f->f_dentry);
1082 fd_install(fd, f);
1085 putname(tmp);
1087 return fd;
1090 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1092 long ret;
1094 if (force_o_largefile())
1095 flags |= O_LARGEFILE;
1097 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1098 /* avoid REGPARM breakage on x86: */
1099 prevent_tail_call(ret);
1100 return ret;
1102 EXPORT_SYMBOL_GPL(sys_open);
1104 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1105 int mode)
1107 long ret;
1109 if (force_o_largefile())
1110 flags |= O_LARGEFILE;
1112 ret = do_sys_open(dfd, filename, flags, mode);
1113 /* avoid REGPARM breakage on x86: */
1114 prevent_tail_call(ret);
1115 return ret;
1117 EXPORT_SYMBOL_GPL(sys_openat);
1119 #ifndef __alpha__
1122 * For backward compatibility? Maybe this should be moved
1123 * into arch/i386 instead?
1125 asmlinkage long sys_creat(const char __user * pathname, int mode)
1127 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1130 #endif
1133 * "id" is the POSIX thread ID. We use the
1134 * files pointer for this..
1136 int filp_close(struct file *filp, fl_owner_t id)
1138 int retval = 0;
1140 if (!file_count(filp)) {
1141 printk(KERN_ERR "VFS: Close: file count is 0\n");
1142 return 0;
1145 if (filp->f_op && filp->f_op->flush)
1146 retval = filp->f_op->flush(filp);
1148 dnotify_flush(filp, id);
1149 locks_remove_posix(filp, id);
1150 fput(filp);
1151 return retval;
1154 EXPORT_SYMBOL(filp_close);
1157 * Careful here! We test whether the file pointer is NULL before
1158 * releasing the fd. This ensures that one clone task can't release
1159 * an fd while another clone is opening it.
1161 asmlinkage long sys_close(unsigned int fd)
1163 struct file * filp;
1164 struct files_struct *files = current->files;
1165 struct fdtable *fdt;
1167 spin_lock(&files->file_lock);
1168 fdt = files_fdtable(files);
1169 if (fd >= fdt->max_fds)
1170 goto out_unlock;
1171 filp = fdt->fd[fd];
1172 if (!filp)
1173 goto out_unlock;
1174 rcu_assign_pointer(fdt->fd[fd], NULL);
1175 FD_CLR(fd, fdt->close_on_exec);
1176 __put_unused_fd(files, fd);
1177 spin_unlock(&files->file_lock);
1178 return filp_close(filp, files);
1180 out_unlock:
1181 spin_unlock(&files->file_lock);
1182 return -EBADF;
1185 EXPORT_SYMBOL(sys_close);
1188 * This routine simulates a hangup on the tty, to arrange that users
1189 * are given clean terminals at login time.
1191 asmlinkage long sys_vhangup(void)
1193 if (capable(CAP_SYS_TTY_CONFIG)) {
1194 tty_vhangup(current->signal->tty);
1195 return 0;
1197 return -EPERM;
1201 * Called when an inode is about to be open.
1202 * We use this to disallow opening large files on 32bit systems if
1203 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1204 * on this flag in sys_open.
1206 int generic_file_open(struct inode * inode, struct file * filp)
1208 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1209 return -EFBIG;
1210 return 0;
1213 EXPORT_SYMBOL(generic_file_open);
1216 * This is used by subsystems that don't want seekable
1217 * file descriptors
1219 int nonseekable_open(struct inode *inode, struct file *filp)
1221 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1222 return 0;
1225 EXPORT_SYMBOL(nonseekable_open);