waitq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. wq->wait_ctr--;
  43. wake_up_interruptible(&wq->queue);
  44. wq = nwq;
  45. }
  46. fput(sbi->pipe); /* Close the pipe */
  47. sbi->pipe = NULL;
  48. sbi->pipefd = -1;
  49. mutex_unlock(&sbi->wq_mutex);
  50. }
  51. static int autofs4_write(struct file *file, const void *addr, int bytes)
  52. {
  53. unsigned long sigpipe, flags;
  54. mm_segment_t fs;
  55. const char *data = (const char *)addr;
  56. ssize_t wr = 0;
  57. /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
  58. sigpipe = sigismember(&current->pending.signal, SIGPIPE);
  59. /* Save pointer to user space and point back to kernel space */
  60. fs = get_fs();
  61. set_fs(KERNEL_DS);
  62. while (bytes &&
  63. (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
  64. data += wr;
  65. bytes -= wr;
  66. }
  67. set_fs(fs);
  68. /* Keep the currently executing process from receiving a
  69. SIGPIPE unless it was already supposed to get one */
  70. if (wr == -EPIPE && !sigpipe) {
  71. spin_lock_irqsave(&current->sighand->siglock, flags);
  72. sigdelset(&current->pending.signal, SIGPIPE);
  73. recalc_sigpending();
  74. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  75. }
  76. return (bytes > 0);
  77. }
  78. static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
  79. struct autofs_wait_queue *wq,
  80. int type)
  81. {
  82. union {
  83. struct autofs_packet_hdr hdr;
  84. union autofs_packet_union v4_pkt;
  85. union autofs_v5_packet_union v5_pkt;
  86. } pkt;
  87. struct file *pipe = NULL;
  88. size_t pktsz;
  89. DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
  90. wq->wait_queue_token, wq->name.len, wq->name.name, type);
  91. memset(&pkt,0,sizeof pkt); /* For security reasons */
  92. pkt.hdr.proto_version = sbi->version;
  93. pkt.hdr.type = type;
  94. switch (type) {
  95. /* Kernel protocol v4 missing and expire packets */
  96. case autofs_ptype_missing:
  97. {
  98. struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
  99. pktsz = sizeof(*mp);
  100. mp->wait_queue_token = wq->wait_queue_token;
  101. mp->len = wq->name.len;
  102. memcpy(mp->name, wq->name.name, wq->name.len);
  103. mp->name[wq->name.len] = '\0';
  104. break;
  105. }
  106. case autofs_ptype_expire_multi:
  107. {
  108. struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
  109. pktsz = sizeof(*ep);
  110. ep->wait_queue_token = wq->wait_queue_token;
  111. ep->len = wq->name.len;
  112. memcpy(ep->name, wq->name.name, wq->name.len);
  113. ep->name[wq->name.len] = '\0';
  114. break;
  115. }
  116. /*
  117. * Kernel protocol v5 packet for handling indirect and direct
  118. * mount missing and expire requests
  119. */
  120. case autofs_ptype_missing_indirect:
  121. case autofs_ptype_expire_indirect:
  122. case autofs_ptype_missing_direct:
  123. case autofs_ptype_expire_direct:
  124. {
  125. struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
  126. pktsz = sizeof(*packet);
  127. packet->wait_queue_token = wq->wait_queue_token;
  128. packet->len = wq->name.len;
  129. memcpy(packet->name, wq->name.name, wq->name.len);
  130. packet->name[wq->name.len] = '\0';
  131. packet->dev = wq->dev;
  132. packet->ino = wq->ino;
  133. packet->uid = wq->uid;
  134. packet->gid = wq->gid;
  135. packet->pid = wq->pid;
  136. packet->tgid = wq->tgid;
  137. break;
  138. }
  139. default:
  140. printk("autofs4_notify_daemon: bad type %d!\n", type);
  141. return;
  142. }
  143. /* Check if we have become catatonic */
  144. mutex_lock(&sbi->wq_mutex);
  145. if (!sbi->catatonic) {
  146. pipe = sbi->pipe;
  147. get_file(pipe);
  148. }
  149. mutex_unlock(&sbi->wq_mutex);
  150. if (pipe) {
  151. if (autofs4_write(pipe, &pkt, pktsz))
  152. autofs4_catatonic_mode(sbi);
  153. fput(pipe);
  154. }
  155. }
  156. static int autofs4_getpath(struct autofs_sb_info *sbi,
  157. struct dentry *dentry, char **name)
  158. {
  159. struct dentry *root = sbi->sb->s_root;
  160. struct dentry *tmp;
  161. char *buf;
  162. char *p;
  163. int len;
  164. unsigned seq;
  165. rename_retry:
  166. buf = *name;
  167. len = 0;
  168. seq = read_seqbegin(&rename_lock);
  169. rcu_read_lock();
  170. spin_lock(&autofs4_lock);
  171. for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
  172. len += tmp->d_name.len + 1;
  173. if (!len || --len > NAME_MAX) {
  174. spin_unlock(&autofs4_lock);
  175. rcu_read_unlock();
  176. if (read_seqretry(&rename_lock, seq))
  177. goto rename_retry;
  178. return 0;
  179. }
  180. *(buf + len) = '\0';
  181. p = buf + len - dentry->d_name.len;
  182. strncpy(p, dentry->d_name.name, dentry->d_name.len);
  183. for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
  184. *(--p) = '/';
  185. p -= tmp->d_name.len;
  186. strncpy(p, tmp->d_name.name, tmp->d_name.len);
  187. }
  188. spin_unlock(&autofs4_lock);
  189. rcu_read_unlock();
  190. if (read_seqretry(&rename_lock, seq))
  191. goto rename_retry;
  192. return len;
  193. }
  194. static struct autofs_wait_queue *
  195. autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
  196. {
  197. struct autofs_wait_queue *wq;
  198. for (wq = sbi->queues; wq; wq = wq->next) {
  199. if (wq->name.hash == qstr->hash &&
  200. wq->name.len == qstr->len &&
  201. wq->name.name &&
  202. !memcmp(wq->name.name, qstr->name, qstr->len))
  203. break;
  204. }
  205. return wq;
  206. }
  207. /*
  208. * Check if we have a valid request.
  209. * Returns
  210. * 1 if the request should continue.
  211. * In this case we can return an autofs_wait_queue entry if one is
  212. * found or NULL to idicate a new wait needs to be created.
  213. * 0 or a negative errno if the request shouldn't continue.
  214. */
  215. static int validate_request(struct autofs_wait_queue **wait,
  216. struct autofs_sb_info *sbi,
  217. struct qstr *qstr,
  218. struct dentry*dentry, enum autofs_notify notify)
  219. {
  220. struct autofs_wait_queue *wq;
  221. struct autofs_info *ino;
  222. /* Wait in progress, continue; */
  223. wq = autofs4_find_wait(sbi, qstr);
  224. if (wq) {
  225. *wait = wq;
  226. return 1;
  227. }
  228. *wait = NULL;
  229. /* If we don't yet have any info this is a new request */
  230. ino = autofs4_dentry_ino(dentry);
  231. if (!ino)
  232. return 1;
  233. /*
  234. * If we've been asked to wait on an existing expire (NFY_NONE)
  235. * but there is no wait in the queue ...
  236. */
  237. if (notify == NFY_NONE) {
  238. /*
  239. * Either we've betean the pending expire to post it's
  240. * wait or it finished while we waited on the mutex.
  241. * So we need to wait till either, the wait appears
  242. * or the expire finishes.
  243. */
  244. while (ino->flags & AUTOFS_INF_EXPIRING) {
  245. mutex_unlock(&sbi->wq_mutex);
  246. schedule_timeout_interruptible(HZ/10);
  247. if (mutex_lock_interruptible(&sbi->wq_mutex))
  248. return -EINTR;
  249. wq = autofs4_find_wait(sbi, qstr);
  250. if (wq) {
  251. *wait = wq;
  252. return 1;
  253. }
  254. }
  255. /*
  256. * Not ideal but the status has already gone. Of the two
  257. * cases where we wait on NFY_NONE neither depend on the
  258. * return status of the wait.
  259. */
  260. return 0;
  261. }
  262. /*
  263. * If we've been asked to trigger a mount and the request
  264. * completed while we waited on the mutex ...
  265. */
  266. if (notify == NFY_MOUNT) {
  267. /*
  268. * If the dentry was successfully mounted while we slept
  269. * on the wait queue mutex we can return success. If it
  270. * isn't mounted (doesn't have submounts for the case of
  271. * a multi-mount with no mount at it's base) we can
  272. * continue on and create a new request.
  273. */
  274. if (have_submounts(dentry))
  275. return 0;
  276. }
  277. return 1;
  278. }
  279. int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
  280. enum autofs_notify notify)
  281. {
  282. struct autofs_wait_queue *wq;
  283. struct qstr qstr;
  284. char *name;
  285. int status, ret, type;
  286. /* In catatonic mode, we don't wait for nobody */
  287. if (sbi->catatonic)
  288. return -ENOENT;
  289. if (!dentry->d_inode) {
  290. /*
  291. * A wait for a negative dentry is invalid for certain
  292. * cases. A direct or offset mount "always" has its mount
  293. * point directory created and so the request dentry must
  294. * be positive or the map key doesn't exist. The situation
  295. * is very similar for indirect mounts except only dentrys
  296. * in the root of the autofs file system may be negative.
  297. */
  298. if (autofs_type_trigger(sbi->type))
  299. return -ENOENT;
  300. else if (!IS_ROOT(dentry->d_parent))
  301. return -ENOENT;
  302. }
  303. name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  304. if (!name)
  305. return -ENOMEM;
  306. /* If this is a direct mount request create a dummy name */
  307. if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
  308. qstr.len = sprintf(name, "%p", dentry);
  309. else {
  310. qstr.len = autofs4_getpath(sbi, dentry, &name);
  311. if (!qstr.len) {
  312. kfree(name);
  313. return -ENOENT;
  314. }
  315. }
  316. qstr.name = name;
  317. qstr.hash = full_name_hash(name, qstr.len);
  318. if (mutex_lock_interruptible(&sbi->wq_mutex)) {
  319. kfree(qstr.name);
  320. return -EINTR;
  321. }
  322. ret = validate_request(&wq, sbi, &qstr, dentry, notify);
  323. if (ret <= 0) {
  324. if (ret == 0)
  325. mutex_unlock(&sbi->wq_mutex);
  326. kfree(qstr.name);
  327. return ret;
  328. }
  329. if (!wq) {
  330. /* Create a new wait queue */
  331. wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
  332. if (!wq) {
  333. kfree(qstr.name);
  334. mutex_unlock(&sbi->wq_mutex);
  335. return -ENOMEM;
  336. }
  337. wq->wait_queue_token = autofs4_next_wait_queue;
  338. if (++autofs4_next_wait_queue == 0)
  339. autofs4_next_wait_queue = 1;
  340. wq->next = sbi->queues;
  341. sbi->queues = wq;
  342. init_waitqueue_head(&wq->queue);
  343. memcpy(&wq->name, &qstr, sizeof(struct qstr));
  344. wq->dev = autofs4_get_dev(sbi);
  345. wq->ino = autofs4_get_ino(sbi);
  346. wq->uid = current_uid();
  347. wq->gid = current_gid();
  348. wq->pid = current->pid;
  349. wq->tgid = current->tgid;
  350. wq->status = -EINTR; /* Status return if interrupted */
  351. wq->wait_ctr = 2;
  352. mutex_unlock(&sbi->wq_mutex);
  353. if (sbi->version < 5) {
  354. if (notify == NFY_MOUNT)
  355. type = autofs_ptype_missing;
  356. else
  357. type = autofs_ptype_expire_multi;
  358. } else {
  359. if (notify == NFY_MOUNT)
  360. type = autofs_type_trigger(sbi->type) ?
  361. autofs_ptype_missing_direct :
  362. autofs_ptype_missing_indirect;
  363. else
  364. type = autofs_type_trigger(sbi->type) ?
  365. autofs_ptype_expire_direct :
  366. autofs_ptype_expire_indirect;
  367. }
  368. DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
  369. (unsigned long) wq->wait_queue_token, wq->name.len,
  370. wq->name.name, notify);
  371. /* autofs4_notify_daemon() may block */
  372. autofs4_notify_daemon(sbi, wq, type);
  373. } else {
  374. wq->wait_ctr++;
  375. mutex_unlock(&sbi->wq_mutex);
  376. kfree(qstr.name);
  377. DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
  378. (unsigned long) wq->wait_queue_token, wq->name.len,
  379. wq->name.name, notify);
  380. }
  381. /*
  382. * wq->name.name is NULL iff the lock is already released
  383. * or the mount has been made catatonic.
  384. */
  385. if (wq->name.name) {
  386. /* Block all but "shutdown" signals while waiting */
  387. sigset_t oldset;
  388. unsigned long irqflags;
  389. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  390. oldset = current->blocked;
  391. siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
  392. recalc_sigpending();
  393. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  394. wait_event_interruptible(wq->queue, wq->name.name == NULL);
  395. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  396. current->blocked = oldset;
  397. recalc_sigpending();
  398. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  399. } else {
  400. DPRINTK("skipped sleeping");
  401. }
  402. status = wq->status;
  403. /*
  404. * For direct and offset mounts we need to track the requester's
  405. * uid and gid in the dentry info struct. This is so it can be
  406. * supplied, on request, by the misc device ioctl interface.
  407. * This is needed during daemon resatart when reconnecting
  408. * to existing, active, autofs mounts. The uid and gid (and
  409. * related string values) may be used for macro substitution
  410. * in autofs mount maps.
  411. */
  412. if (!status) {
  413. struct autofs_info *ino;
  414. struct dentry *de = NULL;
  415. /* direct mount or browsable map */
  416. ino = autofs4_dentry_ino(dentry);
  417. if (!ino) {
  418. /* If not lookup actual dentry used */
  419. de = d_lookup(dentry->d_parent, &dentry->d_name);
  420. if (de)
  421. ino = autofs4_dentry_ino(de);
  422. }
  423. /* Set mount requester */
  424. if (ino) {
  425. spin_lock(&sbi->fs_lock);
  426. ino->uid = wq->uid;
  427. ino->gid = wq->gid;
  428. spin_unlock(&sbi->fs_lock);
  429. }
  430. if (de)
  431. dput(de);
  432. }
  433. /* Are we the last process to need status? */
  434. mutex_lock(&sbi->wq_mutex);
  435. if (!--wq->wait_ctr)
  436. kfree(wq);
  437. mutex_unlock(&sbi->wq_mutex);
  438. return status;
  439. }
  440. int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
  441. {
  442. struct autofs_wait_queue *wq, **wql;
  443. mutex_lock(&sbi->wq_mutex);
  444. for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
  445. if (wq->wait_queue_token == wait_queue_token)
  446. break;
  447. }
  448. if (!wq) {
  449. mutex_unlock(&sbi->wq_mutex);
  450. return -EINVAL;
  451. }
  452. *wql = wq->next; /* Unlink from chain */
  453. kfree(wq->name.name);
  454. wq->name.name = NULL; /* Do not wait on this queue */
  455. wq->status = status;
  456. wake_up_interruptible(&wq->queue);
  457. if (!--wq->wait_ctr)
  458. kfree(wq);
  459. mutex_unlock(&sbi->wq_mutex);
  460. return 0;
  461. }