waitq.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* -*- linux-c -*- --------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/waitq.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. *
  7. * This file is part of the Linux kernel and is made available under
  8. * the terms of the GNU General Public License, version 2, or at your
  9. * option, any later version, incorporated herein by reference.
  10. *
  11. * ------------------------------------------------------------------------- */
  12. #include <linux/slab.h>
  13. #include <linux/time.h>
  14. #include <linux/signal.h>
  15. #include <linux/file.h>
  16. #include "autofs_i.h"
  17. /* We make this a static variable rather than a part of the superblock; it
  18. is better if we don't reassign numbers easily even across filesystems */
  19. static autofs_wqt_t autofs_next_wait_queue = 1;
  20. /* These are the signals we allow interrupting a pending mount */
  21. #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
  22. void autofs_catatonic_mode(struct autofs_sb_info *sbi)
  23. {
  24. struct autofs_wait_queue *wq, *nwq;
  25. DPRINTK(("autofs: entering catatonic mode\n"));
  26. sbi->catatonic = 1;
  27. wq = sbi->queues;
  28. sbi->queues = NULL; /* Erase all wait queues */
  29. while ( wq ) {
  30. nwq = wq->next;
  31. wq->status = -ENOENT; /* Magic is gone - report failure */
  32. kfree(wq->name);
  33. wq->name = NULL;
  34. wake_up(&wq->queue);
  35. wq = nwq;
  36. }
  37. fput(sbi->pipe); /* Close the pipe */
  38. sbi->pipe = NULL;
  39. autofs_hash_dputall(&sbi->dirhash); /* Remove all dentry pointers */
  40. }
  41. static int autofs_write(struct file *file, const void *addr, int bytes)
  42. {
  43. unsigned long sigpipe, flags;
  44. mm_segment_t fs;
  45. const char *data = (const char *)addr;
  46. ssize_t wr = 0;
  47. /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
  48. sigpipe = sigismember(&current->pending.signal, SIGPIPE);
  49. /* Save pointer to user space and point back to kernel space */
  50. fs = get_fs();
  51. set_fs(KERNEL_DS);
  52. while (bytes &&
  53. (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
  54. data += wr;
  55. bytes -= wr;
  56. }
  57. set_fs(fs);
  58. /* Keep the currently executing process from receiving a
  59. SIGPIPE unless it was already supposed to get one */
  60. if (wr == -EPIPE && !sigpipe) {
  61. spin_lock_irqsave(&current->sighand->siglock, flags);
  62. sigdelset(&current->pending.signal, SIGPIPE);
  63. recalc_sigpending();
  64. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  65. }
  66. return (bytes > 0);
  67. }
  68. static void autofs_notify_daemon(struct autofs_sb_info *sbi, struct autofs_wait_queue *wq)
  69. {
  70. struct autofs_packet_missing pkt;
  71. DPRINTK(("autofs_wait: wait id = 0x%08lx, name = ", wq->wait_queue_token));
  72. autofs_say(wq->name,wq->len);
  73. memset(&pkt,0,sizeof pkt); /* For security reasons */
  74. pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
  75. pkt.hdr.type = autofs_ptype_missing;
  76. pkt.wait_queue_token = wq->wait_queue_token;
  77. pkt.len = wq->len;
  78. memcpy(pkt.name, wq->name, pkt.len);
  79. pkt.name[pkt.len] = '\0';
  80. if ( autofs_write(sbi->pipe,&pkt,sizeof(struct autofs_packet_missing)) )
  81. autofs_catatonic_mode(sbi);
  82. }
  83. int autofs_wait(struct autofs_sb_info *sbi, struct qstr *name)
  84. {
  85. struct autofs_wait_queue *wq;
  86. int status;
  87. /* In catatonic mode, we don't wait for nobody */
  88. if ( sbi->catatonic )
  89. return -ENOENT;
  90. /* We shouldn't be able to get here, but just in case */
  91. if ( name->len > NAME_MAX )
  92. return -ENOENT;
  93. for ( wq = sbi->queues ; wq ; wq = wq->next ) {
  94. if ( wq->hash == name->hash &&
  95. wq->len == name->len &&
  96. wq->name && !memcmp(wq->name,name->name,name->len) )
  97. break;
  98. }
  99. if ( !wq ) {
  100. /* Create a new wait queue */
  101. wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
  102. if ( !wq )
  103. return -ENOMEM;
  104. wq->name = kmalloc(name->len,GFP_KERNEL);
  105. if ( !wq->name ) {
  106. kfree(wq);
  107. return -ENOMEM;
  108. }
  109. wq->wait_queue_token = autofs_next_wait_queue++;
  110. init_waitqueue_head(&wq->queue);
  111. wq->hash = name->hash;
  112. wq->len = name->len;
  113. wq->status = -EINTR; /* Status return if interrupted */
  114. memcpy(wq->name, name->name, name->len);
  115. wq->next = sbi->queues;
  116. sbi->queues = wq;
  117. /* autofs_notify_daemon() may block */
  118. wq->wait_ctr = 2;
  119. autofs_notify_daemon(sbi,wq);
  120. } else
  121. wq->wait_ctr++;
  122. /* wq->name is NULL if and only if the lock is already released */
  123. if ( sbi->catatonic ) {
  124. /* We might have slept, so check again for catatonic mode */
  125. wq->status = -ENOENT;
  126. kfree(wq->name);
  127. wq->name = NULL;
  128. }
  129. if ( wq->name ) {
  130. /* Block all but "shutdown" signals while waiting */
  131. sigset_t sigmask;
  132. siginitsetinv(&sigmask, SHUTDOWN_SIGS);
  133. sigprocmask(SIG_BLOCK, &sigmask, &sigmask);
  134. interruptible_sleep_on(&wq->queue);
  135. sigprocmask(SIG_SETMASK, &sigmask, NULL);
  136. } else {
  137. DPRINTK(("autofs_wait: skipped sleeping\n"));
  138. }
  139. status = wq->status;
  140. if ( ! --wq->wait_ctr ) /* Are we the last process to need status? */
  141. kfree(wq);
  142. return status;
  143. }
  144. int autofs_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
  145. {
  146. struct autofs_wait_queue *wq, **wql;
  147. for ( wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next ) {
  148. if ( wq->wait_queue_token == wait_queue_token )
  149. break;
  150. }
  151. if ( !wq )
  152. return -EINVAL;
  153. *wql = wq->next; /* Unlink from chain */
  154. kfree(wq->name);
  155. wq->name = NULL; /* Do not wait on this queue */
  156. wq->status = status;
  157. if ( ! --wq->wait_ctr ) /* Is anyone still waiting for this guy? */
  158. kfree(wq);
  159. else
  160. wake_up(&wq->queue);
  161. return 0;
  162. }