waitq.c 13 KB

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