582
578
struct path path;
583
579
int error = -EINVAL;
586
if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
582
if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
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);
593
591
error = mnt_want_write(path.mnt);
899
900
EXPORT_SYMBOL(fd_install);
902
static inline int build_open_flags(int flags, int mode, struct open_flags *op)
904
int lookup_flags = 0;
907
if (!(flags & O_CREAT))
911
/* Must never be set by userspace */
912
flags &= ~FMODE_NONOTIFY;
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.
920
if (flags & __O_SYNC)
924
* If we have O_PATH in the open flag. Then we
925
* cannot have anything other than the below set of flags
927
if (flags & O_PATH) {
928
flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
931
acc_mode = MAY_OPEN | ACC_MODE(flags);
934
op->open_flag = flags;
936
/* O_TRUNC implies we need access checks for write permissions */
938
acc_mode |= MAY_WRITE;
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;
945
op->acc_mode = acc_mode;
947
op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
949
if (flags & O_CREAT) {
950
op->intent |= LOOKUP_CREATE;
952
op->intent |= LOOKUP_EXCL;
955
if (flags & O_DIRECTORY)
956
lookup_flags |= LOOKUP_DIRECTORY;
957
if (!(flags & O_NOFOLLOW))
958
lookup_flags |= LOOKUP_FOLLOW;
963
* filp_open - open file and return file pointer
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
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..
973
struct file *filp_open(const char *filename, int flags, int mode)
975
struct open_flags op;
976
int lookup = build_open_flags(flags, mode, &op);
977
return do_filp_open(AT_FDCWD, filename, &op, lookup);
979
EXPORT_SYMBOL(filp_open);
981
struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
982
const char *filename, int flags)
984
struct open_flags op;
985
int lookup = build_open_flags(flags, 0, &op);
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);
993
EXPORT_SYMBOL(file_open_root);
901
995
long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
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);
906
1002
if (!IS_ERR(tmp)) {
907
1003
fd = get_unused_fd_flags(flags);
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);