Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- c -*- --------------------------------------------------------------- * |
| 2 | * |
| 3 | * linux/fs/autofs/waitq.c |
| 4 | * |
| 5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 6 | * Copyright 2001-2006 Ian Kent <raven@themaw.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This file is part of the Linux kernel and is made available under |
| 9 | * the terms of the GNU General Public License, version 2, or at your |
| 10 | * option, any later version, incorporated herein by reference. |
| 11 | * |
| 12 | * ------------------------------------------------------------------------- */ |
| 13 | |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/time.h> |
| 16 | #include <linux/signal.h> |
| 17 | #include <linux/file.h> |
| 18 | #include "autofs_i.h" |
| 19 | |
| 20 | /* We make this a static variable rather than a part of the superblock; it |
| 21 | is better if we don't reassign numbers easily even across filesystems */ |
| 22 | static autofs_wqt_t autofs4_next_wait_queue = 1; |
| 23 | |
| 24 | /* These are the signals we allow interrupting a pending mount */ |
| 25 | #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT)) |
| 26 | |
| 27 | void autofs4_catatonic_mode(struct autofs_sb_info *sbi) |
| 28 | { |
| 29 | struct autofs_wait_queue *wq, *nwq; |
| 30 | |
| 31 | DPRINTK("entering catatonic mode"); |
| 32 | |
| 33 | sbi->catatonic = 1; |
| 34 | wq = sbi->queues; |
| 35 | sbi->queues = NULL; /* Erase all wait queues */ |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 36 | while (wq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | nwq = wq->next; |
| 38 | wq->status = -ENOENT; /* Magic is gone - report failure */ |
| 39 | kfree(wq->name); |
| 40 | wq->name = NULL; |
| 41 | wake_up_interruptible(&wq->queue); |
| 42 | wq = nwq; |
| 43 | } |
| 44 | if (sbi->pipe) { |
| 45 | fput(sbi->pipe); /* Close the pipe */ |
| 46 | sbi->pipe = NULL; |
| 47 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | shrink_dcache_sb(sbi->sb); |
| 49 | } |
| 50 | |
| 51 | static int autofs4_write(struct file *file, const void *addr, int bytes) |
| 52 | { |
| 53 | unsigned long sigpipe, flags; |
| 54 | mm_segment_t fs; |
| 55 | const char *data = (const char *)addr; |
| 56 | ssize_t wr = 0; |
| 57 | |
| 58 | /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/ |
| 59 | |
| 60 | sigpipe = sigismember(¤t->pending.signal, SIGPIPE); |
| 61 | |
| 62 | /* Save pointer to user space and point back to kernel space */ |
| 63 | fs = get_fs(); |
| 64 | set_fs(KERNEL_DS); |
| 65 | |
| 66 | while (bytes && |
| 67 | (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) { |
| 68 | data += wr; |
| 69 | bytes -= wr; |
| 70 | } |
| 71 | |
| 72 | set_fs(fs); |
| 73 | |
| 74 | /* Keep the currently executing process from receiving a |
| 75 | SIGPIPE unless it was already supposed to get one */ |
| 76 | if (wr == -EPIPE && !sigpipe) { |
| 77 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 78 | sigdelset(¤t->pending.signal, SIGPIPE); |
| 79 | recalc_sigpending(); |
| 80 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 81 | } |
| 82 | |
| 83 | return (bytes > 0); |
| 84 | } |
| 85 | |
| 86 | static void autofs4_notify_daemon(struct autofs_sb_info *sbi, |
| 87 | struct autofs_wait_queue *wq, |
| 88 | int type) |
| 89 | { |
| 90 | union autofs_packet_union pkt; |
| 91 | size_t pktsz; |
| 92 | |
| 93 | DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d", |
| 94 | wq->wait_queue_token, wq->len, wq->name, type); |
| 95 | |
| 96 | memset(&pkt,0,sizeof pkt); /* For security reasons */ |
| 97 | |
| 98 | pkt.hdr.proto_version = sbi->version; |
| 99 | pkt.hdr.type = type; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 100 | switch (type) { |
| 101 | /* Kernel protocol v4 missing and expire packets */ |
| 102 | case autofs_ptype_missing: |
| 103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct autofs_packet_missing *mp = &pkt.missing; |
| 105 | |
| 106 | pktsz = sizeof(*mp); |
| 107 | |
| 108 | mp->wait_queue_token = wq->wait_queue_token; |
| 109 | mp->len = wq->len; |
| 110 | memcpy(mp->name, wq->name, wq->len); |
| 111 | mp->name[wq->len] = '\0'; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | case autofs_ptype_expire_multi: |
| 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | struct autofs_packet_expire_multi *ep = &pkt.expire_multi; |
| 117 | |
| 118 | pktsz = sizeof(*ep); |
| 119 | |
| 120 | ep->wait_queue_token = wq->wait_queue_token; |
| 121 | ep->len = wq->len; |
| 122 | memcpy(ep->name, wq->name, wq->len); |
| 123 | ep->name[wq->len] = '\0'; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | /* |
| 127 | * Kernel protocol v5 packet for handling indirect and direct |
| 128 | * mount missing and expire requests |
| 129 | */ |
| 130 | case autofs_ptype_missing_indirect: |
| 131 | case autofs_ptype_expire_indirect: |
| 132 | case autofs_ptype_missing_direct: |
| 133 | case autofs_ptype_expire_direct: |
| 134 | { |
| 135 | struct autofs_v5_packet *packet = &pkt.v5_packet; |
| 136 | |
| 137 | pktsz = sizeof(*packet); |
| 138 | |
| 139 | packet->wait_queue_token = wq->wait_queue_token; |
| 140 | packet->len = wq->len; |
| 141 | memcpy(packet->name, wq->name, wq->len); |
| 142 | packet->name[wq->len] = '\0'; |
| 143 | packet->dev = wq->dev; |
| 144 | packet->ino = wq->ino; |
| 145 | packet->uid = wq->uid; |
| 146 | packet->gid = wq->gid; |
| 147 | packet->pid = wq->pid; |
| 148 | packet->tgid = wq->tgid; |
| 149 | break; |
| 150 | } |
| 151 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | printk("autofs4_notify_daemon: bad type %d!\n", type); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (autofs4_write(sbi->pipe, &pkt, pktsz)) |
| 157 | autofs4_catatonic_mode(sbi); |
| 158 | } |
| 159 | |
| 160 | static int autofs4_getpath(struct autofs_sb_info *sbi, |
| 161 | struct dentry *dentry, char **name) |
| 162 | { |
| 163 | struct dentry *root = sbi->sb->s_root; |
| 164 | struct dentry *tmp; |
| 165 | char *buf = *name; |
| 166 | char *p; |
| 167 | int len = 0; |
| 168 | |
| 169 | spin_lock(&dcache_lock); |
| 170 | for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent) |
| 171 | len += tmp->d_name.len + 1; |
| 172 | |
| 173 | if (--len > NAME_MAX) { |
| 174 | spin_unlock(&dcache_lock); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | *(buf + len) = '\0'; |
| 179 | p = buf + len - dentry->d_name.len; |
| 180 | strncpy(p, dentry->d_name.name, dentry->d_name.len); |
| 181 | |
| 182 | for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) { |
| 183 | *(--p) = '/'; |
| 184 | p -= tmp->d_name.len; |
| 185 | strncpy(p, tmp->d_name.name, tmp->d_name.len); |
| 186 | } |
| 187 | spin_unlock(&dcache_lock); |
| 188 | |
| 189 | return len; |
| 190 | } |
| 191 | |
| 192 | int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, |
| 193 | enum autofs_notify notify) |
| 194 | { |
| 195 | struct autofs_wait_queue *wq; |
| 196 | char *name; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 197 | unsigned int len = 0; |
| 198 | unsigned int hash = 0; |
| 199 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | /* In catatonic mode, we don't wait for nobody */ |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 202 | if (sbi->catatonic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return -ENOENT; |
| 204 | |
| 205 | name = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 206 | if (!name) |
| 207 | return -ENOMEM; |
| 208 | |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 209 | /* If this is a direct mount request create a dummy name */ |
Ian Kent | 44d53eb | 2006-03-27 01:14:56 -0800 | [diff] [blame] | 210 | if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT)) |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 211 | len = sprintf(name, "%p", dentry); |
| 212 | else { |
| 213 | len = autofs4_getpath(sbi, dentry, &name); |
| 214 | if (!len) { |
| 215 | kfree(name); |
| 216 | return -ENOENT; |
| 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 219 | hash = full_name_hash(name, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 221 | if (mutex_lock_interruptible(&sbi->wq_mutex)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | kfree(name); |
| 223 | return -EINTR; |
| 224 | } |
| 225 | |
| 226 | for (wq = sbi->queues ; wq ; wq = wq->next) { |
| 227 | if (wq->hash == dentry->d_name.hash && |
| 228 | wq->len == len && |
| 229 | wq->name && !memcmp(wq->name, name, len)) |
| 230 | break; |
| 231 | } |
| 232 | |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 233 | if (!wq) { |
Ian Kent | cc9acc8 | 2005-06-21 17:16:39 -0700 | [diff] [blame] | 234 | /* Can't wait for an expire if there's no mount */ |
| 235 | if (notify == NFY_NONE && !d_mountpoint(dentry)) { |
| 236 | kfree(name); |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 237 | mutex_unlock(&sbi->wq_mutex); |
Ian Kent | cc9acc8 | 2005-06-21 17:16:39 -0700 | [diff] [blame] | 238 | return -ENOENT; |
| 239 | } |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | /* Create a new wait queue */ |
| 242 | wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL); |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 243 | if (!wq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | kfree(name); |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 245 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return -ENOMEM; |
| 247 | } |
| 248 | |
| 249 | wq->wait_queue_token = autofs4_next_wait_queue; |
| 250 | if (++autofs4_next_wait_queue == 0) |
| 251 | autofs4_next_wait_queue = 1; |
| 252 | wq->next = sbi->queues; |
| 253 | sbi->queues = wq; |
| 254 | init_waitqueue_head(&wq->queue); |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 255 | wq->hash = hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | wq->name = name; |
| 257 | wq->len = len; |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 258 | wq->dev = autofs4_get_dev(sbi); |
| 259 | wq->ino = autofs4_get_ino(sbi); |
| 260 | wq->uid = current->uid; |
| 261 | wq->gid = current->gid; |
| 262 | wq->pid = current->pid; |
| 263 | wq->tgid = current->tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | wq->status = -EINTR; /* Status return if interrupted */ |
| 265 | atomic_set(&wq->wait_ctr, 2); |
Ian Kent | 3e7b191 | 2006-03-27 01:14:59 -0800 | [diff] [blame^] | 266 | atomic_set(&wq->notify, 1); |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 267 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } else { |
| 269 | atomic_inc(&wq->wait_ctr); |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 270 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | kfree(name); |
| 272 | DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d", |
| 273 | (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify); |
| 274 | } |
| 275 | |
Ian Kent | 3e7b191 | 2006-03-27 01:14:59 -0800 | [diff] [blame^] | 276 | if (notify != NFY_NONE && atomic_read(&wq->notify)) { |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 277 | int type; |
| 278 | |
Ian Kent | 3e7b191 | 2006-03-27 01:14:59 -0800 | [diff] [blame^] | 279 | atomic_dec(&wq->notify); |
| 280 | |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 281 | if (sbi->version < 5) { |
| 282 | if (notify == NFY_MOUNT) |
| 283 | type = autofs_ptype_missing; |
| 284 | else |
| 285 | type = autofs_ptype_expire_multi; |
| 286 | } else { |
| 287 | if (notify == NFY_MOUNT) |
Ian Kent | 44d53eb | 2006-03-27 01:14:56 -0800 | [diff] [blame] | 288 | type = (sbi->type & AUTOFS_TYPE_DIRECT) ? |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 289 | autofs_ptype_missing_direct : |
| 290 | autofs_ptype_missing_indirect; |
| 291 | else |
Ian Kent | 44d53eb | 2006-03-27 01:14:56 -0800 | [diff] [blame] | 292 | type = (sbi->type & AUTOFS_TYPE_DIRECT) ? |
Ian Kent | 5c0a32f | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 293 | autofs_ptype_expire_direct : |
| 294 | autofs_ptype_expire_indirect; |
| 295 | } |
Ian Kent | 4dcd00b | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 296 | |
Ian Kent | 682d4fc | 2005-07-07 17:57:02 -0700 | [diff] [blame] | 297 | DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n", |
| 298 | (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify); |
Ian Kent | 4dcd00b | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 299 | |
| 300 | /* autofs4_notify_daemon() may block */ |
| 301 | autofs4_notify_daemon(sbi, wq, type); |
| 302 | } |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | /* wq->name is NULL if and only if the lock is already released */ |
| 305 | |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 306 | if (sbi->catatonic) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* We might have slept, so check again for catatonic mode */ |
| 308 | wq->status = -ENOENT; |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 309 | kfree(wq->name); |
| 310 | wq->name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 313 | if (wq->name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | /* Block all but "shutdown" signals while waiting */ |
| 315 | sigset_t oldset; |
| 316 | unsigned long irqflags; |
| 317 | |
| 318 | spin_lock_irqsave(¤t->sighand->siglock, irqflags); |
| 319 | oldset = current->blocked; |
| 320 | siginitsetinv(¤t->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]); |
| 321 | recalc_sigpending(); |
| 322 | spin_unlock_irqrestore(¤t->sighand->siglock, irqflags); |
| 323 | |
| 324 | wait_event_interruptible(wq->queue, wq->name == NULL); |
| 325 | |
| 326 | spin_lock_irqsave(¤t->sighand->siglock, irqflags); |
| 327 | current->blocked = oldset; |
| 328 | recalc_sigpending(); |
| 329 | spin_unlock_irqrestore(¤t->sighand->siglock, irqflags); |
| 330 | } else { |
| 331 | DPRINTK("skipped sleeping"); |
| 332 | } |
| 333 | |
| 334 | status = wq->status; |
| 335 | |
| 336 | /* Are we the last process to need status? */ |
| 337 | if (atomic_dec_and_test(&wq->wait_ctr)) |
| 338 | kfree(wq); |
| 339 | |
| 340 | return status; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status) |
| 345 | { |
| 346 | struct autofs_wait_queue *wq, **wql; |
| 347 | |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 348 | mutex_lock(&sbi->wq_mutex); |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 349 | for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) { |
| 350 | if (wq->wait_queue_token == wait_queue_token) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | |
Ian Kent | e77fbdd | 2006-03-27 01:14:49 -0800 | [diff] [blame] | 354 | if (!wq) { |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 355 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return -EINVAL; |
| 357 | } |
| 358 | |
| 359 | *wql = wq->next; /* Unlink from chain */ |
Ingo Molnar | 1d5599e | 2006-03-23 03:00:41 -0800 | [diff] [blame] | 360 | mutex_unlock(&sbi->wq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | kfree(wq->name); |
| 362 | wq->name = NULL; /* Do not wait on this queue */ |
| 363 | |
| 364 | wq->status = status; |
| 365 | |
| 366 | if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */ |
| 367 | kfree(wq); |
| 368 | else |
| 369 | wake_up_interruptible(&wq->queue); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |