waitq.c 12 KB

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