waitq.c 13 KB

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