Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved |
| 3 | * Copyright 2001-2006 Ian Kent <raven@themaw.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of the Linux kernel and is made available under |
| 6 | * the terms of the GNU General Public License, version 2, or at your |
| 7 | * option, any later version, incorporated herein by reference. |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/time.h> |
| 12 | #include <linux/signal.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 13 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/file.h> |
| 15 | #include "autofs_i.h" |
| 16 | |
| 17 | /* We make this a static variable rather than a part of the superblock; it |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 18 | * is better if we don't reassign numbers easily even across filesystems |
| 19 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static autofs_wqt_t autofs4_next_wait_queue = 1; |
| 21 | |
| 22 | /* These are the signals we allow interrupting a pending mount */ |
| 23 | #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT)) |
| 24 | |
| 25 | void autofs4_catatonic_mode(struct autofs_sb_info *sbi) |
| 26 | { |
| 27 | struct autofs_wait_queue *wq, *nwq; |
| 28 | |
Ian Kent | 5a11d4d | 2008-07-23 21:30:17 -0700 | [diff] [blame] | 29 | mutex_lock(&sbi->wq_mutex); |
| 30 | if (sbi->catatonic) { |
| 31 | mutex_unlock(&sbi->wq_mutex); |
| 32 | return; |
| 33 | } |
| 34 | |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 35 | pr_debug("entering catatonic mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | sbi->catatonic = 1; |
| 38 | wq = sbi->queues; |
| 39 | sbi->queues = NULL; /* Erase all wait queues */ |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 40 | while (wq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | nwq = wq->next; |
| 42 | wq->status = -ENOENT; /* Magic is gone - report failure */ |
Tim Gardner | 5140a8c | 2013-03-01 19:46:48 +0800 | [diff] [blame] | 43 | kfree(wq->name.name); |
| 44 | wq->name.name = NULL; |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 45 | wq->wait_ctr--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | wake_up_interruptible(&wq->queue); |
| 47 | wq = nwq; |
| 48 | } |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 49 | fput(sbi->pipe); /* Close the pipe */ |
| 50 | sbi->pipe = NULL; |
Ian Kent | 5a11d4d | 2008-07-23 21:30:17 -0700 | [diff] [blame] | 51 | sbi->pipefd = -1; |
| 52 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Al Viro | d668dc5 | 2012-01-10 22:35:38 -0500 | [diff] [blame] | 55 | static int autofs4_write(struct autofs_sb_info *sbi, |
| 56 | struct file *file, const void *addr, int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | unsigned long sigpipe, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | const char *data = (const char *)addr; |
| 60 | ssize_t wr = 0; |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | sigpipe = sigismember(¤t->pending.signal, SIGPIPE); |
| 63 | |
Al Viro | d668dc5 | 2012-01-10 22:35:38 -0500 | [diff] [blame] | 64 | mutex_lock(&sbi->pipe_mutex); |
Andrey Vagin | 5a9294e5 | 2016-06-24 14:50:27 -0700 | [diff] [blame] | 65 | while (bytes) { |
Christoph Hellwig | 317d5a5 | 2017-09-01 17:39:10 +0200 | [diff] [blame] | 66 | wr = __kernel_write(file, data, bytes, &file->f_pos); |
Andrey Vagin | 5a9294e5 | 2016-06-24 14:50:27 -0700 | [diff] [blame] | 67 | if (wr <= 0) |
| 68 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | data += wr; |
| 70 | bytes -= wr; |
| 71 | } |
Ian Kent | 8638094 | 2012-01-13 20:41:46 +0800 | [diff] [blame] | 72 | mutex_unlock(&sbi->pipe_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* Keep the currently executing process from receiving a |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 75 | * SIGPIPE unless it was already supposed to get one |
| 76 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (wr == -EPIPE && !sigpipe) { |
| 78 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 79 | sigdelset(¤t->pending.signal, SIGPIPE); |
| 80 | recalc_sigpending(); |
| 81 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 82 | } |
| 83 | |
NeilBrown | ecc0c46 | 2017-11-17 15:29:13 -0800 | [diff] [blame^] | 84 | /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */ |
| 85 | return bytes == 0 ? 0 : wr < 0 ? wr : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
Ian Kent | 0266725 | 2016-03-15 14:58:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | static void autofs4_notify_daemon(struct autofs_sb_info *sbi, |
| 89 | struct autofs_wait_queue *wq, |
| 90 | int type) |
| 91 | { |
Ian Kent | e851447 | 2007-02-20 13:58:09 -0800 | [diff] [blame] | 92 | union { |
| 93 | struct autofs_packet_hdr hdr; |
| 94 | union autofs_packet_union v4_pkt; |
| 95 | union autofs_v5_packet_union v5_pkt; |
| 96 | } pkt; |
Ian Kent | e64be33 | 2008-07-23 21:30:20 -0700 | [diff] [blame] | 97 | struct file *pipe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | size_t pktsz; |
NeilBrown | ecc0c46 | 2017-11-17 15:29:13 -0800 | [diff] [blame^] | 99 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 101 | pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n", |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 102 | (unsigned long) wq->wait_queue_token, |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 103 | wq->name.len, wq->name.name, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 105 | memset(&pkt, 0, sizeof(pkt)); /* For security reasons */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | pkt.hdr.proto_version = sbi->version; |
| 108 | pkt.hdr.type = type; |
Al Viro | 8753333 | 2012-01-10 22:24:48 -0500 | [diff] [blame] | 109 | |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 110 | switch (type) { |
| 111 | /* Kernel protocol v4 missing and expire packets */ |
| 112 | case autofs_ptype_missing: |
| 113 | { |
Ian Kent | e851447 | 2007-02-20 13:58:09 -0800 | [diff] [blame] | 114 | struct autofs_packet_missing *mp = &pkt.v4_pkt.missing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | pktsz = sizeof(*mp); |
| 117 | |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 118 | mp->wait_queue_token = wq->wait_queue_token; |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 119 | mp->len = wq->name.len; |
| 120 | memcpy(mp->name, wq->name.name, wq->name.len); |
| 121 | mp->name[wq->name.len] = '\0'; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | case autofs_ptype_expire_multi: |
| 125 | { |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 126 | struct autofs_packet_expire_multi *ep = |
| 127 | &pkt.v4_pkt.expire_multi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | pktsz = sizeof(*ep); |
| 130 | |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 131 | ep->wait_queue_token = wq->wait_queue_token; |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 132 | ep->len = wq->name.len; |
| 133 | memcpy(ep->name, wq->name.name, wq->name.len); |
| 134 | ep->name[wq->name.len] = '\0'; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | /* |
| 138 | * Kernel protocol v5 packet for handling indirect and direct |
| 139 | * mount missing and expire requests |
| 140 | */ |
| 141 | case autofs_ptype_missing_indirect: |
| 142 | case autofs_ptype_expire_indirect: |
| 143 | case autofs_ptype_missing_direct: |
| 144 | case autofs_ptype_expire_direct: |
| 145 | { |
Ian Kent | e851447 | 2007-02-20 13:58:09 -0800 | [diff] [blame] | 146 | struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet; |
Eric W. Biederman | 45634cd | 2012-02-07 16:18:52 -0800 | [diff] [blame] | 147 | struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 148 | |
Linus Torvalds | fcbf94b | 2012-04-28 08:29:56 -0700 | [diff] [blame] | 149 | pktsz = sizeof(*packet); |
| 150 | |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 151 | packet->wait_queue_token = wq->wait_queue_token; |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 152 | packet->len = wq->name.len; |
| 153 | memcpy(packet->name, wq->name.name, wq->name.len); |
| 154 | packet->name[wq->name.len] = '\0'; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 155 | packet->dev = wq->dev; |
| 156 | packet->ino = wq->ino; |
Eric W. Biederman | 45634cd | 2012-02-07 16:18:52 -0800 | [diff] [blame] | 157 | packet->uid = from_kuid_munged(user_ns, wq->uid); |
| 158 | packet->gid = from_kgid_munged(user_ns, wq->gid); |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 159 | packet->pid = wq->pid; |
| 160 | packet->tgid = wq->tgid; |
| 161 | break; |
| 162 | } |
| 163 | default: |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 164 | pr_warn("bad type %d!\n", type); |
Al Viro | 8753333 | 2012-01-10 22:24:48 -0500 | [diff] [blame] | 165 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 169 | pipe = get_file(sbi->pipe); |
Al Viro | 8753333 | 2012-01-10 22:24:48 -0500 | [diff] [blame] | 170 | |
Ian Kent | e64be33 | 2008-07-23 21:30:20 -0700 | [diff] [blame] | 171 | mutex_unlock(&sbi->wq_mutex); |
| 172 | |
Al Viro | d668dc5 | 2012-01-10 22:35:38 -0500 | [diff] [blame] | 173 | if (autofs4_write(sbi, pipe, &pkt, pktsz)) |
NeilBrown | ecc0c46 | 2017-11-17 15:29:13 -0800 | [diff] [blame^] | 174 | switch (ret = autofs4_write(sbi, pipe, &pkt, pktsz)) { |
| 175 | case 0: |
| 176 | break; |
| 177 | case -ENOMEM: |
| 178 | case -ERESTARTSYS: |
| 179 | /* Just fail this one */ |
| 180 | autofs4_wait_release(sbi, wq->wait_queue_token, ret); |
| 181 | break; |
| 182 | default: |
Al Viro | 8753333 | 2012-01-10 22:24:48 -0500 | [diff] [blame] | 183 | autofs4_catatonic_mode(sbi); |
NeilBrown | ecc0c46 | 2017-11-17 15:29:13 -0800 | [diff] [blame^] | 184 | break; |
| 185 | } |
Al Viro | 8753333 | 2012-01-10 22:24:48 -0500 | [diff] [blame] | 186 | fput(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static int autofs4_getpath(struct autofs_sb_info *sbi, |
| 190 | struct dentry *dentry, char **name) |
| 191 | { |
| 192 | struct dentry *root = sbi->sb->s_root; |
| 193 | struct dentry *tmp; |
Nick Piggin | 949854d | 2011-01-07 17:49:37 +1100 | [diff] [blame] | 194 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | char *p; |
Nick Piggin | 949854d | 2011-01-07 17:49:37 +1100 | [diff] [blame] | 196 | int len; |
| 197 | unsigned seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Nick Piggin | 949854d | 2011-01-07 17:49:37 +1100 | [diff] [blame] | 199 | rename_retry: |
| 200 | buf = *name; |
| 201 | len = 0; |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 202 | |
Nick Piggin | 949854d | 2011-01-07 17:49:37 +1100 | [diff] [blame] | 203 | seq = read_seqbegin(&rename_lock); |
| 204 | rcu_read_lock(); |
Ian Kent | e785472 | 2011-03-25 01:51:31 +0800 | [diff] [blame] | 205 | spin_lock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent) |
| 207 | len += tmp->d_name.len + 1; |
| 208 | |
Ian Kent | cab0936 | 2008-05-01 04:35:07 -0700 | [diff] [blame] | 209 | if (!len || --len > NAME_MAX) { |
Ian Kent | e785472 | 2011-03-25 01:51:31 +0800 | [diff] [blame] | 210 | spin_unlock(&sbi->fs_lock); |
Nick Piggin | 949854d | 2011-01-07 17:49:37 +1100 | [diff] [blame] | 211 | rcu_read_unlock(); |
| 212 | if (read_seqretry(&rename_lock, seq)) |
| 213 | goto rename_retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | *(buf + len) = '\0'; |
| 218 | p = buf + len - dentry->d_name.len; |
| 219 | strncpy(p, dentry->d_name.name, dentry->d_name.len); |
| 220 | |
| 221 | for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) { |
| 222 | *(--p) = '/'; |
| 223 | p -= tmp->d_name.len; |
| 224 | strncpy(p, tmp->d_name.name, tmp->d_name.len); |
| 225 | } |
Ian Kent | e785472 | 2011-03-25 01:51:31 +0800 | [diff] [blame] | 226 | spin_unlock(&sbi->fs_lock); |
Nick Piggin | 949854d | 2011-01-07 17:49:37 +1100 | [diff] [blame] | 227 | rcu_read_unlock(); |
| 228 | if (read_seqretry(&rename_lock, seq)) |
| 229 | goto rename_retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | return len; |
| 232 | } |
| 233 | |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 234 | static struct autofs_wait_queue * |
Al Viro | 8ac790f | 2016-07-20 16:25:07 -0400 | [diff] [blame] | 235 | autofs4_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr) |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 236 | { |
| 237 | struct autofs_wait_queue *wq; |
| 238 | |
| 239 | for (wq = sbi->queues; wq; wq = wq->next) { |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 240 | if (wq->name.hash == qstr->hash && |
| 241 | wq->name.len == qstr->len && |
| 242 | wq->name.name && |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 243 | !memcmp(wq->name.name, qstr->name, qstr->len)) |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 244 | break; |
| 245 | } |
| 246 | return wq; |
| 247 | } |
| 248 | |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 249 | /* |
| 250 | * Check if we have a valid request. |
| 251 | * Returns |
| 252 | * 1 if the request should continue. |
| 253 | * In this case we can return an autofs_wait_queue entry if one is |
| 254 | * found or NULL to idicate a new wait needs to be created. |
| 255 | * 0 or a negative errno if the request shouldn't continue. |
| 256 | */ |
| 257 | static int validate_request(struct autofs_wait_queue **wait, |
| 258 | struct autofs_sb_info *sbi, |
Al Viro | 8ac790f | 2016-07-20 16:25:07 -0400 | [diff] [blame] | 259 | const struct qstr *qstr, |
Ian Kent | 6035974 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 260 | const struct path *path, enum autofs_notify notify) |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 261 | { |
Ian Kent | 6035974 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 262 | struct dentry *dentry = path->dentry; |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 263 | struct autofs_wait_queue *wq; |
| 264 | struct autofs_info *ino; |
| 265 | |
Al Viro | 4041bcd | 2012-01-10 22:20:12 -0500 | [diff] [blame] | 266 | if (sbi->catatonic) |
| 267 | return -ENOENT; |
| 268 | |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 269 | /* Wait in progress, continue; */ |
| 270 | wq = autofs4_find_wait(sbi, qstr); |
| 271 | if (wq) { |
| 272 | *wait = wq; |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | *wait = NULL; |
| 277 | |
| 278 | /* If we don't yet have any info this is a new request */ |
| 279 | ino = autofs4_dentry_ino(dentry); |
| 280 | if (!ino) |
| 281 | return 1; |
| 282 | |
| 283 | /* |
| 284 | * If we've been asked to wait on an existing expire (NFY_NONE) |
| 285 | * but there is no wait in the queue ... |
| 286 | */ |
| 287 | if (notify == NFY_NONE) { |
| 288 | /* |
| 289 | * Either we've betean the pending expire to post it's |
| 290 | * wait or it finished while we waited on the mutex. |
| 291 | * So we need to wait till either, the wait appears |
| 292 | * or the expire finishes. |
| 293 | */ |
| 294 | |
| 295 | while (ino->flags & AUTOFS_INF_EXPIRING) { |
| 296 | mutex_unlock(&sbi->wq_mutex); |
| 297 | schedule_timeout_interruptible(HZ/10); |
| 298 | if (mutex_lock_interruptible(&sbi->wq_mutex)) |
| 299 | return -EINTR; |
| 300 | |
Al Viro | 4041bcd | 2012-01-10 22:20:12 -0500 | [diff] [blame] | 301 | if (sbi->catatonic) |
| 302 | return -ENOENT; |
| 303 | |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 304 | wq = autofs4_find_wait(sbi, qstr); |
| 305 | if (wq) { |
| 306 | *wait = wq; |
| 307 | return 1; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Not ideal but the status has already gone. Of the two |
| 313 | * cases where we wait on NFY_NONE neither depend on the |
| 314 | * return status of the wait. |
| 315 | */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * If we've been asked to trigger a mount and the request |
| 321 | * completed while we waited on the mutex ... |
| 322 | */ |
| 323 | if (notify == NFY_MOUNT) { |
Ian Kent | 9e3fea1 | 2011-01-14 18:46:30 +0000 | [diff] [blame] | 324 | struct dentry *new = NULL; |
Ian Kent | 6035974 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 325 | struct path this; |
Ian Kent | 9e3fea1 | 2011-01-14 18:46:30 +0000 | [diff] [blame] | 326 | int valid = 1; |
| 327 | |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 328 | /* |
Ian Kent | 463aea1 | 2009-06-09 16:26:24 -0700 | [diff] [blame] | 329 | * If the dentry was successfully mounted while we slept |
| 330 | * on the wait queue mutex we can return success. If it |
| 331 | * isn't mounted (doesn't have submounts for the case of |
| 332 | * a multi-mount with no mount at it's base) we can |
| 333 | * continue on and create a new request. |
| 334 | */ |
Ian Kent | 9e3fea1 | 2011-01-14 18:46:30 +0000 | [diff] [blame] | 335 | if (!IS_ROOT(dentry)) { |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 336 | if (d_unhashed(dentry) && |
| 337 | d_really_is_positive(dentry)) { |
Ian Kent | 9e3fea1 | 2011-01-14 18:46:30 +0000 | [diff] [blame] | 338 | struct dentry *parent = dentry->d_parent; |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 339 | |
Ian Kent | 9e3fea1 | 2011-01-14 18:46:30 +0000 | [diff] [blame] | 340 | new = d_lookup(parent, &dentry->d_name); |
| 341 | if (new) |
| 342 | dentry = new; |
| 343 | } |
| 344 | } |
Ian Kent | 6035974 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 345 | this.mnt = path->mnt; |
| 346 | this.dentry = dentry; |
| 347 | if (path_has_submounts(&this)) |
Ian Kent | 9e3fea1 | 2011-01-14 18:46:30 +0000 | [diff] [blame] | 348 | valid = 0; |
| 349 | |
| 350 | if (new) |
| 351 | dput(new); |
| 352 | return valid; |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return 1; |
| 356 | } |
| 357 | |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 358 | int autofs4_wait(struct autofs_sb_info *sbi, |
Ian Kent | dd36a88 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 359 | const struct path *path, enum autofs_notify notify) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Ian Kent | dd36a88 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 361 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | struct autofs_wait_queue *wq; |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 363 | struct qstr qstr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | char *name; |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 365 | int status, ret, type; |
Miklos Szeredi | fbff087 | 2014-01-23 15:54:58 -0800 | [diff] [blame] | 366 | pid_t pid; |
| 367 | pid_t tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* In catatonic mode, we don't wait for nobody */ |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 370 | if (sbi->catatonic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return -ENOENT; |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 372 | |
Miklos Szeredi | fbff087 | 2014-01-23 15:54:58 -0800 | [diff] [blame] | 373 | /* |
| 374 | * Try translating pids to the namespace of the daemon. |
| 375 | * |
| 376 | * Zero means failure: we are in an unrelated pid namespace. |
| 377 | */ |
| 378 | pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp)); |
| 379 | tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp)); |
| 380 | if (pid == 0 || tgid == 0) |
| 381 | return -ENOENT; |
| 382 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 383 | if (d_really_is_negative(dentry)) { |
Ian Kent | c72305b | 2008-07-23 21:30:23 -0700 | [diff] [blame] | 384 | /* |
| 385 | * A wait for a negative dentry is invalid for certain |
| 386 | * cases. A direct or offset mount "always" has its mount |
| 387 | * point directory created and so the request dentry must |
| 388 | * be positive or the map key doesn't exist. The situation |
| 389 | * is very similar for indirect mounts except only dentrys |
| 390 | * in the root of the autofs file system may be negative. |
| 391 | */ |
Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 392 | if (autofs_type_trigger(sbi->type)) |
Ian Kent | c72305b | 2008-07-23 21:30:23 -0700 | [diff] [blame] | 393 | return -ENOENT; |
| 394 | else if (!IS_ROOT(dentry->d_parent)) |
| 395 | return -ENOENT; |
| 396 | } |
Ian Kent | eb3b176 | 2008-07-23 21:30:22 -0700 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | name = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 399 | if (!name) |
| 400 | return -ENOMEM; |
| 401 | |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 402 | /* If this is a direct mount request create a dummy name */ |
Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 403 | if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type)) |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 404 | qstr.len = sprintf(name, "%p", dentry); |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 405 | else { |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 406 | qstr.len = autofs4_getpath(sbi, dentry, &name); |
| 407 | if (!qstr.len) { |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 408 | kfree(name); |
| 409 | return -ENOENT; |
| 410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 412 | qstr.name = name; |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 413 | qstr.hash = full_name_hash(dentry, name, qstr.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Ian Kent | f4c7da0 | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 415 | if (mutex_lock_interruptible(&sbi->wq_mutex)) { |
| 416 | kfree(qstr.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return -EINTR; |
Ian Kent | f4c7da0 | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Ian Kent | 6035974 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 420 | ret = validate_request(&wq, sbi, &qstr, path, notify); |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 421 | if (ret <= 0) { |
Al Viro | 4041bcd | 2012-01-10 22:20:12 -0500 | [diff] [blame] | 422 | if (ret != -EINTR) |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 423 | mutex_unlock(&sbi->wq_mutex); |
Ian Kent | a1362fe | 2008-07-23 21:30:19 -0700 | [diff] [blame] | 424 | kfree(qstr.name); |
| 425 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 428 | if (!wq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | /* Create a new wait queue */ |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 430 | wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL); |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 431 | if (!wq) { |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 432 | kfree(qstr.name); |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 433 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 437 | wq->wait_queue_token = autofs4_next_wait_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (++autofs4_next_wait_queue == 0) |
| 439 | autofs4_next_wait_queue = 1; |
| 440 | wq->next = sbi->queues; |
| 441 | sbi->queues = wq; |
| 442 | init_waitqueue_head(&wq->queue); |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 443 | memcpy(&wq->name, &qstr, sizeof(struct qstr)); |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 444 | wq->dev = autofs4_get_dev(sbi); |
| 445 | wq->ino = autofs4_get_ino(sbi); |
Eric W. Biederman | 93faccbb | 2017-02-01 06:06:16 +1300 | [diff] [blame] | 446 | wq->uid = current_cred()->uid; |
| 447 | wq->gid = current_cred()->gid; |
Miklos Szeredi | fbff087 | 2014-01-23 15:54:58 -0800 | [diff] [blame] | 448 | wq->pid = pid; |
| 449 | wq->tgid = tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | wq->status = -EINTR; /* Status return if interrupted */ |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 451 | wq->wait_ctr = 2; |
Ian Kent | 3e7b191 | 2006-03-27 01:14:59 -0800 | [diff] [blame] | 452 | |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 453 | if (sbi->version < 5) { |
| 454 | if (notify == NFY_MOUNT) |
| 455 | type = autofs_ptype_missing; |
| 456 | else |
| 457 | type = autofs_ptype_expire_multi; |
| 458 | } else { |
| 459 | if (notify == NFY_MOUNT) |
Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 460 | type = autofs_type_trigger(sbi->type) ? |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 461 | autofs_ptype_missing_direct : |
| 462 | autofs_ptype_missing_indirect; |
| 463 | else |
Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 464 | type = autofs_type_trigger(sbi->type) ? |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 465 | autofs_ptype_expire_direct : |
| 466 | autofs_ptype_expire_indirect; |
| 467 | } |
Ian Kent | 4dcd00b | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 468 | |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 469 | pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n", |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 470 | (unsigned long) wq->wait_queue_token, wq->name.len, |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 471 | wq->name.name, notify); |
Ian Kent | 4dcd00b | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 472 | |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 473 | /* |
| 474 | * autofs4_notify_daemon() may block; it will unlock ->wq_mutex |
| 475 | */ |
Ian Kent | 4dcd00b | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 476 | autofs4_notify_daemon(sbi, wq, type); |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 477 | } else { |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 478 | wq->wait_ctr++; |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 479 | pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n", |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 480 | (unsigned long) wq->wait_queue_token, wq->name.len, |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 481 | wq->name.name, notify); |
Al Viro | 606035e | 2013-09-14 17:32:12 -0400 | [diff] [blame] | 482 | mutex_unlock(&sbi->wq_mutex); |
| 483 | kfree(qstr.name); |
Ian Kent | 4dcd00b | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Ian Kent | 5a11d4d | 2008-07-23 21:30:17 -0700 | [diff] [blame] | 486 | /* |
| 487 | * wq->name.name is NULL iff the lock is already released |
| 488 | * or the mount has been made catatonic. |
| 489 | */ |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 490 | if (wq->name.name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* Block all but "shutdown" signals while waiting */ |
Ian Kent | b3f67a9 | 2016-03-15 14:58:30 -0700 | [diff] [blame] | 492 | unsigned long shutdown_sigs_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | unsigned long irqflags; |
Ian Kent | b3f67a9 | 2016-03-15 14:58:30 -0700 | [diff] [blame] | 494 | sigset_t oldset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | spin_lock_irqsave(¤t->sighand->siglock, irqflags); |
| 497 | oldset = current->blocked; |
Ian Kent | b3f67a9 | 2016-03-15 14:58:30 -0700 | [diff] [blame] | 498 | shutdown_sigs_mask = SHUTDOWN_SIGS & ~oldset.sig[0]; |
| 499 | siginitsetinv(¤t->blocked, shutdown_sigs_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | recalc_sigpending(); |
| 501 | spin_unlock_irqrestore(¤t->sighand->siglock, irqflags); |
| 502 | |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 503 | wait_event_interruptible(wq->queue, wq->name.name == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | spin_lock_irqsave(¤t->sighand->siglock, irqflags); |
| 506 | current->blocked = oldset; |
| 507 | recalc_sigpending(); |
| 508 | spin_unlock_irqrestore(¤t->sighand->siglock, irqflags); |
| 509 | } else { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 510 | pr_debug("skipped sleeping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | status = wq->status; |
| 514 | |
Ian Kent | c0f54d3 | 2008-10-15 22:02:52 -0700 | [diff] [blame] | 515 | /* |
| 516 | * For direct and offset mounts we need to track the requester's |
| 517 | * uid and gid in the dentry info struct. This is so it can be |
| 518 | * supplied, on request, by the misc device ioctl interface. |
| 519 | * This is needed during daemon resatart when reconnecting |
| 520 | * to existing, active, autofs mounts. The uid and gid (and |
| 521 | * related string values) may be used for macro substitution |
| 522 | * in autofs mount maps. |
| 523 | */ |
| 524 | if (!status) { |
| 525 | struct autofs_info *ino; |
| 526 | struct dentry *de = NULL; |
| 527 | |
| 528 | /* direct mount or browsable map */ |
| 529 | ino = autofs4_dentry_ino(dentry); |
| 530 | if (!ino) { |
| 531 | /* If not lookup actual dentry used */ |
| 532 | de = d_lookup(dentry->d_parent, &dentry->d_name); |
| 533 | if (de) |
| 534 | ino = autofs4_dentry_ino(de); |
| 535 | } |
| 536 | |
| 537 | /* Set mount requester */ |
| 538 | if (ino) { |
| 539 | spin_lock(&sbi->fs_lock); |
| 540 | ino->uid = wq->uid; |
| 541 | ino->gid = wq->gid; |
| 542 | spin_unlock(&sbi->fs_lock); |
| 543 | } |
| 544 | |
| 545 | if (de) |
| 546 | dput(de); |
| 547 | } |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /* Are we the last process to need status? */ |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 550 | mutex_lock(&sbi->wq_mutex); |
| 551 | if (!--wq->wait_ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | kfree(wq); |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 553 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | return status; |
| 556 | } |
| 557 | |
| 558 | |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 559 | int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
| 561 | struct autofs_wait_queue *wq, **wql; |
| 562 | |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 563 | mutex_lock(&sbi->wq_mutex); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 564 | for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) { |
Linus Torvalds | 7cee938 | 2017-07-10 11:40:19 -0700 | [diff] [blame] | 565 | if (wq->wait_queue_token == wait_queue_token) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 569 | if (!wq) { |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 570 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return -EINVAL; |
| 572 | } |
| 573 | |
| 574 | *wql = wq->next; /* Unlink from chain */ |
Jeff Moyer | 70b52a0 | 2008-07-23 21:30:16 -0700 | [diff] [blame] | 575 | kfree(wq->name.name); |
| 576 | wq->name.name = NULL; /* Do not wait on this queue */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | wq->status = status; |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 578 | wake_up_interruptible(&wq->queue); |
| 579 | if (!--wq->wait_ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | kfree(wq); |
Ian Kent | 296f7bf | 2008-07-23 21:30:21 -0700 | [diff] [blame] | 581 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |