waitq.c 14 KB

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