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