autofs4: use lookup intent flags to trigger mounts
[deliverable/linux.git] / fs / autofs4 / root.c
1 /* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/root.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
8 *
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
12 *
13 * ------------------------------------------------------------------------- */
14
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/param.h>
19 #include <linux/time.h>
20 #include "autofs_i.h"
21
22 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs4_dir_unlink(struct inode *,struct dentry *);
24 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27 static int autofs4_dir_open(struct inode *inode, struct file *file);
28 static int autofs4_dir_close(struct inode *inode, struct file *file);
29 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
32 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
33
34 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
35 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
36
37 const struct file_operations autofs4_root_operations = {
38 .open = dcache_dir_open,
39 .release = dcache_dir_close,
40 .read = generic_read_dir,
41 .readdir = autofs4_root_readdir,
42 .ioctl = autofs4_root_ioctl,
43 };
44
45 const struct file_operations autofs4_dir_operations = {
46 .open = autofs4_dir_open,
47 .release = autofs4_dir_close,
48 .read = generic_read_dir,
49 .readdir = autofs4_dir_readdir,
50 };
51
52 const struct inode_operations autofs4_indirect_root_inode_operations = {
53 .lookup = autofs4_lookup,
54 .unlink = autofs4_dir_unlink,
55 .symlink = autofs4_dir_symlink,
56 .mkdir = autofs4_dir_mkdir,
57 .rmdir = autofs4_dir_rmdir,
58 };
59
60 const struct inode_operations autofs4_direct_root_inode_operations = {
61 .lookup = autofs4_lookup,
62 .unlink = autofs4_dir_unlink,
63 .mkdir = autofs4_dir_mkdir,
64 .rmdir = autofs4_dir_rmdir,
65 .follow_link = autofs4_follow_link,
66 };
67
68 const struct inode_operations autofs4_dir_inode_operations = {
69 .lookup = autofs4_lookup,
70 .unlink = autofs4_dir_unlink,
71 .symlink = autofs4_dir_symlink,
72 .mkdir = autofs4_dir_mkdir,
73 .rmdir = autofs4_dir_rmdir,
74 };
75
76 static int autofs4_root_readdir(struct file *file, void *dirent,
77 filldir_t filldir)
78 {
79 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
80 int oz_mode = autofs4_oz_mode(sbi);
81
82 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
83
84 /*
85 * Don't set reghost flag if:
86 * 1) f_pos is larger than zero -- we've already been here.
87 * 2) we haven't even enabled reghosting in the 1st place.
88 * 3) this is the daemon doing a readdir
89 */
90 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
91 sbi->needs_reghost = 1;
92
93 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
94
95 return dcache_readdir(file, dirent, filldir);
96 }
97
98 static int autofs4_dir_open(struct inode *inode, struct file *file)
99 {
100 struct dentry *dentry = file->f_path.dentry;
101 struct vfsmount *mnt = file->f_path.mnt;
102 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
103 struct dentry *cursor;
104 int status;
105
106 status = dcache_dir_open(inode, file);
107 if (status)
108 goto out;
109
110 cursor = file->private_data;
111 cursor->d_fsdata = NULL;
112
113 DPRINTK("file=%p dentry=%p %.*s",
114 file, dentry, dentry->d_name.len, dentry->d_name.name);
115
116 if (autofs4_oz_mode(sbi))
117 goto out;
118
119 if (autofs4_ispending(dentry)) {
120 DPRINTK("dentry busy");
121 dcache_dir_close(inode, file);
122 status = -EBUSY;
123 goto out;
124 }
125
126 status = -ENOENT;
127 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
128 struct nameidata nd;
129 int empty, ret;
130
131 /* In case there are stale directory dentrys from a failed mount */
132 spin_lock(&dcache_lock);
133 empty = list_empty(&dentry->d_subdirs);
134 spin_unlock(&dcache_lock);
135
136 if (!empty)
137 d_invalidate(dentry);
138
139 nd.flags = LOOKUP_DIRECTORY;
140 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
141
142 if (ret <= 0) {
143 if (ret < 0)
144 status = ret;
145 dcache_dir_close(inode, file);
146 goto out;
147 }
148 }
149
150 if (d_mountpoint(dentry)) {
151 struct file *fp = NULL;
152 struct path fp_path = { .dentry = dentry, .mnt = mnt };
153
154 path_get(&fp_path);
155
156 if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
157 path_put(&fp_path);
158 dcache_dir_close(inode, file);
159 goto out;
160 }
161
162 fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
163 status = PTR_ERR(fp);
164 if (IS_ERR(fp)) {
165 dcache_dir_close(inode, file);
166 goto out;
167 }
168 cursor->d_fsdata = fp;
169 }
170 return 0;
171 out:
172 return status;
173 }
174
175 static int autofs4_dir_close(struct inode *inode, struct file *file)
176 {
177 struct dentry *dentry = file->f_path.dentry;
178 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
179 struct dentry *cursor = file->private_data;
180 int status = 0;
181
182 DPRINTK("file=%p dentry=%p %.*s",
183 file, dentry, dentry->d_name.len, dentry->d_name.name);
184
185 if (autofs4_oz_mode(sbi))
186 goto out;
187
188 if (autofs4_ispending(dentry)) {
189 DPRINTK("dentry busy");
190 status = -EBUSY;
191 goto out;
192 }
193
194 if (d_mountpoint(dentry)) {
195 struct file *fp = cursor->d_fsdata;
196 if (!fp) {
197 status = -ENOENT;
198 goto out;
199 }
200 filp_close(fp, current->files);
201 }
202 out:
203 dcache_dir_close(inode, file);
204 return status;
205 }
206
207 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
208 {
209 struct dentry *dentry = file->f_path.dentry;
210 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
211 struct dentry *cursor = file->private_data;
212 int status;
213
214 DPRINTK("file=%p dentry=%p %.*s",
215 file, dentry, dentry->d_name.len, dentry->d_name.name);
216
217 if (autofs4_oz_mode(sbi))
218 goto out;
219
220 if (autofs4_ispending(dentry)) {
221 DPRINTK("dentry busy");
222 return -EBUSY;
223 }
224
225 if (d_mountpoint(dentry)) {
226 struct file *fp = cursor->d_fsdata;
227
228 if (!fp)
229 return -ENOENT;
230
231 if (!fp->f_op || !fp->f_op->readdir)
232 goto out;
233
234 status = vfs_readdir(fp, filldir, dirent);
235 file->f_pos = fp->f_pos;
236 if (status)
237 autofs4_copy_atime(file, fp);
238 return status;
239 }
240 out:
241 return dcache_readdir(file, dirent, filldir);
242 }
243
244 static int try_to_fill_dentry(struct dentry *dentry, int flags)
245 {
246 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
247 struct autofs_info *ino = autofs4_dentry_ino(dentry);
248 int status;
249
250 /* Block on any pending expiry here; invalidate the dentry
251 when expiration is done to trigger mount request with a new
252 dentry */
253 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
254 DPRINTK("waiting for expire %p name=%.*s",
255 dentry, dentry->d_name.len, dentry->d_name.name);
256
257 status = autofs4_wait(sbi, dentry, NFY_NONE);
258
259 DPRINTK("expire done status=%d", status);
260
261 /*
262 * If the directory still exists the mount request must
263 * continue otherwise it can't be followed at the right
264 * time during the walk.
265 */
266 status = d_invalidate(dentry);
267 if (status != -EBUSY)
268 return -EAGAIN;
269 }
270
271 DPRINTK("dentry=%p %.*s ino=%p",
272 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
273
274 /*
275 * Wait for a pending mount, triggering one if there
276 * isn't one already
277 */
278 if (dentry->d_inode == NULL) {
279 DPRINTK("waiting for mount name=%.*s",
280 dentry->d_name.len, dentry->d_name.name);
281
282 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
283
284 DPRINTK("mount done status=%d", status);
285
286 /* Turn this into a real negative dentry? */
287 if (status == -ENOENT) {
288 spin_lock(&dentry->d_lock);
289 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
290 spin_unlock(&dentry->d_lock);
291 return status;
292 } else if (status) {
293 /* Return a negative dentry, but leave it "pending" */
294 return status;
295 }
296 /* Trigger mount for path component or follow link */
297 } else if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
298 current->link_count) {
299 DPRINTK("waiting for mount name=%.*s",
300 dentry->d_name.len, dentry->d_name.name);
301
302 spin_lock(&dentry->d_lock);
303 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
304 spin_unlock(&dentry->d_lock);
305 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
306
307 DPRINTK("mount done status=%d", status);
308
309 if (status) {
310 spin_lock(&dentry->d_lock);
311 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
312 spin_unlock(&dentry->d_lock);
313 return status;
314 }
315 }
316
317 /* Initialize expiry counter after successful mount */
318 if (ino)
319 ino->last_used = jiffies;
320
321 spin_lock(&dentry->d_lock);
322 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
323 spin_unlock(&dentry->d_lock);
324
325 return 0;
326 }
327
328 /* For autofs direct mounts the follow link triggers the mount */
329 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
330 {
331 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
332 struct autofs_info *ino = autofs4_dentry_ino(dentry);
333 int oz_mode = autofs4_oz_mode(sbi);
334 unsigned int lookup_type;
335 int status;
336
337 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
338 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
339 nd->flags);
340
341 /* If it's our master or we shouldn't trigger a mount we're done */
342 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
343 if (oz_mode || !lookup_type)
344 goto done;
345
346 /* If an expire request is pending wait for it. */
347 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
348 DPRINTK("waiting for active request %p name=%.*s",
349 dentry, dentry->d_name.len, dentry->d_name.name);
350
351 status = autofs4_wait(sbi, dentry, NFY_NONE);
352
353 DPRINTK("request done status=%d", status);
354 }
355
356 /*
357 * If the dentry contains directories then it is an
358 * autofs multi-mount with no root mount offset. So
359 * don't try to mount it again.
360 */
361 spin_lock(&dcache_lock);
362 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
363 spin_unlock(&dcache_lock);
364
365 status = try_to_fill_dentry(dentry, 0);
366 if (status)
367 goto out_error;
368
369 /*
370 * The mount succeeded but if there is no root mount
371 * it must be an autofs multi-mount with no root offset
372 * so we don't need to follow the mount.
373 */
374 if (d_mountpoint(dentry)) {
375 if (!autofs4_follow_mount(&nd->path.mnt,
376 &nd->path.dentry)) {
377 status = -ENOENT;
378 goto out_error;
379 }
380 }
381
382 goto done;
383 }
384 spin_unlock(&dcache_lock);
385
386 done:
387 return NULL;
388
389 out_error:
390 path_put(&nd->path);
391 return ERR_PTR(status);
392 }
393
394 /*
395 * Revalidate is called on every cache lookup. Some of those
396 * cache lookups may actually happen while the dentry is not
397 * yet completely filled in, and revalidate has to delay such
398 * lookups..
399 */
400 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
401 {
402 struct inode *dir = dentry->d_parent->d_inode;
403 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
404 int oz_mode = autofs4_oz_mode(sbi);
405 int flags = nd ? nd->flags : 0;
406 int status = 1;
407
408 /* Pending dentry */
409 if (autofs4_ispending(dentry)) {
410 /* The daemon never causes a mount to trigger */
411 if (oz_mode)
412 return 1;
413
414 /*
415 * A zero status is success otherwise we have a
416 * negative error code.
417 */
418 status = try_to_fill_dentry(dentry, flags);
419 if (status == 0)
420 return 1;
421
422 /*
423 * A status of EAGAIN here means that the dentry has gone
424 * away while waiting for an expire to complete. If we are
425 * racing with expire lookup will wait for it so this must
426 * be a revalidate and we need to send it to lookup.
427 */
428 if (status == -EAGAIN)
429 return 0;
430
431 return status;
432 }
433
434 /* Negative dentry.. invalidate if "old" */
435 if (dentry->d_inode == NULL)
436 return 0;
437
438 /* Check for a non-mountpoint directory with no contents */
439 spin_lock(&dcache_lock);
440 if (S_ISDIR(dentry->d_inode->i_mode) &&
441 !d_mountpoint(dentry) &&
442 __simple_empty(dentry)) {
443 DPRINTK("dentry=%p %.*s, emptydir",
444 dentry, dentry->d_name.len, dentry->d_name.name);
445 spin_unlock(&dcache_lock);
446 /* The daemon never causes a mount to trigger */
447 if (oz_mode)
448 return 1;
449
450 /*
451 * A zero status is success otherwise we have a
452 * negative error code.
453 */
454 status = try_to_fill_dentry(dentry, flags);
455 if (status == 0)
456 return 1;
457
458 return status;
459 }
460 spin_unlock(&dcache_lock);
461
462 return 1;
463 }
464
465 void autofs4_dentry_release(struct dentry *de)
466 {
467 struct autofs_info *inf;
468
469 DPRINTK("releasing %p", de);
470
471 inf = autofs4_dentry_ino(de);
472 de->d_fsdata = NULL;
473
474 if (inf) {
475 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
476
477 if (sbi) {
478 spin_lock(&sbi->lookup_lock);
479 if (!list_empty(&inf->active))
480 list_del(&inf->active);
481 if (!list_empty(&inf->expiring))
482 list_del(&inf->expiring);
483 spin_unlock(&sbi->lookup_lock);
484 }
485
486 inf->dentry = NULL;
487 inf->inode = NULL;
488
489 autofs4_free_ino(inf);
490 }
491 }
492
493 /* For dentries of directories in the root dir */
494 static struct dentry_operations autofs4_root_dentry_operations = {
495 .d_revalidate = autofs4_revalidate,
496 .d_release = autofs4_dentry_release,
497 };
498
499 /* For other dentries */
500 static struct dentry_operations autofs4_dentry_operations = {
501 .d_revalidate = autofs4_revalidate,
502 .d_release = autofs4_dentry_release,
503 };
504
505 static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
506 {
507 unsigned int len = name->len;
508 unsigned int hash = name->hash;
509 const unsigned char *str = name->name;
510 struct list_head *p, *head;
511
512 spin_lock(&dcache_lock);
513 spin_lock(&sbi->lookup_lock);
514 head = &sbi->active_list;
515 list_for_each(p, head) {
516 struct autofs_info *ino;
517 struct dentry *dentry;
518 struct qstr *qstr;
519
520 ino = list_entry(p, struct autofs_info, active);
521 dentry = ino->dentry;
522
523 spin_lock(&dentry->d_lock);
524
525 /* Already gone? */
526 if (atomic_read(&dentry->d_count) == 0)
527 goto next;
528
529 qstr = &dentry->d_name;
530
531 if (dentry->d_name.hash != hash)
532 goto next;
533 if (dentry->d_parent != parent)
534 goto next;
535
536 if (qstr->len != len)
537 goto next;
538 if (memcmp(qstr->name, str, len))
539 goto next;
540
541 if (d_unhashed(dentry)) {
542 dget(dentry);
543 spin_unlock(&dentry->d_lock);
544 spin_unlock(&sbi->lookup_lock);
545 spin_unlock(&dcache_lock);
546 return dentry;
547 }
548 next:
549 spin_unlock(&dentry->d_lock);
550 }
551 spin_unlock(&sbi->lookup_lock);
552 spin_unlock(&dcache_lock);
553
554 return NULL;
555 }
556
557 static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
558 {
559 unsigned int len = name->len;
560 unsigned int hash = name->hash;
561 const unsigned char *str = name->name;
562 struct list_head *p, *head;
563
564 spin_lock(&dcache_lock);
565 spin_lock(&sbi->lookup_lock);
566 head = &sbi->expiring_list;
567 list_for_each(p, head) {
568 struct autofs_info *ino;
569 struct dentry *dentry;
570 struct qstr *qstr;
571
572 ino = list_entry(p, struct autofs_info, expiring);
573 dentry = ino->dentry;
574
575 spin_lock(&dentry->d_lock);
576
577 /* Bad luck, we've already been dentry_iput */
578 if (!dentry->d_inode)
579 goto next;
580
581 qstr = &dentry->d_name;
582
583 if (dentry->d_name.hash != hash)
584 goto next;
585 if (dentry->d_parent != parent)
586 goto next;
587
588 if (qstr->len != len)
589 goto next;
590 if (memcmp(qstr->name, str, len))
591 goto next;
592
593 if (d_unhashed(dentry)) {
594 dget(dentry);
595 spin_unlock(&dentry->d_lock);
596 spin_unlock(&sbi->lookup_lock);
597 spin_unlock(&dcache_lock);
598 return dentry;
599 }
600 next:
601 spin_unlock(&dentry->d_lock);
602 }
603 spin_unlock(&sbi->lookup_lock);
604 spin_unlock(&dcache_lock);
605
606 return NULL;
607 }
608
609 /* Lookups in the root directory */
610 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
611 {
612 struct autofs_sb_info *sbi;
613 struct autofs_info *ino;
614 struct dentry *expiring, *unhashed;
615 int oz_mode;
616
617 DPRINTK("name = %.*s",
618 dentry->d_name.len, dentry->d_name.name);
619
620 /* File name too long to exist */
621 if (dentry->d_name.len > NAME_MAX)
622 return ERR_PTR(-ENAMETOOLONG);
623
624 sbi = autofs4_sbi(dir->i_sb);
625 oz_mode = autofs4_oz_mode(sbi);
626
627 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
628 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
629
630 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
631 if (expiring) {
632 /*
633 * If we are racing with expire the request might not
634 * be quite complete but the directory has been removed
635 * so it must have been successful, so just wait for it.
636 */
637 ino = autofs4_dentry_ino(expiring);
638 while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
639 DPRINTK("wait for incomplete expire %p name=%.*s",
640 expiring, expiring->d_name.len,
641 expiring->d_name.name);
642 autofs4_wait(sbi, expiring, NFY_NONE);
643 DPRINTK("request completed");
644 }
645 spin_lock(&sbi->lookup_lock);
646 if (!list_empty(&ino->expiring))
647 list_del_init(&ino->expiring);
648 spin_unlock(&sbi->lookup_lock);
649 dput(expiring);
650 }
651
652 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
653 if (unhashed)
654 dentry = unhashed;
655 else {
656 /*
657 * Mark the dentry incomplete but don't hash it. We do this
658 * to serialize our inode creation operations (symlink and
659 * mkdir) which prevents deadlock during the callback to
660 * the daemon. Subsequent user space lookups for the same
661 * dentry are placed on the wait queue while the daemon
662 * itself is allowed passage unresticted so the create
663 * operation itself can then hash the dentry. Finally,
664 * we check for the hashed dentry and return the newly
665 * hashed dentry.
666 */
667 dentry->d_op = &autofs4_root_dentry_operations;
668
669 /*
670 * And we need to ensure that the same dentry is used for
671 * all following lookup calls until it is hashed so that
672 * the dentry flags are persistent throughout the request.
673 */
674 ino = autofs4_init_ino(NULL, sbi, 0555);
675 if (!ino)
676 return ERR_PTR(-ENOMEM);
677
678 dentry->d_fsdata = ino;
679 ino->dentry = dentry;
680
681 spin_lock(&sbi->lookup_lock);
682 list_add(&ino->active, &sbi->active_list);
683 spin_unlock(&sbi->lookup_lock);
684
685 d_instantiate(dentry, NULL);
686 }
687
688 if (!oz_mode) {
689 spin_lock(&dentry->d_lock);
690 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
691 spin_unlock(&dentry->d_lock);
692 if (dentry->d_op && dentry->d_op->d_revalidate) {
693 mutex_unlock(&dir->i_mutex);
694 (dentry->d_op->d_revalidate)(dentry, nd);
695 mutex_lock(&dir->i_mutex);
696 }
697 }
698
699 /*
700 * If we are still pending, check if we had to handle
701 * a signal. If so we can force a restart..
702 */
703 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
704 /* See if we were interrupted */
705 if (signal_pending(current)) {
706 sigset_t *sigset = &current->pending.signal;
707 if (sigismember (sigset, SIGKILL) ||
708 sigismember (sigset, SIGQUIT) ||
709 sigismember (sigset, SIGINT)) {
710 if (unhashed)
711 dput(unhashed);
712 return ERR_PTR(-ERESTARTNOINTR);
713 }
714 }
715 if (!oz_mode) {
716 spin_lock(&dentry->d_lock);
717 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
718 spin_unlock(&dentry->d_lock);
719 }
720 }
721
722 /*
723 * If this dentry is unhashed, then we shouldn't honour this
724 * lookup. Returning ENOENT here doesn't do the right thing
725 * for all system calls, but it should be OK for the operations
726 * we permit from an autofs.
727 */
728 if (!oz_mode && d_unhashed(dentry)) {
729 /*
730 * A user space application can (and has done in the past)
731 * remove and re-create this directory during the callback.
732 * This can leave us with an unhashed dentry, but a
733 * successful mount! So we need to perform another
734 * cached lookup in case the dentry now exists.
735 */
736 struct dentry *parent = dentry->d_parent;
737 struct dentry *new = d_lookup(parent, &dentry->d_name);
738 if (new != NULL)
739 dentry = new;
740 else
741 dentry = ERR_PTR(-ENOENT);
742
743 if (unhashed)
744 dput(unhashed);
745
746 return dentry;
747 }
748
749 if (unhashed)
750 return unhashed;
751
752 return NULL;
753 }
754
755 static int autofs4_dir_symlink(struct inode *dir,
756 struct dentry *dentry,
757 const char *symname)
758 {
759 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
760 struct autofs_info *ino = autofs4_dentry_ino(dentry);
761 struct autofs_info *p_ino;
762 struct inode *inode;
763 char *cp;
764
765 DPRINTK("%s <- %.*s", symname,
766 dentry->d_name.len, dentry->d_name.name);
767
768 if (!autofs4_oz_mode(sbi))
769 return -EACCES;
770
771 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
772 if (!ino)
773 return -ENOMEM;
774
775 spin_lock(&sbi->lookup_lock);
776 if (!list_empty(&ino->active))
777 list_del_init(&ino->active);
778 spin_unlock(&sbi->lookup_lock);
779
780 ino->size = strlen(symname);
781 cp = kmalloc(ino->size + 1, GFP_KERNEL);
782 if (!cp) {
783 if (!dentry->d_fsdata)
784 kfree(ino);
785 return -ENOMEM;
786 }
787
788 strcpy(cp, symname);
789
790 inode = autofs4_get_inode(dir->i_sb, ino);
791 if (!inode) {
792 kfree(cp);
793 if (!dentry->d_fsdata)
794 kfree(ino);
795 return -ENOMEM;
796 }
797 d_add(dentry, inode);
798
799 if (dir == dir->i_sb->s_root->d_inode)
800 dentry->d_op = &autofs4_root_dentry_operations;
801 else
802 dentry->d_op = &autofs4_dentry_operations;
803
804 dentry->d_fsdata = ino;
805 ino->dentry = dget(dentry);
806 atomic_inc(&ino->count);
807 p_ino = autofs4_dentry_ino(dentry->d_parent);
808 if (p_ino && dentry->d_parent != dentry)
809 atomic_inc(&p_ino->count);
810 ino->inode = inode;
811
812 ino->u.symlink = cp;
813 dir->i_mtime = CURRENT_TIME;
814
815 return 0;
816 }
817
818 /*
819 * NOTE!
820 *
821 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
822 * that the file no longer exists. However, doing that means that the
823 * VFS layer can turn the dentry into a negative dentry. We don't want
824 * this, because the unlink is probably the result of an expire.
825 * We simply d_drop it and add it to a expiring list in the super block,
826 * which allows the dentry lookup to check for an incomplete expire.
827 *
828 * If a process is blocked on the dentry waiting for the expire to finish,
829 * it will invalidate the dentry and try to mount with a new one.
830 *
831 * Also see autofs4_dir_rmdir()..
832 */
833 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
834 {
835 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
836 struct autofs_info *ino = autofs4_dentry_ino(dentry);
837 struct autofs_info *p_ino;
838
839 /* This allows root to remove symlinks */
840 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
841 return -EACCES;
842
843 if (atomic_dec_and_test(&ino->count)) {
844 p_ino = autofs4_dentry_ino(dentry->d_parent);
845 if (p_ino && dentry->d_parent != dentry)
846 atomic_dec(&p_ino->count);
847 }
848 dput(ino->dentry);
849
850 dentry->d_inode->i_size = 0;
851 clear_nlink(dentry->d_inode);
852
853 dir->i_mtime = CURRENT_TIME;
854
855 spin_lock(&dcache_lock);
856 spin_lock(&sbi->lookup_lock);
857 if (list_empty(&ino->expiring))
858 list_add(&ino->expiring, &sbi->expiring_list);
859 spin_unlock(&sbi->lookup_lock);
860 spin_lock(&dentry->d_lock);
861 __d_drop(dentry);
862 spin_unlock(&dentry->d_lock);
863 spin_unlock(&dcache_lock);
864
865 return 0;
866 }
867
868 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
869 {
870 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
871 struct autofs_info *ino = autofs4_dentry_ino(dentry);
872 struct autofs_info *p_ino;
873
874 DPRINTK("dentry %p, removing %.*s",
875 dentry, dentry->d_name.len, dentry->d_name.name);
876
877 if (!autofs4_oz_mode(sbi))
878 return -EACCES;
879
880 spin_lock(&dcache_lock);
881 if (!list_empty(&dentry->d_subdirs)) {
882 spin_unlock(&dcache_lock);
883 return -ENOTEMPTY;
884 }
885 spin_lock(&sbi->lookup_lock);
886 if (list_empty(&ino->expiring))
887 list_add(&ino->expiring, &sbi->expiring_list);
888 spin_unlock(&sbi->lookup_lock);
889 spin_lock(&dentry->d_lock);
890 __d_drop(dentry);
891 spin_unlock(&dentry->d_lock);
892 spin_unlock(&dcache_lock);
893
894 if (atomic_dec_and_test(&ino->count)) {
895 p_ino = autofs4_dentry_ino(dentry->d_parent);
896 if (p_ino && dentry->d_parent != dentry)
897 atomic_dec(&p_ino->count);
898 }
899 dput(ino->dentry);
900 dentry->d_inode->i_size = 0;
901 clear_nlink(dentry->d_inode);
902
903 if (dir->i_nlink)
904 drop_nlink(dir);
905
906 return 0;
907 }
908
909 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
910 {
911 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
912 struct autofs_info *ino = autofs4_dentry_ino(dentry);
913 struct autofs_info *p_ino;
914 struct inode *inode;
915
916 if (!autofs4_oz_mode(sbi))
917 return -EACCES;
918
919 DPRINTK("dentry %p, creating %.*s",
920 dentry, dentry->d_name.len, dentry->d_name.name);
921
922 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
923 if (!ino)
924 return -ENOMEM;
925
926 spin_lock(&sbi->lookup_lock);
927 if (!list_empty(&ino->active))
928 list_del_init(&ino->active);
929 spin_unlock(&sbi->lookup_lock);
930
931 inode = autofs4_get_inode(dir->i_sb, ino);
932 if (!inode) {
933 if (!dentry->d_fsdata)
934 kfree(ino);
935 return -ENOMEM;
936 }
937 d_add(dentry, inode);
938
939 if (dir == dir->i_sb->s_root->d_inode)
940 dentry->d_op = &autofs4_root_dentry_operations;
941 else
942 dentry->d_op = &autofs4_dentry_operations;
943
944 dentry->d_fsdata = ino;
945 ino->dentry = dget(dentry);
946 atomic_inc(&ino->count);
947 p_ino = autofs4_dentry_ino(dentry->d_parent);
948 if (p_ino && dentry->d_parent != dentry)
949 atomic_inc(&p_ino->count);
950 ino->inode = inode;
951 inc_nlink(dir);
952 dir->i_mtime = CURRENT_TIME;
953
954 return 0;
955 }
956
957 /* Get/set timeout ioctl() operation */
958 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
959 unsigned long __user *p)
960 {
961 int rv;
962 unsigned long ntimeout;
963
964 if ((rv = get_user(ntimeout, p)) ||
965 (rv = put_user(sbi->exp_timeout/HZ, p)))
966 return rv;
967
968 if (ntimeout > ULONG_MAX/HZ)
969 sbi->exp_timeout = 0;
970 else
971 sbi->exp_timeout = ntimeout * HZ;
972
973 return 0;
974 }
975
976 /* Return protocol version */
977 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
978 {
979 return put_user(sbi->version, p);
980 }
981
982 /* Return protocol sub version */
983 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
984 {
985 return put_user(sbi->sub_version, p);
986 }
987
988 /*
989 * Tells the daemon whether we need to reghost or not. Also, clears
990 * the reghost_needed flag.
991 */
992 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
993 {
994 int status;
995
996 DPRINTK("returning %d", sbi->needs_reghost);
997
998 status = put_user(sbi->needs_reghost, p);
999 if (status)
1000 return status;
1001
1002 sbi->needs_reghost = 0;
1003 return 0;
1004 }
1005
1006 /*
1007 * Enable / Disable reghosting ioctl() operation
1008 */
1009 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
1010 {
1011 int status;
1012 int val;
1013
1014 status = get_user(val, p);
1015
1016 DPRINTK("reghost = %d", val);
1017
1018 if (status)
1019 return status;
1020
1021 /* turn on/off reghosting, with the val */
1022 sbi->reghost_enabled = val;
1023 return 0;
1024 }
1025
1026 /*
1027 * Tells the daemon whether it can umount the autofs mount.
1028 */
1029 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1030 {
1031 int status = 0;
1032
1033 if (may_umount(mnt))
1034 status = 1;
1035
1036 DPRINTK("returning %d", status);
1037
1038 status = put_user(status, p);
1039
1040 return status;
1041 }
1042
1043 /* Identify autofs4_dentries - this is so we can tell if there's
1044 an extra dentry refcount or not. We only hold a refcount on the
1045 dentry if its non-negative (ie, d_inode != NULL)
1046 */
1047 int is_autofs4_dentry(struct dentry *dentry)
1048 {
1049 return dentry && dentry->d_inode &&
1050 (dentry->d_op == &autofs4_root_dentry_operations ||
1051 dentry->d_op == &autofs4_dentry_operations) &&
1052 dentry->d_fsdata != NULL;
1053 }
1054
1055 /*
1056 * ioctl()'s on the root directory is the chief method for the daemon to
1057 * generate kernel reactions
1058 */
1059 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
1060 unsigned int cmd, unsigned long arg)
1061 {
1062 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
1063 void __user *p = (void __user *)arg;
1064
1065 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
1066 cmd,arg,sbi,task_pgrp_nr(current));
1067
1068 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
1069 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
1070 return -ENOTTY;
1071
1072 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
1073 return -EPERM;
1074
1075 switch(cmd) {
1076 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
1077 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
1078 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
1079 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
1080 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
1081 autofs4_catatonic_mode(sbi);
1082 return 0;
1083 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
1084 return autofs4_get_protover(sbi, p);
1085 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
1086 return autofs4_get_protosubver(sbi, p);
1087 case AUTOFS_IOC_SETTIMEOUT:
1088 return autofs4_get_set_timeout(sbi, p);
1089
1090 case AUTOFS_IOC_TOGGLEREGHOST:
1091 return autofs4_toggle_reghost(sbi, p);
1092 case AUTOFS_IOC_ASKREGHOST:
1093 return autofs4_ask_reghost(sbi, p);
1094
1095 case AUTOFS_IOC_ASKUMOUNT:
1096 return autofs4_ask_umount(filp->f_path.mnt, p);
1097
1098 /* return a single thing to expire */
1099 case AUTOFS_IOC_EXPIRE:
1100 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
1101 /* same as above, but can send multiple expires through pipe */
1102 case AUTOFS_IOC_EXPIRE_MULTI:
1103 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
1104
1105 default:
1106 return -ENOSYS;
1107 }
1108 }
This page took 0.115889 seconds and 5 git commands to generate.