waitq.c 14 KB

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