aboutsummaryrefslogtreecommitdiffstats
path: root/fs/namei.c
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2011-01-14 13:45:26 -0500
committerAl Viro <viro@zeniv.linux.org.uk>2011-01-15 20:07:31 -0500
commitcc53ce53c86924bfe98a12ea20b7465038a08792 (patch)
tree3c9a4923dd9f413c46bfa83a20cb579446df6deb /fs/namei.c
parent9875cf806403fae66b2410a3c2cc820d97731e04 (diff)
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>
Diffstat (limited to 'fs/namei.c')
-rw-r--r--fs/namei.c72
1 files changed, 70 insertions, 2 deletions
diff --git a/fs/namei.c b/fs/namei.c
index 16109da68bbf..9d3033dc22e9 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -960,6 +960,7 @@ static int follow_automount(struct path *path, unsigned flags,
960 960
961/* 961/*
962 * Handle a dentry that is managed in some way. 962 * Handle a dentry that is managed in some way.
963 * - Flagged for transit management (autofs)
963 * - Flagged as mountpoint 964 * - Flagged as mountpoint
964 * - Flagged as automount point 965 * - Flagged as automount point
965 * 966 *
@@ -979,6 +980,16 @@ static int follow_managed(struct path *path, unsigned flags)
979 while (managed = ACCESS_ONCE(path->dentry->d_flags), 980 while (managed = ACCESS_ONCE(path->dentry->d_flags),
980 managed &= DCACHE_MANAGED_DENTRY, 981 managed &= DCACHE_MANAGED_DENTRY,
981 unlikely(managed != 0)) { 982 unlikely(managed != 0)) {
983 /* Allow the filesystem to manage the transit without i_mutex
984 * being held. */
985 if (managed & DCACHE_MANAGE_TRANSIT) {
986 BUG_ON(!path->dentry->d_op);
987 BUG_ON(!path->dentry->d_op->d_manage);
988 ret = path->dentry->d_op->d_manage(path->dentry, false);
989 if (ret < 0)
990 return ret == -EISDIR ? 0 : ret;
991 }
992
982 /* Transit to a mounted filesystem. */ 993 /* Transit to a mounted filesystem. */
983 if (managed & DCACHE_MOUNTED) { 994 if (managed & DCACHE_MOUNTED) {
984 struct vfsmount *mounted = lookup_mnt(path); 995 struct vfsmount *mounted = lookup_mnt(path);
@@ -1012,7 +1023,7 @@ static int follow_managed(struct path *path, unsigned flags)
1012 return 0; 1023 return 0;
1013} 1024}
1014 1025
1015int follow_down(struct path *path) 1026int follow_down_one(struct path *path)
1016{ 1027{
1017 struct vfsmount *mounted; 1028 struct vfsmount *mounted;
1018 1029
@@ -1029,14 +1040,19 @@ int follow_down(struct path *path)
1029 1040
1030/* 1041/*
1031 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 1042 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
1032 * meet an automount point and we're not walking to "..". True is returned to 1043 * meet a managed dentry and we're not walking to "..". True is returned to
1033 * continue, false to abort. 1044 * continue, false to abort.
1034 */ 1045 */
1035static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1046static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1036 struct inode **inode, bool reverse_transit) 1047 struct inode **inode, bool reverse_transit)
1037{ 1048{
1049 unsigned abort_mask =
1050 reverse_transit ? 0 : DCACHE_MANAGE_TRANSIT;
1051
1038 while (d_mountpoint(path->dentry)) { 1052 while (d_mountpoint(path->dentry)) {
1039 struct vfsmount *mounted; 1053 struct vfsmount *mounted;
1054 if (path->dentry->d_flags & abort_mask)
1055 return true;
1040 mounted = __lookup_mnt(path->mnt, path->dentry, 1); 1056 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1041 if (!mounted) 1057 if (!mounted)
1042 break; 1058 break;
@@ -1087,6 +1103,57 @@ static int follow_dotdot_rcu(struct nameidata *nd)
1087} 1103}
1088 1104
1089/* 1105/*
1106 * Follow down to the covering mount currently visible to userspace. At each
1107 * point, the filesystem owning that dentry may be queried as to whether the
1108 * caller is permitted to proceed or not.
1109 *
1110 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1111 * being true).
1112 */
1113int follow_down(struct path *path, bool mounting_here)
1114{
1115 unsigned managed;
1116 int ret;
1117
1118 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1119 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1120 /* Allow the filesystem to manage the transit without i_mutex
1121 * being held.
1122 *
1123 * We indicate to the filesystem if someone is trying to mount
1124 * something here. This gives autofs the chance to deny anyone
1125 * other than its daemon the right to mount on its
1126 * superstructure.
1127 *
1128 * The filesystem may sleep at this point.
1129 */
1130 if (managed & DCACHE_MANAGE_TRANSIT) {
1131 BUG_ON(!path->dentry->d_op);
1132 BUG_ON(!path->dentry->d_op->d_manage);
1133 ret = path->dentry->d_op->d_manage(path->dentry, mounting_here);
1134 if (ret < 0)
1135 return ret == -EISDIR ? 0 : ret;
1136 }
1137
1138 /* Transit to a mounted filesystem. */
1139 if (managed & DCACHE_MOUNTED) {
1140 struct vfsmount *mounted = lookup_mnt(path);
1141 if (!mounted)
1142 break;
1143 dput(path->dentry);
1144 mntput(path->mnt);
1145 path->mnt = mounted;
1146 path->dentry = dget(mounted->mnt_root);
1147 continue;
1148 }
1149
1150 /* Don't handle automount points here */
1151 break;
1152 }
1153 return 0;
1154}
1155
1156/*
1090 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 1157 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1091 */ 1158 */
1092static void follow_mount(struct path *path) 1159static void follow_mount(struct path *path)
@@ -3530,6 +3597,7 @@ const struct inode_operations page_symlink_inode_operations = {
3530}; 3597};
3531 3598
3532EXPORT_SYMBOL(user_path_at); 3599EXPORT_SYMBOL(user_path_at);
3600EXPORT_SYMBOL(follow_down_one);
3533EXPORT_SYMBOL(follow_down); 3601EXPORT_SYMBOL(follow_down);
3534EXPORT_SYMBOL(follow_up); 3602EXPORT_SYMBOL(follow_up);
3535EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 3603EXPORT_SYMBOL(get_write_access); /* binfmt_aout */