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