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