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