blob: 24a58bf9ca72ce3b51f1188162594654de1c4f82 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010013#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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 Kente9a7c2f2016-03-15 14:58:25 -070018 * is better if we don't reassign numbers easily even across filesystems
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static 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
25void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
26{
27 struct autofs_wait_queue *wq, *nwq;
28
Ian Kent5a11d4d2008-07-23 21:30:17 -070029 mutex_lock(&sbi->wq_mutex);
30 if (sbi->catatonic) {
31 mutex_unlock(&sbi->wq_mutex);
32 return;
33 }
34
Ian Kent8a78d592016-03-15 14:58:45 -070035 pr_debug("entering catatonic mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 sbi->catatonic = 1;
38 wq = sbi->queues;
39 sbi->queues = NULL; /* Erase all wait queues */
Ian Kente77fbdd2006-03-27 01:14:49 -080040 while (wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 nwq = wq->next;
42 wq->status = -ENOENT; /* Magic is gone - report failure */
Tim Gardner5140a8c2013-03-01 19:46:48 +080043 kfree(wq->name.name);
44 wq->name.name = NULL;
Ian Kent296f7bf2008-07-23 21:30:21 -070045 wq->wait_ctr--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 wake_up_interruptible(&wq->queue);
47 wq = nwq;
48 }
Ian Kentba8df432006-11-14 02:03:29 -080049 fput(sbi->pipe); /* Close the pipe */
50 sbi->pipe = NULL;
Ian Kent5a11d4d2008-07-23 21:30:17 -070051 sbi->pipefd = -1;
52 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Al Virod668dc52012-01-10 22:35:38 -050055static int autofs4_write(struct autofs_sb_info *sbi,
56 struct file *file, const void *addr, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 unsigned long sigpipe, flags;
59 mm_segment_t fs;
60 const char *data = (const char *)addr;
61 ssize_t wr = 0;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 sigpipe = sigismember(&current->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 Virod668dc52012-01-10 22:35:38 -050069 mutex_lock(&sbi->pipe_mutex);
Andrey Vagin5a9294e52016-06-24 14:50:27 -070070 while (bytes) {
71 wr = __vfs_write(file, data, bytes, &file->f_pos);
72 if (wr <= 0)
73 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 data += wr;
75 bytes -= wr;
76 }
Ian Kent86380942012-01-13 20:41:46 +080077 mutex_unlock(&sbi->pipe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 set_fs(fs);
80
81 /* Keep the currently executing process from receiving a
Ian Kente9a7c2f2016-03-15 14:58:25 -070082 * SIGPIPE unless it was already supposed to get one
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (wr == -EPIPE && !sigpipe) {
85 spin_lock_irqsave(&current->sighand->siglock, flags);
86 sigdelset(&current->pending.signal, SIGPIPE);
87 recalc_sigpending();
88 spin_unlock_irqrestore(&current->sighand->siglock, flags);
89 }
90
91 return (bytes > 0);
92}
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;
105
Ian Kent8a78d592016-03-15 14:58:45 -0700106 pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
Linus Torvalds7cee9382017-07-10 11:40:19 -0700107 (unsigned long) wq->wait_queue_token,
Ian Kent8a78d592016-03-15 14:58:45 -0700108 wq->name.len, wq->name.name, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ian Kente9a7c2f2016-03-15 14:58:25 -0700110 memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 pkt.hdr.proto_version = sbi->version;
113 pkt.hdr.type = type;
Al Viro87533332012-01-10 22:24:48 -0500114
Ian Kent5c0a32f2006-03-27 01:14:55 -0800115 switch (type) {
116 /* Kernel protocol v4 missing and expire packets */
117 case autofs_ptype_missing:
118 {
Ian Kente8514472007-02-20 13:58:09 -0800119 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 pktsz = sizeof(*mp);
122
Linus Torvalds7cee9382017-07-10 11:40:19 -0700123 mp->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700124 mp->len = wq->name.len;
125 memcpy(mp->name, wq->name.name, wq->name.len);
126 mp->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800127 break;
128 }
129 case autofs_ptype_expire_multi:
130 {
Ian Kente9a7c2f2016-03-15 14:58:25 -0700131 struct autofs_packet_expire_multi *ep =
132 &pkt.v4_pkt.expire_multi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 pktsz = sizeof(*ep);
135
Linus Torvalds7cee9382017-07-10 11:40:19 -0700136 ep->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700137 ep->len = wq->name.len;
138 memcpy(ep->name, wq->name.name, wq->name.len);
139 ep->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800140 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 Kente8514472007-02-20 13:58:09 -0800151 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800152 struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800153
Linus Torvaldsfcbf94b2012-04-28 08:29:56 -0700154 pktsz = sizeof(*packet);
155
Linus Torvalds7cee9382017-07-10 11:40:19 -0700156 packet->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700157 packet->len = wq->name.len;
158 memcpy(packet->name, wq->name.name, wq->name.len);
159 packet->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800160 packet->dev = wq->dev;
161 packet->ino = wq->ino;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800162 packet->uid = from_kuid_munged(user_ns, wq->uid);
163 packet->gid = from_kgid_munged(user_ns, wq->gid);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800164 packet->pid = wq->pid;
165 packet->tgid = wq->tgid;
166 break;
167 }
168 default:
Ian Kent8a78d592016-03-15 14:58:45 -0700169 pr_warn("bad type %d!\n", type);
Al Viro87533332012-01-10 22:24:48 -0500170 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return;
172 }
173
Al Virocb0942b2012-08-27 14:48:26 -0400174 pipe = get_file(sbi->pipe);
Al Viro87533332012-01-10 22:24:48 -0500175
Ian Kente64be332008-07-23 21:30:20 -0700176 mutex_unlock(&sbi->wq_mutex);
177
Al Virod668dc52012-01-10 22:35:38 -0500178 if (autofs4_write(sbi, pipe, &pkt, pktsz))
Al Viro87533332012-01-10 22:24:48 -0500179 autofs4_catatonic_mode(sbi);
180 fput(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183static 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 Piggin949854d2011-01-07 17:49:37 +1100188 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 char *p;
Nick Piggin949854d2011-01-07 17:49:37 +1100190 int len;
191 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Nick Piggin949854d2011-01-07 17:49:37 +1100193rename_retry:
194 buf = *name;
195 len = 0;
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100196
Nick Piggin949854d2011-01-07 17:49:37 +1100197 seq = read_seqbegin(&rename_lock);
198 rcu_read_lock();
Ian Kente7854722011-03-25 01:51:31 +0800199 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
201 len += tmp->d_name.len + 1;
202
Ian Kentcab09362008-05-01 04:35:07 -0700203 if (!len || --len > NAME_MAX) {
Ian Kente7854722011-03-25 01:51:31 +0800204 spin_unlock(&sbi->fs_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100205 rcu_read_unlock();
206 if (read_seqretry(&rename_lock, seq))
207 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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 Kente7854722011-03-25 01:51:31 +0800220 spin_unlock(&sbi->fs_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100221 rcu_read_unlock();
222 if (read_seqretry(&rename_lock, seq))
223 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 return len;
226}
227
Ian Kenta5370552006-05-15 09:43:51 -0700228static struct autofs_wait_queue *
Al Viro8ac790f2016-07-20 16:25:07 -0400229autofs4_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
Ian Kenta5370552006-05-15 09:43:51 -0700230{
231 struct autofs_wait_queue *wq;
232
233 for (wq = sbi->queues; wq; wq = wq->next) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700234 if (wq->name.hash == qstr->hash &&
235 wq->name.len == qstr->len &&
236 wq->name.name &&
Ian Kente9a7c2f2016-03-15 14:58:25 -0700237 !memcmp(wq->name.name, qstr->name, qstr->len))
Ian Kenta5370552006-05-15 09:43:51 -0700238 break;
239 }
240 return wq;
241}
242
Ian Kenta1362fe2008-07-23 21:30:19 -0700243/*
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 */
251static int validate_request(struct autofs_wait_queue **wait,
252 struct autofs_sb_info *sbi,
Al Viro8ac790f2016-07-20 16:25:07 -0400253 const struct qstr *qstr,
Ian Kent60359742016-11-24 08:03:42 +1100254 const struct path *path, enum autofs_notify notify)
Ian Kenta1362fe2008-07-23 21:30:19 -0700255{
Ian Kent60359742016-11-24 08:03:42 +1100256 struct dentry *dentry = path->dentry;
Ian Kenta1362fe2008-07-23 21:30:19 -0700257 struct autofs_wait_queue *wq;
258 struct autofs_info *ino;
259
Al Viro4041bcd2012-01-10 22:20:12 -0500260 if (sbi->catatonic)
261 return -ENOENT;
262
Ian Kenta1362fe2008-07-23 21:30:19 -0700263 /* 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 Viro4041bcd2012-01-10 22:20:12 -0500295 if (sbi->catatonic)
296 return -ENOENT;
297
Ian Kenta1362fe2008-07-23 21:30:19 -0700298 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 Kent9e3fea12011-01-14 18:46:30 +0000318 struct dentry *new = NULL;
Ian Kent60359742016-11-24 08:03:42 +1100319 struct path this;
Ian Kent9e3fea12011-01-14 18:46:30 +0000320 int valid = 1;
321
Ian Kenta1362fe2008-07-23 21:30:19 -0700322 /*
Ian Kent463aea12009-06-09 16:26:24 -0700323 * 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 Kent9e3fea12011-01-14 18:46:30 +0000329 if (!IS_ROOT(dentry)) {
Ian Kente9a7c2f2016-03-15 14:58:25 -0700330 if (d_unhashed(dentry) &&
331 d_really_is_positive(dentry)) {
Ian Kent9e3fea12011-01-14 18:46:30 +0000332 struct dentry *parent = dentry->d_parent;
Ian Kente9a7c2f2016-03-15 14:58:25 -0700333
Ian Kent9e3fea12011-01-14 18:46:30 +0000334 new = d_lookup(parent, &dentry->d_name);
335 if (new)
336 dentry = new;
337 }
338 }
Ian Kent60359742016-11-24 08:03:42 +1100339 this.mnt = path->mnt;
340 this.dentry = dentry;
341 if (path_has_submounts(&this))
Ian Kent9e3fea12011-01-14 18:46:30 +0000342 valid = 0;
343
344 if (new)
345 dput(new);
346 return valid;
Ian Kenta1362fe2008-07-23 21:30:19 -0700347 }
348
349 return 1;
350}
351
Ian Kente9a7c2f2016-03-15 14:58:25 -0700352int autofs4_wait(struct autofs_sb_info *sbi,
Ian Kentdd36a882016-11-24 08:03:42 +1100353 const struct path *path, enum autofs_notify notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Ian Kentdd36a882016-11-24 08:03:42 +1100355 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct autofs_wait_queue *wq;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700357 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 char *name;
Ian Kenta1362fe2008-07-23 21:30:19 -0700359 int status, ret, type;
Miklos Szeredifbff0872014-01-23 15:54:58 -0800360 pid_t pid;
361 pid_t tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800364 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ENOENT;
Ian Kenta1362fe2008-07-23 21:30:19 -0700366
Miklos Szeredifbff0872014-01-23 15:54:58 -0800367 /*
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 Howells2b0143b2015-03-17 22:25:59 +0000377 if (d_really_is_negative(dentry)) {
Ian Kentc72305b2008-07-23 21:30:23 -0700378 /*
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 Kenta92daf62009-01-06 14:42:08 -0800386 if (autofs_type_trigger(sbi->type))
Ian Kentc72305b2008-07-23 21:30:23 -0700387 return -ENOENT;
388 else if (!IS_ROOT(dentry->d_parent))
389 return -ENOENT;
390 }
Ian Kenteb3b1762008-07-23 21:30:22 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
393 if (!name)
394 return -ENOMEM;
395
Ian Kent5c0a32f2006-03-27 01:14:55 -0800396 /* If this is a direct mount request create a dummy name */
Ian Kenta92daf62009-01-06 14:42:08 -0800397 if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
Jeff Moyer70b52a02008-07-23 21:30:16 -0700398 qstr.len = sprintf(name, "%p", dentry);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800399 else {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700400 qstr.len = autofs4_getpath(sbi, dentry, &name);
401 if (!qstr.len) {
Ian Kent5c0a32f2006-03-27 01:14:55 -0800402 kfree(name);
403 return -ENOENT;
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700406 qstr.name = name;
Linus Torvalds8387ff22016-06-10 07:51:30 -0700407 qstr.hash = full_name_hash(dentry, name, qstr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Ian Kentf4c7da02008-07-23 21:30:19 -0700409 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
410 kfree(qstr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -EINTR;
Ian Kentf4c7da02008-07-23 21:30:19 -0700412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Ian Kent60359742016-11-24 08:03:42 +1100414 ret = validate_request(&wq, sbi, &qstr, path, notify);
Ian Kenta1362fe2008-07-23 21:30:19 -0700415 if (ret <= 0) {
Al Viro4041bcd2012-01-10 22:20:12 -0500416 if (ret != -EINTR)
Ian Kenta5370552006-05-15 09:43:51 -0700417 mutex_unlock(&sbi->wq_mutex);
Ian Kenta1362fe2008-07-23 21:30:19 -0700418 kfree(qstr.name);
419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Ian Kente77fbdd2006-03-27 01:14:49 -0800422 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* Create a new wait queue */
Ian Kente9a7c2f2016-03-15 14:58:25 -0700424 wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800425 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700426 kfree(qstr.name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800427 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ENOMEM;
429 }
430
Linus Torvalds7cee9382017-07-10 11:40:19 -0700431 wq->wait_queue_token = autofs4_next_wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 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 Moyer70b52a02008-07-23 21:30:16 -0700437 memcpy(&wq->name, &qstr, sizeof(struct qstr));
Ian Kent5c0a32f2006-03-27 01:14:55 -0800438 wq->dev = autofs4_get_dev(sbi);
439 wq->ino = autofs4_get_ino(sbi);
Eric W. Biederman93faccbb2017-02-01 06:06:16 +1300440 wq->uid = current_cred()->uid;
441 wq->gid = current_cred()->gid;
Miklos Szeredifbff0872014-01-23 15:54:58 -0800442 wq->pid = pid;
443 wq->tgid = tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 wq->status = -EINTR; /* Status return if interrupted */
Ian Kent296f7bf2008-07-23 21:30:21 -0700445 wq->wait_ctr = 2;
Ian Kent3e7b1912006-03-27 01:14:59 -0800446
Ian Kent5c0a32f2006-03-27 01:14:55 -0800447 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 Kenta92daf62009-01-06 14:42:08 -0800454 type = autofs_type_trigger(sbi->type) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800455 autofs_ptype_missing_direct :
456 autofs_ptype_missing_indirect;
457 else
Ian Kenta92daf62009-01-06 14:42:08 -0800458 type = autofs_type_trigger(sbi->type) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800459 autofs_ptype_expire_direct :
460 autofs_ptype_expire_indirect;
461 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700462
Ian Kent8a78d592016-03-15 14:58:45 -0700463 pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
Linus Torvalds7cee9382017-07-10 11:40:19 -0700464 (unsigned long) wq->wait_queue_token, wq->name.len,
Ian Kent8a78d592016-03-15 14:58:45 -0700465 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700466
Ian Kente9a7c2f2016-03-15 14:58:25 -0700467 /*
468 * autofs4_notify_daemon() may block; it will unlock ->wq_mutex
469 */
Ian Kent4dcd00b2005-05-01 08:59:16 -0700470 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700471 } else {
Ian Kent296f7bf2008-07-23 21:30:21 -0700472 wq->wait_ctr++;
Ian Kent8a78d592016-03-15 14:58:45 -0700473 pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
Linus Torvalds7cee9382017-07-10 11:40:19 -0700474 (unsigned long) wq->wait_queue_token, wq->name.len,
Ian Kent8a78d592016-03-15 14:58:45 -0700475 wq->name.name, notify);
Al Viro606035e2013-09-14 17:32:12 -0400476 mutex_unlock(&sbi->wq_mutex);
477 kfree(qstr.name);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700478 }
479
Ian Kent5a11d4d2008-07-23 21:30:17 -0700480 /*
481 * wq->name.name is NULL iff the lock is already released
482 * or the mount has been made catatonic.
483 */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700484 if (wq->name.name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Block all but "shutdown" signals while waiting */
Ian Kentb3f67a92016-03-15 14:58:30 -0700486 unsigned long shutdown_sigs_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unsigned long irqflags;
Ian Kentb3f67a92016-03-15 14:58:30 -0700488 sigset_t oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 spin_lock_irqsave(&current->sighand->siglock, irqflags);
491 oldset = current->blocked;
Ian Kentb3f67a92016-03-15 14:58:30 -0700492 shutdown_sigs_mask = SHUTDOWN_SIGS & ~oldset.sig[0];
493 siginitsetinv(&current->blocked, shutdown_sigs_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 recalc_sigpending();
495 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
496
Jeff Moyer70b52a02008-07-23 21:30:16 -0700497 wait_event_interruptible(wq->queue, wq->name.name == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 spin_lock_irqsave(&current->sighand->siglock, irqflags);
500 current->blocked = oldset;
501 recalc_sigpending();
502 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
503 } else {
Ian Kent8a78d592016-03-15 14:58:45 -0700504 pr_debug("skipped sleeping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 status = wq->status;
508
Ian Kentc0f54d32008-10-15 22:02:52 -0700509 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700543 /* Are we the last process to need status? */
Ian Kent296f7bf2008-07-23 21:30:21 -0700544 mutex_lock(&sbi->wq_mutex);
545 if (!--wq->wait_ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 kfree(wq);
Ian Kent296f7bf2008-07-23 21:30:21 -0700547 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 return status;
550}
551
552
Linus Torvalds7cee9382017-07-10 11:40:19 -0700553int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct autofs_wait_queue *wq, **wql;
556
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800557 mutex_lock(&sbi->wq_mutex);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700558 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
Linus Torvalds7cee9382017-07-10 11:40:19 -0700559 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561 }
562
Ian Kente77fbdd2006-03-27 01:14:49 -0800563 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800564 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EINVAL;
566 }
567
568 *wql = wq->next; /* Unlink from chain */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700569 kfree(wq->name.name);
570 wq->name.name = NULL; /* Do not wait on this queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 wq->status = status;
Ian Kent296f7bf2008-07-23 21:30:21 -0700572 wake_up_interruptible(&wq->queue);
573 if (!--wq->wait_ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 kfree(wq);
Ian Kent296f7bf2008-07-23 21:30:21 -0700575 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return 0;
578}