waitq.c 14 KB

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