blob: a0c57c37fa21917826cfd85f59b8c929a75c8f6d [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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
Al Virod668dc52012-01-10 22:35:38 -050064 mutex_lock(&sbi->pipe_mutex);
Andrey Vagin5a9294e52016-06-24 14:50:27 -070065 while (bytes) {
Christoph Hellwig317d5a52017-09-01 17:39:10 +020066 wr = __kernel_write(file, data, bytes, &file->f_pos);
Andrey Vagin5a9294e52016-06-24 14:50:27 -070067 if (wr <= 0)
68 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 data += wr;
70 bytes -= wr;
71 }
Ian Kent86380942012-01-13 20:41:46 +080072 mutex_unlock(&sbi->pipe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /* Keep the currently executing process from receiving a
Ian Kente9a7c2f2016-03-15 14:58:25 -070075 * SIGPIPE unless it was already supposed to get one
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (wr == -EPIPE && !sigpipe) {
78 spin_lock_irqsave(&current->sighand->siglock, flags);
79 sigdelset(&current->pending.signal, SIGPIPE);
80 recalc_sigpending();
81 spin_unlock_irqrestore(&current->sighand->siglock, flags);
82 }
83
NeilBrownecc0c462017-11-17 15:29:13 -080084 /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
85 return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
Ian Kent02667252016-03-15 14:58:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
89 struct autofs_wait_queue *wq,
90 int type)
91{
Ian Kente8514472007-02-20 13:58:09 -080092 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 Kente64be332008-07-23 21:30:20 -070097 struct file *pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 size_t pktsz;
NeilBrownecc0c462017-11-17 15:29:13 -080099 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Ian Kent8a78d592016-03-15 14:58:45 -0700101 pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
Linus Torvalds7cee9382017-07-10 11:40:19 -0700102 (unsigned long) wq->wait_queue_token,
Ian Kent8a78d592016-03-15 14:58:45 -0700103 wq->name.len, wq->name.name, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ian Kente9a7c2f2016-03-15 14:58:25 -0700105 memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 pkt.hdr.proto_version = sbi->version;
108 pkt.hdr.type = type;
Al Viro87533332012-01-10 22:24:48 -0500109
Ian Kent5c0a32f2006-03-27 01:14:55 -0800110 switch (type) {
111 /* Kernel protocol v4 missing and expire packets */
112 case autofs_ptype_missing:
113 {
Ian Kente8514472007-02-20 13:58:09 -0800114 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 pktsz = sizeof(*mp);
117
Linus Torvalds7cee9382017-07-10 11:40:19 -0700118 mp->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700119 mp->len = wq->name.len;
120 memcpy(mp->name, wq->name.name, wq->name.len);
121 mp->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800122 break;
123 }
124 case autofs_ptype_expire_multi:
125 {
Ian Kente9a7c2f2016-03-15 14:58:25 -0700126 struct autofs_packet_expire_multi *ep =
127 &pkt.v4_pkt.expire_multi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 pktsz = sizeof(*ep);
130
Linus Torvalds7cee9382017-07-10 11:40:19 -0700131 ep->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700132 ep->len = wq->name.len;
133 memcpy(ep->name, wq->name.name, wq->name.len);
134 ep->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800135 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 Kente8514472007-02-20 13:58:09 -0800146 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800147 struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800148
Linus Torvaldsfcbf94b2012-04-28 08:29:56 -0700149 pktsz = sizeof(*packet);
150
Linus Torvalds7cee9382017-07-10 11:40:19 -0700151 packet->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700152 packet->len = wq->name.len;
153 memcpy(packet->name, wq->name.name, wq->name.len);
154 packet->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800155 packet->dev = wq->dev;
156 packet->ino = wq->ino;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800157 packet->uid = from_kuid_munged(user_ns, wq->uid);
158 packet->gid = from_kgid_munged(user_ns, wq->gid);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800159 packet->pid = wq->pid;
160 packet->tgid = wq->tgid;
161 break;
162 }
163 default:
Ian Kent8a78d592016-03-15 14:58:45 -0700164 pr_warn("bad type %d!\n", type);
Al Viro87533332012-01-10 22:24:48 -0500165 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return;
167 }
168
Al Virocb0942b2012-08-27 14:48:26 -0400169 pipe = get_file(sbi->pipe);
Al Viro87533332012-01-10 22:24:48 -0500170
Ian Kente64be332008-07-23 21:30:20 -0700171 mutex_unlock(&sbi->wq_mutex);
172
NeilBrownecc0c462017-11-17 15:29:13 -0800173 switch (ret = autofs4_write(sbi, pipe, &pkt, pktsz)) {
174 case 0:
175 break;
176 case -ENOMEM:
177 case -ERESTARTSYS:
178 /* Just fail this one */
179 autofs4_wait_release(sbi, wq->wait_queue_token, ret);
180 break;
181 default:
Al Viro87533332012-01-10 22:24:48 -0500182 autofs4_catatonic_mode(sbi);
NeilBrownecc0c462017-11-17 15:29:13 -0800183 break;
184 }
Al Viro87533332012-01-10 22:24:48 -0500185 fput(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static int autofs4_getpath(struct autofs_sb_info *sbi,
189 struct dentry *dentry, char **name)
190{
191 struct dentry *root = sbi->sb->s_root;
192 struct dentry *tmp;
Nick Piggin949854d2011-01-07 17:49:37 +1100193 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 char *p;
Nick Piggin949854d2011-01-07 17:49:37 +1100195 int len;
196 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Nick Piggin949854d2011-01-07 17:49:37 +1100198rename_retry:
199 buf = *name;
200 len = 0;
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100201
Nick Piggin949854d2011-01-07 17:49:37 +1100202 seq = read_seqbegin(&rename_lock);
203 rcu_read_lock();
Ian Kente7854722011-03-25 01:51:31 +0800204 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
206 len += tmp->d_name.len + 1;
207
Ian Kentcab09362008-05-01 04:35:07 -0700208 if (!len || --len > NAME_MAX) {
Ian Kente7854722011-03-25 01:51:31 +0800209 spin_unlock(&sbi->fs_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100210 rcu_read_unlock();
211 if (read_seqretry(&rename_lock, seq))
212 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214 }
215
216 *(buf + len) = '\0';
217 p = buf + len - dentry->d_name.len;
218 strncpy(p, dentry->d_name.name, dentry->d_name.len);
219
220 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
221 *(--p) = '/';
222 p -= tmp->d_name.len;
223 strncpy(p, tmp->d_name.name, tmp->d_name.len);
224 }
Ian Kente7854722011-03-25 01:51:31 +0800225 spin_unlock(&sbi->fs_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100226 rcu_read_unlock();
227 if (read_seqretry(&rename_lock, seq))
228 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 return len;
231}
232
Ian Kenta5370552006-05-15 09:43:51 -0700233static struct autofs_wait_queue *
Al Viro8ac790f2016-07-20 16:25:07 -0400234autofs4_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
Ian Kenta5370552006-05-15 09:43:51 -0700235{
236 struct autofs_wait_queue *wq;
237
238 for (wq = sbi->queues; wq; wq = wq->next) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700239 if (wq->name.hash == qstr->hash &&
240 wq->name.len == qstr->len &&
241 wq->name.name &&
Ian Kente9a7c2f2016-03-15 14:58:25 -0700242 !memcmp(wq->name.name, qstr->name, qstr->len))
Ian Kenta5370552006-05-15 09:43:51 -0700243 break;
244 }
245 return wq;
246}
247
Ian Kenta1362fe2008-07-23 21:30:19 -0700248/*
249 * Check if we have a valid request.
250 * Returns
251 * 1 if the request should continue.
252 * In this case we can return an autofs_wait_queue entry if one is
253 * found or NULL to idicate a new wait needs to be created.
254 * 0 or a negative errno if the request shouldn't continue.
255 */
256static int validate_request(struct autofs_wait_queue **wait,
257 struct autofs_sb_info *sbi,
Al Viro8ac790f2016-07-20 16:25:07 -0400258 const struct qstr *qstr,
Ian Kent60359742016-11-24 08:03:42 +1100259 const struct path *path, enum autofs_notify notify)
Ian Kenta1362fe2008-07-23 21:30:19 -0700260{
Ian Kent60359742016-11-24 08:03:42 +1100261 struct dentry *dentry = path->dentry;
Ian Kenta1362fe2008-07-23 21:30:19 -0700262 struct autofs_wait_queue *wq;
263 struct autofs_info *ino;
264
Al Viro4041bcd2012-01-10 22:20:12 -0500265 if (sbi->catatonic)
266 return -ENOENT;
267
Ian Kenta1362fe2008-07-23 21:30:19 -0700268 /* Wait in progress, continue; */
269 wq = autofs4_find_wait(sbi, qstr);
270 if (wq) {
271 *wait = wq;
272 return 1;
273 }
274
275 *wait = NULL;
276
277 /* If we don't yet have any info this is a new request */
278 ino = autofs4_dentry_ino(dentry);
279 if (!ino)
280 return 1;
281
282 /*
283 * If we've been asked to wait on an existing expire (NFY_NONE)
284 * but there is no wait in the queue ...
285 */
286 if (notify == NFY_NONE) {
287 /*
288 * Either we've betean the pending expire to post it's
289 * wait or it finished while we waited on the mutex.
290 * So we need to wait till either, the wait appears
291 * or the expire finishes.
292 */
293
294 while (ino->flags & AUTOFS_INF_EXPIRING) {
295 mutex_unlock(&sbi->wq_mutex);
296 schedule_timeout_interruptible(HZ/10);
297 if (mutex_lock_interruptible(&sbi->wq_mutex))
298 return -EINTR;
299
Al Viro4041bcd2012-01-10 22:20:12 -0500300 if (sbi->catatonic)
301 return -ENOENT;
302
Ian Kenta1362fe2008-07-23 21:30:19 -0700303 wq = autofs4_find_wait(sbi, qstr);
304 if (wq) {
305 *wait = wq;
306 return 1;
307 }
308 }
309
310 /*
311 * Not ideal but the status has already gone. Of the two
312 * cases where we wait on NFY_NONE neither depend on the
313 * return status of the wait.
314 */
315 return 0;
316 }
317
318 /*
319 * If we've been asked to trigger a mount and the request
320 * completed while we waited on the mutex ...
321 */
322 if (notify == NFY_MOUNT) {
Ian Kent9e3fea12011-01-14 18:46:30 +0000323 struct dentry *new = NULL;
Ian Kent60359742016-11-24 08:03:42 +1100324 struct path this;
Ian Kent9e3fea12011-01-14 18:46:30 +0000325 int valid = 1;
326
Ian Kenta1362fe2008-07-23 21:30:19 -0700327 /*
Ian Kent463aea12009-06-09 16:26:24 -0700328 * If the dentry was successfully mounted while we slept
329 * on the wait queue mutex we can return success. If it
330 * isn't mounted (doesn't have submounts for the case of
331 * a multi-mount with no mount at it's base) we can
332 * continue on and create a new request.
333 */
Ian Kent9e3fea12011-01-14 18:46:30 +0000334 if (!IS_ROOT(dentry)) {
Ian Kente9a7c2f2016-03-15 14:58:25 -0700335 if (d_unhashed(dentry) &&
336 d_really_is_positive(dentry)) {
Ian Kent9e3fea12011-01-14 18:46:30 +0000337 struct dentry *parent = dentry->d_parent;
Ian Kente9a7c2f2016-03-15 14:58:25 -0700338
Ian Kent9e3fea12011-01-14 18:46:30 +0000339 new = d_lookup(parent, &dentry->d_name);
340 if (new)
341 dentry = new;
342 }
343 }
Ian Kent60359742016-11-24 08:03:42 +1100344 this.mnt = path->mnt;
345 this.dentry = dentry;
346 if (path_has_submounts(&this))
Ian Kent9e3fea12011-01-14 18:46:30 +0000347 valid = 0;
348
349 if (new)
350 dput(new);
351 return valid;
Ian Kenta1362fe2008-07-23 21:30:19 -0700352 }
353
354 return 1;
355}
356
Ian Kente9a7c2f2016-03-15 14:58:25 -0700357int autofs4_wait(struct autofs_sb_info *sbi,
Ian Kentdd36a882016-11-24 08:03:42 +1100358 const struct path *path, enum autofs_notify notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Ian Kentdd36a882016-11-24 08:03:42 +1100360 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct autofs_wait_queue *wq;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700362 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 char *name;
Ian Kenta1362fe2008-07-23 21:30:19 -0700364 int status, ret, type;
Miklos Szeredifbff0872014-01-23 15:54:58 -0800365 pid_t pid;
366 pid_t tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800369 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -ENOENT;
Ian Kenta1362fe2008-07-23 21:30:19 -0700371
Miklos Szeredifbff0872014-01-23 15:54:58 -0800372 /*
373 * Try translating pids to the namespace of the daemon.
374 *
375 * Zero means failure: we are in an unrelated pid namespace.
376 */
377 pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
378 tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
379 if (pid == 0 || tgid == 0)
380 return -ENOENT;
381
David Howells2b0143b2015-03-17 22:25:59 +0000382 if (d_really_is_negative(dentry)) {
Ian Kentc72305b2008-07-23 21:30:23 -0700383 /*
384 * A wait for a negative dentry is invalid for certain
385 * cases. A direct or offset mount "always" has its mount
386 * point directory created and so the request dentry must
387 * be positive or the map key doesn't exist. The situation
388 * is very similar for indirect mounts except only dentrys
389 * in the root of the autofs file system may be negative.
390 */
Ian Kenta92daf62009-01-06 14:42:08 -0800391 if (autofs_type_trigger(sbi->type))
Ian Kentc72305b2008-07-23 21:30:23 -0700392 return -ENOENT;
393 else if (!IS_ROOT(dentry->d_parent))
394 return -ENOENT;
395 }
Ian Kenteb3b1762008-07-23 21:30:22 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
398 if (!name)
399 return -ENOMEM;
400
Ian Kent5c0a32f2006-03-27 01:14:55 -0800401 /* If this is a direct mount request create a dummy name */
Ian Kenta92daf62009-01-06 14:42:08 -0800402 if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
Jeff Moyer70b52a02008-07-23 21:30:16 -0700403 qstr.len = sprintf(name, "%p", dentry);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800404 else {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700405 qstr.len = autofs4_getpath(sbi, dentry, &name);
406 if (!qstr.len) {
Ian Kent5c0a32f2006-03-27 01:14:55 -0800407 kfree(name);
408 return -ENOENT;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700411 qstr.name = name;
Linus Torvalds8387ff22016-06-10 07:51:30 -0700412 qstr.hash = full_name_hash(dentry, name, qstr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Ian Kentf4c7da02008-07-23 21:30:19 -0700414 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
415 kfree(qstr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EINTR;
Ian Kentf4c7da02008-07-23 21:30:19 -0700417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Ian Kent60359742016-11-24 08:03:42 +1100419 ret = validate_request(&wq, sbi, &qstr, path, notify);
Ian Kenta1362fe2008-07-23 21:30:19 -0700420 if (ret <= 0) {
Al Viro4041bcd2012-01-10 22:20:12 -0500421 if (ret != -EINTR)
Ian Kenta5370552006-05-15 09:43:51 -0700422 mutex_unlock(&sbi->wq_mutex);
Ian Kenta1362fe2008-07-23 21:30:19 -0700423 kfree(qstr.name);
424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Ian Kente77fbdd2006-03-27 01:14:49 -0800427 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Create a new wait queue */
Ian Kente9a7c2f2016-03-15 14:58:25 -0700429 wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800430 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700431 kfree(qstr.name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800432 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -ENOMEM;
434 }
435
Linus Torvalds7cee9382017-07-10 11:40:19 -0700436 wq->wait_queue_token = autofs4_next_wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (++autofs4_next_wait_queue == 0)
438 autofs4_next_wait_queue = 1;
439 wq->next = sbi->queues;
440 sbi->queues = wq;
441 init_waitqueue_head(&wq->queue);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700442 memcpy(&wq->name, &qstr, sizeof(struct qstr));
Ian Kent5c0a32f2006-03-27 01:14:55 -0800443 wq->dev = autofs4_get_dev(sbi);
444 wq->ino = autofs4_get_ino(sbi);
Eric W. Biederman116ceac2017-11-29 17:49:45 -0600445 wq->uid = current_uid();
446 wq->gid = current_gid();
Miklos Szeredifbff0872014-01-23 15:54:58 -0800447 wq->pid = pid;
448 wq->tgid = tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 wq->status = -EINTR; /* Status return if interrupted */
Ian Kent296f7bf2008-07-23 21:30:21 -0700450 wq->wait_ctr = 2;
Ian Kent3e7b1912006-03-27 01:14:59 -0800451
Ian Kent5c0a32f2006-03-27 01:14:55 -0800452 if (sbi->version < 5) {
453 if (notify == NFY_MOUNT)
454 type = autofs_ptype_missing;
455 else
456 type = autofs_ptype_expire_multi;
457 } else {
458 if (notify == NFY_MOUNT)
Ian Kenta92daf62009-01-06 14:42:08 -0800459 type = autofs_type_trigger(sbi->type) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800460 autofs_ptype_missing_direct :
461 autofs_ptype_missing_indirect;
462 else
Ian Kenta92daf62009-01-06 14:42:08 -0800463 type = autofs_type_trigger(sbi->type) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800464 autofs_ptype_expire_direct :
465 autofs_ptype_expire_indirect;
466 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700467
Ian Kent8a78d592016-03-15 14:58:45 -0700468 pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
Linus Torvalds7cee9382017-07-10 11:40:19 -0700469 (unsigned long) wq->wait_queue_token, wq->name.len,
Ian Kent8a78d592016-03-15 14:58:45 -0700470 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700471
Ian Kente9a7c2f2016-03-15 14:58:25 -0700472 /*
473 * autofs4_notify_daemon() may block; it will unlock ->wq_mutex
474 */
Ian Kent4dcd00b2005-05-01 08:59:16 -0700475 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700476 } else {
Ian Kent296f7bf2008-07-23 21:30:21 -0700477 wq->wait_ctr++;
Ian Kent8a78d592016-03-15 14:58:45 -0700478 pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
Linus Torvalds7cee9382017-07-10 11:40:19 -0700479 (unsigned long) wq->wait_queue_token, wq->name.len,
Ian Kent8a78d592016-03-15 14:58:45 -0700480 wq->name.name, notify);
Al Viro606035e2013-09-14 17:32:12 -0400481 mutex_unlock(&sbi->wq_mutex);
482 kfree(qstr.name);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700483 }
484
Ian Kent5a11d4d2008-07-23 21:30:17 -0700485 /*
486 * wq->name.name is NULL iff the lock is already released
487 * or the mount has been made catatonic.
488 */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700489 if (wq->name.name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Block all but "shutdown" signals while waiting */
Ian Kentb3f67a92016-03-15 14:58:30 -0700491 unsigned long shutdown_sigs_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unsigned long irqflags;
Ian Kentb3f67a92016-03-15 14:58:30 -0700493 sigset_t oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 spin_lock_irqsave(&current->sighand->siglock, irqflags);
496 oldset = current->blocked;
Ian Kentb3f67a92016-03-15 14:58:30 -0700497 shutdown_sigs_mask = SHUTDOWN_SIGS & ~oldset.sig[0];
498 siginitsetinv(&current->blocked, shutdown_sigs_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 recalc_sigpending();
500 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
501
Jeff Moyer70b52a02008-07-23 21:30:16 -0700502 wait_event_interruptible(wq->queue, wq->name.name == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 spin_lock_irqsave(&current->sighand->siglock, irqflags);
505 current->blocked = oldset;
506 recalc_sigpending();
507 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
508 } else {
Ian Kent8a78d592016-03-15 14:58:45 -0700509 pr_debug("skipped sleeping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 status = wq->status;
513
Ian Kentc0f54d32008-10-15 22:02:52 -0700514 /*
515 * For direct and offset mounts we need to track the requester's
516 * uid and gid in the dentry info struct. This is so it can be
517 * supplied, on request, by the misc device ioctl interface.
518 * This is needed during daemon resatart when reconnecting
519 * to existing, active, autofs mounts. The uid and gid (and
520 * related string values) may be used for macro substitution
521 * in autofs mount maps.
522 */
523 if (!status) {
524 struct autofs_info *ino;
525 struct dentry *de = NULL;
526
527 /* direct mount or browsable map */
528 ino = autofs4_dentry_ino(dentry);
529 if (!ino) {
530 /* If not lookup actual dentry used */
531 de = d_lookup(dentry->d_parent, &dentry->d_name);
532 if (de)
533 ino = autofs4_dentry_ino(de);
534 }
535
536 /* Set mount requester */
537 if (ino) {
538 spin_lock(&sbi->fs_lock);
539 ino->uid = wq->uid;
540 ino->gid = wq->gid;
541 spin_unlock(&sbi->fs_lock);
542 }
543
544 if (de)
545 dput(de);
546 }
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Are we the last process to need status? */
Ian Kent296f7bf2008-07-23 21:30:21 -0700549 mutex_lock(&sbi->wq_mutex);
550 if (!--wq->wait_ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 kfree(wq);
Ian Kent296f7bf2008-07-23 21:30:21 -0700552 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return status;
555}
556
557
Linus Torvalds7cee9382017-07-10 11:40:19 -0700558int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct autofs_wait_queue *wq, **wql;
561
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800562 mutex_lock(&sbi->wq_mutex);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700563 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
Linus Torvalds7cee9382017-07-10 11:40:19 -0700564 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 break;
566 }
567
Ian Kente77fbdd2006-03-27 01:14:49 -0800568 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800569 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return -EINVAL;
571 }
572
573 *wql = wq->next; /* Unlink from chain */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700574 kfree(wq->name.name);
575 wq->name.name = NULL; /* Do not wait on this queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 wq->status = status;
Ian Kent296f7bf2008-07-23 21:30:21 -0700577 wake_up_interruptible(&wq->queue);
578 if (!--wq->wait_ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 kfree(wq);
Ian Kent296f7bf2008-07-23 21:30:21 -0700580 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return 0;
583}