Lines Matching +full:apr +full:- +full:domain

1 // SPDX-License-Identifier: GPL-2.0
12 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
15 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
47 /* [Feb-1997 T. Schoebel-Theuer]
50 * to know the _real_ pathname, not the user-supplied one, in case
54 * an iterative one (in case of non-nested symlink chains). It does
64 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
68 * There is still a significant amount of tail- and mid- recursion in
75 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
81 * the name is a symlink pointing to a non-existent name.
84 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
87 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
88 * file does succeed in both HP-UX and SunOs, but not in Solaris
92 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
95 * [10-Sep-98 Alan Modra] Another symlink change.
98 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
99 * inside the path - always follow.
100 * in the last component in creation/removal/renaming - never follow.
101 * if LOOKUP_FOLLOW passed - follow.
102 * if the pathname has trailing slashes - follow.
103 * otherwise - don't follow.
108 * During the 2.4 we need to fix the userland stuff depending on it -
113 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
114 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
123 * PATH_MAX includes the nul terminator --RR.
126 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
130 name->uptr = uptr; in initname()
131 name->aname = NULL; in initname()
132 atomic_set(&name->refcnt, 1); in initname()
148 return ERR_PTR(-ENOMEM); in getname_flags()
154 kname = (char *)result->iname; in getname_flags()
155 result->name = kname; in getname_flags()
170 return ERR_PTR(-ENOENT); in getname_flags()
175 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a in getname_flags()
177 * names_cache allocation for the pathname, and re-do the copy from in getname_flags()
186 * result->iname[0] is within the same object and that in getname_flags()
187 * kname can't be equal to result->iname, no matter what. in getname_flags()
192 return ERR_PTR(-ENOMEM); in getname_flags()
194 result->name = kname; in getname_flags()
205 return ERR_PTR(-ENOENT); in getname_flags()
210 return ERR_PTR(-ENAMETOOLONG); in getname_flags()
232 return ERR_PTR(-EFAULT); in __getname_maybe_null()
237 if (!IS_ERR(name) && !(name->name[0])) { in __getname_maybe_null()
251 return ERR_PTR(-ENOMEM); in getname_kernel()
254 result->name = (char *)result->iname; in getname_kernel()
262 return ERR_PTR(-ENOMEM); in getname_kernel()
264 tmp->name = (char *)result; in getname_kernel()
268 return ERR_PTR(-ENAMETOOLONG); in getname_kernel()
270 memcpy((char *)result->name, filename, len); in getname_kernel()
284 refcnt = atomic_read(&name->refcnt); in putname()
289 if (!atomic_dec_and_test(&name->refcnt)) in putname()
293 if (name->name != name->iname) { in putname()
294 __putname(name->name); in putname()
302 * check_acl - perform ACL permission checking
309 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
314 * On non-idmapped mounts or if permission checking is to be performed on the
326 return -EAGAIN; in check_acl()
327 /* no ->get_inode_acl() calls in RCU mode... */ in check_acl()
329 return -ECHILD; in check_acl()
343 return -EAGAIN; in check_acl()
359 return likely(!READ_ONCE(inode->i_acl)); in no_acl_inode()
366 * acl_permission_check - perform basic UNIX permission checking
373 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
378 * On non-idmapped mounts or if permission checking is to be performed on the
384 unsigned int mode = inode->i_mode; in acl_permission_check()
392 * - 'mask&7' is the requested permission bit set in acl_permission_check()
393 * - multiplying by 0111 spreads them out to all of ugo in acl_permission_check()
394 * - '& ~mode' looks for missing inode permission bits in acl_permission_check()
395 * - the '!' is for "no missing permissions" in acl_permission_check()
398 * ACL's on the inode - do the 'IS_POSIXACL()' check last in acl_permission_check()
399 * because it will dereference the ->i_sb pointer and we in acl_permission_check()
414 return (mask & ~mode) ? -EACCES : 0; in acl_permission_check()
420 if (error != -EAGAIN) in acl_permission_check()
439 return (mask & ~mode) ? -EACCES : 0; in acl_permission_check()
443 * generic_permission - check for access rights on a Posix-like filesystem
454 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
456 * It would then be called again in ref-walk mode.
461 * On non-idmapped mounts or if permission checking is to be performed on the
473 if (ret != -EACCES) in generic_permission()
476 if (S_ISDIR(inode->i_mode)) { in generic_permission()
485 return -EACCES; in generic_permission()
501 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) in generic_permission()
506 return -EACCES; in generic_permission()
511 * do_inode_permission - UNIX permission checking
517 * even looking at the inode->i_op values. So we keep a cache
518 * flag in inode->i_opflags, that says "this has not special
524 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { in do_inode_permission()
525 if (likely(inode->i_op->permission)) in do_inode_permission()
526 return inode->i_op->permission(idmap, inode, mask); in do_inode_permission()
529 spin_lock(&inode->i_lock); in do_inode_permission()
530 inode->i_opflags |= IOP_FASTPERM; in do_inode_permission()
531 spin_unlock(&inode->i_lock); in do_inode_permission()
537 * sb_permission - Check superblock-level permissions
542 * Separate out file-system wide checks from inode-specific permission checks.
547 umode_t mode = inode->i_mode; in sb_permission()
549 /* Nobody gets write access to a read-only fs. */ in sb_permission()
551 return -EROFS; in sb_permission()
557 * inode_permission - Check for access rights to a given inode
573 retval = sb_permission(inode->i_sb, inode, mask); in inode_permission()
582 return -EPERM; in inode_permission()
590 return -EACCES; in inode_permission()
606 * path_get - get a reference to a path
613 mntget(path->mnt); in path_get()
614 dget(path->dentry); in path_get()
619 * path_put - put a reference to a path
626 dput(path->dentry); in path_put()
627 mntput(path->mnt); in path_put()
663 struct nameidata *old = current->nameidata; in __set_nameidata()
664 p->stack = p->internal; in __set_nameidata()
665 p->depth = 0; in __set_nameidata()
666 p->dfd = dfd; in __set_nameidata()
667 p->name = name; in __set_nameidata()
668 p->pathname = likely(name) ? name->name : ""; in __set_nameidata()
669 p->path.mnt = NULL; in __set_nameidata()
670 p->path.dentry = NULL; in __set_nameidata()
671 p->total_link_count = old ? old->total_link_count : 0; in __set_nameidata()
672 p->saved = old; in __set_nameidata()
673 current->nameidata = p; in __set_nameidata()
680 p->state = 0; in set_nameidata()
682 p->state = ND_ROOT_PRESET; in set_nameidata()
683 p->root = *root; in set_nameidata()
689 struct nameidata *now = current->nameidata, *old = now->saved; in restore_nameidata()
691 current->nameidata = old; in restore_nameidata()
693 old->total_link_count = now->total_link_count; in restore_nameidata()
694 if (now->stack != now->internal) in restore_nameidata()
695 kfree(now->stack); in restore_nameidata()
703 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); in nd_alloc_stack()
706 memcpy(p, nd->internal, sizeof(nd->internal)); in nd_alloc_stack()
707 nd->stack = p; in nd_alloc_stack()
712 * path_connected - Verify that a dentry is below mnt.mnt_root
721 struct super_block *sb = mnt->mnt_sb; in path_connected()
724 if (mnt->mnt_root == sb->s_root) in path_connected()
727 return is_subdir(dentry, mnt->mnt_root); in path_connected()
732 int i = nd->depth; in drop_links()
733 while (i--) { in drop_links()
734 struct saved *last = nd->stack + i; in drop_links()
735 do_delayed_call(&last->done); in drop_links()
736 clear_delayed_call(&last->done); in drop_links()
742 nd->flags &= ~LOOKUP_RCU; in leave_rcu()
743 nd->seq = nd->next_seq = 0; in leave_rcu()
750 if (!(nd->flags & LOOKUP_RCU)) { in terminate_walk()
752 path_put(&nd->path); in terminate_walk()
753 for (i = 0; i < nd->depth; i++) in terminate_walk()
754 path_put(&nd->stack[i].link); in terminate_walk()
755 if (nd->state & ND_ROOT_GRABBED) { in terminate_walk()
756 path_put(&nd->root); in terminate_walk()
757 nd->state &= ~ND_ROOT_GRABBED; in terminate_walk()
762 nd->depth = 0; in terminate_walk()
763 nd->path.mnt = NULL; in terminate_walk()
764 nd->path.dentry = NULL; in terminate_walk()
770 int res = __legitimize_mnt(path->mnt, mseq); in __legitimize_path()
773 path->mnt = NULL; in __legitimize_path()
774 path->dentry = NULL; in __legitimize_path()
777 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { in __legitimize_path()
778 path->dentry = NULL; in __legitimize_path()
781 return !read_seqcount_retry(&path->dentry->d_seq, seq); in __legitimize_path()
787 return __legitimize_path(path, seq, nd->m_seq); in legitimize_path()
793 if (unlikely(nd->flags & LOOKUP_CACHED)) { in legitimize_links()
795 nd->depth = 0; in legitimize_links()
798 for (i = 0; i < nd->depth; i++) { in legitimize_links()
799 struct saved *last = nd->stack + i; in legitimize_links()
800 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { in legitimize_links()
802 nd->depth = i + 1; in legitimize_links()
811 /* Nothing to do if nd->root is zero or is managed by the VFS user. */ in legitimize_root()
812 if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) in legitimize_root()
814 nd->state |= ND_ROOT_GRABBED; in legitimize_root()
815 return legitimize_path(nd, &nd->root, nd->root_seq); in legitimize_root()
819 * Path walking has 2 modes, rcu-walk and ref-walk (see
820 * Documentation/filesystems/path-lookup.txt). In situations when we can't
821 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
822 * normal reference counts on dentries and vfsmounts to transition to ref-walk
823 * mode. Refcounts are grabbed at the last known good point before rcu-walk
824 * got stuck, so ref-walk may continue from there. If this is not successful
826 * to restart the path walk from the beginning in ref-walk mode.
830 * try_to_unlazy - try to switch to ref-walk mode.
834 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
835 * for ref-walk mode.
836 * Must be called from rcu-walk context.
842 struct dentry *parent = nd->path.dentry; in try_to_unlazy()
844 BUG_ON(!(nd->flags & LOOKUP_RCU)); in try_to_unlazy()
848 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) in try_to_unlazy()
853 BUG_ON(nd->inode != parent->d_inode); in try_to_unlazy()
857 nd->path.mnt = NULL; in try_to_unlazy()
858 nd->path.dentry = NULL; in try_to_unlazy()
865 * try_to_unlazy_next - try to switch to ref-walk mode.
871 * picked by rcu-walk and want to legitimize that in addition to the current
872 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
879 BUG_ON(!(nd->flags & LOOKUP_RCU)); in try_to_unlazy_next()
883 res = __legitimize_mnt(nd->path.mnt, nd->m_seq); in try_to_unlazy_next()
889 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) in try_to_unlazy_next()
893 * We need to move both the parent and the dentry from the RCU domain in try_to_unlazy_next()
899 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) in try_to_unlazy_next()
901 if (read_seqcount_retry(&dentry->d_seq, nd->next_seq)) in try_to_unlazy_next()
913 nd->path.mnt = NULL; in try_to_unlazy_next()
915 nd->path.dentry = NULL; in try_to_unlazy_next()
928 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) in d_revalidate()
929 return dentry->d_op->d_revalidate(dir, name, dentry, flags); in d_revalidate()
935 * complete_walk - successful completion of path walk
938 * If we had been in RCU mode, drop out of it and legitimize nd->path.
941 * success, -error on failure. In case of failure caller does not
942 * need to drop nd->path.
946 struct dentry *dentry = nd->path.dentry; in complete_walk()
949 if (nd->flags & LOOKUP_RCU) { in complete_walk()
951 * We don't want to zero nd->root for scoped-lookups or in complete_walk()
952 * externally-managed nd->root. in complete_walk()
954 if (!(nd->state & ND_ROOT_PRESET)) in complete_walk()
955 if (!(nd->flags & LOOKUP_IS_SCOPED)) in complete_walk()
956 nd->root.mnt = NULL; in complete_walk()
957 nd->flags &= ~LOOKUP_CACHED; in complete_walk()
959 return -ECHILD; in complete_walk()
962 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { in complete_walk()
970 * So, do a final sanity-check to make sure that in the in complete_walk()
971 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) in complete_walk()
979 if (!path_is_under(&nd->path, &nd->root)) in complete_walk()
980 return -EXDEV; in complete_walk()
983 if (likely(!(nd->state & ND_JUMPED))) in complete_walk()
986 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) in complete_walk()
989 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); in complete_walk()
994 status = -ESTALE; in complete_walk()
1001 struct fs_struct *fs = current->fs; in set_root()
1004 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we in set_root()
1008 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) in set_root()
1009 return -ENOTRECOVERABLE; in set_root()
1011 if (nd->flags & LOOKUP_RCU) { in set_root()
1015 seq = read_seqbegin(&fs->seq); in set_root()
1016 nd->root = fs->root; in set_root()
1017 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); in set_root()
1018 } while (read_seqretry(&fs->seq, seq)); in set_root()
1020 get_fs_root(fs, &nd->root); in set_root()
1021 nd->state |= ND_ROOT_GRABBED; in set_root()
1028 if (unlikely(nd->flags & LOOKUP_BENEATH)) in nd_jump_root()
1029 return -EXDEV; in nd_jump_root()
1030 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { in nd_jump_root()
1032 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) in nd_jump_root()
1033 return -EXDEV; in nd_jump_root()
1035 if (!nd->root.mnt) { in nd_jump_root()
1040 if (nd->flags & LOOKUP_RCU) { in nd_jump_root()
1042 nd->path = nd->root; in nd_jump_root()
1043 d = nd->path.dentry; in nd_jump_root()
1044 nd->inode = d->d_inode; in nd_jump_root()
1045 nd->seq = nd->root_seq; in nd_jump_root()
1046 if (read_seqcount_retry(&d->d_seq, nd->seq)) in nd_jump_root()
1047 return -ECHILD; in nd_jump_root()
1049 path_put(&nd->path); in nd_jump_root()
1050 nd->path = nd->root; in nd_jump_root()
1051 path_get(&nd->path); in nd_jump_root()
1052 nd->inode = nd->path.dentry->d_inode; in nd_jump_root()
1054 nd->state |= ND_JUMPED; in nd_jump_root()
1059 * Helper to directly jump to a known parsed path from ->get_link,
1064 int error = -ELOOP; in nd_jump_link()
1065 struct nameidata *nd = current->nameidata; in nd_jump_link()
1067 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) in nd_jump_link()
1070 error = -EXDEV; in nd_jump_link()
1071 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { in nd_jump_link()
1072 if (nd->path.mnt != path->mnt) in nd_jump_link()
1075 /* Not currently safe for scoped-lookups. */ in nd_jump_link()
1076 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) in nd_jump_link()
1079 path_put(&nd->path); in nd_jump_link()
1080 nd->path = *path; in nd_jump_link()
1081 nd->inode = nd->path.dentry->d_inode; in nd_jump_link()
1082 nd->state |= ND_JUMPED; in nd_jump_link()
1092 struct saved *last = nd->stack + --nd->depth; in put_link()
1093 do_delayed_call(&last->done); in put_link()
1094 if (!(nd->flags & LOOKUP_RCU)) in put_link()
1095 path_put(&last->link); in put_link()
1153 * may_follow_link - Check symlink following for unsafe situations
1159 * in a sticky world-writable directory. This is to protect privileged
1163 * world-writable directory, or when the uid of the symlink and follower
1166 * Returns 0 if following the symlink is allowed, -ve on error.
1176 idmap = mnt_idmap(nd->path.mnt); in may_follow_link()
1182 /* Allowed if parent directory not sticky and world-writable. */ in may_follow_link()
1183 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) in may_follow_link()
1187 if (vfsuid_valid(nd->dir_vfsuid) && vfsuid_eq(nd->dir_vfsuid, vfsuid)) in may_follow_link()
1190 if (nd->flags & LOOKUP_RCU) in may_follow_link()
1191 return -ECHILD; in may_follow_link()
1193 audit_inode(nd->name, nd->stack[0].link.dentry, 0); in may_follow_link()
1195 return -EACCES; in may_follow_link()
1199 * safe_hardlink_source - Check for safe hardlink conditions
1204 * - inode is not a regular file
1205 * - inode is setuid
1206 * - inode is setgid and group-exec
1207 * - access failure for read and write
1214 umode_t mode = inode->i_mode; in safe_hardlink_source()
1236 * may_linkat - Check permissions for creating a hardlink
1241 * - sysctl_protected_hardlinks enabled
1242 * - fsuid does not match inode
1243 * - hardlink source is unsafe (see safe_hardlink_source() above)
1244 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
1249 * On non-idmapped mounts or if permission checking is to be performed on the
1252 * Returns 0 if successful, -ve on error.
1256 struct inode *inode = link->dentry->d_inode; in may_linkat()
1261 return -EOVERFLOW; in may_linkat()
1274 return -EPERM; in may_linkat()
1278 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1286 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1287 * - the file already exists
1288 * - we are in a sticky directory
1289 * - we don't own the file
1290 * - the owner of the directory doesn't own the file
1291 * - the directory is world writable
1299 * On non-idmapped mounts or if permission checking is to be performed on the
1302 * Returns 0 if the open is allowed, -ve on error.
1307 umode_t dir_mode = nd->dir_mode; in may_create_in_sticky()
1308 vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; in may_create_in_sticky()
1313 if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) in may_create_in_sticky()
1316 if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) in may_create_in_sticky()
1329 return -EACCES; in may_create_in_sticky()
1333 if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { in may_create_in_sticky()
1336 return -EACCES; in may_create_in_sticky()
1339 if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { in may_create_in_sticky()
1342 return -EACCES; in may_create_in_sticky()
1350 * follow_up - Find the mountpoint of path's vfsmount
1361 struct mount *mnt = real_mount(path->mnt); in follow_up()
1366 parent = mnt->mnt_parent; in follow_up()
1371 mntget(&parent->mnt); in follow_up()
1372 mountpoint = dget(mnt->mnt_mountpoint); in follow_up()
1374 dput(path->dentry); in follow_up()
1375 path->dentry = mountpoint; in follow_up()
1376 mntput(path->mnt); in follow_up()
1377 path->mnt = &parent->mnt; in follow_up()
1386 struct dentry *mountpoint = m->mnt_mountpoint; in choose_mountpoint_rcu()
1388 m = m->mnt_parent; in choose_mountpoint_rcu()
1389 if (unlikely(root->dentry == mountpoint && in choose_mountpoint_rcu()
1390 root->mnt == &m->mnt)) in choose_mountpoint_rcu()
1392 if (mountpoint != m->mnt.mnt_root) { in choose_mountpoint_rcu()
1393 path->mnt = &m->mnt; in choose_mountpoint_rcu()
1394 path->dentry = mountpoint; in choose_mountpoint_rcu()
1395 *seqp = read_seqcount_begin(&mountpoint->d_seq); in choose_mountpoint_rcu()
1429 * - return -EISDIR to tell follow_managed() to stop and return the path we
1434 struct dentry *dentry = path->dentry; in follow_automount()
1436 /* We don't want to mount if someone's just doing a stat - in follow_automount()
1449 dentry->d_inode) in follow_automount()
1450 return -EISDIR; in follow_automount()
1453 return -ELOOP; in follow_automount()
1455 return finish_automount(dentry->d_op->d_automount(path), path); in follow_automount()
1459 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1462 * sufficient for ->d_inode and ->d_flags consistency.
1467 struct vfsmount *mnt = path->mnt; in __traverse_mounts()
1475 ret = path->dentry->d_op->d_manage(path, false); in __traverse_mounts()
1476 flags = smp_load_acquire(&path->dentry->d_flags); in __traverse_mounts()
1484 dput(path->dentry); in __traverse_mounts()
1486 mntput(path->mnt); in __traverse_mounts()
1487 path->mnt = mounted; in __traverse_mounts()
1488 path->dentry = dget(mounted->mnt_root); in __traverse_mounts()
1490 flags = path->dentry->d_flags; in __traverse_mounts()
1501 flags = smp_load_acquire(&path->dentry->d_flags); in __traverse_mounts()
1506 if (ret == -EISDIR) in __traverse_mounts()
1508 // possible if you race with several mount --move in __traverse_mounts()
1509 if (need_mntput && path->mnt == mnt) in __traverse_mounts()
1510 mntput(path->mnt); in __traverse_mounts()
1512 ret = -ENOENT; in __traverse_mounts()
1520 unsigned flags = smp_load_acquire(&path->dentry->d_flags); in traverse_mounts()
1526 return -ENOENT; in traverse_mounts()
1538 dput(path->dentry); in follow_down_one()
1539 mntput(path->mnt); in follow_down_one()
1540 path->mnt = mounted; in follow_down_one()
1541 path->dentry = dget(mounted->mnt_root); in follow_down_one()
1555 struct vfsmount *mnt = path->mnt; in follow_down()
1559 if (path->mnt != mnt) in follow_down()
1571 struct dentry *dentry = path->dentry; in __follow_mount_rcu()
1572 unsigned int flags = dentry->d_flags; in __follow_mount_rcu()
1577 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in __follow_mount_rcu()
1582 * Don't forget we might have a non-mountpoint managed dentry in __follow_mount_rcu()
1586 int res = dentry->d_op->d_manage(path, true); in __follow_mount_rcu()
1588 return res == -EISDIR; in __follow_mount_rcu()
1589 flags = dentry->d_flags; in __follow_mount_rcu()
1593 struct mount *mounted = __lookup_mnt(path->mnt, dentry); in __follow_mount_rcu()
1595 path->mnt = &mounted->mnt; in __follow_mount_rcu()
1596 dentry = path->dentry = mounted->mnt.mnt_root; in __follow_mount_rcu()
1597 nd->state |= ND_JUMPED; in __follow_mount_rcu()
1598 nd->next_seq = read_seqcount_begin(&dentry->d_seq); in __follow_mount_rcu()
1599 flags = dentry->d_flags; in __follow_mount_rcu()
1600 // makes sure that non-RCU pathwalk could reach in __follow_mount_rcu()
1602 if (read_seqretry(&mount_lock, nd->m_seq)) in __follow_mount_rcu()
1606 if (read_seqretry(&mount_lock, nd->m_seq)) in __follow_mount_rcu()
1619 path->mnt = nd->path.mnt; in handle_mounts()
1620 path->dentry = dentry; in handle_mounts()
1621 if (nd->flags & LOOKUP_RCU) { in handle_mounts()
1622 unsigned int seq = nd->next_seq; in handle_mounts()
1625 // *path and nd->next_seq might've been clobbered in handle_mounts()
1626 path->mnt = nd->path.mnt; in handle_mounts()
1627 path->dentry = dentry; in handle_mounts()
1628 nd->next_seq = seq; in handle_mounts()
1630 return -ECHILD; in handle_mounts()
1632 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); in handle_mounts()
1634 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in handle_mounts()
1635 ret = -EXDEV; in handle_mounts()
1637 nd->state |= ND_JUMPED; in handle_mounts()
1640 dput(path->dentry); in handle_mounts()
1641 if (path->mnt != nd->path.mnt) in handle_mounts()
1642 mntput(path->mnt); in handle_mounts()
1657 int error = d_revalidate(dir->d_inode, name, dentry, flags); in lookup_dcache()
1670 * and only case when ->lookup() gets called on non in-lookup
1671 * dentries - as the matter of fact, this only gets called
1672 * when directory is guaranteed to have no in-lookup children
1674 * Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
1675 * Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
1689 dir = base->d_inode; in lookup_one_qstr_excl()
1691 return ERR_PTR(-ENOENT); in lookup_one_qstr_excl()
1695 return ERR_PTR(-ENOMEM); in lookup_one_qstr_excl()
1697 old = dir->i_op->lookup(dir, dentry, flags); in lookup_one_qstr_excl()
1707 return ERR_PTR(-ENOENT); in lookup_one_qstr_excl()
1711 return ERR_PTR(-EEXIST); in lookup_one_qstr_excl()
1718 * lookup_fast - do fast lockless (but racy) lookup of a dentry
1733 struct dentry *dentry, *parent = nd->path.dentry; in lookup_fast()
1739 * going to fall back to non-racy lookup. in lookup_fast()
1741 if (nd->flags & LOOKUP_RCU) { in lookup_fast()
1742 dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq); in lookup_fast()
1745 return ERR_PTR(-ECHILD); in lookup_fast()
1753 if (read_seqcount_retry(&parent->d_seq, nd->seq)) in lookup_fast()
1754 return ERR_PTR(-ECHILD); in lookup_fast()
1756 status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags); in lookup_fast()
1760 return ERR_PTR(-ECHILD); in lookup_fast()
1761 if (status == -ECHILD) in lookup_fast()
1762 /* we'd been told to redo it in non-rcu mode */ in lookup_fast()
1763 status = d_revalidate(nd->inode, &nd->last, in lookup_fast()
1764 dentry, nd->flags); in lookup_fast()
1766 dentry = __d_lookup(parent, &nd->last); in lookup_fast()
1769 status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags); in lookup_fast()
1786 struct inode *inode = dir->d_inode; in __lookup_slow()
1791 return ERR_PTR(-ENOENT); in __lookup_slow()
1808 old = inode->i_op->lookup(inode, dentry, flags); in __lookup_slow()
1822 struct inode *inode = dir->d_inode; in lookup_slow()
1835 mask = nd->flags & LOOKUP_RCU ? MAY_NOT_BLOCK : 0; in may_lookup()
1836 err = inode_permission(idmap, nd->inode, mask | MAY_EXEC); in may_lookup()
1841 if (!(nd->flags & LOOKUP_RCU)) in may_lookup()
1846 return -ECHILD; // redo it all non-lazy in may_lookup()
1848 if (err != -ECHILD) // hard error in may_lookup()
1851 return inode_permission(idmap, nd->inode, MAY_EXEC); in may_lookup()
1856 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) in reserve_stack()
1857 return -ELOOP; in reserve_stack()
1859 if (likely(nd->depth != EMBEDDED_LEVELS)) in reserve_stack()
1861 if (likely(nd->stack != nd->internal)) in reserve_stack()
1866 if (nd->flags & LOOKUP_RCU) { in reserve_stack()
1868 // unlazy even if we fail to grab the link - cleanup needs it in reserve_stack()
1869 bool grabbed_link = legitimize_path(nd, link, nd->next_seq); in reserve_stack()
1872 return -ECHILD; in reserve_stack()
1877 return -ENOMEM; in reserve_stack()
1890 if (!(nd->flags & LOOKUP_RCU)) in pick_link()
1894 last = nd->stack + nd->depth++; in pick_link()
1895 last->link = *link; in pick_link()
1896 clear_delayed_call(&last->done); in pick_link()
1897 last->seq = nd->next_seq; in pick_link()
1905 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || in pick_link()
1906 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) in pick_link()
1907 return ERR_PTR(-ELOOP); in pick_link()
1909 if (unlikely(atime_needs_update(&last->link, inode))) { in pick_link()
1910 if (nd->flags & LOOKUP_RCU) { in pick_link()
1912 return ERR_PTR(-ECHILD); in pick_link()
1914 touch_atime(&last->link); in pick_link()
1918 error = security_inode_follow_link(link->dentry, inode, in pick_link()
1919 nd->flags & LOOKUP_RCU); in pick_link()
1923 res = READ_ONCE(inode->i_link); in pick_link()
1927 get = inode->i_op->get_link; in pick_link()
1928 if (nd->flags & LOOKUP_RCU) { in pick_link()
1929 res = get(NULL, inode, &last->done); in pick_link()
1930 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) in pick_link()
1931 res = get(link->dentry, inode, &last->done); in pick_link()
1933 res = get(link->dentry, inode, &last->done); in pick_link()
1956 * to do this check without having to look at inode->i_op,
1960 * NOTE: dentry must be what nd->next_seq had been sampled from.
1971 inode = path.dentry->d_inode; in step_into()
1973 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || in step_into()
1976 if (nd->flags & LOOKUP_RCU) { in step_into()
1977 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) in step_into()
1978 return ERR_PTR(-ECHILD); in step_into()
1980 return ERR_PTR(-ENOENT); in step_into()
1982 dput(nd->path.dentry); in step_into()
1983 if (nd->path.mnt != path.mnt) in step_into()
1984 mntput(nd->path.mnt); in step_into()
1986 nd->path = path; in step_into()
1987 nd->inode = inode; in step_into()
1988 nd->seq = nd->next_seq; in step_into()
1991 if (nd->flags & LOOKUP_RCU) { in step_into()
1993 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) in step_into()
1994 return ERR_PTR(-ECHILD); in step_into()
1996 if (path.mnt == nd->path.mnt) in step_into()
2006 if (path_equal(&nd->path, &nd->root)) in follow_dotdot_rcu()
2008 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { in follow_dotdot_rcu()
2011 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), in follow_dotdot_rcu()
2012 &nd->root, &path, &seq)) in follow_dotdot_rcu()
2014 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in follow_dotdot_rcu()
2015 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2016 nd->path = path; in follow_dotdot_rcu()
2017 nd->inode = path.dentry->d_inode; in follow_dotdot_rcu()
2018 nd->seq = seq; in follow_dotdot_rcu()
2019 // makes sure that non-RCU pathwalk could reach this state in follow_dotdot_rcu()
2020 if (read_seqretry(&mount_lock, nd->m_seq)) in follow_dotdot_rcu()
2021 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2024 old = nd->path.dentry; in follow_dotdot_rcu()
2025 parent = old->d_parent; in follow_dotdot_rcu()
2026 nd->next_seq = read_seqcount_begin(&parent->d_seq); in follow_dotdot_rcu()
2027 // makes sure that non-RCU pathwalk could reach this state in follow_dotdot_rcu()
2028 if (read_seqcount_retry(&old->d_seq, nd->seq)) in follow_dotdot_rcu()
2029 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2030 if (unlikely(!path_connected(nd->path.mnt, parent))) in follow_dotdot_rcu()
2031 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2034 if (read_seqretry(&mount_lock, nd->m_seq)) in follow_dotdot_rcu()
2035 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2036 if (unlikely(nd->flags & LOOKUP_BENEATH)) in follow_dotdot_rcu()
2037 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2038 nd->next_seq = nd->seq; in follow_dotdot_rcu()
2039 return nd->path.dentry; in follow_dotdot_rcu()
2046 if (path_equal(&nd->path, &nd->root)) in follow_dotdot()
2048 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { in follow_dotdot()
2051 if (!choose_mountpoint(real_mount(nd->path.mnt), in follow_dotdot()
2052 &nd->root, &path)) in follow_dotdot()
2054 path_put(&nd->path); in follow_dotdot()
2055 nd->path = path; in follow_dotdot()
2056 nd->inode = path.dentry->d_inode; in follow_dotdot()
2057 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in follow_dotdot()
2058 return ERR_PTR(-EXDEV); in follow_dotdot()
2061 parent = dget_parent(nd->path.dentry); in follow_dotdot()
2062 if (unlikely(!path_connected(nd->path.mnt, parent))) { in follow_dotdot()
2064 return ERR_PTR(-ENOENT); in follow_dotdot()
2069 if (unlikely(nd->flags & LOOKUP_BENEATH)) in follow_dotdot()
2070 return ERR_PTR(-EXDEV); in follow_dotdot()
2071 return dget(nd->path.dentry); in follow_dotdot()
2080 if (!nd->root.mnt) { in handle_dots()
2085 if (nd->flags & LOOKUP_RCU) in handle_dots()
2095 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { in handle_dots()
2099 * above nd->root (and so userspace should retry or use in handle_dots()
2103 if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)) in handle_dots()
2104 return ERR_PTR(-EAGAIN); in handle_dots()
2105 if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)) in handle_dots()
2106 return ERR_PTR(-EAGAIN); in handle_dots()
2116 * "." and ".." are special - ".." especially so because it has in walk_component()
2120 if (unlikely(nd->last_type != LAST_NORM)) { in walk_component()
2121 if (!(flags & WALK_MORE) && nd->depth) in walk_component()
2123 return handle_dots(nd, nd->last_type); in walk_component()
2129 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); in walk_component()
2133 if (!(flags & WALK_MORE) && nd->depth) in walk_component()
2142 * - Architectures with fast unaligned word accesses. We could
2146 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2150 * - Furthermore, we need an efficient 64-bit compile for the
2151 * 64-bit case in order to generate the "number of bytes in
2157 #include <asm/word-at-a-time.h>
2166 * on 32-bit x86, but almost any function requires one state value and
2175 * Rotate constants are scored by considering either 64 one-bit input
2176 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2185 * Input delta: 1-bit 2-bit
2200 * Fold two longs into one 32-bit hash value. This must be fast, but
2211 #else /* 32-bit case */
2215 * Input delta: 1-bit 2-bit
2231 /* Use arch-optimized multiply if one exists */ in fold_hash()
2256 len -= sizeof(unsigned long); in full_name_hash()
2264 /* Return the "hash_len" (hash and length) of a null-terminated string */
2297 unsigned long a, b, x, y = (unsigned long)nd->path.dentry; in hash_name()
2314 nd->last.hash = fold_hash(a, y); in hash_name()
2315 nd->last.len = len; in hash_name()
2334 *lastword = 0; // Multi-word components cannot be DOT or DOTDOT in hash_name()
2336 nd->last.hash = fold_hash(x, y); in hash_name()
2337 nd->last.len = len; in hash_name()
2342 * Note that the 'last' word is always zero-masked, but
2343 * was loaded as a possibly big-endian word.
2346 #define LAST_WORD_IS_DOT (0x2eul << (BITS_PER_LONG-8))
2347 #define LAST_WORD_IS_DOTDOT (0x2e2eul << (BITS_PER_LONG-16))
2350 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2356 while (len--) in full_name_hash()
2362 /* Return the "hash_len" (hash and length) of a null-terminated string */
2384 unsigned long hash = init_name_hash(nd->path.dentry); in hash_name()
2396 // cannot contain NUL characters - top bits being zero means in hash_name()
2399 nd->last.hash = end_name_hash(hash); in hash_name()
2400 nd->last.len = len; in hash_name()
2421 int depth = 0; // depth <= nd->depth in link_path_walk()
2424 nd->last_type = LAST_ROOT; in link_path_walk()
2425 nd->flags |= LOOKUP_PARENT; in link_path_walk()
2434 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy in link_path_walk()
2444 idmap = mnt_idmap(nd->path.mnt); in link_path_walk()
2449 nd->last.name = name; in link_path_walk()
2454 nd->last_type = LAST_DOTDOT; in link_path_walk()
2455 nd->state |= ND_JUMPED; in link_path_walk()
2459 nd->last_type = LAST_DOT; in link_path_walk()
2463 nd->last_type = LAST_NORM; in link_path_walk()
2464 nd->state &= ~ND_JUMPED; in link_path_walk()
2466 struct dentry *parent = nd->path.dentry; in link_path_walk()
2467 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { in link_path_walk()
2468 err = parent->d_op->d_hash(parent, &nd->last); in link_path_walk()
2487 nd->dir_vfsuid = i_uid_into_vfsuid(idmap, nd->inode); in link_path_walk()
2488 nd->dir_mode = nd->inode->i_mode; in link_path_walk()
2489 nd->flags &= ~LOOKUP_PARENT; in link_path_walk()
2493 name = nd->stack[--depth].name; in link_path_walk()
2503 nd->stack[depth++].name = name; in link_path_walk()
2507 if (unlikely(!d_can_lookup(nd->path.dentry))) { in link_path_walk()
2508 if (nd->flags & LOOKUP_RCU) { in link_path_walk()
2510 return -ECHILD; in link_path_walk()
2512 return -ENOTDIR; in link_path_walk()
2521 const char *s = nd->pathname; in path_init()
2525 return ERR_PTR(-EAGAIN); in path_init()
2532 nd->seq = nd->next_seq = 0; in path_init()
2534 nd->flags = flags; in path_init()
2535 nd->state |= ND_JUMPED; in path_init()
2537 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); in path_init()
2538 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); in path_init()
2541 if (nd->state & ND_ROOT_PRESET) { in path_init()
2542 struct dentry *root = nd->root.dentry; in path_init()
2543 struct inode *inode = root->d_inode; in path_init()
2545 return ERR_PTR(-ENOTDIR); in path_init()
2546 nd->path = nd->root; in path_init()
2547 nd->inode = inode; in path_init()
2549 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); in path_init()
2550 nd->root_seq = nd->seq; in path_init()
2552 path_get(&nd->path); in path_init()
2557 nd->root.mnt = NULL; in path_init()
2559 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ in path_init()
2567 /* Relative pathname -- get the starting-point it is relative to. */ in path_init()
2568 if (nd->dfd == AT_FDCWD) { in path_init()
2570 struct fs_struct *fs = current->fs; in path_init()
2574 seq = read_seqbegin(&fs->seq); in path_init()
2575 nd->path = fs->pwd; in path_init()
2576 nd->inode = nd->path.dentry->d_inode; in path_init()
2577 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); in path_init()
2578 } while (read_seqretry(&fs->seq, seq)); in path_init()
2580 get_fs_pwd(current->fs, &nd->path); in path_init()
2581 nd->inode = nd->path.dentry->d_inode; in path_init()
2585 CLASS(fd_raw, f)(nd->dfd); in path_init()
2589 return ERR_PTR(-EBADF); in path_init()
2592 if (fd_file(f)->f_cred != current_cred() && in path_init()
2593 !ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH)) in path_init()
2594 return ERR_PTR(-ENOENT); in path_init()
2597 dentry = fd_file(f)->f_path.dentry; in path_init()
2600 return ERR_PTR(-ENOTDIR); in path_init()
2602 nd->path = fd_file(f)->f_path; in path_init()
2604 nd->inode = nd->path.dentry->d_inode; in path_init()
2605 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); in path_init()
2607 path_get(&nd->path); in path_init()
2608 nd->inode = nd->path.dentry->d_inode; in path_init()
2612 /* For scoped-lookups we need to set the root to the dirfd as well. */ in path_init()
2614 nd->root = nd->path; in path_init()
2616 nd->root_seq = nd->seq; in path_init()
2618 path_get(&nd->root); in path_init()
2619 nd->state |= ND_ROOT_GRABBED; in path_init()
2627 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) in lookup_last()
2628 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; in lookup_last()
2635 if (!(nd->flags & LOOKUP_RCU)) in handle_lookup_down()
2636 dget(nd->path.dentry); in handle_lookup_down()
2637 nd->next_seq = nd->seq; in handle_lookup_down()
2638 return PTR_ERR(step_into(nd, WALK_NOFOLLOW, nd->path.dentry)); in handle_lookup_down()
2656 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { in path_lookupat()
2658 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... in path_lookupat()
2663 if (!err && nd->flags & LOOKUP_DIRECTORY) in path_lookupat()
2664 if (!d_can_lookup(nd->path.dentry)) in path_lookupat()
2665 err = -ENOTDIR; in path_lookupat()
2667 *path = nd->path; in path_lookupat()
2668 nd->path.mnt = NULL; in path_lookupat()
2669 nd->path.dentry = NULL; in path_lookupat()
2684 if (unlikely(retval == -ECHILD)) in filename_lookup()
2686 if (unlikely(retval == -ESTALE)) in filename_lookup()
2690 audit_inode(name, path->dentry, in filename_lookup()
2705 *parent = nd->path; in path_parentat()
2706 nd->path.mnt = NULL; in path_parentat()
2707 nd->path.dentry = NULL; in path_parentat()
2726 if (unlikely(retval == -ECHILD)) in __filename_parentat()
2728 if (unlikely(retval == -ESTALE)) in __filename_parentat()
2733 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); in __filename_parentat()
2758 return ERR_PTR(-EINVAL); in __kern_path_locked()
2759 inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT); in __kern_path_locked()
2762 inode_unlock(parent_path.dentry->d_inode); in __kern_path_locked()
2765 path->dentry = no_free_ptr(parent_path.dentry); in __kern_path_locked()
2766 path->mnt = no_free_ptr(parent_path.mnt); in __kern_path_locked()
2782 return ERR_PTR(-EINVAL); in kern_path_locked_negative()
2783 inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT); in kern_path_locked_negative()
2786 inode_unlock(parent_path.dentry->d_inode); in kern_path_locked_negative()
2789 path->dentry = no_free_ptr(parent_path.dentry); in kern_path_locked_negative()
2790 path->mnt = no_free_ptr(parent_path.mnt); in kern_path_locked_negative()
2825 * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair
2843 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2868 const char *name = qname->name; in lookup_noperm_common()
2869 u32 len = qname->len; in lookup_noperm_common()
2871 qname->hash = full_name_hash(base, name, len); in lookup_noperm_common()
2873 return -EACCES; in lookup_noperm_common()
2876 return -EACCES; in lookup_noperm_common()
2878 while (len--) { in lookup_noperm_common()
2881 return -EACCES; in lookup_noperm_common()
2884 * See if the low-level filesystem might want in lookup_noperm_common()
2887 if (base->d_flags & DCACHE_OP_HASH) { in lookup_noperm_common()
2888 int err = base->d_op->d_hash(base, qname); in lookup_noperm_common()
2902 return inode_permission(idmap, base->d_inode, MAY_EXEC); in lookup_one_common()
2906 * try_lookup_noperm - filesystem helper to lookup single pathname component
2917 * No locks need be held - only a counted reference to @base is needed.
2933 * lookup_noperm - filesystem helper to lookup single pathname component
2940 * The caller must hold base->i_rwsem.
2947 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); in lookup_noperm()
2959 * lookup_one - lookup single pathname component
2964 * This can be used for in-kernel filesystem clients such as file servers.
2966 * The caller must hold base->i_rwsem.
2974 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); in lookup_one()
2986 * lookup_one_unlocked - lookup single pathname component
2991 * This can be used for in-kernel filesystem clients such as file servers.
3014 * lookup_one_positive_unlocked - lookup single pathname component
3019 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
3026 * This can be used for in-kernel filesystem clients such as file servers.
3036 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { in lookup_one_positive_unlocked()
3038 ret = ERR_PTR(-ENOENT); in lookup_one_positive_unlocked()
3045 * lookup_noperm_unlocked - filesystem helper to lookup single pathname component
3075 * Like lookup_noperm_unlocked(), except that it yields ERR_PTR(-ENOENT)
3079 * need to be very careful; pinned positives have ->d_inode stable, so
3088 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { in lookup_noperm_positive_unlocked()
3090 ret = ERR_PTR(-ENOENT); in lookup_noperm_positive_unlocked()
3102 struct dentry *parent = dget_parent(path->dentry); in path_pts()
3106 if (unlikely(!path_connected(path->mnt, parent))) { in path_pts()
3108 return -ENOENT; in path_pts()
3110 dput(path->dentry); in path_pts()
3111 path->dentry = parent; in path_pts()
3114 return -ENOENT; in path_pts()
3116 path->dentry = child; in path_pts()
3150 * 1. We can't do it if dir is read-only (done in permission())
3152 * 3. We can't remove anything from append-only dir
3158 * 6. If the victim is append-only or immutable we can't do antyhing with
3161 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
3162 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
3174 return -ENOENT; in may_delete()
3177 BUG_ON(victim->d_parent->d_inode != dir); in may_delete()
3182 return -EOVERFLOW; in may_delete()
3190 return -EPERM; in may_delete()
3195 return -EPERM; in may_delete()
3198 return -ENOTDIR; in may_delete()
3200 return -EBUSY; in may_delete()
3202 return -EISDIR; in may_delete()
3204 return -ENOENT; in may_delete()
3205 if (victim->d_flags & DCACHE_NFSFS_RENAMED) in may_delete()
3206 return -EBUSY; in may_delete()
3214 * 2. We can't do it if dir is read-only (done in permission())
3223 if (child->d_inode) in may_create()
3224 return -EEXIST; in may_create()
3226 return -ENOENT; in may_create()
3227 if (!fsuidgid_has_mapping(dir->i_sb, idmap)) in may_create()
3228 return -EOVERFLOW; in may_create()
3233 // p1 != p2, both are on the same filesystem, ->s_vfs_rename_mutex is held
3238 while ((r = p->d_parent) != p2 && r != p) in lock_two_directories()
3242 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); in lock_two_directories()
3243 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT2); in lock_two_directories()
3248 while ((r = q->d_parent) != p1 && r != p && r != q) in lock_two_directories()
3252 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); in lock_two_directories()
3253 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); in lock_two_directories()
3257 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); in lock_two_directories()
3258 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); in lock_two_directories()
3261 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); in lock_two_directories()
3262 return ERR_PTR(-EXDEV); in lock_two_directories()
3272 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); in lock_rename()
3276 mutex_lock(&p1->d_sb->s_vfs_rename_mutex); in lock_rename()
3286 if (READ_ONCE(c1->d_parent) == p2) { in lock_rename_child()
3288 * hopefully won't need to touch ->s_vfs_rename_mutex at all. in lock_rename_child()
3290 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); in lock_rename_child()
3295 if (likely(c1->d_parent == p2)) in lock_rename_child()
3302 inode_unlock(p2->d_inode); in lock_rename_child()
3305 mutex_lock(&c1->d_sb->s_vfs_rename_mutex); in lock_rename_child()
3309 if (likely(c1->d_parent != p2)) in lock_rename_child()
3310 return lock_two_directories(c1->d_parent, p2); in lock_rename_child()
3314 * we need p2 locked and ->s_vfs_rename_mutex unlocked, in lock_rename_child()
3317 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); in lock_rename_child()
3318 mutex_unlock(&c1->d_sb->s_vfs_rename_mutex); in lock_rename_child()
3325 inode_unlock(p1->d_inode); in unlock_rename()
3327 inode_unlock(p2->d_inode); in unlock_rename()
3328 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); in unlock_rename()
3334 * vfs_prepare_mode - prepare the mode to be used for a new inode
3346 * after setgid stripping allows the same ordering for both non-POSIX ACL and
3352 * non-zero type.
3374 * vfs_create - create new file
3386 * On non-idmapped mounts or if permission checking is to be performed on the
3398 if (!dir->i_op->create) in vfs_create()
3399 return -EACCES; /* shouldn't it be ENOSYS? */ in vfs_create()
3405 error = dir->i_op->create(idmap, dir, dentry, mode, want_excl); in vfs_create()
3416 struct inode *dir = dentry->d_parent->d_inode; in vfs_mkobj()
3435 return !(path->mnt->mnt_flags & MNT_NODEV) && in may_open_dev()
3436 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); in may_open_dev()
3442 struct dentry *dentry = path->dentry; in may_open()
3443 struct inode *inode = dentry->d_inode; in may_open()
3447 return -ENOENT; in may_open()
3449 switch (inode->i_mode & S_IFMT) { in may_open()
3451 return -ELOOP; in may_open()
3454 return -EISDIR; in may_open()
3456 return -EACCES; in may_open()
3461 return -EACCES; in may_open()
3466 return -EACCES; in may_open()
3471 return -EACCES; in may_open()
3482 * An append-only file must be opened in append mode for writing. in may_open()
3486 return -EPERM; in may_open()
3488 return -EPERM; in may_open()
3493 return -EPERM; in may_open()
3500 const struct path *path = &filp->f_path; in handle_truncate()
3501 struct inode *inode = path->dentry->d_inode; in handle_truncate()
3508 error = do_truncate(idmap, path->dentry, 0, in handle_truncate()
3519 flag--; in open_to_namei_flags()
3531 if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap)) in may_o_create()
3532 return -EOVERFLOW; in may_o_create()
3534 error = inode_permission(idmap, dir->dentry->d_inode, in may_o_create()
3539 return security_inode_create(dir->dentry->d_inode, dentry, mode); in may_o_create()
3559 struct dentry *const DENTRY_NOT_SET = (void *) -1UL; in atomic_open()
3560 struct inode *dir = nd->path.dentry->d_inode; in atomic_open()
3563 if (nd->flags & LOOKUP_DIRECTORY) in atomic_open()
3566 file->f_path.dentry = DENTRY_NOT_SET; in atomic_open()
3567 file->f_path.mnt = nd->path.mnt; in atomic_open()
3568 error = dir->i_op->atomic_open(dir, dentry, file, in atomic_open()
3572 if (file->f_mode & FMODE_OPENED) { in atomic_open()
3573 if (unlikely(dentry != file->f_path.dentry)) { in atomic_open()
3575 dentry = dget(file->f_path.dentry); in atomic_open()
3577 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { in atomic_open()
3578 error = -EIO; in atomic_open()
3580 if (file->f_path.dentry) { in atomic_open()
3582 dentry = file->f_path.dentry; in atomic_open()
3585 error = -ENOENT; in atomic_open()
3604 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3615 struct dentry *dir = nd->path.dentry; in lookup_open()
3616 struct inode *dir_inode = dir->d_inode; in lookup_open()
3617 int open_flag = op->open_flag; in lookup_open()
3620 umode_t mode = op->mode; in lookup_open()
3624 return ERR_PTR(-ENOENT); in lookup_open()
3626 file->f_mode &= ~FMODE_CREATED; in lookup_open()
3627 dentry = d_lookup(dir, &nd->last); in lookup_open()
3630 dentry = d_alloc_parallel(dir, &nd->last, &wq); in lookup_open()
3637 error = d_revalidate(dir_inode, &nd->last, dentry, nd->flags); in lookup_open()
3646 if (dentry->d_inode) { in lookup_open()
3647 /* Cached positive dentry: will open in f_op->open */ in lookup_open()
3652 audit_inode(nd->name, dir, AUDIT_INODE_PARENT); in lookup_open()
3665 idmap = mnt_idmap(nd->path.mnt); in lookup_open()
3669 mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode); in lookup_open()
3671 create_error = may_o_create(idmap, &nd->path, in lookup_open()
3674 create_error = -EROFS; in lookup_open()
3678 if (dir_inode->i_op->atomic_open) { in lookup_open()
3680 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) in lookup_open()
3686 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, in lookup_open()
3687 nd->flags); in lookup_open()
3700 if (!dentry->d_inode && (open_flag & O_CREAT)) { in lookup_open()
3701 file->f_mode |= FMODE_CREATED; in lookup_open()
3703 if (!dir_inode->i_op->create) { in lookup_open()
3704 error = -EACCES; in lookup_open()
3708 error = dir_inode->i_op->create(idmap, dir_inode, dentry, in lookup_open()
3713 if (unlikely(create_error) && !dentry->d_inode) { in lookup_open()
3726 return (bool)nd->last.name[nd->last.len]; in trailing_slashes()
3735 return ERR_PTR(-EISDIR); in lookup_fast_for_open()
3743 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; in lookup_fast_for_open()
3751 if (!dentry->d_inode) { in lookup_fast_for_open()
3752 if (!(nd->flags & LOOKUP_RCU)) in lookup_fast_for_open()
3763 struct dentry *dir = nd->path.dentry; in open_last_lookups()
3764 int open_flag = op->open_flag; in open_last_lookups()
3769 nd->flags |= op->intent; in open_last_lookups()
3771 if (nd->last_type != LAST_NORM) { in open_last_lookups()
3772 if (nd->depth) in open_last_lookups()
3774 return handle_dots(nd, nd->last_type); in open_last_lookups()
3786 if (WARN_ON_ONCE(nd->flags & LOOKUP_RCU)) in open_last_lookups()
3787 return ERR_PTR(-ECHILD); in open_last_lookups()
3789 if (nd->flags & LOOKUP_RCU) { in open_last_lookups()
3791 return ERR_PTR(-ECHILD); in open_last_lookups()
3796 got_write = !mnt_want_write(nd->path.mnt); in open_last_lookups()
3798 * do _not_ fail yet - we might not need that or fail with in open_last_lookups()
3804 inode_lock(dir->d_inode); in open_last_lookups()
3806 inode_lock_shared(dir->d_inode); in open_last_lookups()
3809 if (file->f_mode & FMODE_CREATED) in open_last_lookups()
3810 fsnotify_create(dir->d_inode, dentry); in open_last_lookups()
3811 if (file->f_mode & FMODE_OPENED) in open_last_lookups()
3815 inode_unlock(dir->d_inode); in open_last_lookups()
3817 inode_unlock_shared(dir->d_inode); in open_last_lookups()
3820 mnt_drop_write(nd->path.mnt); in open_last_lookups()
3825 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { in open_last_lookups()
3826 dput(nd->path.dentry); in open_last_lookups()
3827 nd->path.dentry = dentry; in open_last_lookups()
3832 if (nd->depth) in open_last_lookups()
3836 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); in open_last_lookups()
3847 int open_flag = op->open_flag; in do_open()
3852 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { in do_open()
3857 if (!(file->f_mode & FMODE_CREATED)) in do_open()
3858 audit_inode(nd->name, nd->path.dentry, 0); in do_open()
3859 idmap = mnt_idmap(nd->path.mnt); in do_open()
3861 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) in do_open()
3862 return -EEXIST; in do_open()
3863 if (d_is_dir(nd->path.dentry)) in do_open()
3864 return -EISDIR; in do_open()
3866 d_backing_inode(nd->path.dentry)); in do_open()
3870 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) in do_open()
3871 return -ENOTDIR; in do_open()
3874 acc_mode = op->acc_mode; in do_open()
3875 if (file->f_mode & FMODE_CREATED) { in do_open()
3879 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { in do_open()
3880 error = mnt_want_write(nd->path.mnt); in do_open()
3885 error = may_open(idmap, &nd->path, acc_mode, open_flag); in do_open()
3886 if (!error && !(file->f_mode & FMODE_OPENED)) in do_open()
3887 error = vfs_open(&nd->path, file); in do_open()
3889 error = security_file_post_open(file, op->acc_mode); in do_open()
3894 error = -EINVAL; in do_open()
3897 mnt_drop_write(nd->path.mnt); in do_open()
3902 * vfs_tmpfile - create tmpfile
3913 * On non-idmapped mounts or if permission checking is to be performed on the
3921 struct inode *dir = d_inode(parentpath->dentry); in vfs_tmpfile()
3924 int open_flag = file->f_flags; in vfs_tmpfile()
3930 if (!dir->i_op->tmpfile) in vfs_tmpfile()
3931 return -EOPNOTSUPP; in vfs_tmpfile()
3932 child = d_alloc(parentpath->dentry, &slash_name); in vfs_tmpfile()
3934 return -ENOMEM; in vfs_tmpfile()
3935 file->f_path.mnt = parentpath->mnt; in vfs_tmpfile()
3936 file->f_path.dentry = child; in vfs_tmpfile()
3938 error = dir->i_op->tmpfile(idmap, dir, file, mode); in vfs_tmpfile()
3940 if (file->f_mode & FMODE_OPENED) in vfs_tmpfile()
3945 error = may_open(idmap, &file->f_path, 0, file->f_flags); in vfs_tmpfile()
3950 spin_lock(&inode->i_lock); in vfs_tmpfile()
3951 inode->i_state |= I_LINKABLE; in vfs_tmpfile()
3952 spin_unlock(&inode->i_lock); in vfs_tmpfile()
3959 * kernel_tmpfile_open - open a tmpfile for kernel internal use
4003 error = vfs_tmpfile(mnt_idmap(path.mnt), &path, file, op->mode); in do_tmpfile()
4006 audit_inode(nd->name, file->f_path.dentry, 0); in do_tmpfile()
4019 audit_inode(nd->name, path.dentry, 0); in do_o_path()
4032 file = alloc_empty_file(op->open_flag, current_cred()); in path_openat()
4036 if (unlikely(file->f_flags & __O_TMPFILE)) { in path_openat()
4038 } else if (unlikely(file->f_flags & O_PATH)) { in path_openat()
4050 if (likely(file->f_mode & FMODE_OPENED)) in path_openat()
4053 error = -EINVAL; in path_openat()
4056 if (error == -EOPENSTALE) { in path_openat()
4058 error = -ECHILD; in path_openat()
4060 error = -ESTALE; in path_openat()
4069 int flags = op->lookup_flags; in do_filp_open()
4074 if (unlikely(filp == ERR_PTR(-ECHILD))) in do_filp_open()
4076 if (unlikely(filp == ERR_PTR(-ESTALE))) in do_filp_open()
4088 int flags = op->lookup_flags; in do_file_open_root()
4090 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) in do_file_open_root()
4091 return ERR_PTR(-ELOOP); in do_file_open_root()
4097 set_nameidata(&nd, -1, filename, root); in do_file_open_root()
4099 if (unlikely(file == ERR_PTR(-ECHILD))) in do_file_open_root()
4101 if (unlikely(file == ERR_PTR(-ESTALE))) in do_file_open_root()
4111 struct dentry *dentry = ERR_PTR(-EEXIST); in filename_create()
4132 err2 = mnt_want_write(path->mnt); in filename_create()
4139 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); in filename_create()
4140 dentry = lookup_one_qstr_excl(&last, path->dentry, in filename_create()
4154 inode_unlock(path->dentry->d_inode); in filename_create()
4156 mnt_drop_write(path->mnt); in filename_create()
4177 inode_unlock(path->dentry->d_inode); in done_path_create()
4178 mnt_drop_write(path->mnt); in done_path_create()
4195 * vfs_mknod - create device node or file
4207 * On non-idmapped mounts or if permission checking is to be performed on the
4221 return -EPERM; in vfs_mknod()
4223 if (!dir->i_op->mknod) in vfs_mknod()
4224 return -EPERM; in vfs_mknod()
4235 error = dir->i_op->mknod(idmap, dir, dentry, mode, dev); in vfs_mknod()
4253 return -EPERM; in may_mknod()
4255 return -EINVAL; in may_mknod()
4278 mode_strip_umask(path.dentry->d_inode, mode), dev); in do_mknodat()
4285 error = vfs_create(idmap, path.dentry->d_inode, in do_mknodat()
4291 error = vfs_mknod(idmap, path.dentry->d_inode, in do_mknodat()
4295 error = vfs_mknod(idmap, path.dentry->d_inode, in do_mknodat()
4322 * vfs_mkdir - create directory returning correct dentry if possible
4333 * On non-idmapped mounts or if permission checking is to be performed on the
4346 unsigned max_links = dir->i_sb->s_max_links; in vfs_mkdir()
4353 error = -EPERM; in vfs_mkdir()
4354 if (!dir->i_op->mkdir) in vfs_mkdir()
4362 error = -EMLINK; in vfs_mkdir()
4363 if (max_links && dir->i_nlink >= max_links) in vfs_mkdir()
4366 de = dir->i_op->mkdir(idmap, dir, dentry, mode); in vfs_mkdir()
4397 mode_strip_umask(path.dentry->d_inode, mode)); in do_mkdirat()
4399 dentry = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode, in do_mkdirat()
4425 * vfs_rmdir - remove directory
4435 * On non-idmapped mounts or if permission checking is to be performed on the
4446 if (!dir->i_op->rmdir) in vfs_rmdir()
4447 return -EPERM; in vfs_rmdir()
4450 inode_lock(dentry->d_inode); in vfs_rmdir()
4452 error = -EBUSY; in vfs_rmdir()
4454 (dentry->d_inode->i_flags & S_KERNEL_FILE)) in vfs_rmdir()
4461 error = dir->i_op->rmdir(dir, dentry); in vfs_rmdir()
4466 dentry->d_inode->i_flags |= S_DEAD; in vfs_rmdir()
4471 inode_unlock(dentry->d_inode); in vfs_rmdir()
4494 error = -ENOTEMPTY; in do_rmdir()
4497 error = -EINVAL; in do_rmdir()
4500 error = -EBUSY; in do_rmdir()
4508 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); in do_rmdir()
4516 error = vfs_rmdir(mnt_idmap(path.mnt), path.dentry->d_inode, dentry); in do_rmdir()
4520 inode_unlock(path.dentry->d_inode); in do_rmdir()
4539 * vfs_unlink - unlink a filesystem object
4545 * The caller must hold dir->i_rwsem exclusively.
4547 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4551 * dir->i_rwsem before doing so.
4560 * On non-idmapped mounts or if permission checking is to be performed on the
4566 struct inode *target = dentry->d_inode; in vfs_unlink()
4572 if (!dir->i_op->unlink) in vfs_unlink()
4573 return -EPERM; in vfs_unlink()
4577 error = -EPERM; in vfs_unlink()
4579 error = -EBUSY; in vfs_unlink()
4586 error = dir->i_op->unlink(dir, dentry); in vfs_unlink()
4596 /* We don't d_delete() NFS sillyrenamed files--they still exist. */ in vfs_unlink()
4597 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { in vfs_unlink()
4629 error = -EISDIR; in do_unlinkat()
4637 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); in do_unlinkat()
4645 inode = dentry->d_inode; in do_unlinkat()
4650 error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode, in do_unlinkat()
4655 inode_unlock(path.dentry->d_inode); in do_unlinkat()
4678 error = -EISDIR; in do_unlinkat()
4680 error = -ENOTDIR; in do_unlinkat()
4687 return -EINVAL; in SYSCALL_DEFINE3()
4700 * vfs_symlink - create symlink
4711 * On non-idmapped mounts or if permission checking is to be performed on the
4723 if (!dir->i_op->symlink) in vfs_symlink()
4724 return -EPERM; in vfs_symlink()
4730 error = dir->i_op->symlink(idmap, dir, dentry, oldname); in vfs_symlink()
4754 error = security_path_symlink(&path, dentry, from->name); in do_symlinkat()
4756 error = vfs_symlink(mnt_idmap(path.mnt), path.dentry->d_inode, in do_symlinkat()
4757 dentry, from->name); in do_symlinkat()
4781 * vfs_link - create a new link
4788 * The caller must hold dir->i_rwsem exclusively.
4790 * If vfs_link discovers a delegation on the to-be-linked file in need
4791 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4803 * On non-idmapped mounts or if permission checking is to be performed on the
4810 struct inode *inode = old_dentry->d_inode; in vfs_link()
4811 unsigned max_links = dir->i_sb->s_max_links; in vfs_link()
4815 return -ENOENT; in vfs_link()
4821 if (dir->i_sb != inode->i_sb) in vfs_link()
4822 return -EXDEV; in vfs_link()
4825 * A link to an append-only or immutable file cannot be created. in vfs_link()
4828 return -EPERM; in vfs_link()
4835 return -EPERM; in vfs_link()
4836 if (!dir->i_op->link) in vfs_link()
4837 return -EPERM; in vfs_link()
4838 if (S_ISDIR(inode->i_mode)) in vfs_link()
4839 return -EPERM; in vfs_link()
4847 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) in vfs_link()
4848 error = -ENOENT; in vfs_link()
4849 else if (max_links && inode->i_nlink >= max_links) in vfs_link()
4850 error = -EMLINK; in vfs_link()
4854 error = dir->i_op->link(old_dentry, dir, new_dentry); in vfs_link()
4857 if (!error && (inode->i_state & I_LINKABLE)) { in vfs_link()
4858 spin_lock(&inode->i_lock); in vfs_link()
4859 inode->i_state &= ~I_LINKABLE; in vfs_link()
4860 spin_unlock(&inode->i_lock); in vfs_link()
4871 * security-related surprises by not following symlinks on the
4872 * newname. --KAB
4875 * with linux 2.0, and to avoid hard-linking to directories
4876 * and other special files. --ADM
4889 error = -EINVAL; in do_linkat()
4894 * that the open-time creds of the dfd matches current. in do_linkat()
4914 error = -EXDEV; in do_linkat()
4924 error = vfs_link(old_path.dentry, idmap, new_path.dentry->d_inode, in do_linkat()
4962 * vfs_rename - rename a filesystem object
4965 * The caller must hold multiple mutexes--see lock_rename()).
4968 * the source or destination, it will return -EWOULDBLOCK and return a
4978 * The worst of all namespace operations - renaming directory. "Perverted"
4983 * b) race potential - two innocent renames can create a loop together.
4985 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4987 * c) we may have to lock up to _four_ objects - parents and victim (if it exists),
4988 * and source (if it's a non-directory or a subdirectory that moves to
4990 * And that - after we got ->i_rwsem on parents (until then we don't know
4993 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4995 * (ancestors first) and rank all non-directories after them.
4997 * lock child" and rename is under ->s_vfs_rename_mutex.
4998 * HOWEVER, it relies on the assumption that any object with ->lookup()
5001 * d) conversion from fhandle to dentry may come in the wrong moment - when
5002 * we are removing the target. Solution: we will have to grab ->i_rwsem
5003 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
5004 * ->i_rwsem on parents, which works but leads to some truly excessive
5010 struct inode *old_dir = d_inode(rd->old_parent); in vfs_rename()
5011 struct inode *new_dir = d_inode(rd->new_parent); in vfs_rename()
5012 struct dentry *old_dentry = rd->old_dentry; in vfs_rename()
5013 struct dentry *new_dentry = rd->new_dentry; in vfs_rename()
5014 struct inode **delegated_inode = rd->delegated_inode; in vfs_rename()
5015 unsigned int flags = rd->flags; in vfs_rename()
5017 struct inode *source = old_dentry->d_inode; in vfs_rename()
5018 struct inode *target = new_dentry->d_inode; in vfs_rename()
5020 unsigned max_links = new_dir->i_sb->s_max_links; in vfs_rename()
5027 error = may_delete(rd->old_mnt_idmap, old_dir, old_dentry, is_dir); in vfs_rename()
5032 error = may_create(rd->new_mnt_idmap, new_dir, new_dentry); in vfs_rename()
5037 error = may_delete(rd->new_mnt_idmap, new_dir, in vfs_rename()
5040 error = may_delete(rd->new_mnt_idmap, new_dir, in vfs_rename()
5046 if (!old_dir->i_op->rename) in vfs_rename()
5047 return -EPERM; in vfs_rename()
5050 * If we are going to change the parent - check write permissions, in vfs_rename()
5055 error = inode_permission(rd->old_mnt_idmap, source, in vfs_rename()
5061 error = inode_permission(rd->new_mnt_idmap, target, in vfs_rename()
5077 * The source subdirectory needs to be locked on cross-directory in vfs_rename()
5078 * rename or cross-directory exchange since its parent changes. in vfs_rename()
5079 * The target subdirectory needs to be locked on cross-directory in vfs_rename()
5082 * Non-directories need locking in all cases (for NFS reasons); in vfs_rename()
5085 * NOTE: WE ONLY LOCK UNRELATED DIRECTORIES IN CROSS-DIRECTORY CASE. in vfs_rename()
5086 * NEVER, EVER DO THAT WITHOUT ->s_vfs_rename_mutex. in vfs_rename()
5103 error = -EPERM; in vfs_rename()
5107 error = -EBUSY; in vfs_rename()
5112 error = -EMLINK; in vfs_rename()
5113 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) in vfs_rename()
5116 old_dir->i_nlink >= max_links) in vfs_rename()
5129 error = old_dir->i_op->rename(rd->new_mnt_idmap, old_dir, old_dentry, in vfs_rename()
5137 target->i_flags |= S_DEAD; in vfs_rename()
5142 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { in vfs_rename()
5158 fsnotify_move(new_dir, old_dir, &old_dentry->d_name, in vfs_rename()
5181 int error = -EINVAL; in do_renameat2()
5206 error = -EXDEV; in do_renameat2()
5210 error = -EBUSY; in do_renameat2()
5215 error = -EEXIST; in do_renameat2()
5242 error = -ENOTDIR; in do_renameat2()
5247 /* unless the source is a directory trailing slashes give -ENOTDIR */ in do_renameat2()
5249 error = -ENOTDIR; in do_renameat2()
5256 error = -EINVAL; in do_renameat2()
5261 error = -ENOTEMPTY; in do_renameat2()
5337 copylen = -EFAULT; in readlink_copy()
5342 * vfs_readlink - copy symlink body into userspace buffer
5358 if (inode->i_opflags & IOP_CACHED_LINK) in vfs_readlink()
5359 return readlink_copy(buffer, buflen, inode->i_link, inode->i_linklen); in vfs_readlink()
5361 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { in vfs_readlink()
5362 if (unlikely(inode->i_op->readlink)) in vfs_readlink()
5363 return inode->i_op->readlink(dentry, buffer, buflen); in vfs_readlink()
5366 return -EINVAL; in vfs_readlink()
5368 spin_lock(&inode->i_lock); in vfs_readlink()
5369 inode->i_opflags |= IOP_DEFAULT_READLINK; in vfs_readlink()
5370 spin_unlock(&inode->i_lock); in vfs_readlink()
5373 link = READ_ONCE(inode->i_link); in vfs_readlink()
5375 link = inode->i_op->get_link(dentry, inode, &done); in vfs_readlink()
5386 * vfs_get_link - get symlink body
5390 * Calls security hook and i_op->get_link() on the supplied inode.
5398 const char *res = ERR_PTR(-EINVAL); in vfs_get_link()
5404 res = inode->i_op->get_link(dentry, inode, done); in vfs_get_link()
5415 struct address_space *mapping = inode->i_mapping; in __page_get_link()
5420 return ERR_PTR(-ECHILD); in __page_get_link()
5423 return ERR_PTR(-ECHILD); in __page_get_link()
5443 * page_get_link() - An implementation of the get_link inode_operation.
5451 * Return: A pointer to the NUL-terminated symlink.
5459 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); in page_get_link()
5465 * page_put_link() - Drop the reference to the symlink.
5501 struct address_space *mapping = inode->i_mapping; in page_symlink()
5502 const struct address_space_operations *aops = mapping->a_ops; in page_symlink()
5512 err = aops->write_begin(NULL, mapping, 0, len-1, &folio, &fsdata); in page_symlink()
5518 memcpy(folio_address(folio), symname, len - 1); in page_symlink()
5520 err = aops->write_end(NULL, mapping, 0, len - 1, len - 1, in page_symlink()
5524 if (err < len-1) in page_symlink()