waitq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 = file->f_op->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. mutex_lock(&sbi->wq_mutex);
  95. /* Check if we have become catatonic */
  96. if (sbi->catatonic) {
  97. mutex_unlock(&sbi->wq_mutex);
  98. return;
  99. }
  100. switch (type) {
  101. /* Kernel protocol v4 missing and expire packets */
  102. case autofs_ptype_missing:
  103. {
  104. struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
  105. pktsz = sizeof(*mp);
  106. mp->wait_queue_token = wq->wait_queue_token;
  107. mp->len = wq->name.len;
  108. memcpy(mp->name, wq->name.name, wq->name.len);
  109. mp->name[wq->name.len] = '\0';
  110. break;
  111. }
  112. case autofs_ptype_expire_multi:
  113. {
  114. struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
  115. pktsz = sizeof(*ep);
  116. ep->wait_queue_token = wq->wait_queue_token;
  117. ep->len = wq->name.len;
  118. memcpy(ep->name, wq->name.name, wq->name.len);
  119. ep->name[wq->name.len] = '\0';
  120. break;
  121. }
  122. /*
  123. * Kernel protocol v5 packet for handling indirect and direct
  124. * mount missing and expire requests
  125. */
  126. case autofs_ptype_missing_indirect:
  127. case autofs_ptype_expire_indirect:
  128. case autofs_ptype_missing_direct:
  129. case autofs_ptype_expire_direct:
  130. {
  131. struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
  132. struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
  133. pktsz = sizeof(*packet);
  134. packet->wait_queue_token = wq->wait_queue_token;
  135. packet->len = wq->name.len;
  136. memcpy(packet->name, wq->name.name, wq->name.len);
  137. packet->name[wq->name.len] = '\0';
  138. packet->dev = wq->dev;
  139. packet->ino = wq->ino;
  140. packet->uid = from_kuid_munged(user_ns, wq->uid);
  141. packet->gid = from_kgid_munged(user_ns, wq->gid);
  142. packet->pid = wq->pid;
  143. packet->tgid = wq->tgid;
  144. break;
  145. }
  146. default:
  147. printk("autofs4_notify_daemon: bad type %d!\n", type);
  148. mutex_unlock(&sbi->wq_mutex);
  149. return;
  150. }
  151. pipe = get_file(sbi->pipe);
  152. mutex_unlock(&sbi->wq_mutex);
  153. if (autofs4_write(sbi, pipe, &pkt, pktsz))
  154. autofs4_catatonic_mode(sbi);
  155. fput(pipe);
  156. }
  157. static int autofs4_getpath(struct autofs_sb_info *sbi,
  158. struct dentry *dentry, char **name)
  159. {
  160. struct dentry *root = sbi->sb->s_root;
  161. struct dentry *tmp;
  162. char *buf;
  163. char *p;
  164. int len;
  165. unsigned seq;
  166. rename_retry:
  167. buf = *name;
  168. len = 0;
  169. seq = read_seqbegin(&rename_lock);
  170. rcu_read_lock();
  171. spin_lock(&sbi->fs_lock);
  172. for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
  173. len += tmp->d_name.len + 1;
  174. if (!len || --len > NAME_MAX) {
  175. spin_unlock(&sbi->fs_lock);
  176. rcu_read_unlock();
  177. if (read_seqretry(&rename_lock, seq))
  178. goto rename_retry;
  179. return 0;
  180. }
  181. *(buf + len) = '\0';
  182. p = buf + len - dentry->d_name.len;
  183. strncpy(p, dentry->d_name.name, dentry->d_name.len);
  184. for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
  185. *(--p) = '/';
  186. p -= tmp->d_name.len;
  187. strncpy(p, tmp->d_name.name, tmp->d_name.len);
  188. }
  189. spin_unlock(&sbi->fs_lock);
  190. rcu_read_unlock();
  191. if (read_seqretry(&rename_lock, seq))
  192. goto rename_retry;
  193. return len;
  194. }
  195. static struct autofs_wait_queue *
  196. autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
  197. {
  198. struct autofs_wait_queue *wq;
  199. for (wq = sbi->queues; wq; wq = wq->next) {
  200. if (wq->name.hash == qstr->hash &&
  201. wq->name.len == qstr->len &&
  202. wq->name.name &&
  203. !memcmp(wq->name.name, qstr->name, qstr->len))
  204. break;
  205. }
  206. return wq;
  207. }
  208. /*
  209. * Check if we have a valid request.
  210. * Returns
  211. * 1 if the request should continue.
  212. * In this case we can return an autofs_wait_queue entry if one is
  213. * found or NULL to idicate a new wait needs to be created.
  214. * 0 or a negative errno if the request shouldn't continue.
  215. */
  216. static int validate_request(struct autofs_wait_queue **wait,
  217. struct autofs_sb_info *sbi,
  218. struct qstr *qstr,
  219. struct dentry*dentry, enum autofs_notify notify)
  220. {
  221. struct autofs_wait_queue *wq;
  222. struct autofs_info *ino;
  223. if (sbi->catatonic)
  224. return -ENOENT;
  225. /* Wait in progress, continue; */
  226. wq = autofs4_find_wait(sbi, qstr);
  227. if (wq) {
  228. *wait = wq;
  229. return 1;
  230. }
  231. *wait = NULL;
  232. /* If we don't yet have any info this is a new request */
  233. ino = autofs4_dentry_ino(dentry);
  234. if (!ino)
  235. return 1;
  236. /*
  237. * If we've been asked to wait on an existing expire (NFY_NONE)
  238. * but there is no wait in the queue ...
  239. */
  240. if (notify == NFY_NONE) {
  241. /*
  242. * Either we've betean the pending expire to post it's
  243. * wait or it finished while we waited on the mutex.
  244. * So we need to wait till either, the wait appears
  245. * or the expire finishes.
  246. */
  247. while (ino->flags & AUTOFS_INF_EXPIRING) {
  248. mutex_unlock(&sbi->wq_mutex);
  249. schedule_timeout_interruptible(HZ/10);
  250. if (mutex_lock_interruptible(&sbi->wq_mutex))
  251. return -EINTR;
  252. if (sbi->catatonic)
  253. return -ENOENT;
  254. wq = autofs4_find_wait(sbi, qstr);
  255. if (wq) {
  256. *wait = wq;
  257. return 1;
  258. }
  259. }
  260. /*
  261. * Not ideal but the status has already gone. Of the two
  262. * cases where we wait on NFY_NONE neither depend on the
  263. * return status of the wait.
  264. */
  265. return 0;
  266. }
  267. /*
  268. * If we've been asked to trigger a mount and the request
  269. * completed while we waited on the mutex ...
  270. */
  271. if (notify == NFY_MOUNT) {
  272. struct dentry *new = NULL;
  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 (dentry->d_inode && d_unhashed(dentry)) {
  283. struct dentry *parent = dentry->d_parent;
  284. new = d_lookup(parent, &dentry->d_name);
  285. if (new)
  286. dentry = new;
  287. }
  288. }
  289. if (have_submounts(dentry))
  290. valid = 0;
  291. if (new)
  292. dput(new);
  293. return valid;
  294. }
  295. return 1;
  296. }
  297. int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
  298. enum autofs_notify notify)
  299. {
  300. struct autofs_wait_queue *wq;
  301. struct qstr qstr;
  302. char *name;
  303. int status, ret, type;
  304. /* In catatonic mode, we don't wait for nobody */
  305. if (sbi->catatonic)
  306. return -ENOENT;
  307. if (!dentry->d_inode) {
  308. /*
  309. * A wait for a negative dentry is invalid for certain
  310. * cases. A direct or offset mount "always" has its mount
  311. * point directory created and so the request dentry must
  312. * be positive or the map key doesn't exist. The situation
  313. * is very similar for indirect mounts except only dentrys
  314. * in the root of the autofs file system may be negative.
  315. */
  316. if (autofs_type_trigger(sbi->type))
  317. return -ENOENT;
  318. else if (!IS_ROOT(dentry->d_parent))
  319. return -ENOENT;
  320. }
  321. name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  322. if (!name)
  323. return -ENOMEM;
  324. /* If this is a direct mount request create a dummy name */
  325. if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
  326. qstr.len = sprintf(name, "%p", dentry);
  327. else {
  328. qstr.len = autofs4_getpath(sbi, dentry, &name);
  329. if (!qstr.len) {
  330. kfree(name);
  331. return -ENOENT;
  332. }
  333. }
  334. qstr.name = name;
  335. qstr.hash = full_name_hash(name, qstr.len);
  336. if (mutex_lock_interruptible(&sbi->wq_mutex)) {
  337. kfree(qstr.name);
  338. return -EINTR;
  339. }
  340. ret = validate_request(&wq, sbi, &qstr, dentry, notify);
  341. if (ret <= 0) {
  342. if (ret != -EINTR)
  343. mutex_unlock(&sbi->wq_mutex);
  344. kfree(qstr.name);
  345. return ret;
  346. }
  347. if (!wq) {
  348. /* Create a new wait queue */
  349. wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
  350. if (!wq) {
  351. kfree(qstr.name);
  352. mutex_unlock(&sbi->wq_mutex);
  353. return -ENOMEM;
  354. }
  355. wq->wait_queue_token = autofs4_next_wait_queue;
  356. if (++autofs4_next_wait_queue == 0)
  357. autofs4_next_wait_queue = 1;
  358. wq->next = sbi->queues;
  359. sbi->queues = wq;
  360. init_waitqueue_head(&wq->queue);
  361. memcpy(&wq->name, &qstr, sizeof(struct qstr));
  362. wq->dev = autofs4_get_dev(sbi);
  363. wq->ino = autofs4_get_ino(sbi);
  364. wq->uid = current_uid();
  365. wq->gid = current_gid();
  366. wq->pid = current->pid;
  367. wq->tgid = current->tgid;
  368. wq->status = -EINTR; /* Status return if interrupted */
  369. wq->wait_ctr = 2;
  370. mutex_unlock(&sbi->wq_mutex);
  371. if (sbi->version < 5) {
  372. if (notify == NFY_MOUNT)
  373. type = autofs_ptype_missing;
  374. else
  375. type = autofs_ptype_expire_multi;
  376. } else {
  377. if (notify == NFY_MOUNT)
  378. type = autofs_type_trigger(sbi->type) ?
  379. autofs_ptype_missing_direct :
  380. autofs_ptype_missing_indirect;
  381. else
  382. type = autofs_type_trigger(sbi->type) ?
  383. autofs_ptype_expire_direct :
  384. autofs_ptype_expire_indirect;
  385. }
  386. DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
  387. (unsigned long) wq->wait_queue_token, wq->name.len,
  388. wq->name.name, notify);
  389. /* autofs4_notify_daemon() may block */
  390. autofs4_notify_daemon(sbi, wq, type);
  391. } else {
  392. wq->wait_ctr++;
  393. mutex_unlock(&sbi->wq_mutex);
  394. kfree(qstr.name);
  395. DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
  396. (unsigned long) wq->wait_queue_token, wq->name.len,
  397. wq->name.name, notify);
  398. }
  399. /*
  400. * wq->name.name is NULL iff the lock is already released
  401. * or the mount has been made catatonic.
  402. */
  403. if (wq->name.name) {
  404. /* Block all but "shutdown" signals while waiting */
  405. sigset_t oldset;
  406. unsigned long irqflags;
  407. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  408. oldset = current->blocked;
  409. siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
  410. recalc_sigpending();
  411. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  412. wait_event_interruptible(wq->queue, wq->name.name == NULL);
  413. spin_lock_irqsave(&current->sighand->siglock, irqflags);
  414. current->blocked = oldset;
  415. recalc_sigpending();
  416. spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
  417. } else {
  418. DPRINTK("skipped sleeping");
  419. }
  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 = autofs4_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 = autofs4_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 autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
  459. {
  460. struct autofs_wait_queue *wq, **wql;
  461. mutex_lock(&sbi->wq_mutex);
  462. for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
  463. if (wq->wait_queue_token == wait_queue_token)
  464. break;
  465. }
  466. if (!wq) {
  467. mutex_unlock(&sbi->wq_mutex);
  468. return -EINVAL;
  469. }
  470. *wql = wq->next; /* Unlink from chain */
  471. kfree(wq->name.name);
  472. wq->name.name = NULL; /* Do not wait on this queue */
  473. wq->status = status;
  474. wake_up_interruptible(&wq->queue);
  475. if (!--wq->wait_ctr)
  476. kfree(wq);
  477. mutex_unlock(&sbi->wq_mutex);
  478. return 0;
  479. }