2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
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.
10 #include <linux/slab.h>
11 #include <linux/time.h>
12 #include <linux/signal.h>
13 #include <linux/file.h>
16 /* We make this a static variable rather than a part of the superblock; it
17 * is better if we don't reassign numbers easily even across filesystems
19 static autofs_wqt_t autofs4_next_wait_queue
= 1;
21 /* These are the signals we allow interrupting a pending mount */
22 #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
24 void autofs4_catatonic_mode(struct autofs_sb_info
*sbi
)
26 struct autofs_wait_queue
*wq
, *nwq
;
28 mutex_lock(&sbi
->wq_mutex
);
30 mutex_unlock(&sbi
->wq_mutex
);
34 pr_debug("entering catatonic mode\n");
38 sbi
->queues
= NULL
; /* Erase all wait queues */
41 wq
->status
= -ENOENT
; /* Magic is gone - report failure */
45 wake_up_interruptible(&wq
->queue
);
48 fput(sbi
->pipe
); /* Close the pipe */
51 mutex_unlock(&sbi
->wq_mutex
);
54 static int autofs4_write(struct autofs_sb_info
*sbi
,
55 struct file
*file
, const void *addr
, int bytes
)
57 unsigned long sigpipe
, flags
;
59 const char *data
= (const char *)addr
;
62 sigpipe
= sigismember(¤t
->pending
.signal
, SIGPIPE
);
64 /* Save pointer to user space and point back to kernel space */
68 mutex_lock(&sbi
->pipe_mutex
);
70 wr
= __vfs_write(file
, data
, bytes
, &file
->f_pos
);
76 mutex_unlock(&sbi
->pipe_mutex
);
80 /* Keep the currently executing process from receiving a
81 * SIGPIPE unless it was already supposed to get one
83 if (wr
== -EPIPE
&& !sigpipe
) {
84 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
85 sigdelset(¤t
->pending
.signal
, SIGPIPE
);
87 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
90 /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
91 return bytes
== 0 ? 0 : wr
< 0 ? wr
: -EIO
;
94 static void autofs4_notify_daemon(struct autofs_sb_info
*sbi
,
95 struct autofs_wait_queue
*wq
,
99 struct autofs_packet_hdr hdr
;
100 union autofs_packet_union v4_pkt
;
101 union autofs_v5_packet_union v5_pkt
;
103 struct file
*pipe
= NULL
;
107 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
);
111 memset(&pkt
, 0, sizeof(pkt
)); /* For security reasons */
113 pkt
.hdr
.proto_version
= sbi
->version
;
117 /* Kernel protocol v4 missing and expire packets */
118 case autofs_ptype_missing
:
120 struct autofs_packet_missing
*mp
= &pkt
.v4_pkt
.missing
;
124 mp
->wait_queue_token
= wq
->wait_queue_token
;
125 mp
->len
= wq
->name
.len
;
126 memcpy(mp
->name
, wq
->name
.name
, wq
->name
.len
);
127 mp
->name
[wq
->name
.len
] = '\0';
130 case autofs_ptype_expire_multi
:
132 struct autofs_packet_expire_multi
*ep
=
133 &pkt
.v4_pkt
.expire_multi
;
137 ep
->wait_queue_token
= wq
->wait_queue_token
;
138 ep
->len
= wq
->name
.len
;
139 memcpy(ep
->name
, wq
->name
.name
, wq
->name
.len
);
140 ep
->name
[wq
->name
.len
] = '\0';
144 * Kernel protocol v5 packet for handling indirect and direct
145 * mount missing and expire requests
147 case autofs_ptype_missing_indirect
:
148 case autofs_ptype_expire_indirect
:
149 case autofs_ptype_missing_direct
:
150 case autofs_ptype_expire_direct
:
152 struct autofs_v5_packet
*packet
= &pkt
.v5_pkt
.v5_packet
;
153 struct user_namespace
*user_ns
= sbi
->pipe
->f_cred
->user_ns
;
155 pktsz
= sizeof(*packet
);
157 packet
->wait_queue_token
= wq
->wait_queue_token
;
158 packet
->len
= wq
->name
.len
;
159 memcpy(packet
->name
, wq
->name
.name
, wq
->name
.len
);
160 packet
->name
[wq
->name
.len
] = '\0';
161 packet
->dev
= wq
->dev
;
162 packet
->ino
= wq
->ino
;
163 packet
->uid
= from_kuid_munged(user_ns
, wq
->uid
);
164 packet
->gid
= from_kgid_munged(user_ns
, wq
->gid
);
165 packet
->pid
= wq
->pid
;
166 packet
->tgid
= wq
->tgid
;
170 pr_warn("bad type %d!\n", type
);
171 mutex_unlock(&sbi
->wq_mutex
);
175 pipe
= get_file(sbi
->pipe
);
177 mutex_unlock(&sbi
->wq_mutex
);
179 switch (ret
= autofs4_write(sbi
, pipe
, &pkt
, pktsz
)) {
184 /* Just fail this one */
185 autofs4_wait_release(sbi
, wq
->wait_queue_token
, ret
);
188 autofs4_catatonic_mode(sbi
);
194 static int autofs4_getpath(struct autofs_sb_info
*sbi
,
195 struct dentry
*dentry
, char **name
)
197 struct dentry
*root
= sbi
->sb
->s_root
;
208 seq
= read_seqbegin(&rename_lock
);
210 spin_lock(&sbi
->fs_lock
);
211 for (tmp
= dentry
; tmp
!= root
; tmp
= tmp
->d_parent
)
212 len
+= tmp
->d_name
.len
+ 1;
214 if (!len
|| --len
> NAME_MAX
) {
215 spin_unlock(&sbi
->fs_lock
);
217 if (read_seqretry(&rename_lock
, seq
))
223 p
= buf
+ len
- dentry
->d_name
.len
;
224 strncpy(p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
226 for (tmp
= dentry
->d_parent
; tmp
!= root
; tmp
= tmp
->d_parent
) {
228 p
-= tmp
->d_name
.len
;
229 strncpy(p
, tmp
->d_name
.name
, tmp
->d_name
.len
);
231 spin_unlock(&sbi
->fs_lock
);
233 if (read_seqretry(&rename_lock
, seq
))
239 static struct autofs_wait_queue
*
240 autofs4_find_wait(struct autofs_sb_info
*sbi
, const struct qstr
*qstr
)
242 struct autofs_wait_queue
*wq
;
244 for (wq
= sbi
->queues
; wq
; wq
= wq
->next
) {
245 if (wq
->name
.hash
== qstr
->hash
&&
246 wq
->name
.len
== qstr
->len
&&
248 !memcmp(wq
->name
.name
, qstr
->name
, qstr
->len
))
255 * Check if we have a valid request.
257 * 1 if the request should continue.
258 * In this case we can return an autofs_wait_queue entry if one is
259 * found or NULL to idicate a new wait needs to be created.
260 * 0 or a negative errno if the request shouldn't continue.
262 static int validate_request(struct autofs_wait_queue
**wait
,
263 struct autofs_sb_info
*sbi
,
264 const struct qstr
*qstr
,
265 struct dentry
*dentry
, enum autofs_notify notify
)
267 struct autofs_wait_queue
*wq
;
268 struct autofs_info
*ino
;
273 /* Wait in progress, continue; */
274 wq
= autofs4_find_wait(sbi
, qstr
);
282 /* If we don't yet have any info this is a new request */
283 ino
= autofs4_dentry_ino(dentry
);
288 * If we've been asked to wait on an existing expire (NFY_NONE)
289 * but there is no wait in the queue ...
291 if (notify
== NFY_NONE
) {
293 * Either we've betean the pending expire to post it's
294 * wait or it finished while we waited on the mutex.
295 * So we need to wait till either, the wait appears
296 * or the expire finishes.
299 while (ino
->flags
& AUTOFS_INF_EXPIRING
) {
300 mutex_unlock(&sbi
->wq_mutex
);
301 schedule_timeout_interruptible(HZ
/10);
302 if (mutex_lock_interruptible(&sbi
->wq_mutex
))
308 wq
= autofs4_find_wait(sbi
, qstr
);
316 * Not ideal but the status has already gone. Of the two
317 * cases where we wait on NFY_NONE neither depend on the
318 * return status of the wait.
324 * If we've been asked to trigger a mount and the request
325 * completed while we waited on the mutex ...
327 if (notify
== NFY_MOUNT
) {
328 struct dentry
*new = NULL
;
332 * If the dentry was successfully mounted while we slept
333 * on the wait queue mutex we can return success. If it
334 * isn't mounted (doesn't have submounts for the case of
335 * a multi-mount with no mount at it's base) we can
336 * continue on and create a new request.
338 if (!IS_ROOT(dentry
)) {
339 if (d_unhashed(dentry
) &&
340 d_really_is_positive(dentry
)) {
341 struct dentry
*parent
= dentry
->d_parent
;
343 new = d_lookup(parent
, &dentry
->d_name
);
348 if (have_submounts(dentry
))
359 int autofs4_wait(struct autofs_sb_info
*sbi
,
360 struct dentry
*dentry
, enum autofs_notify notify
)
362 struct autofs_wait_queue
*wq
;
365 int status
, ret
, type
;
369 /* In catatonic mode, we don't wait for nobody */
374 * Try translating pids to the namespace of the daemon.
376 * Zero means failure: we are in an unrelated pid namespace.
378 pid
= task_pid_nr_ns(current
, ns_of_pid(sbi
->oz_pgrp
));
379 tgid
= task_tgid_nr_ns(current
, ns_of_pid(sbi
->oz_pgrp
));
380 if (pid
== 0 || tgid
== 0)
383 if (d_really_is_negative(dentry
)) {
385 * A wait for a negative dentry is invalid for certain
386 * cases. A direct or offset mount "always" has its mount
387 * point directory created and so the request dentry must
388 * be positive or the map key doesn't exist. The situation
389 * is very similar for indirect mounts except only dentrys
390 * in the root of the autofs file system may be negative.
392 if (autofs_type_trigger(sbi
->type
))
394 else if (!IS_ROOT(dentry
->d_parent
))
398 name
= kmalloc(NAME_MAX
+ 1, GFP_KERNEL
);
402 /* If this is a direct mount request create a dummy name */
403 if (IS_ROOT(dentry
) && autofs_type_trigger(sbi
->type
))
404 qstr
.len
= sprintf(name
, "%p", dentry
);
406 qstr
.len
= autofs4_getpath(sbi
, dentry
, &name
);
413 qstr
.hash
= full_name_hash(dentry
, name
, qstr
.len
);
415 if (mutex_lock_interruptible(&sbi
->wq_mutex
)) {
420 ret
= validate_request(&wq
, sbi
, &qstr
, dentry
, notify
);
423 mutex_unlock(&sbi
->wq_mutex
);
429 /* Create a new wait queue */
430 wq
= kmalloc(sizeof(struct autofs_wait_queue
), GFP_KERNEL
);
433 mutex_unlock(&sbi
->wq_mutex
);
437 wq
->wait_queue_token
= autofs4_next_wait_queue
;
438 if (++autofs4_next_wait_queue
== 0)
439 autofs4_next_wait_queue
= 1;
440 wq
->next
= sbi
->queues
;
442 init_waitqueue_head(&wq
->queue
);
443 memcpy(&wq
->name
, &qstr
, sizeof(struct qstr
));
444 wq
->dev
= autofs4_get_dev(sbi
);
445 wq
->ino
= autofs4_get_ino(sbi
);
446 wq
->uid
= current_cred()->uid
;
447 wq
->gid
= current_cred()->gid
;
450 wq
->status
= -EINTR
; /* Status return if interrupted */
453 if (sbi
->version
< 5) {
454 if (notify
== NFY_MOUNT
)
455 type
= autofs_ptype_missing
;
457 type
= autofs_ptype_expire_multi
;
459 if (notify
== NFY_MOUNT
)
460 type
= autofs_type_trigger(sbi
->type
) ?
461 autofs_ptype_missing_direct
:
462 autofs_ptype_missing_indirect
;
464 type
= autofs_type_trigger(sbi
->type
) ?
465 autofs_ptype_expire_direct
:
466 autofs_ptype_expire_indirect
;
469 pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
470 (unsigned long) wq
->wait_queue_token
, wq
->name
.len
,
471 wq
->name
.name
, notify
);
474 * autofs4_notify_daemon() may block; it will unlock ->wq_mutex
476 autofs4_notify_daemon(sbi
, wq
, type
);
479 pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
480 (unsigned long) wq
->wait_queue_token
, wq
->name
.len
,
481 wq
->name
.name
, notify
);
482 mutex_unlock(&sbi
->wq_mutex
);
487 * wq->name.name is NULL iff the lock is already released
488 * or the mount has been made catatonic.
491 /* Block all but "shutdown" signals while waiting */
492 unsigned long shutdown_sigs_mask
;
493 unsigned long irqflags
;
496 spin_lock_irqsave(¤t
->sighand
->siglock
, irqflags
);
497 oldset
= current
->blocked
;
498 shutdown_sigs_mask
= SHUTDOWN_SIGS
& ~oldset
.sig
[0];
499 siginitsetinv(¤t
->blocked
, shutdown_sigs_mask
);
501 spin_unlock_irqrestore(¤t
->sighand
->siglock
, irqflags
);
503 wait_event_interruptible(wq
->queue
, wq
->name
.name
== NULL
);
505 spin_lock_irqsave(¤t
->sighand
->siglock
, irqflags
);
506 current
->blocked
= oldset
;
508 spin_unlock_irqrestore(¤t
->sighand
->siglock
, irqflags
);
510 pr_debug("skipped sleeping\n");
516 * For direct and offset mounts we need to track the requester's
517 * uid and gid in the dentry info struct. This is so it can be
518 * supplied, on request, by the misc device ioctl interface.
519 * This is needed during daemon resatart when reconnecting
520 * to existing, active, autofs mounts. The uid and gid (and
521 * related string values) may be used for macro substitution
522 * in autofs mount maps.
525 struct autofs_info
*ino
;
526 struct dentry
*de
= NULL
;
528 /* direct mount or browsable map */
529 ino
= autofs4_dentry_ino(dentry
);
531 /* If not lookup actual dentry used */
532 de
= d_lookup(dentry
->d_parent
, &dentry
->d_name
);
534 ino
= autofs4_dentry_ino(de
);
537 /* Set mount requester */
539 spin_lock(&sbi
->fs_lock
);
542 spin_unlock(&sbi
->fs_lock
);
549 /* Are we the last process to need status? */
550 mutex_lock(&sbi
->wq_mutex
);
553 mutex_unlock(&sbi
->wq_mutex
);
559 int autofs4_wait_release(struct autofs_sb_info
*sbi
, autofs_wqt_t wait_queue_token
, int status
)
561 struct autofs_wait_queue
*wq
, **wql
;
563 mutex_lock(&sbi
->wq_mutex
);
564 for (wql
= &sbi
->queues
; (wq
= *wql
) != NULL
; wql
= &wq
->next
) {
565 if (wq
->wait_queue_token
== wait_queue_token
)
570 mutex_unlock(&sbi
->wq_mutex
);
574 *wql
= wq
->next
; /* Unlink from chain */
575 kfree(wq
->name
.name
);
576 wq
->name
.name
= NULL
; /* Do not wait on this queue */
578 wake_up_interruptible(&wq
->queue
);
581 mutex_unlock(&sbi
->wq_mutex
);