waitq.c 9.9 KB

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