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