waitq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/waitq.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. * Copyright 2001-2003 Ian Kent <raven@themaw.net>
  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. #include <linux/slab.h>
  14. #include <linux/time.h>
  15. #include <linux/signal.h>
  16. #include <linux/file.h>
  17. #include "autofs_i.h"
  18. /* We make this a static variable rather than a part of the superblock; it
  19. is better if we don't reassign numbers easily even across filesystems */
  20. 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))
  23. void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
  24. {
  25. struct autofs_wait_queue *wq, *nwq;
  26. DPRINTK("entering catatonic mode");
  27. sbi->catatonic = 1;
  28. wq = sbi->queues;
  29. sbi->queues = NULL; /* Erase all wait queues */
  30. while ( wq ) {
  31. nwq = wq->next;
  32. wq->status = -ENOENT; /* Magic is gone - report failure */
  33. kfree(wq->name);
  34. wq->name = NULL;
  35. wake_up_interruptible(&wq->queue);
  36. wq = nwq;
  37. }
  38. if (sbi->pipe) {
  39. fput(sbi->pipe); /* Close the pipe */
  40. sbi->pipe = NULL;
  41. }
  42. shrink_dcache_sb(sbi->sb);
  43. }
  44. static int autofs4_write(struct file *file, const void *addr, int bytes)
  45. {
  46. unsigned long sigpipe, flags;
  47. mm_segment_t fs;
  48. const char *data = (const char *)addr;
  49. ssize_t wr = 0;
  50. /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
  51. sigpipe = sigismember(&current->pending.signal, SIGPIPE);
  52. /* Save pointer to user space and point back to kernel space */
  53. fs = get_fs();
  54. set_fs(KERNEL_DS);
  55. while (bytes &&
  56. (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
  57. data += wr;
  58. bytes -= wr;
  59. }
  60. set_fs(fs);
  61. /* Keep the currently executing process from receiving a
  62. SIGPIPE unless it was already supposed to get one */
  63. if (wr == -EPIPE && !sigpipe) {
  64. spin_lock_irqsave(&current->sighand->siglock, flags);
  65. sigdelset(&current->pending.signal, SIGPIPE);
  66. recalc_sigpending();
  67. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  68. }
  69. return (bytes > 0);
  70. }
  71. static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
  72. struct autofs_wait_queue *wq,
  73. int type)
  74. {
  75. union autofs_packet_union pkt;
  76. size_t pktsz;
  77. DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
  78. wq->wait_queue_token, wq->len, wq->name, type);
  79. memset(&pkt,0,sizeof pkt); /* For security reasons */
  80. pkt.hdr.proto_version = sbi->version;
  81. pkt.hdr.type = type;
  82. if (type == autofs_ptype_missing) {
  83. struct autofs_packet_missing *mp = &pkt.missing;
  84. pktsz = sizeof(*mp);
  85. mp->wait_queue_token = wq->wait_queue_token;
  86. mp->len = wq->len;
  87. memcpy(mp->name, wq->name, wq->len);
  88. mp->name[wq->len] = '\0';
  89. } else if (type == autofs_ptype_expire_multi) {
  90. struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
  91. pktsz = sizeof(*ep);
  92. ep->wait_queue_token = wq->wait_queue_token;
  93. ep->len = wq->len;
  94. memcpy(ep->name, wq->name, wq->len);
  95. ep->name[wq->len] = '\0';
  96. } else {
  97. printk("autofs4_notify_daemon: bad type %d!\n", type);
  98. return;
  99. }
  100. if (autofs4_write(sbi->pipe, &pkt, pktsz))
  101. autofs4_catatonic_mode(sbi);
  102. }
  103. static int autofs4_getpath(struct autofs_sb_info *sbi,
  104. struct dentry *dentry, char **name)
  105. {
  106. struct dentry *root = sbi->sb->s_root;
  107. struct dentry *tmp;
  108. char *buf = *name;
  109. char *p;
  110. int len = 0;
  111. spin_lock(&dcache_lock);
  112. for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
  113. len += tmp->d_name.len + 1;
  114. if (--len > NAME_MAX) {
  115. spin_unlock(&dcache_lock);
  116. return 0;
  117. }
  118. *(buf + len) = '\0';
  119. p = buf + len - dentry->d_name.len;
  120. strncpy(p, dentry->d_name.name, dentry->d_name.len);
  121. for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
  122. *(--p) = '/';
  123. p -= tmp->d_name.len;
  124. strncpy(p, tmp->d_name.name, tmp->d_name.len);
  125. }
  126. spin_unlock(&dcache_lock);
  127. return len;
  128. }
  129. int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
  130. enum autofs_notify notify)
  131. {
  132. struct autofs_wait_queue *wq;
  133. char *name;
  134. int len, status;
  135. /* In catatonic mode, we don't wait for nobody */
  136. if ( sbi->catatonic )
  137. return -ENOENT;
  138. name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  139. if (!name)
  140. return -ENOMEM;
  141. len = autofs4_getpath(sbi, dentry, &name);
  142. if (!len) {
  143. kfree(name);
  144. return -ENOENT;
  145. }
  146. if (down_interruptible(&sbi->wq_sem)) {
  147. kfree(name);
  148. return -EINTR;
  149. }
  150. for (wq = sbi->queues ; wq ; wq = wq->next) {
  151. if (wq->hash == dentry->d_name.hash &&
  152. wq->len == len &&
  153. wq->name && !memcmp(wq->name, name, len))
  154. break;
  155. }
  156. if ( !wq ) {
  157. /* Can't wait for an expire if there's no mount */
  158. if (notify == NFY_NONE && !d_mountpoint(dentry)) {
  159. kfree(name);
  160. up(&sbi->wq_sem);
  161. return -ENOENT;
  162. }
  163. /* Create a new wait queue */
  164. wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
  165. if ( !wq ) {
  166. kfree(name);
  167. up(&sbi->wq_sem);
  168. return -ENOMEM;
  169. }
  170. wq->wait_queue_token = autofs4_next_wait_queue;
  171. if (++autofs4_next_wait_queue == 0)
  172. autofs4_next_wait_queue = 1;
  173. wq->next = sbi->queues;
  174. sbi->queues = wq;
  175. init_waitqueue_head(&wq->queue);
  176. wq->hash = dentry->d_name.hash;
  177. wq->name = name;
  178. wq->len = len;
  179. wq->status = -EINTR; /* Status return if interrupted */
  180. atomic_set(&wq->wait_ctr, 2);
  181. atomic_set(&wq->notified, 1);
  182. up(&sbi->wq_sem);
  183. } else {
  184. atomic_inc(&wq->wait_ctr);
  185. up(&sbi->wq_sem);
  186. kfree(name);
  187. DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
  188. (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
  189. }
  190. if (notify != NFY_NONE && atomic_dec_and_test(&wq->notified)) {
  191. int type = (notify == NFY_MOUNT ?
  192. autofs_ptype_missing : autofs_ptype_expire_multi);
  193. DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
  194. (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
  195. /* autofs4_notify_daemon() may block */
  196. autofs4_notify_daemon(sbi, wq, type);
  197. }
  198. /* wq->name is NULL if and only if the lock is already released */
  199. if ( sbi->catatonic ) {
  200. /* We might have slept, so check again for catatonic mode */
  201. wq->status = -ENOENT;
  202. kfree(wq->name);
  203. wq->name = NULL;
  204. }
  205. if ( wq->name ) {
  206. /* Block all but "shutdown" signals while waiting */
  207. sigset_t oldset;
  208. unsigned long irqflags;
  209. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  210. oldset = current->blocked;
  211. siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
  212. recalc_sigpending();
  213. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  214. wait_event_interruptible(wq->queue, wq->name == NULL);
  215. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  216. current->blocked = oldset;
  217. recalc_sigpending();
  218. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  219. } else {
  220. DPRINTK("skipped sleeping");
  221. }
  222. status = wq->status;
  223. /* Are we the last process to need status? */
  224. if (atomic_dec_and_test(&wq->wait_ctr))
  225. kfree(wq);
  226. return status;
  227. }
  228. int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
  229. {
  230. struct autofs_wait_queue *wq, **wql;
  231. down(&sbi->wq_sem);
  232. for ( wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next ) {
  233. if ( wq->wait_queue_token == wait_queue_token )
  234. break;
  235. }
  236. if ( !wq ) {
  237. up(&sbi->wq_sem);
  238. return -EINVAL;
  239. }
  240. *wql = wq->next; /* Unlink from chain */
  241. up(&sbi->wq_sem);
  242. kfree(wq->name);
  243. wq->name = NULL; /* Do not wait on this queue */
  244. wq->status = status;
  245. if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
  246. kfree(wq);
  247. else
  248. wake_up_interruptible(&wq->queue);
  249. return 0;
  250. }