xref: /linux/fs/autofs/waitq.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
1d6910058SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ebc921caSIan Kent /*
3ebc921caSIan Kent  * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
4ebc921caSIan Kent  * Copyright 2001-2006 Ian Kent <raven@themaw.net>
5ebc921caSIan Kent  */
6ebc921caSIan Kent 
7ebc921caSIan Kent #include <linux/sched/signal.h>
8ebc921caSIan Kent #include "autofs_i.h"
9ebc921caSIan Kent 
10ebc921caSIan Kent /* We make this a static variable rather than a part of the superblock; it
11ebc921caSIan Kent  * is better if we don't reassign numbers easily even across filesystems
12ebc921caSIan Kent  */
13ebc921caSIan Kent static autofs_wqt_t autofs_next_wait_queue = 1;
14ebc921caSIan Kent 
autofs_catatonic_mode(struct autofs_sb_info * sbi)15ebc921caSIan Kent void autofs_catatonic_mode(struct autofs_sb_info *sbi)
16ebc921caSIan Kent {
17ebc921caSIan Kent 	struct autofs_wait_queue *wq, *nwq;
18ebc921caSIan Kent 
19ebc921caSIan Kent 	mutex_lock(&sbi->wq_mutex);
209d8719a4SIan Kent 	if (sbi->flags & AUTOFS_SBI_CATATONIC) {
21ebc921caSIan Kent 		mutex_unlock(&sbi->wq_mutex);
22ebc921caSIan Kent 		return;
23ebc921caSIan Kent 	}
24ebc921caSIan Kent 
25ebc921caSIan Kent 	pr_debug("entering catatonic mode\n");
26ebc921caSIan Kent 
279d8719a4SIan Kent 	sbi->flags |= AUTOFS_SBI_CATATONIC;
28ebc921caSIan Kent 	wq = sbi->queues;
29ebc921caSIan Kent 	sbi->queues = NULL;	/* Erase all wait queues */
30ebc921caSIan Kent 	while (wq) {
31ebc921caSIan Kent 		nwq = wq->next;
32ebc921caSIan Kent 		wq->status = -ENOENT; /* Magic is gone - report failure */
332be7828cSAl Viro 		kfree(wq->name.name - wq->offset);
34ebc921caSIan Kent 		wq->name.name = NULL;
35*17fce12eSIan Kent 		wake_up(&wq->queue);
36ccbe77f7SFedor Pchelkin 		if (!--wq->wait_ctr)
37ccbe77f7SFedor Pchelkin 			kfree(wq);
38ebc921caSIan Kent 		wq = nwq;
39ebc921caSIan Kent 	}
40ebc921caSIan Kent 	fput(sbi->pipe);	/* Close the pipe */
41ebc921caSIan Kent 	sbi->pipe = NULL;
42ebc921caSIan Kent 	sbi->pipefd = -1;
43ebc921caSIan Kent 	mutex_unlock(&sbi->wq_mutex);
44ebc921caSIan Kent }
45ebc921caSIan Kent 
autofs_write(struct autofs_sb_info * sbi,struct file * file,const void * addr,int bytes)46ebc921caSIan Kent static int autofs_write(struct autofs_sb_info *sbi,
47ebc921caSIan Kent 			struct file *file, const void *addr, int bytes)
48ebc921caSIan Kent {
49ebc921caSIan Kent 	unsigned long sigpipe, flags;
50ebc921caSIan Kent 	const char *data = (const char *)addr;
51ebc921caSIan Kent 	ssize_t wr = 0;
52ebc921caSIan Kent 
53ebc921caSIan Kent 	sigpipe = sigismember(&current->pending.signal, SIGPIPE);
54ebc921caSIan Kent 
55ebc921caSIan Kent 	mutex_lock(&sbi->pipe_mutex);
56ebc921caSIan Kent 	while (bytes) {
5790fb7027SLinus Torvalds 		wr = __kernel_write(file, data, bytes, NULL);
58ebc921caSIan Kent 		if (wr <= 0)
59ebc921caSIan Kent 			break;
60ebc921caSIan Kent 		data += wr;
61ebc921caSIan Kent 		bytes -= wr;
62ebc921caSIan Kent 	}
63ebc921caSIan Kent 	mutex_unlock(&sbi->pipe_mutex);
64ebc921caSIan Kent 
65ebc921caSIan Kent 	/* Keep the currently executing process from receiving a
66ebc921caSIan Kent 	 * SIGPIPE unless it was already supposed to get one
67ebc921caSIan Kent 	 */
68ebc921caSIan Kent 	if (wr == -EPIPE && !sigpipe) {
69ebc921caSIan Kent 		spin_lock_irqsave(&current->sighand->siglock, flags);
70ebc921caSIan Kent 		sigdelset(&current->pending.signal, SIGPIPE);
71ebc921caSIan Kent 		recalc_sigpending();
72ebc921caSIan Kent 		spin_unlock_irqrestore(&current->sighand->siglock, flags);
73ebc921caSIan Kent 	}
74ebc921caSIan Kent 
75ebc921caSIan Kent 	/* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
76ebc921caSIan Kent 	return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
77ebc921caSIan Kent }
78ebc921caSIan Kent 
autofs_notify_daemon(struct autofs_sb_info * sbi,struct autofs_wait_queue * wq,int type)79ebc921caSIan Kent static void autofs_notify_daemon(struct autofs_sb_info *sbi,
80ebc921caSIan Kent 				 struct autofs_wait_queue *wq,
81ebc921caSIan Kent 				 int type)
82ebc921caSIan Kent {
83ebc921caSIan Kent 	union {
84ebc921caSIan Kent 		struct autofs_packet_hdr hdr;
85ebc921caSIan Kent 		union autofs_packet_union v4_pkt;
86ebc921caSIan Kent 		union autofs_v5_packet_union v5_pkt;
87ebc921caSIan Kent 	} pkt;
88ebc921caSIan Kent 	struct file *pipe = NULL;
89ebc921caSIan Kent 	size_t pktsz;
90ebc921caSIan Kent 	int ret;
91ebc921caSIan Kent 
92ebc921caSIan Kent 	pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
93ebc921caSIan Kent 		 (unsigned long) wq->wait_queue_token,
94ebc921caSIan Kent 		 wq->name.len, wq->name.name, type);
95ebc921caSIan Kent 
96ebc921caSIan Kent 	memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
97ebc921caSIan Kent 
98ebc921caSIan Kent 	pkt.hdr.proto_version = sbi->version;
99ebc921caSIan Kent 	pkt.hdr.type = type;
100ebc921caSIan Kent 
101ebc921caSIan Kent 	switch (type) {
102ebc921caSIan Kent 	/* Kernel protocol v4 missing and expire packets */
103ebc921caSIan Kent 	case autofs_ptype_missing:
104ebc921caSIan Kent 	{
105ebc921caSIan Kent 		struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
106ebc921caSIan Kent 
107ebc921caSIan Kent 		pktsz = sizeof(*mp);
108ebc921caSIan Kent 
109ebc921caSIan Kent 		mp->wait_queue_token = wq->wait_queue_token;
110ebc921caSIan Kent 		mp->len = wq->name.len;
111ebc921caSIan Kent 		memcpy(mp->name, wq->name.name, wq->name.len);
112ebc921caSIan Kent 		mp->name[wq->name.len] = '\0';
113ebc921caSIan Kent 		break;
114ebc921caSIan Kent 	}
115ebc921caSIan Kent 	case autofs_ptype_expire_multi:
116ebc921caSIan Kent 	{
117ebc921caSIan Kent 		struct autofs_packet_expire_multi *ep =
118ebc921caSIan Kent 					&pkt.v4_pkt.expire_multi;
119ebc921caSIan Kent 
120ebc921caSIan Kent 		pktsz = sizeof(*ep);
121ebc921caSIan Kent 
122ebc921caSIan Kent 		ep->wait_queue_token = wq->wait_queue_token;
123ebc921caSIan Kent 		ep->len = wq->name.len;
124ebc921caSIan Kent 		memcpy(ep->name, wq->name.name, wq->name.len);
125ebc921caSIan Kent 		ep->name[wq->name.len] = '\0';
126ebc921caSIan Kent 		break;
127ebc921caSIan Kent 	}
128ebc921caSIan Kent 	/*
129ebc921caSIan Kent 	 * Kernel protocol v5 packet for handling indirect and direct
130ebc921caSIan Kent 	 * mount missing and expire requests
131ebc921caSIan Kent 	 */
132ebc921caSIan Kent 	case autofs_ptype_missing_indirect:
133ebc921caSIan Kent 	case autofs_ptype_expire_indirect:
134ebc921caSIan Kent 	case autofs_ptype_missing_direct:
135ebc921caSIan Kent 	case autofs_ptype_expire_direct:
136ebc921caSIan Kent 	{
137ebc921caSIan Kent 		struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
138ebc921caSIan Kent 		struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
139ebc921caSIan Kent 
140ebc921caSIan Kent 		pktsz = sizeof(*packet);
141ebc921caSIan Kent 
142ebc921caSIan Kent 		packet->wait_queue_token = wq->wait_queue_token;
143ebc921caSIan Kent 		packet->len = wq->name.len;
144ebc921caSIan Kent 		memcpy(packet->name, wq->name.name, wq->name.len);
145ebc921caSIan Kent 		packet->name[wq->name.len] = '\0';
146ebc921caSIan Kent 		packet->dev = wq->dev;
147ebc921caSIan Kent 		packet->ino = wq->ino;
148ebc921caSIan Kent 		packet->uid = from_kuid_munged(user_ns, wq->uid);
149ebc921caSIan Kent 		packet->gid = from_kgid_munged(user_ns, wq->gid);
150ebc921caSIan Kent 		packet->pid = wq->pid;
151ebc921caSIan Kent 		packet->tgid = wq->tgid;
152ebc921caSIan Kent 		break;
153ebc921caSIan Kent 	}
154ebc921caSIan Kent 	default:
155ebc921caSIan Kent 		pr_warn("bad type %d!\n", type);
156ebc921caSIan Kent 		mutex_unlock(&sbi->wq_mutex);
157ebc921caSIan Kent 		return;
158ebc921caSIan Kent 	}
159ebc921caSIan Kent 
160ebc921caSIan Kent 	pipe = get_file(sbi->pipe);
161ebc921caSIan Kent 
162ebc921caSIan Kent 	mutex_unlock(&sbi->wq_mutex);
163ebc921caSIan Kent 
164ebc921caSIan Kent 	switch (ret = autofs_write(sbi, pipe, &pkt, pktsz)) {
165ebc921caSIan Kent 	case 0:
166ebc921caSIan Kent 		break;
167ebc921caSIan Kent 	case -ENOMEM:
168ebc921caSIan Kent 	case -ERESTARTSYS:
169ebc921caSIan Kent 		/* Just fail this one */
170ebc921caSIan Kent 		autofs_wait_release(sbi, wq->wait_queue_token, ret);
171ebc921caSIan Kent 		break;
172ebc921caSIan Kent 	default:
173ebc921caSIan Kent 		autofs_catatonic_mode(sbi);
174ebc921caSIan Kent 		break;
175ebc921caSIan Kent 	}
176ebc921caSIan Kent 	fput(pipe);
177ebc921caSIan Kent }
178ebc921caSIan Kent 
179ebc921caSIan Kent static struct autofs_wait_queue *
autofs_find_wait(struct autofs_sb_info * sbi,const struct qstr * qstr)180ebc921caSIan Kent autofs_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
181ebc921caSIan Kent {
182ebc921caSIan Kent 	struct autofs_wait_queue *wq;
183ebc921caSIan Kent 
184ebc921caSIan Kent 	for (wq = sbi->queues; wq; wq = wq->next) {
185ebc921caSIan Kent 		if (wq->name.hash == qstr->hash &&
186ebc921caSIan Kent 		    wq->name.len == qstr->len &&
187ebc921caSIan Kent 		    wq->name.name &&
188ebc921caSIan Kent 		    !memcmp(wq->name.name, qstr->name, qstr->len))
189ebc921caSIan Kent 			break;
190ebc921caSIan Kent 	}
191ebc921caSIan Kent 	return wq;
192ebc921caSIan Kent }
193ebc921caSIan Kent 
194ebc921caSIan Kent /*
195ebc921caSIan Kent  * Check if we have a valid request.
196ebc921caSIan Kent  * Returns
197ebc921caSIan Kent  * 1 if the request should continue.
198ebc921caSIan Kent  *   In this case we can return an autofs_wait_queue entry if one is
199ebc921caSIan Kent  *   found or NULL to idicate a new wait needs to be created.
200ebc921caSIan Kent  * 0 or a negative errno if the request shouldn't continue.
201ebc921caSIan Kent  */
validate_request(struct autofs_wait_queue ** wait,struct autofs_sb_info * sbi,const struct qstr * qstr,const struct path * path,enum autofs_notify notify)202ebc921caSIan Kent static int validate_request(struct autofs_wait_queue **wait,
203ebc921caSIan Kent 			    struct autofs_sb_info *sbi,
204ebc921caSIan Kent 			    const struct qstr *qstr,
205ebc921caSIan Kent 			    const struct path *path, enum autofs_notify notify)
206ebc921caSIan Kent {
207ebc921caSIan Kent 	struct dentry *dentry = path->dentry;
208ebc921caSIan Kent 	struct autofs_wait_queue *wq;
209ebc921caSIan Kent 	struct autofs_info *ino;
210ebc921caSIan Kent 
2119d8719a4SIan Kent 	if (sbi->flags & AUTOFS_SBI_CATATONIC)
212ebc921caSIan Kent 		return -ENOENT;
213ebc921caSIan Kent 
214ebc921caSIan Kent 	/* Wait in progress, continue; */
215ebc921caSIan Kent 	wq = autofs_find_wait(sbi, qstr);
216ebc921caSIan Kent 	if (wq) {
217ebc921caSIan Kent 		*wait = wq;
218ebc921caSIan Kent 		return 1;
219ebc921caSIan Kent 	}
220ebc921caSIan Kent 
221ebc921caSIan Kent 	*wait = NULL;
222ebc921caSIan Kent 
223ebc921caSIan Kent 	/* If we don't yet have any info this is a new request */
224ebc921caSIan Kent 	ino = autofs_dentry_ino(dentry);
225ebc921caSIan Kent 	if (!ino)
226ebc921caSIan Kent 		return 1;
227ebc921caSIan Kent 
228ebc921caSIan Kent 	/*
229ebc921caSIan Kent 	 * If we've been asked to wait on an existing expire (NFY_NONE)
230ebc921caSIan Kent 	 * but there is no wait in the queue ...
231ebc921caSIan Kent 	 */
232ebc921caSIan Kent 	if (notify == NFY_NONE) {
233ebc921caSIan Kent 		/*
234ebc921caSIan Kent 		 * Either we've betean the pending expire to post it's
235ebc921caSIan Kent 		 * wait or it finished while we waited on the mutex.
236ebc921caSIan Kent 		 * So we need to wait till either, the wait appears
237ebc921caSIan Kent 		 * or the expire finishes.
238ebc921caSIan Kent 		 */
239ebc921caSIan Kent 
240ebc921caSIan Kent 		while (ino->flags & AUTOFS_INF_EXPIRING) {
241ebc921caSIan Kent 			mutex_unlock(&sbi->wq_mutex);
242ebc921caSIan Kent 			schedule_timeout_interruptible(HZ/10);
243ebc921caSIan Kent 			if (mutex_lock_interruptible(&sbi->wq_mutex))
244ebc921caSIan Kent 				return -EINTR;
245ebc921caSIan Kent 
2469d8719a4SIan Kent 			if (sbi->flags & AUTOFS_SBI_CATATONIC)
247ebc921caSIan Kent 				return -ENOENT;
248ebc921caSIan Kent 
249ebc921caSIan Kent 			wq = autofs_find_wait(sbi, qstr);
250ebc921caSIan Kent 			if (wq) {
251ebc921caSIan Kent 				*wait = wq;
252ebc921caSIan Kent 				return 1;
253ebc921caSIan Kent 			}
254ebc921caSIan Kent 		}
255ebc921caSIan Kent 
256ebc921caSIan Kent 		/*
257ebc921caSIan Kent 		 * Not ideal but the status has already gone. Of the two
258ebc921caSIan Kent 		 * cases where we wait on NFY_NONE neither depend on the
259ebc921caSIan Kent 		 * return status of the wait.
260ebc921caSIan Kent 		 */
261ebc921caSIan Kent 		return 0;
262ebc921caSIan Kent 	}
263ebc921caSIan Kent 
264ebc921caSIan Kent 	/*
265ebc921caSIan Kent 	 * If we've been asked to trigger a mount and the request
266ebc921caSIan Kent 	 * completed while we waited on the mutex ...
267ebc921caSIan Kent 	 */
268ebc921caSIan Kent 	if (notify == NFY_MOUNT) {
269ebc921caSIan Kent 		struct dentry *new = NULL;
270ebc921caSIan Kent 		struct path this;
271ebc921caSIan Kent 		int valid = 1;
272ebc921caSIan Kent 
273ebc921caSIan Kent 		/*
274ebc921caSIan Kent 		 * If the dentry was successfully mounted while we slept
275ebc921caSIan Kent 		 * on the wait queue mutex we can return success. If it
276ebc921caSIan Kent 		 * isn't mounted (doesn't have submounts for the case of
277ebc921caSIan Kent 		 * a multi-mount with no mount at it's base) we can
278ebc921caSIan Kent 		 * continue on and create a new request.
279ebc921caSIan Kent 		 */
280ebc921caSIan Kent 		if (!IS_ROOT(dentry)) {
281ebc921caSIan Kent 			if (d_unhashed(dentry) &&
282ebc921caSIan Kent 			    d_really_is_positive(dentry)) {
283ebc921caSIan Kent 				struct dentry *parent = dentry->d_parent;
284ebc921caSIan Kent 
285ebc921caSIan Kent 				new = d_lookup(parent, &dentry->d_name);
286ebc921caSIan Kent 				if (new)
287ebc921caSIan Kent 					dentry = new;
288ebc921caSIan Kent 			}
289ebc921caSIan Kent 		}
290ebc921caSIan Kent 		this.mnt = path->mnt;
291ebc921caSIan Kent 		this.dentry = dentry;
292ebc921caSIan Kent 		if (path_has_submounts(&this))
293ebc921caSIan Kent 			valid = 0;
294ebc921caSIan Kent 
295ebc921caSIan Kent 		if (new)
296ebc921caSIan Kent 			dput(new);
297ebc921caSIan Kent 		return valid;
298ebc921caSIan Kent 	}
299ebc921caSIan Kent 
300ebc921caSIan Kent 	return 1;
301ebc921caSIan Kent }
302ebc921caSIan Kent 
autofs_wait(struct autofs_sb_info * sbi,const struct path * path,enum autofs_notify notify)303ebc921caSIan Kent int autofs_wait(struct autofs_sb_info *sbi,
304ebc921caSIan Kent 		 const struct path *path, enum autofs_notify notify)
305ebc921caSIan Kent {
306ebc921caSIan Kent 	struct dentry *dentry = path->dentry;
307ebc921caSIan Kent 	struct autofs_wait_queue *wq;
308ebc921caSIan Kent 	struct qstr qstr;
309ebc921caSIan Kent 	char *name;
310ebc921caSIan Kent 	int status, ret, type;
3112be7828cSAl Viro 	unsigned int offset = 0;
312ebc921caSIan Kent 	pid_t pid;
313ebc921caSIan Kent 	pid_t tgid;
314ebc921caSIan Kent 
315ebc921caSIan Kent 	/* In catatonic mode, we don't wait for nobody */
3169d8719a4SIan Kent 	if (sbi->flags & AUTOFS_SBI_CATATONIC)
317ebc921caSIan Kent 		return -ENOENT;
318ebc921caSIan Kent 
319ebc921caSIan Kent 	/*
320ebc921caSIan Kent 	 * Try translating pids to the namespace of the daemon.
321ebc921caSIan Kent 	 *
322ebc921caSIan Kent 	 * Zero means failure: we are in an unrelated pid namespace.
323ebc921caSIan Kent 	 */
324ebc921caSIan Kent 	pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
325ebc921caSIan Kent 	tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
326ebc921caSIan Kent 	if (pid == 0 || tgid == 0)
327ebc921caSIan Kent 		return -ENOENT;
328ebc921caSIan Kent 
329ebc921caSIan Kent 	if (d_really_is_negative(dentry)) {
330ebc921caSIan Kent 		/*
331ebc921caSIan Kent 		 * A wait for a negative dentry is invalid for certain
332ebc921caSIan Kent 		 * cases. A direct or offset mount "always" has its mount
333ebc921caSIan Kent 		 * point directory created and so the request dentry must
334ebc921caSIan Kent 		 * be positive or the map key doesn't exist. The situation
335ebc921caSIan Kent 		 * is very similar for indirect mounts except only dentrys
336ebc921caSIan Kent 		 * in the root of the autofs file system may be negative.
337ebc921caSIan Kent 		 */
338ebc921caSIan Kent 		if (autofs_type_trigger(sbi->type))
339ebc921caSIan Kent 			return -ENOENT;
340ebc921caSIan Kent 		else if (!IS_ROOT(dentry->d_parent))
341ebc921caSIan Kent 			return -ENOENT;
342ebc921caSIan Kent 	}
343ebc921caSIan Kent 
344ebc921caSIan Kent 	name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
345ebc921caSIan Kent 	if (!name)
346ebc921caSIan Kent 		return -ENOMEM;
347ebc921caSIan Kent 
348ebc921caSIan Kent 	/* If this is a direct mount request create a dummy name */
3492be7828cSAl Viro 	if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type)) {
3502be7828cSAl Viro 		qstr.name = name;
351ebc921caSIan Kent 		qstr.len = sprintf(name, "%p", dentry);
3522be7828cSAl Viro 	} else {
3532be7828cSAl Viro 		char *p = dentry_path_raw(dentry, name, NAME_MAX);
3542be7828cSAl Viro 		if (IS_ERR(p)) {
355ebc921caSIan Kent 			kfree(name);
356ebc921caSIan Kent 			return -ENOENT;
357ebc921caSIan Kent 		}
3582be7828cSAl Viro 		qstr.name = ++p; // skip the leading slash
3592be7828cSAl Viro 		qstr.len = strlen(p);
3602be7828cSAl Viro 		offset = p - name;
361ebc921caSIan Kent 	}
36225f54d08SIan Kent 	qstr.hash = full_name_hash(dentry, qstr.name, qstr.len);
363ebc921caSIan Kent 
364ebc921caSIan Kent 	if (mutex_lock_interruptible(&sbi->wq_mutex)) {
3652be7828cSAl Viro 		kfree(name);
366ebc921caSIan Kent 		return -EINTR;
367ebc921caSIan Kent 	}
368ebc921caSIan Kent 
369ebc921caSIan Kent 	ret = validate_request(&wq, sbi, &qstr, path, notify);
370ebc921caSIan Kent 	if (ret <= 0) {
371ebc921caSIan Kent 		if (ret != -EINTR)
372ebc921caSIan Kent 			mutex_unlock(&sbi->wq_mutex);
3732be7828cSAl Viro 		kfree(name);
374ebc921caSIan Kent 		return ret;
375ebc921caSIan Kent 	}
376ebc921caSIan Kent 
377ebc921caSIan Kent 	if (!wq) {
378ebc921caSIan Kent 		/* Create a new wait queue */
379ebc921caSIan Kent 		wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
380ebc921caSIan Kent 		if (!wq) {
3812be7828cSAl Viro 			kfree(name);
382ebc921caSIan Kent 			mutex_unlock(&sbi->wq_mutex);
383ebc921caSIan Kent 			return -ENOMEM;
384ebc921caSIan Kent 		}
385ebc921caSIan Kent 
386ebc921caSIan Kent 		wq->wait_queue_token = autofs_next_wait_queue;
387ebc921caSIan Kent 		if (++autofs_next_wait_queue == 0)
388ebc921caSIan Kent 			autofs_next_wait_queue = 1;
389ebc921caSIan Kent 		wq->next = sbi->queues;
390ebc921caSIan Kent 		sbi->queues = wq;
391ebc921caSIan Kent 		init_waitqueue_head(&wq->queue);
392ebc921caSIan Kent 		memcpy(&wq->name, &qstr, sizeof(struct qstr));
3932be7828cSAl Viro 		wq->offset = offset;
394ebc921caSIan Kent 		wq->dev = autofs_get_dev(sbi);
395ebc921caSIan Kent 		wq->ino = autofs_get_ino(sbi);
396ebc921caSIan Kent 		wq->uid = current_uid();
397ebc921caSIan Kent 		wq->gid = current_gid();
398ebc921caSIan Kent 		wq->pid = pid;
399ebc921caSIan Kent 		wq->tgid = tgid;
400ebc921caSIan Kent 		wq->status = -EINTR; /* Status return if interrupted */
401ebc921caSIan Kent 		wq->wait_ctr = 2;
402ebc921caSIan Kent 
403ebc921caSIan Kent 		if (sbi->version < 5) {
404ebc921caSIan Kent 			if (notify == NFY_MOUNT)
405ebc921caSIan Kent 				type = autofs_ptype_missing;
406ebc921caSIan Kent 			else
407ebc921caSIan Kent 				type = autofs_ptype_expire_multi;
408ebc921caSIan Kent 		} else {
409ebc921caSIan Kent 			if (notify == NFY_MOUNT)
410ebc921caSIan Kent 				type = autofs_type_trigger(sbi->type) ?
411ebc921caSIan Kent 					autofs_ptype_missing_direct :
412ebc921caSIan Kent 					 autofs_ptype_missing_indirect;
413ebc921caSIan Kent 			else
414ebc921caSIan Kent 				type = autofs_type_trigger(sbi->type) ?
415ebc921caSIan Kent 					autofs_ptype_expire_direct :
416ebc921caSIan Kent 					autofs_ptype_expire_indirect;
417ebc921caSIan Kent 		}
418ebc921caSIan Kent 
419ebc921caSIan Kent 		pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
420ebc921caSIan Kent 			 (unsigned long) wq->wait_queue_token, wq->name.len,
421ebc921caSIan Kent 			 wq->name.name, notify);
422ebc921caSIan Kent 
423ebc921caSIan Kent 		/*
424ebc921caSIan Kent 		 * autofs_notify_daemon() may block; it will unlock ->wq_mutex
425ebc921caSIan Kent 		 */
426ebc921caSIan Kent 		autofs_notify_daemon(sbi, wq, type);
427ebc921caSIan Kent 	} else {
428ebc921caSIan Kent 		wq->wait_ctr++;
429ebc921caSIan Kent 		pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
430ebc921caSIan Kent 			 (unsigned long) wq->wait_queue_token, wq->name.len,
431ebc921caSIan Kent 			 wq->name.name, notify);
432ebc921caSIan Kent 		mutex_unlock(&sbi->wq_mutex);
4332be7828cSAl Viro 		kfree(name);
434ebc921caSIan Kent 	}
435ebc921caSIan Kent 
436ebc921caSIan Kent 	/*
437ebc921caSIan Kent 	 * wq->name.name is NULL iff the lock is already released
438ebc921caSIan Kent 	 * or the mount has been made catatonic.
439ebc921caSIan Kent 	 */
440ebc921caSIan Kent 	wait_event_killable(wq->queue, wq->name.name == NULL);
441ebc921caSIan Kent 	status = wq->status;
442ebc921caSIan Kent 
443ebc921caSIan Kent 	/*
444ebc921caSIan Kent 	 * For direct and offset mounts we need to track the requester's
445ebc921caSIan Kent 	 * uid and gid in the dentry info struct. This is so it can be
446ebc921caSIan Kent 	 * supplied, on request, by the misc device ioctl interface.
447ebc921caSIan Kent 	 * This is needed during daemon resatart when reconnecting
448ebc921caSIan Kent 	 * to existing, active, autofs mounts. The uid and gid (and
449ebc921caSIan Kent 	 * related string values) may be used for macro substitution
450ebc921caSIan Kent 	 * in autofs mount maps.
451ebc921caSIan Kent 	 */
452ebc921caSIan Kent 	if (!status) {
453ebc921caSIan Kent 		struct autofs_info *ino;
454ebc921caSIan Kent 		struct dentry *de = NULL;
455ebc921caSIan Kent 
456ebc921caSIan Kent 		/* direct mount or browsable map */
457ebc921caSIan Kent 		ino = autofs_dentry_ino(dentry);
458ebc921caSIan Kent 		if (!ino) {
459ebc921caSIan Kent 			/* If not lookup actual dentry used */
460ebc921caSIan Kent 			de = d_lookup(dentry->d_parent, &dentry->d_name);
461ebc921caSIan Kent 			if (de)
462ebc921caSIan Kent 				ino = autofs_dentry_ino(de);
463ebc921caSIan Kent 		}
464ebc921caSIan Kent 
465ebc921caSIan Kent 		/* Set mount requester */
466ebc921caSIan Kent 		if (ino) {
467ebc921caSIan Kent 			spin_lock(&sbi->fs_lock);
468ebc921caSIan Kent 			ino->uid = wq->uid;
469ebc921caSIan Kent 			ino->gid = wq->gid;
470ebc921caSIan Kent 			spin_unlock(&sbi->fs_lock);
471ebc921caSIan Kent 		}
472ebc921caSIan Kent 
473ebc921caSIan Kent 		if (de)
474ebc921caSIan Kent 			dput(de);
475ebc921caSIan Kent 	}
476ebc921caSIan Kent 
477ebc921caSIan Kent 	/* Are we the last process to need status? */
478ebc921caSIan Kent 	mutex_lock(&sbi->wq_mutex);
479ebc921caSIan Kent 	if (!--wq->wait_ctr)
480ebc921caSIan Kent 		kfree(wq);
481ebc921caSIan Kent 	mutex_unlock(&sbi->wq_mutex);
482ebc921caSIan Kent 
483ebc921caSIan Kent 	return status;
484ebc921caSIan Kent }
485ebc921caSIan Kent 
486ebc921caSIan Kent 
autofs_wait_release(struct autofs_sb_info * sbi,autofs_wqt_t wait_queue_token,int status)487ebc921caSIan Kent int autofs_wait_release(struct autofs_sb_info *sbi,
488ebc921caSIan Kent 			autofs_wqt_t wait_queue_token, int status)
489ebc921caSIan Kent {
490ebc921caSIan Kent 	struct autofs_wait_queue *wq, **wql;
491ebc921caSIan Kent 
492ebc921caSIan Kent 	mutex_lock(&sbi->wq_mutex);
493ebc921caSIan Kent 	for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
494ebc921caSIan Kent 		if (wq->wait_queue_token == wait_queue_token)
495ebc921caSIan Kent 			break;
496ebc921caSIan Kent 	}
497ebc921caSIan Kent 
498ebc921caSIan Kent 	if (!wq) {
499ebc921caSIan Kent 		mutex_unlock(&sbi->wq_mutex);
500ebc921caSIan Kent 		return -EINVAL;
501ebc921caSIan Kent 	}
502ebc921caSIan Kent 
503ebc921caSIan Kent 	*wql = wq->next;	/* Unlink from chain */
5042be7828cSAl Viro 	kfree(wq->name.name - wq->offset);
505ebc921caSIan Kent 	wq->name.name = NULL;	/* Do not wait on this queue */
506ebc921caSIan Kent 	wq->status = status;
507ebc921caSIan Kent 	wake_up(&wq->queue);
508ebc921caSIan Kent 	if (!--wq->wait_ctr)
509ebc921caSIan Kent 		kfree(wq);
510ebc921caSIan Kent 	mutex_unlock(&sbi->wq_mutex);
511ebc921caSIan Kent 
512ebc921caSIan Kent 	return 0;
513ebc921caSIan Kent }
514