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