waitq.c 14 KB

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