waitq.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/waitq.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. * Copyright 2001-2006 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. switch (type) {
  83. /* Kernel protocol v4 missing and expire packets */
  84. case autofs_ptype_missing:
  85. {
  86. struct autofs_packet_missing *mp = &pkt.missing;
  87. pktsz = sizeof(*mp);
  88. mp->wait_queue_token = wq->wait_queue_token;
  89. mp->len = wq->len;
  90. memcpy(mp->name, wq->name, wq->len);
  91. mp->name[wq->len] = '\0';
  92. break;
  93. }
  94. case autofs_ptype_expire_multi:
  95. {
  96. struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
  97. pktsz = sizeof(*ep);
  98. ep->wait_queue_token = wq->wait_queue_token;
  99. ep->len = wq->len;
  100. memcpy(ep->name, wq->name, wq->len);
  101. ep->name[wq->len] = '\0';
  102. break;
  103. }
  104. /*
  105. * Kernel protocol v5 packet for handling indirect and direct
  106. * mount missing and expire requests
  107. */
  108. case autofs_ptype_missing_indirect:
  109. case autofs_ptype_expire_indirect:
  110. case autofs_ptype_missing_direct:
  111. case autofs_ptype_expire_direct:
  112. {
  113. struct autofs_v5_packet *packet = &pkt.v5_packet;
  114. pktsz = sizeof(*packet);
  115. packet->wait_queue_token = wq->wait_queue_token;
  116. packet->len = wq->len;
  117. memcpy(packet->name, wq->name, wq->len);
  118. packet->name[wq->len] = '\0';
  119. packet->dev = wq->dev;
  120. packet->ino = wq->ino;
  121. packet->uid = wq->uid;
  122. packet->gid = wq->gid;
  123. packet->pid = wq->pid;
  124. packet->tgid = wq->tgid;
  125. break;
  126. }
  127. default:
  128. printk("autofs4_notify_daemon: bad type %d!\n", type);
  129. return;
  130. }
  131. if (autofs4_write(sbi->pipe, &pkt, pktsz))
  132. autofs4_catatonic_mode(sbi);
  133. }
  134. static int autofs4_getpath(struct autofs_sb_info *sbi,
  135. struct dentry *dentry, char **name)
  136. {
  137. struct dentry *root = sbi->sb->s_root;
  138. struct dentry *tmp;
  139. char *buf = *name;
  140. char *p;
  141. int len = 0;
  142. spin_lock(&dcache_lock);
  143. for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
  144. len += tmp->d_name.len + 1;
  145. if (--len > NAME_MAX) {
  146. spin_unlock(&dcache_lock);
  147. return 0;
  148. }
  149. *(buf + len) = '\0';
  150. p = buf + len - dentry->d_name.len;
  151. strncpy(p, dentry->d_name.name, dentry->d_name.len);
  152. for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
  153. *(--p) = '/';
  154. p -= tmp->d_name.len;
  155. strncpy(p, tmp->d_name.name, tmp->d_name.len);
  156. }
  157. spin_unlock(&dcache_lock);
  158. return len;
  159. }
  160. static struct autofs_wait_queue *
  161. autofs4_find_wait(struct autofs_sb_info *sbi,
  162. char *name, unsigned int hash, unsigned int len)
  163. {
  164. struct autofs_wait_queue *wq;
  165. for (wq = sbi->queues; wq; wq = wq->next) {
  166. if (wq->hash == hash &&
  167. wq->len == len &&
  168. wq->name && !memcmp(wq->name, name, len))
  169. break;
  170. }
  171. return wq;
  172. }
  173. int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
  174. enum autofs_notify notify)
  175. {
  176. struct autofs_info *ino;
  177. struct autofs_wait_queue *wq;
  178. char *name;
  179. unsigned int len = 0;
  180. unsigned int hash = 0;
  181. int status, type;
  182. /* In catatonic mode, we don't wait for nobody */
  183. if (sbi->catatonic)
  184. return -ENOENT;
  185. name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  186. if (!name)
  187. return -ENOMEM;
  188. /* If this is a direct mount request create a dummy name */
  189. if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
  190. len = sprintf(name, "%p", dentry);
  191. else {
  192. len = autofs4_getpath(sbi, dentry, &name);
  193. if (!len) {
  194. kfree(name);
  195. return -ENOENT;
  196. }
  197. }
  198. hash = full_name_hash(name, len);
  199. if (mutex_lock_interruptible(&sbi->wq_mutex)) {
  200. kfree(name);
  201. return -EINTR;
  202. }
  203. wq = autofs4_find_wait(sbi, name, hash, len);
  204. ino = autofs4_dentry_ino(dentry);
  205. if (!wq && ino && notify == NFY_NONE) {
  206. /*
  207. * Either we've betean the pending expire to post it's
  208. * wait or it finished while we waited on the mutex.
  209. * So we need to wait till either, the wait appears
  210. * or the expire finishes.
  211. */
  212. while (ino->flags & AUTOFS_INF_EXPIRING) {
  213. mutex_unlock(&sbi->wq_mutex);
  214. schedule_timeout_interruptible(HZ/10);
  215. if (mutex_lock_interruptible(&sbi->wq_mutex)) {
  216. kfree(name);
  217. return -EINTR;
  218. }
  219. wq = autofs4_find_wait(sbi, name, hash, len);
  220. if (wq)
  221. break;
  222. }
  223. /*
  224. * Not ideal but the status has already gone. Of the two
  225. * cases where we wait on NFY_NONE neither depend on the
  226. * return status of the wait.
  227. */
  228. if (!wq) {
  229. kfree(name);
  230. mutex_unlock(&sbi->wq_mutex);
  231. return 0;
  232. }
  233. }
  234. if (!wq) {
  235. /* Create a new wait queue */
  236. wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
  237. if (!wq) {
  238. kfree(name);
  239. mutex_unlock(&sbi->wq_mutex);
  240. return -ENOMEM;
  241. }
  242. wq->wait_queue_token = autofs4_next_wait_queue;
  243. if (++autofs4_next_wait_queue == 0)
  244. autofs4_next_wait_queue = 1;
  245. wq->next = sbi->queues;
  246. sbi->queues = wq;
  247. init_waitqueue_head(&wq->queue);
  248. wq->hash = hash;
  249. wq->name = name;
  250. wq->len = len;
  251. wq->dev = autofs4_get_dev(sbi);
  252. wq->ino = autofs4_get_ino(sbi);
  253. wq->uid = current->uid;
  254. wq->gid = current->gid;
  255. wq->pid = current->pid;
  256. wq->tgid = current->tgid;
  257. wq->status = -EINTR; /* Status return if interrupted */
  258. atomic_set(&wq->wait_ctr, 2);
  259. mutex_unlock(&sbi->wq_mutex);
  260. if (sbi->version < 5) {
  261. if (notify == NFY_MOUNT)
  262. type = autofs_ptype_missing;
  263. else
  264. type = autofs_ptype_expire_multi;
  265. } else {
  266. if (notify == NFY_MOUNT)
  267. type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
  268. autofs_ptype_missing_direct :
  269. autofs_ptype_missing_indirect;
  270. else
  271. type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
  272. autofs_ptype_expire_direct :
  273. autofs_ptype_expire_indirect;
  274. }
  275. DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
  276. (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
  277. /* autofs4_notify_daemon() may block */
  278. autofs4_notify_daemon(sbi, wq, type);
  279. } else {
  280. atomic_inc(&wq->wait_ctr);
  281. mutex_unlock(&sbi->wq_mutex);
  282. kfree(name);
  283. DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
  284. (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
  285. }
  286. /* wq->name is NULL if and only if the lock is already released */
  287. if (sbi->catatonic) {
  288. /* We might have slept, so check again for catatonic mode */
  289. wq->status = -ENOENT;
  290. kfree(wq->name);
  291. wq->name = NULL;
  292. }
  293. if (wq->name) {
  294. /* Block all but "shutdown" signals while waiting */
  295. sigset_t oldset;
  296. unsigned long irqflags;
  297. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  298. oldset = current->blocked;
  299. siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
  300. recalc_sigpending();
  301. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  302. wait_event_interruptible(wq->queue, wq->name == NULL);
  303. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  304. current->blocked = oldset;
  305. recalc_sigpending();
  306. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  307. } else {
  308. DPRINTK("skipped sleeping");
  309. }
  310. status = wq->status;
  311. /* Are we the last process to need status? */
  312. if (atomic_dec_and_test(&wq->wait_ctr))
  313. kfree(wq);
  314. return status;
  315. }
  316. int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
  317. {
  318. struct autofs_wait_queue *wq, **wql;
  319. mutex_lock(&sbi->wq_mutex);
  320. for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) {
  321. if (wq->wait_queue_token == wait_queue_token)
  322. break;
  323. }
  324. if (!wq) {
  325. mutex_unlock(&sbi->wq_mutex);
  326. return -EINVAL;
  327. }
  328. *wql = wq->next; /* Unlink from chain */
  329. mutex_unlock(&sbi->wq_mutex);
  330. kfree(wq->name);
  331. wq->name = NULL; /* Do not wait on this queue */
  332. wq->status = status;
  333. if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
  334. kfree(wq);
  335. else
  336. wake_up_interruptible(&wq->queue);
  337. return 0;
  338. }