aboutsummaryrefslogtreecommitdiffstats
path: root/fs/autofs4/root.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /fs/autofs4/root.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'fs/autofs4/root.c')
-rw-r--r--fs/autofs4/root.c772
1 files changed, 328 insertions, 444 deletions
diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c
index cb1bd38dc08c..f55ae23b137e 100644
--- a/fs/autofs4/root.c
+++ b/fs/autofs4/root.c
@@ -19,7 +19,7 @@
19#include <linux/param.h> 19#include <linux/param.h>
20#include <linux/time.h> 20#include <linux/time.h>
21#include <linux/compat.h> 21#include <linux/compat.h>
22#include <linux/smp_lock.h> 22#include <linux/mutex.h>
23 23
24#include "autofs_i.h" 24#include "autofs_i.h"
25 25
@@ -28,13 +28,14 @@ static int autofs4_dir_unlink(struct inode *,struct dentry *);
28static int autofs4_dir_rmdir(struct inode *,struct dentry *); 28static int autofs4_dir_rmdir(struct inode *,struct dentry *);
29static int autofs4_dir_mkdir(struct inode *,struct dentry *,int); 29static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
30static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long); 30static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
31#ifdef CONFIG_COMPAT
31static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long); 32static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
33#endif
32static int autofs4_dir_open(struct inode *inode, struct file *file); 34static int autofs4_dir_open(struct inode *inode, struct file *file);
33static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *); 35static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
34static void *autofs4_follow_link(struct dentry *, struct nameidata *); 36static struct vfsmount *autofs4_d_automount(struct path *);
35 37static int autofs4_d_manage(struct dentry *, bool);
36#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) 38static void autofs4_dentry_release(struct dentry *);
37#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
38 39
39const struct file_operations autofs4_root_operations = { 40const struct file_operations autofs4_root_operations = {
40 .open = dcache_dir_open, 41 .open = dcache_dir_open,
@@ -56,7 +57,7 @@ const struct file_operations autofs4_dir_operations = {
56 .llseek = dcache_dir_lseek, 57 .llseek = dcache_dir_lseek,
57}; 58};
58 59
59const struct inode_operations autofs4_indirect_root_inode_operations = { 60const struct inode_operations autofs4_dir_inode_operations = {
60 .lookup = autofs4_lookup, 61 .lookup = autofs4_lookup,
61 .unlink = autofs4_dir_unlink, 62 .unlink = autofs4_dir_unlink,
62 .symlink = autofs4_dir_symlink, 63 .symlink = autofs4_dir_symlink,
@@ -64,20 +65,10 @@ const struct inode_operations autofs4_indirect_root_inode_operations = {
64 .rmdir = autofs4_dir_rmdir, 65 .rmdir = autofs4_dir_rmdir,
65}; 66};
66 67
67const struct inode_operations autofs4_direct_root_inode_operations = { 68const struct dentry_operations autofs4_dentry_operations = {
68 .lookup = autofs4_lookup, 69 .d_automount = autofs4_d_automount,
69 .unlink = autofs4_dir_unlink, 70 .d_manage = autofs4_d_manage,
70 .mkdir = autofs4_dir_mkdir, 71 .d_release = autofs4_dentry_release,
71 .rmdir = autofs4_dir_rmdir,
72 .follow_link = autofs4_follow_link,
73};
74
75const struct inode_operations autofs4_dir_inode_operations = {
76 .lookup = autofs4_lookup,
77 .unlink = autofs4_dir_unlink,
78 .symlink = autofs4_dir_symlink,
79 .mkdir = autofs4_dir_mkdir,
80 .rmdir = autofs4_dir_rmdir,
81}; 72};
82 73
83static void autofs4_add_active(struct dentry *dentry) 74static void autofs4_add_active(struct dentry *dentry)
@@ -112,14 +103,6 @@ static void autofs4_del_active(struct dentry *dentry)
112 return; 103 return;
113} 104}
114 105
115static unsigned int autofs4_need_mount(unsigned int flags)
116{
117 unsigned int res = 0;
118 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
119 res = 1;
120 return res;
121}
122
123static int autofs4_dir_open(struct inode *inode, struct file *file) 106static int autofs4_dir_open(struct inode *inode, struct file *file)
124{ 107{
125 struct dentry *dentry = file->f_path.dentry; 108 struct dentry *dentry = file->f_path.dentry;
@@ -140,275 +123,41 @@ static int autofs4_dir_open(struct inode *inode, struct file *file)
140 * autofs file system so just let the libfs routines handle 123 * autofs file system so just let the libfs routines handle
141 * it. 124 * it.
142 */ 125 */
143 spin_lock(&dcache_lock); 126 spin_lock(&sbi->lookup_lock);
127 spin_lock(&dentry->d_lock);
144 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { 128 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
145 spin_unlock(&dcache_lock); 129 spin_unlock(&dentry->d_lock);
130 spin_unlock(&sbi->lookup_lock);
146 return -ENOENT; 131 return -ENOENT;
147 } 132 }
148 spin_unlock(&dcache_lock); 133 spin_unlock(&dentry->d_lock);
134 spin_unlock(&sbi->lookup_lock);
149 135
150out: 136out:
151 return dcache_dir_open(inode, file); 137 return dcache_dir_open(inode, file);
152} 138}
153 139
154static int try_to_fill_dentry(struct dentry *dentry, int flags) 140static void autofs4_dentry_release(struct dentry *de)
155{ 141{
156 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); 142 struct autofs_info *ino = autofs4_dentry_ino(de);
157 struct autofs_info *ino = autofs4_dentry_ino(dentry); 143 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
158 int status;
159
160 DPRINTK("dentry=%p %.*s ino=%p",
161 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
162
163 /*
164 * Wait for a pending mount, triggering one if there
165 * isn't one already
166 */
167 if (dentry->d_inode == NULL) {
168 DPRINTK("waiting for mount name=%.*s",
169 dentry->d_name.len, dentry->d_name.name);
170
171 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
172
173 DPRINTK("mount done status=%d", status);
174
175 /* Turn this into a real negative dentry? */
176 if (status == -ENOENT) {
177 spin_lock(&sbi->fs_lock);
178 ino->flags &= ~AUTOFS_INF_PENDING;
179 spin_unlock(&sbi->fs_lock);
180 return status;
181 } else if (status) {
182 /* Return a negative dentry, but leave it "pending" */
183 return status;
184 }
185 /* Trigger mount for path component or follow link */
186 } else if (ino->flags & AUTOFS_INF_PENDING ||
187 autofs4_need_mount(flags)) {
188 DPRINTK("waiting for mount name=%.*s",
189 dentry->d_name.len, dentry->d_name.name);
190
191 spin_lock(&sbi->fs_lock);
192 ino->flags |= AUTOFS_INF_PENDING;
193 spin_unlock(&sbi->fs_lock);
194 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
195
196 DPRINTK("mount done status=%d", status);
197
198 if (status) {
199 spin_lock(&sbi->fs_lock);
200 ino->flags &= ~AUTOFS_INF_PENDING;
201 spin_unlock(&sbi->fs_lock);
202 return status;
203 }
204 }
205
206 /* Initialize expiry counter after successful mount */
207 ino->last_used = jiffies;
208
209 spin_lock(&sbi->fs_lock);
210 ino->flags &= ~AUTOFS_INF_PENDING;
211 spin_unlock(&sbi->fs_lock);
212
213 return 0;
214}
215
216/* For autofs direct mounts the follow link triggers the mount */
217static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
218{
219 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
220 struct autofs_info *ino = autofs4_dentry_ino(dentry);
221 int oz_mode = autofs4_oz_mode(sbi);
222 unsigned int lookup_type;
223 int status;
224
225 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
226 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
227 nd->flags);
228 /*
229 * For an expire of a covered direct or offset mount we need
230 * to break out of follow_down() at the autofs mount trigger
231 * (d_mounted--), so we can see the expiring flag, and manage
232 * the blocking and following here until the expire is completed.
233 */
234 if (oz_mode) {
235 spin_lock(&sbi->fs_lock);
236 if (ino->flags & AUTOFS_INF_EXPIRING) {
237 spin_unlock(&sbi->fs_lock);
238 /* Follow down to our covering mount. */
239 if (!follow_down(&nd->path))
240 goto done;
241 goto follow;
242 }
243 spin_unlock(&sbi->fs_lock);
244 goto done;
245 }
246
247 /* If an expire request is pending everyone must wait. */
248 autofs4_expire_wait(dentry);
249
250 /* We trigger a mount for almost all flags */
251 lookup_type = autofs4_need_mount(nd->flags);
252 spin_lock(&sbi->fs_lock);
253 spin_lock(&dcache_lock);
254 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
255 spin_unlock(&dcache_lock);
256 spin_unlock(&sbi->fs_lock);
257 goto follow;
258 }
259
260 /*
261 * If the dentry contains directories then it is an autofs
262 * multi-mount with no root mount offset. So don't try to
263 * mount it again.
264 */
265 if (ino->flags & AUTOFS_INF_PENDING ||
266 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
267 spin_unlock(&dcache_lock);
268 spin_unlock(&sbi->fs_lock);
269
270 status = try_to_fill_dentry(dentry, nd->flags);
271 if (status)
272 goto out_error;
273
274 goto follow;
275 }
276 spin_unlock(&dcache_lock);
277 spin_unlock(&sbi->fs_lock);
278follow:
279 /*
280 * If there is no root mount it must be an autofs
281 * multi-mount with no root offset so we don't need
282 * to follow it.
283 */
284 if (d_mountpoint(dentry)) {
285 if (!autofs4_follow_mount(&nd->path)) {
286 status = -ENOENT;
287 goto out_error;
288 }
289 }
290
291done:
292 return NULL;
293
294out_error:
295 path_put(&nd->path);
296 return ERR_PTR(status);
297}
298
299/*
300 * Revalidate is called on every cache lookup. Some of those
301 * cache lookups may actually happen while the dentry is not
302 * yet completely filled in, and revalidate has to delay such
303 * lookups..
304 */
305static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
306{
307 struct inode *dir = dentry->d_parent->d_inode;
308 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
309 int oz_mode = autofs4_oz_mode(sbi);
310 int flags = nd ? nd->flags : 0;
311 int status = 1;
312
313 /* Pending dentry */
314 spin_lock(&sbi->fs_lock);
315 if (autofs4_ispending(dentry)) {
316 /* The daemon never causes a mount to trigger */
317 spin_unlock(&sbi->fs_lock);
318
319 if (oz_mode)
320 return 1;
321
322 /*
323 * If the directory has gone away due to an expire
324 * we have been called as ->d_revalidate() and so
325 * we need to return false and proceed to ->lookup().
326 */
327 if (autofs4_expire_wait(dentry) == -EAGAIN)
328 return 0;
329
330 /*
331 * A zero status is success otherwise we have a
332 * negative error code.
333 */
334 status = try_to_fill_dentry(dentry, flags);
335 if (status == 0)
336 return 1;
337
338 return status;
339 }
340 spin_unlock(&sbi->fs_lock);
341
342 /* Negative dentry.. invalidate if "old" */
343 if (dentry->d_inode == NULL)
344 return 0;
345
346 /* Check for a non-mountpoint directory with no contents */
347 spin_lock(&dcache_lock);
348 if (S_ISDIR(dentry->d_inode->i_mode) &&
349 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
350 DPRINTK("dentry=%p %.*s, emptydir",
351 dentry, dentry->d_name.len, dentry->d_name.name);
352 spin_unlock(&dcache_lock);
353
354 /* The daemon never causes a mount to trigger */
355 if (oz_mode)
356 return 1;
357
358 /*
359 * A zero status is success otherwise we have a
360 * negative error code.
361 */
362 status = try_to_fill_dentry(dentry, flags);
363 if (status == 0)
364 return 1;
365
366 return status;
367 }
368 spin_unlock(&dcache_lock);
369
370 return 1;
371}
372
373void autofs4_dentry_release(struct dentry *de)
374{
375 struct autofs_info *inf;
376 144
377 DPRINTK("releasing %p", de); 145 DPRINTK("releasing %p", de);
378 146
379 inf = autofs4_dentry_ino(de); 147 if (!ino)
380 de->d_fsdata = NULL; 148 return;
381
382 if (inf) {
383 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
384
385 if (sbi) {
386 spin_lock(&sbi->lookup_lock);
387 if (!list_empty(&inf->active))
388 list_del(&inf->active);
389 if (!list_empty(&inf->expiring))
390 list_del(&inf->expiring);
391 spin_unlock(&sbi->lookup_lock);
392 }
393
394 inf->dentry = NULL;
395 inf->inode = NULL;
396 149
397 autofs4_free_ino(inf); 150 if (sbi) {
151 spin_lock(&sbi->lookup_lock);
152 if (!list_empty(&ino->active))
153 list_del(&ino->active);
154 if (!list_empty(&ino->expiring))
155 list_del(&ino->expiring);
156 spin_unlock(&sbi->lookup_lock);
398 } 157 }
399}
400 158
401/* For dentries of directories in the root dir */ 159 autofs4_free_ino(ino);
402static const struct dentry_operations autofs4_root_dentry_operations = { 160}
403 .d_revalidate = autofs4_revalidate,
404 .d_release = autofs4_dentry_release,
405};
406
407/* For other dentries */
408static const struct dentry_operations autofs4_dentry_operations = {
409 .d_revalidate = autofs4_revalidate,
410 .d_release = autofs4_dentry_release,
411};
412 161
413static struct dentry *autofs4_lookup_active(struct dentry *dentry) 162static struct dentry *autofs4_lookup_active(struct dentry *dentry)
414{ 163{
@@ -420,7 +169,6 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry)
420 const unsigned char *str = name->name; 169 const unsigned char *str = name->name;
421 struct list_head *p, *head; 170 struct list_head *p, *head;
422 171
423 spin_lock(&dcache_lock);
424 spin_lock(&sbi->lookup_lock); 172 spin_lock(&sbi->lookup_lock);
425 head = &sbi->active_list; 173 head = &sbi->active_list;
426 list_for_each(p, head) { 174 list_for_each(p, head) {
@@ -434,7 +182,7 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry)
434 spin_lock(&active->d_lock); 182 spin_lock(&active->d_lock);
435 183
436 /* Already gone? */ 184 /* Already gone? */
437 if (atomic_read(&active->d_count) == 0) 185 if (active->d_count == 0)
438 goto next; 186 goto next;
439 187
440 qstr = &active->d_name; 188 qstr = &active->d_name;
@@ -450,17 +198,15 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry)
450 goto next; 198 goto next;
451 199
452 if (d_unhashed(active)) { 200 if (d_unhashed(active)) {
453 dget(active); 201 dget_dlock(active);
454 spin_unlock(&active->d_lock); 202 spin_unlock(&active->d_lock);
455 spin_unlock(&sbi->lookup_lock); 203 spin_unlock(&sbi->lookup_lock);
456 spin_unlock(&dcache_lock);
457 return active; 204 return active;
458 } 205 }
459next: 206next:
460 spin_unlock(&active->d_lock); 207 spin_unlock(&active->d_lock);
461 } 208 }
462 spin_unlock(&sbi->lookup_lock); 209 spin_unlock(&sbi->lookup_lock);
463 spin_unlock(&dcache_lock);
464 210
465 return NULL; 211 return NULL;
466} 212}
@@ -475,7 +221,6 @@ static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
475 const unsigned char *str = name->name; 221 const unsigned char *str = name->name;
476 struct list_head *p, *head; 222 struct list_head *p, *head;
477 223
478 spin_lock(&dcache_lock);
479 spin_lock(&sbi->lookup_lock); 224 spin_lock(&sbi->lookup_lock);
480 head = &sbi->expiring_list; 225 head = &sbi->expiring_list;
481 list_for_each(p, head) { 226 list_for_each(p, head) {
@@ -505,66 +250,250 @@ static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
505 goto next; 250 goto next;
506 251
507 if (d_unhashed(expiring)) { 252 if (d_unhashed(expiring)) {
508 dget(expiring); 253 dget_dlock(expiring);
509 spin_unlock(&expiring->d_lock); 254 spin_unlock(&expiring->d_lock);
510 spin_unlock(&sbi->lookup_lock); 255 spin_unlock(&sbi->lookup_lock);
511 spin_unlock(&dcache_lock);
512 return expiring; 256 return expiring;
513 } 257 }
514next: 258next:
515 spin_unlock(&expiring->d_lock); 259 spin_unlock(&expiring->d_lock);
516 } 260 }
517 spin_unlock(&sbi->lookup_lock); 261 spin_unlock(&sbi->lookup_lock);
518 spin_unlock(&dcache_lock);
519 262
520 return NULL; 263 return NULL;
521} 264}
522 265
266static int autofs4_mount_wait(struct dentry *dentry)
267{
268 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
269 struct autofs_info *ino = autofs4_dentry_ino(dentry);
270 int status = 0;
271
272 if (ino->flags & AUTOFS_INF_PENDING) {
273 DPRINTK("waiting for mount name=%.*s",
274 dentry->d_name.len, dentry->d_name.name);
275 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
276 DPRINTK("mount wait done status=%d", status);
277 }
278 ino->last_used = jiffies;
279 return status;
280}
281
282static int do_expire_wait(struct dentry *dentry)
283{
284 struct dentry *expiring;
285
286 expiring = autofs4_lookup_expiring(dentry);
287 if (!expiring)
288 return autofs4_expire_wait(dentry);
289 else {
290 /*
291 * If we are racing with expire the request might not
292 * be quite complete, but the directory has been removed
293 * so it must have been successful, just wait for it.
294 */
295 autofs4_expire_wait(expiring);
296 autofs4_del_expiring(expiring);
297 dput(expiring);
298 }
299 return 0;
300}
301
302static struct dentry *autofs4_mountpoint_changed(struct path *path)
303{
304 struct dentry *dentry = path->dentry;
305 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
306
307 /*
308 * If this is an indirect mount the dentry could have gone away
309 * as a result of an expire and a new one created.
310 */
311 if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
312 struct dentry *parent = dentry->d_parent;
313 struct autofs_info *ino;
314 struct dentry *new = d_lookup(parent, &dentry->d_name);
315 if (!new)
316 return NULL;
317 ino = autofs4_dentry_ino(new);
318 ino->last_used = jiffies;
319 dput(path->dentry);
320 path->dentry = new;
321 }
322 return path->dentry;
323}
324
325static struct vfsmount *autofs4_d_automount(struct path *path)
326{
327 struct dentry *dentry = path->dentry;
328 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
329 struct autofs_info *ino = autofs4_dentry_ino(dentry);
330 int status;
331
332 DPRINTK("dentry=%p %.*s",
333 dentry, dentry->d_name.len, dentry->d_name.name);
334
335 /* The daemon never triggers a mount. */
336 if (autofs4_oz_mode(sbi))
337 return NULL;
338
339 /*
340 * If an expire request is pending everyone must wait.
341 * If the expire fails we're still mounted so continue
342 * the follow and return. A return of -EAGAIN (which only
343 * happens with indirect mounts) means the expire completed
344 * and the directory was removed, so just go ahead and try
345 * the mount.
346 */
347 status = do_expire_wait(dentry);
348 if (status && status != -EAGAIN)
349 return NULL;
350
351 /* Callback to the daemon to perform the mount or wait */
352 spin_lock(&sbi->fs_lock);
353 if (ino->flags & AUTOFS_INF_PENDING) {
354 spin_unlock(&sbi->fs_lock);
355 status = autofs4_mount_wait(dentry);
356 if (status)
357 return ERR_PTR(status);
358 spin_lock(&sbi->fs_lock);
359 goto done;
360 }
361
362 /*
363 * If the dentry is a symlink it's equivalent to a directory
364 * having d_mountpoint() true, so there's no need to call back
365 * to the daemon.
366 */
367 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
368 goto done;
369 if (!d_mountpoint(dentry)) {
370 /*
371 * It's possible that user space hasn't removed directories
372 * after umounting a rootless multi-mount, although it
373 * should. For v5 have_submounts() is sufficient to handle
374 * this because the leaves of the directory tree under the
375 * mount never trigger mounts themselves (they have an autofs
376 * trigger mount mounted on them). But v4 pseudo direct mounts
377 * do need the leaves to to trigger mounts. In this case we
378 * have no choice but to use the list_empty() check and
379 * require user space behave.
380 */
381 if (sbi->version > 4) {
382 if (have_submounts(dentry))
383 goto done;
384 } else {
385 spin_lock(&dentry->d_lock);
386 if (!list_empty(&dentry->d_subdirs)) {
387 spin_unlock(&dentry->d_lock);
388 goto done;
389 }
390 spin_unlock(&dentry->d_lock);
391 }
392 ino->flags |= AUTOFS_INF_PENDING;
393 spin_unlock(&sbi->fs_lock);
394 status = autofs4_mount_wait(dentry);
395 if (status)
396 return ERR_PTR(status);
397 spin_lock(&sbi->fs_lock);
398 ino->flags &= ~AUTOFS_INF_PENDING;
399 }
400done:
401 if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
402 /*
403 * Any needed mounting has been completed and the path
404 * updated so clear DCACHE_NEED_AUTOMOUNT so we don't
405 * call ->d_automount() on rootless multi-mounts since
406 * it can lead to an incorrect ELOOP error return.
407 *
408 * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
409 * symlinks as in all other cases the dentry will be covered by
410 * an actual mount so ->d_automount() won't be called during
411 * the follow.
412 */
413 spin_lock(&dentry->d_lock);
414 if ((!d_mountpoint(dentry) &&
415 !list_empty(&dentry->d_subdirs)) ||
416 (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
417 __managed_dentry_clear_automount(dentry);
418 spin_unlock(&dentry->d_lock);
419 }
420 spin_unlock(&sbi->fs_lock);
421
422 /* Mount succeeded, check if we ended up with a new dentry */
423 dentry = autofs4_mountpoint_changed(path);
424 if (!dentry)
425 return ERR_PTR(-ENOENT);
426
427 return NULL;
428}
429
430int autofs4_d_manage(struct dentry *dentry, bool rcu_walk)
431{
432 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
433
434 DPRINTK("dentry=%p %.*s",
435 dentry, dentry->d_name.len, dentry->d_name.name);
436
437 /* The daemon never waits. */
438 if (autofs4_oz_mode(sbi)) {
439 if (rcu_walk)
440 return 0;
441 if (!d_mountpoint(dentry))
442 return -EISDIR;
443 return 0;
444 }
445
446 /* We need to sleep, so we need pathwalk to be in ref-mode */
447 if (rcu_walk)
448 return -ECHILD;
449
450 /* Wait for pending expires */
451 do_expire_wait(dentry);
452
453 /*
454 * This dentry may be under construction so wait on mount
455 * completion.
456 */
457 return autofs4_mount_wait(dentry);
458}
459
523/* Lookups in the root directory */ 460/* Lookups in the root directory */
524static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 461static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
525{ 462{
526 struct autofs_sb_info *sbi; 463 struct autofs_sb_info *sbi;
527 struct autofs_info *ino; 464 struct autofs_info *ino;
528 struct dentry *expiring, *active; 465 struct dentry *active;
529 int oz_mode;
530 466
531 DPRINTK("name = %.*s", 467 DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
532 dentry->d_name.len, dentry->d_name.name);
533 468
534 /* File name too long to exist */ 469 /* File name too long to exist */
535 if (dentry->d_name.len > NAME_MAX) 470 if (dentry->d_name.len > NAME_MAX)
536 return ERR_PTR(-ENAMETOOLONG); 471 return ERR_PTR(-ENAMETOOLONG);
537 472
538 sbi = autofs4_sbi(dir->i_sb); 473 sbi = autofs4_sbi(dir->i_sb);
539 oz_mode = autofs4_oz_mode(sbi);
540 474
541 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", 475 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
542 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); 476 current->pid, task_pgrp_nr(current), sbi->catatonic,
477 autofs4_oz_mode(sbi));
543 478
544 active = autofs4_lookup_active(dentry); 479 active = autofs4_lookup_active(dentry);
545 if (active) { 480 if (active) {
546 dentry = active; 481 return active;
547 ino = autofs4_dentry_ino(dentry);
548 } else { 482 } else {
549 /* 483 /*
550 * Mark the dentry incomplete but don't hash it. We do this 484 * A dentry that is not within the root can never trigger a
551 * to serialize our inode creation operations (symlink and 485 * mount operation, unless the directory already exists, so we
552 * mkdir) which prevents deadlock during the callback to 486 * can return fail immediately. The daemon however does need
553 * the daemon. Subsequent user space lookups for the same 487 * to create directories within the file system.
554 * dentry are placed on the wait queue while the daemon
555 * itself is allowed passage unresticted so the create
556 * operation itself can then hash the dentry. Finally,
557 * we check for the hashed dentry and return the newly
558 * hashed dentry.
559 */ 488 */
560 dentry->d_op = &autofs4_root_dentry_operations; 489 if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
490 return ERR_PTR(-ENOENT);
561 491
562 /* 492 /* Mark entries in the root as mount triggers */
563 * And we need to ensure that the same dentry is used for 493 if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent))
564 * all following lookup calls until it is hashed so that 494 __managed_dentry_set_managed(dentry);
565 * the dentry flags are persistent throughout the request. 495
566 */ 496 ino = autofs4_new_ino(sbi);
567 ino = autofs4_init_ino(NULL, sbi, 0555);
568 if (!ino) 497 if (!ino)
569 return ERR_PTR(-ENOMEM); 498 return ERR_PTR(-ENOMEM);
570 499
@@ -575,82 +504,6 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
575 504
576 d_instantiate(dentry, NULL); 505 d_instantiate(dentry, NULL);
577 } 506 }
578
579 if (!oz_mode) {
580 mutex_unlock(&dir->i_mutex);
581 expiring = autofs4_lookup_expiring(dentry);
582 if (expiring) {
583 /*
584 * If we are racing with expire the request might not
585 * be quite complete but the directory has been removed
586 * so it must have been successful, so just wait for it.
587 */
588 autofs4_expire_wait(expiring);
589 autofs4_del_expiring(expiring);
590 dput(expiring);
591 }
592
593 spin_lock(&sbi->fs_lock);
594 ino->flags |= AUTOFS_INF_PENDING;
595 spin_unlock(&sbi->fs_lock);
596 if (dentry->d_op && dentry->d_op->d_revalidate)
597 (dentry->d_op->d_revalidate)(dentry, nd);
598 mutex_lock(&dir->i_mutex);
599 }
600
601 /*
602 * If we are still pending, check if we had to handle
603 * a signal. If so we can force a restart..
604 */
605 if (ino->flags & AUTOFS_INF_PENDING) {
606 /* See if we were interrupted */
607 if (signal_pending(current)) {
608 sigset_t *sigset = &current->pending.signal;
609 if (sigismember (sigset, SIGKILL) ||
610 sigismember (sigset, SIGQUIT) ||
611 sigismember (sigset, SIGINT)) {
612 if (active)
613 dput(active);
614 return ERR_PTR(-ERESTARTNOINTR);
615 }
616 }
617 if (!oz_mode) {
618 spin_lock(&sbi->fs_lock);
619 ino->flags &= ~AUTOFS_INF_PENDING;
620 spin_unlock(&sbi->fs_lock);
621 }
622 }
623
624 /*
625 * If this dentry is unhashed, then we shouldn't honour this
626 * lookup. Returning ENOENT here doesn't do the right thing
627 * for all system calls, but it should be OK for the operations
628 * we permit from an autofs.
629 */
630 if (!oz_mode && d_unhashed(dentry)) {
631 /*
632 * A user space application can (and has done in the past)
633 * remove and re-create this directory during the callback.
634 * This can leave us with an unhashed dentry, but a
635 * successful mount! So we need to perform another
636 * cached lookup in case the dentry now exists.
637 */
638 struct dentry *parent = dentry->d_parent;
639 struct dentry *new = d_lookup(parent, &dentry->d_name);
640 if (new != NULL)
641 dentry = new;
642 else
643 dentry = ERR_PTR(-ENOENT);
644
645 if (active)
646 dput(active);
647
648 return dentry;
649 }
650
651 if (active)
652 return active;
653
654 return NULL; 507 return NULL;
655} 508}
656 509
@@ -662,6 +515,7 @@ static int autofs4_dir_symlink(struct inode *dir,
662 struct autofs_info *ino = autofs4_dentry_ino(dentry); 515 struct autofs_info *ino = autofs4_dentry_ino(dentry);
663 struct autofs_info *p_ino; 516 struct autofs_info *p_ino;
664 struct inode *inode; 517 struct inode *inode;
518 size_t size = strlen(symname);
665 char *cp; 519 char *cp;
666 520
667 DPRINTK("%s <- %.*s", symname, 521 DPRINTK("%s <- %.*s", symname,
@@ -670,45 +524,35 @@ static int autofs4_dir_symlink(struct inode *dir,
670 if (!autofs4_oz_mode(sbi)) 524 if (!autofs4_oz_mode(sbi))
671 return -EACCES; 525 return -EACCES;
672 526
673 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); 527 BUG_ON(!ino);
674 if (!ino) 528
675 return -ENOMEM; 529 autofs4_clean_ino(ino);
676 530
677 autofs4_del_active(dentry); 531 autofs4_del_active(dentry);
678 532
679 ino->size = strlen(symname); 533 cp = kmalloc(size + 1, GFP_KERNEL);
680 cp = kmalloc(ino->size + 1, GFP_KERNEL); 534 if (!cp)
681 if (!cp) {
682 if (!dentry->d_fsdata)
683 kfree(ino);
684 return -ENOMEM; 535 return -ENOMEM;
685 }
686 536
687 strcpy(cp, symname); 537 strcpy(cp, symname);
688 538
689 inode = autofs4_get_inode(dir->i_sb, ino); 539 inode = autofs4_get_inode(dir->i_sb, S_IFLNK | 0555);
690 if (!inode) { 540 if (!inode) {
691 kfree(cp); 541 kfree(cp);
692 if (!dentry->d_fsdata) 542 if (!dentry->d_fsdata)
693 kfree(ino); 543 kfree(ino);
694 return -ENOMEM; 544 return -ENOMEM;
695 } 545 }
546 inode->i_private = cp;
547 inode->i_size = size;
696 d_add(dentry, inode); 548 d_add(dentry, inode);
697 549
698 if (dir == dir->i_sb->s_root->d_inode) 550 dget(dentry);
699 dentry->d_op = &autofs4_root_dentry_operations;
700 else
701 dentry->d_op = &autofs4_dentry_operations;
702
703 dentry->d_fsdata = ino;
704 ino->dentry = dget(dentry);
705 atomic_inc(&ino->count); 551 atomic_inc(&ino->count);
706 p_ino = autofs4_dentry_ino(dentry->d_parent); 552 p_ino = autofs4_dentry_ino(dentry->d_parent);
707 if (p_ino && dentry->d_parent != dentry) 553 if (p_ino && dentry->d_parent != dentry)
708 atomic_inc(&p_ino->count); 554 atomic_inc(&p_ino->count);
709 ino->inode = inode;
710 555
711 ino->u.symlink = cp;
712 dir->i_mtime = CURRENT_TIME; 556 dir->i_mtime = CURRENT_TIME;
713 557
714 return 0; 558 return 0;
@@ -751,16 +595,68 @@ static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
751 595
752 dir->i_mtime = CURRENT_TIME; 596 dir->i_mtime = CURRENT_TIME;
753 597
754 spin_lock(&dcache_lock); 598 spin_lock(&sbi->lookup_lock);
755 autofs4_add_expiring(dentry); 599 __autofs4_add_expiring(dentry);
756 spin_lock(&dentry->d_lock); 600 spin_lock(&dentry->d_lock);
757 __d_drop(dentry); 601 __d_drop(dentry);
758 spin_unlock(&dentry->d_lock); 602 spin_unlock(&dentry->d_lock);
759 spin_unlock(&dcache_lock); 603 spin_unlock(&sbi->lookup_lock);
760 604
761 return 0; 605 return 0;
762} 606}
763 607
608/*
609 * Version 4 of autofs provides a pseudo direct mount implementation
610 * that relies on directories at the leaves of a directory tree under
611 * an indirect mount to trigger mounts. To allow for this we need to
612 * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
613 * of the directory tree. There is no need to clear the automount flag
614 * following a mount or restore it after an expire because these mounts
615 * are always covered. However, it is necessary to ensure that these
616 * flags are clear on non-empty directories to avoid unnecessary calls
617 * during path walks.
618 */
619static void autofs_set_leaf_automount_flags(struct dentry *dentry)
620{
621 struct dentry *parent;
622
623 /* root and dentrys in the root are already handled */
624 if (IS_ROOT(dentry->d_parent))
625 return;
626
627 managed_dentry_set_managed(dentry);
628
629 parent = dentry->d_parent;
630 /* only consider parents below dentrys in the root */
631 if (IS_ROOT(parent->d_parent))
632 return;
633 managed_dentry_clear_managed(parent);
634 return;
635}
636
637static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
638{
639 struct list_head *d_child;
640 struct dentry *parent;
641
642 /* flags for dentrys in the root are handled elsewhere */
643 if (IS_ROOT(dentry->d_parent))
644 return;
645
646 managed_dentry_clear_managed(dentry);
647
648 parent = dentry->d_parent;
649 /* only consider parents below dentrys in the root */
650 if (IS_ROOT(parent->d_parent))
651 return;
652 d_child = &dentry->d_u.d_child;
653 /* Set parent managed if it's becoming empty */
654 if (d_child->next == &parent->d_subdirs &&
655 d_child->prev == &parent->d_subdirs)
656 managed_dentry_set_managed(parent);
657 return;
658}
659
764static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) 660static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
765{ 661{
766 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); 662 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
@@ -773,16 +669,20 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
773 if (!autofs4_oz_mode(sbi)) 669 if (!autofs4_oz_mode(sbi))
774 return -EACCES; 670 return -EACCES;
775 671
776 spin_lock(&dcache_lock); 672 spin_lock(&sbi->lookup_lock);
673 spin_lock(&dentry->d_lock);
777 if (!list_empty(&dentry->d_subdirs)) { 674 if (!list_empty(&dentry->d_subdirs)) {
778 spin_unlock(&dcache_lock); 675 spin_unlock(&dentry->d_lock);
676 spin_unlock(&sbi->lookup_lock);
779 return -ENOTEMPTY; 677 return -ENOTEMPTY;
780 } 678 }
781 autofs4_add_expiring(dentry); 679 __autofs4_add_expiring(dentry);
782 spin_lock(&dentry->d_lock);
783 __d_drop(dentry); 680 __d_drop(dentry);
784 spin_unlock(&dentry->d_lock); 681 spin_unlock(&dentry->d_lock);
785 spin_unlock(&dcache_lock); 682 spin_unlock(&sbi->lookup_lock);
683
684 if (sbi->version < 5)
685 autofs_clear_leaf_automount_flags(dentry);
786 686
787 if (atomic_dec_and_test(&ino->count)) { 687 if (atomic_dec_and_test(&ino->count)) {
788 p_ino = autofs4_dentry_ino(dentry->d_parent); 688 p_ino = autofs4_dentry_ino(dentry->d_parent);
@@ -812,32 +712,25 @@ static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
812 DPRINTK("dentry %p, creating %.*s", 712 DPRINTK("dentry %p, creating %.*s",
813 dentry, dentry->d_name.len, dentry->d_name.name); 713 dentry, dentry->d_name.len, dentry->d_name.name);
814 714
815 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); 715 BUG_ON(!ino);
816 if (!ino) 716
817 return -ENOMEM; 717 autofs4_clean_ino(ino);
818 718
819 autofs4_del_active(dentry); 719 autofs4_del_active(dentry);
820 720
821 inode = autofs4_get_inode(dir->i_sb, ino); 721 inode = autofs4_get_inode(dir->i_sb, S_IFDIR | 0555);
822 if (!inode) { 722 if (!inode)
823 if (!dentry->d_fsdata)
824 kfree(ino);
825 return -ENOMEM; 723 return -ENOMEM;
826 }
827 d_add(dentry, inode); 724 d_add(dentry, inode);
828 725
829 if (dir == dir->i_sb->s_root->d_inode) 726 if (sbi->version < 5)
830 dentry->d_op = &autofs4_root_dentry_operations; 727 autofs_set_leaf_automount_flags(dentry);
831 else
832 dentry->d_op = &autofs4_dentry_operations;
833 728
834 dentry->d_fsdata = ino; 729 dget(dentry);
835 ino->dentry = dget(dentry);
836 atomic_inc(&ino->count); 730 atomic_inc(&ino->count);
837 p_ino = autofs4_dentry_ino(dentry->d_parent); 731 p_ino = autofs4_dentry_ino(dentry->d_parent);
838 if (p_ino && dentry->d_parent != dentry) 732 if (p_ino && dentry->d_parent != dentry)
839 atomic_inc(&p_ino->count); 733 atomic_inc(&p_ino->count);
840 ino->inode = inode;
841 inc_nlink(dir); 734 inc_nlink(dir);
842 dir->i_mtime = CURRENT_TIME; 735 dir->i_mtime = CURRENT_TIME;
843 736
@@ -919,8 +812,7 @@ static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
919int is_autofs4_dentry(struct dentry *dentry) 812int is_autofs4_dentry(struct dentry *dentry)
920{ 813{
921 return dentry && dentry->d_inode && 814 return dentry && dentry->d_inode &&
922 (dentry->d_op == &autofs4_root_dentry_operations || 815 dentry->d_op == &autofs4_dentry_operations &&
923 dentry->d_op == &autofs4_dentry_operations) &&
924 dentry->d_fsdata != NULL; 816 dentry->d_fsdata != NULL;
925} 817}
926 818
@@ -981,14 +873,8 @@ static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
981static long autofs4_root_ioctl(struct file *filp, 873static long autofs4_root_ioctl(struct file *filp,
982 unsigned int cmd, unsigned long arg) 874 unsigned int cmd, unsigned long arg)
983{ 875{
984 long ret;
985 struct inode *inode = filp->f_dentry->d_inode; 876 struct inode *inode = filp->f_dentry->d_inode;
986 877 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
987 lock_kernel();
988 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
989 unlock_kernel();
990
991 return ret;
992} 878}
993 879
994#ifdef CONFIG_COMPAT 880#ifdef CONFIG_COMPAT
@@ -998,13 +884,11 @@ static long autofs4_root_compat_ioctl(struct file *filp,
998 struct inode *inode = filp->f_path.dentry->d_inode; 884 struct inode *inode = filp->f_path.dentry->d_inode;
999 int ret; 885 int ret;
1000 886
1001 lock_kernel();
1002 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL) 887 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1003 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg); 888 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1004 else 889 else
1005 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, 890 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1006 (unsigned long)compat_ptr(arg)); 891 (unsigned long)compat_ptr(arg));
1007 unlock_kernel();
1008 892
1009 return ret; 893 return ret;
1010} 894}