waitq.c 14 KB

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