LCOV - code coverage report
Current view: top level - fs - namei.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 2044 2319 88.1 %
Date: 2023-07-31 20:08:34 Functions: 153 179 85.5 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  *  linux/fs/namei.c
       4             :  *
       5             :  *  Copyright (C) 1991, 1992  Linus Torvalds
       6             :  */
       7             : 
       8             : /*
       9             :  * Some corrections by tytso.
      10             :  */
      11             : 
      12             : /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
      13             :  * lookup logic.
      14             :  */
      15             : /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
      16             :  */
      17             : 
      18             : #include <linux/init.h>
      19             : #include <linux/export.h>
      20             : #include <linux/kernel.h>
      21             : #include <linux/slab.h>
      22             : #include <linux/fs.h>
      23             : #include <linux/filelock.h>
      24             : #include <linux/namei.h>
      25             : #include <linux/pagemap.h>
      26             : #include <linux/sched/mm.h>
      27             : #include <linux/fsnotify.h>
      28             : #include <linux/personality.h>
      29             : #include <linux/security.h>
      30             : #include <linux/ima.h>
      31             : #include <linux/syscalls.h>
      32             : #include <linux/mount.h>
      33             : #include <linux/audit.h>
      34             : #include <linux/capability.h>
      35             : #include <linux/file.h>
      36             : #include <linux/fcntl.h>
      37             : #include <linux/device_cgroup.h>
      38             : #include <linux/fs_struct.h>
      39             : #include <linux/posix_acl.h>
      40             : #include <linux/hash.h>
      41             : #include <linux/bitops.h>
      42             : #include <linux/init_task.h>
      43             : #include <linux/uaccess.h>
      44             : 
      45             : #include "internal.h"
      46             : #include "mount.h"
      47             : 
      48             : /* [Feb-1997 T. Schoebel-Theuer]
      49             :  * Fundamental changes in the pathname lookup mechanisms (namei)
      50             :  * were necessary because of omirr.  The reason is that omirr needs
      51             :  * to know the _real_ pathname, not the user-supplied one, in case
      52             :  * of symlinks (and also when transname replacements occur).
      53             :  *
      54             :  * The new code replaces the old recursive symlink resolution with
      55             :  * an iterative one (in case of non-nested symlink chains).  It does
      56             :  * this with calls to <fs>_follow_link().
      57             :  * As a side effect, dir_namei(), _namei() and follow_link() are now 
      58             :  * replaced with a single function lookup_dentry() that can handle all 
      59             :  * the special cases of the former code.
      60             :  *
      61             :  * With the new dcache, the pathname is stored at each inode, at least as
      62             :  * long as the refcount of the inode is positive.  As a side effect, the
      63             :  * size of the dcache depends on the inode cache and thus is dynamic.
      64             :  *
      65             :  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
      66             :  * resolution to correspond with current state of the code.
      67             :  *
      68             :  * Note that the symlink resolution is not *completely* iterative.
      69             :  * There is still a significant amount of tail- and mid- recursion in
      70             :  * the algorithm.  Also, note that <fs>_readlink() is not used in
      71             :  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
      72             :  * may return different results than <fs>_follow_link().  Many virtual
      73             :  * filesystems (including /proc) exhibit this behavior.
      74             :  */
      75             : 
      76             : /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
      77             :  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
      78             :  * and the name already exists in form of a symlink, try to create the new
      79             :  * name indicated by the symlink. The old code always complained that the
      80             :  * name already exists, due to not following the symlink even if its target
      81             :  * is nonexistent.  The new semantics affects also mknod() and link() when
      82             :  * the name is a symlink pointing to a non-existent name.
      83             :  *
      84             :  * I don't know which semantics is the right one, since I have no access
      85             :  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
      86             :  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
      87             :  * "old" one. Personally, I think the new semantics is much more logical.
      88             :  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
      89             :  * file does succeed in both HP-UX and SunOs, but not in Solaris
      90             :  * and in the old Linux semantics.
      91             :  */
      92             : 
      93             : /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
      94             :  * semantics.  See the comments in "open_namei" and "do_link" below.
      95             :  *
      96             :  * [10-Sep-98 Alan Modra] Another symlink change.
      97             :  */
      98             : 
      99             : /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
     100             :  *      inside the path - always follow.
     101             :  *      in the last component in creation/removal/renaming - never follow.
     102             :  *      if LOOKUP_FOLLOW passed - follow.
     103             :  *      if the pathname has trailing slashes - follow.
     104             :  *      otherwise - don't follow.
     105             :  * (applied in that order).
     106             :  *
     107             :  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
     108             :  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
     109             :  * During the 2.4 we need to fix the userland stuff depending on it -
     110             :  * hopefully we will be able to get rid of that wart in 2.5. So far only
     111             :  * XEmacs seems to be relying on it...
     112             :  */
     113             : /*
     114             :  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
     115             :  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
     116             :  * any extra contention...
     117             :  */
     118             : 
     119             : /* In order to reduce some races, while at the same time doing additional
     120             :  * checking and hopefully speeding things up, we copy filenames to the
     121             :  * kernel data space before using them..
     122             :  *
     123             :  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
     124             :  * PATH_MAX includes the nul terminator --RR.
     125             :  */
     126             : 
     127             : #define EMBEDDED_NAME_MAX       (PATH_MAX - offsetof(struct filename, iname))
     128             : 
     129             : struct filename *
     130 11819263056 : getname_flags(const char __user *filename, int flags, int *empty)
     131             : {
     132 11819263056 :         struct filename *result;
     133 11819263056 :         char *kname;
     134 11819263056 :         int len;
     135             : 
     136 11819263056 :         result = audit_reusename(filename);
     137 11819263056 :         if (result)
     138             :                 return result;
     139             : 
     140 11819263056 :         result = __getname();
     141 11823611396 :         if (unlikely(!result))
     142             :                 return ERR_PTR(-ENOMEM);
     143             : 
     144             :         /*
     145             :          * First, try to embed the struct filename inside the names_cache
     146             :          * allocation
     147             :          */
     148 11823611396 :         kname = (char *)result->iname;
     149 11823611396 :         result->name = kname;
     150             : 
     151 11823611396 :         len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
     152 11828922401 :         if (unlikely(len < 0)) {
     153         219 :                 __putname(result);
     154         219 :                 return ERR_PTR(len);
     155             :         }
     156             : 
     157             :         /*
     158             :          * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
     159             :          * separate struct filename so we can dedicate the entire
     160             :          * names_cache allocation for the pathname, and re-do the copy from
     161             :          * userland.
     162             :          */
     163 11828922182 :         if (unlikely(len == EMBEDDED_NAME_MAX)) {
     164     6350509 :                 const size_t size = offsetof(struct filename, iname[1]);
     165     6350509 :                 kname = (char *)result;
     166             : 
     167             :                 /*
     168             :                  * size is chosen that way we to guarantee that
     169             :                  * result->iname[0] is within the same object and that
     170             :                  * kname can't be equal to result->iname, no matter what.
     171             :                  */
     172     6350509 :                 result = kzalloc(size, GFP_KERNEL);
     173     6350489 :                 if (unlikely(!result)) {
     174           0 :                         __putname(kname);
     175           0 :                         return ERR_PTR(-ENOMEM);
     176             :                 }
     177     6350489 :                 result->name = kname;
     178     6350489 :                 len = strncpy_from_user(kname, filename, PATH_MAX);
     179     6350418 :                 if (unlikely(len < 0)) {
     180           0 :                         __putname(kname);
     181           0 :                         kfree(result);
     182           0 :                         return ERR_PTR(len);
     183             :                 }
     184     6350418 :                 if (unlikely(len == PATH_MAX)) {
     185         104 :                         __putname(kname);
     186         104 :                         kfree(result);
     187         104 :                         return ERR_PTR(-ENAMETOOLONG);
     188             :                 }
     189             :         }
     190             : 
     191 11828921987 :         result->refcnt = 1;
     192             :         /* The empty path is special. */
     193 11828921987 :         if (unlikely(!len)) {
     194  1189385500 :                 if (empty)
     195           0 :                         *empty = 1;
     196  1189385500 :                 if (!(flags & LOOKUP_EMPTY)) {
     197    95089123 :                         putname(result);
     198    95089123 :                         return ERR_PTR(-ENOENT);
     199             :                 }
     200             :         }
     201             : 
     202 11733832864 :         result->uptr = filename;
     203 11733832864 :         result->aname = NULL;
     204 11733832864 :         audit_getname(result);
     205 11733832864 :         return result;
     206             : }
     207             : 
     208             : struct filename *
     209         104 : getname_uflags(const char __user *filename, int uflags)
     210             : {
     211     3551921 :         int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
     212             : 
     213     3551921 :         return getname_flags(filename, flags, NULL);
     214             : }
     215             : 
     216             : struct filename *
     217  1195362128 : getname(const char __user * filename)
     218             : {
     219  1905693799 :         return getname_flags(filename, 0, NULL);
     220             : }
     221             : 
     222             : struct filename *
     223    54236685 : getname_kernel(const char * filename)
     224             : {
     225    54236685 :         struct filename *result;
     226    54236685 :         int len = strlen(filename) + 1;
     227             : 
     228    54236685 :         result = __getname();
     229    54235133 :         if (unlikely(!result))
     230             :                 return ERR_PTR(-ENOMEM);
     231             : 
     232    54235133 :         if (len <= EMBEDDED_NAME_MAX) {
     233    54235133 :                 result->name = (char *)result->iname;
     234           0 :         } else if (len <= PATH_MAX) {
     235           0 :                 const size_t size = offsetof(struct filename, iname[1]);
     236           0 :                 struct filename *tmp;
     237             : 
     238           0 :                 tmp = kmalloc(size, GFP_KERNEL);
     239           0 :                 if (unlikely(!tmp)) {
     240           0 :                         __putname(result);
     241           0 :                         return ERR_PTR(-ENOMEM);
     242             :                 }
     243           0 :                 tmp->name = (char *)result;
     244           0 :                 result = tmp;
     245             :         } else {
     246           0 :                 __putname(result);
     247           0 :                 return ERR_PTR(-ENAMETOOLONG);
     248             :         }
     249   108470266 :         memcpy((char *)result->name, filename, len);
     250    54235133 :         result->uptr = NULL;
     251    54235133 :         result->aname = NULL;
     252    54235133 :         result->refcnt = 1;
     253    54235133 :         audit_getname(result);
     254             : 
     255    54235133 :         return result;
     256             : }
     257             : EXPORT_SYMBOL(getname_kernel);
     258             : 
     259 11961834748 : void putname(struct filename *name)
     260             : {
     261 11961834748 :         if (IS_ERR(name))
     262             :                 return;
     263             : 
     264 11866813442 :         BUG_ON(name->refcnt <= 0);
     265             : 
     266 11866813442 :         if (--name->refcnt > 0)
     267             :                 return;
     268             : 
     269 11866813442 :         if (name->name != name->iname) {
     270     6350375 :                 __putname(name->name);
     271     6350393 :                 kfree(name);
     272             :         } else
     273 11860463067 :                 __putname(name);
     274             : }
     275             : EXPORT_SYMBOL(putname);
     276             : 
     277             : /**
     278             :  * check_acl - perform ACL permission checking
     279             :  * @idmap:      idmap of the mount the inode was found from
     280             :  * @inode:      inode to check permissions on
     281             :  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
     282             :  *
     283             :  * This function performs the ACL permission checking. Since this function
     284             :  * retrieve POSIX acls it needs to know whether it is called from a blocking or
     285             :  * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
     286             :  *
     287             :  * If the inode has been found through an idmapped mount the idmap of
     288             :  * the vfsmount must be passed through @idmap. This function will then take
     289             :  * care to map the inode according to @idmap before checking permissions.
     290             :  * On non-idmapped mounts or if permission checking is to be performed on the
     291             :  * raw inode simply passs @nop_mnt_idmap.
     292             :  */
     293 10720113811 : static int check_acl(struct mnt_idmap *idmap,
     294             :                      struct inode *inode, int mask)
     295             : {
     296             : #ifdef CONFIG_FS_POSIX_ACL
     297 10720113811 :         struct posix_acl *acl;
     298             : 
     299 10720113811 :         if (mask & MAY_NOT_BLOCK) {
     300 10413262421 :                 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
     301 10413524443 :                 if (!acl)
     302             :                         return -EAGAIN;
     303             :                 /* no ->get_inode_acl() calls in RCU mode... */
     304      688504 :                 if (is_uncached_acl(acl))
     305             :                         return -ECHILD;
     306         403 :                 return posix_acl_permission(idmap, inode, acl, mask);
     307             :         }
     308             : 
     309   306851390 :         acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
     310   306768471 :         if (IS_ERR(acl))
     311         191 :                 return PTR_ERR(acl);
     312   306768280 :         if (acl) {
     313        1013 :                 int error = posix_acl_permission(idmap, inode, acl, mask);
     314        1013 :                 posix_acl_release(acl);
     315        1013 :                 return error;
     316             :         }
     317             : #endif
     318             : 
     319             :         return -EAGAIN;
     320             : }
     321             : 
     322             : /**
     323             :  * acl_permission_check - perform basic UNIX permission checking
     324             :  * @idmap:      idmap of the mount the inode was found from
     325             :  * @inode:      inode to check permissions on
     326             :  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
     327             :  *
     328             :  * This function performs the basic UNIX permission checking. Since this
     329             :  * function may retrieve POSIX acls it needs to know whether it is called from a
     330             :  * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
     331             :  *
     332             :  * If the inode has been found through an idmapped mount the idmap of
     333             :  * the vfsmount must be passed through @idmap. This function will then take
     334             :  * care to map the inode according to @idmap before checking permissions.
     335             :  * On non-idmapped mounts or if permission checking is to be performed on the
     336             :  * raw inode simply passs @nop_mnt_idmap.
     337             :  */
     338 70283428742 : static int acl_permission_check(struct mnt_idmap *idmap,
     339             :                                 struct inode *inode, int mask)
     340             : {
     341 70283428742 :         unsigned int mode = inode->i_mode;
     342 70283428742 :         vfsuid_t vfsuid;
     343             : 
     344             :         /* Are we the owner? If so, ACL's don't matter */
     345 70283428742 :         vfsuid = i_uid_into_vfsuid(idmap, inode);
     346 >14118*10^7 :         if (likely(vfsuid_eq_kuid(vfsuid, current_fsuid()))) {
     347 59853030280 :                 mask &= 7;
     348 59853030280 :                 mode >>= 6;
     349 59853030280 :                 return (mask & ~mode) ? -EACCES : 0;
     350             :         }
     351             : 
     352             :         /* Do we have ACL's? */
     353 10738041359 :         if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
     354 10719950068 :                 int error = check_acl(idmap, inode, mask);
     355 10719112472 :                 if (error != -EAGAIN)
     356             :                         return error;
     357             :         }
     358             : 
     359             :         /* Only RWX matters for group/other mode bits */
     360 10736933842 :         mask &= 7;
     361             : 
     362             :         /*
     363             :          * Are the group permissions different from
     364             :          * the other permissions in the bits we care
     365             :          * about? Need to check group ownership if so.
     366             :          */
     367 10736933842 :         if (mask & (mode ^ (mode >> 3))) {
     368    10589814 :                 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
     369    10594381 :                 if (vfsgid_in_group_p(vfsgid))
     370         130 :                         mode >>= 3;
     371             :         }
     372             : 
     373             :         /* Bits in 'mode' clear that we require? */
     374 10736932664 :         return (mask & ~mode) ? -EACCES : 0;
     375             : }
     376             : 
     377             : /**
     378             :  * generic_permission -  check for access rights on a Posix-like filesystem
     379             :  * @idmap:      idmap of the mount the inode was found from
     380             :  * @inode:      inode to check access rights for
     381             :  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
     382             :  *              %MAY_NOT_BLOCK ...)
     383             :  *
     384             :  * Used to check for read/write/execute permissions on a file.
     385             :  * We use "fsuid" for this, letting us set arbitrary permissions
     386             :  * for filesystem access without changing the "normal" uids which
     387             :  * are used for other things.
     388             :  *
     389             :  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
     390             :  * request cannot be satisfied (eg. requires blocking or too much complexity).
     391             :  * It would then be called again in ref-walk mode.
     392             :  *
     393             :  * If the inode has been found through an idmapped mount the idmap of
     394             :  * the vfsmount must be passed through @idmap. This function will then take
     395             :  * care to map the inode according to @idmap before checking permissions.
     396             :  * On non-idmapped mounts or if permission checking is to be performed on the
     397             :  * raw inode simply passs @nop_mnt_idmap.
     398             :  */
     399 70305933222 : int generic_permission(struct mnt_idmap *idmap, struct inode *inode,
     400             :                        int mask)
     401             : {
     402 70305933222 :         int ret;
     403             : 
     404             :         /*
     405             :          * Do the basic permission checks.
     406             :          */
     407 70305933222 :         ret = acl_permission_check(idmap, inode, mask);
     408 70037415766 :         if (ret != -EACCES)
     409             :                 return ret;
     410             : 
     411    14089826 :         if (S_ISDIR(inode->i_mode)) {
     412             :                 /* DACs are overridable for directories */
     413    11739651 :                 if (!(mask & MAY_WRITE))
     414      453520 :                         if (capable_wrt_inode_uidgid(idmap, inode,
     415             :                                                      CAP_DAC_READ_SEARCH))
     416             :                                 return 0;
     417    11302119 :                 if (capable_wrt_inode_uidgid(idmap, inode,
     418             :                                              CAP_DAC_OVERRIDE))
     419             :                         return 0;
     420       16834 :                 return -EACCES;
     421             :         }
     422             : 
     423             :         /*
     424             :          * Searching includes executable on directories, else just read.
     425             :          */
     426     2350175 :         mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
     427     2350175 :         if (mask == MAY_READ)
     428        6860 :                 if (capable_wrt_inode_uidgid(idmap, inode,
     429             :                                              CAP_DAC_READ_SEARCH))
     430             :                         return 0;
     431             :         /*
     432             :          * Read/write DACs are always overridable.
     433             :          * Executable DACs are overridable when there is
     434             :          * at least one exec bit set.
     435             :          */
     436     2348516 :         if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
     437     2315590 :                 if (capable_wrt_inode_uidgid(idmap, inode,
     438             :                                              CAP_DAC_OVERRIDE))
     439     2251799 :                         return 0;
     440             : 
     441             :         return -EACCES;
     442             : }
     443             : EXPORT_SYMBOL(generic_permission);
     444             : 
     445             : /**
     446             :  * do_inode_permission - UNIX permission checking
     447             :  * @idmap:      idmap of the mount the inode was found from
     448             :  * @inode:      inode to check permissions on
     449             :  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
     450             :  *
     451             :  * We _really_ want to just do "generic_permission()" without
     452             :  * even looking at the inode->i_op values. So we keep a cache
     453             :  * flag in inode->i_opflags, that says "this has not special
     454             :  * permission function, use the fast case".
     455             :  */
     456 80040219340 : static inline int do_inode_permission(struct mnt_idmap *idmap,
     457             :                                       struct inode *inode, int mask)
     458             : {
     459 80040219340 :         if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
     460 10972641858 :                 if (likely(inode->i_op->permission))
     461 10883347643 :                         return inode->i_op->permission(idmap, inode, mask);
     462             : 
     463             :                 /* This gets set once for the inode lifetime */
     464    89294215 :                 spin_lock(&inode->i_lock);
     465    90026665 :                 inode->i_opflags |= IOP_FASTPERM;
     466    90026665 :                 spin_unlock(&inode->i_lock);
     467             :         }
     468 69158693048 :         return generic_permission(idmap, inode, mask);
     469             : }
     470             : 
     471             : /**
     472             :  * sb_permission - Check superblock-level permissions
     473             :  * @sb: Superblock of inode to check permission on
     474             :  * @inode: Inode to check permission on
     475             :  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
     476             :  *
     477             :  * Separate out file-system wide checks from inode-specific permission checks.
     478             :  */
     479 80085403170 : static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
     480             : {
     481 80085403170 :         if (unlikely(mask & MAY_WRITE)) {
     482  2060672960 :                 umode_t mode = inode->i_mode;
     483             : 
     484             :                 /* Nobody gets write access to a read-only fs. */
     485  2060672960 :                 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
     486          13 :                         return -EROFS;
     487             :         }
     488             :         return 0;
     489             : }
     490             : 
     491             : /**
     492             :  * inode_permission - Check for access rights to a given inode
     493             :  * @idmap:      idmap of the mount the inode was found from
     494             :  * @inode:      Inode to check permission on
     495             :  * @mask:       Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
     496             :  *
     497             :  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
     498             :  * this, letting us set arbitrary permissions for filesystem access without
     499             :  * changing the "normal" UIDs which are used for other things.
     500             :  *
     501             :  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
     502             :  */
     503 80062246113 : int inode_permission(struct mnt_idmap *idmap,
     504             :                      struct inode *inode, int mask)
     505             : {
     506 80062246113 :         int retval;
     507             : 
     508 80062246113 :         retval = sb_permission(inode->i_sb, inode, mask);
     509 80062246113 :         if (retval)
     510             :                 return retval;
     511             : 
     512 80062246100 :         if (unlikely(mask & MAY_WRITE)) {
     513             :                 /*
     514             :                  * Nobody gets write access to an immutable file.
     515             :                  */
     516  2060668402 :                 if (IS_IMMUTABLE(inode))
     517             :                         return -EPERM;
     518             : 
     519             :                 /*
     520             :                  * Updating mtime will likely cause i_uid and i_gid to be
     521             :                  * written back improperly if their true value is unknown
     522             :                  * to the vfs.
     523             :                  */
     524  2060667655 :                 if (HAS_UNMAPPED_ID(idmap, inode))
     525             :                         return -EACCES;
     526             :         }
     527             : 
     528 80061406330 :         retval = do_inode_permission(idmap, inode, mask);
     529 80111593994 :         if (retval)
     530   256939732 :                 return retval;
     531             : 
     532             :         retval = devcgroup_inode_permission(inode, mask);
     533             :         if (retval)
     534             :                 return retval;
     535             : 
     536             :         return security_inode_permission(inode, mask);
     537             : }
     538             : EXPORT_SYMBOL(inode_permission);
     539             : 
     540             : /**
     541             :  * path_get - get a reference to a path
     542             :  * @path: path to get the reference to
     543             :  *
     544             :  * Given a path increment the reference count to the dentry and the vfsmount.
     545             :  */
     546  3653212162 : void path_get(const struct path *path)
     547             : {
     548  3653212162 :         mntget(path->mnt);
     549  3654140436 :         dget(path->dentry);
     550  3657251589 : }
     551             : EXPORT_SYMBOL(path_get);
     552             : 
     553             : /**
     554             :  * path_put - put a reference to a path
     555             :  * @path: path to put the reference to
     556             :  *
     557             :  * Given a path decrement the reference count to the dentry and the vfsmount.
     558             :  */
     559 21936812148 : void path_put(const struct path *path)
     560             : {
     561 21936812148 :         dput(path->dentry);
     562 21943643403 :         mntput(path->mnt);
     563 21929917338 : }
     564             : EXPORT_SYMBOL(path_put);
     565             : 
     566             : #define EMBEDDED_LEVELS 2
     567             : struct nameidata {
     568             :         struct path     path;
     569             :         struct qstr     last;
     570             :         struct path     root;
     571             :         struct inode    *inode; /* path.dentry.d_inode */
     572             :         unsigned int    flags, state;
     573             :         unsigned        seq, next_seq, m_seq, r_seq;
     574             :         int             last_type;
     575             :         unsigned        depth;
     576             :         int             total_link_count;
     577             :         struct saved {
     578             :                 struct path link;
     579             :                 struct delayed_call done;
     580             :                 const char *name;
     581             :                 unsigned seq;
     582             :         } *stack, internal[EMBEDDED_LEVELS];
     583             :         struct filename *name;
     584             :         struct nameidata *saved;
     585             :         unsigned        root_seq;
     586             :         int             dfd;
     587             :         vfsuid_t        dir_vfsuid;
     588             :         umode_t         dir_mode;
     589             : } __randomize_layout;
     590             : 
     591             : #define ND_ROOT_PRESET 1
     592             : #define ND_ROOT_GRABBED 2
     593             : #define ND_JUMPED 4
     594             : 
     595 11127923565 : static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name)
     596             : {
     597 11127923565 :         struct nameidata *old = current->nameidata;
     598 11127923565 :         p->stack = p->internal;
     599 11127923565 :         p->depth = 0;
     600 11127923565 :         p->dfd = dfd;
     601 11127923565 :         p->name = name;
     602 11127923565 :         p->path.mnt = NULL;
     603 11127923565 :         p->path.dentry = NULL;
     604 11127923565 :         p->total_link_count = old ? old->total_link_count : 0;
     605 11127923565 :         p->saved = old;
     606 11127923565 :         current->nameidata = p;
     607 11127923565 : }
     608             : 
     609  9899525894 : static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name,
     610             :                           const struct path *root)
     611             : {
     612  9899525894 :         __set_nameidata(p, dfd, name);
     613 11128555218 :         p->state = 0;
     614  9897698834 :         if (unlikely(root)) {
     615       44887 :                 p->state = ND_ROOT_PRESET;
     616       44887 :                 p->root = *root;
     617             :         }
     618  9897698834 : }
     619             : 
     620 11124579650 : static void restore_nameidata(void)
     621             : {
     622 11124579650 :         struct nameidata *now = current->nameidata, *old = now->saved;
     623             : 
     624 11124579650 :         current->nameidata = old;
     625 11124579650 :         if (old)
     626           0 :                 old->total_link_count = now->total_link_count;
     627 11124579650 :         if (now->stack != now->internal)
     628           0 :                 kfree(now->stack);
     629 11124579650 : }
     630             : 
     631           0 : static bool nd_alloc_stack(struct nameidata *nd)
     632             : {
     633           0 :         struct saved *p;
     634             : 
     635           0 :         p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
     636           0 :                          nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
     637           0 :         if (unlikely(!p))
     638             :                 return false;
     639           0 :         memcpy(p, nd->internal, sizeof(nd->internal));
     640           0 :         nd->stack = p;
     641           0 :         return true;
     642             : }
     643             : 
     644             : /**
     645             :  * path_connected - Verify that a dentry is below mnt.mnt_root
     646             :  *
     647             :  * Rename can sometimes move a file or directory outside of a bind
     648             :  * mount, path_connected allows those cases to be detected.
     649             :  */
     650   552852890 : static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
     651             : {
     652   552852890 :         struct super_block *sb = mnt->mnt_sb;
     653             : 
     654             :         /* Bind mounts can have disconnected paths */
     655   552852890 :         if (mnt->mnt_root == sb->s_root)
     656             :                 return true;
     657             : 
     658    12906519 :         return is_subdir(dentry, mnt->mnt_root);
     659             : }
     660             : 
     661 11127261849 : static void drop_links(struct nameidata *nd)
     662             : {
     663 11127261849 :         int i = nd->depth;
     664 11128342734 :         while (i--) {
     665      271195 :                 struct saved *last = nd->stack + i;
     666      271195 :                 do_delayed_call(&last->done);
     667     1080885 :                 clear_delayed_call(&last->done);
     668             :         }
     669 11128071539 : }
     670             : 
     671             : static void leave_rcu(struct nameidata *nd)
     672             : {
     673 10048374420 :         nd->flags &= ~LOOKUP_RCU;
     674 10048374420 :         nd->seq = nd->next_seq = 0;
     675 10048374420 :         rcu_read_unlock();
     676   327478112 : }
     677             : 
     678 11128900645 : static void terminate_walk(struct nameidata *nd)
     679             : {
     680 11128900645 :         drop_links(nd);
     681 11126643293 :         if (!(nd->flags & LOOKUP_RCU)) {
     682 10799192465 :                 int i;
     683 10799192465 :                 path_put(&nd->path);
     684 21595203022 :                 for (i = 0; i < nd->depth; i++)
     685           0 :                         path_put(&nd->stack[i].link);
     686 10797605989 :                 if (nd->state & ND_ROOT_GRABBED) {
     687  1263898366 :                         path_put(&nd->root);
     688  1264556379 :                         nd->state &= ~ND_ROOT_GRABBED;
     689             :                 }
     690             :         } else {
     691   327450828 :                 leave_rcu(nd);
     692             :         }
     693 11125742114 :         nd->depth = 0;
     694 11125742114 :         nd->path.mnt = NULL;
     695 11125742114 :         nd->path.dentry = NULL;
     696 11125742114 : }
     697             : 
     698             : /* path_put is needed afterwards regardless of success or failure */
     699 10956336140 : static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
     700             : {
     701 10956336140 :         int res = __legitimize_mnt(path->mnt, mseq);
     702 10973440136 :         if (unlikely(res)) {
     703       69312 :                 if (res > 0)
     704       66134 :                         path->mnt = NULL;
     705       69312 :                 path->dentry = NULL;
     706       69312 :                 return false;
     707             :         }
     708 10973370824 :         if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
     709           7 :                 path->dentry = NULL;
     710           7 :                 return false;
     711             :         }
     712 10973594488 :         return !read_seqcount_retry(&path->dentry->d_seq, seq);
     713             : }
     714             : 
     715             : static inline bool legitimize_path(struct nameidata *nd,
     716             :                             struct path *path, unsigned seq)
     717             : {
     718 10957611581 :         return __legitimize_path(path, seq, nd->m_seq);
     719             : }
     720             : 
     721  9704924030 : static bool legitimize_links(struct nameidata *nd)
     722             : {
     723  9704924030 :         int i;
     724  9704924030 :         if (unlikely(nd->flags & LOOKUP_CACHED)) {
     725           0 :                 drop_links(nd);
     726           0 :                 nd->depth = 0;
     727           0 :                 return false;
     728             :         }
     729  9731700976 :         for (i = 0; i < nd->depth; i++) {
     730    26581858 :                 struct saved *last = nd->stack + i;
     731    26581858 :                 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
     732         649 :                         drop_links(nd);
     733         649 :                         nd->depth = i + 1;
     734         649 :                         return false;
     735             :                 }
     736             :         }
     737             :         return true;
     738             : }
     739             : 
     740  9721930885 : static bool legitimize_root(struct nameidata *nd)
     741             : {
     742             :         /* Nothing to do if nd->root is zero or is managed by the VFS user. */
     743  9721930885 :         if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET))
     744             :                 return true;
     745  1256257591 :         nd->state |= ND_ROOT_GRABBED;
     746  1256257591 :         return legitimize_path(nd, &nd->root, nd->root_seq);
     747             : }
     748             : 
     749             : /*
     750             :  * Path walking has 2 modes, rcu-walk and ref-walk (see
     751             :  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
     752             :  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
     753             :  * normal reference counts on dentries and vfsmounts to transition to ref-walk
     754             :  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
     755             :  * got stuck, so ref-walk may continue from there. If this is not successful
     756             :  * (eg. a seqcount has changed), then failure is returned and it's up to caller
     757             :  * to restart the path walk from the beginning in ref-walk mode.
     758             :  */
     759             : 
     760             : /**
     761             :  * try_to_unlazy - try to switch to ref-walk mode.
     762             :  * @nd: nameidata pathwalk data
     763             :  * Returns: true on success, false on failure
     764             :  *
     765             :  * try_to_unlazy attempts to legitimize the current nd->path and nd->root
     766             :  * for ref-walk mode.
     767             :  * Must be called from rcu-walk context.
     768             :  * Nothing should touch nameidata between try_to_unlazy() failure and
     769             :  * terminate_walk().
     770             :  */
     771  9676665705 : static bool try_to_unlazy(struct nameidata *nd)
     772             : {
     773  9676665705 :         struct dentry *parent = nd->path.dentry;
     774             : 
     775  9676665705 :         BUG_ON(!(nd->flags & LOOKUP_RCU));
     776             : 
     777  9676665705 :         if (unlikely(!legitimize_links(nd)))
     778         620 :                 goto out1;
     779  9674772132 :         if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
     780       66138 :                 goto out;
     781  9691520696 :         if (unlikely(!legitimize_root(nd)))
     782        2621 :                 goto out;
     783  9690679497 :         leave_rcu(nd);
     784  9689776498 :         BUG_ON(nd->inode != parent->d_inode);
     785             :         return true;
     786             : 
     787             : out1:
     788         620 :         nd->path.mnt = NULL;
     789         620 :         nd->path.dentry = NULL;
     790       69379 : out:
     791       69379 :         leave_rcu(nd);
     792       69379 :         return false;
     793             : }
     794             : 
     795             : /**
     796             :  * try_to_unlazy_next - try to switch to ref-walk mode.
     797             :  * @nd: nameidata pathwalk data
     798             :  * @dentry: next dentry to step into
     799             :  * Returns: true on success, false on failure
     800             :  *
     801             :  * Similar to try_to_unlazy(), but here we have the next dentry already
     802             :  * picked by rcu-walk and want to legitimize that in addition to the current
     803             :  * nd->path and nd->root for ref-walk mode.  Must be called from rcu-walk context.
     804             :  * Nothing should touch nameidata between try_to_unlazy_next() failure and
     805             :  * terminate_walk().
     806             :  */
     807    30129289 : static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry)
     808             : {
     809    30129289 :         int res;
     810    30129289 :         BUG_ON(!(nd->flags & LOOKUP_RCU));
     811             : 
     812    30129289 :         if (unlikely(!legitimize_links(nd)))
     813          29 :                 goto out2;
     814    30131470 :         res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
     815    30164316 :         if (unlikely(res)) {
     816       72651 :                 if (res > 0)
     817       72522 :                         goto out2;
     818         129 :                 goto out1;
     819             :         }
     820    30091665 :         if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
     821           0 :                 goto out1;
     822             : 
     823             :         /*
     824             :          * We need to move both the parent and the dentry from the RCU domain
     825             :          * to be properly refcounted. And the sequence number in the dentry
     826             :          * validates *both* dentry counters, since we checked the sequence
     827             :          * number of the parent after we got the child sequence number. So we
     828             :          * know the parent must still be valid if the child sequence number is
     829             :          */
     830    30092938 :         if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
     831           0 :                 goto out;
     832    30098997 :         if (read_seqcount_retry(&dentry->d_seq, nd->next_seq))
     833         181 :                 goto out_dput;
     834             :         /*
     835             :          * Sequence counts matched. Now make sure that the root is
     836             :          * still valid and get it if required.
     837             :          */
     838    30098824 :         if (unlikely(!legitimize_root(nd)))
     839         225 :                 goto out_dput;
     840    30101630 :         leave_rcu(nd);
     841    30101630 :         return true;
     842             : 
     843       72551 : out2:
     844       72551 :         nd->path.mnt = NULL;
     845       72680 : out1:
     846       72680 :         nd->path.dentry = NULL;
     847       72680 : out:
     848       72680 :         leave_rcu(nd);
     849       72680 :         return false;
     850         406 : out_dput:
     851         406 :         leave_rcu(nd);
     852         406 :         dput(dentry);
     853         406 :         return false;
     854             : }
     855             : 
     856 72067620088 : static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
     857             : {
     858 72067620088 :         if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
     859  9966212810 :                 return dentry->d_op->d_revalidate(dentry, flags);
     860             :         else
     861             :                 return 1;
     862             : }
     863             : 
     864             : /**
     865             :  * complete_walk - successful completion of path walk
     866             :  * @nd:  pointer nameidata
     867             :  *
     868             :  * If we had been in RCU mode, drop out of it and legitimize nd->path.
     869             :  * Revalidate the final result, unless we'd already done that during
     870             :  * the path walk or the filesystem doesn't ask for it.  Return 0 on
     871             :  * success, -error on failure.  In case of failure caller does not
     872             :  * need to drop nd->path.
     873             :  */
     874  9714891910 : static int complete_walk(struct nameidata *nd)
     875             : {
     876  9714891910 :         struct dentry *dentry = nd->path.dentry;
     877  9714891910 :         int status;
     878             : 
     879  9714891910 :         if (nd->flags & LOOKUP_RCU) {
     880             :                 /*
     881             :                  * We don't want to zero nd->root for scoped-lookups or
     882             :                  * externally-managed nd->root.
     883             :                  */
     884  8293662197 :                 if (!(nd->state & ND_ROOT_PRESET))
     885  8292450389 :                         if (!(nd->flags & LOOKUP_IS_SCOPED))
     886  8292059688 :                                 nd->root.mnt = NULL;
     887  8293662197 :                 nd->flags &= ~LOOKUP_CACHED;
     888  8293662197 :                 if (!try_to_unlazy(nd))
     889             :                         return -ECHILD;
     890             :         }
     891             : 
     892  9719350816 :         if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
     893             :                 /*
     894             :                  * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
     895             :                  * ever step outside the root during lookup" and should already
     896             :                  * be guaranteed by the rest of namei, we want to avoid a namei
     897             :                  * BUG resulting in userspace being given a path that was not
     898             :                  * scoped within the root at some point during the lookup.
     899             :                  *
     900             :                  * So, do a final sanity-check to make sure that in the
     901             :                  * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
     902             :                  * we won't silently return an fd completely outside of the
     903             :                  * requested root to userspace.
     904             :                  *
     905             :                  * Userspace could move the path outside the root after this
     906             :                  * check, but as discussed elsewhere this is not a concern (the
     907             :                  * resolved file was inside the root at some point).
     908             :                  */
     909           0 :                 if (!path_is_under(&nd->path, &nd->root))
     910             :                         return -EXDEV;
     911             :         }
     912             : 
     913  9719350816 :         if (likely(!(nd->state & ND_JUMPED)))
     914             :                 return 0;
     915             : 
     916  4714219465 :         if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
     917             :                 return 0;
     918             : 
     919   611878047 :         status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
     920   611757564 :         if (status > 0)
     921             :                 return 0;
     922             : 
     923           4 :         if (!status)
     924           4 :                 status = -ESTALE;
     925             : 
     926             :         return status;
     927             : }
     928             : 
     929  5034102016 : static int set_root(struct nameidata *nd)
     930             : {
     931  5034102016 :         struct fs_struct *fs = current->fs;
     932             : 
     933             :         /*
     934             :          * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
     935             :          * still have to ensure it doesn't happen because it will cause a breakout
     936             :          * from the dirfd.
     937             :          */
     938  5034102016 :         if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
     939             :                 return -ENOTRECOVERABLE;
     940             : 
     941  5034102016 :         if (nd->flags & LOOKUP_RCU) {
     942             :                 unsigned seq;
     943             : 
     944             :                 do {
     945  5026121831 :                         seq = read_seqcount_begin(&fs->seq);
     946  5025375841 :                         nd->root = fs->root;
     947  5025375841 :                         nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
     948  5025916726 :                 } while (read_seqcount_retry(&fs->seq, seq));
     949             :         } else {
     950     7980186 :                 get_fs_root(fs, &nd->root);
     951     8007736 :                 nd->state |= ND_ROOT_GRABBED;
     952             :         }
     953             :         return 0;
     954             : }
     955             : 
     956  4581773306 : static int nd_jump_root(struct nameidata *nd)
     957             : {
     958  4581773306 :         if (unlikely(nd->flags & LOOKUP_BENEATH))
     959             :                 return -EXDEV;
     960  4581773306 :         if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
     961             :                 /* Absolute path arguments to path_init() are allowed. */
     962           0 :                 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
     963             :                         return -EXDEV;
     964             :         }
     965  4581773306 :         if (!nd->root.mnt) {
     966  4514090564 :                 int error = set_root(nd);
     967  4511975163 :                 if (error)
     968             :                         return error;
     969             :         }
     970  4579657905 :         if (nd->flags & LOOKUP_RCU) {
     971  4565830595 :                 struct dentry *d;
     972  4565830595 :                 nd->path = nd->root;
     973  4565830595 :                 d = nd->path.dentry;
     974  4565830595 :                 nd->inode = d->d_inode;
     975  4565830595 :                 nd->seq = nd->root_seq;
     976  4565830595 :                 if (read_seqcount_retry(&d->d_seq, nd->seq))
     977             :                         return -ECHILD;
     978             :         } else {
     979    13827310 :                 path_put(&nd->path);
     980    13827426 :                 nd->path = nd->root;
     981    13827426 :                 path_get(&nd->path);
     982    13828376 :                 nd->inode = nd->path.dentry->d_inode;
     983             :         }
     984  4580479086 :         nd->state |= ND_JUMPED;
     985  4580479086 :         return 0;
     986             : }
     987             : 
     988             : /*
     989             :  * Helper to directly jump to a known parsed path from ->get_link,
     990             :  * caller must have taken a reference to path beforehand.
     991             :  */
     992     2380970 : int nd_jump_link(const struct path *path)
     993             : {
     994     2380970 :         int error = -ELOOP;
     995     2380970 :         struct nameidata *nd = current->nameidata;
     996             : 
     997     2380970 :         if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
     998           0 :                 goto err;
     999             : 
    1000     2380970 :         error = -EXDEV;
    1001     2380970 :         if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
    1002           0 :                 if (nd->path.mnt != path->mnt)
    1003           0 :                         goto err;
    1004             :         }
    1005             :         /* Not currently safe for scoped-lookups. */
    1006     2380970 :         if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
    1007           0 :                 goto err;
    1008             : 
    1009     2380970 :         path_put(&nd->path);
    1010     2381348 :         nd->path = *path;
    1011     2381348 :         nd->inode = nd->path.dentry->d_inode;
    1012     2381348 :         nd->state |= ND_JUMPED;
    1013     2381348 :         return 0;
    1014             : 
    1015           0 : err:
    1016           0 :         path_put(path);
    1017           0 :         return error;
    1018             : }
    1019             : 
    1020   453189735 : static inline void put_link(struct nameidata *nd)
    1021             : {
    1022   453189735 :         struct saved *last = nd->stack + --nd->depth;
    1023   453189735 :         do_delayed_call(&last->done);
    1024   453261977 :         if (!(nd->flags & LOOKUP_RCU))
    1025    38523580 :                 path_put(&last->link);
    1026   453292000 : }
    1027             : 
    1028             : static int sysctl_protected_symlinks __read_mostly;
    1029             : static int sysctl_protected_hardlinks __read_mostly;
    1030             : static int sysctl_protected_fifos __read_mostly;
    1031             : static int sysctl_protected_regular __read_mostly;
    1032             : 
    1033             : #ifdef CONFIG_SYSCTL
    1034             : static struct ctl_table namei_sysctls[] = {
    1035             :         {
    1036             :                 .procname       = "protected_symlinks",
    1037             :                 .data           = &sysctl_protected_symlinks,
    1038             :                 .maxlen         = sizeof(int),
    1039             :                 .mode           = 0644,
    1040             :                 .proc_handler   = proc_dointvec_minmax,
    1041             :                 .extra1         = SYSCTL_ZERO,
    1042             :                 .extra2         = SYSCTL_ONE,
    1043             :         },
    1044             :         {
    1045             :                 .procname       = "protected_hardlinks",
    1046             :                 .data           = &sysctl_protected_hardlinks,
    1047             :                 .maxlen         = sizeof(int),
    1048             :                 .mode           = 0644,
    1049             :                 .proc_handler   = proc_dointvec_minmax,
    1050             :                 .extra1         = SYSCTL_ZERO,
    1051             :                 .extra2         = SYSCTL_ONE,
    1052             :         },
    1053             :         {
    1054             :                 .procname       = "protected_fifos",
    1055             :                 .data           = &sysctl_protected_fifos,
    1056             :                 .maxlen         = sizeof(int),
    1057             :                 .mode           = 0644,
    1058             :                 .proc_handler   = proc_dointvec_minmax,
    1059             :                 .extra1         = SYSCTL_ZERO,
    1060             :                 .extra2         = SYSCTL_TWO,
    1061             :         },
    1062             :         {
    1063             :                 .procname       = "protected_regular",
    1064             :                 .data           = &sysctl_protected_regular,
    1065             :                 .maxlen         = sizeof(int),
    1066             :                 .mode           = 0644,
    1067             :                 .proc_handler   = proc_dointvec_minmax,
    1068             :                 .extra1         = SYSCTL_ZERO,
    1069             :                 .extra2         = SYSCTL_TWO,
    1070             :         },
    1071             :         { }
    1072             : };
    1073             : 
    1074           0 : static int __init init_fs_namei_sysctls(void)
    1075             : {
    1076           0 :         register_sysctl_init("fs", namei_sysctls);
    1077           0 :         return 0;
    1078             : }
    1079             : fs_initcall(init_fs_namei_sysctls);
    1080             : 
    1081             : #endif /* CONFIG_SYSCTL */
    1082             : 
    1083             : /**
    1084             :  * may_follow_link - Check symlink following for unsafe situations
    1085             :  * @nd: nameidata pathwalk data
    1086             :  *
    1087             :  * In the case of the sysctl_protected_symlinks sysctl being enabled,
    1088             :  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
    1089             :  * in a sticky world-writable directory. This is to protect privileged
    1090             :  * processes from failing races against path names that may change out
    1091             :  * from under them by way of other users creating malicious symlinks.
    1092             :  * It will permit symlinks to be followed only when outside a sticky
    1093             :  * world-writable directory, or when the uid of the symlink and follower
    1094             :  * match, or when the directory owner matches the symlink's owner.
    1095             :  *
    1096             :  * Returns 0 if following the symlink is allowed, -ve on error.
    1097             :  */
    1098   158754876 : static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
    1099             : {
    1100   158754876 :         struct mnt_idmap *idmap;
    1101   158754876 :         vfsuid_t vfsuid;
    1102             : 
    1103   158754876 :         if (!sysctl_protected_symlinks)
    1104             :                 return 0;
    1105             : 
    1106   158750596 :         idmap = mnt_idmap(nd->path.mnt);
    1107   158752358 :         vfsuid = i_uid_into_vfsuid(idmap, inode);
    1108             :         /* Allowed if owner and follower match. */
    1109   317539132 :         if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
    1110             :                 return 0;
    1111             : 
    1112             :         /* Allowed if parent directory not sticky and world-writable. */
    1113      884497 :         if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
    1114             :                 return 0;
    1115             : 
    1116             :         /* Allowed if parent directory and link owner match. */
    1117         546 :         if (vfsuid_valid(nd->dir_vfsuid) && vfsuid_eq(nd->dir_vfsuid, vfsuid))
    1118             :                 return 0;
    1119             : 
    1120         182 :         if (nd->flags & LOOKUP_RCU)
    1121          91 :                 return -ECHILD;
    1122             : 
    1123             :         audit_inode(nd->name, nd->stack[0].link.dentry, 0);
    1124             :         audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
    1125             :         return -EACCES;
    1126             : }
    1127             : 
    1128             : /**
    1129             :  * safe_hardlink_source - Check for safe hardlink conditions
    1130             :  * @idmap: idmap of the mount the inode was found from
    1131             :  * @inode: the source inode to hardlink from
    1132             :  *
    1133             :  * Return false if at least one of the following conditions:
    1134             :  *    - inode is not a regular file
    1135             :  *    - inode is setuid
    1136             :  *    - inode is setgid and group-exec
    1137             :  *    - access failure for read and write
    1138             :  *
    1139             :  * Otherwise returns true.
    1140             :  */
    1141    14001839 : static bool safe_hardlink_source(struct mnt_idmap *idmap,
    1142             :                                  struct inode *inode)
    1143             : {
    1144    14001839 :         umode_t mode = inode->i_mode;
    1145             : 
    1146             :         /* Special files should not get pinned to the filesystem. */
    1147    14001839 :         if (!S_ISREG(mode))
    1148             :                 return false;
    1149             : 
    1150             :         /* Setuid files should not get pinned to the filesystem. */
    1151     8797281 :         if (mode & S_ISUID)
    1152             :                 return false;
    1153             : 
    1154             :         /* Executable setgid files should not get pinned to the filesystem. */
    1155     8797281 :         if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
    1156             :                 return false;
    1157             : 
    1158             :         /* Hardlinking to unreadable or unwritable sources is dangerous. */
    1159     8797255 :         if (inode_permission(idmap, inode, MAY_READ | MAY_WRITE))
    1160          92 :                 return false;
    1161             : 
    1162             :         return true;
    1163             : }
    1164             : 
    1165             : /**
    1166             :  * may_linkat - Check permissions for creating a hardlink
    1167             :  * @idmap: idmap of the mount the inode was found from
    1168             :  * @link:  the source to hardlink from
    1169             :  *
    1170             :  * Block hardlink when all of:
    1171             :  *  - sysctl_protected_hardlinks enabled
    1172             :  *  - fsuid does not match inode
    1173             :  *  - hardlink source is unsafe (see safe_hardlink_source() above)
    1174             :  *  - not CAP_FOWNER in a namespace with the inode owner uid mapped
    1175             :  *
    1176             :  * If the inode has been found through an idmapped mount the idmap of
    1177             :  * the vfsmount must be passed through @idmap. This function will then take
    1178             :  * care to map the inode according to @idmap before checking permissions.
    1179             :  * On non-idmapped mounts or if permission checking is to be performed on the
    1180             :  * raw inode simply pass @nop_mnt_idmap.
    1181             :  *
    1182             :  * Returns 0 if successful, -ve on error.
    1183             :  */
    1184    14001889 : int may_linkat(struct mnt_idmap *idmap, const struct path *link)
    1185             : {
    1186    14001889 :         struct inode *inode = link->dentry->d_inode;
    1187             : 
    1188             :         /* Inode writeback is not safe when the uid or gid are invalid. */
    1189    28004075 :         if (!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) ||
    1190             :             !vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))
    1191           0 :                 return -EOVERFLOW;
    1192             : 
    1193    14002186 :         if (!sysctl_protected_hardlinks)
    1194             :                 return 0;
    1195             : 
    1196             :         /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
    1197             :          * otherwise, it must be a safe source.
    1198             :          */
    1199    19207461 :         if (safe_hardlink_source(idmap, inode) ||
    1200     5205339 :             inode_owner_or_capable(idmap, inode))
    1201    13994456 :                 return 0;
    1202             : 
    1203             :         audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
    1204             :         return -EPERM;
    1205             : }
    1206             : 
    1207             : /**
    1208             :  * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
    1209             :  *                        should be allowed, or not, on files that already
    1210             :  *                        exist.
    1211             :  * @idmap: idmap of the mount the inode was found from
    1212             :  * @nd: nameidata pathwalk data
    1213             :  * @inode: the inode of the file to open
    1214             :  *
    1215             :  * Block an O_CREAT open of a FIFO (or a regular file) when:
    1216             :  *   - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
    1217             :  *   - the file already exists
    1218             :  *   - we are in a sticky directory
    1219             :  *   - we don't own the file
    1220             :  *   - the owner of the directory doesn't own the file
    1221             :  *   - the directory is world writable
    1222             :  * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
    1223             :  * the directory doesn't have to be world writable: being group writable will
    1224             :  * be enough.
    1225             :  *
    1226             :  * If the inode has been found through an idmapped mount the idmap of
    1227             :  * the vfsmount must be passed through @idmap. This function will then take
    1228             :  * care to map the inode according to @idmap before checking permissions.
    1229             :  * On non-idmapped mounts or if permission checking is to be performed on the
    1230             :  * raw inode simply pass @nop_mnt_idmap.
    1231             :  *
    1232             :  * Returns 0 if the open is allowed, -ve on error.
    1233             :  */
    1234    72225067 : static int may_create_in_sticky(struct mnt_idmap *idmap,
    1235             :                                 struct nameidata *nd, struct inode *const inode)
    1236             : {
    1237    72225067 :         umode_t dir_mode = nd->dir_mode;
    1238    72225067 :         vfsuid_t dir_vfsuid = nd->dir_vfsuid;
    1239             : 
    1240    72225067 :         if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
    1241    72225041 :             (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
    1242    72742500 :             likely(!(dir_mode & S_ISVTX)) ||
    1243         117 :             vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) ||
    1244         117 :             vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid()))
    1245    72224963 :                 return 0;
    1246             : 
    1247         104 :         if (likely(dir_mode & 0002) ||
    1248          52 :             (dir_mode & 0020 &&
    1249          52 :              ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
    1250          39 :               (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
    1251          78 :                 const char *operation = S_ISFIFO(inode->i_mode) ?
    1252             :                                         "sticky_create_fifo" :
    1253             :                                         "sticky_create_regular";
    1254          78 :                 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
    1255          78 :                 return -EACCES;
    1256             :         }
    1257             :         return 0;
    1258             : }
    1259             : 
    1260             : /*
    1261             :  * follow_up - Find the mountpoint of path's vfsmount
    1262             :  *
    1263             :  * Given a path, find the mountpoint of its source file system.
    1264             :  * Replace @path with the path of the mountpoint in the parent mount.
    1265             :  * Up is towards /.
    1266             :  *
    1267             :  * Return 1 if we went up a level and 0 if we were already at the
    1268             :  * root.
    1269             :  */
    1270           0 : int follow_up(struct path *path)
    1271             : {
    1272           0 :         struct mount *mnt = real_mount(path->mnt);
    1273           0 :         struct mount *parent;
    1274           0 :         struct dentry *mountpoint;
    1275             : 
    1276           0 :         read_seqlock_excl(&mount_lock);
    1277           0 :         parent = mnt->mnt_parent;
    1278           0 :         if (parent == mnt) {
    1279           0 :                 read_sequnlock_excl(&mount_lock);
    1280           0 :                 return 0;
    1281             :         }
    1282           0 :         mntget(&parent->mnt);
    1283           0 :         mountpoint = dget(mnt->mnt_mountpoint);
    1284           0 :         read_sequnlock_excl(&mount_lock);
    1285           0 :         dput(path->dentry);
    1286           0 :         path->dentry = mountpoint;
    1287           0 :         mntput(path->mnt);
    1288           0 :         path->mnt = &parent->mnt;
    1289           0 :         return 1;
    1290             : }
    1291             : EXPORT_SYMBOL(follow_up);
    1292             : 
    1293        1589 : static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
    1294             :                                   struct path *path, unsigned *seqp)
    1295             : {
    1296        1589 :         while (mnt_has_parent(m)) {
    1297        1589 :                 struct dentry *mountpoint = m->mnt_mountpoint;
    1298             : 
    1299        1589 :                 m = m->mnt_parent;
    1300        1589 :                 if (unlikely(root->dentry == mountpoint &&
    1301             :                              root->mnt == &m->mnt))
    1302             :                         break;
    1303        1589 :                 if (mountpoint != m->mnt.mnt_root) {
    1304        1589 :                         path->mnt = &m->mnt;
    1305        1589 :                         path->dentry = mountpoint;
    1306        1589 :                         *seqp = read_seqcount_begin(&mountpoint->d_seq);
    1307        1589 :                         return true;
    1308             :                 }
    1309             :         }
    1310             :         return false;
    1311             : }
    1312             : 
    1313        1523 : static bool choose_mountpoint(struct mount *m, const struct path *root,
    1314             :                               struct path *path)
    1315             : {
    1316        1523 :         bool found;
    1317             : 
    1318        1523 :         rcu_read_lock();
    1319           0 :         while (1) {
    1320        1523 :                 unsigned seq, mseq = read_seqbegin(&mount_lock);
    1321             : 
    1322        1523 :                 found = choose_mountpoint_rcu(m, root, path, &seq);
    1323        1523 :                 if (unlikely(!found)) {
    1324           0 :                         if (!read_seqretry(&mount_lock, mseq))
    1325             :                                 break;
    1326             :                 } else {
    1327        1523 :                         if (likely(__legitimize_path(path, seq, mseq)))
    1328             :                                 break;
    1329           0 :                         rcu_read_unlock();
    1330           0 :                         path_put(path);
    1331           0 :                         rcu_read_lock();
    1332             :                 }
    1333             :         }
    1334        1523 :         rcu_read_unlock();
    1335        1523 :         return found;
    1336             : }
    1337             : 
    1338             : /*
    1339             :  * Perform an automount
    1340             :  * - return -EISDIR to tell follow_managed() to stop and return the path we
    1341             :  *   were called with.
    1342             :  */
    1343          11 : static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
    1344             : {
    1345          11 :         struct dentry *dentry = path->dentry;
    1346             : 
    1347             :         /* We don't want to mount if someone's just doing a stat -
    1348             :          * unless they're stat'ing a directory and appended a '/' to
    1349             :          * the name.
    1350             :          *
    1351             :          * We do, however, want to mount if someone wants to open or
    1352             :          * create a file of any type under the mountpoint, wants to
    1353             :          * traverse through the mountpoint or wants to open the
    1354             :          * mounted directory.  Also, autofs may mark negative dentries
    1355             :          * as being automount points.  These will need the attentions
    1356             :          * of the daemon to instantiate them before they can be used.
    1357             :          */
    1358          11 :         if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
    1359           0 :                            LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
    1360           0 :             dentry->d_inode)
    1361             :                 return -EISDIR;
    1362             : 
    1363          11 :         if (count && (*count)++ >= MAXSYMLINKS)
    1364             :                 return -ELOOP;
    1365             : 
    1366          11 :         return finish_automount(dentry->d_op->d_automount(path), path);
    1367             : }
    1368             : 
    1369             : /*
    1370             :  * mount traversal - out-of-line part.  One note on ->d_flags accesses -
    1371             :  * dentries are pinned but not locked here, so negative dentry can go
    1372             :  * positive right under us.  Use of smp_load_acquire() provides a barrier
    1373             :  * sufficient for ->d_inode and ->d_flags consistency.
    1374             :  */
    1375    74301920 : static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
    1376             :                              int *count, unsigned lookup_flags)
    1377             : {
    1378    74301920 :         struct vfsmount *mnt = path->mnt;
    1379    74301920 :         bool need_mntput = false;
    1380    74301920 :         int ret = 0;
    1381             : 
    1382   148580332 :         while (flags & DCACHE_MANAGED_DENTRY) {
    1383             :                 /* Allow the filesystem to manage the transit without i_mutex
    1384             :                  * being held. */
    1385    74397891 :                 if (flags & DCACHE_MANAGE_TRANSIT) {
    1386           0 :                         ret = path->dentry->d_op->d_manage(path, false);
    1387           0 :                         flags = smp_load_acquire(&path->dentry->d_flags);
    1388           0 :                         if (ret < 0)
    1389             :                                 break;
    1390             :                 }
    1391             : 
    1392    74397891 :                 if (flags & DCACHE_MOUNTED) {       // something's mounted on it..
    1393    74398532 :                         struct vfsmount *mounted = lookup_mnt(path);
    1394    74414494 :                         if (mounted) {          // ... in our namespace
    1395    74289451 :                                 dput(path->dentry);
    1396    74302280 :                                 if (need_mntput)
    1397           0 :                                         mntput(path->mnt);
    1398    74302280 :                                 path->mnt = mounted;
    1399    74302280 :                                 path->dentry = dget(mounted->mnt_root);
    1400             :                                 // here we know it's positive
    1401    74312281 :                                 flags = path->dentry->d_flags;
    1402    74312281 :                                 need_mntput = true;
    1403    74312281 :                                 continue;
    1404             :                         }
    1405             :                 }
    1406             : 
    1407      124402 :                 if (!(flags & DCACHE_NEED_AUTOMOUNT))
    1408             :                         break;
    1409             : 
    1410             :                 // uncovered automount point
    1411          11 :                 ret = follow_automount(path, count, lookup_flags);
    1412          11 :                 flags = smp_load_acquire(&path->dentry->d_flags);
    1413           0 :                 if (ret < 0)
    1414             :                         break;
    1415             :         }
    1416             : 
    1417    74306832 :         if (ret == -EISDIR)
    1418           0 :                 ret = 0;
    1419             :         // possible if you race with several mount --move
    1420    74306832 :         if (need_mntput && path->mnt == mnt)
    1421           0 :                 mntput(path->mnt);
    1422    74306832 :         if (!ret && unlikely(d_flags_negative(flags)))
    1423           0 :                 ret = -ENOENT;
    1424    74306832 :         *jumped = need_mntput;
    1425    74306832 :         return ret;
    1426             : }
    1427             : 
    1428  1759010896 : static inline int traverse_mounts(struct path *path, bool *jumped,
    1429             :                                   int *count, unsigned lookup_flags)
    1430             : {
    1431  1759010896 :         unsigned flags = smp_load_acquire(&path->dentry->d_flags);
    1432             : 
    1433             :         /* fastpath */
    1434  1759182841 :         if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
    1435  1684872396 :                 *jumped = false;
    1436  1684872396 :                 if (unlikely(d_flags_negative(flags)))
    1437             :                         return -ENOENT;
    1438   652521211 :                 return 0;
    1439             :         }
    1440    74310445 :         return __traverse_mounts(path, flags, jumped, count, lookup_flags);
    1441             : }
    1442             : 
    1443        3812 : int follow_down_one(struct path *path)
    1444             : {
    1445        3812 :         struct vfsmount *mounted;
    1446             : 
    1447        3812 :         mounted = lookup_mnt(path);
    1448        3812 :         if (mounted) {
    1449        1906 :                 dput(path->dentry);
    1450        1906 :                 mntput(path->mnt);
    1451        1906 :                 path->mnt = mounted;
    1452        1906 :                 path->dentry = dget(mounted->mnt_root);
    1453        1906 :                 return 1;
    1454             :         }
    1455             :         return 0;
    1456             : }
    1457             : EXPORT_SYMBOL(follow_down_one);
    1458             : 
    1459             : /*
    1460             :  * Follow down to the covering mount currently visible to userspace.  At each
    1461             :  * point, the filesystem owning that dentry may be queried as to whether the
    1462             :  * caller is permitted to proceed or not.
    1463             :  */
    1464           2 : int follow_down(struct path *path, unsigned int flags)
    1465             : {
    1466           2 :         struct vfsmount *mnt = path->mnt;
    1467           2 :         bool jumped;
    1468           2 :         int ret = traverse_mounts(path, &jumped, NULL, flags);
    1469             : 
    1470           2 :         if (path->mnt != mnt)
    1471           2 :                 mntput(mnt);
    1472           2 :         return ret;
    1473             : }
    1474             : EXPORT_SYMBOL(follow_down);
    1475             : 
    1476             : /*
    1477             :  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
    1478             :  * we meet a managed dentry that would need blocking.
    1479             :  */
    1480 71110496755 : static bool __follow_mount_rcu(struct nameidata *nd, struct path *path)
    1481             : {
    1482 71110496755 :         struct dentry *dentry = path->dentry;
    1483 71110496755 :         unsigned int flags = dentry->d_flags;
    1484             : 
    1485 71110496755 :         if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
    1486             :                 return true;
    1487             : 
    1488  3257136299 :         if (unlikely(nd->flags & LOOKUP_NO_XDEV))
    1489             :                 return false;
    1490             : 
    1491  9664070359 :         for (;;) {
    1492             :                 /*
    1493             :                  * Don't forget we might have a non-mountpoint managed dentry
    1494             :                  * that wants to block transit.
    1495             :                  */
    1496  6460603329 :                 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
    1497           0 :                         int res = dentry->d_op->d_manage(path, true);
    1498           0 :                         if (res)
    1499           0 :                                 return res == -EISDIR;
    1500           0 :                         flags = dentry->d_flags;
    1501             :                 }
    1502             : 
    1503  6460603329 :                 if (flags & DCACHE_MOUNTED) {
    1504  4261719346 :                         struct mount *mounted = __lookup_mnt(path->mnt, dentry);
    1505  4264122309 :                         if (mounted) {
    1506  3203216021 :                                 path->mnt = &mounted->mnt;
    1507  3203216021 :                                 dentry = path->dentry = mounted->mnt.mnt_root;
    1508  3203216021 :                                 nd->state |= ND_JUMPED;
    1509  3203216021 :                                 nd->next_seq = read_seqcount_begin(&dentry->d_seq);
    1510  3203895865 :                                 flags = dentry->d_flags;
    1511             :                                 // makes sure that non-RCU pathwalk could reach
    1512             :                                 // this state.
    1513  3203895865 :                                 if (read_seqretry(&mount_lock, nd->m_seq))
    1514             :                                         return false;
    1515  3203467030 :                                 continue;
    1516             :                         }
    1517  1060906288 :                         if (read_seqretry(&mount_lock, nd->m_seq))
    1518             :                                 return false;
    1519             :                 }
    1520  3259768765 :                 return !(flags & DCACHE_NEED_AUTOMOUNT);
    1521             :         }
    1522             : }
    1523             : 
    1524 72848040715 : static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
    1525             :                           struct path *path)
    1526             : {
    1527 72848040715 :         bool jumped;
    1528 72848040715 :         int ret;
    1529             : 
    1530 72848040715 :         path->mnt = nd->path.mnt;
    1531 72848040715 :         path->dentry = dentry;
    1532 72848040715 :         if (nd->flags & LOOKUP_RCU) {
    1533 71117974896 :                 unsigned int seq = nd->next_seq;
    1534 71117974896 :                 if (likely(__follow_mount_rcu(nd, path)))
    1535             :                         return 0;
    1536             :                 // *path and nd->next_seq might've been clobbered
    1537       66909 :                 path->mnt = nd->path.mnt;
    1538       66909 :                 path->dentry = dentry;
    1539       66909 :                 nd->next_seq = seq;
    1540       66909 :                 if (!try_to_unlazy_next(nd, dentry))
    1541             :                         return -ECHILD;
    1542             :         }
    1543  1730065907 :         ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
    1544  1758961435 :         if (jumped) {
    1545    74282605 :                 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
    1546             :                         ret = -EXDEV;
    1547             :                 else
    1548    74282605 :                         nd->state |= ND_JUMPED;
    1549             :         }
    1550  1758961435 :         if (unlikely(ret)) {
    1551  1032321692 :                 dput(path->dentry);
    1552  1032504117 :                 if (path->mnt != nd->path.mnt)
    1553           0 :                         mntput(path->mnt);
    1554             :         }
    1555             :         return ret;
    1556             : }
    1557             : 
    1558             : /*
    1559             :  * This looks up the name in dcache and possibly revalidates the found dentry.
    1560             :  * NULL is returned if the dentry does not exist in the cache.
    1561             :  */
    1562   889332267 : static struct dentry *lookup_dcache(const struct qstr *name,
    1563             :                                     struct dentry *dir,
    1564             :                                     unsigned int flags)
    1565             : {
    1566   889332267 :         struct dentry *dentry = d_lookup(dir, name);
    1567   889761120 :         if (dentry) {
    1568   750379650 :                 int error = d_revalidate(dentry, flags);
    1569   750308527 :                 if (unlikely(error <= 0)) {
    1570         936 :                         if (!error)
    1571         936 :                                 d_invalidate(dentry);
    1572         936 :                         dput(dentry);
    1573         936 :                         return ERR_PTR(error);
    1574             :                 }
    1575             :         }
    1576             :         return dentry;
    1577             : }
    1578             : 
    1579             : /*
    1580             :  * Parent directory has inode locked exclusive.  This is one
    1581             :  * and only case when ->lookup() gets called on non in-lookup
    1582             :  * dentries - as the matter of fact, this only gets called
    1583             :  * when directory is guaranteed to have no in-lookup children
    1584             :  * at all.
    1585             :  */
    1586   877891451 : struct dentry *lookup_one_qstr_excl(const struct qstr *name,
    1587             :                                     struct dentry *base,
    1588             :                                     unsigned int flags)
    1589             : {
    1590   877891451 :         struct dentry *dentry = lookup_dcache(name, base, flags);
    1591   877973722 :         struct dentry *old;
    1592   877973722 :         struct inode *dir = base->d_inode;
    1593             : 
    1594   877973722 :         if (dentry)
    1595             :                 return dentry;
    1596             : 
    1597             :         /* Don't create child dentry for a dead directory. */
    1598   133395489 :         if (unlikely(IS_DEADDIR(dir)))
    1599             :                 return ERR_PTR(-ENOENT);
    1600             : 
    1601   133395445 :         dentry = d_alloc(base, name);
    1602   133387925 :         if (unlikely(!dentry))
    1603             :                 return ERR_PTR(-ENOMEM);
    1604             : 
    1605   133387925 :         old = dir->i_op->lookup(dir, dentry, flags);
    1606   133333002 :         if (unlikely(old)) {
    1607      199494 :                 dput(dentry);
    1608      199494 :                 dentry = old;
    1609             :         }
    1610             :         return dentry;
    1611             : }
    1612             : EXPORT_SYMBOL(lookup_one_qstr_excl);
    1613             : 
    1614 72205103918 : static struct dentry *lookup_fast(struct nameidata *nd)
    1615             : {
    1616 72205103918 :         struct dentry *dentry, *parent = nd->path.dentry;
    1617 72205103918 :         int status = 1;
    1618             : 
    1619             :         /*
    1620             :          * Rename seqlock is not required here because in the off chance
    1621             :          * of a false negative due to a concurrent rename, the caller is
    1622             :          * going to fall back to non-racy lookup.
    1623             :          */
    1624 72205103918 :         if (nd->flags & LOOKUP_RCU) {
    1625 71556940571 :                 dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq);
    1626 71690010132 :                 if (unlikely(!dentry)) {
    1627  1039426598 :                         if (!try_to_unlazy(nd))
    1628             :                                 return ERR_PTR(-ECHILD);
    1629  1040724913 :                         return NULL;
    1630             :                 }
    1631             : 
    1632             :                 /*
    1633             :                  * This sequence count validates that the parent had no
    1634             :                  * changes while we did the lookup of the dentry above.
    1635             :                  */
    1636 70650583534 :                 if (read_seqcount_retry(&parent->d_seq, nd->seq))
    1637             :                         return ERR_PTR(-ECHILD);
    1638             : 
    1639 70683465313 :                 status = d_revalidate(dentry, nd->flags);
    1640 70643617217 :                 if (likely(status > 0))
    1641             :                         return dentry;
    1642    30067276 :                 if (!try_to_unlazy_next(nd, dentry))
    1643             :                         return ERR_PTR(-ECHILD);
    1644    30100453 :                 if (status == -ECHILD)
    1645             :                         /* we'd been told to redo it in non-rcu mode */
    1646    30098902 :                         status = d_revalidate(dentry, nd->flags);
    1647             :         } else {
    1648   648163347 :                 dentry = __d_lookup(parent, &nd->last);
    1649   648708500 :                 if (unlikely(!dentry))
    1650             :                         return NULL;
    1651   625793929 :                 status = d_revalidate(dentry, nd->flags);
    1652             :         }
    1653   655553741 :         if (unlikely(status <= 0)) {
    1654     1576701 :                 if (!status)
    1655     1572515 :                         d_invalidate(dentry);
    1656     1577919 :                 dput(dentry);
    1657     1577949 :                 return ERR_PTR(status);
    1658             :         }
    1659             :         return dentry;
    1660             : }
    1661             : 
    1662             : /* Fast lookup failed, do it the slow way */
    1663  1037574833 : static struct dentry *__lookup_slow(const struct qstr *name,
    1664             :                                     struct dentry *dir,
    1665             :                                     unsigned int flags)
    1666             : {
    1667  1037574833 :         struct dentry *dentry, *old;
    1668  1037574833 :         struct inode *inode = dir->d_inode;
    1669  1037574833 :         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
    1670             : 
    1671             :         /* Don't go there if it's already dead */
    1672  1037574833 :         if (unlikely(IS_DEADDIR(inode)))
    1673             :                 return ERR_PTR(-ENOENT);
    1674  1036451898 : again:
    1675  1036452217 :         dentry = d_alloc_parallel(dir, name, &wq);
    1676  1037250201 :         if (IS_ERR(dentry))
    1677           0 :                 return dentry;
    1678  1037250201 :         if (unlikely(!d_in_lookup(dentry))) {
    1679     9727686 :                 int error = d_revalidate(dentry, flags);
    1680     9723516 :                 if (unlikely(error <= 0)) {
    1681         318 :                         if (!error) {
    1682         318 :                                 d_invalidate(dentry);
    1683         319 :                                 dput(dentry);
    1684         319 :                                 goto again;
    1685             :                         }
    1686           0 :                         dput(dentry);
    1687           0 :                         dentry = ERR_PTR(error);
    1688             :                 }
    1689             :         } else {
    1690  1027522515 :                 old = inode->i_op->lookup(inode, dentry, flags);
    1691  1027538099 :                 d_lookup_done(dentry);
    1692  1027504405 :                 if (unlikely(old)) {
    1693     3036029 :                         dput(dentry);
    1694     3036029 :                         dentry = old;
    1695             :                 }
    1696             :         }
    1697             :         return dentry;
    1698             : }
    1699             : 
    1700  1034958852 : static struct dentry *lookup_slow(const struct qstr *name,
    1701             :                                   struct dentry *dir,
    1702             :                                   unsigned int flags)
    1703             : {
    1704  1034958852 :         struct inode *inode = dir->d_inode;
    1705  1034958852 :         struct dentry *res;
    1706  1034958852 :         inode_lock_shared(inode);
    1707  1034870034 :         res = __lookup_slow(name, dir, flags);
    1708  1035236572 :         inode_unlock_shared(inode);
    1709  1035266886 :         return res;
    1710             : }
    1711             : 
    1712 75629811806 : static inline int may_lookup(struct mnt_idmap *idmap,
    1713             :                              struct nameidata *nd)
    1714             : {
    1715 75629811806 :         if (nd->flags & LOOKUP_RCU) {
    1716 75217938667 :                 int err = inode_permission(idmap, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
    1717 75235279732 :                 if (err != -ECHILD || !try_to_unlazy(nd))
    1718 74978567095 :                         return err;
    1719             :         }
    1720   668826134 :         return inode_permission(idmap, nd->inode, MAY_EXEC);
    1721             : }
    1722             : 
    1723   453325964 : static int reserve_stack(struct nameidata *nd, struct path *link)
    1724             : {
    1725   453325964 :         if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
    1726             :                 return -ELOOP;
    1727             : 
    1728   453317603 :         if (likely(nd->depth != EMBEDDED_LEVELS))
    1729             :                 return 0;
    1730           0 :         if (likely(nd->stack != nd->internal))
    1731             :                 return 0;
    1732           0 :         if (likely(nd_alloc_stack(nd)))
    1733             :                 return 0;
    1734             : 
    1735           0 :         if (nd->flags & LOOKUP_RCU) {
    1736             :                 // we need to grab link before we do unlazy.  And we can't skip
    1737             :                 // unlazy even if we fail to grab the link - cleanup needs it
    1738           0 :                 bool grabbed_link = legitimize_path(nd, link, nd->next_seq);
    1739             : 
    1740           0 :                 if (!try_to_unlazy(nd) || !grabbed_link)
    1741             :                         return -ECHILD;
    1742             : 
    1743           0 :                 if (nd_alloc_stack(nd))
    1744           0 :                         return 0;
    1745             :         }
    1746             :         return -ENOMEM;
    1747             : }
    1748             : 
    1749             : enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
    1750             : 
    1751   453353373 : static const char *pick_link(struct nameidata *nd, struct path *link,
    1752             :                      struct inode *inode, int flags)
    1753             : {
    1754   453353373 :         struct saved *last;
    1755   453353373 :         const char *res;
    1756   453353373 :         int error = reserve_stack(nd, link);
    1757             : 
    1758   453372537 :         if (unlikely(error)) {
    1759        8362 :                 if (!(nd->flags & LOOKUP_RCU))
    1760        8340 :                         path_put(link);
    1761        8363 :                 return ERR_PTR(error);
    1762             :         }
    1763   453364175 :         last = nd->stack + nd->depth++;
    1764   453364175 :         last->link = *link;
    1765   453364175 :         clear_delayed_call(&last->done);
    1766   453364175 :         last->seq = nd->next_seq;
    1767             : 
    1768   453364175 :         if (flags & WALK_TRAILING) {
    1769   158787552 :                 error = may_follow_link(nd, inode);
    1770   158719450 :                 if (unlikely(error))
    1771         182 :                         return ERR_PTR(error);
    1772             :         }
    1773             : 
    1774   453295891 :         if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
    1775   453295891 :                         unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
    1776             :                 return ERR_PTR(-ELOOP);
    1777             : 
    1778   453295891 :         if (!(nd->flags & LOOKUP_RCU)) {
    1779    12035121 :                 touch_atime(&last->link);
    1780    12024662 :                 cond_resched();
    1781   441260770 :         } else if (atime_needs_update(&last->link, inode)) {
    1782     1221991 :                 if (!try_to_unlazy(nd))
    1783             :                         return ERR_PTR(-ECHILD);
    1784     1223659 :                 touch_atime(&last->link);
    1785             :         }
    1786             : 
    1787   453159531 :         error = security_inode_follow_link(link->dentry, inode,
    1788   453159531 :                                            nd->flags & LOOKUP_RCU);
    1789   453159531 :         if (unlikely(error))
    1790             :                 return ERR_PTR(error);
    1791             : 
    1792   453159531 :         res = READ_ONCE(inode->i_link);
    1793   453159531 :         if (!res) {
    1794   448858039 :                 const char * (*get)(struct dentry *, struct inode *,
    1795             :                                 struct delayed_call *);
    1796   448858039 :                 get = inode->i_op->get_link;
    1797   448858039 :                 if (nd->flags & LOOKUP_RCU) {
    1798   436763274 :                         res = get(NULL, inode, &last->done);
    1799   437044127 :                         if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
    1800    14394485 :                                 res = get(link->dentry, inode, &last->done);
    1801             :                 } else {
    1802    12094765 :                         res = get(link->dentry, inode, &last->done);
    1803             :                 }
    1804   449147102 :                 if (!res)
    1805     2380624 :                         goto all_done;
    1806   446766478 :                 if (IS_ERR(res))
    1807             :                         return res;
    1808             :         }
    1809   451043770 :         if (*res == '/') {
    1810    67238111 :                 error = nd_jump_root(nd);
    1811    67252294 :                 if (unlikely(error))
    1812           0 :                         return ERR_PTR(error);
    1813    67252294 :                 while (unlikely(*++res == '/'))
    1814             :                         ;
    1815             :         }
    1816   451057953 :         if (*res)
    1817             :                 return res;
    1818           0 : all_done: // pure jump
    1819     2380624 :         put_link(nd);
    1820     2380624 :         return NULL;
    1821             : }
    1822             : 
    1823             : /*
    1824             :  * Do we need to follow links? We _really_ want to be able
    1825             :  * to do this check without having to look at inode->i_op,
    1826             :  * so we keep a cache of "no, this doesn't need follow_link"
    1827             :  * for the common case.
    1828             :  *
    1829             :  * NOTE: dentry must be what nd->next_seq had been sampled from.
    1830             :  */
    1831 72892557830 : static const char *step_into(struct nameidata *nd, int flags,
    1832             :                      struct dentry *dentry)
    1833             : {
    1834 72892557830 :         struct path path;
    1835 72892557830 :         struct inode *inode;
    1836 72892557830 :         int err = handle_mounts(nd, dentry, &path);
    1837             : 
    1838 72885665442 :         if (err < 0)
    1839  1032492469 :                 return ERR_PTR(err);
    1840 71853172973 :         inode = path.dentry->d_inode;
    1841 71853172973 :         if (likely(!d_is_symlink(path.dentry)) ||
    1842   760022717 :            ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
    1843   453295780 :            (flags & WALK_NOFOLLOW)) {
    1844             :                 /* not a symlink or should not follow */
    1845 71399877193 :                 if (nd->flags & LOOKUP_RCU) {
    1846 70685173323 :                         if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq))
    1847             :                                 return ERR_PTR(-ECHILD);
    1848 70693559149 :                         if (unlikely(!inode))
    1849             :                                 return ERR_PTR(-ENOENT);
    1850             :                 } else {
    1851   714703870 :                         dput(nd->path.dentry);
    1852   715336493 :                         if (nd->path.mnt != path.mnt)
    1853    74320022 :                                 mntput(nd->path.mnt);
    1854             :                 }
    1855 71081399119 :                 nd->path = path;
    1856 71081399119 :                 nd->inode = inode;
    1857 71081399119 :                 nd->seq = nd->next_seq;
    1858 71081399119 :                 return NULL;
    1859             :         }
    1860   453295780 :         if (nd->flags & LOOKUP_RCU) {
    1861             :                 /* make sure that d_is_symlink above matches inode */
    1862   441242871 :                 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq))
    1863             :                         return ERR_PTR(-ECHILD);
    1864             :         } else {
    1865    12052909 :                 if (path.mnt == nd->path.mnt)
    1866    12046997 :                         mntget(path.mnt);
    1867             :         }
    1868   453326678 :         return pick_link(nd, &path, inode, flags);
    1869             : }
    1870             : 
    1871   530045580 : static struct dentry *follow_dotdot_rcu(struct nameidata *nd)
    1872             : {
    1873   530045580 :         struct dentry *parent, *old;
    1874             : 
    1875   530045580 :         if (path_equal(&nd->path, &nd->root))
    1876           0 :                 goto in_root;
    1877   530045580 :         if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
    1878          66 :                 struct path path;
    1879          66 :                 unsigned seq;
    1880          66 :                 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
    1881          66 :                                            &nd->root, &path, &seq))
    1882           0 :                         goto in_root;
    1883          66 :                 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
    1884           0 :                         return ERR_PTR(-ECHILD);
    1885          66 :                 nd->path = path;
    1886          66 :                 nd->inode = path.dentry->d_inode;
    1887          66 :                 nd->seq = seq;
    1888             :                 // makes sure that non-RCU pathwalk could reach this state
    1889          66 :                 if (read_seqretry(&mount_lock, nd->m_seq))
    1890             :                         return ERR_PTR(-ECHILD);
    1891             :                 /* we know that mountpoint was pinned */
    1892             :         }
    1893   530045580 :         old = nd->path.dentry;
    1894   530045580 :         parent = old->d_parent;
    1895   530045580 :         nd->next_seq = read_seqcount_begin(&parent->d_seq);
    1896             :         // makes sure that non-RCU pathwalk could reach this state
    1897   530117491 :         if (read_seqcount_retry(&old->d_seq, nd->seq))
    1898             :                 return ERR_PTR(-ECHILD);
    1899   530120021 :         if (unlikely(!path_connected(nd->path.mnt, parent)))
    1900           0 :                 return ERR_PTR(-ECHILD);
    1901             :         return parent;
    1902           0 : in_root:
    1903           0 :         if (read_seqretry(&mount_lock, nd->m_seq))
    1904             :                 return ERR_PTR(-ECHILD);
    1905           0 :         if (unlikely(nd->flags & LOOKUP_BENEATH))
    1906             :                 return ERR_PTR(-ECHILD);
    1907           0 :         nd->next_seq = nd->seq;
    1908           0 :         return nd->path.dentry;
    1909             : }
    1910             : 
    1911    22742685 : static struct dentry *follow_dotdot(struct nameidata *nd)
    1912             : {
    1913    22742685 :         struct dentry *parent;
    1914             : 
    1915    22742685 :         if (path_equal(&nd->path, &nd->root))
    1916           0 :                 goto in_root;
    1917    22742685 :         if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
    1918        1523 :                 struct path path;
    1919             : 
    1920        1523 :                 if (!choose_mountpoint(real_mount(nd->path.mnt),
    1921        1523 :                                        &nd->root, &path))
    1922           0 :                         goto in_root;
    1923        1523 :                 path_put(&nd->path);
    1924        1523 :                 nd->path = path;
    1925        1523 :                 nd->inode = path.dentry->d_inode;
    1926        1523 :                 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
    1927           0 :                         return ERR_PTR(-EXDEV);
    1928             :         }
    1929             :         /* rare case of legitimate dget_parent()... */
    1930    22742685 :         parent = dget_parent(nd->path.dentry);
    1931    22768451 :         if (unlikely(!path_connected(nd->path.mnt, parent))) {
    1932           0 :                 dput(parent);
    1933           0 :                 return ERR_PTR(-ENOENT);
    1934             :         }
    1935             :         return parent;
    1936             : 
    1937           0 : in_root:
    1938           0 :         if (unlikely(nd->flags & LOOKUP_BENEATH))
    1939             :                 return ERR_PTR(-EXDEV);
    1940           0 :         return dget(nd->path.dentry);
    1941             : }
    1942             : 
    1943  3590340428 : static const char *handle_dots(struct nameidata *nd, int type)
    1944             : {
    1945  3590340428 :         if (type == LAST_DOTDOT) {
    1946   552739171 :                 const char *error = NULL;
    1947   552739171 :                 struct dentry *parent;
    1948             : 
    1949   552739171 :                 if (!nd->root.mnt) {
    1950   520658036 :                         error = ERR_PTR(set_root(nd));
    1951   520727685 :                         if (error)
    1952             :                                 return error;
    1953             :                 }
    1954   552808820 :                 if (nd->flags & LOOKUP_RCU)
    1955   530054436 :                         parent = follow_dotdot_rcu(nd);
    1956             :                 else
    1957    22754384 :                         parent = follow_dotdot(nd);
    1958   552817194 :                 if (IS_ERR(parent))
    1959             :                         return ERR_CAST(parent);
    1960   552817194 :                 error = step_into(nd, WALK_NOFOLLOW, parent);
    1961   552765244 :                 if (unlikely(error))
    1962             :                         return error;
    1963             : 
    1964   552765244 :                 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
    1965             :                         /*
    1966             :                          * If there was a racing rename or mount along our
    1967             :                          * path, then we can't be sure that ".." hasn't jumped
    1968             :                          * above nd->root (and so userspace should retry or use
    1969             :                          * some fallback).
    1970             :                          */
    1971           0 :                         smp_rmb();
    1972           0 :                         if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))
    1973             :                                 return ERR_PTR(-EAGAIN);
    1974           0 :                         if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))
    1975           0 :                                 return ERR_PTR(-EAGAIN);
    1976             :                 }
    1977             :         }
    1978             :         return NULL;
    1979             : }
    1980             : 
    1981 74577273255 : static const char *walk_component(struct nameidata *nd, int flags)
    1982             : {
    1983 74577273255 :         struct dentry *dentry;
    1984             :         /*
    1985             :          * "." and ".." are special - ".." especially so because it has
    1986             :          * to be able to know about the current root directory and
    1987             :          * parent relationships.
    1988             :          */
    1989 74577273255 :         if (unlikely(nd->last_type != LAST_NORM)) {
    1990  3570522422 :                 if (!(flags & WALK_MORE) && nd->depth)
    1991           0 :                         put_link(nd);
    1992  3570522422 :                 return handle_dots(nd, nd->last_type);
    1993             :         }
    1994 71006750833 :         dentry = lookup_fast(nd);
    1995 71149502980 :         if (IS_ERR(dentry))
    1996             :                 return ERR_CAST(dentry);
    1997 71149492154 :         if (unlikely(!dentry)) {
    1998  1032154493 :                 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
    1999  1032418230 :                 if (IS_ERR(dentry))
    2000             :                         return ERR_CAST(dentry);
    2001             :         }
    2002 71148422148 :         if (!(flags & WALK_MORE) && nd->depth)
    2003   310024869 :                 put_link(nd);
    2004 71148421201 :         return step_into(nd, flags, dentry);
    2005             : }
    2006             : 
    2007             : /*
    2008             :  * We can do the critical dentry name comparison and hashing
    2009             :  * operations one word at a time, but we are limited to:
    2010             :  *
    2011             :  * - Architectures with fast unaligned word accesses. We could
    2012             :  *   do a "get_unaligned()" if this helps and is sufficiently
    2013             :  *   fast.
    2014             :  *
    2015             :  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
    2016             :  *   do not trap on the (extremely unlikely) case of a page
    2017             :  *   crossing operation.
    2018             :  *
    2019             :  * - Furthermore, we need an efficient 64-bit compile for the
    2020             :  *   64-bit case in order to generate the "number of bytes in
    2021             :  *   the final mask". Again, that could be replaced with a
    2022             :  *   efficient population count instruction or similar.
    2023             :  */
    2024             : #ifdef CONFIG_DCACHE_WORD_ACCESS
    2025             : 
    2026             : #include <asm/word-at-a-time.h>
    2027             : 
    2028             : #ifdef HASH_MIX
    2029             : 
    2030             : /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
    2031             : 
    2032             : #elif defined(CONFIG_64BIT)
    2033             : /*
    2034             :  * Register pressure in the mixing function is an issue, particularly
    2035             :  * on 32-bit x86, but almost any function requires one state value and
    2036             :  * one temporary.  Instead, use a function designed for two state values
    2037             :  * and no temporaries.
    2038             :  *
    2039             :  * This function cannot create a collision in only two iterations, so
    2040             :  * we have two iterations to achieve avalanche.  In those two iterations,
    2041             :  * we have six layers of mixing, which is enough to spread one bit's
    2042             :  * influence out to 2^6 = 64 state bits.
    2043             :  *
    2044             :  * Rotate constants are scored by considering either 64 one-bit input
    2045             :  * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
    2046             :  * probability of that delta causing a change to each of the 128 output
    2047             :  * bits, using a sample of random initial states.
    2048             :  *
    2049             :  * The Shannon entropy of the computed probabilities is then summed
    2050             :  * to produce a score.  Ideally, any input change has a 50% chance of
    2051             :  * toggling any given output bit.
    2052             :  *
    2053             :  * Mixing scores (in bits) for (12,45):
    2054             :  * Input delta: 1-bit      2-bit
    2055             :  * 1 round:     713.3    42542.6
    2056             :  * 2 rounds:   2753.7   140389.8
    2057             :  * 3 rounds:   5954.1   233458.2
    2058             :  * 4 rounds:   7862.6   256672.2
    2059             :  * Perfect:    8192     258048
    2060             :  *            (64*128) (64*63/2 * 128)
    2061             :  */
    2062             : #define HASH_MIX(x, y, a)       \
    2063             :         (       x ^= (a),       \
    2064             :         y ^= x, x = rol64(x,12),\
    2065             :         x += y, y = rol64(y,45),\
    2066             :         y *= 9                  )
    2067             : 
    2068             : /*
    2069             :  * Fold two longs into one 32-bit hash value.  This must be fast, but
    2070             :  * latency isn't quite as critical, as there is a fair bit of additional
    2071             :  * work done before the hash value is used.
    2072             :  */
    2073             : static inline unsigned int fold_hash(unsigned long x, unsigned long y)
    2074             : {
    2075 75622917059 :         y ^= x * GOLDEN_RATIO_64;
    2076 75622917059 :         y *= GOLDEN_RATIO_64;
    2077 75622917059 :         return y >> 32;
    2078             : }
    2079             : 
    2080             : #else   /* 32-bit case */
    2081             : 
    2082             : /*
    2083             :  * Mixing scores (in bits) for (7,20):
    2084             :  * Input delta: 1-bit      2-bit
    2085             :  * 1 round:     330.3     9201.6
    2086             :  * 2 rounds:   1246.4    25475.4
    2087             :  * 3 rounds:   1907.1    31295.1
    2088             :  * 4 rounds:   2042.3    31718.6
    2089             :  * Perfect:    2048      31744
    2090             :  *            (32*64)   (32*31/2 * 64)
    2091             :  */
    2092             : #define HASH_MIX(x, y, a)       \
    2093             :         (       x ^= (a),       \
    2094             :         y ^= x, x = rol32(x, 7),\
    2095             :         x += y, y = rol32(y,20),\
    2096             :         y *= 9                  )
    2097             : 
    2098             : static inline unsigned int fold_hash(unsigned long x, unsigned long y)
    2099             : {
    2100             :         /* Use arch-optimized multiply if one exists */
    2101             :         return __hash_32(y ^ __hash_32(x));
    2102             : }
    2103             : 
    2104             : #endif
    2105             : 
    2106             : /*
    2107             :  * Return the hash of a string of known length.  This is carfully
    2108             :  * designed to match hash_name(), which is the more critical function.
    2109             :  * In particular, we must end by hashing a final word containing 0..7
    2110             :  * payload bytes, to match the way that hash_name() iterates until it
    2111             :  * finds the delimiter after the name.
    2112             :  */
    2113    34523237 : unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
    2114             : {
    2115    34523237 :         unsigned long a, x = 0, y = (unsigned long)salt;
    2116             : 
    2117    53784849 :         for (;;) {
    2118    44154043 :                 if (!len)
    2119      199593 :                         goto done;
    2120    43954450 :                 a = load_unaligned_zeropad(name);
    2121    43953486 :                 if (len < sizeof(unsigned long))
    2122             :                         break;
    2123     9630806 :                 HASH_MIX(x, y, a);
    2124     9630806 :                 name += sizeof(unsigned long);
    2125     9630806 :                 len -= sizeof(unsigned long);
    2126             :         }
    2127    34322680 :         x ^= a & bytemask_from_count(len);
    2128    34522273 : done:
    2129    34522273 :         return fold_hash(x, y);
    2130             : }
    2131             : EXPORT_SYMBOL(full_name_hash);
    2132             : 
    2133             : /* Return the "hash_len" (hash and length) of a null-terminated string */
    2134         165 : u64 hashlen_string(const void *salt, const char *name)
    2135             : {
    2136         165 :         unsigned long a = 0, x = 0, y = (unsigned long)salt;
    2137         165 :         unsigned long adata, mask, len;
    2138         165 :         const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
    2139             : 
    2140         165 :         len = 0;
    2141         165 :         goto inside;
    2142             : 
    2143          82 :         do {
    2144          82 :                 HASH_MIX(x, y, a);
    2145          82 :                 len += sizeof(unsigned long);
    2146         247 : inside:
    2147         247 :                 a = load_unaligned_zeropad(name+len);
    2148         247 :         } while (!has_zero(a, &adata, &constants));
    2149             : 
    2150         165 :         adata = prep_zero_mask(a, adata, &constants);
    2151         165 :         mask = create_zero_mask(adata);
    2152         165 :         x ^= a & zero_bytemask(mask);
    2153             : 
    2154         165 :         return hashlen_create(fold_hash(x, y), len + find_zero(mask));
    2155             : }
    2156             : EXPORT_SYMBOL(hashlen_string);
    2157             : 
    2158             : /*
    2159             :  * Calculate the length and hash of the path component, and
    2160             :  * return the "hash_len" as the result.
    2161             :  */
    2162 75587608656 : static inline u64 hash_name(const void *salt, const char *name)
    2163             : {
    2164 75587608656 :         unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
    2165 75587608656 :         unsigned long adata, bdata, mask, len;
    2166 75587608656 :         const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
    2167             : 
    2168 75587608656 :         len = 0;
    2169 75587608656 :         goto inside;
    2170             : 
    2171  2586043811 :         do {
    2172  2586043811 :                 HASH_MIX(x, y, a);
    2173  2586043811 :                 len += sizeof(unsigned long);
    2174 78173652467 : inside:
    2175 78173652467 :                 a = load_unaligned_zeropad(name+len);
    2176 78174438432 :                 b = a ^ REPEAT_BYTE('/');
    2177 78174438432 :         } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
    2178             : 
    2179 75588394621 :         adata = prep_zero_mask(a, adata, &constants);
    2180 75588394621 :         bdata = prep_zero_mask(b, bdata, &constants);
    2181 75588394621 :         mask = create_zero_mask(adata | bdata);
    2182 75588394621 :         x ^= a & zero_bytemask(mask);
    2183             : 
    2184 75588394621 :         return hashlen_create(fold_hash(x, y), len + find_zero(mask));
    2185             : }
    2186             : 
    2187             : #else   /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
    2188             : 
    2189             : /* Return the hash of a string of known length */
    2190             : unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
    2191             : {
    2192             :         unsigned long hash = init_name_hash(salt);
    2193             :         while (len--)
    2194             :                 hash = partial_name_hash((unsigned char)*name++, hash);
    2195             :         return end_name_hash(hash);
    2196             : }
    2197             : EXPORT_SYMBOL(full_name_hash);
    2198             : 
    2199             : /* Return the "hash_len" (hash and length) of a null-terminated string */
    2200             : u64 hashlen_string(const void *salt, const char *name)
    2201             : {
    2202             :         unsigned long hash = init_name_hash(salt);
    2203             :         unsigned long len = 0, c;
    2204             : 
    2205             :         c = (unsigned char)*name;
    2206             :         while (c) {
    2207             :                 len++;
    2208             :                 hash = partial_name_hash(c, hash);
    2209             :                 c = (unsigned char)name[len];
    2210             :         }
    2211             :         return hashlen_create(end_name_hash(hash), len);
    2212             : }
    2213             : EXPORT_SYMBOL(hashlen_string);
    2214             : 
    2215             : /*
    2216             :  * We know there's a real path component here of at least
    2217             :  * one character.
    2218             :  */
    2219             : static inline u64 hash_name(const void *salt, const char *name)
    2220             : {
    2221             :         unsigned long hash = init_name_hash(salt);
    2222             :         unsigned long len = 0, c;
    2223             : 
    2224             :         c = (unsigned char)*name;
    2225             :         do {
    2226             :                 len++;
    2227             :                 hash = partial_name_hash(c, hash);
    2228             :                 c = (unsigned char)name[len];
    2229             :         } while (c && c != '/');
    2230             :         return hashlen_create(end_name_hash(hash), len);
    2231             : }
    2232             : 
    2233             : #endif
    2234             : 
    2235             : /*
    2236             :  * Name resolution.
    2237             :  * This is the basic name resolution function, turning a pathname into
    2238             :  * the final dentry. We expect 'base' to be positive and a directory.
    2239             :  *
    2240             :  * Returns 0 and nd will have valid dentry and mnt on success.
    2241             :  * Returns error and drops reference to input namei data on failure.
    2242             :  */
    2243 12582646943 : static int link_path_walk(const char *name, struct nameidata *nd)
    2244             : {
    2245 12582646943 :         int depth = 0; // depth <= nd->depth
    2246 12582646943 :         int err;
    2247             : 
    2248 12582646943 :         nd->last_type = LAST_ROOT;
    2249 12582646943 :         nd->flags |= LOOKUP_PARENT;
    2250 12582646943 :         if (IS_ERR(name))
    2251  1289761890 :                 return PTR_ERR(name);
    2252 15804965158 :         while (*name=='/')
    2253  4512080105 :                 name++;
    2254 11292885053 :         if (!*name) {
    2255  1107038551 :                 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
    2256  1107038551 :                 return 0;
    2257             :         }
    2258             : 
    2259             :         /* At this point we know we have a real path component. */
    2260 75711505717 :         for(;;) {
    2261 75711505717 :                 struct mnt_idmap *idmap;
    2262 75711505717 :                 const char *link;
    2263 75711505717 :                 u64 hash_len;
    2264 75711505717 :                 int type;
    2265             : 
    2266 75711505717 :                 idmap = mnt_idmap(nd->path.mnt);
    2267 75658262684 :                 err = may_lookup(idmap, nd);
    2268 75645916272 :                 if (err)
    2269       27585 :                         return err;
    2270             : 
    2271 75645888687 :                 hash_len = hash_name(nd->path.dentry, name);
    2272             : 
    2273 75712301207 :                 type = LAST_NORM;
    2274 75712301207 :                 if (name[0] == '.') switch (hashlen_len(hash_len)) {
    2275   552779890 :                         case 2:
    2276   552779890 :                                 if (name[1] == '.') {
    2277   552796307 :                                         type = LAST_DOTDOT;
    2278   552796307 :                                         nd->state |= ND_JUMPED;
    2279             :                                 }
    2280             :                                 break;
    2281  1932135234 :                         case 1:
    2282  1932135234 :                                 type = LAST_DOT;
    2283             :                 }
    2284 75712301207 :                 if (likely(type == LAST_NORM)) {
    2285 73227614418 :                         struct dentry *parent = nd->path.dentry;
    2286 73227614418 :                         nd->state &= ~ND_JUMPED;
    2287 73227614418 :                         if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
    2288           0 :                                 struct qstr this = { { .hash_len = hash_len }, .name = name };
    2289           0 :                                 err = parent->d_op->d_hash(parent, &this);
    2290           0 :                                 if (err < 0)
    2291           0 :                                         return err;
    2292           0 :                                 hash_len = this.hash_len;
    2293           0 :                                 name = this.name;
    2294             :                         }
    2295             :                 }
    2296             : 
    2297 75712301207 :                 nd->last.hash_len = hash_len;
    2298 75712301207 :                 nd->last.name = name;
    2299 75712301207 :                 nd->last_type = type;
    2300             : 
    2301 75712301207 :                 name += hashlen_len(hash_len);
    2302 75712301207 :                 if (!*name)
    2303 10400820678 :                         goto OK;
    2304             :                 /*
    2305             :                  * If it wasn't NUL, we know it was '/'. Skip that
    2306             :                  * slash, and continue until no more slashes.
    2307             :                  */
    2308 65317734158 :                 do {
    2309 65317734158 :                         name++;
    2310 65317734158 :                 } while (unlikely(*name == '/'));
    2311 65311480529 :                 if (unlikely(!*name)) {
    2312     2993952 : OK:
    2313             :                         /* pathname or trailing symlink, done */
    2314 10403814630 :                         if (!depth) {
    2315 10109633614 :                                 nd->dir_vfsuid = i_uid_into_vfsuid(idmap, nd->inode);
    2316 10104525003 :                                 nd->dir_mode = nd->inode->i_mode;
    2317 10104525003 :                                 nd->flags &= ~LOOKUP_PARENT;
    2318 10104525003 :                                 return 0;
    2319             :                         }
    2320             :                         /* last component of nested symlink */
    2321   294181016 :                         name = nd->stack[--depth].name;
    2322   294181016 :                         link = walk_component(nd, 0);
    2323             :                 } else {
    2324             :                         /* not the last component */
    2325 65308486577 :                         link = walk_component(nd, WALK_MORE);
    2326             :                 }
    2327 65598137861 :                 if (unlikely(link)) {
    2328   367197841 :                         if (IS_ERR(link))
    2329    72477969 :                                 return PTR_ERR(link);
    2330             :                         /* a symlink to follow */
    2331   294719872 :                         nd->stack[depth++].name = name;
    2332   294719872 :                         name = link;
    2333   294719872 :                         continue;
    2334             :                 }
    2335 65230940020 :                 if (unlikely(!d_can_lookup(nd->path.dentry))) {
    2336         677 :                         if (nd->flags & LOOKUP_RCU) {
    2337         677 :                                 if (!try_to_unlazy(nd))
    2338             :                                         return -ECHILD;
    2339             :                         }
    2340         677 :                         return -ENOTDIR;
    2341             :                 }
    2342             :         }
    2343             : }
    2344             : 
    2345             : /* must be paired with terminate_walk() */
    2346 11137204614 : static const char *path_init(struct nameidata *nd, unsigned flags)
    2347             : {
    2348 11137204614 :         int error;
    2349 11137204614 :         const char *s = nd->name->name;
    2350             : 
    2351             :         /* LOOKUP_CACHED requires RCU, ask caller to retry */
    2352 11137204614 :         if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
    2353             :                 return ERR_PTR(-EAGAIN);
    2354             : 
    2355 11137204614 :         if (!*s)
    2356  1093952581 :                 flags &= ~LOOKUP_RCU;
    2357 11137204614 :         if (flags & LOOKUP_RCU)
    2358 10043126713 :                 rcu_read_lock();
    2359             :         else
    2360  1094077901 :                 nd->seq = nd->next_seq = 0;
    2361             : 
    2362 11133777530 :         nd->flags = flags;
    2363 11133777530 :         nd->state |= ND_JUMPED;
    2364             : 
    2365 11138789757 :         nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
    2366 11241262261 :         nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
    2367 11128679765 :         smp_rmb();
    2368             : 
    2369 11131954295 :         if (nd->state & ND_ROOT_PRESET) {
    2370       44887 :                 struct dentry *root = nd->root.dentry;
    2371       44887 :                 struct inode *inode = root->d_inode;
    2372       44887 :                 if (*s && unlikely(!d_can_lookup(root)))
    2373             :                         return ERR_PTR(-ENOTDIR);
    2374       44887 :                 nd->path = nd->root;
    2375       44887 :                 nd->inode = inode;
    2376       44887 :                 if (flags & LOOKUP_RCU) {
    2377        3443 :                         nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
    2378        3443 :                         nd->root_seq = nd->seq;
    2379             :                 } else {
    2380       41444 :                         path_get(&nd->path);
    2381             :                 }
    2382       44887 :                 return s;
    2383             :         }
    2384             : 
    2385 11131909408 :         nd->root.mnt = NULL;
    2386             : 
    2387             :         /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
    2388 11131909408 :         if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
    2389  4511091566 :                 error = nd_jump_root(nd);
    2390  4512151524 :                 if (unlikely(error))
    2391           0 :                         return ERR_PTR(error);
    2392             :                 return s;
    2393             :         }
    2394             : 
    2395             :         /* Relative pathname -- get the starting-point it is relative to. */
    2396  6620817842 :         if (nd->dfd == AT_FDCWD) {
    2397  5398229016 :                 if (flags & LOOKUP_RCU) {
    2398  5398221937 :                         struct fs_struct *fs = current->fs;
    2399             :                         unsigned seq;
    2400             : 
    2401             :                         do {
    2402  5398221937 :                                 seq = read_seqcount_begin(&fs->seq);
    2403  5400068564 :                                 nd->path = fs->pwd;
    2404  5400068564 :                                 nd->inode = nd->path.dentry->d_inode;
    2405  5400068714 :                                 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
    2406  5400198075 :                         } while (read_seqcount_retry(&fs->seq, seq));
    2407             :                 } else {
    2408        7079 :                         get_fs_pwd(current->fs, &nd->path);
    2409        7088 :                         nd->inode = nd->path.dentry->d_inode;
    2410             :                 }
    2411             :         } else {
    2412             :                 /* Caller must check execute permissions on the starting path component */
    2413  1222588826 :                 struct fd f = fdget_raw(nd->dfd);
    2414  1223012910 :                 struct dentry *dentry;
    2415             : 
    2416  1223012910 :                 if (!f.file)
    2417             :                         return ERR_PTR(-EBADF);
    2418             : 
    2419  1223012910 :                 dentry = f.file->f_path.dentry;
    2420             : 
    2421  1223012910 :                 if (*s && unlikely(!d_can_lookup(dentry))) {
    2422           0 :                         fdput(f);
    2423           0 :                         return ERR_PTR(-ENOTDIR);
    2424             :                 }
    2425             : 
    2426  1223012910 :                 nd->path = f.file->f_path;
    2427  1223012910 :                 if (flags & LOOKUP_RCU) {
    2428   129184005 :                         nd->inode = nd->path.dentry->d_inode;
    2429   129184005 :                         nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
    2430             :                 } else {
    2431  1093828905 :                         path_get(&nd->path);
    2432  1094535889 :                         nd->inode = nd->path.dentry->d_inode;
    2433             :                 }
    2434  1223720733 :                 fdput(f);
    2435             :         }
    2436             : 
    2437             :         /* For scoped-lookups we need to set the root to the dirfd as well. */
    2438  6623538485 :         if (flags & LOOKUP_IS_SCOPED) {
    2439           0 :                 nd->root = nd->path;
    2440           0 :                 if (flags & LOOKUP_RCU) {
    2441           0 :                         nd->root_seq = nd->seq;
    2442             :                 } else {
    2443           0 :                         path_get(&nd->root);
    2444           0 :                         nd->state |= ND_ROOT_GRABBED;
    2445             :                 }
    2446             :         }
    2447             :         return s;
    2448             : }
    2449             : 
    2450  9054942729 : static inline const char *lookup_last(struct nameidata *nd)
    2451             : {
    2452  9054942729 :         if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
    2453     1456704 :                 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
    2454             : 
    2455  9054942729 :         return walk_component(nd, WALK_TRAILING);
    2456             : }
    2457             : 
    2458      274921 : static int handle_lookup_down(struct nameidata *nd)
    2459             : {
    2460      274921 :         if (!(nd->flags & LOOKUP_RCU))
    2461         296 :                 dget(nd->path.dentry);
    2462      274921 :         nd->next_seq = nd->seq;
    2463      274921 :         return PTR_ERR(step_into(nd, WALK_NOFOLLOW, nd->path.dentry));
    2464             : }
    2465             : 
    2466             : /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
    2467  9112400640 : static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
    2468             : {
    2469  9112400640 :         const char *s = path_init(nd, flags);
    2470  9110260909 :         int err;
    2471             : 
    2472  9110260909 :         if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
    2473           0 :                 err = handle_lookup_down(nd);
    2474           0 :                 if (unlikely(err < 0))
    2475           0 :                         s = ERR_PTR(err);
    2476             :         }
    2477             : 
    2478 18170186361 :         while (!(err = link_path_walk(s, nd)) &&
    2479  9056707991 :                (s = lookup_last(nd)) != NULL)
    2480             :                 ;
    2481  9109218197 :         if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
    2482      275023 :                 err = handle_lookup_down(nd);
    2483      274965 :                 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
    2484             :         }
    2485  9109218139 :         if (!err)
    2486  7771211904 :                 err = complete_walk(nd);
    2487             : 
    2488  9110068556 :         if (!err && nd->flags & LOOKUP_DIRECTORY)
    2489  1044353741 :                 if (!d_can_lookup(nd->path.dentry))
    2490             :                         err = -ENOTDIR;
    2491  9110068290 :         if (!err) {
    2492  7771592767 :                 *path = nd->path;
    2493  7771592767 :                 nd->path.mnt = NULL;
    2494  7771592767 :                 nd->path.dentry = NULL;
    2495             :         }
    2496  9110068556 :         terminate_walk(nd);
    2497  9103496817 :         return err;
    2498             : }
    2499             : 
    2500  9122047411 : int filename_lookup(int dfd, struct filename *name, unsigned flags,
    2501             :                     struct path *path, struct path *root)
    2502             : {
    2503  9122047411 :         int retval;
    2504  9122047411 :         struct nameidata nd;
    2505  9122047411 :         if (IS_ERR(name))
    2506    95022503 :                 return PTR_ERR(name);
    2507  9027024908 :         set_nameidata(&nd, dfd, name, root);
    2508  9020939792 :         retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
    2509  9020447118 :         if (unlikely(retval == -ECHILD))
    2510      124033 :                 retval = path_lookupat(&nd, flags, path);
    2511  9020447538 :         if (unlikely(retval == -ESTALE))
    2512           2 :                 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
    2513             : 
    2514  9020447538 :         if (likely(!retval))
    2515             :                 audit_inode(name, path->dentry,
    2516             :                             flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
    2517  9020447538 :         restore_nameidata();
    2518  9020447538 :         return retval;
    2519             : }
    2520             : 
    2521             : /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
    2522   877937995 : static int path_parentat(struct nameidata *nd, unsigned flags,
    2523             :                                 struct path *parent)
    2524             : {
    2525   877937995 :         const char *s = path_init(nd, flags);
    2526   877908250 :         int err = link_path_walk(s, nd);
    2527   878085392 :         if (!err)
    2528   878042361 :                 err = complete_walk(nd);
    2529   878104945 :         if (!err) {
    2530   878067458 :                 *parent = nd->path;
    2531   878067458 :                 nd->path.mnt = NULL;
    2532   878067458 :                 nd->path.dentry = NULL;
    2533             :         }
    2534   878104945 :         terminate_walk(nd);
    2535   877958419 :         return err;
    2536             : }
    2537             : 
    2538             : /* Note: this does not consume "name" */
    2539   878200074 : static int __filename_parentat(int dfd, struct filename *name,
    2540             :                                unsigned int flags, struct path *parent,
    2541             :                                struct qstr *last, int *type,
    2542             :                                const struct path *root)
    2543             : {
    2544   878200074 :         int retval;
    2545   878200074 :         struct nameidata nd;
    2546             : 
    2547   878200074 :         if (IS_ERR(name))
    2548        1072 :                 return PTR_ERR(name);
    2549   878199002 :         set_nameidata(&nd, dfd, name, root);
    2550   877947330 :         retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
    2551   878004827 :         if (unlikely(retval == -ECHILD))
    2552        6597 :                 retval = path_parentat(&nd, flags, parent);
    2553   878004832 :         if (unlikely(retval == -ESTALE))
    2554           0 :                 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
    2555   878004832 :         if (likely(!retval)) {
    2556   877953811 :                 *last = nd.last;
    2557   877953811 :                 *type = nd.last_type;
    2558   877953811 :                 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
    2559             :         }
    2560   878004832 :         restore_nameidata();
    2561   878004832 :         return retval;
    2562             : }
    2563             : 
    2564             : static int filename_parentat(int dfd, struct filename *name,
    2565             :                              unsigned int flags, struct path *parent,
    2566             :                              struct qstr *last, int *type)
    2567             : {
    2568   878147860 :         return __filename_parentat(dfd, name, flags, parent, last, type, NULL);
    2569             : }
    2570             : 
    2571             : /* does lookup, returns the object with parent locked */
    2572        3429 : static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
    2573             : {
    2574        3429 :         struct dentry *d;
    2575        3429 :         struct qstr last;
    2576        3429 :         int type, error;
    2577             : 
    2578        3429 :         error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
    2579        3429 :         if (error)
    2580           0 :                 return ERR_PTR(error);
    2581        3429 :         if (unlikely(type != LAST_NORM)) {
    2582           0 :                 path_put(path);
    2583           0 :                 return ERR_PTR(-EINVAL);
    2584             :         }
    2585        3429 :         inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
    2586        3429 :         d = lookup_one_qstr_excl(&last, path->dentry, 0);
    2587        3429 :         if (IS_ERR(d)) {
    2588           0 :                 inode_unlock(path->dentry->d_inode);
    2589           0 :                 path_put(path);
    2590             :         }
    2591             :         return d;
    2592             : }
    2593             : 
    2594        3429 : struct dentry *kern_path_locked(const char *name, struct path *path)
    2595             : {
    2596        3429 :         struct filename *filename = getname_kernel(name);
    2597        3429 :         struct dentry *res = __kern_path_locked(filename, path);
    2598             : 
    2599        3429 :         putname(filename);
    2600        3429 :         return res;
    2601             : }
    2602             : 
    2603    16837420 : int kern_path(const char *name, unsigned int flags, struct path *path)
    2604             : {
    2605    16837420 :         struct filename *filename = getname_kernel(name);
    2606    16833620 :         int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL);
    2607             : 
    2608    16838365 :         putname(filename);
    2609    16837876 :         return ret;
    2610             : 
    2611             : }
    2612             : EXPORT_SYMBOL(kern_path);
    2613             : 
    2614             : /**
    2615             :  * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair
    2616             :  * @filename: filename structure
    2617             :  * @flags: lookup flags
    2618             :  * @parent: pointer to struct path to fill
    2619             :  * @last: last component
    2620             :  * @type: type of the last component
    2621             :  * @root: pointer to struct path of the base directory
    2622             :  */
    2623           0 : int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
    2624             :                            struct path *parent, struct qstr *last, int *type,
    2625             :                            const struct path *root)
    2626             : {
    2627           0 :         return  __filename_parentat(AT_FDCWD, filename, flags, parent, last,
    2628             :                                     type, root);
    2629             : }
    2630             : EXPORT_SYMBOL(vfs_path_parent_lookup);
    2631             : 
    2632             : /**
    2633             :  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
    2634             :  * @dentry:  pointer to dentry of the base directory
    2635             :  * @mnt: pointer to vfs mount of the base directory
    2636             :  * @name: pointer to file name
    2637             :  * @flags: lookup flags
    2638             :  * @path: pointer to struct path to fill
    2639             :  */
    2640        3443 : int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
    2641             :                     const char *name, unsigned int flags,
    2642             :                     struct path *path)
    2643             : {
    2644        3443 :         struct filename *filename;
    2645        3443 :         struct path root = {.mnt = mnt, .dentry = dentry};
    2646        3443 :         int ret;
    2647             : 
    2648        3443 :         filename = getname_kernel(name);
    2649             :         /* the first argument of filename_lookup() is ignored with root */
    2650        3443 :         ret = filename_lookup(AT_FDCWD, filename, flags, path, &root);
    2651        3443 :         putname(filename);
    2652        3443 :         return ret;
    2653             : }
    2654             : EXPORT_SYMBOL(vfs_path_lookup);
    2655             : 
    2656    11651008 : static int lookup_one_common(struct mnt_idmap *idmap,
    2657             :                              const char *name, struct dentry *base, int len,
    2658             :                              struct qstr *this)
    2659             : {
    2660    11651008 :         this->name = name;
    2661    11651008 :         this->len = len;
    2662    11651008 :         this->hash = full_name_hash(base, name, len);
    2663    11651008 :         if (!len)
    2664             :                 return -EACCES;
    2665             : 
    2666    11651008 :         if (unlikely(name[0] == '.')) {
    2667        5154 :                 if (len < 2 || (len == 2 && name[1] == '.'))
    2668             :                         return -EACCES;
    2669             :         }
    2670             : 
    2671   111614380 :         while (len--) {
    2672    99963372 :                 unsigned int c = *(const unsigned char *)name++;
    2673    99963372 :                 if (c == '/' || c == '\0')
    2674             :                         return -EACCES;
    2675             :         }
    2676             :         /*
    2677             :          * See if the low-level filesystem might want
    2678             :          * to use its own hash..
    2679             :          */
    2680    11651008 :         if (base->d_flags & DCACHE_OP_HASH) {
    2681           0 :                 int err = base->d_op->d_hash(base, this);
    2682           0 :                 if (err < 0)
    2683             :                         return err;
    2684             :         }
    2685             : 
    2686    11651008 :         return inode_permission(idmap, base->d_inode, MAY_EXEC);
    2687             : }
    2688             : 
    2689             : /**
    2690             :  * try_lookup_one_len - filesystem helper to lookup single pathname component
    2691             :  * @name:       pathname component to lookup
    2692             :  * @base:       base directory to lookup from
    2693             :  * @len:        maximum length @len should be interpreted to
    2694             :  *
    2695             :  * Look up a dentry by name in the dcache, returning NULL if it does not
    2696             :  * currently exist.  The function does not try to create a dentry.
    2697             :  *
    2698             :  * Note that this routine is purely a helper for filesystem usage and should
    2699             :  * not be called by generic code.
    2700             :  *
    2701             :  * The caller must hold base->i_mutex.
    2702             :  */
    2703           0 : struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
    2704             : {
    2705           0 :         struct qstr this;
    2706           0 :         int err;
    2707             : 
    2708           0 :         WARN_ON_ONCE(!inode_is_locked(base->d_inode));
    2709             : 
    2710           0 :         err = lookup_one_common(&nop_mnt_idmap, name, base, len, &this);
    2711           0 :         if (err)
    2712           0 :                 return ERR_PTR(err);
    2713             : 
    2714           0 :         return lookup_dcache(&this, base, 0);
    2715             : }
    2716             : EXPORT_SYMBOL(try_lookup_one_len);
    2717             : 
    2718             : /**
    2719             :  * lookup_one_len - filesystem helper to lookup single pathname component
    2720             :  * @name:       pathname component to lookup
    2721             :  * @base:       base directory to lookup from
    2722             :  * @len:        maximum length @len should be interpreted to
    2723             :  *
    2724             :  * Note that this routine is purely a helper for filesystem usage and should
    2725             :  * not be called by generic code.
    2726             :  *
    2727             :  * The caller must hold base->i_mutex.
    2728             :  */
    2729     7759030 : struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
    2730             : {
    2731     7759030 :         struct dentry *dentry;
    2732     7759030 :         struct qstr this;
    2733     7759030 :         int err;
    2734             : 
    2735     7759030 :         WARN_ON_ONCE(!inode_is_locked(base->d_inode));
    2736             : 
    2737     7759030 :         err = lookup_one_common(&nop_mnt_idmap, name, base, len, &this);
    2738     7759030 :         if (err)
    2739           0 :                 return ERR_PTR(err);
    2740             : 
    2741     7759030 :         dentry = lookup_dcache(&this, base, 0);
    2742     7759030 :         return dentry ? dentry : __lookup_slow(&this, base, 0);
    2743             : }
    2744             : EXPORT_SYMBOL(lookup_one_len);
    2745             : 
    2746             : /**
    2747             :  * lookup_one - filesystem helper to lookup single pathname component
    2748             :  * @idmap:      idmap of the mount the lookup is performed from
    2749             :  * @name:       pathname component to lookup
    2750             :  * @base:       base directory to lookup from
    2751             :  * @len:        maximum length @len should be interpreted to
    2752             :  *
    2753             :  * Note that this routine is purely a helper for filesystem usage and should
    2754             :  * not be called by generic code.
    2755             :  *
    2756             :  * The caller must hold base->i_mutex.
    2757             :  */
    2758      780356 : struct dentry *lookup_one(struct mnt_idmap *idmap, const char *name,
    2759             :                           struct dentry *base, int len)
    2760             : {
    2761      780356 :         struct dentry *dentry;
    2762      780356 :         struct qstr this;
    2763      780356 :         int err;
    2764             : 
    2765      780356 :         WARN_ON_ONCE(!inode_is_locked(base->d_inode));
    2766             : 
    2767      780356 :         err = lookup_one_common(idmap, name, base, len, &this);
    2768      780295 :         if (err)
    2769           0 :                 return ERR_PTR(err);
    2770             : 
    2771      780295 :         dentry = lookup_dcache(&this, base, 0);
    2772      780471 :         return dentry ? dentry : __lookup_slow(&this, base, 0);
    2773             : }
    2774             : EXPORT_SYMBOL(lookup_one);
    2775             : 
    2776             : /**
    2777             :  * lookup_one_unlocked - filesystem helper to lookup single pathname component
    2778             :  * @idmap:      idmap of the mount the lookup is performed from
    2779             :  * @name:       pathname component to lookup
    2780             :  * @base:       base directory to lookup from
    2781             :  * @len:        maximum length @len should be interpreted to
    2782             :  *
    2783             :  * Note that this routine is purely a helper for filesystem usage and should
    2784             :  * not be called by generic code.
    2785             :  *
    2786             :  * Unlike lookup_one_len, it should be called without the parent
    2787             :  * i_mutex held, and will take the i_mutex itself if necessary.
    2788             :  */
    2789     3119716 : struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
    2790             :                                    const char *name, struct dentry *base,
    2791             :                                    int len)
    2792             : {
    2793     3119716 :         struct qstr this;
    2794     3119716 :         int err;
    2795     3119716 :         struct dentry *ret;
    2796             : 
    2797     3119716 :         err = lookup_one_common(idmap, name, base, len, &this);
    2798     3117427 :         if (err)
    2799           0 :                 return ERR_PTR(err);
    2800             : 
    2801     3117427 :         ret = lookup_dcache(&this, base, 0);
    2802     3124548 :         if (!ret)
    2803     2852977 :                 ret = lookup_slow(&this, base, 0);
    2804             :         return ret;
    2805             : }
    2806             : EXPORT_SYMBOL(lookup_one_unlocked);
    2807             : 
    2808             : /**
    2809             :  * lookup_one_positive_unlocked - filesystem helper to lookup single
    2810             :  *                                pathname component
    2811             :  * @idmap:      idmap of the mount the lookup is performed from
    2812             :  * @name:       pathname component to lookup
    2813             :  * @base:       base directory to lookup from
    2814             :  * @len:        maximum length @len should be interpreted to
    2815             :  *
    2816             :  * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
    2817             :  * known positive or ERR_PTR(). This is what most of the users want.
    2818             :  *
    2819             :  * Note that pinned negative with unlocked parent _can_ become positive at any
    2820             :  * time, so callers of lookup_one_unlocked() need to be very careful; pinned
    2821             :  * positives have >d_inode stable, so this one avoids such problems.
    2822             :  *
    2823             :  * Note that this routine is purely a helper for filesystem usage and should
    2824             :  * not be called by generic code.
    2825             :  *
    2826             :  * The helper should be called without i_mutex held.
    2827             :  */
    2828       73855 : struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
    2829             :                                             const char *name,
    2830             :                                             struct dentry *base, int len)
    2831             : {
    2832       73855 :         struct dentry *ret = lookup_one_unlocked(idmap, name, base, len);
    2833             : 
    2834       73897 :         if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
    2835       22705 :                 dput(ret);
    2836       22705 :                 ret = ERR_PTR(-ENOENT);
    2837             :         }
    2838       73897 :         return ret;
    2839             : }
    2840             : EXPORT_SYMBOL(lookup_one_positive_unlocked);
    2841             : 
    2842             : /**
    2843             :  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
    2844             :  * @name:       pathname component to lookup
    2845             :  * @base:       base directory to lookup from
    2846             :  * @len:        maximum length @len should be interpreted to
    2847             :  *
    2848             :  * Note that this routine is purely a helper for filesystem usage and should
    2849             :  * not be called by generic code.
    2850             :  *
    2851             :  * Unlike lookup_one_len, it should be called without the parent
    2852             :  * i_mutex held, and will take the i_mutex itself if necessary.
    2853             :  */
    2854           0 : struct dentry *lookup_one_len_unlocked(const char *name,
    2855             :                                        struct dentry *base, int len)
    2856             : {
    2857           0 :         return lookup_one_unlocked(&nop_mnt_idmap, name, base, len);
    2858             : }
    2859             : EXPORT_SYMBOL(lookup_one_len_unlocked);
    2860             : 
    2861             : /*
    2862             :  * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
    2863             :  * on negatives.  Returns known positive or ERR_PTR(); that's what
    2864             :  * most of the users want.  Note that pinned negative with unlocked parent
    2865             :  * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
    2866             :  * need to be very careful; pinned positives have ->d_inode stable, so
    2867             :  * this one avoids such problems.
    2868             :  */
    2869        3755 : struct dentry *lookup_positive_unlocked(const char *name,
    2870             :                                        struct dentry *base, int len)
    2871             : {
    2872        3755 :         return lookup_one_positive_unlocked(&nop_mnt_idmap, name, base, len);
    2873             : }
    2874             : EXPORT_SYMBOL(lookup_positive_unlocked);
    2875             : 
    2876             : #ifdef CONFIG_UNIX98_PTYS
    2877           2 : int path_pts(struct path *path)
    2878             : {
    2879             :         /* Find something mounted on "pts" in the same directory as
    2880             :          * the input path.
    2881             :          */
    2882           2 :         struct dentry *parent = dget_parent(path->dentry);
    2883           2 :         struct dentry *child;
    2884           2 :         struct qstr this = QSTR_INIT("pts", 3);
    2885             : 
    2886           2 :         if (unlikely(!path_connected(path->mnt, parent))) {
    2887           0 :                 dput(parent);
    2888           0 :                 return -ENOENT;
    2889             :         }
    2890           2 :         dput(path->dentry);
    2891           2 :         path->dentry = parent;
    2892           2 :         child = d_hash_and_lookup(parent, &this);
    2893           2 :         if (!child)
    2894             :                 return -ENOENT;
    2895             : 
    2896           2 :         path->dentry = child;
    2897           2 :         dput(parent);
    2898           2 :         follow_down(path, 0);
    2899           2 :         return 0;
    2900             : }
    2901             : #endif
    2902             : 
    2903  5451053753 : int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
    2904             :                  struct path *path, int *empty)
    2905             : {
    2906  5451053753 :         struct filename *filename = getname_flags(name, flags, empty);
    2907  5449300827 :         int ret = filename_lookup(dfd, filename, flags, path, NULL);
    2908             : 
    2909  5446345748 :         putname(filename);
    2910  5447025738 :         return ret;
    2911             : }
    2912             : EXPORT_SYMBOL(user_path_at_empty);
    2913             : 
    2914      375898 : int __check_sticky(struct mnt_idmap *idmap, struct inode *dir,
    2915             :                    struct inode *inode)
    2916             : {
    2917      375898 :         kuid_t fsuid = current_fsuid();
    2918             : 
    2919      751796 :         if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), fsuid))
    2920             :                 return 0;
    2921        1222 :         if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, dir), fsuid))
    2922             :                 return 0;
    2923         481 :         return !capable_wrt_inode_uidgid(idmap, inode, CAP_FOWNER);
    2924             : }
    2925             : EXPORT_SYMBOL(__check_sticky);
    2926             : 
    2927             : /*
    2928             :  *      Check whether we can remove a link victim from directory dir, check
    2929             :  *  whether the type of victim is right.
    2930             :  *  1. We can't do it if dir is read-only (done in permission())
    2931             :  *  2. We should have write and exec permissions on dir
    2932             :  *  3. We can't remove anything from append-only dir
    2933             :  *  4. We can't do anything with immutable dir (done in permission())
    2934             :  *  5. If the sticky bit on dir is set we should either
    2935             :  *      a. be owner of dir, or
    2936             :  *      b. be owner of victim, or
    2937             :  *      c. have CAP_FOWNER capability
    2938             :  *  6. If the victim is append-only or immutable we can't do antyhing with
    2939             :  *     links pointing to it.
    2940             :  *  7. If the victim has an unknown uid or gid we can't change the inode.
    2941             :  *  8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
    2942             :  *  9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
    2943             :  * 10. We can't remove a root or mountpoint.
    2944             :  * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
    2945             :  *     nfs_async_unlink().
    2946             :  */
    2947   148208570 : static int may_delete(struct mnt_idmap *idmap, struct inode *dir,
    2948             :                       struct dentry *victim, bool isdir)
    2949             : {
    2950   148208570 :         struct inode *inode = d_backing_inode(victim);
    2951   148208570 :         int error;
    2952             : 
    2953   148208570 :         if (d_is_negative(victim))
    2954             :                 return -ENOENT;
    2955   148208570 :         BUG_ON(!inode);
    2956             : 
    2957   148208570 :         BUG_ON(victim->d_parent->d_inode != dir);
    2958             : 
    2959             :         /* Inode writeback is not safe when the uid or gid are invalid. */
    2960   296416160 :         if (!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) ||
    2961             :             !vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))
    2962           0 :                 return -EOVERFLOW;
    2963             : 
    2964   148207590 :         audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
    2965             : 
    2966   148207590 :         error = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
    2967   148176331 :         if (error)
    2968             :                 return error;
    2969   148176226 :         if (IS_APPEND(dir))
    2970             :                 return -EPERM;
    2971             : 
    2972   148176031 :         if (check_sticky(idmap, dir, inode) || IS_APPEND(inode) ||
    2973   296323773 :             IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
    2974   148155550 :             HAS_UNMAPPED_ID(idmap, inode))
    2975         476 :                 return -EPERM;
    2976   148168059 :         if (isdir) {
    2977    25144499 :                 if (!d_is_dir(victim))
    2978             :                         return -ENOTDIR;
    2979    25065058 :                 if (IS_ROOT(victim))
    2980             :                         return -EBUSY;
    2981   246104852 :         } else if (d_is_dir(victim))
    2982             :                 return -EISDIR;
    2983   148106627 :         if (IS_DEADDIR(dir))
    2984             :                 return -ENOENT;
    2985   148106627 :         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
    2986           0 :                 return -EBUSY;
    2987             :         return 0;
    2988             : }
    2989             : 
    2990             : /*      Check whether we can create an object with dentry child in directory
    2991             :  *  dir.
    2992             :  *  1. We can't do it if child already exists (open has special treatment for
    2993             :  *     this case, but since we are inlined it's OK)
    2994             :  *  2. We can't do it if dir is read-only (done in permission())
    2995             :  *  3. We can't do it if the fs can't represent the fsuid or fsgid.
    2996             :  *  4. We should have write and exec permissions on dir
    2997             :  *  5. We can't do it if dir is immutable (done in permission())
    2998             :  */
    2999   720214423 : static inline int may_create(struct mnt_idmap *idmap,
    3000             :                              struct inode *dir, struct dentry *child)
    3001             : {
    3002   720214423 :         audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
    3003   720214423 :         if (child->d_inode)
    3004             :                 return -EEXIST;
    3005   720214423 :         if (IS_DEADDIR(dir))
    3006             :                 return -ENOENT;
    3007   720214423 :         if (!fsuidgid_has_mapping(dir->i_sb, idmap))
    3008             :                 return -EOVERFLOW;
    3009             : 
    3010   720390610 :         return inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
    3011             : }
    3012             : 
    3013    49739309 : static struct dentry *lock_two_directories(struct dentry *p1, struct dentry *p2)
    3014             : {
    3015    49739309 :         struct dentry *p;
    3016             : 
    3017    49739309 :         p = d_ancestor(p2, p1);
    3018    49739309 :         if (p) {
    3019     2603176 :                 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
    3020     2603176 :                 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
    3021     2603176 :                 return p;
    3022             :         }
    3023             : 
    3024    47136133 :         p = d_ancestor(p1, p2);
    3025    47136133 :         if (p) {
    3026     2638987 :                 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
    3027     2638987 :                 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
    3028     2638987 :                 return p;
    3029             :         }
    3030             : 
    3031    44497146 :         lock_two_inodes(p1->d_inode, p2->d_inode,
    3032             :                         I_MUTEX_PARENT, I_MUTEX_PARENT2);
    3033    44497146 :         return NULL;
    3034             : }
    3035             : 
    3036             : /*
    3037             :  * p1 and p2 should be directories on the same fs.
    3038             :  */
    3039    57182966 : struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
    3040             : {
    3041    57182966 :         if (p1 == p2) {
    3042     7450282 :                 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
    3043     7450282 :                 return NULL;
    3044             :         }
    3045             : 
    3046    49732684 :         mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
    3047    49739309 :         return lock_two_directories(p1, p2);
    3048             : }
    3049             : EXPORT_SYMBOL(lock_rename);
    3050             : 
    3051             : /*
    3052             :  * c1 and p2 should be on the same fs.
    3053             :  */
    3054           0 : struct dentry *lock_rename_child(struct dentry *c1, struct dentry *p2)
    3055             : {
    3056           0 :         if (READ_ONCE(c1->d_parent) == p2) {
    3057             :                 /*
    3058             :                  * hopefully won't need to touch ->s_vfs_rename_mutex at all.
    3059             :                  */
    3060           0 :                 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
    3061             :                 /*
    3062             :                  * now that p2 is locked, nobody can move in or out of it,
    3063             :                  * so the test below is safe.
    3064             :                  */
    3065           0 :                 if (likely(c1->d_parent == p2))
    3066             :                         return NULL;
    3067             : 
    3068             :                 /*
    3069             :                  * c1 got moved out of p2 while we'd been taking locks;
    3070             :                  * unlock and fall back to slow case.
    3071             :                  */
    3072           0 :                 inode_unlock(p2->d_inode);
    3073             :         }
    3074             : 
    3075           0 :         mutex_lock(&c1->d_sb->s_vfs_rename_mutex);
    3076             :         /*
    3077             :          * nobody can move out of any directories on this fs.
    3078             :          */
    3079           0 :         if (likely(c1->d_parent != p2))
    3080           0 :                 return lock_two_directories(c1->d_parent, p2);
    3081             : 
    3082             :         /*
    3083             :          * c1 got moved into p2 while we were taking locks;
    3084             :          * we need p2 locked and ->s_vfs_rename_mutex unlocked,
    3085             :          * for consistency with lock_rename().
    3086             :          */
    3087           0 :         inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
    3088           0 :         mutex_unlock(&c1->d_sb->s_vfs_rename_mutex);
    3089           0 :         return NULL;
    3090             : }
    3091             : EXPORT_SYMBOL(lock_rename_child);
    3092             : 
    3093    57190210 : void unlock_rename(struct dentry *p1, struct dentry *p2)
    3094             : {
    3095    57190210 :         inode_unlock(p1->d_inode);
    3096    57189457 :         if (p1 != p2) {
    3097    49739309 :                 inode_unlock(p2->d_inode);
    3098    49739309 :                 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
    3099             :         }
    3100    57189260 : }
    3101             : EXPORT_SYMBOL(unlock_rename);
    3102             : 
    3103             : /**
    3104             :  * mode_strip_umask - handle vfs umask stripping
    3105             :  * @dir:        parent directory of the new inode
    3106             :  * @mode:       mode of the new inode to be created in @dir
    3107             :  *
    3108             :  * Umask stripping depends on whether or not the filesystem supports POSIX
    3109             :  * ACLs. If the filesystem doesn't support it umask stripping is done directly
    3110             :  * in here. If the filesystem does support POSIX ACLs umask stripping is
    3111             :  * deferred until the filesystem calls posix_acl_create().
    3112             :  *
    3113             :  * Returns: mode
    3114             :  */
    3115   114912884 : static inline umode_t mode_strip_umask(const struct inode *dir, umode_t mode)
    3116             : {
    3117   114912884 :         if (!IS_POSIXACL(dir))
    3118      209634 :                 mode &= ~current_umask();
    3119   114912884 :         return mode;
    3120             : }
    3121             : 
    3122             : /**
    3123             :  * vfs_prepare_mode - prepare the mode to be used for a new inode
    3124             :  * @idmap:      idmap of the mount the inode was found from
    3125             :  * @dir:        parent directory of the new inode
    3126             :  * @mode:       mode of the new inode
    3127             :  * @mask_perms: allowed permission by the vfs
    3128             :  * @type:       type of file to be created
    3129             :  *
    3130             :  * This helper consolidates and enforces vfs restrictions on the @mode of a new
    3131             :  * object to be created.
    3132             :  *
    3133             :  * Umask stripping depends on whether the filesystem supports POSIX ACLs (see
    3134             :  * the kernel documentation for mode_strip_umask()). Moving umask stripping
    3135             :  * after setgid stripping allows the same ordering for both non-POSIX ACL and
    3136             :  * POSIX ACL supporting filesystems.
    3137             :  *
    3138             :  * Note that it's currently valid for @type to be 0 if a directory is created.
    3139             :  * Filesystems raise that flag individually and we need to check whether each
    3140             :  * filesystem can deal with receiving S_IFDIR from the vfs before we enforce a
    3141             :  * non-zero type.
    3142             :  *
    3143             :  * Returns: mode to be passed to the filesystem
    3144             :  */
    3145    89746208 : static inline umode_t vfs_prepare_mode(struct mnt_idmap *idmap,
    3146             :                                        const struct inode *dir, umode_t mode,
    3147             :                                        umode_t mask_perms, umode_t type)
    3148             : {
    3149    89746208 :         mode = mode_strip_sgid(idmap, dir, mode);
    3150    89059122 :         mode = mode_strip_umask(dir, mode);
    3151             : 
    3152             :         /*
    3153             :          * Apply the vfs mandated allowed permission mask and set the type of
    3154             :          * file to be created before we call into the filesystem.
    3155             :          */
    3156    89121193 :         mode &= (mask_perms & ~S_IFMT);
    3157    89121193 :         mode |= (type & S_IFMT);
    3158             : 
    3159    89121193 :         return mode;
    3160             : }
    3161             : 
    3162             : /**
    3163             :  * vfs_create - create new file
    3164             :  * @idmap:      idmap of the mount the inode was found from
    3165             :  * @dir:        inode of @dentry
    3166             :  * @dentry:     pointer to dentry of the base directory
    3167             :  * @mode:       mode of the new file
    3168             :  * @want_excl:  whether the file must not yet exist
    3169             :  *
    3170             :  * Create a new file.
    3171             :  *
    3172             :  * If the inode has been found through an idmapped mount the idmap of
    3173             :  * the vfsmount must be passed through @idmap. This function will then take
    3174             :  * care to map the inode according to @idmap before checking permissions.
    3175             :  * On non-idmapped mounts or if permission checking is to be performed on the
    3176             :  * raw inode simply passs @nop_mnt_idmap.
    3177             :  */
    3178      286378 : int vfs_create(struct mnt_idmap *idmap, struct inode *dir,
    3179             :                struct dentry *dentry, umode_t mode, bool want_excl)
    3180             : {
    3181      286378 :         int error;
    3182             : 
    3183      286378 :         error = may_create(idmap, dir, dentry);
    3184      286431 :         if (error)
    3185             :                 return error;
    3186             : 
    3187      286418 :         if (!dir->i_op->create)
    3188             :                 return -EACCES; /* shouldn't it be ENOSYS? */
    3189             : 
    3190      286418 :         mode = vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);
    3191      286393 :         error = security_inode_create(dir, dentry, mode);
    3192      286393 :         if (error)
    3193             :                 return error;
    3194      286393 :         error = dir->i_op->create(idmap, dir, dentry, mode, want_excl);
    3195      286443 :         if (!error)
    3196      286435 :                 fsnotify_create(dir, dentry);
    3197             :         return error;
    3198             : }
    3199             : EXPORT_SYMBOL(vfs_create);
    3200             : 
    3201           0 : int vfs_mkobj(struct dentry *dentry, umode_t mode,
    3202             :                 int (*f)(struct dentry *, umode_t, void *),
    3203             :                 void *arg)
    3204             : {
    3205           0 :         struct inode *dir = dentry->d_parent->d_inode;
    3206           0 :         int error = may_create(&nop_mnt_idmap, dir, dentry);
    3207           0 :         if (error)
    3208             :                 return error;
    3209             : 
    3210           0 :         mode &= S_IALLUGO;
    3211           0 :         mode |= S_IFREG;
    3212           0 :         error = security_inode_create(dir, dentry, mode);
    3213           0 :         if (error)
    3214             :                 return error;
    3215           0 :         error = f(dentry, mode, arg);
    3216           0 :         if (!error)
    3217           0 :                 fsnotify_create(dir, dentry);
    3218             :         return error;
    3219             : }
    3220             : EXPORT_SYMBOL(vfs_mkobj);
    3221             : 
    3222    16421491 : bool may_open_dev(const struct path *path)
    3223             : {
    3224    16421491 :         return !(path->mnt->mnt_flags & MNT_NODEV) &&
    3225    16421491 :                 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
    3226             : }
    3227             : 
    3228  1130773883 : static int may_open(struct mnt_idmap *idmap, const struct path *path,
    3229             :                     int acc_mode, int flag)
    3230             : {
    3231  1130773883 :         struct dentry *dentry = path->dentry;
    3232  1130773883 :         struct inode *inode = dentry->d_inode;
    3233  1130773883 :         int error;
    3234             : 
    3235  1130773883 :         if (!inode)
    3236             :                 return -ENOENT;
    3237             : 
    3238  1130773883 :         switch (inode->i_mode & S_IFMT) {
    3239             :         case S_IFLNK:
    3240             :                 return -ELOOP;
    3241    38493026 :         case S_IFDIR:
    3242    38493026 :                 if (acc_mode & MAY_WRITE)
    3243             :                         return -EISDIR;
    3244    35255801 :                 if (acc_mode & MAY_EXEC)
    3245             :                         return -EACCES;
    3246             :                 break;
    3247    14537669 :         case S_IFBLK:
    3248             :         case S_IFCHR:
    3249    14537669 :                 if (!may_open_dev(path))
    3250             :                         return -EACCES;
    3251    15630668 :                 fallthrough;
    3252             :         case S_IFIFO:
    3253             :         case S_IFSOCK:
    3254    15630668 :                 if (acc_mode & MAY_EXEC)
    3255             :                         return -EACCES;
    3256    15630668 :                 flag &= ~O_TRUNC;
    3257    15630668 :                 break;
    3258  1076650189 :         case S_IFREG:
    3259  1076650189 :                 if ((acc_mode & MAY_EXEC) && path_noexec(path))
    3260             :                         return -EACCES;
    3261             :                 break;
    3262             :         }
    3263             : 
    3264  1127523161 :         error = inode_permission(idmap, inode, MAY_OPEN | acc_mode);
    3265  1127371016 :         if (error)
    3266             :                 return error;
    3267             : 
    3268             :         /*
    3269             :          * An append-only file must be opened in append mode for writing.
    3270             :          */
    3271  1127307516 :         if (IS_APPEND(inode)) {
    3272        1113 :                 if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
    3273             :                         return -EPERM;
    3274        1009 :                 if (flag & O_TRUNC)
    3275             :                         return -EPERM;
    3276             :         }
    3277             : 
    3278             :         /* O_NOATIME can only be set by the owner or superuser */
    3279  1127307360 :         if (flag & O_NOATIME && !inode_owner_or_capable(idmap, inode))
    3280           0 :                 return -EPERM;
    3281             : 
    3282             :         return 0;
    3283             : }
    3284             : 
    3285     1428134 : static int handle_truncate(struct mnt_idmap *idmap, struct file *filp)
    3286             : {
    3287     1428134 :         const struct path *path = &filp->f_path;
    3288     1428134 :         struct inode *inode = path->dentry->d_inode;
    3289     1428134 :         int error = get_write_access(inode);
    3290     1428466 :         if (error)
    3291             :                 return error;
    3292             : 
    3293     1428474 :         error = security_file_truncate(filp);
    3294     1428474 :         if (!error) {
    3295     1428474 :                 error = do_truncate(idmap, path->dentry, 0,
    3296             :                                     ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
    3297             :                                     filp);
    3298             :         }
    3299     1428322 :         put_write_access(inode);
    3300     1428322 :         return error;
    3301             : }
    3302             : 
    3303             : static inline int open_to_namei_flags(int flag)
    3304             : {
    3305       79394 :         if ((flag & O_ACCMODE) == 3)
    3306           0 :                 flag--;
    3307       79394 :         return flag;
    3308             : }
    3309             : 
    3310    54788230 : static int may_o_create(struct mnt_idmap *idmap,
    3311             :                         const struct path *dir, struct dentry *dentry,
    3312             :                         umode_t mode)
    3313             : {
    3314    54788230 :         int error = security_path_mknod(dir, dentry, mode, 0);
    3315    54788230 :         if (error)
    3316             :                 return error;
    3317             : 
    3318    54788230 :         if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap))
    3319             :                 return -EOVERFLOW;
    3320             : 
    3321    55005722 :         error = inode_permission(idmap, dir->dentry->d_inode,
    3322             :                                  MAY_WRITE | MAY_EXEC);
    3323    54986803 :         if (error)
    3324         413 :                 return error;
    3325             : 
    3326             :         return security_inode_create(dir->dentry->d_inode, dentry, mode);
    3327             : }
    3328             : 
    3329             : /*
    3330             :  * Attempt to atomically look up, create and open a file from a negative
    3331             :  * dentry.
    3332             :  *
    3333             :  * Returns 0 if successful.  The file will have been created and attached to
    3334             :  * @file by the filesystem calling finish_open().
    3335             :  *
    3336             :  * If the file was looked up only or didn't need creating, FMODE_OPENED won't
    3337             :  * be set.  The caller will need to perform the open themselves.  @path will
    3338             :  * have been updated to point to the new dentry.  This may be negative.
    3339             :  *
    3340             :  * Returns an error code otherwise.
    3341             :  */
    3342       79394 : static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
    3343             :                                   struct file *file,
    3344             :                                   int open_flag, umode_t mode)
    3345             : {
    3346       79394 :         struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
    3347       79394 :         struct inode *dir =  nd->path.dentry->d_inode;
    3348       79394 :         int error;
    3349             : 
    3350       79394 :         if (nd->flags & LOOKUP_DIRECTORY)
    3351         211 :                 open_flag |= O_DIRECTORY;
    3352             : 
    3353       79394 :         file->f_path.dentry = DENTRY_NOT_SET;
    3354       79394 :         file->f_path.mnt = nd->path.mnt;
    3355       79394 :         error = dir->i_op->atomic_open(dir, dentry, file,
    3356             :                                        open_to_namei_flags(open_flag), mode);
    3357       79392 :         d_lookup_done(dentry);
    3358       79389 :         if (!error) {
    3359       68914 :                 if (file->f_mode & FMODE_OPENED) {
    3360       38196 :                         if (unlikely(dentry != file->f_path.dentry)) {
    3361           0 :                                 dput(dentry);
    3362           0 :                                 dentry = dget(file->f_path.dentry);
    3363             :                         }
    3364       30718 :                 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
    3365             :                         error = -EIO;
    3366             :                 } else {
    3367       30718 :                         if (file->f_path.dentry) {
    3368           0 :                                 dput(dentry);
    3369           0 :                                 dentry = file->f_path.dentry;
    3370             :                         }
    3371       30718 :                         if (unlikely(d_is_negative(dentry)))
    3372             :                                 error = -ENOENT;
    3373             :                 }
    3374             :         }
    3375       79374 :         if (error) {
    3376       10491 :                 dput(dentry);
    3377       10491 :                 dentry = ERR_PTR(error);
    3378             :         }
    3379       79389 :         return dentry;
    3380             : }
    3381             : 
    3382             : /*
    3383             :  * Look up and maybe create and open the last component.
    3384             :  *
    3385             :  * Must be called with parent locked (exclusive in O_CREAT case).
    3386             :  *
    3387             :  * Returns 0 on success, that is, if
    3388             :  *  the file was successfully atomically created (if necessary) and opened, or
    3389             :  *  the file was not completely opened at this time, though lookups and
    3390             :  *  creations were performed.
    3391             :  * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
    3392             :  * In the latter case dentry returned in @path might be negative if O_CREAT
    3393             :  * hadn't been specified.
    3394             :  *
    3395             :  * An error code is returned on failure.
    3396             :  */
    3397   106649478 : static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
    3398             :                                   const struct open_flags *op,
    3399             :                                   bool got_write)
    3400             : {
    3401   106649478 :         struct mnt_idmap *idmap;
    3402   106649478 :         struct dentry *dir = nd->path.dentry;
    3403   106649478 :         struct inode *dir_inode = dir->d_inode;
    3404   106649478 :         int open_flag = op->open_flag;
    3405   106649478 :         struct dentry *dentry;
    3406   106649478 :         int error, create_error = 0;
    3407   106649478 :         umode_t mode = op->mode;
    3408   106649478 :         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
    3409             : 
    3410   106649478 :         if (unlikely(IS_DEADDIR(dir_inode)))
    3411             :                 return ERR_PTR(-ENOENT);
    3412             : 
    3413   106649471 :         file->f_mode &= ~FMODE_CREATED;
    3414   106649471 :         dentry = d_lookup(dir, &nd->last);
    3415   106838524 :         for (;;) {
    3416   106838524 :                 if (!dentry) {
    3417    84229277 :                         dentry = d_alloc_parallel(dir, &nd->last, &wq);
    3418    84242388 :                         if (IS_ERR(dentry))
    3419             :                                 return dentry;
    3420             :                 }
    3421   106851635 :                 if (d_in_lookup(dentry))
    3422             :                         break;
    3423             : 
    3424    22618469 :                 error = d_revalidate(dentry, nd->flags);
    3425    22617563 :                 if (likely(error > 0))
    3426             :                         break;
    3427           0 :                 if (error)
    3428           0 :                         goto out_dput;
    3429           0 :                 d_invalidate(dentry);
    3430           0 :                 dput(dentry);
    3431           0 :                 dentry = NULL;
    3432             :         }
    3433   106850729 :         if (dentry->d_inode) {
    3434             :                 /* Cached positive dentry: will open in f_op->open */
    3435             :                 return dentry;
    3436             :         }
    3437             : 
    3438             :         /*
    3439             :          * Checking write permission is tricky, bacuse we don't know if we are
    3440             :          * going to actually need it: O_CREAT opens should work as long as the
    3441             :          * file exists.  But checking existence breaks atomicity.  The trick is
    3442             :          * to check access and if not granted clear O_CREAT from the flags.
    3443             :          *
    3444             :          * Another problem is returing the "right" error value (e.g. for an
    3445             :          * O_EXCL open we want to return EEXIST not EROFS).
    3446             :          */
    3447    88130828 :         if (unlikely(!got_write))
    3448    10252355 :                 open_flag &= ~O_TRUNC;
    3449    88130828 :         idmap = mnt_idmap(nd->path.mnt);
    3450    88138942 :         if (open_flag & O_CREAT) {
    3451    55082314 :                 if (open_flag & O_EXCL)
    3452    16553277 :                         open_flag &= ~O_TRUNC;
    3453    55082314 :                 mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode);
    3454    54790085 :                 if (likely(got_write))
    3455    54789309 :                         create_error = may_o_create(idmap, &nd->path,
    3456             :                                                     dentry, mode);
    3457             :                 else
    3458             :                         create_error = -EROFS;
    3459             :         }
    3460    88052841 :         if (create_error)
    3461        1202 :                 open_flag &= ~O_CREAT;
    3462    88052841 :         if (dir_inode->i_op->atomic_open) {
    3463       79395 :                 dentry = atomic_open(nd, dentry, file, open_flag, mode);
    3464       79389 :                 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
    3465         375 :                         dentry = ERR_PTR(create_error);
    3466       79389 :                 return dentry;
    3467             :         }
    3468             : 
    3469    87973446 :         if (d_in_lookup(dentry)) {
    3470    83922087 :                 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
    3471             :                                                              nd->flags);
    3472    83879354 :                 d_lookup_done(dentry);
    3473    83681736 :                 if (unlikely(res)) {
    3474      543266 :                         if (IS_ERR(res)) {
    3475      419197 :                                 error = PTR_ERR(res);
    3476      419197 :                                 goto out_dput;
    3477             :                         }
    3478      124069 :                         dput(dentry);
    3479      124069 :                         dentry = res;
    3480             :                 }
    3481             :         }
    3482             : 
    3483             :         /* Negative dentry, just create the file */
    3484    87313898 :         if (!dentry->d_inode && (open_flag & O_CREAT)) {
    3485    54189481 :                 file->f_mode |= FMODE_CREATED;
    3486    54189481 :                 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
    3487    54189481 :                 if (!dir_inode->i_op->create) {
    3488           0 :                         error = -EACCES;
    3489           0 :                         goto out_dput;
    3490             :                 }
    3491             : 
    3492    54189481 :                 error = dir_inode->i_op->create(idmap, dir_inode, dentry,
    3493    54189481 :                                                 mode, open_flag & O_EXCL);
    3494    54551425 :                 if (error)
    3495      502492 :                         goto out_dput;
    3496             :         }
    3497    87173350 :         if (unlikely(create_error) && !dentry->d_inode) {
    3498         415 :                 error = create_error;
    3499         415 :                 goto out_dput;
    3500             :         }
    3501             :         return dentry;
    3502             : 
    3503      922104 : out_dput:
    3504      922104 :         dput(dentry);
    3505      944838 :         return ERR_PTR(error);
    3506             : }
    3507             : 
    3508  1279967085 : static const char *open_last_lookups(struct nameidata *nd,
    3509             :                    struct file *file, const struct open_flags *op)
    3510             : {
    3511  1279967085 :         struct dentry *dir = nd->path.dentry;
    3512  1279967085 :         int open_flag = op->open_flag;
    3513  1279967085 :         bool got_write = false;
    3514  1279967085 :         struct dentry *dentry;
    3515  1279967085 :         const char *res;
    3516             : 
    3517  1279967085 :         nd->flags |= op->intent;
    3518             : 
    3519  1279967085 :         if (nd->last_type != LAST_NORM) {
    3520    19944812 :                 if (nd->depth)
    3521           0 :                         put_link(nd);
    3522    19944812 :                 return handle_dots(nd, nd->last_type);
    3523             :         }
    3524             : 
    3525  1260022273 :         if (!(open_flag & O_CREAT)) {
    3526  1186350697 :                 if (nd->last.name[nd->last.len])
    3527      415085 :                         nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
    3528             :                 /* we _can_ be in RCU mode here */
    3529  1186350697 :                 dentry = lookup_fast(nd);
    3530  1186445970 :                 if (IS_ERR(dentry))
    3531             :                         return ERR_CAST(dentry);
    3532  1186439283 :                 if (likely(dentry))
    3533  1153414118 :                         goto finish_lookup;
    3534             : 
    3535    33025165 :                 BUG_ON(nd->flags & LOOKUP_RCU);
    3536             :         } else {
    3537             :                 /* create side of things */
    3538    73671576 :                 if (nd->flags & LOOKUP_RCU) {
    3539    72783452 :                         if (!try_to_unlazy(nd))
    3540             :                                 return ERR_PTR(-ECHILD);
    3541             :                 }
    3542    73951479 :                 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
    3543             :                 /* trailing slashes? */
    3544    73951479 :                 if (unlikely(nd->last.name[nd->last.len]))
    3545             :                         return ERR_PTR(-EISDIR);
    3546             :         }
    3547             : 
    3548   106976644 :         if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
    3549    96722048 :                 got_write = !mnt_want_write(nd->path.mnt);
    3550             :                 /*
    3551             :                  * do _not_ fail yet - we might not need that or fail with
    3552             :                  * a different error; let lookup_open() decide; we'll be
    3553             :                  * dropping this one anyway.
    3554             :                  */
    3555             :         }
    3556   106872141 :         if (open_flag & O_CREAT)
    3557    73849152 :                 inode_lock(dir->d_inode);
    3558             :         else
    3559    33022989 :                 inode_lock_shared(dir->d_inode);
    3560   106605253 :         dentry = lookup_open(nd, file, op, got_write);
    3561   106736754 :         if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
    3562    54026007 :                 fsnotify_create(dir->d_inode, dentry);
    3563   106591074 :         if (open_flag & O_CREAT)
    3564    73567799 :                 inode_unlock(dir->d_inode);
    3565             :         else
    3566    33023275 :                 inode_unlock_shared(dir->d_inode);
    3567             : 
    3568   106963857 :         if (got_write)
    3569    96698460 :                 mnt_drop_write(nd->path.mnt);
    3570             : 
    3571   106836324 :         if (IS_ERR(dentry))
    3572             :                 return ERR_CAST(dentry);
    3573             : 
    3574   105881506 :         if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
    3575    54150603 :                 dput(nd->path.dentry);
    3576    54046865 :                 nd->path.dentry = dentry;
    3577    54046865 :                 return NULL;
    3578             :         }
    3579             : 
    3580    51730903 : finish_lookup:
    3581  1205145021 :         if (nd->depth)
    3582   140891560 :                 put_link(nd);
    3583  1205151272 :         res = step_into(nd, WALK_TRAILING, dentry);
    3584  1205202544 :         if (unlikely(res))
    3585   156038631 :                 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
    3586             :         return res;
    3587             : }
    3588             : 
    3589             : /*
    3590             :  * Handle the last step of open()
    3591             :  */
    3592  1122764468 : static int do_open(struct nameidata *nd,
    3593             :                    struct file *file, const struct open_flags *op)
    3594             : {
    3595  1122764468 :         struct mnt_idmap *idmap;
    3596  1122764468 :         int open_flag = op->open_flag;
    3597  1122764468 :         bool do_truncate;
    3598  1122764468 :         int acc_mode;
    3599  1122764468 :         int error;
    3600             : 
    3601  1122764468 :         if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
    3602  1069009804 :                 error = complete_walk(nd);
    3603  1068990968 :                 if (error)
    3604             :                         return error;
    3605             :         }
    3606  1122704951 :         if (!(file->f_mode & FMODE_CREATED))
    3607             :                 audit_inode(nd->name, nd->path.dentry, 0);
    3608  1122704951 :         idmap = mnt_idmap(nd->path.mnt);
    3609  1122498444 :         if (open_flag & O_CREAT) {
    3610    72560586 :                 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
    3611             :                         return -EEXIST;
    3612   144314303 :                 if (d_is_dir(nd->path.dentry))
    3613             :                         return -EISDIR;
    3614    72150579 :                 error = may_create_in_sticky(idmap, nd,
    3615             :                                              d_backing_inode(nd->path.dentry));
    3616    72113200 :                 if (unlikely(error))
    3617             :                         return error;
    3618             :         }
    3619  1122050980 :         if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
    3620             :                 return -ENOTDIR;
    3621             : 
    3622  1122049601 :         do_truncate = false;
    3623  1122049601 :         acc_mode = op->acc_mode;
    3624  1122049601 :         if (file->f_mode & FMODE_CREATED) {
    3625             :                 /* Don't check for write permission, don't truncate */
    3626    53802723 :                 open_flag &= ~O_TRUNC;
    3627    53802723 :                 acc_mode = 0;
    3628  1068246878 :         } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
    3629     1428688 :                 error = mnt_want_write(nd->path.mnt);
    3630     1428895 :                 if (error)
    3631             :                         return error;
    3632             :                 do_truncate = true;
    3633             :         }
    3634  1122049808 :         error = may_open(idmap, &nd->path, acc_mode, open_flag);
    3635  1122095434 :         if (!error && !(file->f_mode & FMODE_OPENED))
    3636  1118790007 :                 error = vfs_open(&nd->path, file);
    3637  1122393127 :         if (!error)
    3638  1117388346 :                 error = ima_file_check(file, op->acc_mode);
    3639  1122393127 :         if (!error && do_truncate)
    3640     1428331 :                 error = handle_truncate(idmap, file);
    3641  1122393091 :         if (unlikely(error > 0)) {
    3642           0 :                 WARN_ON(1);
    3643           0 :                 error = -EINVAL;
    3644             :         }
    3645  1122393091 :         if (do_truncate)
    3646     1428602 :                 mnt_drop_write(nd->path.mnt);
    3647             :         return error;
    3648             : }
    3649             : 
    3650             : /**
    3651             :  * vfs_tmpfile - create tmpfile
    3652             :  * @idmap:      idmap of the mount the inode was found from
    3653             :  * @parentpath: pointer to the path of the base directory
    3654             :  * @file:       file descriptor of the new tmpfile
    3655             :  * @mode:       mode of the new tmpfile
    3656             :  *
    3657             :  * Create a temporary file.
    3658             :  *
    3659             :  * If the inode has been found through an idmapped mount the idmap of
    3660             :  * the vfsmount must be passed through @idmap. This function will then take
    3661             :  * care to map the inode according to @idmap before checking permissions.
    3662             :  * On non-idmapped mounts or if permission checking is to be performed on the
    3663             :  * raw inode simply passs @nop_mnt_idmap.
    3664             :  */
    3665     8845499 : static int vfs_tmpfile(struct mnt_idmap *idmap,
    3666             :                        const struct path *parentpath,
    3667             :                        struct file *file, umode_t mode)
    3668             : {
    3669     8845499 :         struct dentry *child;
    3670     8845499 :         struct inode *dir = d_inode(parentpath->dentry);
    3671     8845499 :         struct inode *inode;
    3672     8845499 :         int error;
    3673     8845499 :         int open_flag = file->f_flags;
    3674             : 
    3675             :         /* we want directory to be writable */
    3676     8845499 :         error = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
    3677     8576932 :         if (error)
    3678             :                 return error;
    3679     8576932 :         if (!dir->i_op->tmpfile)
    3680             :                 return -EOPNOTSUPP;
    3681     8576932 :         child = d_alloc(parentpath->dentry, &slash_name);
    3682     8652075 :         if (unlikely(!child))
    3683             :                 return -ENOMEM;
    3684     8652075 :         file->f_path.mnt = parentpath->mnt;
    3685     8652075 :         file->f_path.dentry = child;
    3686     8652075 :         mode = vfs_prepare_mode(idmap, dir, mode, mode, mode);
    3687     8228978 :         error = dir->i_op->tmpfile(idmap, dir, file, mode);
    3688     8474170 :         dput(child);
    3689     8670360 :         if (error)
    3690             :                 return error;
    3691             :         /* Don't check for other permissions, the inode was just created */
    3692     8594341 :         error = may_open(idmap, &file->f_path, 0, file->f_flags);
    3693     8336549 :         if (error)
    3694             :                 return error;
    3695     8336549 :         inode = file_inode(file);
    3696     8336549 :         if (!(open_flag & O_EXCL)) {
    3697     8370158 :                 spin_lock(&inode->i_lock);
    3698     8877580 :                 inode->i_state |= I_LINKABLE;
    3699     8877580 :                 spin_unlock(&inode->i_lock);
    3700             :         }
    3701             :         ima_post_create_tmpfile(idmap, inode);
    3702             :         return 0;
    3703             : }
    3704             : 
    3705             : /**
    3706             :  * kernel_tmpfile_open - open a tmpfile for kernel internal use
    3707             :  * @idmap:      idmap of the mount the inode was found from
    3708             :  * @parentpath: path of the base directory
    3709             :  * @mode:       mode of the new tmpfile
    3710             :  * @open_flag:  flags
    3711             :  * @cred:       credentials for open
    3712             :  *
    3713             :  * Create and open a temporary file.  The file is not accounted in nr_files,
    3714             :  * hence this is only for kernel internal use, and must not be installed into
    3715             :  * file tables or such.
    3716             :  */
    3717      102246 : struct file *kernel_tmpfile_open(struct mnt_idmap *idmap,
    3718             :                                  const struct path *parentpath,
    3719             :                                  umode_t mode, int open_flag,
    3720             :                                  const struct cred *cred)
    3721             : {
    3722      102246 :         struct file *file;
    3723      102246 :         int error;
    3724             : 
    3725      102246 :         file = alloc_empty_file_noaccount(open_flag, cred);
    3726      102408 :         if (IS_ERR(file))
    3727             :                 return file;
    3728             : 
    3729      102408 :         error = vfs_tmpfile(idmap, parentpath, file, mode);
    3730      102364 :         if (error) {
    3731           0 :                 fput(file);
    3732           0 :                 file = ERR_PTR(error);
    3733             :         }
    3734             :         return file;
    3735             : }
    3736             : EXPORT_SYMBOL(kernel_tmpfile_open);
    3737             : 
    3738     8536799 : static int do_tmpfile(struct nameidata *nd, unsigned flags,
    3739             :                 const struct open_flags *op,
    3740             :                 struct file *file)
    3741             : {
    3742     8536799 :         struct path path;
    3743     8536799 :         int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
    3744             : 
    3745     8303208 :         if (unlikely(error))
    3746             :                 return error;
    3747     8303206 :         error = mnt_want_write(path.mnt);
    3748     8755500 :         if (unlikely(error))
    3749           0 :                 goto out;
    3750     8755500 :         error = vfs_tmpfile(mnt_idmap(path.mnt), &path, file, op->mode);
    3751     8562408 :         if (error)
    3752             :                 goto out2;
    3753     8562408 :         audit_inode(nd->name, file->f_path.dentry, 0);
    3754             : out2:
    3755     8562408 :         mnt_drop_write(path.mnt);
    3756     8171322 : out:
    3757     8171322 :         path_put(&path);
    3758     8171322 :         return error;
    3759             : }
    3760             : 
    3761    76930184 : static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
    3762             : {
    3763    76930184 :         struct path path;
    3764    76930184 :         int error = path_lookupat(nd, flags, &path);
    3765    76862128 :         if (!error) {
    3766    74046467 :                 audit_inode(nd->name, path.dentry, 0);
    3767    74046467 :                 error = vfs_open(&path, file);
    3768    74113159 :                 path_put(&path);
    3769             :         }
    3770    76939854 :         return error;
    3771             : }
    3772             : 
    3773  1231785071 : static struct file *path_openat(struct nameidata *nd,
    3774             :                         const struct open_flags *op, unsigned flags)
    3775             : {
    3776  1231785071 :         struct file *file;
    3777  1231785071 :         int error;
    3778             : 
    3779  1231785071 :         file = alloc_empty_file(op->open_flag, current_cred());
    3780  1232686085 :         if (IS_ERR(file))
    3781             :                 return file;
    3782             : 
    3783  1232686085 :         if (unlikely(file->f_flags & __O_TMPFILE)) {
    3784     8609921 :                 error = do_tmpfile(nd, flags, op, file);
    3785  1224076164 :         } else if (unlikely(file->f_flags & O_PATH)) {
    3786    76970982 :                 error = do_o_path(nd, flags, file);
    3787             :         } else {
    3788  1147105182 :                 const char *s = path_init(nd, flags);
    3789  2427040543 :                 while (!(error = link_path_walk(s, nd)) &&
    3790  1280259903 :                        (s = open_last_lookups(nd, file, op)) != NULL)
    3791             :                         ;
    3792  1146621667 :                 if (!error)
    3793  1122958494 :                         error = do_open(nd, file, op);
    3794  1146109888 :                 terminate_walk(nd);
    3795             :         }
    3796  1231879321 :         if (likely(!error)) {
    3797  1200162524 :                 if (likely(file->f_mode & FMODE_OPENED))
    3798             :                         return file;
    3799           0 :                 WARN_ON(1);
    3800           0 :                 error = -EINVAL;
    3801             :         }
    3802    31716797 :         fput(file);
    3803    31723955 :         if (error == -EOPENSTALE) {
    3804           3 :                 if (flags & LOOKUP_RCU)
    3805             :                         error = -ECHILD;
    3806             :                 else
    3807           1 :                         error = -ESTALE;
    3808             :         }
    3809    31723955 :         return ERR_PTR(error);
    3810             : }
    3811             : 
    3812  1231983733 : struct file *do_filp_open(int dfd, struct filename *pathname,
    3813             :                 const struct open_flags *op)
    3814             : {
    3815  1231983733 :         struct nameidata nd;
    3816  1231983733 :         int flags = op->lookup_flags;
    3817  1231983733 :         struct file *filp;
    3818             : 
    3819  1231983733 :         set_nameidata(&nd, dfd, pathname, NULL);
    3820  1230856384 :         filp = path_openat(&nd, op, flags | LOOKUP_RCU);
    3821  1232042746 :         if (unlikely(filp == ERR_PTR(-ECHILD)))
    3822       91487 :                 filp = path_openat(&nd, op, flags);
    3823  1232043663 :         if (unlikely(filp == ERR_PTR(-ESTALE)))
    3824        2088 :                 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
    3825  1232043664 :         restore_nameidata();
    3826  1231383040 :         return filp;
    3827             : }
    3828             : 
    3829       41444 : struct file *do_file_open_root(const struct path *root,
    3830             :                 const char *name, const struct open_flags *op)
    3831             : {
    3832       41444 :         struct nameidata nd;
    3833       41444 :         struct file *file;
    3834       41444 :         struct filename *filename;
    3835       41444 :         int flags = op->lookup_flags;
    3836             : 
    3837       41444 :         if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
    3838             :                 return ERR_PTR(-ELOOP);
    3839             : 
    3840       41444 :         filename = getname_kernel(name);
    3841       41444 :         if (IS_ERR(filename))
    3842             :                 return ERR_CAST(filename);
    3843             : 
    3844       41444 :         set_nameidata(&nd, -1, filename, root);
    3845       41444 :         file = path_openat(&nd, op, flags | LOOKUP_RCU);
    3846       41444 :         if (unlikely(file == ERR_PTR(-ECHILD)))
    3847           0 :                 file = path_openat(&nd, op, flags);
    3848       41444 :         if (unlikely(file == ERR_PTR(-ESTALE)))
    3849           0 :                 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
    3850       41444 :         restore_nameidata();
    3851       41444 :         putname(filename);
    3852       41444 :         return file;
    3853             : }
    3854             : 
    3855   684455095 : static struct dentry *filename_create(int dfd, struct filename *name,
    3856             :                                       struct path *path, unsigned int lookup_flags)
    3857             : {
    3858   684455095 :         struct dentry *dentry = ERR_PTR(-EEXIST);
    3859   684455095 :         struct qstr last;
    3860   684455095 :         bool want_dir = lookup_flags & LOOKUP_DIRECTORY;
    3861   684455095 :         unsigned int reval_flag = lookup_flags & LOOKUP_REVAL;
    3862   684455095 :         unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL;
    3863   684455095 :         int type;
    3864   684455095 :         int err2;
    3865   684455095 :         int error;
    3866             : 
    3867   684455095 :         error = filename_parentat(dfd, name, reval_flag, path, &last, &type);
    3868   684342802 :         if (error)
    3869         226 :                 return ERR_PTR(error);
    3870             : 
    3871             :         /*
    3872             :          * Yucky last component or no last component at all?
    3873             :          * (foo/., foo/.., /////)
    3874             :          */
    3875   684342576 :         if (unlikely(type != LAST_NORM))
    3876           0 :                 goto out;
    3877             : 
    3878             :         /* don't fail immediately if it's r/o, at least try to report other errors */
    3879   684342576 :         err2 = mnt_want_write(path->mnt);
    3880             :         /*
    3881             :          * Do the final lookup.  Suppress 'create' if there is a trailing
    3882             :          * '/', and a directory wasn't requested.
    3883             :          */
    3884   684525497 :         if (last.name[last.len] && !want_dir)
    3885          92 :                 create_flags = 0;
    3886   684525497 :         inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
    3887   684563494 :         dentry = lookup_one_qstr_excl(&last, path->dentry,
    3888             :                                       reval_flag | create_flags);
    3889   684382839 :         if (IS_ERR(dentry))
    3890        9938 :                 goto unlock;
    3891             : 
    3892   684372901 :         error = -EEXIST;
    3893   684372901 :         if (d_is_positive(dentry))
    3894     2050331 :                 goto fail;
    3895             : 
    3896             :         /*
    3897             :          * Special case - lookup gave negative, but... we had foo/bar/
    3898             :          * From the vfs_mknod() POV we just have a negative dentry -
    3899             :          * all is fine. Let's be bastards - you had / on the end, you've
    3900             :          * been asking for (non-existent) directory. -ENOENT for you.
    3901             :          */
    3902   682322570 :         if (unlikely(!create_flags)) {
    3903           0 :                 error = -ENOENT;
    3904           0 :                 goto fail;
    3905             :         }
    3906   682322570 :         if (unlikely(err2)) {
    3907           7 :                 error = err2;
    3908           7 :                 goto fail;
    3909             :         }
    3910             :         return dentry;
    3911     2050338 : fail:
    3912     2050338 :         dput(dentry);
    3913     2050530 :         dentry = ERR_PTR(error);
    3914     2060468 : unlock:
    3915     2060468 :         inode_unlock(path->dentry->d_inode);
    3916     2060459 :         if (!err2)
    3917     1915591 :                 mnt_drop_write(path->mnt);
    3918      144868 : out:
    3919     2060339 :         path_put(path);
    3920     2060339 :         return dentry;
    3921             : }
    3922             : 
    3923        9729 : struct dentry *kern_path_create(int dfd, const char *pathname,
    3924             :                                 struct path *path, unsigned int lookup_flags)
    3925             : {
    3926        9729 :         struct filename *filename = getname_kernel(pathname);
    3927        9729 :         struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
    3928             : 
    3929        9729 :         putname(filename);
    3930        9729 :         return res;
    3931             : }
    3932             : EXPORT_SYMBOL(kern_path_create);
    3933             : 
    3934   682242604 : void done_path_create(struct path *path, struct dentry *dentry)
    3935             : {
    3936   682242604 :         dput(dentry);
    3937   682509338 :         inode_unlock(path->dentry->d_inode);
    3938   682582611 :         mnt_drop_write(path->mnt);
    3939   682320206 :         path_put(path);
    3940   682078521 : }
    3941             : EXPORT_SYMBOL(done_path_create);
    3942             : 
    3943           0 : inline struct dentry *user_path_create(int dfd, const char __user *pathname,
    3944             :                                 struct path *path, unsigned int lookup_flags)
    3945             : {
    3946           0 :         struct filename *filename = getname(pathname);
    3947           0 :         struct dentry *res = filename_create(dfd, filename, path, lookup_flags);
    3948             : 
    3949           0 :         putname(filename);
    3950           0 :         return res;
    3951             : }
    3952             : EXPORT_SYMBOL(user_path_create);
    3953             : 
    3954             : /**
    3955             :  * vfs_mknod - create device node or file
    3956             :  * @idmap:      idmap of the mount the inode was found from
    3957             :  * @dir:        inode of @dentry
    3958             :  * @dentry:     pointer to dentry of the base directory
    3959             :  * @mode:       mode of the new device node or file
    3960             :  * @dev:        device number of device to create
    3961             :  *
    3962             :  * Create a device node or file.
    3963             :  *
    3964             :  * If the inode has been found through an idmapped mount the idmap of
    3965             :  * the vfsmount must be passed through @idmap. This function will then take
    3966             :  * care to map the inode according to @idmap before checking permissions.
    3967             :  * On non-idmapped mounts or if permission checking is to be performed on the
    3968             :  * raw inode simply passs @nop_mnt_idmap.
    3969             :  */
    3970    12044560 : int vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
    3971             :               struct dentry *dentry, umode_t mode, dev_t dev)
    3972             : {
    3973    12044560 :         bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
    3974    12044560 :         int error = may_create(idmap, dir, dentry);
    3975             : 
    3976    12045430 :         if (error)
    3977             :                 return error;
    3978             : 
    3979    12049817 :         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
    3980        4413 :             !capable(CAP_MKNOD))
    3981             :                 return -EPERM;
    3982             : 
    3983    12044508 :         if (!dir->i_op->mknod)
    3984             :                 return -EPERM;
    3985             : 
    3986    12044508 :         mode = vfs_prepare_mode(idmap, dir, mode, mode, mode);
    3987    12043379 :         error = devcgroup_inode_mknod(mode, dev);
    3988    12043379 :         if (error)
    3989             :                 return error;
    3990             : 
    3991    12043379 :         error = security_inode_mknod(dir, dentry, mode, dev);
    3992    12043379 :         if (error)
    3993             :                 return error;
    3994             : 
    3995    12043379 :         error = dir->i_op->mknod(idmap, dir, dentry, mode, dev);
    3996    12039445 :         if (!error)
    3997    11886757 :                 fsnotify_create(dir, dentry);
    3998             :         return error;
    3999             : }
    4000             : EXPORT_SYMBOL(vfs_mknod);
    4001             : 
    4002    12471378 : static int may_mknod(umode_t mode)
    4003             : {
    4004    12471378 :         switch (mode & S_IFMT) {
    4005             :         case S_IFREG:
    4006             :         case S_IFCHR:
    4007             :         case S_IFBLK:
    4008             :         case S_IFIFO:
    4009             :         case S_IFSOCK:
    4010             :         case 0: /* zero mode translates to S_IFREG */
    4011             :                 return 0;
    4012           0 :         case S_IFDIR:
    4013           0 :                 return -EPERM;
    4014           0 :         default:
    4015           0 :                 return -EINVAL;
    4016             :         }
    4017             : }
    4018             : 
    4019    12471100 : static int do_mknodat(int dfd, struct filename *name, umode_t mode,
    4020             :                 unsigned int dev)
    4021             : {
    4022    12471100 :         struct mnt_idmap *idmap;
    4023    12471100 :         struct dentry *dentry;
    4024    12471100 :         struct path path;
    4025    12471100 :         int error;
    4026    12471100 :         unsigned int lookup_flags = 0;
    4027             : 
    4028    12471100 :         error = may_mknod(mode);
    4029    12471100 :         if (error)
    4030           0 :                 goto out1;
    4031    12471100 : retry:
    4032    12471128 :         dentry = filename_create(dfd, name, &path, lookup_flags);
    4033    12470835 :         error = PTR_ERR(dentry);
    4034    12470835 :         if (IS_ERR(dentry))
    4035      302719 :                 goto out1;
    4036             : 
    4037    12168116 :         error = security_path_mknod(&path, dentry,
    4038    12168116 :                         mode_strip_umask(path.dentry->d_inode, mode), dev);
    4039    12167413 :         if (error)
    4040             :                 goto out2;
    4041             : 
    4042    12167413 :         idmap = mnt_idmap(path.mnt);
    4043    12167512 :         switch (mode & S_IFMT) {
    4044      131523 :                 case 0: case S_IFREG:
    4045      131523 :                         error = vfs_create(idmap, path.dentry->d_inode,
    4046             :                                            dentry, mode, true);
    4047      131523 :                         if (!error)
    4048             :                                 ima_post_path_mknod(idmap, dentry);
    4049             :                         break;
    4050             :                 case S_IFCHR: case S_IFBLK:
    4051    11998052 :                         error = vfs_mknod(idmap, path.dentry->d_inode,
    4052             :                                           dentry, mode, new_decode_dev(dev));
    4053    11998052 :                         break;
    4054       37937 :                 case S_IFIFO: case S_IFSOCK:
    4055       37937 :                         error = vfs_mknod(idmap, path.dentry->d_inode,
    4056             :                                           dentry, mode, 0);
    4057       37937 :                         break;
    4058             :         }
    4059    12159005 : out2:
    4060    12159005 :         done_path_create(&path, dentry);
    4061    24328300 :         if (retry_estale(error, lookup_flags)) {
    4062          29 :                 lookup_flags |= LOOKUP_REVAL;
    4063          29 :                 goto retry;
    4064             :         }
    4065    12164122 : out1:
    4066    12466841 :         putname(name);
    4067    12464595 :         return error;
    4068             : }
    4069             : 
    4070    24943324 : SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
    4071             :                 unsigned int, dev)
    4072             : {
    4073    12471532 :         return do_mknodat(dfd, getname(filename), mode, dev);
    4074             : }
    4075             : 
    4076           0 : SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
    4077             : {
    4078           0 :         return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
    4079             : }
    4080             : 
    4081             : /**
    4082             :  * vfs_mkdir - create directory
    4083             :  * @idmap:      idmap of the mount the inode was found from
    4084             :  * @dir:        inode of @dentry
    4085             :  * @dentry:     pointer to dentry of the base directory
    4086             :  * @mode:       mode of the new directory
    4087             :  *
    4088             :  * Create a directory.
    4089             :  *
    4090             :  * If the inode has been found through an idmapped mount the idmap of
    4091             :  * the vfsmount must be passed through @idmap. This function will then take
    4092             :  * care to map the inode according to @idmap before checking permissions.
    4093             :  * On non-idmapped mounts or if permission checking is to be performed on the
    4094             :  * raw inode simply passs @nop_mnt_idmap.
    4095             :  */
    4096    13772426 : int vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
    4097             :               struct dentry *dentry, umode_t mode)
    4098             : {
    4099    13772426 :         int error;
    4100    13772426 :         unsigned max_links = dir->i_sb->s_max_links;
    4101             : 
    4102    13772426 :         error = may_create(idmap, dir, dentry);
    4103    13773612 :         if (error)
    4104             :                 return error;
    4105             : 
    4106    13773573 :         if (!dir->i_op->mkdir)
    4107             :                 return -EPERM;
    4108             : 
    4109    13773573 :         mode = vfs_prepare_mode(idmap, dir, mode, S_IRWXUGO | S_ISVTX, 0);
    4110    13772088 :         error = security_inode_mkdir(dir, dentry, mode);
    4111    13772088 :         if (error)
    4112             :                 return error;
    4113             : 
    4114    13772088 :         if (max_links && dir->i_nlink >= max_links)
    4115             :                 return -EMLINK;
    4116             : 
    4117    13772088 :         error = dir->i_op->mkdir(idmap, dir, dentry, mode);
    4118    13765932 :         if (!error)
    4119    13581029 :                 fsnotify_mkdir(dir, dentry);
    4120             :         return error;
    4121             : }
    4122             : EXPORT_SYMBOL(vfs_mkdir);
    4123             : 
    4124    14850224 : int do_mkdirat(int dfd, struct filename *name, umode_t mode)
    4125             : {
    4126    14850224 :         struct dentry *dentry;
    4127    14850224 :         struct path path;
    4128    14850224 :         int error;
    4129    14850224 :         unsigned int lookup_flags = LOOKUP_DIRECTORY;
    4130             : 
    4131    14850172 : retry:
    4132    14850172 :         dentry = filename_create(dfd, name, &path, lookup_flags);
    4133    14849730 :         error = PTR_ERR(dentry);
    4134    14849730 :         if (IS_ERR(dentry))
    4135     1189434 :                 goto out_putname;
    4136             : 
    4137    13660296 :         error = security_path_mkdir(&path, dentry,
    4138    13660296 :                         mode_strip_umask(path.dentry->d_inode, mode));
    4139    13659338 :         if (!error) {
    4140    13659338 :                 error = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode,
    4141             :                                   dentry, mode);
    4142             :         }
    4143    13641249 :         done_path_create(&path, dentry);
    4144    27308302 :         if (retry_estale(error, lookup_flags)) {
    4145           6 :                 lookup_flags |= LOOKUP_REVAL;
    4146           6 :                 goto retry;
    4147             :         }
    4148    13654203 : out_putname:
    4149    14843637 :         putname(name);
    4150    14841591 :         return error;
    4151             : }
    4152             : 
    4153      274170 : SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
    4154             : {
    4155      137085 :         return do_mkdirat(dfd, getname(pathname), mode);
    4156             : }
    4157             : 
    4158    29428171 : SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
    4159             : {
    4160    14713967 :         return do_mkdirat(AT_FDCWD, getname(pathname), mode);
    4161             : }
    4162             : 
    4163             : /**
    4164             :  * vfs_rmdir - remove directory
    4165             :  * @idmap:      idmap of the mount the inode was found from
    4166             :  * @dir:        inode of @dentry
    4167             :  * @dentry:     pointer to dentry of the base directory
    4168             :  *
    4169             :  * Remove a directory.
    4170             :  *
    4171             :  * If the inode has been found through an idmapped mount the idmap of
    4172             :  * the vfsmount must be passed through @idmap. This function will then take
    4173             :  * care to map the inode according to @idmap before checking permissions.
    4174             :  * On non-idmapped mounts or if permission checking is to be performed on the
    4175             :  * raw inode simply passs @nop_mnt_idmap.
    4176             :  */
    4177     5640075 : int vfs_rmdir(struct mnt_idmap *idmap, struct inode *dir,
    4178             :                      struct dentry *dentry)
    4179             : {
    4180     5640075 :         int error = may_delete(idmap, dir, dentry, 1);
    4181             : 
    4182     5640078 :         if (error)
    4183             :                 return error;
    4184             : 
    4185     5608139 :         if (!dir->i_op->rmdir)
    4186             :                 return -EPERM;
    4187             : 
    4188     5608139 :         dget(dentry);
    4189     5608226 :         inode_lock(dentry->d_inode);
    4190             : 
    4191     5608259 :         error = -EBUSY;
    4192     5608259 :         if (is_local_mountpoint(dentry) ||
    4193     5608100 :             (dentry->d_inode->i_flags & S_KERNEL_FILE))
    4194          44 :                 goto out;
    4195             : 
    4196     5608100 :         error = security_inode_rmdir(dir, dentry);
    4197     5608100 :         if (error)
    4198             :                 goto out;
    4199             : 
    4200     5608100 :         error = dir->i_op->rmdir(dir, dentry);
    4201     5608168 :         if (error)
    4202     3898733 :                 goto out;
    4203             : 
    4204     1709435 :         shrink_dcache_parent(dentry);
    4205     1709397 :         dentry->d_inode->i_flags |= S_DEAD;
    4206     1709397 :         dont_mount(dentry);
    4207     1709416 :         detach_mounts(dentry);
    4208             : 
    4209     5608190 : out:
    4210     5608190 :         inode_unlock(dentry->d_inode);
    4211     5608107 :         dput(dentry);
    4212     5608221 :         if (!error)
    4213     1709413 :                 d_delete_notify(dir, dentry);
    4214             :         return error;
    4215             : }
    4216             : EXPORT_SYMBOL(vfs_rmdir);
    4217             : 
    4218     5811859 : int do_rmdir(int dfd, struct filename *name)
    4219             : {
    4220     5811859 :         int error;
    4221     5811859 :         struct dentry *dentry;
    4222     5811859 :         struct path path;
    4223     5811859 :         struct qstr last;
    4224     5811859 :         int type;
    4225     5811859 :         unsigned int lookup_flags = 0;
    4226     5811855 : retry:
    4227     5811855 :         error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
    4228     5811954 :         if (error)
    4229           0 :                 goto exit1;
    4230             : 
    4231     5811954 :         switch (type) {
    4232             :         case LAST_DOTDOT:
    4233             :                 error = -ENOTEMPTY;
    4234             :                 goto exit2;
    4235             :         case LAST_DOT:
    4236             :                 error = -EINVAL;
    4237             :                 goto exit2;
    4238             :         case LAST_ROOT:
    4239             :                 error = -EBUSY;
    4240             :                 goto exit2;
    4241             :         }
    4242             : 
    4243     5811954 :         error = mnt_want_write(path.mnt);
    4244     5812100 :         if (error)
    4245         532 :                 goto exit2;
    4246             : 
    4247     5811568 :         inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
    4248     5811509 :         dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags);
    4249     5811440 :         error = PTR_ERR(dentry);
    4250     5811440 :         if (IS_ERR(dentry))
    4251          19 :                 goto exit3;
    4252     5811421 :         if (!dentry->d_inode) {
    4253      172003 :                 error = -ENOENT;
    4254      172003 :                 goto exit4;
    4255             :         }
    4256     5639418 :         error = security_path_rmdir(&path, dentry);
    4257     5639418 :         if (error)
    4258             :                 goto exit4;
    4259     5639418 :         error = vfs_rmdir(mnt_idmap(path.mnt), path.dentry->d_inode, dentry);
    4260     5811216 : exit4:
    4261     5811216 :         dput(dentry);
    4262     5811581 : exit3:
    4263     5811581 :         inode_unlock(path.dentry->d_inode);
    4264     5811506 :         mnt_drop_write(path.mnt);
    4265     5811872 : exit2:
    4266     5811872 :         path_put(&path);
    4267    11623888 :         if (retry_estale(error, lookup_flags)) {
    4268           2 :                 lookup_flags |= LOOKUP_REVAL;
    4269           2 :                 goto retry;
    4270             :         }
    4271     5811948 : exit1:
    4272     5811948 :         putname(name);
    4273     5811689 :         return error;
    4274             : }
    4275             : 
    4276    10341467 : SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
    4277             : {
    4278     5170722 :         return do_rmdir(AT_FDCWD, getname(pathname));
    4279             : }
    4280             : 
    4281             : /**
    4282             :  * vfs_unlink - unlink a filesystem object
    4283             :  * @idmap:      idmap of the mount the inode was found from
    4284             :  * @dir:        parent directory
    4285             :  * @dentry:     victim
    4286             :  * @delegated_inode: returns victim inode, if the inode is delegated.
    4287             :  *
    4288             :  * The caller must hold dir->i_mutex.
    4289             :  *
    4290             :  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
    4291             :  * return a reference to the inode in delegated_inode.  The caller
    4292             :  * should then break the delegation on that inode and retry.  Because
    4293             :  * breaking a delegation may take a long time, the caller should drop
    4294             :  * dir->i_mutex before doing so.
    4295             :  *
    4296             :  * Alternatively, a caller may pass NULL for delegated_inode.  This may
    4297             :  * be appropriate for callers that expect the underlying filesystem not
    4298             :  * to be NFS exported.
    4299             :  *
    4300             :  * If the inode has been found through an idmapped mount the idmap of
    4301             :  * the vfsmount must be passed through @idmap. This function will then take
    4302             :  * care to map the inode according to @idmap before checking permissions.
    4303             :  * On non-idmapped mounts or if permission checking is to be performed on the
    4304             :  * raw inode simply passs @nop_mnt_idmap.
    4305             :  */
    4306    70235771 : int vfs_unlink(struct mnt_idmap *idmap, struct inode *dir,
    4307             :                struct dentry *dentry, struct inode **delegated_inode)
    4308             : {
    4309    70235771 :         struct inode *target = dentry->d_inode;
    4310    70235771 :         int error = may_delete(idmap, dir, dentry, 0);
    4311             : 
    4312    70183331 :         if (error)
    4313             :                 return error;
    4314             : 
    4315    70168521 :         if (!dir->i_op->unlink)
    4316             :                 return -EPERM;
    4317             : 
    4318    70168521 :         inode_lock(target);
    4319    70180924 :         if (IS_SWAPFILE(target))
    4320             :                 error = -EPERM;
    4321    70181552 :         else if (is_local_mountpoint(dentry))
    4322             :                 error = -EBUSY;
    4323             :         else {
    4324    70174264 :                 error = security_inode_unlink(dir, dentry);
    4325    70174264 :                 if (!error) {
    4326    70174264 :                         error = try_break_deleg(target, delegated_inode);
    4327    70220918 :                         if (error)
    4328           0 :                                 goto out;
    4329    70220918 :                         error = dir->i_op->unlink(dir, dentry);
    4330    70238066 :                         if (!error) {
    4331    70237674 :                                 dont_mount(dentry);
    4332    70238640 :                                 detach_mounts(dentry);
    4333             :                         }
    4334             :                 }
    4335             :         }
    4336         392 : out:
    4337    70236936 :         inode_unlock(target);
    4338             : 
    4339             :         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
    4340    70238999 :         if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
    4341           0 :                 fsnotify_unlink(dir, dentry);
    4342    70238999 :         } else if (!error) {
    4343    70238415 :                 fsnotify_link_count(target);
    4344    70237224 :                 d_delete_notify(dir, dentry);
    4345             :         }
    4346             : 
    4347             :         return error;
    4348             : }
    4349             : EXPORT_SYMBOL(vfs_unlink);
    4350             : 
    4351             : /*
    4352             :  * Make sure that the actual truncation of the file will occur outside its
    4353             :  * directory's i_mutex.  Truncate can take a long time if there is a lot of
    4354             :  * writeout happening, and we don't want to prevent access to the directory
    4355             :  * while waiting on the I/O.
    4356             :  */
    4357    74226329 : int do_unlinkat(int dfd, struct filename *name)
    4358             : {
    4359    74226329 :         int error;
    4360    74226329 :         struct dentry *dentry;
    4361    74226329 :         struct path path;
    4362    74226329 :         struct qstr last;
    4363    74226329 :         int type;
    4364    74226329 :         struct inode *inode = NULL;
    4365    74226329 :         struct inode *delegated_inode = NULL;
    4366    74226329 :         unsigned int lookup_flags = 0;
    4367    74225380 : retry:
    4368    74225380 :         error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
    4369    74225243 :         if (error)
    4370       27758 :                 goto exit1;
    4371             : 
    4372    74197485 :         error = -EISDIR;
    4373    74197485 :         if (type != LAST_NORM)
    4374           0 :                 goto exit2;
    4375             : 
    4376    74197485 :         error = mnt_want_write(path.mnt);
    4377    74230997 :         if (error)
    4378         690 :                 goto exit2;
    4379    74230307 : retry_deleg:
    4380    74230307 :         inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
    4381    74202271 :         dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags);
    4382    74233518 :         error = PTR_ERR(dentry);
    4383    74233518 :         if (!IS_ERR(dentry)) {
    4384             : 
    4385             :                 /* Why not before? Because we want correct error value */
    4386    74200108 :                 if (last.name[last.len])
    4387           0 :                         goto slashes;
    4388    74200108 :                 inode = dentry->d_inode;
    4389    74200108 :                 if (d_is_negative(dentry))
    4390     4089020 :                         goto slashes;
    4391    70111088 :                 ihold(inode);
    4392    70131843 :                 error = security_path_unlink(&path, dentry);
    4393    70131843 :                 if (error)
    4394             :                         goto exit3;
    4395    70131843 :                 error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode,
    4396             :                                    dentry, &delegated_inode);
    4397    74235412 : exit3:
    4398    74235412 :                 dput(dentry);
    4399             :         }
    4400    74267603 :         inode_unlock(path.dentry->d_inode);
    4401    74269315 :         if (inode)
    4402    70146994 :                 iput(inode);    /* truncate the inode here */
    4403    74141143 :         inode = NULL;
    4404    74141143 :         if (delegated_inode) {
    4405           0 :                 error = break_deleg_wait(&delegated_inode);
    4406           0 :                 if (!error)
    4407           0 :                         goto retry_deleg;
    4408             :         }
    4409    74141143 :         mnt_drop_write(path.mnt);
    4410    74207646 : exit2:
    4411    74207646 :         path_put(&path);
    4412   148329144 :         if (retry_estale(error, lookup_flags)) {
    4413         726 :                 lookup_flags |= LOOKUP_REVAL;
    4414         726 :                 inode = NULL;
    4415         726 :                 goto retry;
    4416             :         }
    4417    74165521 : exit1:
    4418    74193279 :         putname(name);
    4419    74186855 :         return error;
    4420             : 
    4421     4089020 : slashes:
    4422     4089020 :         if (d_is_negative(dentry))
    4423             :                 error = -ENOENT;
    4424           0 :         else if (d_is_dir(dentry))
    4425             :                 error = -EISDIR;
    4426             :         else
    4427           0 :                 error = -ENOTDIR;
    4428     4089020 :         goto exit3;
    4429             : }
    4430             : 
    4431    51727246 : SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
    4432             : {
    4433    25862768 :         if ((flag & ~AT_REMOVEDIR) != 0)
    4434             :                 return -EINVAL;
    4435             : 
    4436    25862768 :         if (flag & AT_REMOVEDIR)
    4437      641304 :                 return do_rmdir(dfd, getname(pathname));
    4438    25221464 :         return do_unlinkat(dfd, getname(pathname));
    4439             : }
    4440             : 
    4441    98026582 : SYSCALL_DEFINE1(unlink, const char __user *, pathname)
    4442             : {
    4443    49008018 :         return do_unlinkat(AT_FDCWD, getname(pathname));
    4444             : }
    4445             : 
    4446             : /**
    4447             :  * vfs_symlink - create symlink
    4448             :  * @idmap:      idmap of the mount the inode was found from
    4449             :  * @dir:        inode of @dentry
    4450             :  * @dentry:     pointer to dentry of the base directory
    4451             :  * @oldname:    name of the file to link to
    4452             :  *
    4453             :  * Create a symlink.
    4454             :  *
    4455             :  * If the inode has been found through an idmapped mount the idmap of
    4456             :  * the vfsmount must be passed through @idmap. This function will then take
    4457             :  * care to map the inode according to @idmap before checking permissions.
    4458             :  * On non-idmapped mounts or if permission checking is to be performed on the
    4459             :  * raw inode simply passs @nop_mnt_idmap.
    4460             :  */
    4461   642317216 : int vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
    4462             :                 struct dentry *dentry, const char *oldname)
    4463             : {
    4464   642317216 :         int error;
    4465             : 
    4466   642317216 :         error = may_create(idmap, dir, dentry);
    4467   642306110 :         if (error)
    4468             :                 return error;
    4469             : 
    4470   642306071 :         if (!dir->i_op->symlink)
    4471             :                 return -EPERM;
    4472             : 
    4473   642306071 :         error = security_inode_symlink(dir, dentry, oldname);
    4474   642306071 :         if (error)
    4475             :                 return error;
    4476             : 
    4477   642306071 :         error = dir->i_op->symlink(idmap, dir, dentry, oldname);
    4478   642575448 :         if (!error)
    4479    34191007 :                 fsnotify_create(dir, dentry);
    4480             :         return error;
    4481             : }
    4482             : EXPORT_SYMBOL(vfs_symlink);
    4483             : 
    4484   643210597 : int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
    4485             : {
    4486   643210597 :         int error;
    4487   643210597 :         struct dentry *dentry;
    4488   643210597 :         struct path path;
    4489   643210597 :         unsigned int lookup_flags = 0;
    4490             : 
    4491   643210597 :         if (IS_ERR(from)) {
    4492       31187 :                 error = PTR_ERR(from);
    4493       31187 :                 goto out_putnames;
    4494             :         }
    4495   643179410 : retry:
    4496   643182431 :         dentry = filename_create(newdfd, to, &path, lookup_flags);
    4497   642950273 :         error = PTR_ERR(dentry);
    4498   642950273 :         if (IS_ERR(dentry))
    4499      465066 :                 goto out_putnames;
    4500             : 
    4501   642485207 :         error = security_path_symlink(&path, dentry, from->name);
    4502   642485207 :         if (!error)
    4503   642485207 :                 error = vfs_symlink(mnt_idmap(path.mnt), path.dentry->d_inode,
    4504             :                                     dentry, from->name);
    4505   642435841 :         done_path_create(&path, dentry);
    4506  1284591062 :         if (retry_estale(error, lookup_flags)) {
    4507        3021 :                 lookup_flags |= LOOKUP_REVAL;
    4508        3021 :                 goto retry;
    4509             :         }
    4510   642292510 : out_putnames:
    4511   642788763 :         putname(to);
    4512   642697881 :         putname(from);
    4513   642954213 :         return error;
    4514             : }
    4515             : 
    4516      159844 : SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
    4517             :                 int, newdfd, const char __user *, newname)
    4518             : {
    4519      159844 :         return do_symlinkat(getname(oldname), newdfd, getname(newname));
    4520             : }
    4521             : 
    4522  1286147075 : SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
    4523             : {
    4524  1286255852 :         return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
    4525             : }
    4526             : 
    4527             : /**
    4528             :  * vfs_link - create a new link
    4529             :  * @old_dentry: object to be linked
    4530             :  * @idmap:      idmap of the mount
    4531             :  * @dir:        new parent
    4532             :  * @new_dentry: where to create the new link
    4533             :  * @delegated_inode: returns inode needing a delegation break
    4534             :  *
    4535             :  * The caller must hold dir->i_mutex
    4536             :  *
    4537             :  * If vfs_link discovers a delegation on the to-be-linked file in need
    4538             :  * of breaking, it will return -EWOULDBLOCK and return a reference to the
    4539             :  * inode in delegated_inode.  The caller should then break the delegation
    4540             :  * and retry.  Because breaking a delegation may take a long time, the
    4541             :  * caller should drop the i_mutex before doing so.
    4542             :  *
    4543             :  * Alternatively, a caller may pass NULL for delegated_inode.  This may
    4544             :  * be appropriate for callers that expect the underlying filesystem not
    4545             :  * to be NFS exported.
    4546             :  *
    4547             :  * If the inode has been found through an idmapped mount the idmap of
    4548             :  * the vfsmount must be passed through @idmap. This function will then take
    4549             :  * care to map the inode according to @idmap before checking permissions.
    4550             :  * On non-idmapped mounts or if permission checking is to be performed on the
    4551             :  * raw inode simply passs @nop_mnt_idmap.
    4552             :  */
    4553    14045061 : int vfs_link(struct dentry *old_dentry, struct mnt_idmap *idmap,
    4554             :              struct inode *dir, struct dentry *new_dentry,
    4555             :              struct inode **delegated_inode)
    4556             : {
    4557    14045061 :         struct inode *inode = old_dentry->d_inode;
    4558    14045061 :         unsigned max_links = dir->i_sb->s_max_links;
    4559    14045061 :         int error;
    4560             : 
    4561    14045061 :         if (!inode)
    4562             :                 return -ENOENT;
    4563             : 
    4564    14045061 :         error = may_create(idmap, dir, new_dentry);
    4565    14045911 :         if (error)
    4566             :                 return error;
    4567             : 
    4568    14045872 :         if (dir->i_sb != inode->i_sb)
    4569             :                 return -EXDEV;
    4570             : 
    4571             :         /*
    4572             :          * A link to an append-only or immutable file cannot be created.
    4573             :          */
    4574    14045872 :         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
    4575             :                 return -EPERM;
    4576             :         /*
    4577             :          * Updating the link count will likely cause i_uid and i_gid to
    4578             :          * be writen back improperly if their true value is unknown to
    4579             :          * the vfs.
    4580             :          */
    4581    14045833 :         if (HAS_UNMAPPED_ID(idmap, inode))
    4582             :                 return -EPERM;
    4583    14045195 :         if (!dir->i_op->link)
    4584             :                 return -EPERM;
    4585    14045195 :         if (S_ISDIR(inode->i_mode))
    4586             :                 return -EPERM;
    4587             : 
    4588    14045195 :         error = security_inode_link(old_dentry, dir, new_dentry);
    4589    14045195 :         if (error)
    4590             :                 return error;
    4591             : 
    4592    14045195 :         inode_lock(inode);
    4593             :         /* Make sure we don't allow creating hardlink to an unlinked file */
    4594    14045899 :         if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
    4595             :                 error =  -ENOENT;
    4596    14045746 :         else if (max_links && inode->i_nlink >= max_links)
    4597             :                 error = -EMLINK;
    4598             :         else {
    4599    14045788 :                 error = try_break_deleg(inode, delegated_inode);
    4600    14045826 :                 if (!error)
    4601    14045367 :                         error = dir->i_op->link(old_dentry, dir, new_dentry);
    4602             :         }
    4603             : 
    4604    14047088 :         if (!error && (inode->i_state & I_LINKABLE)) {
    4605       51438 :                 spin_lock(&inode->i_lock);
    4606       51438 :                 inode->i_state &= ~I_LINKABLE;
    4607       51438 :                 spin_unlock(&inode->i_lock);
    4608             :         }
    4609    14047199 :         inode_unlock(inode);
    4610    14046613 :         if (!error)
    4611    13972958 :                 fsnotify_link(dir, inode, new_dentry);
    4612             :         return error;
    4613             : }
    4614             : EXPORT_SYMBOL(vfs_link);
    4615             : 
    4616             : /*
    4617             :  * Hardlinks are often used in delicate situations.  We avoid
    4618             :  * security-related surprises by not following symlinks on the
    4619             :  * newname.  --KAB
    4620             :  *
    4621             :  * We don't follow them on the oldname either to be compatible
    4622             :  * with linux 2.0, and to avoid hard-linking to directories
    4623             :  * and other special files.  --ADM
    4624             :  */
    4625    14135853 : int do_linkat(int olddfd, struct filename *old, int newdfd,
    4626             :               struct filename *new, int flags)
    4627             : {
    4628    14135853 :         struct mnt_idmap *idmap;
    4629    14135853 :         struct dentry *new_dentry;
    4630    14135853 :         struct path old_path, new_path;
    4631    14135853 :         struct inode *delegated_inode = NULL;
    4632    14135853 :         int how = 0;
    4633    14135853 :         int error;
    4634             : 
    4635    14135853 :         if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
    4636           0 :                 error = -EINVAL;
    4637           0 :                 goto out_putnames;
    4638             :         }
    4639             :         /*
    4640             :          * To use null names we require CAP_DAC_READ_SEARCH
    4641             :          * This ensures that not everyone will be able to create
    4642             :          * handlink using the passed filedescriptor.
    4643             :          */
    4644    14135853 :         if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
    4645           0 :                 error = -ENOENT;
    4646           0 :                 goto out_putnames;
    4647             :         }
    4648             : 
    4649    14135853 :         if (flags & AT_SYMLINK_FOLLOW)
    4650     3300297 :                 how |= LOOKUP_FOLLOW;
    4651    10835556 : retry:
    4652    14135853 :         error = filename_lookup(olddfd, old, how, &old_path, NULL);
    4653    14135586 :         if (error)
    4654       29895 :                 goto out_putnames;
    4655             : 
    4656    14105691 :         new_dentry = filename_create(newdfd, new, &new_path,
    4657             :                                         (how & LOOKUP_REVAL));
    4658    14104162 :         error = PTR_ERR(new_dentry);
    4659    14104162 :         if (IS_ERR(new_dentry))
    4660      102758 :                 goto out_putpath;
    4661             : 
    4662    14001404 :         error = -EXDEV;
    4663    14001404 :         if (old_path.mnt != new_path.mnt)
    4664          26 :                 goto out_dput;
    4665    14001378 :         idmap = mnt_idmap(new_path.mnt);
    4666    14001722 :         error = may_linkat(idmap, &old_path);
    4667    14002137 :         if (unlikely(error))
    4668        7778 :                 goto out_dput;
    4669    13994359 :         error = security_path_link(old_path.dentry, &new_path, new_dentry);
    4670    13994359 :         if (error)
    4671             :                 goto out_dput;
    4672    13994359 :         error = vfs_link(old_path.dentry, idmap, new_path.dentry->d_inode,
    4673             :                          new_dentry, &delegated_inode);
    4674    14003086 : out_dput:
    4675    14003086 :         done_path_create(&new_path, new_dentry);
    4676    14003208 :         if (delegated_inode) {
    4677           0 :                 error = break_deleg_wait(&delegated_inode);
    4678           0 :                 if (!error) {
    4679           0 :                         path_put(&old_path);
    4680           0 :                         goto retry;
    4681             :                 }
    4682             :         }
    4683    28006416 :         if (retry_estale(error, how)) {
    4684           5 :                 path_put(&old_path);
    4685           0 :                 how |= LOOKUP_REVAL;
    4686           0 :                 goto retry;
    4687             :         }
    4688    14003204 : out_putpath:
    4689    14105962 :         path_put(&old_path);
    4690    14135888 : out_putnames:
    4691    14135888 :         putname(old);
    4692    14135604 :         putname(new);
    4693             : 
    4694    14135771 :         return error;
    4695             : }
    4696             : 
    4697     7103836 : SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
    4698             :                 int, newdfd, const char __user *, newname, int, flags)
    4699             : {
    4700     7103822 :         return do_linkat(olddfd, getname_uflags(oldname, flags),
    4701             :                 newdfd, getname(newname), flags);
    4702             : }
    4703             : 
    4704    21168029 : SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
    4705             : {
    4706    21168110 :         return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
    4707             : }
    4708             : 
    4709             : /**
    4710             :  * vfs_rename - rename a filesystem object
    4711             :  * @rd:         pointer to &struct renamedata info
    4712             :  *
    4713             :  * The caller must hold multiple mutexes--see lock_rename()).
    4714             :  *
    4715             :  * If vfs_rename discovers a delegation in need of breaking at either
    4716             :  * the source or destination, it will return -EWOULDBLOCK and return a
    4717             :  * reference to the inode in delegated_inode.  The caller should then
    4718             :  * break the delegation and retry.  Because breaking a delegation may
    4719             :  * take a long time, the caller should drop all locks before doing
    4720             :  * so.
    4721             :  *
    4722             :  * Alternatively, a caller may pass NULL for delegated_inode.  This may
    4723             :  * be appropriate for callers that expect the underlying filesystem not
    4724             :  * to be NFS exported.
    4725             :  *
    4726             :  * The worst of all namespace operations - renaming directory. "Perverted"
    4727             :  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
    4728             :  * Problems:
    4729             :  *
    4730             :  *      a) we can get into loop creation.
    4731             :  *      b) race potential - two innocent renames can create a loop together.
    4732             :  *         That's where 4.4 screws up. Current fix: serialization on
    4733             :  *         sb->s_vfs_rename_mutex. We might be more accurate, but that's another
    4734             :  *         story.
    4735             :  *      c) we have to lock _four_ objects - parents and victim (if it exists),
    4736             :  *         and source.
    4737             :  *         And that - after we got ->i_mutex on parents (until then we don't know
    4738             :  *         whether the target exists).  Solution: try to be smart with locking
    4739             :  *         order for inodes.  We rely on the fact that tree topology may change
    4740             :  *         only under ->s_vfs_rename_mutex _and_ that parent of the object we
    4741             :  *         move will be locked.  Thus we can rank directories by the tree
    4742             :  *         (ancestors first) and rank all non-directories after them.
    4743             :  *         That works since everybody except rename does "lock parent, lookup,
    4744             :  *         lock child" and rename is under ->s_vfs_rename_mutex.
    4745             :  *         HOWEVER, it relies on the assumption that any object with ->lookup()
    4746             :  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
    4747             :  *         we'd better make sure that there's no link(2) for them.
    4748             :  *      d) conversion from fhandle to dentry may come in the wrong moment - when
    4749             :  *         we are removing the target. Solution: we will have to grab ->i_mutex
    4750             :  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
    4751             :  *         ->i_mutex on parents, which works but leads to some truly excessive
    4752             :  *         locking].
    4753             :  */
    4754    55303254 : int vfs_rename(struct renamedata *rd)
    4755             : {
    4756    55303254 :         int error;
    4757    55303254 :         struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
    4758    55303254 :         struct dentry *old_dentry = rd->old_dentry;
    4759    55303254 :         struct dentry *new_dentry = rd->new_dentry;
    4760    55303254 :         struct inode **delegated_inode = rd->delegated_inode;
    4761    55303254 :         unsigned int flags = rd->flags;
    4762    55303254 :         bool is_dir = d_is_dir(old_dentry);
    4763    55303254 :         struct inode *source = old_dentry->d_inode;
    4764    55303254 :         struct inode *target = new_dentry->d_inode;
    4765    55303254 :         bool new_is_dir = false;
    4766    55303254 :         unsigned max_links = new_dir->i_sb->s_max_links;
    4767    55303254 :         struct name_snapshot old_name;
    4768             : 
    4769    55303254 :         if (source == target)
    4770             :                 return 0;
    4771             : 
    4772    55114122 :         error = may_delete(rd->old_mnt_idmap, old_dir, old_dentry, is_dir);
    4773    55112951 :         if (error)
    4774             :                 return error;
    4775             : 
    4776    55112678 :         if (!target) {
    4777    37884927 :                 error = may_create(rd->new_mnt_idmap, new_dir, new_dentry);
    4778             :         } else {
    4779    17227751 :                 new_is_dir = d_is_dir(new_dentry);
    4780             : 
    4781    17227751 :                 if (!(flags & RENAME_EXCHANGE))
    4782     3196415 :                         error = may_delete(rd->new_mnt_idmap, new_dir,
    4783             :                                            new_dentry, is_dir);
    4784             :                 else
    4785    14031336 :                         error = may_delete(rd->new_mnt_idmap, new_dir,
    4786             :                                            new_dentry, new_is_dir);
    4787             :         }
    4788    55112960 :         if (error)
    4789             :                 return error;
    4790             : 
    4791    55097748 :         if (!old_dir->i_op->rename)
    4792             :                 return -EPERM;
    4793             : 
    4794             :         /*
    4795             :          * If we are going to change the parent - check write permissions,
    4796             :          * we'll need to flip '..'.
    4797             :          */
    4798    55097748 :         if (new_dir != old_dir) {
    4799    48108167 :                 if (is_dir) {
    4800    15259977 :                         error = inode_permission(rd->old_mnt_idmap, source,
    4801             :                                                  MAY_WRITE);
    4802    15259977 :                         if (error)
    4803             :                                 return error;
    4804             :                 }
    4805    48108167 :                 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
    4806     4103408 :                         error = inode_permission(rd->new_mnt_idmap, target,
    4807             :                                                  MAY_WRITE);
    4808     4103408 :                         if (error)
    4809             :                                 return error;
    4810             :                 }
    4811             :         }
    4812             : 
    4813    55097748 :         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
    4814             :                                       flags);
    4815    55097748 :         if (error)
    4816             :                 return error;
    4817             : 
    4818    55097748 :         take_dentry_name_snapshot(&old_name, old_dentry);
    4819    55097070 :         dget(new_dentry);
    4820             :         /*
    4821             :          * Lock all moved children. Moved directories may need to change parent
    4822             :          * pointer so they need the lock to prevent against concurrent
    4823             :          * directory changes moving parent pointer. For regular files we've
    4824             :          * historically always done this. The lockdep locking subclasses are
    4825             :          * somewhat arbitrary but RENAME_EXCHANGE in particular can swap
    4826             :          * regular files and directories so it's difficult to tell which
    4827             :          * subclasses to use.
    4828             :          */
    4829    55097800 :         lock_two_inodes(source, target, I_MUTEX_NORMAL, I_MUTEX_NONDIR2);
    4830             : 
    4831    55097188 :         error = -EPERM;
    4832    55097188 :         if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target)))
    4833           0 :                 goto out;
    4834             : 
    4835    55097188 :         error = -EBUSY;
    4836    55097188 :         if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
    4837           0 :                 goto out;
    4838             : 
    4839    55096823 :         if (max_links && new_dir != old_dir) {
    4840    47712706 :                 error = -EMLINK;
    4841    47712706 :                 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
    4842           0 :                         goto out;
    4843    47712706 :                 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
    4844          44 :                     old_dir->i_nlink >= max_links)
    4845           0 :                         goto out;
    4846             :         }
    4847    55096823 :         if (!is_dir) {
    4848    39768653 :                 error = try_break_deleg(source, delegated_inode);
    4849    39769865 :                 if (error)
    4850           0 :                         goto out;
    4851             :         }
    4852    55098035 :         if (target && !new_is_dir) {
    4853    13091519 :                 error = try_break_deleg(target, delegated_inode);
    4854    13091717 :                 if (error)
    4855           0 :                         goto out;
    4856             :         }
    4857    55098233 :         error = old_dir->i_op->rename(rd->new_mnt_idmap, old_dir, old_dentry,
    4858             :                                       new_dir, new_dentry, flags);
    4859    55099052 :         if (error)
    4860      628305 :                 goto out;
    4861             : 
    4862    54470747 :         if (!(flags & RENAME_EXCHANGE) && target) {
    4863     3181104 :                 if (is_dir) {
    4864        2301 :                         shrink_dcache_parent(new_dentry);
    4865        2301 :                         target->i_flags |= S_DEAD;
    4866             :                 }
    4867     3181104 :                 dont_mount(new_dentry);
    4868     3181249 :                 detach_mounts(new_dentry);
    4869             :         }
    4870    54470156 :         if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
    4871    54470156 :                 if (!(flags & RENAME_EXCHANGE))
    4872    40580531 :                         d_move(old_dentry, new_dentry);
    4873             :                 else
    4874    13889625 :                         d_exchange(old_dentry, new_dentry);
    4875             :         }
    4876           0 : out:
    4877    55099366 :         inode_unlock(source);
    4878    55099376 :         if (target)
    4879    17213840 :                 inode_unlock(target);
    4880    55099374 :         dput(new_dentry);
    4881    55099363 :         if (!error) {
    4882    54471061 :                 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
    4883    54471061 :                               !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
    4884    54471003 :                 if (flags & RENAME_EXCHANGE) {
    4885    13889662 :                         fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
    4886             :                                       new_is_dir, NULL, new_dentry);
    4887             :                 }
    4888             :         }
    4889    55099305 :         release_dentry_name_snapshot(&old_name);
    4890             : 
    4891    55099305 :         return error;
    4892             : }
    4893             : EXPORT_SYMBOL(vfs_rename);
    4894             : 
    4895    56827695 : int do_renameat2(int olddfd, struct filename *from, int newdfd,
    4896             :                  struct filename *to, unsigned int flags)
    4897             : {
    4898    56827695 :         struct renamedata rd;
    4899    56827695 :         struct dentry *old_dentry, *new_dentry;
    4900    56827695 :         struct dentry *trap;
    4901    56827695 :         struct path old_path, new_path;
    4902    56827695 :         struct qstr old_last, new_last;
    4903    56827695 :         int old_type, new_type;
    4904    56827695 :         struct inode *delegated_inode = NULL;
    4905    56827695 :         unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
    4906    56827695 :         bool should_retry = false;
    4907    56827695 :         int error = -EINVAL;
    4908             : 
    4909    56827695 :         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
    4910           0 :                 goto put_names;
    4911             : 
    4912    56827695 :         if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
    4913    21410101 :             (flags & RENAME_EXCHANGE))
    4914           0 :                 goto put_names;
    4915             : 
    4916    56827695 :         if (flags & RENAME_EXCHANGE)
    4917    14219978 :                 target_flags = 0;
    4918             : 
    4919    56827695 : retry:
    4920    56827695 :         error = filename_parentat(olddfd, from, lookup_flags, &old_path,
    4921             :                                   &old_last, &old_type);
    4922    56824491 :         if (error)
    4923          85 :                 goto put_names;
    4924             : 
    4925    56824406 :         error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
    4926             :                                   &new_type);
    4927    56827409 :         if (error)
    4928       24024 :                 goto exit1;
    4929             : 
    4930    56803385 :         error = -EXDEV;
    4931    56803385 :         if (old_path.mnt != new_path.mnt)
    4932          93 :                 goto exit2;
    4933             : 
    4934    56803292 :         error = -EBUSY;
    4935    56803292 :         if (old_type != LAST_NORM)
    4936           0 :                 goto exit2;
    4937             : 
    4938    56803292 :         if (flags & RENAME_NOREPLACE)
    4939    17526973 :                 error = -EEXIST;
    4940    56803292 :         if (new_type != LAST_NORM)
    4941           0 :                 goto exit2;
    4942             : 
    4943    56803292 :         error = mnt_want_write(old_path.mnt);
    4944    56802865 :         if (error)
    4945           0 :                 goto exit2;
    4946             : 
    4947    56802865 : retry_deleg:
    4948    56802865 :         trap = lock_rename(new_path.dentry, old_path.dentry);
    4949             : 
    4950    56806200 :         old_dentry = lookup_one_qstr_excl(&old_last, old_path.dentry,
    4951             :                                           lookup_flags);
    4952    56806718 :         error = PTR_ERR(old_dentry);
    4953    56806718 :         if (IS_ERR(old_dentry))
    4954          28 :                 goto exit3;
    4955             :         /* source must exist */
    4956    56806690 :         error = -ENOENT;
    4957    56806690 :         if (d_is_negative(old_dentry))
    4958       20744 :                 goto exit4;
    4959    56785946 :         new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry,
    4960             :                                           lookup_flags | target_flags);
    4961    56787550 :         error = PTR_ERR(new_dentry);
    4962    56787550 :         if (IS_ERR(new_dentry))
    4963        1094 :                 goto exit4;
    4964    56786456 :         error = -EEXIST;
    4965    56786456 :         if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
    4966     1364893 :                 goto exit5;
    4967    55421563 :         if (flags & RENAME_EXCHANGE) {
    4968    14220314 :                 error = -ENOENT;
    4969    14220314 :                 if (d_is_negative(new_dentry))
    4970         108 :                         goto exit5;
    4971             : 
    4972    24292249 :                 if (!d_is_dir(new_dentry)) {
    4973    10072043 :                         error = -ENOTDIR;
    4974    10072043 :                         if (new_last.name[new_last.len])
    4975           0 :                                 goto exit5;
    4976             :                 }
    4977             :         }
    4978             :         /* unless the source is a directory trailing slashes give -ENOTDIR */
    4979    95102894 :         if (!d_is_dir(old_dentry)) {
    4980    39681514 :                 error = -ENOTDIR;
    4981    39681514 :                 if (old_last.name[old_last.len])
    4982           0 :                         goto exit5;
    4983    39681514 :                 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
    4984           0 :                         goto exit5;
    4985             :         }
    4986             :         /* source should not be ancestor of target */
    4987    55421455 :         error = -EINVAL;
    4988    55421455 :         if (old_dentry == trap)
    4989      426885 :                 goto exit5;
    4990             :         /* target should not be an ancestor of source */
    4991    54994570 :         if (!(flags & RENAME_EXCHANGE))
    4992    40774493 :                 error = -ENOTEMPTY;
    4993    54994570 :         if (new_dentry == trap)
    4994           0 :                 goto exit5;
    4995             : 
    4996    54994570 :         error = security_path_rename(&old_path, old_dentry,
    4997             :                                      &new_path, new_dentry, flags);
    4998    54994570 :         if (error)
    4999             :                 goto exit5;
    5000             : 
    5001    54994570 :         rd.old_dir         = old_path.dentry->d_inode;
    5002    54994570 :         rd.old_dentry      = old_dentry;
    5003    54994570 :         rd.old_mnt_idmap   = mnt_idmap(old_path.mnt);
    5004    54994508 :         rd.new_dir         = new_path.dentry->d_inode;
    5005    54994508 :         rd.new_dentry      = new_dentry;
    5006    54994508 :         rd.new_mnt_idmap   = mnt_idmap(new_path.mnt);
    5007    54994156 :         rd.delegated_inode = &delegated_inode;
    5008    54994156 :         rd.flags           = flags;
    5009    54994156 :         error = vfs_rename(&rd);
    5010    56787124 : exit5:
    5011    56787124 :         dput(new_dentry);
    5012    56808012 : exit4:
    5013    56808012 :         dput(old_dentry);
    5014    56807673 : exit3:
    5015    56807673 :         unlock_rename(new_path.dentry, old_path.dentry);
    5016    56807438 :         if (delegated_inode) {
    5017           0 :                 error = break_deleg_wait(&delegated_inode);
    5018           0 :                 if (!error)
    5019           0 :                         goto retry_deleg;
    5020             :         }
    5021    56807438 :         mnt_drop_write(old_path.mnt);
    5022    56807437 : exit2:
    5023    56807530 :         if (retry_estale(error, lookup_flags))
    5024           0 :                 should_retry = true;
    5025    56807530 :         path_put(&new_path);
    5026    56831209 : exit1:
    5027    56831209 :         path_put(&old_path);
    5028    56831952 :         if (should_retry) {
    5029           0 :                 should_retry = false;
    5030           0 :                 lookup_flags |= LOOKUP_REVAL;
    5031           0 :                 goto retry;
    5032             :         }
    5033    56831952 : put_names:
    5034    56832037 :         putname(from);
    5035    56831189 :         putname(to);
    5036    56831524 :         return error;
    5037             : }
    5038             : 
    5039    71258510 : SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
    5040             :                 int, newdfd, const char __user *, newname, unsigned int, flags)
    5041             : {
    5042    71259429 :         return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
    5043             :                                 flags);
    5044             : }
    5045             : 
    5046        2894 : SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
    5047             :                 int, newdfd, const char __user *, newname)
    5048             : {
    5049        2894 :         return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
    5050             :                                 0);
    5051             : }
    5052             : 
    5053    42394732 : SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
    5054             : {
    5055    42395211 :         return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
    5056             :                                 getname(newname), 0);
    5057             : }
    5058             : 
    5059   297988125 : int readlink_copy(char __user *buffer, int buflen, const char *link)
    5060             : {
    5061   297988125 :         int len = PTR_ERR(link);
    5062   297988125 :         if (IS_ERR(link))
    5063           0 :                 goto out;
    5064             : 
    5065   297988125 :         len = strlen(link);
    5066   297988125 :         if (len > (unsigned) buflen)
    5067         400 :                 len = buflen;
    5068   596476543 :         if (copy_to_user(buffer, link, len))
    5069           0 :                 len = -EFAULT;
    5070   298488418 : out:
    5071   298488418 :         return len;
    5072             : }
    5073             : 
    5074             : /**
    5075             :  * vfs_readlink - copy symlink body into userspace buffer
    5076             :  * @dentry: dentry on which to get symbolic link
    5077             :  * @buffer: user memory pointer
    5078             :  * @buflen: size of buffer
    5079             :  *
    5080             :  * Does not touch atime.  That's up to the caller if necessary
    5081             :  *
    5082             :  * Does not call security hook.
    5083             :  */
    5084   298301918 : int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
    5085             : {
    5086   298301918 :         struct inode *inode = d_inode(dentry);
    5087   298301918 :         DEFINE_DELAYED_CALL(done);
    5088   298301918 :         const char *link;
    5089   298301918 :         int res;
    5090             : 
    5091   298301918 :         if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
    5092    27883021 :                 if (unlikely(inode->i_op->readlink))
    5093      405054 :                         return inode->i_op->readlink(dentry, buffer, buflen);
    5094             : 
    5095    27477967 :                 if (!d_is_symlink(dentry))
    5096             :                         return -EINVAL;
    5097             : 
    5098    27477967 :                 spin_lock(&inode->i_lock);
    5099    27485273 :                 inode->i_opflags |= IOP_DEFAULT_READLINK;
    5100    27485273 :                 spin_unlock(&inode->i_lock);
    5101             :         }
    5102             : 
    5103   297904240 :         link = READ_ONCE(inode->i_link);
    5104   297904240 :         if (!link) {
    5105   295189938 :                 link = inode->i_op->get_link(dentry, inode, &done);
    5106   295326437 :                 if (IS_ERR(link))
    5107          33 :                         return PTR_ERR(link);
    5108             :         }
    5109   298040706 :         res = readlink_copy(buffer, buflen, link);
    5110   298466378 :         do_delayed_call(&done);
    5111             :         return res;
    5112             : }
    5113             : EXPORT_SYMBOL(vfs_readlink);
    5114             : 
    5115             : /**
    5116             :  * vfs_get_link - get symlink body
    5117             :  * @dentry: dentry on which to get symbolic link
    5118             :  * @done: caller needs to free returned data with this
    5119             :  *
    5120             :  * Calls security hook and i_op->get_link() on the supplied inode.
    5121             :  *
    5122             :  * It does not touch atime.  That's up to the caller if necessary.
    5123             :  *
    5124             :  * Does not work on "special" symlinks like /proc/$$/fd/N
    5125             :  */
    5126    13890361 : const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
    5127             : {
    5128    13890361 :         const char *res = ERR_PTR(-EINVAL);
    5129    13890361 :         struct inode *inode = d_inode(dentry);
    5130             : 
    5131    13890361 :         if (d_is_symlink(dentry)) {
    5132    13890202 :                 res = ERR_PTR(security_inode_readlink(dentry));
    5133    13890202 :                 if (!res)
    5134    13890202 :                         res = inode->i_op->get_link(dentry, inode, done);
    5135             :         }
    5136    13890088 :         return res;
    5137             : }
    5138             : EXPORT_SYMBOL(vfs_get_link);
    5139             : 
    5140             : /* get the link contents into pagecache */
    5141       40302 : const char *page_get_link(struct dentry *dentry, struct inode *inode,
    5142             :                           struct delayed_call *callback)
    5143             : {
    5144       40302 :         char *kaddr;
    5145       40302 :         struct page *page;
    5146       40302 :         struct address_space *mapping = inode->i_mapping;
    5147             : 
    5148       40302 :         if (!dentry) {
    5149        1085 :                 page = find_get_page(mapping, 0);
    5150        1085 :                 if (!page)
    5151             :                         return ERR_PTR(-ECHILD);
    5152         977 :                 if (!PageUptodate(page)) {
    5153           2 :                         put_page(page);
    5154           2 :                         return ERR_PTR(-ECHILD);
    5155             :                 }
    5156             :         } else {
    5157       39217 :                 page = read_mapping_page(mapping, 0, NULL);
    5158       39212 :                 if (IS_ERR(page))
    5159             :                         return (char*)page;
    5160             :         }
    5161       40187 :         set_delayed_call(callback, page_put_link, page);
    5162       40187 :         BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
    5163       40187 :         kaddr = page_address(page);
    5164       40187 :         nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
    5165       40187 :         return kaddr;
    5166             : }
    5167             : 
    5168             : EXPORT_SYMBOL(page_get_link);
    5169             : 
    5170   414594385 : void page_put_link(void *arg)
    5171             : {
    5172   414594385 :         put_page(arg);
    5173   414597038 : }
    5174             : EXPORT_SYMBOL(page_put_link);
    5175             : 
    5176           0 : int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
    5177             : {
    5178           0 :         DEFINE_DELAYED_CALL(done);
    5179           0 :         int res = readlink_copy(buffer, buflen,
    5180             :                                 page_get_link(dentry, d_inode(dentry),
    5181             :                                               &done));
    5182           0 :         do_delayed_call(&done);
    5183           0 :         return res;
    5184             : }
    5185             : EXPORT_SYMBOL(page_readlink);
    5186             : 
    5187          26 : int page_symlink(struct inode *inode, const char *symname, int len)
    5188             : {
    5189          26 :         struct address_space *mapping = inode->i_mapping;
    5190          26 :         const struct address_space_operations *aops = mapping->a_ops;
    5191          26 :         bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS);
    5192          26 :         struct page *page;
    5193          26 :         void *fsdata = NULL;
    5194          26 :         int err;
    5195          26 :         unsigned int flags;
    5196             : 
    5197          26 : retry:
    5198          26 :         if (nofs)
    5199           0 :                 flags = memalloc_nofs_save();
    5200          26 :         err = aops->write_begin(NULL, mapping, 0, len-1, &page, &fsdata);
    5201          26 :         if (nofs)
    5202           0 :                 memalloc_nofs_restore(flags);
    5203          26 :         if (err)
    5204           1 :                 goto fail;
    5205             : 
    5206          50 :         memcpy(page_address(page), symname, len-1);
    5207             : 
    5208          25 :         err = aops->write_end(NULL, mapping, 0, len-1, len-1,
    5209             :                                                         page, fsdata);
    5210          25 :         if (err < 0)
    5211           0 :                 goto fail;
    5212          25 :         if (err < len-1)
    5213           0 :                 goto retry;
    5214             : 
    5215          25 :         mark_inode_dirty(inode);
    5216          25 :         return 0;
    5217             : fail:
    5218             :         return err;
    5219             : }
    5220             : EXPORT_SYMBOL(page_symlink);
    5221             : 
    5222             : const struct inode_operations page_symlink_inode_operations = {
    5223             :         .get_link       = page_get_link,
    5224             : };
    5225             : EXPORT_SYMBOL(page_symlink_inode_operations);

Generated by: LCOV version 1.14