waitq.c 14 KB

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