waitq.c 13 KB

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