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))
141 return ERR_PTR(-ENOMEM); in getname_flags()
147 kname = (char *)result->iname; in getname_flags()
148 result->name = kname; in getname_flags()
163 return ERR_PTR(-ENOENT); in getname_flags()
168 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a in getname_flags()
170 * names_cache allocation for the pathname, and re-do the copy from in getname_flags()
179 * result->iname[0] is within the same object and that in getname_flags()
180 * kname can't be equal to result->iname, no matter what. in getname_flags()
185 return ERR_PTR(-ENOMEM); in getname_flags()
187 result->name = kname; in getname_flags()
198 return ERR_PTR(-ENOENT); in getname_flags()
203 return ERR_PTR(-ENAMETOOLONG); in getname_flags()
207 atomic_set(&result->refcnt, 1); in getname_flags()
208 result->uptr = filename; in getname_flags()
209 result->aname = NULL; in getname_flags()
233 return ERR_PTR(-EFAULT); in __getname_maybe_null()
238 if (!IS_ERR(name) && !(name->name[0])) { in __getname_maybe_null()
252 return ERR_PTR(-ENOMEM); in getname_kernel()
255 result->name = (char *)result->iname; in getname_kernel()
263 return ERR_PTR(-ENOMEM); in getname_kernel()
265 tmp->name = (char *)result; in getname_kernel()
269 return ERR_PTR(-ENAMETOOLONG); in getname_kernel()
271 memcpy((char *)result->name, filename, len); in getname_kernel()
272 result->uptr = NULL; in getname_kernel()
273 result->aname = NULL; in getname_kernel()
274 atomic_set(&result->refcnt, 1); in getname_kernel()
286 if (WARN_ON_ONCE(!atomic_read(&name->refcnt))) in putname()
289 if (!atomic_dec_and_test(&name->refcnt)) in putname()
292 if (name->name != name->iname) { in putname()
293 __putname(name->name); in putname()
301 * check_acl - perform ACL permission checking
308 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
313 * On non-idmapped mounts or if permission checking is to be performed on the
325 return -EAGAIN; in check_acl()
326 /* no ->get_inode_acl() calls in RCU mode... */ in check_acl()
328 return -ECHILD; in check_acl()
342 return -EAGAIN; in check_acl()
358 return likely(!READ_ONCE(inode->i_acl)); in no_acl_inode()
365 * acl_permission_check - perform basic UNIX permission checking
372 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
377 * On non-idmapped mounts or if permission checking is to be performed on the
383 unsigned int mode = inode->i_mode; in acl_permission_check()
391 * - 'mask&7' is the requested permission bit set in acl_permission_check()
392 * - multiplying by 0111 spreads them out to all of ugo in acl_permission_check()
393 * - '& ~mode' looks for missing inode permission bits in acl_permission_check()
394 * - the '!' is for "no missing permissions" in acl_permission_check()
397 * ACL's on the inode - do the 'IS_POSIXACL()' check last in acl_permission_check()
398 * because it will dereference the ->i_sb pointer and we in acl_permission_check()
413 return (mask & ~mode) ? -EACCES : 0; in acl_permission_check()
419 if (error != -EAGAIN) in acl_permission_check()
438 return (mask & ~mode) ? -EACCES : 0; in acl_permission_check()
442 * generic_permission - check for access rights on a Posix-like filesystem
453 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
455 * It would then be called again in ref-walk mode.
460 * On non-idmapped mounts or if permission checking is to be performed on the
472 if (ret != -EACCES) in generic_permission()
475 if (S_ISDIR(inode->i_mode)) { in generic_permission()
484 return -EACCES; in generic_permission()
500 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) in generic_permission()
505 return -EACCES; in generic_permission()
510 * do_inode_permission - UNIX permission checking
516 * even looking at the inode->i_op values. So we keep a cache
517 * flag in inode->i_opflags, that says "this has not special
523 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { in do_inode_permission()
524 if (likely(inode->i_op->permission)) in do_inode_permission()
525 return inode->i_op->permission(idmap, inode, mask); in do_inode_permission()
528 spin_lock(&inode->i_lock); in do_inode_permission()
529 inode->i_opflags |= IOP_FASTPERM; in do_inode_permission()
530 spin_unlock(&inode->i_lock); in do_inode_permission()
536 * sb_permission - Check superblock-level permissions
541 * Separate out file-system wide checks from inode-specific permission checks.
546 umode_t mode = inode->i_mode; in sb_permission()
548 /* Nobody gets write access to a read-only fs. */ in sb_permission()
550 return -EROFS; in sb_permission()
556 * inode_permission - Check for access rights to a given inode
572 retval = sb_permission(inode->i_sb, inode, mask); in inode_permission()
581 return -EPERM; in inode_permission()
589 return -EACCES; in inode_permission()
605 * path_get - get a reference to a path
612 mntget(path->mnt); in path_get()
613 dget(path->dentry); in path_get()
618 * path_put - put a reference to a path
625 dput(path->dentry); in path_put()
626 mntput(path->mnt); in path_put()
662 struct nameidata *old = current->nameidata; in __set_nameidata()
663 p->stack = p->internal; in __set_nameidata()
664 p->depth = 0; in __set_nameidata()
665 p->dfd = dfd; in __set_nameidata()
666 p->name = name; in __set_nameidata()
667 p->pathname = likely(name) ? name->name : ""; in __set_nameidata()
668 p->path.mnt = NULL; in __set_nameidata()
669 p->path.dentry = NULL; in __set_nameidata()
670 p->total_link_count = old ? old->total_link_count : 0; in __set_nameidata()
671 p->saved = old; in __set_nameidata()
672 current->nameidata = p; in __set_nameidata()
679 p->state = 0; in set_nameidata()
681 p->state = ND_ROOT_PRESET; in set_nameidata()
682 p->root = *root; in set_nameidata()
688 struct nameidata *now = current->nameidata, *old = now->saved; in restore_nameidata()
690 current->nameidata = old; in restore_nameidata()
692 old->total_link_count = now->total_link_count; in restore_nameidata()
693 if (now->stack != now->internal) in restore_nameidata()
694 kfree(now->stack); in restore_nameidata()
702 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); in nd_alloc_stack()
705 memcpy(p, nd->internal, sizeof(nd->internal)); in nd_alloc_stack()
706 nd->stack = p; in nd_alloc_stack()
711 * path_connected - Verify that a dentry is below mnt.mnt_root
720 struct super_block *sb = mnt->mnt_sb; in path_connected()
723 if (mnt->mnt_root == sb->s_root) in path_connected()
726 return is_subdir(dentry, mnt->mnt_root); in path_connected()
731 int i = nd->depth; in drop_links()
732 while (i--) { in drop_links()
733 struct saved *last = nd->stack + i; in drop_links()
734 do_delayed_call(&last->done); in drop_links()
735 clear_delayed_call(&last->done); in drop_links()
741 nd->flags &= ~LOOKUP_RCU; in leave_rcu()
742 nd->seq = nd->next_seq = 0; in leave_rcu()
749 if (!(nd->flags & LOOKUP_RCU)) { in terminate_walk()
751 path_put(&nd->path); in terminate_walk()
752 for (i = 0; i < nd->depth; i++) in terminate_walk()
753 path_put(&nd->stack[i].link); in terminate_walk()
754 if (nd->state & ND_ROOT_GRABBED) { in terminate_walk()
755 path_put(&nd->root); in terminate_walk()
756 nd->state &= ~ND_ROOT_GRABBED; in terminate_walk()
761 nd->depth = 0; in terminate_walk()
762 nd->path.mnt = NULL; in terminate_walk()
763 nd->path.dentry = NULL; in terminate_walk()
769 int res = __legitimize_mnt(path->mnt, mseq); in __legitimize_path()
772 path->mnt = NULL; in __legitimize_path()
773 path->dentry = NULL; in __legitimize_path()
776 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { in __legitimize_path()
777 path->dentry = NULL; in __legitimize_path()
780 return !read_seqcount_retry(&path->dentry->d_seq, seq); in __legitimize_path()
786 return __legitimize_path(path, seq, nd->m_seq); in legitimize_path()
792 if (unlikely(nd->flags & LOOKUP_CACHED)) { in legitimize_links()
794 nd->depth = 0; in legitimize_links()
797 for (i = 0; i < nd->depth; i++) { in legitimize_links()
798 struct saved *last = nd->stack + i; in legitimize_links()
799 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { in legitimize_links()
801 nd->depth = i + 1; in legitimize_links()
810 /* Nothing to do if nd->root is zero or is managed by the VFS user. */ in legitimize_root()
811 if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) in legitimize_root()
813 nd->state |= ND_ROOT_GRABBED; in legitimize_root()
814 return legitimize_path(nd, &nd->root, nd->root_seq); in legitimize_root()
818 * Path walking has 2 modes, rcu-walk and ref-walk (see
819 * Documentation/filesystems/path-lookup.txt). In situations when we can't
820 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
821 * normal reference counts on dentries and vfsmounts to transition to ref-walk
822 * mode. Refcounts are grabbed at the last known good point before rcu-walk
823 * got stuck, so ref-walk may continue from there. If this is not successful
825 * to restart the path walk from the beginning in ref-walk mode.
829 * try_to_unlazy - try to switch to ref-walk mode.
833 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
834 * for ref-walk mode.
835 * Must be called from rcu-walk context.
841 struct dentry *parent = nd->path.dentry; in try_to_unlazy()
843 BUG_ON(!(nd->flags & LOOKUP_RCU)); in try_to_unlazy()
847 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) in try_to_unlazy()
852 BUG_ON(nd->inode != parent->d_inode); in try_to_unlazy()
856 nd->path.mnt = NULL; in try_to_unlazy()
857 nd->path.dentry = NULL; in try_to_unlazy()
864 * try_to_unlazy_next - try to switch to ref-walk mode.
870 * picked by rcu-walk and want to legitimize that in addition to the current
871 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
878 BUG_ON(!(nd->flags & LOOKUP_RCU)); in try_to_unlazy_next()
882 res = __legitimize_mnt(nd->path.mnt, nd->m_seq); in try_to_unlazy_next()
888 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) in try_to_unlazy_next()
892 * We need to move both the parent and the dentry from the RCU domain in try_to_unlazy_next()
898 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) in try_to_unlazy_next()
900 if (read_seqcount_retry(&dentry->d_seq, nd->next_seq)) in try_to_unlazy_next()
912 nd->path.mnt = NULL; in try_to_unlazy_next()
914 nd->path.dentry = NULL; in try_to_unlazy_next()
927 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) in d_revalidate()
928 return dentry->d_op->d_revalidate(dir, name, dentry, flags); in d_revalidate()
934 * complete_walk - successful completion of path walk
937 * If we had been in RCU mode, drop out of it and legitimize nd->path.
940 * success, -error on failure. In case of failure caller does not
941 * need to drop nd->path.
945 struct dentry *dentry = nd->path.dentry; in complete_walk()
948 if (nd->flags & LOOKUP_RCU) { in complete_walk()
950 * We don't want to zero nd->root for scoped-lookups or in complete_walk()
951 * externally-managed nd->root. in complete_walk()
953 if (!(nd->state & ND_ROOT_PRESET)) in complete_walk()
954 if (!(nd->flags & LOOKUP_IS_SCOPED)) in complete_walk()
955 nd->root.mnt = NULL; in complete_walk()
956 nd->flags &= ~LOOKUP_CACHED; in complete_walk()
958 return -ECHILD; in complete_walk()
961 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { in complete_walk()
969 * So, do a final sanity-check to make sure that in the in complete_walk()
970 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) in complete_walk()
978 if (!path_is_under(&nd->path, &nd->root)) in complete_walk()
979 return -EXDEV; in complete_walk()
982 if (likely(!(nd->state & ND_JUMPED))) in complete_walk()
985 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) in complete_walk()
988 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); in complete_walk()
993 status = -ESTALE; in complete_walk()
1000 struct fs_struct *fs = current->fs; in set_root()
1003 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we in set_root()
1007 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) in set_root()
1008 return -ENOTRECOVERABLE; in set_root()
1010 if (nd->flags & LOOKUP_RCU) { in set_root()
1014 seq = read_seqcount_begin(&fs->seq); in set_root()
1015 nd->root = fs->root; in set_root()
1016 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); in set_root()
1017 } while (read_seqcount_retry(&fs->seq, seq)); in set_root()
1019 get_fs_root(fs, &nd->root); in set_root()
1020 nd->state |= ND_ROOT_GRABBED; in set_root()
1027 if (unlikely(nd->flags & LOOKUP_BENEATH)) in nd_jump_root()
1028 return -EXDEV; in nd_jump_root()
1029 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { in nd_jump_root()
1031 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) in nd_jump_root()
1032 return -EXDEV; in nd_jump_root()
1034 if (!nd->root.mnt) { in nd_jump_root()
1039 if (nd->flags & LOOKUP_RCU) { in nd_jump_root()
1041 nd->path = nd->root; in nd_jump_root()
1042 d = nd->path.dentry; in nd_jump_root()
1043 nd->inode = d->d_inode; in nd_jump_root()
1044 nd->seq = nd->root_seq; in nd_jump_root()
1045 if (read_seqcount_retry(&d->d_seq, nd->seq)) in nd_jump_root()
1046 return -ECHILD; in nd_jump_root()
1048 path_put(&nd->path); in nd_jump_root()
1049 nd->path = nd->root; in nd_jump_root()
1050 path_get(&nd->path); in nd_jump_root()
1051 nd->inode = nd->path.dentry->d_inode; in nd_jump_root()
1053 nd->state |= ND_JUMPED; in nd_jump_root()
1058 * Helper to directly jump to a known parsed path from ->get_link,
1063 int error = -ELOOP; in nd_jump_link()
1064 struct nameidata *nd = current->nameidata; in nd_jump_link()
1066 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) in nd_jump_link()
1069 error = -EXDEV; in nd_jump_link()
1070 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { in nd_jump_link()
1071 if (nd->path.mnt != path->mnt) in nd_jump_link()
1074 /* Not currently safe for scoped-lookups. */ in nd_jump_link()
1075 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) in nd_jump_link()
1078 path_put(&nd->path); in nd_jump_link()
1079 nd->path = *path; in nd_jump_link()
1080 nd->inode = nd->path.dentry->d_inode; in nd_jump_link()
1081 nd->state |= ND_JUMPED; in nd_jump_link()
1091 struct saved *last = nd->stack + --nd->depth; in put_link()
1092 do_delayed_call(&last->done); in put_link()
1093 if (!(nd->flags & LOOKUP_RCU)) in put_link()
1094 path_put(&last->link); in put_link()
1152 * may_follow_link - Check symlink following for unsafe situations
1158 * in a sticky world-writable directory. This is to protect privileged
1162 * world-writable directory, or when the uid of the symlink and follower
1165 * Returns 0 if following the symlink is allowed, -ve on error.
1175 idmap = mnt_idmap(nd->path.mnt); in may_follow_link()
1181 /* Allowed if parent directory not sticky and world-writable. */ in may_follow_link()
1182 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) in may_follow_link()
1186 if (vfsuid_valid(nd->dir_vfsuid) && vfsuid_eq(nd->dir_vfsuid, vfsuid)) in may_follow_link()
1189 if (nd->flags & LOOKUP_RCU) in may_follow_link()
1190 return -ECHILD; in may_follow_link()
1192 audit_inode(nd->name, nd->stack[0].link.dentry, 0); in may_follow_link()
1194 return -EACCES; in may_follow_link()
1198 * safe_hardlink_source - Check for safe hardlink conditions
1203 * - inode is not a regular file
1204 * - inode is setuid
1205 * - inode is setgid and group-exec
1206 * - access failure for read and write
1213 umode_t mode = inode->i_mode; in safe_hardlink_source()
1235 * may_linkat - Check permissions for creating a hardlink
1240 * - sysctl_protected_hardlinks enabled
1241 * - fsuid does not match inode
1242 * - hardlink source is unsafe (see safe_hardlink_source() above)
1243 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
1248 * On non-idmapped mounts or if permission checking is to be performed on the
1251 * Returns 0 if successful, -ve on error.
1255 struct inode *inode = link->dentry->d_inode; in may_linkat()
1260 return -EOVERFLOW; in may_linkat()
1273 return -EPERM; in may_linkat()
1277 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1285 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1286 * - the file already exists
1287 * - we are in a sticky directory
1288 * - we don't own the file
1289 * - the owner of the directory doesn't own the file
1290 * - the directory is world writable
1298 * On non-idmapped mounts or if permission checking is to be performed on the
1301 * Returns 0 if the open is allowed, -ve on error.
1306 umode_t dir_mode = nd->dir_mode; in may_create_in_sticky()
1307 vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; in may_create_in_sticky()
1312 if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) in may_create_in_sticky()
1315 if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) in may_create_in_sticky()
1328 return -EACCES; in may_create_in_sticky()
1332 if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { in may_create_in_sticky()
1335 return -EACCES; in may_create_in_sticky()
1338 if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { in may_create_in_sticky()
1341 return -EACCES; in may_create_in_sticky()
1349 * follow_up - Find the mountpoint of path's vfsmount
1360 struct mount *mnt = real_mount(path->mnt); in follow_up()
1365 parent = mnt->mnt_parent; in follow_up()
1370 mntget(&parent->mnt); in follow_up()
1371 mountpoint = dget(mnt->mnt_mountpoint); in follow_up()
1373 dput(path->dentry); in follow_up()
1374 path->dentry = mountpoint; in follow_up()
1375 mntput(path->mnt); in follow_up()
1376 path->mnt = &parent->mnt; in follow_up()
1385 struct dentry *mountpoint = m->mnt_mountpoint; in choose_mountpoint_rcu()
1387 m = m->mnt_parent; in choose_mountpoint_rcu()
1388 if (unlikely(root->dentry == mountpoint && in choose_mountpoint_rcu()
1389 root->mnt == &m->mnt)) in choose_mountpoint_rcu()
1391 if (mountpoint != m->mnt.mnt_root) { in choose_mountpoint_rcu()
1392 path->mnt = &m->mnt; in choose_mountpoint_rcu()
1393 path->dentry = mountpoint; in choose_mountpoint_rcu()
1394 *seqp = read_seqcount_begin(&mountpoint->d_seq); in choose_mountpoint_rcu()
1428 * - return -EISDIR to tell follow_managed() to stop and return the path we
1433 struct dentry *dentry = path->dentry; in follow_automount()
1435 /* We don't want to mount if someone's just doing a stat - in follow_automount()
1448 dentry->d_inode) in follow_automount()
1449 return -EISDIR; in follow_automount()
1452 return -ELOOP; in follow_automount()
1454 return finish_automount(dentry->d_op->d_automount(path), path); in follow_automount()
1458 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1461 * sufficient for ->d_inode and ->d_flags consistency.
1466 struct vfsmount *mnt = path->mnt; in __traverse_mounts()
1474 ret = path->dentry->d_op->d_manage(path, false); in __traverse_mounts()
1475 flags = smp_load_acquire(&path->dentry->d_flags); in __traverse_mounts()
1483 dput(path->dentry); in __traverse_mounts()
1485 mntput(path->mnt); in __traverse_mounts()
1486 path->mnt = mounted; in __traverse_mounts()
1487 path->dentry = dget(mounted->mnt_root); in __traverse_mounts()
1489 flags = path->dentry->d_flags; in __traverse_mounts()
1500 flags = smp_load_acquire(&path->dentry->d_flags); in __traverse_mounts()
1505 if (ret == -EISDIR) in __traverse_mounts()
1507 // possible if you race with several mount --move in __traverse_mounts()
1508 if (need_mntput && path->mnt == mnt) in __traverse_mounts()
1509 mntput(path->mnt); in __traverse_mounts()
1511 ret = -ENOENT; in __traverse_mounts()
1519 unsigned flags = smp_load_acquire(&path->dentry->d_flags); in traverse_mounts()
1525 return -ENOENT; in traverse_mounts()
1537 dput(path->dentry); in follow_down_one()
1538 mntput(path->mnt); in follow_down_one()
1539 path->mnt = mounted; in follow_down_one()
1540 path->dentry = dget(mounted->mnt_root); in follow_down_one()
1554 struct vfsmount *mnt = path->mnt; in follow_down()
1558 if (path->mnt != mnt) in follow_down()
1570 struct dentry *dentry = path->dentry; in __follow_mount_rcu()
1571 unsigned int flags = dentry->d_flags; in __follow_mount_rcu()
1576 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in __follow_mount_rcu()
1581 * Don't forget we might have a non-mountpoint managed dentry in __follow_mount_rcu()
1585 int res = dentry->d_op->d_manage(path, true); in __follow_mount_rcu()
1587 return res == -EISDIR; in __follow_mount_rcu()
1588 flags = dentry->d_flags; in __follow_mount_rcu()
1592 struct mount *mounted = __lookup_mnt(path->mnt, dentry); in __follow_mount_rcu()
1594 path->mnt = &mounted->mnt; in __follow_mount_rcu()
1595 dentry = path->dentry = mounted->mnt.mnt_root; in __follow_mount_rcu()
1596 nd->state |= ND_JUMPED; in __follow_mount_rcu()
1597 nd->next_seq = read_seqcount_begin(&dentry->d_seq); in __follow_mount_rcu()
1598 flags = dentry->d_flags; in __follow_mount_rcu()
1599 // makes sure that non-RCU pathwalk could reach in __follow_mount_rcu()
1601 if (read_seqretry(&mount_lock, nd->m_seq)) in __follow_mount_rcu()
1605 if (read_seqretry(&mount_lock, nd->m_seq)) in __follow_mount_rcu()
1618 path->mnt = nd->path.mnt; in handle_mounts()
1619 path->dentry = dentry; in handle_mounts()
1620 if (nd->flags & LOOKUP_RCU) { in handle_mounts()
1621 unsigned int seq = nd->next_seq; in handle_mounts()
1624 // *path and nd->next_seq might've been clobbered in handle_mounts()
1625 path->mnt = nd->path.mnt; in handle_mounts()
1626 path->dentry = dentry; in handle_mounts()
1627 nd->next_seq = seq; in handle_mounts()
1629 return -ECHILD; in handle_mounts()
1631 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); in handle_mounts()
1633 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in handle_mounts()
1634 ret = -EXDEV; in handle_mounts()
1636 nd->state |= ND_JUMPED; in handle_mounts()
1639 dput(path->dentry); in handle_mounts()
1640 if (path->mnt != nd->path.mnt) in handle_mounts()
1641 mntput(path->mnt); in handle_mounts()
1656 int error = d_revalidate(dir->d_inode, name, dentry, flags); in lookup_dcache()
1669 * and only case when ->lookup() gets called on non in-lookup
1670 * dentries - as the matter of fact, this only gets called
1671 * when directory is guaranteed to have no in-lookup children
1680 struct inode *dir = base->d_inode; in lookup_one_qstr_excl()
1687 return ERR_PTR(-ENOENT); in lookup_one_qstr_excl()
1691 return ERR_PTR(-ENOMEM); in lookup_one_qstr_excl()
1693 old = dir->i_op->lookup(dir, dentry, flags); in lookup_one_qstr_excl()
1703 * lookup_fast - do fast lockless (but racy) lookup of a dentry
1718 struct dentry *dentry, *parent = nd->path.dentry; in lookup_fast()
1724 * going to fall back to non-racy lookup. in lookup_fast()
1726 if (nd->flags & LOOKUP_RCU) { in lookup_fast()
1727 dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq); in lookup_fast()
1730 return ERR_PTR(-ECHILD); in lookup_fast()
1738 if (read_seqcount_retry(&parent->d_seq, nd->seq)) in lookup_fast()
1739 return ERR_PTR(-ECHILD); in lookup_fast()
1741 status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags); in lookup_fast()
1745 return ERR_PTR(-ECHILD); in lookup_fast()
1746 if (status == -ECHILD) in lookup_fast()
1747 /* we'd been told to redo it in non-rcu mode */ in lookup_fast()
1748 status = d_revalidate(nd->inode, &nd->last, in lookup_fast()
1749 dentry, nd->flags); in lookup_fast()
1751 dentry = __d_lookup(parent, &nd->last); in lookup_fast()
1754 status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags); in lookup_fast()
1771 struct inode *inode = dir->d_inode; in __lookup_slow()
1776 return ERR_PTR(-ENOENT); in __lookup_slow()
1793 old = inode->i_op->lookup(inode, dentry, flags); in __lookup_slow()
1807 struct inode *inode = dir->d_inode; in lookup_slow()
1820 mask = nd->flags & LOOKUP_RCU ? MAY_NOT_BLOCK : 0; in may_lookup()
1821 err = inode_permission(idmap, nd->inode, mask | MAY_EXEC); in may_lookup()
1826 if (!(nd->flags & LOOKUP_RCU)) in may_lookup()
1831 return -ECHILD; // redo it all non-lazy in may_lookup()
1833 if (err != -ECHILD) // hard error in may_lookup()
1836 return inode_permission(idmap, nd->inode, MAY_EXEC); in may_lookup()
1841 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) in reserve_stack()
1842 return -ELOOP; in reserve_stack()
1844 if (likely(nd->depth != EMBEDDED_LEVELS)) in reserve_stack()
1846 if (likely(nd->stack != nd->internal)) in reserve_stack()
1851 if (nd->flags & LOOKUP_RCU) { in reserve_stack()
1853 // unlazy even if we fail to grab the link - cleanup needs it in reserve_stack()
1854 bool grabbed_link = legitimize_path(nd, link, nd->next_seq); in reserve_stack()
1857 return -ECHILD; in reserve_stack()
1862 return -ENOMEM; in reserve_stack()
1875 if (!(nd->flags & LOOKUP_RCU)) in pick_link()
1879 last = nd->stack + nd->depth++; in pick_link()
1880 last->link = *link; in pick_link()
1881 clear_delayed_call(&last->done); in pick_link()
1882 last->seq = nd->next_seq; in pick_link()
1890 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || in pick_link()
1891 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) in pick_link()
1892 return ERR_PTR(-ELOOP); in pick_link()
1894 if (!(nd->flags & LOOKUP_RCU)) { in pick_link()
1895 touch_atime(&last->link); in pick_link()
1897 } else if (atime_needs_update(&last->link, inode)) { in pick_link()
1899 return ERR_PTR(-ECHILD); in pick_link()
1900 touch_atime(&last->link); in pick_link()
1903 error = security_inode_follow_link(link->dentry, inode, in pick_link()
1904 nd->flags & LOOKUP_RCU); in pick_link()
1908 res = READ_ONCE(inode->i_link); in pick_link()
1912 get = inode->i_op->get_link; in pick_link()
1913 if (nd->flags & LOOKUP_RCU) { in pick_link()
1914 res = get(NULL, inode, &last->done); in pick_link()
1915 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) in pick_link()
1916 res = get(link->dentry, inode, &last->done); in pick_link()
1918 res = get(link->dentry, inode, &last->done); in pick_link()
1941 * to do this check without having to look at inode->i_op,
1945 * NOTE: dentry must be what nd->next_seq had been sampled from.
1956 inode = path.dentry->d_inode; in step_into()
1958 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || in step_into()
1961 if (nd->flags & LOOKUP_RCU) { in step_into()
1962 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) in step_into()
1963 return ERR_PTR(-ECHILD); in step_into()
1965 return ERR_PTR(-ENOENT); in step_into()
1967 dput(nd->path.dentry); in step_into()
1968 if (nd->path.mnt != path.mnt) in step_into()
1969 mntput(nd->path.mnt); in step_into()
1971 nd->path = path; in step_into()
1972 nd->inode = inode; in step_into()
1973 nd->seq = nd->next_seq; in step_into()
1976 if (nd->flags & LOOKUP_RCU) { in step_into()
1978 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) in step_into()
1979 return ERR_PTR(-ECHILD); in step_into()
1981 if (path.mnt == nd->path.mnt) in step_into()
1991 if (path_equal(&nd->path, &nd->root)) in follow_dotdot_rcu()
1993 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { in follow_dotdot_rcu()
1996 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), in follow_dotdot_rcu()
1997 &nd->root, &path, &seq)) in follow_dotdot_rcu()
1999 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in follow_dotdot_rcu()
2000 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2001 nd->path = path; in follow_dotdot_rcu()
2002 nd->inode = path.dentry->d_inode; in follow_dotdot_rcu()
2003 nd->seq = seq; in follow_dotdot_rcu()
2004 // makes sure that non-RCU pathwalk could reach this state in follow_dotdot_rcu()
2005 if (read_seqretry(&mount_lock, nd->m_seq)) in follow_dotdot_rcu()
2006 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2009 old = nd->path.dentry; in follow_dotdot_rcu()
2010 parent = old->d_parent; in follow_dotdot_rcu()
2011 nd->next_seq = read_seqcount_begin(&parent->d_seq); in follow_dotdot_rcu()
2012 // makes sure that non-RCU pathwalk could reach this state in follow_dotdot_rcu()
2013 if (read_seqcount_retry(&old->d_seq, nd->seq)) in follow_dotdot_rcu()
2014 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2015 if (unlikely(!path_connected(nd->path.mnt, parent))) in follow_dotdot_rcu()
2016 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2019 if (read_seqretry(&mount_lock, nd->m_seq)) in follow_dotdot_rcu()
2020 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2021 if (unlikely(nd->flags & LOOKUP_BENEATH)) in follow_dotdot_rcu()
2022 return ERR_PTR(-ECHILD); in follow_dotdot_rcu()
2023 nd->next_seq = nd->seq; in follow_dotdot_rcu()
2024 return nd->path.dentry; in follow_dotdot_rcu()
2031 if (path_equal(&nd->path, &nd->root)) in follow_dotdot()
2033 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { in follow_dotdot()
2036 if (!choose_mountpoint(real_mount(nd->path.mnt), in follow_dotdot()
2037 &nd->root, &path)) in follow_dotdot()
2039 path_put(&nd->path); in follow_dotdot()
2040 nd->path = path; in follow_dotdot()
2041 nd->inode = path.dentry->d_inode; in follow_dotdot()
2042 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) in follow_dotdot()
2043 return ERR_PTR(-EXDEV); in follow_dotdot()
2046 parent = dget_parent(nd->path.dentry); in follow_dotdot()
2047 if (unlikely(!path_connected(nd->path.mnt, parent))) { in follow_dotdot()
2049 return ERR_PTR(-ENOENT); in follow_dotdot()
2054 if (unlikely(nd->flags & LOOKUP_BENEATH)) in follow_dotdot()
2055 return ERR_PTR(-EXDEV); in follow_dotdot()
2056 return dget(nd->path.dentry); in follow_dotdot()
2065 if (!nd->root.mnt) { in handle_dots()
2070 if (nd->flags & LOOKUP_RCU) in handle_dots()
2080 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { in handle_dots()
2084 * above nd->root (and so userspace should retry or use in handle_dots()
2088 if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)) in handle_dots()
2089 return ERR_PTR(-EAGAIN); in handle_dots()
2090 if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)) in handle_dots()
2091 return ERR_PTR(-EAGAIN); in handle_dots()
2101 * "." and ".." are special - ".." especially so because it has in walk_component()
2105 if (unlikely(nd->last_type != LAST_NORM)) { in walk_component()
2106 if (!(flags & WALK_MORE) && nd->depth) in walk_component()
2108 return handle_dots(nd, nd->last_type); in walk_component()
2114 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); in walk_component()
2118 if (!(flags & WALK_MORE) && nd->depth) in walk_component()
2127 * - Architectures with fast unaligned word accesses. We could
2131 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2135 * - Furthermore, we need an efficient 64-bit compile for the
2136 * 64-bit case in order to generate the "number of bytes in
2142 #include <asm/word-at-a-time.h>
2151 * on 32-bit x86, but almost any function requires one state value and
2160 * Rotate constants are scored by considering either 64 one-bit input
2161 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2170 * Input delta: 1-bit 2-bit
2185 * Fold two longs into one 32-bit hash value. This must be fast, but
2196 #else /* 32-bit case */
2200 * Input delta: 1-bit 2-bit
2216 /* Use arch-optimized multiply if one exists */ in fold_hash()
2241 len -= sizeof(unsigned long); in full_name_hash()
2249 /* Return the "hash_len" (hash and length) of a null-terminated string */
2282 unsigned long a, b, x, y = (unsigned long)nd->path.dentry; in hash_name()
2299 nd->last.hash = fold_hash(a, y); in hash_name()
2300 nd->last.len = len; in hash_name()
2319 *lastword = 0; // Multi-word components cannot be DOT or DOTDOT in hash_name()
2321 nd->last.hash = fold_hash(x, y); in hash_name()
2322 nd->last.len = len; in hash_name()
2327 * Note that the 'last' word is always zero-masked, but
2328 * was loaded as a possibly big-endian word.
2331 #define LAST_WORD_IS_DOT (0x2eul << (BITS_PER_LONG-8))
2332 #define LAST_WORD_IS_DOTDOT (0x2e2eul << (BITS_PER_LONG-16))
2335 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2341 while (len--) in full_name_hash()
2347 /* Return the "hash_len" (hash and length) of a null-terminated string */
2369 unsigned long hash = init_name_hash(nd->path.dentry); in hash_name()
2381 // cannot contain NUL characters - top bits being zero means in hash_name()
2384 nd->last.hash = end_name_hash(hash); in hash_name()
2385 nd->last.len = len; in hash_name()
2406 int depth = 0; // depth <= nd->depth in link_path_walk()
2409 nd->last_type = LAST_ROOT; in link_path_walk()
2410 nd->flags |= LOOKUP_PARENT; in link_path_walk()
2416 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy in link_path_walk()
2426 idmap = mnt_idmap(nd->path.mnt); in link_path_walk()
2431 nd->last.name = name; in link_path_walk()
2436 nd->last_type = LAST_DOTDOT; in link_path_walk()
2437 nd->state |= ND_JUMPED; in link_path_walk()
2441 nd->last_type = LAST_DOT; in link_path_walk()
2445 nd->last_type = LAST_NORM; in link_path_walk()
2446 nd->state &= ~ND_JUMPED; in link_path_walk()
2448 struct dentry *parent = nd->path.dentry; in link_path_walk()
2449 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { in link_path_walk()
2450 err = parent->d_op->d_hash(parent, &nd->last); in link_path_walk()
2469 nd->dir_vfsuid = i_uid_into_vfsuid(idmap, nd->inode); in link_path_walk()
2470 nd->dir_mode = nd->inode->i_mode; in link_path_walk()
2471 nd->flags &= ~LOOKUP_PARENT; in link_path_walk()
2475 name = nd->stack[--depth].name; in link_path_walk()
2485 nd->stack[depth++].name = name; in link_path_walk()
2489 if (unlikely(!d_can_lookup(nd->path.dentry))) { in link_path_walk()
2490 if (nd->flags & LOOKUP_RCU) { in link_path_walk()
2492 return -ECHILD; in link_path_walk()
2494 return -ENOTDIR; in link_path_walk()
2503 const char *s = nd->pathname; in path_init()
2507 return ERR_PTR(-EAGAIN); in path_init()
2514 nd->seq = nd->next_seq = 0; in path_init()
2516 nd->flags = flags; in path_init()
2517 nd->state |= ND_JUMPED; in path_init()
2519 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); in path_init()
2520 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); in path_init()
2523 if (nd->state & ND_ROOT_PRESET) { in path_init()
2524 struct dentry *root = nd->root.dentry; in path_init()
2525 struct inode *inode = root->d_inode; in path_init()
2527 return ERR_PTR(-ENOTDIR); in path_init()
2528 nd->path = nd->root; in path_init()
2529 nd->inode = inode; in path_init()
2531 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); in path_init()
2532 nd->root_seq = nd->seq; in path_init()
2534 path_get(&nd->path); in path_init()
2539 nd->root.mnt = NULL; in path_init()
2541 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ in path_init()
2549 /* Relative pathname -- get the starting-point it is relative to. */ in path_init()
2550 if (nd->dfd == AT_FDCWD) { in path_init()
2552 struct fs_struct *fs = current->fs; in path_init()
2556 seq = read_seqcount_begin(&fs->seq); in path_init()
2557 nd->path = fs->pwd; in path_init()
2558 nd->inode = nd->path.dentry->d_inode; in path_init()
2559 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); in path_init()
2560 } while (read_seqcount_retry(&fs->seq, seq)); in path_init()
2562 get_fs_pwd(current->fs, &nd->path); in path_init()
2563 nd->inode = nd->path.dentry->d_inode; in path_init()
2567 CLASS(fd_raw, f)(nd->dfd); in path_init()
2571 return ERR_PTR(-EBADF); in path_init()
2574 if (fd_file(f)->f_cred != current_cred() && in path_init()
2575 !ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH)) in path_init()
2576 return ERR_PTR(-ENOENT); in path_init()
2579 dentry = fd_file(f)->f_path.dentry; in path_init()
2582 return ERR_PTR(-ENOTDIR); in path_init()
2584 nd->path = fd_file(f)->f_path; in path_init()
2586 nd->inode = nd->path.dentry->d_inode; in path_init()
2587 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); in path_init()
2589 path_get(&nd->path); in path_init()
2590 nd->inode = nd->path.dentry->d_inode; in path_init()
2594 /* For scoped-lookups we need to set the root to the dirfd as well. */ in path_init()
2596 nd->root = nd->path; in path_init()
2598 nd->root_seq = nd->seq; in path_init()
2600 path_get(&nd->root); in path_init()
2601 nd->state |= ND_ROOT_GRABBED; in path_init()
2609 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) in lookup_last()
2610 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; in lookup_last()
2617 if (!(nd->flags & LOOKUP_RCU)) in handle_lookup_down()
2618 dget(nd->path.dentry); in handle_lookup_down()
2619 nd->next_seq = nd->seq; in handle_lookup_down()
2620 return PTR_ERR(step_into(nd, WALK_NOFOLLOW, nd->path.dentry)); in handle_lookup_down()
2638 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { in path_lookupat()
2640 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... in path_lookupat()
2645 if (!err && nd->flags & LOOKUP_DIRECTORY) in path_lookupat()
2646 if (!d_can_lookup(nd->path.dentry)) in path_lookupat()
2647 err = -ENOTDIR; in path_lookupat()
2649 *path = nd->path; in path_lookupat()
2650 nd->path.mnt = NULL; in path_lookupat()
2651 nd->path.dentry = NULL; in path_lookupat()
2666 if (unlikely(retval == -ECHILD)) in filename_lookup()
2668 if (unlikely(retval == -ESTALE)) in filename_lookup()
2672 audit_inode(name, path->dentry, in filename_lookup()
2687 *parent = nd->path; in path_parentat()
2688 nd->path.mnt = NULL; in path_parentat()
2689 nd->path.dentry = NULL; in path_parentat()
2708 if (unlikely(retval == -ECHILD)) in __filename_parentat()
2710 if (unlikely(retval == -ESTALE)) in __filename_parentat()
2715 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); in __filename_parentat()
2740 return ERR_PTR(-EINVAL); in __kern_path_locked()
2742 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); in __kern_path_locked()
2743 d = lookup_one_qstr_excl(&last, path->dentry, 0); in __kern_path_locked()
2745 inode_unlock(path->dentry->d_inode); in __kern_path_locked()
2782 * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair
2800 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2827 this->name = name; in lookup_one_common()
2828 this->len = len; in lookup_one_common()
2829 this->hash = full_name_hash(base, name, len); in lookup_one_common()
2831 return -EACCES; in lookup_one_common()
2834 return -EACCES; in lookup_one_common()
2836 while (len--) { in lookup_one_common()
2839 return -EACCES; in lookup_one_common()
2842 * See if the low-level filesystem might want in lookup_one_common()
2845 if (base->d_flags & DCACHE_OP_HASH) { in lookup_one_common()
2846 int err = base->d_op->d_hash(base, this); in lookup_one_common()
2851 return inode_permission(idmap, base->d_inode, MAY_EXEC); in lookup_one_common()
2855 * try_lookup_one_len - filesystem helper to lookup single pathname component
2866 * The caller must hold base->i_mutex.
2873 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); in try_lookup_one_len()
2884 * lookup_one_len - filesystem helper to lookup single pathname component
2892 * The caller must hold base->i_mutex.
2900 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); in lookup_one_len()
2912 * lookup_one - filesystem helper to lookup single pathname component
2921 * The caller must hold base->i_mutex.
2930 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); in lookup_one()
2942 * lookup_one_unlocked - filesystem helper to lookup single pathname component
2974 * lookup_one_positive_unlocked - filesystem helper to lookup single
2981 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
2999 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { in lookup_one_positive_unlocked()
3001 ret = ERR_PTR(-ENOENT); in lookup_one_positive_unlocked()
3008 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
3027 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
3031 * need to be very careful; pinned positives have ->d_inode stable, so
3047 struct dentry *parent = dget_parent(path->dentry); in path_pts()
3051 if (unlikely(!path_connected(path->mnt, parent))) { in path_pts()
3053 return -ENOENT; in path_pts()
3055 dput(path->dentry); in path_pts()
3056 path->dentry = parent; in path_pts()
3059 return -ENOENT; in path_pts()
3061 path->dentry = child; in path_pts()
3095 * 1. We can't do it if dir is read-only (done in permission())
3097 * 3. We can't remove anything from append-only dir
3103 * 6. If the victim is append-only or immutable we can't do antyhing with
3106 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
3107 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
3119 return -ENOENT; in may_delete()
3122 BUG_ON(victim->d_parent->d_inode != dir); in may_delete()
3127 return -EOVERFLOW; in may_delete()
3135 return -EPERM; in may_delete()
3140 return -EPERM; in may_delete()
3143 return -ENOTDIR; in may_delete()
3145 return -EBUSY; in may_delete()
3147 return -EISDIR; in may_delete()
3149 return -ENOENT; in may_delete()
3150 if (victim->d_flags & DCACHE_NFSFS_RENAMED) in may_delete()
3151 return -EBUSY; in may_delete()
3159 * 2. We can't do it if dir is read-only (done in permission())
3168 if (child->d_inode) in may_create()
3169 return -EEXIST; in may_create()
3171 return -ENOENT; in may_create()
3172 if (!fsuidgid_has_mapping(dir->i_sb, idmap)) in may_create()
3173 return -EOVERFLOW; in may_create()
3178 // p1 != p2, both are on the same filesystem, ->s_vfs_rename_mutex is held
3183 while ((r = p->d_parent) != p2 && r != p) in lock_two_directories()
3187 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); in lock_two_directories()
3188 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT2); in lock_two_directories()
3193 while ((r = q->d_parent) != p1 && r != p && r != q) in lock_two_directories()
3197 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); in lock_two_directories()
3198 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); in lock_two_directories()
3202 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); in lock_two_directories()
3203 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); in lock_two_directories()
3206 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); in lock_two_directories()
3207 return ERR_PTR(-EXDEV); in lock_two_directories()
3217 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); in lock_rename()
3221 mutex_lock(&p1->d_sb->s_vfs_rename_mutex); in lock_rename()
3231 if (READ_ONCE(c1->d_parent) == p2) { in lock_rename_child()
3233 * hopefully won't need to touch ->s_vfs_rename_mutex at all. in lock_rename_child()
3235 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); in lock_rename_child()
3240 if (likely(c1->d_parent == p2)) in lock_rename_child()
3247 inode_unlock(p2->d_inode); in lock_rename_child()
3250 mutex_lock(&c1->d_sb->s_vfs_rename_mutex); in lock_rename_child()
3254 if (likely(c1->d_parent != p2)) in lock_rename_child()
3255 return lock_two_directories(c1->d_parent, p2); in lock_rename_child()
3259 * we need p2 locked and ->s_vfs_rename_mutex unlocked, in lock_rename_child()
3262 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); in lock_rename_child()
3263 mutex_unlock(&c1->d_sb->s_vfs_rename_mutex); in lock_rename_child()
3270 inode_unlock(p1->d_inode); in unlock_rename()
3272 inode_unlock(p2->d_inode); in unlock_rename()
3273 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); in unlock_rename()
3279 * vfs_prepare_mode - prepare the mode to be used for a new inode
3291 * after setgid stripping allows the same ordering for both non-POSIX ACL and
3297 * non-zero type.
3319 * vfs_create - create new file
3331 * On non-idmapped mounts or if permission checking is to be performed on the
3343 if (!dir->i_op->create) in vfs_create()
3344 return -EACCES; /* shouldn't it be ENOSYS? */ in vfs_create()
3350 error = dir->i_op->create(idmap, dir, dentry, mode, want_excl); in vfs_create()
3361 struct inode *dir = dentry->d_parent->d_inode; in vfs_mkobj()
3380 return !(path->mnt->mnt_flags & MNT_NODEV) && in may_open_dev()
3381 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); in may_open_dev()
3387 struct dentry *dentry = path->dentry; in may_open()
3388 struct inode *inode = dentry->d_inode; in may_open()
3392 return -ENOENT; in may_open()
3394 switch (inode->i_mode & S_IFMT) { in may_open()
3396 return -ELOOP; in may_open()
3399 return -EISDIR; in may_open()
3401 return -EACCES; in may_open()
3406 return -EACCES; in may_open()
3411 return -EACCES; in may_open()
3416 return -EACCES; in may_open()
3425 * An append-only file must be opened in append mode for writing. in may_open()
3429 return -EPERM; in may_open()
3431 return -EPERM; in may_open()
3436 return -EPERM; in may_open()
3443 const struct path *path = &filp->f_path; in handle_truncate()
3444 struct inode *inode = path->dentry->d_inode; in handle_truncate()
3451 error = do_truncate(idmap, path->dentry, 0, in handle_truncate()
3462 flag--; in open_to_namei_flags()
3474 if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap)) in may_o_create()
3475 return -EOVERFLOW; in may_o_create()
3477 error = inode_permission(idmap, dir->dentry->d_inode, in may_o_create()
3482 return security_inode_create(dir->dentry->d_inode, dentry, mode); in may_o_create()
3502 struct dentry *const DENTRY_NOT_SET = (void *) -1UL; in atomic_open()
3503 struct inode *dir = nd->path.dentry->d_inode; in atomic_open()
3506 if (nd->flags & LOOKUP_DIRECTORY) in atomic_open()
3509 file->f_path.dentry = DENTRY_NOT_SET; in atomic_open()
3510 file->f_path.mnt = nd->path.mnt; in atomic_open()
3511 error = dir->i_op->atomic_open(dir, dentry, file, in atomic_open()
3515 if (file->f_mode & FMODE_OPENED) { in atomic_open()
3516 if (unlikely(dentry != file->f_path.dentry)) { in atomic_open()
3518 dentry = dget(file->f_path.dentry); in atomic_open()
3520 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { in atomic_open()
3521 error = -EIO; in atomic_open()
3523 if (file->f_path.dentry) { in atomic_open()
3525 dentry = file->f_path.dentry; in atomic_open()
3528 error = -ENOENT; in atomic_open()
3547 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3558 struct dentry *dir = nd->path.dentry; in lookup_open()
3559 struct inode *dir_inode = dir->d_inode; in lookup_open()
3560 int open_flag = op->open_flag; in lookup_open()
3563 umode_t mode = op->mode; in lookup_open()
3567 return ERR_PTR(-ENOENT); in lookup_open()
3569 file->f_mode &= ~FMODE_CREATED; in lookup_open()
3570 dentry = d_lookup(dir, &nd->last); in lookup_open()
3573 dentry = d_alloc_parallel(dir, &nd->last, &wq); in lookup_open()
3580 error = d_revalidate(dir_inode, &nd->last, dentry, nd->flags); in lookup_open()
3589 if (dentry->d_inode) { in lookup_open()
3590 /* Cached positive dentry: will open in f_op->open */ in lookup_open()
3595 audit_inode(nd->name, dir, AUDIT_INODE_PARENT); in lookup_open()
3608 idmap = mnt_idmap(nd->path.mnt); in lookup_open()
3612 mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode); in lookup_open()
3614 create_error = may_o_create(idmap, &nd->path, in lookup_open()
3617 create_error = -EROFS; in lookup_open()
3621 if (dir_inode->i_op->atomic_open) { in lookup_open()
3623 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) in lookup_open()
3629 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, in lookup_open()
3630 nd->flags); in lookup_open()
3643 if (!dentry->d_inode && (open_flag & O_CREAT)) { in lookup_open()
3644 file->f_mode |= FMODE_CREATED; in lookup_open()
3646 if (!dir_inode->i_op->create) { in lookup_open()
3647 error = -EACCES; in lookup_open()
3651 error = dir_inode->i_op->create(idmap, dir_inode, dentry, in lookup_open()
3656 if (unlikely(create_error) && !dentry->d_inode) { in lookup_open()
3669 return (bool)nd->last.name[nd->last.len]; in trailing_slashes()
3678 return ERR_PTR(-EISDIR); in lookup_fast_for_open()
3686 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; in lookup_fast_for_open()
3694 if (!dentry->d_inode) { in lookup_fast_for_open()
3695 if (!(nd->flags & LOOKUP_RCU)) in lookup_fast_for_open()
3706 struct dentry *dir = nd->path.dentry; in open_last_lookups()
3707 int open_flag = op->open_flag; in open_last_lookups()
3712 nd->flags |= op->intent; in open_last_lookups()
3714 if (nd->last_type != LAST_NORM) { in open_last_lookups()
3715 if (nd->depth) in open_last_lookups()
3717 return handle_dots(nd, nd->last_type); in open_last_lookups()
3729 if (WARN_ON_ONCE(nd->flags & LOOKUP_RCU)) in open_last_lookups()
3730 return ERR_PTR(-ECHILD); in open_last_lookups()
3732 if (nd->flags & LOOKUP_RCU) { in open_last_lookups()
3734 return ERR_PTR(-ECHILD); in open_last_lookups()
3739 got_write = !mnt_want_write(nd->path.mnt); in open_last_lookups()
3741 * do _not_ fail yet - we might not need that or fail with in open_last_lookups()
3747 inode_lock(dir->d_inode); in open_last_lookups()
3749 inode_lock_shared(dir->d_inode); in open_last_lookups()
3752 if (file->f_mode & FMODE_CREATED) in open_last_lookups()
3753 fsnotify_create(dir->d_inode, dentry); in open_last_lookups()
3754 if (file->f_mode & FMODE_OPENED) in open_last_lookups()
3758 inode_unlock(dir->d_inode); in open_last_lookups()
3760 inode_unlock_shared(dir->d_inode); in open_last_lookups()
3763 mnt_drop_write(nd->path.mnt); in open_last_lookups()
3768 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { in open_last_lookups()
3769 dput(nd->path.dentry); in open_last_lookups()
3770 nd->path.dentry = dentry; in open_last_lookups()
3775 if (nd->depth) in open_last_lookups()
3779 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); in open_last_lookups()
3790 int open_flag = op->open_flag; in do_open()
3795 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { in do_open()
3800 if (!(file->f_mode & FMODE_CREATED)) in do_open()
3801 audit_inode(nd->name, nd->path.dentry, 0); in do_open()
3802 idmap = mnt_idmap(nd->path.mnt); in do_open()
3804 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) in do_open()
3805 return -EEXIST; in do_open()
3806 if (d_is_dir(nd->path.dentry)) in do_open()
3807 return -EISDIR; in do_open()
3809 d_backing_inode(nd->path.dentry)); in do_open()
3813 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) in do_open()
3814 return -ENOTDIR; in do_open()
3817 acc_mode = op->acc_mode; in do_open()
3818 if (file->f_mode & FMODE_CREATED) { in do_open()
3822 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { in do_open()
3823 error = mnt_want_write(nd->path.mnt); in do_open()
3828 error = may_open(idmap, &nd->path, acc_mode, open_flag); in do_open()
3829 if (!error && !(file->f_mode & FMODE_OPENED)) in do_open()
3830 error = vfs_open(&nd->path, file); in do_open()
3832 error = security_file_post_open(file, op->acc_mode); in do_open()
3837 error = -EINVAL; in do_open()
3840 mnt_drop_write(nd->path.mnt); in do_open()
3845 * vfs_tmpfile - create tmpfile
3856 * On non-idmapped mounts or if permission checking is to be performed on the
3864 struct inode *dir = d_inode(parentpath->dentry); in vfs_tmpfile()
3867 int open_flag = file->f_flags; in vfs_tmpfile()
3873 if (!dir->i_op->tmpfile) in vfs_tmpfile()
3874 return -EOPNOTSUPP; in vfs_tmpfile()
3875 child = d_alloc(parentpath->dentry, &slash_name); in vfs_tmpfile()
3877 return -ENOMEM; in vfs_tmpfile()
3878 file->f_path.mnt = parentpath->mnt; in vfs_tmpfile()
3879 file->f_path.dentry = child; in vfs_tmpfile()
3881 error = dir->i_op->tmpfile(idmap, dir, file, mode); in vfs_tmpfile()
3883 if (file->f_mode & FMODE_OPENED) in vfs_tmpfile()
3888 error = may_open(idmap, &file->f_path, 0, file->f_flags); in vfs_tmpfile()
3893 spin_lock(&inode->i_lock); in vfs_tmpfile()
3894 inode->i_state |= I_LINKABLE; in vfs_tmpfile()
3895 spin_unlock(&inode->i_lock); in vfs_tmpfile()
3902 * kernel_tmpfile_open - open a tmpfile for kernel internal use
3946 error = vfs_tmpfile(mnt_idmap(path.mnt), &path, file, op->mode); in do_tmpfile()
3949 audit_inode(nd->name, file->f_path.dentry, 0); in do_tmpfile()
3962 audit_inode(nd->name, path.dentry, 0); in do_o_path()
3975 file = alloc_empty_file(op->open_flag, current_cred()); in path_openat()
3979 if (unlikely(file->f_flags & __O_TMPFILE)) { in path_openat()
3981 } else if (unlikely(file->f_flags & O_PATH)) { in path_openat()
3993 if (likely(file->f_mode & FMODE_OPENED)) in path_openat()
3996 error = -EINVAL; in path_openat()
3999 if (error == -EOPENSTALE) { in path_openat()
4001 error = -ECHILD; in path_openat()
4003 error = -ESTALE; in path_openat()
4012 int flags = op->lookup_flags; in do_filp_open()
4017 if (unlikely(filp == ERR_PTR(-ECHILD))) in do_filp_open()
4019 if (unlikely(filp == ERR_PTR(-ESTALE))) in do_filp_open()
4031 int flags = op->lookup_flags; in do_file_open_root()
4033 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) in do_file_open_root()
4034 return ERR_PTR(-ELOOP); in do_file_open_root()
4040 set_nameidata(&nd, -1, filename, root); in do_file_open_root()
4042 if (unlikely(file == ERR_PTR(-ECHILD))) in do_file_open_root()
4044 if (unlikely(file == ERR_PTR(-ESTALE))) in do_file_open_root()
4054 struct dentry *dentry = ERR_PTR(-EEXIST); in filename_create()
4075 err2 = mnt_want_write(path->mnt); in filename_create()
4082 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); in filename_create()
4083 dentry = lookup_one_qstr_excl(&last, path->dentry, in filename_create()
4088 error = -EEXIST; in filename_create()
4093 * Special case - lookup gave negative, but... we had foo/bar/ in filename_create()
4094 * From the vfs_mknod() POV we just have a negative dentry - in filename_create()
4095 * all is fine. Let's be bastards - you had / on the end, you've in filename_create()
4096 * been asking for (non-existent) directory. -ENOENT for you. in filename_create()
4099 error = -ENOENT; in filename_create()
4111 inode_unlock(path->dentry->d_inode); in filename_create()
4113 mnt_drop_write(path->mnt); in filename_create()
4133 inode_unlock(path->dentry->d_inode); in done_path_create()
4134 mnt_drop_write(path->mnt); in done_path_create()
4151 * vfs_mknod - create device node or file
4163 * On non-idmapped mounts or if permission checking is to be performed on the
4177 return -EPERM; in vfs_mknod()
4179 if (!dir->i_op->mknod) in vfs_mknod()
4180 return -EPERM; in vfs_mknod()
4191 error = dir->i_op->mknod(idmap, dir, dentry, mode, dev); in vfs_mknod()
4209 return -EPERM; in may_mknod()
4211 return -EINVAL; in may_mknod()
4234 mode_strip_umask(path.dentry->d_inode, mode), dev); in do_mknodat()
4241 error = vfs_create(idmap, path.dentry->d_inode, in do_mknodat()
4247 error = vfs_mknod(idmap, path.dentry->d_inode, in do_mknodat()
4251 error = vfs_mknod(idmap, path.dentry->d_inode, in do_mknodat()
4278 * vfs_mkdir - create directory
4289 * On non-idmapped mounts or if permission checking is to be performed on the
4296 unsigned max_links = dir->i_sb->s_max_links; in vfs_mkdir()
4302 if (!dir->i_op->mkdir) in vfs_mkdir()
4303 return -EPERM; in vfs_mkdir()
4310 if (max_links && dir->i_nlink >= max_links) in vfs_mkdir()
4311 return -EMLINK; in vfs_mkdir()
4313 error = dir->i_op->mkdir(idmap, dir, dentry, mode); in vfs_mkdir()
4334 mode_strip_umask(path.dentry->d_inode, mode)); in do_mkdirat()
4336 error = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode, in do_mkdirat()
4360 * vfs_rmdir - remove directory
4370 * On non-idmapped mounts or if permission checking is to be performed on the
4381 if (!dir->i_op->rmdir) in vfs_rmdir()
4382 return -EPERM; in vfs_rmdir()
4385 inode_lock(dentry->d_inode); in vfs_rmdir()
4387 error = -EBUSY; in vfs_rmdir()
4389 (dentry->d_inode->i_flags & S_KERNEL_FILE)) in vfs_rmdir()
4396 error = dir->i_op->rmdir(dir, dentry); in vfs_rmdir()
4401 dentry->d_inode->i_flags |= S_DEAD; in vfs_rmdir()
4406 inode_unlock(dentry->d_inode); in vfs_rmdir()
4429 error = -ENOTEMPTY; in do_rmdir()
4432 error = -EINVAL; in do_rmdir()
4435 error = -EBUSY; in do_rmdir()
4443 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); in do_rmdir()
4448 if (!dentry->d_inode) { in do_rmdir()
4449 error = -ENOENT; in do_rmdir()
4455 error = vfs_rmdir(mnt_idmap(path.mnt), path.dentry->d_inode, dentry); in do_rmdir()
4459 inode_unlock(path.dentry->d_inode); in do_rmdir()
4478 * vfs_unlink - unlink a filesystem object
4484 * The caller must hold dir->i_mutex.
4486 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4490 * dir->i_mutex before doing so.
4499 * On non-idmapped mounts or if permission checking is to be performed on the
4505 struct inode *target = dentry->d_inode; in vfs_unlink()
4511 if (!dir->i_op->unlink) in vfs_unlink()
4512 return -EPERM; in vfs_unlink()
4516 error = -EPERM; in vfs_unlink()
4518 error = -EBUSY; in vfs_unlink()
4525 error = dir->i_op->unlink(dir, dentry); in vfs_unlink()
4535 /* We don't d_delete() NFS sillyrenamed files--they still exist. */ in vfs_unlink()
4536 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { in vfs_unlink()
4568 error = -EISDIR; in do_unlinkat()
4576 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); in do_unlinkat()
4584 inode = dentry->d_inode; in do_unlinkat()
4589 error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode, in do_unlinkat()
4594 inode_unlock(path.dentry->d_inode); in do_unlinkat()
4617 error = -ENOENT; in do_unlinkat()
4619 error = -EISDIR; in do_unlinkat()
4621 error = -ENOTDIR; in do_unlinkat()
4628 return -EINVAL; in SYSCALL_DEFINE3()
4641 * vfs_symlink - create symlink
4652 * On non-idmapped mounts or if permission checking is to be performed on the
4664 if (!dir->i_op->symlink) in vfs_symlink()
4665 return -EPERM; in vfs_symlink()
4671 error = dir->i_op->symlink(idmap, dir, dentry, oldname); in vfs_symlink()
4695 error = security_path_symlink(&path, dentry, from->name); in do_symlinkat()
4697 error = vfs_symlink(mnt_idmap(path.mnt), path.dentry->d_inode, in do_symlinkat()
4698 dentry, from->name); in do_symlinkat()
4722 * vfs_link - create a new link
4729 * The caller must hold dir->i_mutex
4731 * If vfs_link discovers a delegation on the to-be-linked file in need
4732 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4744 * On non-idmapped mounts or if permission checking is to be performed on the
4751 struct inode *inode = old_dentry->d_inode; in vfs_link()
4752 unsigned max_links = dir->i_sb->s_max_links; in vfs_link()
4756 return -ENOENT; in vfs_link()
4762 if (dir->i_sb != inode->i_sb) in vfs_link()
4763 return -EXDEV; in vfs_link()
4766 * A link to an append-only or immutable file cannot be created. in vfs_link()
4769 return -EPERM; in vfs_link()
4776 return -EPERM; in vfs_link()
4777 if (!dir->i_op->link) in vfs_link()
4778 return -EPERM; in vfs_link()
4779 if (S_ISDIR(inode->i_mode)) in vfs_link()
4780 return -EPERM; in vfs_link()
4788 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) in vfs_link()
4789 error = -ENOENT; in vfs_link()
4790 else if (max_links && inode->i_nlink >= max_links) in vfs_link()
4791 error = -EMLINK; in vfs_link()
4795 error = dir->i_op->link(old_dentry, dir, new_dentry); in vfs_link()
4798 if (!error && (inode->i_state & I_LINKABLE)) { in vfs_link()
4799 spin_lock(&inode->i_lock); in vfs_link()
4800 inode->i_state &= ~I_LINKABLE; in vfs_link()
4801 spin_unlock(&inode->i_lock); in vfs_link()
4812 * security-related surprises by not following symlinks on the
4813 * newname. --KAB
4816 * with linux 2.0, and to avoid hard-linking to directories
4817 * and other special files. --ADM
4830 error = -EINVAL; in do_linkat()
4835 * that the open-time creds of the dfd matches current. in do_linkat()
4855 error = -EXDEV; in do_linkat()
4865 error = vfs_link(old_path.dentry, idmap, new_path.dentry->d_inode, in do_linkat()
4903 * vfs_rename - rename a filesystem object
4906 * The caller must hold multiple mutexes--see lock_rename()).
4909 * the source or destination, it will return -EWOULDBLOCK and return a
4919 * The worst of all namespace operations - renaming directory. "Perverted"
4924 * b) race potential - two innocent renames can create a loop together.
4926 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4928 * c) we may have to lock up to _four_ objects - parents and victim (if it exists),
4929 * and source (if it's a non-directory or a subdirectory that moves to
4931 * And that - after we got ->i_mutex on parents (until then we don't know
4934 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4936 * (ancestors first) and rank all non-directories after them.
4938 * lock child" and rename is under ->s_vfs_rename_mutex.
4939 * HOWEVER, it relies on the assumption that any object with ->lookup()
4942 * d) conversion from fhandle to dentry may come in the wrong moment - when
4943 * we are removing the target. Solution: we will have to grab ->i_mutex
4944 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4945 * ->i_mutex on parents, which works but leads to some truly excessive
4951 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; in vfs_rename()
4952 struct dentry *old_dentry = rd->old_dentry; in vfs_rename()
4953 struct dentry *new_dentry = rd->new_dentry; in vfs_rename()
4954 struct inode **delegated_inode = rd->delegated_inode; in vfs_rename()
4955 unsigned int flags = rd->flags; in vfs_rename()
4957 struct inode *source = old_dentry->d_inode; in vfs_rename()
4958 struct inode *target = new_dentry->d_inode; in vfs_rename()
4960 unsigned max_links = new_dir->i_sb->s_max_links; in vfs_rename()
4967 error = may_delete(rd->old_mnt_idmap, old_dir, old_dentry, is_dir); in vfs_rename()
4972 error = may_create(rd->new_mnt_idmap, new_dir, new_dentry); in vfs_rename()
4977 error = may_delete(rd->new_mnt_idmap, new_dir, in vfs_rename()
4980 error = may_delete(rd->new_mnt_idmap, new_dir, in vfs_rename()
4986 if (!old_dir->i_op->rename) in vfs_rename()
4987 return -EPERM; in vfs_rename()
4990 * If we are going to change the parent - check write permissions, in vfs_rename()
4995 error = inode_permission(rd->old_mnt_idmap, source, in vfs_rename()
5001 error = inode_permission(rd->new_mnt_idmap, target, in vfs_rename()
5017 * The source subdirectory needs to be locked on cross-directory in vfs_rename()
5018 * rename or cross-directory exchange since its parent changes. in vfs_rename()
5019 * The target subdirectory needs to be locked on cross-directory in vfs_rename()
5022 * Non-directories need locking in all cases (for NFS reasons); in vfs_rename()
5025 * NOTE: WE ONLY LOCK UNRELATED DIRECTORIES IN CROSS-DIRECTORY CASE. in vfs_rename()
5026 * NEVER, EVER DO THAT WITHOUT ->s_vfs_rename_mutex. in vfs_rename()
5043 error = -EPERM; in vfs_rename()
5047 error = -EBUSY; in vfs_rename()
5052 error = -EMLINK; in vfs_rename()
5053 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) in vfs_rename()
5056 old_dir->i_nlink >= max_links) in vfs_rename()
5069 error = old_dir->i_op->rename(rd->new_mnt_idmap, old_dir, old_dentry, in vfs_rename()
5077 target->i_flags |= S_DEAD; in vfs_rename()
5082 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { in vfs_rename()
5098 fsnotify_move(new_dir, old_dir, &old_dentry->d_name, in vfs_rename()
5120 int error = -EINVAL; in do_renameat2()
5143 error = -EXDEV; in do_renameat2()
5147 error = -EBUSY; in do_renameat2()
5152 error = -EEXIST; in do_renameat2()
5173 error = -ENOENT; in do_renameat2()
5181 error = -EEXIST; in do_renameat2()
5185 error = -ENOENT; in do_renameat2()
5190 error = -ENOTDIR; in do_renameat2()
5195 /* unless the source is a directory trailing slashes give -ENOTDIR */ in do_renameat2()
5197 error = -ENOTDIR; in do_renameat2()
5204 error = -EINVAL; in do_renameat2()
5209 error = -ENOTEMPTY; in do_renameat2()
5218 rd.old_dir = old_path.dentry->d_inode; in do_renameat2()
5221 rd.new_dir = new_path.dentry->d_inode; in do_renameat2()
5285 copylen = -EFAULT; in readlink_copy()
5290 * vfs_readlink - copy symlink body into userspace buffer
5306 if (inode->i_opflags & IOP_CACHED_LINK) in vfs_readlink()
5307 return readlink_copy(buffer, buflen, inode->i_link, inode->i_linklen); in vfs_readlink()
5309 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { in vfs_readlink()
5310 if (unlikely(inode->i_op->readlink)) in vfs_readlink()
5311 return inode->i_op->readlink(dentry, buffer, buflen); in vfs_readlink()
5314 return -EINVAL; in vfs_readlink()
5316 spin_lock(&inode->i_lock); in vfs_readlink()
5317 inode->i_opflags |= IOP_DEFAULT_READLINK; in vfs_readlink()
5318 spin_unlock(&inode->i_lock); in vfs_readlink()
5321 link = READ_ONCE(inode->i_link); in vfs_readlink()
5323 link = inode->i_op->get_link(dentry, inode, &done); in vfs_readlink()
5334 * vfs_get_link - get symlink body
5338 * Calls security hook and i_op->get_link() on the supplied inode.
5346 const char *res = ERR_PTR(-EINVAL); in vfs_get_link()
5352 res = inode->i_op->get_link(dentry, inode, done); in vfs_get_link()
5363 struct address_space *mapping = inode->i_mapping; in __page_get_link()
5368 return ERR_PTR(-ECHILD); in __page_get_link()
5371 return ERR_PTR(-ECHILD); in __page_get_link()
5396 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); in page_get_link()
5425 struct address_space *mapping = inode->i_mapping; in page_symlink()
5426 const struct address_space_operations *aops = mapping->a_ops; in page_symlink()
5436 err = aops->write_begin(NULL, mapping, 0, len-1, &folio, &fsdata); in page_symlink()
5442 memcpy(folio_address(folio), symname, len - 1); in page_symlink()
5444 err = aops->write_end(NULL, mapping, 0, len - 1, len - 1, in page_symlink()
5448 if (err < len-1) in page_symlink()