waitq.c 9.9 KB

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