~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/open.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <linux/fs_struct.h>
31
31
#include <linux/ima.h>
32
32
#include <linux/dnotify.h>
33
 
#include <trace/fs.h>
34
33
 
35
34
#include "internal.h"
36
35
 
37
 
DEFINE_TRACE(fs_open);
38
 
DEFINE_TRACE(fs_close);
39
 
 
40
36
#define CREATE_TRACE_POINTS
41
37
#include <trace/events/fs.h>
42
38
 
581
577
{
582
578
        struct path path;
583
579
        int error = -EINVAL;
584
 
        int follow;
 
580
        int lookup_flags;
585
581
 
586
 
        if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
 
582
        if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
587
583
                goto out;
588
584
 
589
 
        follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
590
 
        error = user_path_at(dfd, filename, follow, &path);
 
585
        lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
 
586
        if (flag & AT_EMPTY_PATH)
 
587
                lookup_flags |= LOOKUP_EMPTY;
 
588
        error = user_path_at(dfd, filename, lookup_flags, &path);
591
589
        if (error)
592
590
                goto out;
593
591
        error = mnt_want_write(path.mnt);
677
675
                                        int (*open)(struct inode *, struct file *),
678
676
                                        const struct cred *cred)
679
677
{
 
678
        static const struct file_operations empty_fops = {};
680
679
        struct inode *inode;
681
680
        int error;
682
681
 
683
682
        f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
684
683
                                FMODE_PREAD | FMODE_PWRITE;
 
684
 
 
685
        if (unlikely(f->f_flags & O_PATH))
 
686
                f->f_mode = FMODE_PATH;
 
687
 
685
688
        inode = dentry->d_inode;
686
689
        if (f->f_mode & FMODE_WRITE) {
687
690
                error = __get_file_write_access(inode, mnt);
695
698
        f->f_path.dentry = dentry;
696
699
        f->f_path.mnt = mnt;
697
700
        f->f_pos = 0;
 
701
        file_sb_list_add(f, inode->i_sb);
 
702
 
 
703
        if (unlikely(f->f_mode & FMODE_PATH)) {
 
704
                f->f_op = &empty_fops;
 
705
                return f;
 
706
        }
 
707
 
698
708
        f->f_op = fops_get(inode->i_fop);
699
 
        file_sb_list_add(f, inode->i_sb);
700
709
 
701
710
        error = security_dentry_open(f, cred);
702
711
        if (error)
709
718
                if (error)
710
719
                        goto cleanup_all;
711
720
        }
712
 
        ima_counts_get(f);
 
721
        if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
 
722
                i_readcount_inc(inode);
713
723
 
714
724
        f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
715
725
 
829
839
 
830
840
        validate_creds(cred);
831
841
 
832
 
        /*
833
 
         * We must always pass in a valid mount pointer.   Historically
834
 
         * callers got away with not passing it, but we must enforce this at
835
 
         * the earliest possible point now to avoid strange problems deep in the
836
 
         * filesystem stack.
837
 
         */
838
 
        if (!mnt) {
839
 
                printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
840
 
                dump_stack();
841
 
                return ERR_PTR(-EINVAL);
842
 
        }
 
842
        /* We must always pass in a valid mount pointer. */
 
843
        BUG_ON(!mnt);
843
844
 
844
845
        error = -ENFILE;
845
846
        f = get_empty_filp();
898
899
 
899
900
EXPORT_SYMBOL(fd_install);
900
901
 
 
902
static inline int build_open_flags(int flags, int mode, struct open_flags *op)
 
903
{
 
904
        int lookup_flags = 0;
 
905
        int acc_mode;
 
906
 
 
907
        if (!(flags & O_CREAT))
 
908
                mode = 0;
 
909
        op->mode = mode;
 
910
 
 
911
        /* Must never be set by userspace */
 
912
        flags &= ~FMODE_NONOTIFY;
 
913
 
 
914
        /*
 
915
         * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
 
916
         * check for O_DSYNC if the need any syncing at all we enforce it's
 
917
         * always set instead of having to deal with possibly weird behaviour
 
918
         * for malicious applications setting only __O_SYNC.
 
919
         */
 
920
        if (flags & __O_SYNC)
 
921
                flags |= O_DSYNC;
 
922
 
 
923
        /*
 
924
         * If we have O_PATH in the open flag. Then we
 
925
         * cannot have anything other than the below set of flags
 
926
         */
 
927
        if (flags & O_PATH) {
 
928
                flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
 
929
                acc_mode = 0;
 
930
        } else {
 
931
                acc_mode = MAY_OPEN | ACC_MODE(flags);
 
932
        }
 
933
 
 
934
        op->open_flag = flags;
 
935
 
 
936
        /* O_TRUNC implies we need access checks for write permissions */
 
937
        if (flags & O_TRUNC)
 
938
                acc_mode |= MAY_WRITE;
 
939
 
 
940
        /* Allow the LSM permission hook to distinguish append
 
941
           access from general write access. */
 
942
        if (flags & O_APPEND)
 
943
                acc_mode |= MAY_APPEND;
 
944
 
 
945
        op->acc_mode = acc_mode;
 
946
 
 
947
        op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
 
948
 
 
949
        if (flags & O_CREAT) {
 
950
                op->intent |= LOOKUP_CREATE;
 
951
                if (flags & O_EXCL)
 
952
                        op->intent |= LOOKUP_EXCL;
 
953
        }
 
954
 
 
955
        if (flags & O_DIRECTORY)
 
956
                lookup_flags |= LOOKUP_DIRECTORY;
 
957
        if (!(flags & O_NOFOLLOW))
 
958
                lookup_flags |= LOOKUP_FOLLOW;
 
959
        return lookup_flags;
 
960
}
 
961
 
 
962
/**
 
963
 * filp_open - open file and return file pointer
 
964
 *
 
965
 * @filename:   path to open
 
966
 * @flags:      open flags as per the open(2) second argument
 
967
 * @mode:       mode for the new file if O_CREAT is set, else ignored
 
968
 *
 
969
 * This is the helper to open a file from kernelspace if you really
 
970
 * have to.  But in generally you should not do this, so please move
 
971
 * along, nothing to see here..
 
972
 */
 
973
struct file *filp_open(const char *filename, int flags, int mode)
 
974
{
 
975
        struct open_flags op;
 
976
        int lookup = build_open_flags(flags, mode, &op);
 
977
        return do_filp_open(AT_FDCWD, filename, &op, lookup);
 
978
}
 
979
EXPORT_SYMBOL(filp_open);
 
980
 
 
981
struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
 
982
                            const char *filename, int flags)
 
