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