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