waitq.c 14 KB

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