waitq.c 14 KB

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