waitq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/waitq.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. * Copyright 2001-2006 Ian Kent <raven@themaw.net>
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * ------------------------------------------------------------------------- */
  13. #include <linux/slab.h>
  14. #include <linux/time.h>
  15. #include <linux/signal.h>
  16. #include <linux/file.h>
  17. #include "autofs_i.h"
  18. /* We make this a static variable rather than a part of the superblock; it
  19. is better if we don't reassign numbers easily even across filesystems */
  20. static autofs_wqt_t autofs4_next_wait_queue = 1;
  21. /* These are the signals we allow interrupting a pending mount */
  22. #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
  23. void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
  24. {
  25. struct autofs_wait_queue *wq, *nwq;
  26. mutex_lock(&sbi->wq_mutex);
  27. if (sbi->catatonic) {
  28. mutex_unlock(&sbi->wq_mutex);
  29. return;
  30. }
  31. DPRINTK("entering catatonic mode");
  32. sbi->catatonic = 1;
  33. wq = sbi->queues;
  34. sbi->queues = NULL; /* Erase all wait queues */
  35. while (wq) {
  36. nwq = wq->next;
  37. wq->status = -ENOENT; /* Magic is gone - report failure */
  38. kfree(wq->name.name);
  39. wq->name.name = NULL;
  40. wq->wait_ctr--;
  41. wake_up_interruptible(&wq->queue);
  42. wq = nwq;
  43. }
  44. fput(sbi->pipe); /* Close the pipe */
  45. sbi->pipe = NULL;
  46. sbi->pipefd = -1;
  47. mutex_unlock(&sbi->wq_mutex);
  48. }
  49. static int autofs4_write(struct autofs_sb_info *sbi,
  50. struct file *file, const void *addr, int bytes)
  51. {
  52. unsigned long sigpipe, flags;
  53. mm_segment_t fs;
  54. const char *data = (const char *)addr;
  55. ssize_t wr = 0;
  56. sigpipe = sigismember(&current->pending.signal, SIGPIPE);
  57. /* Save pointer to user space and point back to kernel space */
  58. fs = get_fs();
  59. set_fs(KERNEL_DS);
  60. mutex_lock(&sbi->pipe_mutex);
  61. while (bytes &&
  62. (wr = __vfs_write(file,data,bytes,&file->f_pos)) > 0) {
  63. data += wr;
  64. bytes -= wr;
  65. }
  66. mutex_unlock(&sbi->pipe_mutex);
  67. set_fs(fs);
  68. /* Keep the currently executing process from receiving a
  69. SIGPIPE unless it was already supposed to get one */
  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. /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
  77. return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
  78. }
  79. static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
  80. struct autofs_wait_queue *wq,
  81. int type)
  82. {
  83. union {
  84. struct autofs_packet_hdr hdr;
  85. union autofs_packet_union v4_pkt;
  86. union autofs_v5_packet_union v5_pkt;
  87. } pkt;
  88. struct file *pipe = NULL;
  89. size_t pktsz;
  90. int ret;
  91. DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
  92. (unsigned long) wq->wait_queue_token, wq->name.len, wq->name.name, type);
  93. memset(&pkt,0,sizeof pkt); /* For security reasons */
  94. pkt.hdr.proto_version = sbi->version;
  95. pkt.hdr.type = type;
  96. switch (type) {
  97. /* Kernel protocol v4 missing and expire packets */
  98. case autofs_ptype_missing:
  99. {
  100. struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
  101. pktsz = sizeof(*mp);
  102. mp->wait_queue_token = wq->wait_queue_token;
  103. mp->len = wq->name.len;
  104. memcpy(mp->name, wq->name.name, wq->name.len);
  105. mp->name[wq->name.len] = '\0';
  106. break;
  107. }
  108. case autofs_ptype_expire_multi:
  109. {
  110. struct autofs_packet_expire_multi *ep = &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. printk("autofs4_notify_daemon: 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. switch (ret = autofs4_write(sbi, pipe, &pkt, pktsz)) {
  150. case 0:
  151. break;
  152. case -ENOMEM:
  153. case -ERESTARTSYS:
  154. /* Just fail this one */
  155. autofs4_wait_release(sbi, wq->wait_queue_token, ret);
  156. break;
  157. default:
  158. autofs4_catatonic_mode(sbi);
  159. break;
  160. }
  161. fput(pipe);
  162. }
  163. static int autofs4_getpath(struct autofs_sb_info *sbi,
  164. struct dentry *dentry, char **name)
  165. {
  166. struct dentry *root = sbi->sb->s_root;
  167. struct dentry *tmp;
  168. char *buf;
  169. char *p;
  170. int len;
  171. unsigned seq;
  172. rename_retry:
  173. buf = *name;
  174. len = 0;
  175. seq = read_seqbegin(&rename_lock);
  176. rcu_read_lock();
  177. spin_lock(&sbi->fs_lock);
  178. for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
  179. len += tmp->d_name.len + 1;
  180. if (!len || --len > NAME_MAX) {
  181. spin_unlock(&sbi->fs_lock);
  182. rcu_read_unlock();
  183. if (read_seqretry(&rename_lock, seq))
  184. goto rename_retry;
  185. return 0;
  186. }
  187. *(buf + len) = '\0';
  188. p = buf + len - dentry->d_name.len;
  189. strncpy(p, dentry->d_name.name, dentry->d_name.len);
  190. for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
  191. *(--p) = '/';
  192. p -= tmp->d_name.len;
  193. strncpy(p, tmp->d_name.name, tmp->d_name.len);
  194. }
  195. spin_unlock(&sbi->fs_lock);
  196. rcu_read_unlock();
  197. if (read_seqretry(&rename_lock, seq))
  198. goto rename_retry;
  199. return len;
  200. }
  201. static struct autofs_wait_queue *
  202. autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
  203. {
  204. struct autofs_wait_queue *wq;
  205. for (wq = sbi->queues; wq; wq = wq->next) {
  206. if (wq->name.hash == qstr->hash &&
  207. wq->name.len == qstr->len &&
  208. wq->name.name &&
  209. !memcmp(wq->name.name, qstr->name, qstr->len))
  210. break;
  211. }
  212. return wq;
  213. }
  214. /*
  215. * Check if we have a valid request.
  216. * Returns
  217. * 1 if the request should continue.
  218. * In this case we can return an autofs_wait_queue entry if one is
  219. * found or NULL to idicate a new wait needs to be created.
  220. * 0 or a negative errno if the request shouldn't continue.
  221. */
  222. static int validate_request(struct autofs_wait_queue **wait,
  223. struct autofs_sb_info *sbi,
  224. struct qstr *qstr,
  225. struct dentry*dentry, enum autofs_notify notify)
  226. {
  227. struct autofs_wait_queue *wq;
  228. struct autofs_info *ino;
  229. if (sbi->catatonic)
  230. return -ENOENT;
  231. /* Wait in progress, continue; */
  232. wq = autofs4_find_wait(sbi, qstr);
  233. if (wq) {
  234. *wait = wq;
  235. return 1;
  236. }
  237. *wait = NULL;
  238. /* If we don't yet have any info this is a new request */
  239. ino = autofs4_dentry_ino(dentry);
  240. if (!ino)
  241. return 1;
  242. /*
  243. * If we've been asked to wait on an existing expire (NFY_NONE)
  244. * but there is no wait in the queue ...
  245. */
  246. if (notify == NFY_NONE) {
  247. /*
  248. * Either we've betean the pending expire to post it's
  249. * wait or it finished while we waited on the mutex.
  250. * So we need to wait till either, the wait appears
  251. * or the expire finishes.
  252. */
  253. while (ino->flags & AUTOFS_INF_EXPIRING) {
  254. mutex_unlock(&sbi->wq_mutex);
  255. schedule_timeout_interruptible(HZ/10);
  256. if (mutex_lock_interruptible(&sbi->wq_mutex))
  257. return -EINTR;
  258. if (sbi->catatonic)
  259. return -ENOENT;
  260. wq = autofs4_find_wait(sbi, qstr);
  261. if (wq) {
  262. *wait = wq;
  263. return 1;
  264. }
  265. }
  266. /*
  267. * Not ideal but the status has already gone. Of the two
  268. * cases where we wait on NFY_NONE neither depend on the
  269. * return status of the wait.
  270. */
  271. return 0;
  272. }
  273. /*
  274. * If we've been asked to trigger a mount and the request
  275. * completed while we waited on the mutex ...
  276. */
  277. if (notify == NFY_MOUNT) {
  278. struct dentry *new = NULL;
  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_really_is_positive(dentry) && d_unhashed(dentry)) {
  289. struct dentry *parent = dentry->d_parent;
  290. new = d_lookup(parent, &dentry->d_name);
  291. if (new)
  292. dentry = new;
  293. }
  294. }
  295. if (have_submounts(dentry))
  296. valid = 0;
  297. if (new)
  298. dput(new);
  299. return valid;
  300. }
  301. return 1;
  302. }
  303. int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
  304. enum autofs_notify notify)
  305. {
  306. struct autofs_wait_queue *wq;
  307. struct qstr qstr;
  308. char *name;
  309. int status, ret, type;
  310. pid_t pid;
  311. pid_t tgid;
  312. /* In catatonic mode, we don't wait for nobody */
  313. if (sbi->catatonic)
  314. return -ENOENT;
  315. /*
  316. * Try translating pids to the namespace of the daemon.
  317. *
  318. * Zero means failure: we are in an unrelated pid namespace.
  319. */
  320. pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
  321. tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
  322. if (pid == 0 || tgid == 0)
  323. return -ENOENT;
  324. if (d_really_is_negative(dentry)) {
  325. /*
  326. * A wait for a negative dentry is invalid for certain
  327. * cases. A direct or offset mount "always" has its mount
  328. * point directory created and so the request dentry must
  329. * be positive or the map key doesn't exist. The situation
  330. * is very similar for indirect mounts except only dentrys
  331. * in the root of the autofs file system may be negative.
  332. */
  333. if (autofs_type_trigger(sbi->type))
  334. return -ENOENT;
  335. else if (!IS_ROOT(dentry->d_parent))
  336. return -ENOENT;
  337. }
  338. name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  339. if (!name)
  340. return -ENOMEM;
  341. /* If this is a direct mount request create a dummy name */
  342. if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
  343. qstr.len = sprintf(name, "%p", dentry);
  344. else {
  345. qstr.len = autofs4_getpath(sbi, dentry, &name);
  346. if (!qstr.len) {
  347. kfree(name);
  348. return -ENOENT;
  349. }
  350. }
  351. qstr.name = name;
  352. qstr.hash = full_name_hash(name, qstr.len);
  353. if (mutex_lock_interruptible(&sbi->wq_mutex)) {
  354. kfree(qstr.name);
  355. return -EINTR;
  356. }
  357. ret = validate_request(&wq, sbi, &qstr, dentry, notify);
  358. if (ret <= 0) {
  359. if (ret != -EINTR)
  360. mutex_unlock(&sbi->wq_mutex);
  361. kfree(qstr.name);
  362. return ret;
  363. }
  364. if (!wq) {
  365. /* Create a new wait queue */
  366. wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
  367. if (!wq) {
  368. kfree(qstr.name);
  369. mutex_unlock(&sbi->wq_mutex);
  370. return -ENOMEM;
  371. }
  372. wq->wait_queue_token = autofs4_next_wait_queue;
  373. if (++autofs4_next_wait_queue == 0)
  374. autofs4_next_wait_queue = 1;
  375. wq->next = sbi->queues;
  376. sbi->queues = wq;
  377. init_waitqueue_head(&wq->queue);
  378. memcpy(&wq->name, &qstr, sizeof(struct qstr));
  379. wq->dev = autofs4_get_dev(sbi);
  380. wq->ino = autofs4_get_ino(sbi);
  381. wq->uid = current_uid();
  382. wq->gid = current_gid();
  383. wq->pid = pid;
  384. wq->tgid = tgid;
  385. wq->status = -EINTR; /* Status return if interrupted */
  386. wq->wait_ctr = 2;
  387. if (sbi->version < 5) {
  388. if (notify == NFY_MOUNT)
  389. type = autofs_ptype_missing;
  390. else
  391. type = autofs_ptype_expire_multi;
  392. } else {
  393. if (notify == NFY_MOUNT)
  394. type = autofs_type_trigger(sbi->type) ?
  395. autofs_ptype_missing_direct :
  396. autofs_ptype_missing_indirect;
  397. else
  398. type = autofs_type_trigger(sbi->type) ?
  399. autofs_ptype_expire_direct :
  400. autofs_ptype_expire_indirect;
  401. }
  402. DPRINTK("new 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. /* autofs4_notify_daemon() may block; it will unlock ->wq_mutex */
  406. autofs4_notify_daemon(sbi, wq, type);
  407. } else {
  408. wq->wait_ctr++;
  409. DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
  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. if (wq->name.name) {
  420. /* Block all but "shutdown" signals while waiting */
  421. sigset_t oldset;
  422. unsigned long irqflags;
  423. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  424. oldset = current->blocked;
  425. siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
  426. recalc_sigpending();
  427. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  428. wait_event_interruptible(wq->queue, wq->name.name == NULL);
  429. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  430. current->blocked = oldset;
  431. recalc_sigpending();
  432. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  433. } else {
  434. DPRINTK("skipped sleeping");
  435. }
  436. status = wq->status;
  437. /*
  438. * For direct and offset mounts we need to track the requester's
  439. * uid and gid in the dentry info struct. This is so it can be
  440. * supplied, on request, by the misc device ioctl interface.
  441. * This is needed during daemon resatart when reconnecting
  442. * to existing, active, autofs mounts. The uid and gid (and
  443. * related string values) may be used for macro substitution
  444. * in autofs mount maps.
  445. */
  446. if (!status) {
  447. struct autofs_info *ino;
  448. struct dentry *de = NULL;
  449. /* direct mount or browsable map */
  450. ino = autofs4_dentry_ino(dentry);
  451. if (!ino) {
  452. /* If not lookup actual dentry used */
  453. de = d_lookup(dentry->d_parent, &dentry->d_name);
  454. if (de)
  455. ino = autofs4_dentry_ino(de);
  456. }
  457. /* Set mount requester */
  458. if (ino) {
  459. spin_lock(&sbi->fs_lock);
  460. ino->uid = wq->uid;
  461. ino->gid = wq->gid;
  462. spin_unlock(&sbi->fs_lock);
  463. }
  464. if (de)
  465. dput(de);
  466. }
  467. /* Are we the last process to need status? */
  468. mutex_lock(&sbi->wq_mutex);
  469. if (!--wq->wait_ctr)
  470. kfree(wq);
  471. mutex_unlock(&sbi->wq_mutex);
  472. return status;
  473. }
  474. int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
  475. {
  476. struct autofs_wait_queue *wq, **wql;
  477. mutex_lock(&sbi->wq_mutex);
  478. for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
  479. if (wq->wait_queue_token == wait_queue_token)
  480. break;
  481. }
  482. if (!wq) {
  483. mutex_unlock(&sbi->wq_mutex);
  484. return -EINVAL;
  485. }
  486. *wql = wq->next; /* Unlink from chain */
  487. kfree(wq->name.name);
  488. wq->name.name = NULL; /* Do not wait on this queue */
  489. wq->status = status;
  490. wake_up_interruptible(&wq->queue);
  491. if (!--wq->wait_ctr)
  492. kfree(wq);
  493. mutex_unlock(&sbi->wq_mutex);
  494. return 0;
  495. }