983
{
 
984
        struct open_flags op;
 
985
        int lookup = build_open_flags(flags, 0, &op);
 
986
        if (flags & O_CREAT)
 
987
                return ERR_PTR(-EINVAL);
 
988
        if (!filename && (flags & O_DIRECTORY))
 
989
                if (!dentry->d_inode->i_op->lookup)
 
990
                        return ERR_PTR(-ENOTDIR);
 
991
        return do_file_open_root(dentry, mnt, filename, &op, lookup);
 
992
}
 
993
EXPORT_SYMBOL(file_open_root);
 
994
 
901
995
long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
902
996
{
 
997
        struct open_flags op;
 
998
        int lookup = build_open_flags(flags, mode, &op);
903
999
        char *tmp = getname(filename);
904
1000
        int fd = PTR_ERR(tmp);
905
1001
 
906
1002
        if (!IS_ERR(tmp)) {
907
1003
                fd = get_unused_fd_flags(flags);
908
1004
                if (fd >= 0) {
909
 
                        struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
 
1005
                        struct file *f = do_filp_open(dfd, tmp, &op, lookup);
910
1006
                        if (IS_ERR(f)) {
911
1007
                                put_unused_fd(fd);
912
1008
                                fd = PTR_ERR(f);
915
1011
                                fd_install(fd, f);
916
1012
                                trace_do_sys_open(tmp, flags, mode);
917
1013
                        }
918
 
                        trace_fs_open(fd, tmp);
919
1014
                }
920
1015
                putname(tmp);
921
1016
        }
978
1073
        if (filp->f_op && filp->f_op->flush)
979
1074
                retval = filp->f_op->flush(filp, id);
980
1075
 
981
 
        dnotify_flush(filp, id);
982
 
        locks_remove_posix(filp, id);
 
1076
        if (likely(!(filp->f_mode & FMODE_PATH))) {
 
1077
                dnotify_flush(filp, id);
 
1078
                locks_remove_posix(filp, id);
 
1079
        }
983
1080
        fput(filp);
984
1081
        return retval;
985
1082
}
1005
1102
        filp = fdt->fd[fd];
1006
1103
        if (!filp)
1007
1104
                goto out_unlock;
1008
 
        trace_fs_close(fd);
1009
1105
        rcu_assign_pointer(fdt->fd[fd], NULL);
1010
1106
        FD_CLR(fd, fdt->close_on_exec);
1011
1107
        __put_unused_fd(files, fd);