2005-04-17 06:20:36 +08:00
|
|
|
#ifndef _LINUX_NAMEI_H
|
|
|
|
#define _LINUX_NAMEI_H
|
|
|
|
|
2015-05-13 21:12:02 +08:00
|
|
|
#include <linux/kernel.h>
|
2008-02-15 11:34:31 +08:00
|
|
|
#include <linux/path.h>
|
2015-05-13 21:12:02 +08:00
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/errno.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-14 15:24:29 +08:00
|
|
|
enum { MAX_NESTED_LINKS = 8 };
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-05-02 19:16:16 +08:00
|
|
|
#define MAXSYMLINKS 40
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Type of the last component on LOOKUP_PARENT
|
|
|
|
*/
|
|
|
|
enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The bitmask for a lookup event:
|
|
|
|
* - follow links at the end
|
|
|
|
* - require a directory
|
|
|
|
* - ending slashes ok even for nonexistent files
|
2009-06-16 17:17:53 +08:00
|
|
|
* - internal "there are more path components" flag
|
2005-04-17 06:20:36 +08:00
|
|
|
* - dentry cache is untrusted; force a real lookup
|
2011-01-15 02:45:31 +08:00
|
|
|
* - suppress terminal automount
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
fs: rcu-walk for path lookup
Perform common cases of path lookups without any stores or locking in the
ancestor dentry elements. This is called rcu-walk, as opposed to the current
algorithm which is a refcount based walk, or ref-walk.
This results in far fewer atomic operations on every path element,
significantly improving path lookup performance. It also avoids cacheline
bouncing on common dentries, significantly improving scalability.
The overall design is like this:
* LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk.
* Take the RCU lock for the entire path walk, starting with the acquiring
of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are
not required for dentry persistence.
* synchronize_rcu is called when unregistering a filesystem, so we can
access d_ops and i_ops during rcu-walk.
* Similarly take the vfsmount lock for the entire path walk. So now mnt
refcounts are not required for persistence. Also we are free to perform mount
lookups, and to assume dentry mount points and mount roots are stable up and
down the path.
* Have a per-dentry seqlock to protect the dentry name, parent, and inode,
so we can load this tuple atomically, and also check whether any of its
members have changed.
* Dentry lookups (based on parent, candidate string tuple) recheck the parent
sequence after the child is found in case anything changed in the parent
during the path walk.
* inode is also RCU protected so we can load d_inode and use the inode for
limited things.
* i_mode, i_uid, i_gid can be tested for exec permissions during path walk.
* i_op can be loaded.
When we reach the destination dentry, we lock it, recheck lookup sequence,
and increment its refcount and mountpoint refcount. RCU and vfsmount locks
are dropped. This is termed "dropping rcu-walk". If the dentry refcount does
not match, we can not drop rcu-walk gracefully at the current point in the
lokup, so instead return -ECHILD (for want of a better errno). This signals the
path walking code to re-do the entire lookup with a ref-walk.
Aside from the final dentry, there are other situations that may be encounted
where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take
a reference on the last good dentry) and continue with a ref-walk. Again, if
we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup
using ref-walk. But it is very important that we can continue with ref-walk
for most cases, particularly to avoid the overhead of double lookups, and to
gain the scalability advantages on common path elements (like cwd and root).
The cases where rcu-walk cannot continue are:
* NULL dentry (ie. any uncached path element)
* parent with d_inode->i_op->permission or ACLs
* dentries with d_revalidate
* Following links
In future patches, permission checks and d_revalidate become rcu-walk aware. It
may be possible eventually to make following links rcu-walk aware.
Uncached path elements will always require dropping to ref-walk mode, at the
very least because i_mutex needs to be grabbed, and objects allocated.
Signed-off-by: Nick Piggin <npiggin@kernel.dk>
2011-01-07 14:49:52 +08:00
|
|
|
#define LOOKUP_FOLLOW 0x0001
|
|
|
|
#define LOOKUP_DIRECTORY 0x0002
|
vfs pathname lookup: Add LOOKUP_AUTOMOUNT flag
Since we've now turned around and made LOOKUP_FOLLOW *not* force an
automount, we want to add the ability to force an automount event on
lookup even if we don't happen to have one of the other flags that force
it implicitly (LOOKUP_OPEN, LOOKUP_DIRECTORY, LOOKUP_PARENT..)
Most cases will never want to use this, since you'd normally want to
delay automounting as long as possible, which usually implies
LOOKUP_OPEN (when we open a file or directory, we really cannot avoid
the automount any more).
But Trond argued sufficiently forcefully that at a minimum bind mounting
a file and quotactl will want to force the automount lookup. Some other
cases (like nfs_follow_remote_path()) could use it too, although
LOOKUP_DIRECTORY would work there as well.
This commit just adds the flag and logic, no users yet, though. It also
doesn't actually touch the LOOKUP_NO_AUTOMOUNT flag that is related, and
was made irrelevant by the same change that made us not follow on
LOOKUP_FOLLOW.
Cc: Trond Myklebust <Trond.Myklebust@netapp.com>
Cc: Ian Kent <raven@themaw.net>
Cc: Jeff Layton <jlayton@redhat.com>
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: David Howells <dhowells@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Greg KH <gregkh@suse.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-09-27 08:44:55 +08:00
|
|
|
#define LOOKUP_AUTOMOUNT 0x0004
|
fs: rcu-walk for path lookup
Perform common cases of path lookups without any stores or locking in the
ancestor dentry elements. This is called rcu-walk, as opposed to the current
algorithm which is a refcount based walk, or ref-walk.
This results in far fewer atomic operations on every path element,
significantly improving path lookup performance. It also avoids cacheline
bouncing on common dentries, significantly improving scalability.
The overall design is like this:
* LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk.
* Take the RCU lock for the entire path walk, starting with the acquiring
of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are
not required for dentry persistence.
* synchronize_rcu is called when unregistering a filesystem, so we can
access d_ops and i_ops during rcu-walk.
* Similarly take the vfsmount lock for the entire path walk. So now mnt
refcounts are not required for persistence. Also we are free to perform mount
lookups, and to assume dentry mount points and mount roots are stable up and
down the path.
* Have a per-dentry seqlock to protect the dentry name, parent, and inode,
so we can load this tuple atomically, and also check whether any of its
members have changed.
* Dentry lookups (based on parent, candidate string tuple) recheck the parent
sequence after the child is found in case anything changed in the parent
during the path walk.
* inode is also RCU protected so we can load d_inode and use the inode for
limited things.
* i_mode, i_uid, i_gid can be tested for exec permissions during path walk.
* i_op can be loaded.
When we reach the destination dentry, we lock it, recheck lookup sequence,
and increment its refcount and mountpoint refcount. RCU and vfsmount locks
are dropped. This is termed "dropping rcu-walk". If the dentry refcount does
not match, we can not drop rcu-walk gracefully at the current point in the
lokup, so instead return -ECHILD (for want of a better errno). This signals the
path walking code to re-do the entire lookup with a ref-walk.
Aside from the final dentry, there are other situations that may be encounted
where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take
a reference on the last good dentry) and continue with a ref-walk. Again, if
we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup
using ref-walk. But it is very important that we can continue with ref-walk
for most cases, particularly to avoid the overhead of double lookups, and to
gain the scalability advantages on common path elements (like cwd and root).
The cases where rcu-walk cannot continue are:
* NULL dentry (ie. any uncached path element)
* parent with d_inode->i_op->permission or ACLs
* dentries with d_revalidate
* Following links
In future patches, permission checks and d_revalidate become rcu-walk aware. It
may be possible eventually to make following links rcu-walk aware.
Uncached path elements will always require dropping to ref-walk mode, at the
very least because i_mutex needs to be grabbed, and objects allocated.
Signed-off-by: Nick Piggin <npiggin@kernel.dk>
2011-01-07 14:49:52 +08:00
|
|
|
|
|
|
|
#define LOOKUP_PARENT 0x0010
|
|
|
|
#define LOOKUP_REVAL 0x0020
|
|
|
|
#define LOOKUP_RCU 0x0040
|
2016-03-07 03:20:52 +08:00
|
|
|
#define LOOKUP_NO_REVAL 0x0080
|
2011-09-27 23:12:33 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Intent data
|
|
|
|
*/
|
2008-08-05 15:00:49 +08:00
|
|
|
#define LOOKUP_OPEN 0x0100
|
|
|
|
#define LOOKUP_CREATE 0x0200
|
|
|
|
#define LOOKUP_EXCL 0x0400
|
2008-10-16 06:50:29 +08:00
|
|
|
#define LOOKUP_RENAME_TARGET 0x0800
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 04:50:10 +08:00
|
|
|
#define LOOKUP_JUMPED 0x1000
|
2011-03-10 12:04:47 +08:00
|
|
|
#define LOOKUP_ROOT 0x2000
|
2011-03-15 06:56:51 +08:00
|
|
|
#define LOOKUP_EMPTY 0x4000
|
2011-02-23 04:50:10 +08:00
|
|
|
|
devpts: Make each mount of devpts an independent filesystem.
The /dev/ptmx device node is changed to lookup the directory entry "pts"
in the same directory as the /dev/ptmx device node was opened in. If
there is a "pts" entry and that entry is a devpts filesystem /dev/ptmx
uses that filesystem. Otherwise the open of /dev/ptmx fails.
The DEVPTS_MULTIPLE_INSTANCES configuration option is removed, so that
userspace can now safely depend on each mount of devpts creating a new
instance of the filesystem.
Each mount of devpts is now a separate and equal filesystem.
Reserved ttys are now available to all instances of devpts where the
mounter is in the initial mount namespace.
A new vfs helper path_pts is introduced that finds a directory entry
named "pts" in the directory of the passed in path, and changes the
passed in path to point to it. The helper path_pts uses a function
path_parent_directory that was factored out of follow_dotdot.
In the implementation of devpts:
- devpts_mnt is killed as it is no longer meaningful if all mounts of
devpts are equal.
- pts_sb_from_inode is replaced by just inode->i_sb as all cached
inodes in the tty layer are now from the devpts filesystem.
- devpts_add_ref is rolled into the new function devpts_ptmx. And the
unnecessary inode hold is removed.
- devpts_del_ref is renamed devpts_release and reduced to just a
deacrivate_super.
- The newinstance mount option continues to be accepted but is now
ignored.
In devpts_fs.h definitions for when !CONFIG_UNIX98_PTYS are removed as
they are never used.
Documentation/filesystems/devices.txt is updated to describe the current
situation.
This has been verified to work properly on openwrt-15.05, centos5,
centos6, centos7, debian-6.0.2, debian-7.9, debian-8.2, ubuntu-14.04.3,
ubuntu-15.10, fedora23, magia-5, mint-17.3, opensuse-42.1,
slackware-14.1, gentoo-20151225 (13.0?), archlinux-2015-12-01. With the
caveat that on centos6 and on slackware-14.1 that there wind up being
two instances of the devpts filesystem mounted on /dev/pts, the lower
copy does not end up getting used.
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Greg KH <greg@kroah.com>
Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: Peter Anvin <hpa@zytor.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Serge Hallyn <serge.hallyn@ubuntu.com>
Cc: Willy Tarreau <w@1wt.eu>
Cc: Aurelien Jarno <aurelien@aurel32.net>
Cc: One Thousand Gnomes <gnomes@lxorguk.ukuu.org.uk>
Cc: Jann Horn <jann@thejh.net>
Cc: Jiri Slaby <jslaby@suse.com>
Cc: Florian Weimer <fw@deneb.enyo.de>
Cc: Konstantin Khlebnikov <koct9i@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-06-02 23:29:47 +08:00
|
|
|
extern int path_pts(struct path *path);
|
|
|
|
|
2011-11-02 16:44:39 +08:00
|
|
|
extern int user_path_at_empty(int, const char __user *, unsigned, struct path *, int *empty);
|
2008-07-22 21:59:21 +08:00
|
|
|
|
2015-05-13 21:12:02 +08:00
|
|
|
static inline int user_path_at(int dfd, const char __user *name, unsigned flags,
|
|
|
|
struct path *path)
|
|
|
|
{
|
|
|
|
return user_path_at_empty(dfd, name, flags, path, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int user_path(const char __user *name, struct path *path)
|
|
|
|
{
|
|
|
|
return user_path_at_empty(AT_FDCWD, name, LOOKUP_FOLLOW, path, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int user_lpath(const char __user *name, struct path *path)
|
|
|
|
{
|
|
|
|
return user_path_at_empty(AT_FDCWD, name, 0, path, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int user_path_dir(const char __user *name, struct path *path)
|
|
|
|
{
|
|
|
|
return user_path_at_empty(AT_FDCWD, name,
|
|
|
|
LOOKUP_FOLLOW | LOOKUP_DIRECTORY, path, NULL);
|
|
|
|
}
|
2008-07-22 21:59:21 +08:00
|
|
|
|
2008-08-02 12:49:18 +08:00
|
|
|
extern int kern_path(const char *, unsigned, struct path *);
|
|
|
|
|
2012-12-12 01:10:06 +08:00
|
|
|
extern struct dentry *kern_path_create(int, const char *, struct path *, unsigned int);
|
|
|
|
extern struct dentry *user_path_create(int, const char __user *, struct path *, unsigned int);
|
2012-07-20 05:15:31 +08:00
|
|
|
extern void done_path_create(struct path *, struct dentry *);
|
2012-06-15 07:01:42 +08:00
|
|
|
extern struct dentry *kern_path_locked(const char *, struct path *);
|
2013-09-09 08:18:44 +08:00
|
|
|
extern int kern_path_mountpoint(int, const char *, struct path *, unsigned int);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-17 14:25:38 +08:00
|
|
|
extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
|
2016-01-08 05:08:20 +08:00
|
|
|
extern struct dentry *lookup_one_len_unlocked(const char *, struct dentry *, int);
|
2016-05-11 07:16:37 +08:00
|
|
|
struct qstr;
|
|
|
|
extern struct dentry *lookup_hash(const struct qstr *, struct dentry *);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
Add a dentry op to allow processes to be held during pathwalk transit
Add a dentry op (d_manage) to permit a filesystem to hold a process and make it
sleep when it tries to transit away from one of that filesystem's directories
during a pathwalk. The operation is keyed off a new dentry flag
(DCACHE_MANAGE_TRANSIT).
The filesystem is allowed to be selective about which processes it holds and
which it permits to continue on or prohibits from transiting from each flagged
directory. This will allow autofs to hold up client processes whilst letting
its userspace daemon through to maintain the directory or the stuff behind it
or mounted upon it.
The ->d_manage() dentry operation:
int (*d_manage)(struct path *path, bool mounting_here);
takes a pointer to the directory about to be transited away from and a flag
indicating whether the transit is undertaken by do_add_mount() or
do_move_mount() skipping through a pile of filesystems mounted on a mountpoint.
It should return 0 if successful and to let the process continue on its way;
-EISDIR to prohibit the caller from skipping to overmounted filesystems or
automounting, and to use this directory; or some other error code to return to
the user.
->d_manage() is called with namespace_sem writelocked if mounting_here is true
and no other locks held, so it may sleep. However, if mounting_here is true,
it may not initiate or wait for a mount or unmount upon the parameter
directory, even if the act is actually performed by userspace.
Within fs/namei.c, follow_managed() is extended to check with d_manage() first
on each managed directory, before transiting away from it or attempting to
automount upon it.
follow_down() is renamed follow_down_one() and should only be used where the
filesystem deliberately intends to avoid management steps (e.g. autofs).
A new follow_down() is added that incorporates the loop done by all other
callers of follow_down() (do_add/move_mount(), autofs and NFSD; whilst AFS, NFS
and CIFS do use it, their use is removed by converting them to use
d_automount()). The new follow_down() calls d_manage() as appropriate. It
also takes an extra parameter to indicate if it is being called from mount code
(with namespace_sem writelocked) which it passes to d_manage(). follow_down()
ignores automount points so that it can be used to mount on them.
__follow_mount_rcu() is made to abort rcu-walk mode if it hits a directory with
DCACHE_MANAGE_TRANSIT set on the basis that we're probably going to have to
sleep. It would be possible to enter d_manage() in rcu-walk mode too, and have
that determine whether to abort or not itself. That would allow the autofs
daemon to continue on in rcu-walk mode.
Note that DCACHE_MANAGE_TRANSIT on a directory should be cleared when it isn't
required as every tranist from that directory will cause d_manage() to be
invoked. It can always be set again when necessary.
==========================
WHAT THIS MEANS FOR AUTOFS
==========================
Autofs currently uses the lookup() inode op and the d_revalidate() dentry op to
trigger the automounting of indirect mounts, and both of these can be called
with i_mutex held.
autofs knows that the i_mutex will be held by the caller in lookup(), and so
can drop it before invoking the daemon - but this isn't so for d_revalidate(),
since the lock is only held on _some_ of the code paths that call it. This
means that autofs can't risk dropping i_mutex from its d_revalidate() function
before it calls the daemon.
The bug could manifest itself as, for example, a process that's trying to
validate an automount dentry that gets made to wait because that dentry is
expired and needs cleaning up:
mkdir S ffffffff8014e05a 0 32580 24956
Call Trace:
[<ffffffff885371fd>] :autofs4:autofs4_wait+0x674/0x897
[<ffffffff80127f7d>] avc_has_perm+0x46/0x58
[<ffffffff8009fdcf>] autoremove_wake_function+0x0/0x2e
[<ffffffff88537be6>] :autofs4:autofs4_expire_wait+0x41/0x6b
[<ffffffff88535cfc>] :autofs4:autofs4_revalidate+0x91/0x149
[<ffffffff80036d96>] __lookup_hash+0xa0/0x12f
[<ffffffff80057a2f>] lookup_create+0x46/0x80
[<ffffffff800e6e31>] sys_mkdirat+0x56/0xe4
versus the automount daemon which wants to remove that dentry, but can't
because the normal process is holding the i_mutex lock:
automount D ffffffff8014e05a 0 32581 1 32561
Call Trace:
[<ffffffff80063c3f>] __mutex_lock_slowpath+0x60/0x9b
[<ffffffff8000ccf1>] do_path_lookup+0x2ca/0x2f1
[<ffffffff80063c89>] .text.lock.mutex+0xf/0x14
[<ffffffff800e6d55>] do_rmdir+0x77/0xde
[<ffffffff8005d229>] tracesys+0x71/0xe0
[<ffffffff8005d28d>] tracesys+0xd5/0xe0
which means that the system is deadlocked.
This patch allows autofs to hold up normal processes whilst the daemon goes
ahead and does things to the dentry tree behind the automouter point without
risking a deadlock as almost no locks are held in d_manage() and none in
d_automount().
Signed-off-by: David Howells <dhowells@redhat.com>
Was-Acked-by: Ian Kent <raven@themaw.net>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2011-01-15 02:45:26 +08:00
|
|
|
extern int follow_down_one(struct path *);
|
2011-03-18 21:04:20 +08:00
|
|
|
extern int follow_down(struct path *);
|
2009-04-18 15:26:48 +08:00
|
|
|
extern int follow_up(struct path *);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
extern struct dentry *lock_rename(struct dentry *, struct dentry *);
|
|
|
|
extern void unlock_rename(struct dentry *, struct dentry *);
|
|
|
|
|
2015-05-03 01:37:52 +08:00
|
|
|
extern void nd_jump_link(struct path *path);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-12-20 04:47:11 +08:00
|
|
|
static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
|
|
|
|
{
|
|
|
|
((char *) name)[min(len, maxlen)] = '\0';
|
|
|
|
}
|
|
|
|
|
2012-12-21 03:59:40 +08:00
|
|
|
/**
|
|
|
|
* retry_estale - determine whether the caller should retry an operation
|
|
|
|
* @error: the error that would currently be returned
|
|
|
|
* @flags: flags being used for next lookup attempt
|
|
|
|
*
|
|
|
|
* Check to see if the error code was -ESTALE, and then determine whether
|
|
|
|
* to retry the call based on whether "flags" already has LOOKUP_REVAL set.
|
|
|
|
*
|
|
|
|
* Returns true if the caller should try the operation again.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
retry_estale(const long error, const unsigned int flags)
|
|
|
|
{
|
|
|
|
return error == -ESTALE && !(flags & LOOKUP_REVAL);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif /* _LINUX_NAMEI_H */
|