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