diff --git a/fs/namei.c b/fs/namei.c
index 5bc6f3d1dc8ac97fbe52ba59e4b01ffb7f92cb31..cf362dc9d1fde0db1983fb963a2ae02610c0c5ad 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2964,8 +2964,9 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
 	struct path path;
 	int error;
 
-	if (S_ISDIR(mode))
-		return -EPERM;
+	error = may_mknod(mode);
+	if (error)
+		return error;
 
 	dentry = user_path_create(dfd, filename, &path, 0);
 	if (IS_ERR(dentry))
@@ -2973,9 +2974,6 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
 
 	if (!IS_POSIXACL(path.dentry->d_inode))
 		mode &= ~current_umask();
-	error = may_mknod(mode);
-	if (error)
-		goto out_dput;
 	error = mnt_want_write(path.mnt);
 	if (error)
 		goto out_dput;