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