blob: 4c71dba90120a52648045171fae8b1e025b49997 [file] [log] [blame]
Ian Kente9a7c2f2016-03-15 14:58:25 -07001/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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 Kente9a7c2f2016-03-15 14:58:25 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/slab.h>
11#include <linux/time.h>
12#include <linux/signal.h>
13#include <linux/file.h>
14#include "autofs_i.h"
15
16/* We make this a static variable rather than a part of the superblock; it
Ian Kente9a7c2f2016-03-15 14:58:25 -070017 * is better if we don't reassign numbers easily even across filesystems
18 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static autofs_wqt_t autofs4_next_wait_queue = 1;
20
21/* These are the signals we allow interrupting a pending mount */
22#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
23
24void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
25{
26 struct autofs_wait_queue *wq, *nwq;
27
Ian Kent5a11d4d2008-07-23 21:30:17 -070028 mutex_lock(&sbi->wq_mutex);
29 if (sbi->catatonic) {
30 mutex_unlock(&sbi->wq_mutex);
31 return;
32 }
33
Ian Kent8a78d592016-03-15 14:58:45 -070034 pr_debug("entering catatonic mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 sbi->catatonic = 1;
37 wq = sbi->queues;
38 sbi->queues = NULL; /* Erase all wait queues */
Ian Kente77fbdd2006-03-27 01:14:49 -080039 while (wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 nwq = wq->next;
41 wq->status = -ENOENT; /* Magic is gone - report failure */
Tim Gardner5140a8c2013-03-01 19:46:48 +080042 kfree(wq->name.name);
43 wq->name.name = NULL;
Ian Kent296f7bf2008-07-23 21:30:21 -070044 wq->wait_ctr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 wake_up_interruptible(&wq->queue);
46 wq = nwq;
47 }
Ian Kentba8df432006-11-14 02:03:29 -080048 fput(sbi->pipe); /* Close the pipe */
49 sbi->pipe = NULL;
Ian Kent5a11d4d2008-07-23 21:30:17 -070050 sbi->pipefd = -1;
51 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Al Virod668dc52012-01-10 22:35:38 -050054static int autofs4_write(struct autofs_sb_info *sbi,
55 struct file *file, const void *addr, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 unsigned long sigpipe, flags;
58 mm_segment_t fs;
59 const char *data = (const char *)addr;
60 ssize_t wr = 0;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
63
64 /* Save pointer to user space and point back to kernel space */
65 fs = get_fs();
66 set_fs(KERNEL_DS);
67
Al Virod668dc52012-01-10 22:35:38 -050068 mutex_lock(&sbi->pipe_mutex);
Andrey Vagin5a9294e52016-06-24 14:50:27 -070069 while (bytes) {
70 wr = __vfs_write(file, data, bytes, &file->f_pos);
71 if (wr <= 0)
72 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 data += wr;
74 bytes -= wr;
75 }
Ian Kent86380942012-01-13 20:41:46 +080076 mutex_unlock(&sbi->pipe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 set_fs(fs);
79
80 /* Keep the currently executing process from receiving a
Ian Kente9a7c2f2016-03-15 14:58:25 -070081 * SIGPIPE unless it was already supposed to get one
82 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (wr == -EPIPE && !sigpipe) {
84 spin_lock_irqsave(&current->sighand->siglock, flags);
85 sigdelset(&current->pending.signal, SIGPIPE);
86 recalc_sigpending();
87 spin_unlock_irqrestore(&current->sighand->siglock, flags);
88 }
89
NeilBrownab96d9c2017-11-17 15:29:13 -080090 /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
91 return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Ian Kent02667252016-03-15 14:58:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
95 struct autofs_wait_queue *wq,
96 int type)
97{
Ian Kente8514472007-02-20 13:58:09 -080098 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 Kente64be332008-07-23 21:30:20 -0700103 struct file *pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 size_t pktsz;
NeilBrownab96d9c2017-11-17 15:29:13 -0800105 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ian Kent8a78d592016-03-15 14:58:45 -0700107 pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
108 (unsigned long) wq->wait_queue_token,
109 wq->name.len, wq->name.name, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ian Kente9a7c2f2016-03-15 14:58:25 -0700111 memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 pkt.hdr.proto_version = sbi->version;
114 pkt.hdr.type = type;
Al Viro87533332012-01-10 22:24:48 -0500115
Ian Kent5c0a32f2006-03-27 01:14:55 -0800116 switch (type) {
117 /* Kernel protocol v4 missing and expire packets */
118 case autofs_ptype_missing:
119 {
Ian Kente8514472007-02-20 13:58:09 -0800120 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 pktsz = sizeof(*mp);
123
124 mp->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700125 mp->len = wq->name.len;
126 memcpy(mp->name, wq->name.name, wq->name.len);
127 mp->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800128 break;
129 }
130 case autofs_ptype_expire_multi:
131 {
Ian Kente9a7c2f2016-03-15 14:58:25 -0700132 struct autofs_packet_expire_multi *ep =
133 &pkt.v4_pkt.expire_multi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 pktsz = sizeof(*ep);
136
137 ep->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700138 ep->len = wq->name.len;
139 memcpy(ep->name, wq->name.name, wq->name.len);
140 ep->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800141 break;
142 }
143 /*
144 * Kernel protocol v5 packet for handling indirect and direct
145 * mount missing and expire requests
146 */
147 case autofs_ptype_missing_indirect:
148 case autofs_ptype_expire_indirect:
149 case autofs_ptype_missing_direct:
150 case autofs_ptype_expire_direct:
151 {
Ian Kente8514472007-02-20 13:58:09 -0800152 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800153 struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800154
Linus Torvaldsfcbf94b2012-04-28 08:29:56 -0700155 pktsz = sizeof(*packet);
156
Ian Kent5c0a32f2006-03-27 01:14:55 -0800157 packet->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700158 packet->len = wq->name.len;
159 memcpy(packet->name, wq->name.name, wq->name.len);
160 packet->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800161 packet->dev = wq->dev;
162 packet->ino = wq->ino;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800163 packet->uid = from_kuid_munged(user_ns, wq->uid);
164 packet->gid = from_kgid_munged(user_ns, wq->gid);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800165 packet->pid = wq->pid;
166 packet->tgid = wq->tgid;
167 break;
168 }
169 default:
Ian Kent8a78d592016-03-15 14:58:45 -0700170 pr_warn("bad type %d!\n", type);
Al Viro87533332012-01-10 22:24:48 -0500171 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return;
173 }
174
Al Virocb0942b2012-08-27 14:48:26 -0400175 pipe = get_file(sbi->pipe);
Al Viro87533332012-01-10 22:24:48 -0500176
Ian Kente64be332008-07-23 21:30:20 -0700177 mutex_unlock(&sbi->wq_mutex);
178
Al Virod668dc52012-01-10 22:35:38 -0500179 if (autofs4_write(sbi, pipe, &pkt, pktsz))
NeilBrownab96d9c2017-11-17 15:29:13 -0800180 switch (ret = autofs4_write(sbi, pipe, &pkt, pktsz)) {
181 case 0:
182 break;
183 case -ENOMEM:
184 case -ERESTARTSYS:
185 /* Just fail this one */
186 autofs4_wait_release(sbi, wq->wait_queue_token, ret);
187 break;
188 default:
Al Viro87533332012-01-10 22:24:48 -0500189 autofs4_catatonic_mode(sbi);
NeilBrownab96d9c2017-11-17 15:29:13 -0800190 break;
191 }
Al Viro87533332012-01-10 22:24:48 -0500192 fput(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static int autofs4_getpath(struct autofs_sb_info *sbi,
196 struct dentry *dentry, char **name)
197{
198 struct dentry *root = sbi->sb->s_root;
199 struct dentry *tmp;
Nick Piggin949854d2011-01-07 17:49:37 +1100200 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 char *p;
Nick Piggin949854d2011-01-07 17:49:37 +1100202 int len;
203 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Nick Piggin949854d2011-01-07 17:49:37 +1100205rename_retry:
206 buf = *name;
207 len = 0;
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100208
Nick Piggin949854d2011-01-07 17:49:37 +1100209 seq = read_seqbegin(&rename_lock);
210 rcu_read_lock();
Ian Kente7854722011-03-25 01:51:31 +0800211 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
213 len += tmp->d_name.len + 1;
214
Ian Kentcab09362008-05-01 04:35:07 -0700215 if (!len || --len > NAME_MAX) {
Ian Kente7854722011-03-25 01:51:31 +0800216 spin_unlock(&sbi->fs_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100217 rcu_read_unlock();
218 if (read_seqretry(&rename_lock, seq))
219 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221 }
222
223 *(buf + len) = '\0';
224 p = buf + len - dentry->d_name.len;
225 strncpy(p, dentry->d_name.name, dentry->d_name.len);
226
227 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
228 *(--p) = '/';
229 p -= tmp->d_name.len;
230 strncpy(p, tmp->d_name.name, tmp->d_name.len);
231 }
Ian Kente7854722011-03-25 01:51:31 +0800232 spin_unlock(&sbi->fs_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100233 rcu_read_unlock();
234 if (read_seqretry(&rename_lock, seq))
235 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 return len;
238}
239
Ian Kenta5370552006-05-15 09:43:51 -0700240static struct autofs_wait_queue *
Al Viro8ac790f2016-07-20 16:25:07 -0400241autofs4_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
Ian Kenta5370552006-05-15 09:43:51 -0700242{
243 struct autofs_wait_queue *wq;
244
245 for (wq = sbi->queues; wq; wq = wq->next) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700246 if (wq->name.hash == qstr->hash &&
247 wq->name.len == qstr->len &&
248 wq->name.name &&
Ian Kente9a7c2f2016-03-15 14:58:25 -0700249 !memcmp(wq->name.name, qstr->name, qstr->len))
Ian Kenta5370552006-05-15 09:43:51 -0700250 break;
251 }
252 return wq;
253}
254
Ian Kenta1362fe2008-07-23 21:30:19 -0700255/*
256 * Check if we have a valid request.
257 * Returns
258 * 1 if the request should continue.
259 * In this case we can return an autofs_wait_queue entry if one is
260 * found or NULL to idicate a new wait needs to be created.
261 * 0 or a negative errno if the request shouldn't continue.
262 */
263static int validate_request(struct autofs_wait_queue **wait,
264 struct autofs_sb_info *sbi,
Al Viro8ac790f2016-07-20 16:25:07 -0400265 const struct qstr *qstr,
Ian Kente9a7c2f2016-03-15 14:58:25 -0700266 struct dentry *dentry, enum autofs_notify notify)
Ian Kenta1362fe2008-07-23 21:30:19 -0700267{
268 struct autofs_wait_queue *wq;
269 struct autofs_info *ino;
270
Al Viro4041bcd2012-01-10 22:20:12 -0500271 if (sbi->catatonic)
272 return -ENOENT;
273
Ian Kenta1362fe2008-07-23 21:30:19 -0700274 /* Wait in progress, continue; */
275 wq = autofs4_find_wait(sbi, qstr);
276 if (wq) {
277 *wait = wq;
278 return 1;
279 }
280
281 *wait = NULL;
282
283 /* If we don't yet have any info this is a new request */
284 ino = autofs4_dentry_ino(dentry);
285 if (!ino)
286 return 1;
287
288 /*
289 * If we've been asked to wait on an existing expire (NFY_NONE)
290 * but there is no wait in the queue ...
291 */
292 if (notify == NFY_NONE) {
293 /*
294 * Either we've betean the pending expire to post it's
295 * wait or it finished while we waited on the mutex.
296 * So we need to wait till either, the wait appears
297 * or the expire finishes.
298 */
299
300 while (ino->flags & AUTOFS_INF_EXPIRING) {
301 mutex_unlock(&sbi->wq_mutex);
302 schedule_timeout_interruptible(HZ/10);
303 if (mutex_lock_interruptible(&sbi->wq_mutex))
304 return -EINTR;
305
Al Viro4041bcd2012-01-10 22:20:12 -0500306 if (sbi->catatonic)
307 return -ENOENT;
308
Ian Kenta1362fe2008-07-23 21:30:19 -0700309 wq = autofs4_find_wait(sbi, qstr);
310 if (wq) {
311 *wait = wq;
312 return 1;
313 }
314 }
315
316 /*
317 * Not ideal but the status has already gone. Of the two
318 * cases where we wait on NFY_NONE neither depend on the
319 * return status of the wait.
320 */
321 return 0;
322 }
323
324 /*
325 * If we've been asked to trigger a mount and the request
326 * completed while we waited on the mutex ...
327 */
328 if (notify == NFY_MOUNT) {
Ian Kent9e3fea12011-01-14 18:46:30 +0000329 struct dentry *new = NULL;
330 int valid = 1;
331
Ian Kenta1362fe2008-07-23 21:30:19 -0700332 /*
Ian Kent463aea12009-06-09 16:26:24 -0700333 * If the dentry was successfully mounted while we slept
334 * on the wait queue mutex we can return success. If it
335 * isn't mounted (doesn't have submounts for the case of
336 * a multi-mount with no mount at it's base) we can
337 * continue on and create a new request.
338 */
Ian Kent9e3fea12011-01-14 18:46:30 +0000339 if (!IS_ROOT(dentry)) {
Ian Kente9a7c2f2016-03-15 14:58:25 -0700340 if (d_unhashed(dentry) &&
341 d_really_is_positive(dentry)) {
Ian Kent9e3fea12011-01-14 18:46:30 +0000342 struct dentry *parent = dentry->d_parent;
Ian Kente9a7c2f2016-03-15 14:58:25 -0700343
Ian Kent9e3fea12011-01-14 18:46:30 +0000344 new = d_lookup(parent, &dentry->d_name);
345 if (new)
346 dentry = new;
347 }
348 }
Ian Kent463aea12009-06-09 16:26:24 -0700349 if (have_submounts(dentry))
Ian Kent9e3fea12011-01-14 18:46:30 +0000350 valid = 0;
351
352 if (new)
353 dput(new);
354 return valid;
Ian Kenta1362fe2008-07-23 21:30:19 -0700355 }
356
357 return 1;
358}
359
Ian Kente9a7c2f2016-03-15 14:58:25 -0700360int autofs4_wait(struct autofs_sb_info *sbi,
361 struct dentry *dentry, enum autofs_notify notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct autofs_wait_queue *wq;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700364 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 char *name;
Ian Kenta1362fe2008-07-23 21:30:19 -0700366 int status, ret, type;
Miklos Szeredifbff0872014-01-23 15:54:58 -0800367 pid_t pid;
368 pid_t tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800371 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -ENOENT;
Ian Kenta1362fe2008-07-23 21:30:19 -0700373
Miklos Szeredifbff0872014-01-23 15:54:58 -0800374 /*
375 * Try translating pids to the namespace of the daemon.
376 *
377 * Zero means failure: we are in an unrelated pid namespace.
378 */
379 pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
380 tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
381 if (pid == 0 || tgid == 0)
382 return -ENOENT;
383
David Howells2b0143b2015-03-17 22:25:59 +0000384 if (d_really_is_negative(dentry)) {
Ian Kentc72305b2008-07-23 21:30:23 -0700385 /*
386 * A wait for a negative dentry is invalid for certain
387 * cases. A direct or offset mount "always" has its mount
388 * point directory created and so the request dentry must
389 * be positive or the map key doesn't exist. The situation
390 * is very similar for indirect mounts except only dentrys
391 * in the root of the autofs file system may be negative.
392 */
Ian Kenta92daf62009-01-06 14:42:08 -0800393 if (autofs_type_trigger(sbi->type))
Ian Kentc72305b2008-07-23 21:30:23 -0700394 return -ENOENT;
395 else if (!IS_ROOT(dentry->d_parent))
396 return -ENOENT;
397 }
Ian Kenteb3b1762008-07-23 21:30:22 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
400 if (!name)
401 return -ENOMEM;
402
Ian Kent5c0a32f2006-03-27 01:14:55 -0800403 /* If this is a direct mount request create a dummy name */
Ian Kenta92daf62009-01-06 14:42:08 -0800404 if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
Jeff Moyer70b52a02008-07-23 21:30:16 -0700405 qstr.len = sprintf(name, "%p", dentry);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800406 else {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700407 qstr.len = autofs4_getpath(sbi, dentry, &name);
408 if (!qstr.len) {
Ian Kent5c0a32f2006-03-27 01:14:55 -0800409 kfree(name);
410 return -ENOENT;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700413 qstr.name = name;
Linus Torvalds8387ff22016-06-10 07:51:30 -0700414 qstr.hash = full_name_hash(dentry, name, qstr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Ian Kentf4c7da02008-07-23 21:30:19 -0700416 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
417 kfree(qstr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -EINTR;
Ian Kentf4c7da02008-07-23 21:30:19 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Ian Kenta1362fe2008-07-23 21:30:19 -0700421 ret = validate_request(&wq, sbi, &qstr, dentry, notify);
422 if (ret <= 0) {
Al Viro4041bcd2012-01-10 22:20:12 -0500423 if (ret != -EINTR)
Ian Kenta5370552006-05-15 09:43:51 -0700424 mutex_unlock(&sbi->wq_mutex);
Ian Kenta1362fe2008-07-23 21:30:19 -0700425 kfree(qstr.name);
426 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Ian Kente77fbdd2006-03-27 01:14:49 -0800429 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* Create a new wait queue */
Ian Kente9a7c2f2016-03-15 14:58:25 -0700431 wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800432 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700433 kfree(qstr.name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800434 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -ENOMEM;
436 }
437
438 wq->wait_queue_token = autofs4_next_wait_queue;
439 if (++autofs4_next_wait_queue == 0)
440 autofs4_next_wait_queue = 1;
441 wq->next = sbi->queues;
442 sbi->queues = wq;
443 init_waitqueue_head(&wq->queue);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700444 memcpy(&wq->name, &qstr, sizeof(struct qstr));
Ian Kent5c0a32f2006-03-27 01:14:55 -0800445 wq->dev = autofs4_get_dev(sbi);
446 wq->ino = autofs4_get_ino(sbi);
Eric W. Biedermand3381fa2017-02-01 06:06:16 +1300447 wq->uid = current_cred()->uid;
448 wq->gid = current_cred()->gid;
Miklos Szeredifbff0872014-01-23 15:54:58 -0800449 wq->pid = pid;
450 wq->tgid = tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 wq->status = -EINTR; /* Status return if interrupted */
Ian Kent296f7bf2008-07-23 21:30:21 -0700452 wq->wait_ctr = 2;
Ian Kent3e7b1912006-03-27 01:14:59 -0800453
Ian Kent5c0a32f2006-03-27 01:14:55 -0800454 if (sbi->version < 5) {
455 if (notify == NFY_MOUNT)
456 type = autofs_ptype_missing;
457 else
458 type = autofs_ptype_expire_multi;
459 } else {
460 if (notify == NFY_MOUNT)
Ian Kenta92daf62009-01-06 14:42:08 -0800461 type = autofs_type_trigger(sbi->type) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800462 autofs_ptype_missing_direct :
463 autofs_ptype_missing_indirect;
464 else
Ian Kenta92daf62009-01-06 14:42:08 -0800465 type = autofs_type_trigger(sbi->type) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800466 autofs_ptype_expire_direct :
467 autofs_ptype_expire_indirect;
468 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700469
Ian Kent8a78d592016-03-15 14:58:45 -0700470 pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
471 (unsigned long) wq->wait_queue_token, wq->name.len,
472 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700473
Ian Kente9a7c2f2016-03-15 14:58:25 -0700474 /*
475 * autofs4_notify_daemon() may block; it will unlock ->wq_mutex
476 */
Ian Kent4dcd00b2005-05-01 08:59:16 -0700477 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700478 } else {
Ian Kent296f7bf2008-07-23 21:30:21 -0700479 wq->wait_ctr++;
Ian Kent8a78d592016-03-15 14:58:45 -0700480 pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
481 (unsigned long) wq->wait_queue_token, wq->name.len,
482 wq->name.name, notify);
Al Viro606035e2013-09-14 17:32:12 -0400483 mutex_unlock(&sbi->wq_mutex);
484 kfree(qstr.name);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700485 }
486
Ian Kent5a11d4d2008-07-23 21:30:17 -0700487 /*
488 * wq->name.name is NULL iff the lock is already released
489 * or the mount has been made catatonic.
490 */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700491 if (wq->name.name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Block all but "shutdown" signals while waiting */
Ian Kentb3f67a92016-03-15 14:58:30 -0700493 unsigned long shutdown_sigs_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned long irqflags;
Ian Kentb3f67a92016-03-15 14:58:30 -0700495 sigset_t oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 spin_lock_irqsave(&current->sighand->siglock, irqflags);
498 oldset = current->blocked;
Ian Kentb3f67a92016-03-15 14:58:30 -0700499 shutdown_sigs_mask = SHUTDOWN_SIGS & ~oldset.sig[0];
500 siginitsetinv(&current->blocked, shutdown_sigs_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 recalc_sigpending();
502 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
503
Jeff Moyer70b52a02008-07-23 21:30:16 -0700504 wait_event_interruptible(wq->queue, wq->name.name == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 spin_lock_irqsave(&current->sighand->siglock, irqflags);
507 current->blocked = oldset;
508 recalc_sigpending();
509 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
510 } else {
Ian Kent8a78d592016-03-15 14:58:45 -0700511 pr_debug("skipped sleeping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 status = wq->status;
515
Ian Kentc0f54d32008-10-15 22:02:52 -0700516 /*
517 * For direct and offset mounts we need to track the requester's
518 * uid and gid in the dentry info struct. This is so it can be
519 * supplied, on request, by the misc device ioctl interface.
520 * This is needed during daemon resatart when reconnecting
521 * to existing, active, autofs mounts. The uid and gid (and
522 * related string values) may be used for macro substitution
523 * in autofs mount maps.
524 */
525 if (!status) {
526 struct autofs_info *ino;
527 struct dentry *de = NULL;
528
529 /* direct mount or browsable map */
530 ino = autofs4_dentry_ino(dentry);
531 if (!ino) {
532 /* If not lookup actual dentry used */
533 de = d_lookup(dentry->d_parent, &dentry->d_name);
534 if (de)
535 ino = autofs4_dentry_ino(de);
536 }
537
538 /* Set mount requester */
539 if (ino) {
540 spin_lock(&sbi->fs_lock);
541 ino->uid = wq->uid;
542 ino->gid = wq->gid;
543 spin_unlock(&sbi->fs_lock);
544 }
545
546 if (de)
547 dput(de);
548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Are we the last process to need status? */
Ian Kent296f7bf2008-07-23 21:30:21 -0700551 mutex_lock(&sbi->wq_mutex);
552 if (!--wq->wait_ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 kfree(wq);
Ian Kent296f7bf2008-07-23 21:30:21 -0700554 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return status;
557}
558
559
560int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
561{
562 struct autofs_wait_queue *wq, **wql;
563
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800564 mutex_lock(&sbi->wq_mutex);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700565 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
Ian Kente77fbdd2006-03-27 01:14:49 -0800566 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 }
569
Ian Kente77fbdd2006-03-27 01:14:49 -0800570 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800571 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return -EINVAL;
573 }
574
575 *wql = wq->next; /* Unlink from chain */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700576 kfree(wq->name.name);
577 wq->name.name = NULL; /* Do not wait on this queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 wq->status = status;
Ian Kent296f7bf2008-07-23 21:30:21 -0700579 wake_up_interruptible(&wq->queue);
580 if (!--wq->wait_ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 kfree(wq);
Ian Kent296f7bf2008-07-23 21:30:21 -0700582 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return 0;
585}