waitq.c 10 KB

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