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> | |
20 | #include <linux/smp_lock.h> | |
21 | #include "autofs_i.h" | |
22 | ||
23 | static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *); | |
24 | static int autofs4_dir_unlink(struct inode *,struct dentry *); | |
25 | static int autofs4_dir_rmdir(struct inode *,struct dentry *); | |
26 | static int autofs4_dir_mkdir(struct inode *,struct dentry *,int); | |
27 | static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long); | |
28 | static int autofs4_dir_open(struct inode *inode, struct file *file); | |
29 | static int autofs4_dir_close(struct inode *inode, struct file *file); | |
30 | static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir); | |
31 | static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir); | |
32 | static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *); | |
34ca959c | 33 | static void *autofs4_follow_link(struct dentry *, struct nameidata *); |
1da177e4 | 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 LT |
44 | .open = autofs4_dir_open, |
45 | .release = autofs4_dir_close, | |
46 | .read = generic_read_dir, | |
47 | .readdir = autofs4_dir_readdir, | |
48 | }; | |
49 | ||
34ca959c | 50 | 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 | ||
34ca959c IK |
58 | struct inode_operations autofs4_direct_root_inode_operations = { |
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 | ||
1da177e4 LT |
66 | struct inode_operations autofs4_dir_inode_operations = { |
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 | { | |
77 | struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb); | |
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 | { | |
98 | struct dentry *dentry = file->f_dentry; | |
99 | struct vfsmount *mnt = file->f_vfsmnt; | |
100 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
f360ce3b | 101 | struct dentry *cursor; |
1da177e4 LT |
102 | int status; |
103 | ||
f360ce3b IK |
104 | status = dcache_dir_open(inode, file); |
105 | if (status) | |
106 | goto out; | |
107 | ||
108 | cursor = file->private_data; | |
109 | cursor->d_fsdata = NULL; | |
110 | ||
1da177e4 LT |
111 | DPRINTK("file=%p dentry=%p %.*s", |
112 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
113 | ||
114 | if (autofs4_oz_mode(sbi)) | |
115 | goto out; | |
116 | ||
117 | if (autofs4_ispending(dentry)) { | |
118 | DPRINTK("dentry busy"); | |
f360ce3b IK |
119 | dcache_dir_close(inode, file); |
120 | status = -EBUSY; | |
121 | goto out; | |
1da177e4 LT |
122 | } |
123 | ||
f360ce3b | 124 | status = -ENOENT; |
1da177e4 LT |
125 | if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) { |
126 | struct nameidata nd; | |
f360ce3b | 127 | int empty, ret; |
1da177e4 LT |
128 | |
129 | /* In case there are stale directory dentrys from a failed mount */ | |
130 | spin_lock(&dcache_lock); | |
131 | empty = list_empty(&dentry->d_subdirs); | |
132 | spin_unlock(&dcache_lock); | |
133 | ||
134 | if (!empty) | |
135 | d_invalidate(dentry); | |
136 | ||
137 | nd.flags = LOOKUP_DIRECTORY; | |
f360ce3b | 138 | ret = (dentry->d_op->d_revalidate)(dentry, &nd); |
1da177e4 | 139 | |
bcdc5e01 IK |
140 | if (ret <= 0) { |
141 | if (ret < 0) | |
142 | status = ret; | |
f360ce3b IK |
143 | dcache_dir_close(inode, file); |
144 | goto out; | |
145 | } | |
1da177e4 LT |
146 | } |
147 | ||
148 | if (d_mountpoint(dentry)) { | |
149 | struct file *fp = NULL; | |
150 | struct vfsmount *fp_mnt = mntget(mnt); | |
151 | struct dentry *fp_dentry = dget(dentry); | |
152 | ||
9b1e3afd IK |
153 | if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) { |
154 | dput(fp_dentry); | |
155 | mntput(fp_mnt); | |
f360ce3b IK |
156 | dcache_dir_close(inode, file); |
157 | goto out; | |
9b1e3afd | 158 | } |
1da177e4 LT |
159 | |
160 | fp = dentry_open(fp_dentry, fp_mnt, file->f_flags); | |
161 | status = PTR_ERR(fp); | |
162 | if (IS_ERR(fp)) { | |
f360ce3b IK |
163 | dcache_dir_close(inode, file); |
164 | goto out; | |
1da177e4 | 165 | } |
f360ce3b | 166 | cursor->d_fsdata = fp; |
1da177e4 | 167 | } |
1da177e4 | 168 | return 0; |
f360ce3b IK |
169 | out: |
170 | return status; | |
1da177e4 LT |
171 | } |
172 | ||
173 | static int autofs4_dir_close(struct inode *inode, struct file *file) | |
174 | { | |
175 | struct dentry *dentry = file->f_dentry; | |
176 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
f360ce3b IK |
177 | struct dentry *cursor = file->private_data; |
178 | int status = 0; | |
1da177e4 LT |
179 | |
180 | DPRINTK("file=%p dentry=%p %.*s", | |
181 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
182 | ||
183 | if (autofs4_oz_mode(sbi)) | |
184 | goto out; | |
185 | ||
186 | if (autofs4_ispending(dentry)) { | |
187 | DPRINTK("dentry busy"); | |
f360ce3b IK |
188 | status = -EBUSY; |
189 | goto out; | |
1da177e4 LT |
190 | } |
191 | ||
192 | if (d_mountpoint(dentry)) { | |
f360ce3b IK |
193 | struct file *fp = cursor->d_fsdata; |
194 | if (!fp) { | |
195 | status = -ENOENT; | |
196 | goto out; | |
197 | } | |
1da177e4 | 198 | filp_close(fp, current->files); |
1da177e4 LT |
199 | } |
200 | out: | |
f360ce3b IK |
201 | dcache_dir_close(inode, file); |
202 | return status; | |
1da177e4 LT |
203 | } |
204 | ||
205 | static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir) | |
206 | { | |
207 | struct dentry *dentry = file->f_dentry; | |
208 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
f360ce3b | 209 | struct dentry *cursor = file->private_data; |
1da177e4 LT |
210 | int status; |
211 | ||
212 | DPRINTK("file=%p dentry=%p %.*s", | |
213 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
214 | ||
215 | if (autofs4_oz_mode(sbi)) | |
216 | goto out; | |
217 | ||
218 | if (autofs4_ispending(dentry)) { | |
219 | DPRINTK("dentry busy"); | |
220 | return -EBUSY; | |
221 | } | |
222 | ||
223 | if (d_mountpoint(dentry)) { | |
f360ce3b | 224 | struct file *fp = cursor->d_fsdata; |
1da177e4 LT |
225 | |
226 | if (!fp) | |
227 | return -ENOENT; | |
228 | ||
229 | if (!fp->f_op || !fp->f_op->readdir) | |
230 | goto out; | |
231 | ||
232 | status = vfs_readdir(fp, filldir, dirent); | |
233 | file->f_pos = fp->f_pos; | |
234 | if (status) | |
235 | autofs4_copy_atime(file, fp); | |
236 | return status; | |
237 | } | |
238 | out: | |
f360ce3b | 239 | return dcache_readdir(file, dirent, filldir); |
1da177e4 LT |
240 | } |
241 | ||
862b110f | 242 | static int try_to_fill_dentry(struct dentry *dentry, int flags) |
1da177e4 | 243 | { |
862b110f | 244 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
718c604a | 245 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
1da177e4 LT |
246 | int status = 0; |
247 | ||
248 | /* Block on any pending expiry here; invalidate the dentry | |
249 | when expiration is done to trigger mount request with a new | |
250 | dentry */ | |
718c604a | 251 | if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) { |
1da177e4 LT |
252 | DPRINTK("waiting for expire %p name=%.*s", |
253 | dentry, dentry->d_name.len, dentry->d_name.name); | |
254 | ||
255 | status = autofs4_wait(sbi, dentry, NFY_NONE); | |
718c604a | 256 | |
1da177e4 | 257 | DPRINTK("expire done status=%d", status); |
718c604a | 258 | |
1684b2bb IK |
259 | /* |
260 | * If the directory still exists the mount request must | |
261 | * continue otherwise it can't be followed at the right | |
262 | * time during the walk. | |
263 | */ | |
264 | status = d_invalidate(dentry); | |
265 | if (status != -EBUSY) | |
34ca959c | 266 | return -ENOENT; |
1da177e4 LT |
267 | } |
268 | ||
269 | DPRINTK("dentry=%p %.*s ino=%p", | |
270 | dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode); | |
271 | ||
718c604a IK |
272 | /* |
273 | * Wait for a pending mount, triggering one if there | |
274 | * isn't one already | |
275 | */ | |
1da177e4 LT |
276 | if (dentry->d_inode == NULL) { |
277 | DPRINTK("waiting for mount name=%.*s", | |
278 | dentry->d_name.len, dentry->d_name.name); | |
279 | ||
280 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); | |
718c604a | 281 | |
1da177e4 LT |
282 | DPRINTK("mount done status=%d", status); |
283 | ||
1da177e4 LT |
284 | /* Turn this into a real negative dentry? */ |
285 | if (status == -ENOENT) { | |
1da177e4 LT |
286 | spin_lock(&dentry->d_lock); |
287 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
288 | spin_unlock(&dentry->d_lock); | |
34ca959c | 289 | return status; |
1da177e4 LT |
290 | } else if (status) { |
291 | /* Return a negative dentry, but leave it "pending" */ | |
34ca959c | 292 | return status; |
1da177e4 LT |
293 | } |
294 | /* Trigger mount for path component or follow link */ | |
295 | } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) || | |
296 | current->link_count) { | |
297 | DPRINTK("waiting for mount name=%.*s", | |
298 | dentry->d_name.len, dentry->d_name.name); | |
299 | ||
300 | spin_lock(&dentry->d_lock); | |
301 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; | |
302 | spin_unlock(&dentry->d_lock); | |
303 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); | |
304 | ||
305 | DPRINTK("mount done status=%d", status); | |
306 | ||
307 | if (status) { | |
308 | spin_lock(&dentry->d_lock); | |
309 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
310 | spin_unlock(&dentry->d_lock); | |
34ca959c | 311 | return status; |
1da177e4 LT |
312 | } |
313 | } | |
314 | ||
e0a7aae9 IK |
315 | /* Initialize expiry counter after successful mount */ |
316 | if (ino) | |
317 | ino->last_used = jiffies; | |
318 | ||
1da177e4 LT |
319 | spin_lock(&dentry->d_lock); |
320 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
321 | spin_unlock(&dentry->d_lock); | |
34ca959c IK |
322 | return status; |
323 | } | |
324 | ||
325 | /* For autofs direct mounts the follow link triggers the mount */ | |
326 | static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) | |
327 | { | |
328 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
a5370553 | 329 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
34ca959c IK |
330 | int oz_mode = autofs4_oz_mode(sbi); |
331 | unsigned int lookup_type; | |
332 | int status; | |
333 | ||
334 | DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d", | |
335 | dentry, dentry->d_name.len, dentry->d_name.name, oz_mode, | |
336 | nd->flags); | |
337 | ||
338 | /* If it's our master or we shouldn't trigger a mount we're done */ | |
339 | lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY); | |
340 | if (oz_mode || !lookup_type) | |
341 | goto done; | |
342 | ||
a5370553 IK |
343 | /* If an expire request is pending wait for it. */ |
344 | if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) { | |
871f9434 IK |
345 | DPRINTK("waiting for active request %p name=%.*s", |
346 | dentry, dentry->d_name.len, dentry->d_name.name); | |
347 | ||
348 | status = autofs4_wait(sbi, dentry, NFY_NONE); | |
349 | ||
350 | DPRINTK("request done status=%d", status); | |
351 | } | |
34ca959c | 352 | |
871f9434 IK |
353 | /* |
354 | * If the dentry contains directories then it is an | |
355 | * autofs multi-mount with no root mount offset. So | |
356 | * don't try to mount it again. | |
357 | */ | |
358 | spin_lock(&dcache_lock); | |
be3ca7fe | 359 | if (!d_mountpoint(dentry) && __simple_empty(dentry)) { |
871f9434 IK |
360 | spin_unlock(&dcache_lock); |
361 | ||
362 | status = try_to_fill_dentry(dentry, 0); | |
363 | if (status) | |
364 | goto out_error; | |
365 | ||
366 | /* | |
367 | * The mount succeeded but if there is no root mount | |
368 | * it must be an autofs multi-mount with no root offset | |
369 | * so we don't need to follow the mount. | |
370 | */ | |
371 | if (d_mountpoint(dentry)) { | |
372 | if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) { | |
373 | status = -ENOENT; | |
374 | goto out_error; | |
375 | } | |
376 | } | |
377 | ||
378 | goto done; | |
34ca959c | 379 | } |
871f9434 | 380 | spin_unlock(&dcache_lock); |
34ca959c IK |
381 | |
382 | done: | |
383 | return NULL; | |
384 | ||
385 | out_error: | |
386 | path_release(nd); | |
387 | return ERR_PTR(status); | |
1da177e4 LT |
388 | } |
389 | ||
390 | /* | |
391 | * Revalidate is called on every cache lookup. Some of those | |
392 | * cache lookups may actually happen while the dentry is not | |
393 | * yet completely filled in, and revalidate has to delay such | |
394 | * lookups.. | |
395 | */ | |
718c604a | 396 | static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) |
1da177e4 | 397 | { |
718c604a | 398 | struct inode *dir = dentry->d_parent->d_inode; |
1da177e4 LT |
399 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
400 | int oz_mode = autofs4_oz_mode(sbi); | |
401 | int flags = nd ? nd->flags : 0; | |
bcdc5e01 | 402 | int status = 1; |
1da177e4 LT |
403 | |
404 | /* Pending dentry */ | |
405 | if (autofs4_ispending(dentry)) { | |
bcdc5e01 IK |
406 | /* The daemon never causes a mount to trigger */ |
407 | if (oz_mode) | |
408 | return 1; | |
409 | ||
410 | /* | |
411 | * A zero status is success otherwise we have a | |
412 | * negative error code. | |
413 | */ | |
414 | status = try_to_fill_dentry(dentry, flags); | |
415 | if (status == 0) | |
416 | return 1; | |
417 | ||
418 | return status; | |
1da177e4 LT |
419 | } |
420 | ||
421 | /* Negative dentry.. invalidate if "old" */ | |
422 | if (dentry->d_inode == NULL) | |
2d753e62 | 423 | return 0; |
1da177e4 LT |
424 | |
425 | /* Check for a non-mountpoint directory with no contents */ | |
426 | spin_lock(&dcache_lock); | |
427 | if (S_ISDIR(dentry->d_inode->i_mode) && | |
428 | !d_mountpoint(dentry) && | |
90a59c7c | 429 | __simple_empty(dentry)) { |
1da177e4 LT |
430 | DPRINTK("dentry=%p %.*s, emptydir", |
431 | dentry, dentry->d_name.len, dentry->d_name.name); | |
432 | spin_unlock(&dcache_lock); | |
bcdc5e01 IK |
433 | /* The daemon never causes a mount to trigger */ |
434 | if (oz_mode) | |
435 | return 1; | |
436 | ||
437 | /* | |
438 | * A zero status is success otherwise we have a | |
439 | * negative error code. | |
440 | */ | |
441 | status = try_to_fill_dentry(dentry, flags); | |
442 | if (status == 0) | |
443 | return 1; | |
444 | ||
445 | return status; | |
1da177e4 LT |
446 | } |
447 | spin_unlock(&dcache_lock); | |
448 | ||
1da177e4 LT |
449 | return 1; |
450 | } | |
451 | ||
34ca959c | 452 | void autofs4_dentry_release(struct dentry *de) |
1da177e4 LT |
453 | { |
454 | struct autofs_info *inf; | |
455 | ||
456 | DPRINTK("releasing %p", de); | |
457 | ||
458 | inf = autofs4_dentry_ino(de); | |
459 | de->d_fsdata = NULL; | |
460 | ||
461 | if (inf) { | |
462 | inf->dentry = NULL; | |
463 | inf->inode = NULL; | |
464 | ||
465 | autofs4_free_ino(inf); | |
466 | } | |
467 | } | |
468 | ||
469 | /* For dentries of directories in the root dir */ | |
470 | static struct dentry_operations autofs4_root_dentry_operations = { | |
471 | .d_revalidate = autofs4_revalidate, | |
472 | .d_release = autofs4_dentry_release, | |
473 | }; | |
474 | ||
475 | /* For other dentries */ | |
476 | static struct dentry_operations autofs4_dentry_operations = { | |
477 | .d_revalidate = autofs4_revalidate, | |
478 | .d_release = autofs4_dentry_release, | |
479 | }; | |
480 | ||
481 | /* Lookups in the root directory */ | |
482 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | |
483 | { | |
484 | struct autofs_sb_info *sbi; | |
485 | int oz_mode; | |
486 | ||
487 | DPRINTK("name = %.*s", | |
488 | dentry->d_name.len, dentry->d_name.name); | |
489 | ||
718c604a | 490 | /* File name too long to exist */ |
1da177e4 | 491 | if (dentry->d_name.len > NAME_MAX) |
718c604a | 492 | return ERR_PTR(-ENAMETOOLONG); |
1da177e4 LT |
493 | |
494 | sbi = autofs4_sbi(dir->i_sb); | |
1da177e4 | 495 | oz_mode = autofs4_oz_mode(sbi); |
718c604a | 496 | |
1da177e4 LT |
497 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
498 | current->pid, process_group(current), sbi->catatonic, oz_mode); | |
499 | ||
500 | /* | |
501 | * Mark the dentry incomplete, but add it. This is needed so | |
502 | * that the VFS layer knows about the dentry, and we can count | |
503 | * on catching any lookups through the revalidate. | |
504 | * | |
505 | * Let all the hard work be done by the revalidate function that | |
506 | * needs to be able to do this anyway.. | |
507 | * | |
508 | * We need to do this before we release the directory semaphore. | |
509 | */ | |
510 | dentry->d_op = &autofs4_root_dentry_operations; | |
511 | ||
512 | if (!oz_mode) { | |
513 | spin_lock(&dentry->d_lock); | |
514 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; | |
515 | spin_unlock(&dentry->d_lock); | |
516 | } | |
517 | dentry->d_fsdata = NULL; | |
518 | d_add(dentry, NULL); | |
519 | ||
520 | if (dentry->d_op && dentry->d_op->d_revalidate) { | |
1b1dcc1b | 521 | mutex_unlock(&dir->i_mutex); |
1da177e4 | 522 | (dentry->d_op->d_revalidate)(dentry, nd); |
1b1dcc1b | 523 | mutex_lock(&dir->i_mutex); |
1da177e4 LT |
524 | } |
525 | ||
526 | /* | |
527 | * If we are still pending, check if we had to handle | |
528 | * a signal. If so we can force a restart.. | |
529 | */ | |
530 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { | |
531 | /* See if we were interrupted */ | |
532 | if (signal_pending(current)) { | |
533 | sigset_t *sigset = ¤t->pending.signal; | |
534 | if (sigismember (sigset, SIGKILL) || | |
535 | sigismember (sigset, SIGQUIT) || | |
536 | sigismember (sigset, SIGINT)) { | |
537 | return ERR_PTR(-ERESTARTNOINTR); | |
538 | } | |
539 | } | |
44938af6 IK |
540 | spin_lock(&dentry->d_lock); |
541 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
542 | spin_unlock(&dentry->d_lock); | |
1da177e4 LT |
543 | } |
544 | ||
545 | /* | |
546 | * If this dentry is unhashed, then we shouldn't honour this | |
547 | * lookup even if the dentry is positive. Returning ENOENT here | |
548 | * doesn't do the right thing for all system calls, but it should | |
549 | * be OK for the operations we permit from an autofs. | |
550 | */ | |
718c604a | 551 | if (dentry->d_inode && d_unhashed(dentry)) |
1da177e4 LT |
552 | return ERR_PTR(-ENOENT); |
553 | ||
554 | return NULL; | |
555 | } | |
556 | ||
557 | static int autofs4_dir_symlink(struct inode *dir, | |
558 | struct dentry *dentry, | |
559 | const char *symname) | |
560 | { | |
561 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
562 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 563 | struct autofs_info *p_ino; |
1da177e4 LT |
564 | struct inode *inode; |
565 | char *cp; | |
566 | ||
567 | DPRINTK("%s <- %.*s", symname, | |
568 | dentry->d_name.len, dentry->d_name.name); | |
569 | ||
570 | if (!autofs4_oz_mode(sbi)) | |
571 | return -EACCES; | |
572 | ||
573 | ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); | |
574 | if (ino == NULL) | |
575 | return -ENOSPC; | |
576 | ||
577 | ino->size = strlen(symname); | |
578 | ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL); | |
579 | ||
580 | if (cp == NULL) { | |
581 | kfree(ino); | |
582 | return -ENOSPC; | |
583 | } | |
584 | ||
585 | strcpy(cp, symname); | |
586 | ||
587 | inode = autofs4_get_inode(dir->i_sb, ino); | |
588 | d_instantiate(dentry, inode); | |
589 | ||
590 | if (dir == dir->i_sb->s_root->d_inode) | |
591 | dentry->d_op = &autofs4_root_dentry_operations; | |
592 | else | |
593 | dentry->d_op = &autofs4_dentry_operations; | |
594 | ||
595 | dentry->d_fsdata = ino; | |
596 | ino->dentry = dget(dentry); | |
1aff3c8b IK |
597 | atomic_inc(&ino->count); |
598 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
599 | if (p_ino && dentry->d_parent != dentry) | |
600 | atomic_inc(&p_ino->count); | |
1da177e4 LT |
601 | ino->inode = inode; |
602 | ||
603 | dir->i_mtime = CURRENT_TIME; | |
604 | ||
605 | return 0; | |
606 | } | |
607 | ||
608 | /* | |
609 | * NOTE! | |
610 | * | |
611 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache | |
612 | * that the file no longer exists. However, doing that means that the | |
613 | * VFS layer can turn the dentry into a negative dentry. We don't want | |
614 | * this, because since the unlink is probably the result of an expire. | |
615 | * We simply d_drop it, which allows the dentry lookup to remount it | |
616 | * if necessary. | |
617 | * | |
618 | * If a process is blocked on the dentry waiting for the expire to finish, | |
619 | * it will invalidate the dentry and try to mount with a new one. | |
620 | * | |
621 | * Also see autofs4_dir_rmdir().. | |
622 | */ | |
623 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) | |
624 | { | |
625 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
626 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 627 | struct autofs_info *p_ino; |
1da177e4 LT |
628 | |
629 | /* This allows root to remove symlinks */ | |
630 | if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) | |
631 | return -EACCES; | |
632 | ||
1aff3c8b IK |
633 | if (atomic_dec_and_test(&ino->count)) { |
634 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
635 | if (p_ino && dentry->d_parent != dentry) | |
636 | atomic_dec(&p_ino->count); | |
637 | } | |
1da177e4 LT |
638 | dput(ino->dentry); |
639 | ||
640 | dentry->d_inode->i_size = 0; | |
ce71ec36 | 641 | clear_nlink(dentry->d_inode); |
1da177e4 LT |
642 | |
643 | dir->i_mtime = CURRENT_TIME; | |
644 | ||
645 | d_drop(dentry); | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) | |
651 | { | |
652 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
653 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 654 | struct autofs_info *p_ino; |
1da177e4 LT |
655 | |
656 | if (!autofs4_oz_mode(sbi)) | |
657 | return -EACCES; | |
658 | ||
659 | spin_lock(&dcache_lock); | |
660 | if (!list_empty(&dentry->d_subdirs)) { | |
661 | spin_unlock(&dcache_lock); | |
662 | return -ENOTEMPTY; | |
663 | } | |
664 | spin_lock(&dentry->d_lock); | |
665 | __d_drop(dentry); | |
666 | spin_unlock(&dentry->d_lock); | |
667 | spin_unlock(&dcache_lock); | |
668 | ||
1aff3c8b IK |
669 | if (atomic_dec_and_test(&ino->count)) { |
670 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
671 | if (p_ino && dentry->d_parent != dentry) | |
672 | atomic_dec(&p_ino->count); | |
673 | } | |
1da177e4 | 674 | dput(ino->dentry); |
1da177e4 | 675 | dentry->d_inode->i_size = 0; |
ce71ec36 | 676 | clear_nlink(dentry->d_inode); |
1da177e4 LT |
677 | |
678 | if (dir->i_nlink) | |
9a53c3a7 | 679 | drop_nlink(dir); |
1da177e4 LT |
680 | |
681 | return 0; | |
682 | } | |
683 | ||
684 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
685 | { | |
686 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
687 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 688 | struct autofs_info *p_ino; |
1da177e4 LT |
689 | struct inode *inode; |
690 | ||
691 | if ( !autofs4_oz_mode(sbi) ) | |
692 | return -EACCES; | |
693 | ||
694 | DPRINTK("dentry %p, creating %.*s", | |
695 | dentry, dentry->d_name.len, dentry->d_name.name); | |
696 | ||
697 | ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); | |
698 | if (ino == NULL) | |
699 | return -ENOSPC; | |
700 | ||
701 | inode = autofs4_get_inode(dir->i_sb, ino); | |
702 | d_instantiate(dentry, inode); | |
703 | ||
704 | if (dir == dir->i_sb->s_root->d_inode) | |
705 | dentry->d_op = &autofs4_root_dentry_operations; | |
706 | else | |
707 | dentry->d_op = &autofs4_dentry_operations; | |
708 | ||
709 | dentry->d_fsdata = ino; | |
710 | ino->dentry = dget(dentry); | |
1aff3c8b IK |
711 | atomic_inc(&ino->count); |
712 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
713 | if (p_ino && dentry->d_parent != dentry) | |
714 | atomic_inc(&p_ino->count); | |
1da177e4 | 715 | ino->inode = inode; |
d8c76e6f | 716 | inc_nlink(dir); |
1da177e4 LT |
717 | dir->i_mtime = CURRENT_TIME; |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
722 | /* Get/set timeout ioctl() operation */ | |
723 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, | |
724 | unsigned long __user *p) | |
725 | { | |
726 | int rv; | |
727 | unsigned long ntimeout; | |
728 | ||
729 | if ( (rv = get_user(ntimeout, p)) || | |
730 | (rv = put_user(sbi->exp_timeout/HZ, p)) ) | |
731 | return rv; | |
732 | ||
733 | if ( ntimeout > ULONG_MAX/HZ ) | |
734 | sbi->exp_timeout = 0; | |
735 | else | |
736 | sbi->exp_timeout = ntimeout * HZ; | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
741 | /* Return protocol version */ | |
742 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) | |
743 | { | |
744 | return put_user(sbi->version, p); | |
745 | } | |
746 | ||
747 | /* Return protocol sub version */ | |
748 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) | |
749 | { | |
750 | return put_user(sbi->sub_version, p); | |
751 | } | |
752 | ||
753 | /* | |
754 | * Tells the daemon whether we need to reghost or not. Also, clears | |
755 | * the reghost_needed flag. | |
756 | */ | |
757 | static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p) | |
758 | { | |
759 | int status; | |
760 | ||
761 | DPRINTK("returning %d", sbi->needs_reghost); | |
762 | ||
763 | status = put_user(sbi->needs_reghost, p); | |
764 | if ( status ) | |
765 | return status; | |
766 | ||
767 | sbi->needs_reghost = 0; | |
768 | return 0; | |
769 | } | |
770 | ||
771 | /* | |
772 | * Enable / Disable reghosting ioctl() operation | |
773 | */ | |
774 | static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p) | |
775 | { | |
776 | int status; | |
777 | int val; | |
778 | ||
779 | status = get_user(val, p); | |
780 | ||
781 | DPRINTK("reghost = %d", val); | |
782 | ||
783 | if (status) | |
784 | return status; | |
785 | ||
786 | /* turn on/off reghosting, with the val */ | |
787 | sbi->reghost_enabled = val; | |
788 | return 0; | |
789 | } | |
790 | ||
791 | /* | |
792 | * Tells the daemon whether it can umount the autofs mount. | |
793 | */ | |
794 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) | |
795 | { | |
796 | int status = 0; | |
797 | ||
e3474a8e | 798 | if (may_umount(mnt)) |
1da177e4 LT |
799 | status = 1; |
800 | ||
801 | DPRINTK("returning %d", status); | |
802 | ||
803 | status = put_user(status, p); | |
804 | ||
805 | return status; | |
806 | } | |
807 | ||
808 | /* Identify autofs4_dentries - this is so we can tell if there's | |
809 | an extra dentry refcount or not. We only hold a refcount on the | |
810 | dentry if its non-negative (ie, d_inode != NULL) | |
811 | */ | |
812 | int is_autofs4_dentry(struct dentry *dentry) | |
813 | { | |
814 | return dentry && dentry->d_inode && | |
815 | (dentry->d_op == &autofs4_root_dentry_operations || | |
816 | dentry->d_op == &autofs4_dentry_operations) && | |
817 | dentry->d_fsdata != NULL; | |
818 | } | |
819 | ||
820 | /* | |
821 | * ioctl()'s on the root directory is the chief method for the daemon to | |
822 | * generate kernel reactions | |
823 | */ | |
824 | static int autofs4_root_ioctl(struct inode *inode, struct file *filp, | |
825 | unsigned int cmd, unsigned long arg) | |
826 | { | |
827 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); | |
828 | void __user *p = (void __user *)arg; | |
829 | ||
830 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", | |
831 | cmd,arg,sbi,process_group(current)); | |
832 | ||
833 | if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || | |
834 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT ) | |
835 | return -ENOTTY; | |
836 | ||
837 | if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) | |
838 | return -EPERM; | |
839 | ||
840 | switch(cmd) { | |
841 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ | |
842 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); | |
843 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ | |
844 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); | |
845 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ | |
846 | autofs4_catatonic_mode(sbi); | |
847 | return 0; | |
848 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ | |
849 | return autofs4_get_protover(sbi, p); | |
850 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ | |
851 | return autofs4_get_protosubver(sbi, p); | |
852 | case AUTOFS_IOC_SETTIMEOUT: | |
853 | return autofs4_get_set_timeout(sbi, p); | |
854 | ||
855 | case AUTOFS_IOC_TOGGLEREGHOST: | |
856 | return autofs4_toggle_reghost(sbi, p); | |
857 | case AUTOFS_IOC_ASKREGHOST: | |
858 | return autofs4_ask_reghost(sbi, p); | |
859 | ||
860 | case AUTOFS_IOC_ASKUMOUNT: | |
861 | return autofs4_ask_umount(filp->f_vfsmnt, p); | |
862 | ||
863 | /* return a single thing to expire */ | |
864 | case AUTOFS_IOC_EXPIRE: | |
865 | return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p); | |
866 | /* same as above, but can send multiple expires through pipe */ | |
867 | case AUTOFS_IOC_EXPIRE_MULTI: | |
868 | return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p); | |
869 | ||
870 | default: | |
871 | return -ENOSYS; | |
872 | } | |
873 | } |