1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
4 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
7 #include <linux/sched/signal.h>
10 /* We make this a static variable rather than a part of the superblock; it
11 * is better if we don't reassign numbers easily even across filesystems
13 static autofs_wqt_t autofs_next_wait_queue = 1;
15 void autofs_catatonic_mode(struct autofs_sb_info *sbi)
17 struct autofs_wait_queue *wq, *nwq;
19 mutex_lock(&sbi->wq_mutex);
20 if (sbi->flags & AUTOFS_SBI_CATATONIC) {
21 mutex_unlock(&sbi->wq_mutex);
25 pr_debug("entering catatonic mode\n");
27 sbi->flags |= AUTOFS_SBI_CATATONIC;
29 sbi->queues = NULL; /* Erase all wait queues */
32 wq->status = -ENOENT; /* Magic is gone - report failure */
33 kfree(wq->name.name - wq->offset);
40 fput(sbi->pipe); /* Close the pipe */
43 mutex_unlock(&sbi->wq_mutex);
46 static int autofs_write(struct autofs_sb_info *sbi,
47 struct file *file, const void *addr, int bytes)
49 unsigned long sigpipe, flags;
50 const char *data = (const char *)addr;
53 sigpipe = sigismember(¤t->pending.signal, SIGPIPE);
55 mutex_lock(&sbi->pipe_mutex);
57 wr = __kernel_write(file, data, bytes, NULL);
63 mutex_unlock(&sbi->pipe_mutex);
65 /* Keep the currently executing process from receiving a
66 * SIGPIPE unless it was already supposed to get one
68 if (wr == -EPIPE && !sigpipe) {
69 spin_lock_irqsave(¤t->sighand->siglock, flags);
70 sigdelset(¤t->pending.signal, SIGPIPE);
72 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
75 /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
76 return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
79 static void autofs_notify_daemon(struct autofs_sb_info *sbi,
80 struct autofs_wait_queue *wq,
84 struct autofs_packet_hdr hdr;
85 union autofs_packet_union v4_pkt;
86 union autofs_v5_packet_union v5_pkt;
88 struct file *pipe = NULL;
92 pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
93 (unsigned long) wq->wait_queue_token,
94 wq->name.len, wq->name.name, type);
96 memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
98 pkt.hdr.proto_version = sbi->version;
102 /* Kernel protocol v4 missing and expire packets */
103 case autofs_ptype_missing:
105 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
109 mp->wait_queue_token = wq->wait_queue_token;
110 mp->len = wq->name.len;
111 memcpy(mp->name, wq->name.name, wq->name.len);
112 mp->name[wq->name.len] = '\0';
115 case autofs_ptype_expire_multi:
117 struct autofs_packet_expire_multi *ep =
118 &pkt.v4_pkt.expire_multi;
122 ep->wait_queue_token = wq->wait_queue_token;
123 ep->len = wq->name.len;
124 memcpy(ep->name, wq->name.name, wq->name.len);
125 ep->name[wq->name.len] = '\0';
129 * Kernel protocol v5 packet for handling indirect and direct
130 * mount missing and expire requests
132 case autofs_ptype_missing_indirect:
133 case autofs_ptype_expire_indirect:
134 case autofs_ptype_missing_direct:
135 case autofs_ptype_expire_direct:
137 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
138 struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
140 pktsz = sizeof(*packet);
142 packet->wait_queue_token = wq->wait_queue_token;
143 packet->len = wq->name.len;
144 memcpy(packet->name, wq->name.name, wq->name.len);
145 packet->name[wq->name.len] = '\0';
146 packet->dev = wq->dev;
147 packet->ino = wq->ino;
148 packet->uid = from_kuid_munged(user_ns, wq->uid);
149 packet->gid = from_kgid_munged(user_ns, wq->gid);
150 packet->pid = wq->pid;
151 packet->tgid = wq->tgid;
155 pr_warn("bad type %d!\n", type);
156 mutex_unlock(&sbi->wq_mutex);
160 pipe = get_file(sbi->pipe);
162 mutex_unlock(&sbi->wq_mutex);
164 switch (ret = autofs_write(sbi, pipe, &pkt, pktsz)) {
169 /* Just fail this one */
170 autofs_wait_release(sbi, wq->wait_queue_token, ret);
173 autofs_catatonic_mode(sbi);
179 static struct autofs_wait_queue *
180 autofs_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
182 struct autofs_wait_queue *wq;
184 for (wq = sbi->queues; wq; wq = wq->next) {
185 if (wq->name.hash == qstr->hash &&
186 wq->name.len == qstr->len &&
188 !memcmp(wq->name.name, qstr->name, qstr->len))
195 * Check if we have a valid request.
197 * 1 if the request should continue.
198 * In this case we can return an autofs_wait_queue entry if one is
199 * found or NULL to idicate a new wait needs to be created.
200 * 0 or a negative errno if the request shouldn't continue.
202 static int validate_request(struct autofs_wait_queue **wait,
203 struct autofs_sb_info *sbi,
204 const struct qstr *qstr,
205 const struct path *path, enum autofs_notify notify)
207 struct dentry *dentry = path->dentry;
208 struct autofs_wait_queue *wq;
209 struct autofs_info *ino;
211 if (sbi->flags & AUTOFS_SBI_CATATONIC)
214 /* Wait in progress, continue; */
215 wq = autofs_find_wait(sbi, qstr);
223 /* If we don't yet have any info this is a new request */
224 ino = autofs_dentry_ino(dentry);
229 * If we've been asked to wait on an existing expire (NFY_NONE)
230 * but there is no wait in the queue ...
232 if (notify == NFY_NONE) {
234 * Either we've betean the pending expire to post it's
235 * wait or it finished while we waited on the mutex.
236 * So we need to wait till either, the wait appears
237 * or the expire finishes.
240 while (ino->flags & AUTOFS_INF_EXPIRING) {
241 mutex_unlock(&sbi->wq_mutex);
242 schedule_timeout_interruptible(HZ/10);
243 if (mutex_lock_interruptible(&sbi->wq_mutex))
246 if (sbi->flags & AUTOFS_SBI_CATATONIC)
249 wq = autofs_find_wait(sbi, qstr);
257 * Not ideal but the status has already gone. Of the two
258 * cases where we wait on NFY_NONE neither depend on the
259 * return status of the wait.
265 * If we've been asked to trigger a mount and the request
266 * completed while we waited on the mutex ...
268 if (notify == NFY_MOUNT) {
269 struct dentry *new = NULL;
274 * If the dentry was successfully mounted while we slept
275 * on the wait queue mutex we can return success. If it
276 * isn't mounted (doesn't have submounts for the case of
277 * a multi-mount with no mount at it's base) we can
278 * continue on and create a new request.
280 if (!IS_ROOT(dentry)) {
281 if (d_unhashed(dentry) &&
282 d_really_is_positive(dentry)) {
283 struct dentry *parent = dentry->d_parent;
285 new = d_lookup(parent, &dentry->d_name);
290 this.mnt = path->mnt;
291 this.dentry = dentry;
292 if (path_has_submounts(&this))
303 int autofs_wait(struct autofs_sb_info *sbi,
304 const struct path *path, enum autofs_notify notify)
306 struct dentry *dentry = path->dentry;
307 struct autofs_wait_queue *wq;
310 int status, ret, type;
311 unsigned int offset = 0;
315 /* In catatonic mode, we don't wait for nobody */
316 if (sbi->flags & AUTOFS_SBI_CATATONIC)
320 * Try translating pids to the namespace of the daemon.
322 * Zero means failure: we are in an unrelated pid namespace.
324 pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
325 tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
326 if (pid == 0 || tgid == 0)
329 if (d_really_is_negative(dentry)) {
331 * A wait for a negative dentry is invalid for certain
332 * cases. A direct or offset mount "always" has its mount
333 * point directory created and so the request dentry must
334 * be positive or the map key doesn't exist. The situation
335 * is very similar for indirect mounts except only dentrys
336 * in the root of the autofs file system may be negative.
338 if (autofs_type_trigger(sbi->type))
340 else if (!IS_ROOT(dentry->d_parent))
344 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
348 /* If this is a direct mount request create a dummy name */
349 if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type)) {
351 qstr.len = sprintf(name, "%p", dentry);
353 char *p = dentry_path_raw(dentry, name, NAME_MAX);
358 qstr.name = ++p; // skip the leading slash
359 qstr.len = strlen(p);
362 qstr.hash = full_name_hash(dentry, qstr.name, qstr.len);
364 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
369 ret = validate_request(&wq, sbi, &qstr, path, notify);
372 mutex_unlock(&sbi->wq_mutex);
378 /* Create a new wait queue */
379 wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
382 mutex_unlock(&sbi->wq_mutex);
386 wq->wait_queue_token = autofs_next_wait_queue;
387 if (++autofs_next_wait_queue == 0)
388 autofs_next_wait_queue = 1;
389 wq->next = sbi->queues;
391 init_waitqueue_head(&wq->queue);
392 memcpy(&wq->name, &qstr, sizeof(struct qstr));
394 wq->dev = autofs_get_dev(sbi);
395 wq->ino = autofs_get_ino(sbi);
396 wq->uid = current_uid();
397 wq->gid = current_gid();
400 wq->status = -EINTR; /* Status return if interrupted */
403 if (sbi->version < 5) {
404 if (notify == NFY_MOUNT)
405 type = autofs_ptype_missing;
407 type = autofs_ptype_expire_multi;
409 if (notify == NFY_MOUNT)
410 type = autofs_type_trigger(sbi->type) ?
411 autofs_ptype_missing_direct :
412 autofs_ptype_missing_indirect;
414 type = autofs_type_trigger(sbi->type) ?
415 autofs_ptype_expire_direct :
416 autofs_ptype_expire_indirect;
419 pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
420 (unsigned long) wq->wait_queue_token, wq->name.len,
421 wq->name.name, notify);
424 * autofs_notify_daemon() may block; it will unlock ->wq_mutex
426 autofs_notify_daemon(sbi, wq, type);
429 pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
430 (unsigned long) wq->wait_queue_token, wq->name.len,
431 wq->name.name, notify);
432 mutex_unlock(&sbi->wq_mutex);
437 * wq->name.name is NULL iff the lock is already released
438 * or the mount has been made catatonic.
440 wait_event_killable(wq->queue, wq->name.name == NULL);
444 * For direct and offset mounts we need to track the requester's
445 * uid and gid in the dentry info struct. This is so it can be
446 * supplied, on request, by the misc device ioctl interface.
447 * This is needed during daemon resatart when reconnecting
448 * to existing, active, autofs mounts. The uid and gid (and
449 * related string values) may be used for macro substitution
450 * in autofs mount maps.
453 struct autofs_info *ino;
454 struct dentry *de = NULL;
456 /* direct mount or browsable map */
457 ino = autofs_dentry_ino(dentry);
459 /* If not lookup actual dentry used */
460 de = d_lookup(dentry->d_parent, &dentry->d_name);
462 ino = autofs_dentry_ino(de);
465 /* Set mount requester */
467 spin_lock(&sbi->fs_lock);
470 spin_unlock(&sbi->fs_lock);
477 /* Are we the last process to need status? */
478 mutex_lock(&sbi->wq_mutex);
481 mutex_unlock(&sbi->wq_mutex);
487 int autofs_wait_release(struct autofs_sb_info *sbi,
488 autofs_wqt_t wait_queue_token, int status)
490 struct autofs_wait_queue *wq, **wql;
492 mutex_lock(&sbi->wq_mutex);
493 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
494 if (wq->wait_queue_token == wait_queue_token)
499 mutex_unlock(&sbi->wq_mutex);
503 *wql = wq->next; /* Unlink from chain */
504 kfree(wq->name.name - wq->offset);
505 wq->name.name = NULL; /* Do not wait on this queue */
510 mutex_unlock(&sbi->wq_mutex);