aboutsummaryrefslogtreecommitdiffstats
path: root/fs/autofs4
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
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'fs/autofs4')
-rw-r--r--fs/autofs4/autofs_i.h132
-rw-r--r--fs/autofs4/dev-ioctl.c7
-rw-r--r--fs/autofs4/expire.c212
-rw-r--r--fs/autofs4/init.c8
-rw-r--r--fs/autofs4/inode.c115
-rw-r--r--fs/autofs4/root.c772
-rw-r--r--fs/autofs4/symlink.c3
-rw-r--r--fs/autofs4/waitq.c40
8 files changed, 625 insertions, 664 deletions
diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
index 3d283abf67d7..475f9c597cb7 100644
--- a/fs/autofs4/autofs_i.h
+++ b/fs/autofs4/autofs_i.h
@@ -16,6 +16,7 @@
16#include <linux/auto_fs4.h> 16#include <linux/auto_fs4.h>
17#include <linux/auto_dev-ioctl.h> 17#include <linux/auto_dev-ioctl.h>
18#include <linux/mutex.h> 18#include <linux/mutex.h>
19#include <linux/spinlock.h>
19#include <linux/list.h> 20#include <linux/list.h>
20 21
21/* This is the range of ioctl() numbers we claim as ours */ 22/* This is the range of ioctl() numbers we claim as ours */
@@ -85,18 +86,9 @@ struct autofs_info {
85 86
86 uid_t uid; 87 uid_t uid;
87 gid_t gid; 88 gid_t gid;
88
89 mode_t mode;
90 size_t size;
91
92 void (*free)(struct autofs_info *);
93 union {
94 const char *symlink;
95 } u;
96}; 89};
97 90
98#define AUTOFS_INF_EXPIRING (1<<0) /* dentry is in the process of expiring */ 91#define AUTOFS_INF_EXPIRING (1<<0) /* dentry is in the process of expiring */
99#define AUTOFS_INF_MOUNTPOINT (1<<1) /* mountpoint status for direct expire */
100#define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */ 92#define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */
101 93
102struct autofs_wait_queue { 94struct autofs_wait_queue {
@@ -173,14 +165,7 @@ static inline int autofs4_ispending(struct dentry *dentry)
173 return 0; 165 return 0;
174} 166}
175 167
176static inline void autofs4_copy_atime(struct file *src, struct file *dst) 168struct inode *autofs4_get_inode(struct super_block *, mode_t);
177{
178 dst->f_path.dentry->d_inode->i_atime =
179 src->f_path.dentry->d_inode->i_atime;
180 return;
181}
182
183struct inode *autofs4_get_inode(struct super_block *, struct autofs_info *);
184void autofs4_free_ino(struct autofs_info *); 169void autofs4_free_ino(struct autofs_info *);
185 170
186/* Expiration */ 171/* Expiration */
@@ -209,16 +194,89 @@ void autofs_dev_ioctl_exit(void);
209 194
210extern const struct inode_operations autofs4_symlink_inode_operations; 195extern const struct inode_operations autofs4_symlink_inode_operations;
211extern const struct inode_operations autofs4_dir_inode_operations; 196extern const struct inode_operations autofs4_dir_inode_operations;
212extern const struct inode_operations autofs4_root_inode_operations;
213extern const struct inode_operations autofs4_indirect_root_inode_operations;
214extern const struct inode_operations autofs4_direct_root_inode_operations;
215extern const struct file_operations autofs4_dir_operations; 197extern const struct file_operations autofs4_dir_operations;
216extern const struct file_operations autofs4_root_operations; 198extern const struct file_operations autofs4_root_operations;
199extern const struct dentry_operations autofs4_dentry_operations;
200
201/* VFS automount flags management functions */
202
203static inline void __managed_dentry_set_automount(struct dentry *dentry)
204{
205 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
206}
207
208static inline void managed_dentry_set_automount(struct dentry *dentry)
209{
210 spin_lock(&dentry->d_lock);
211 __managed_dentry_set_automount(dentry);
212 spin_unlock(&dentry->d_lock);
213}
214
215static inline void __managed_dentry_clear_automount(struct dentry *dentry)
216{
217 dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
218}
219
220static inline void managed_dentry_clear_automount(struct dentry *dentry)
221{
222 spin_lock(&dentry->d_lock);
223 __managed_dentry_clear_automount(dentry);
224 spin_unlock(&dentry->d_lock);
225}
226
227static inline void __managed_dentry_set_transit(struct dentry *dentry)
228{
229 dentry->d_flags |= DCACHE_MANAGE_TRANSIT;
230}
231
232static inline void managed_dentry_set_transit(struct dentry *dentry)
233{
234 spin_lock(&dentry->d_lock);
235 __managed_dentry_set_transit(dentry);
236 spin_unlock(&dentry->d_lock);
237}
238
239static inline void __managed_dentry_clear_transit(struct dentry *dentry)
240{
241 dentry->d_flags &= ~DCACHE_MANAGE_TRANSIT;
242}
243
244static inline void managed_dentry_clear_transit(struct dentry *dentry)
245{
246 spin_lock(&dentry->d_lock);
247 __managed_dentry_clear_transit(dentry);
248 spin_unlock(&dentry->d_lock);
249}
250
251static inline void __managed_dentry_set_managed(struct dentry *dentry)
252{
253 dentry->d_flags |= (DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
254}
255
256static inline void managed_dentry_set_managed(struct dentry *dentry)
257{
258 spin_lock(&dentry->d_lock);
259 __managed_dentry_set_managed(dentry);
260 spin_unlock(&dentry->d_lock);
261}
262
263static inline void __managed_dentry_clear_managed(struct dentry *dentry)
264{
265 dentry->d_flags &= ~(DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
266}
267
268static inline void managed_dentry_clear_managed(struct dentry *dentry)
269{
270 spin_lock(&dentry->d_lock);
271 __managed_dentry_clear_managed(dentry);
272 spin_unlock(&dentry->d_lock);
273}
217 274
218/* Initializing function */ 275/* Initializing function */
219 276
220int autofs4_fill_super(struct super_block *, void *, int); 277int autofs4_fill_super(struct super_block *, void *, int);
221struct autofs_info *autofs4_init_ino(struct autofs_info *, struct autofs_sb_info *sbi, mode_t mode); 278struct autofs_info *autofs4_new_ino(struct autofs_sb_info *);
279void autofs4_clean_ino(struct autofs_info *);
222 280
223/* Queue management functions */ 281/* Queue management functions */
224 282
@@ -226,19 +284,6 @@ int autofs4_wait(struct autofs_sb_info *,struct dentry *, enum autofs_notify);
226int autofs4_wait_release(struct autofs_sb_info *,autofs_wqt_t,int); 284int autofs4_wait_release(struct autofs_sb_info *,autofs_wqt_t,int);
227void autofs4_catatonic_mode(struct autofs_sb_info *); 285void autofs4_catatonic_mode(struct autofs_sb_info *);
228 286
229static inline int autofs4_follow_mount(struct path *path)
230{
231 int res = 0;
232
233 while (d_mountpoint(path->dentry)) {
234 int followed = follow_down(path);
235 if (!followed)
236 break;
237 res = 1;
238 }
239 return res;
240}
241
242static inline u32 autofs4_get_dev(struct autofs_sb_info *sbi) 287static inline u32 autofs4_get_dev(struct autofs_sb_info *sbi)
243{ 288{
244 return new_encode_dev(sbi->sb->s_dev); 289 return new_encode_dev(sbi->sb->s_dev);
@@ -254,17 +299,15 @@ static inline int simple_positive(struct dentry *dentry)
254 return dentry->d_inode && !d_unhashed(dentry); 299 return dentry->d_inode && !d_unhashed(dentry);
255} 300}
256 301
257static inline int __simple_empty(struct dentry *dentry) 302static inline void __autofs4_add_expiring(struct dentry *dentry)
258{ 303{
259 struct dentry *child; 304 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
260 int ret = 0; 305 struct autofs_info *ino = autofs4_dentry_ino(dentry);
261 306 if (ino) {
262 list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child) 307 if (list_empty(&ino->expiring))
263 if (simple_positive(child)) 308 list_add(&ino->expiring, &sbi->expiring_list);
264 goto out; 309 }
265 ret = 1; 310 return;
266out:
267 return ret;
268} 311}
269 312
270static inline void autofs4_add_expiring(struct dentry *dentry) 313static inline void autofs4_add_expiring(struct dentry *dentry)
@@ -293,5 +336,4 @@ static inline void autofs4_del_expiring(struct dentry *dentry)
293 return; 336 return;
294} 337}
295 338
296void autofs4_dentry_release(struct dentry *);
297extern void autofs4_kill_sb(struct super_block *); 339extern void autofs4_kill_sb(struct super_block *);
diff --git a/fs/autofs4/dev-ioctl.c b/fs/autofs4/dev-ioctl.c
index ba4a38b9c22f..509fe1eb66ae 100644
--- a/fs/autofs4/dev-ioctl.c
+++ b/fs/autofs4/dev-ioctl.c
@@ -372,6 +372,10 @@ static int autofs_dev_ioctl_setpipefd(struct file *fp,
372 return -EBUSY; 372 return -EBUSY;
373 } else { 373 } else {
374 struct file *pipe = fget(pipefd); 374 struct file *pipe = fget(pipefd);
375 if (!pipe) {
376 err = -EBADF;
377 goto out;
378 }
375 if (!pipe->f_op || !pipe->f_op->write) { 379 if (!pipe->f_op || !pipe->f_op->write) {
376 err = -EPIPE; 380 err = -EPIPE;
377 fput(pipe); 381 fput(pipe);
@@ -551,7 +555,7 @@ static int autofs_dev_ioctl_ismountpoint(struct file *fp,
551 555
552 err = have_submounts(path.dentry); 556 err = have_submounts(path.dentry);
553 557
554 if (follow_down(&path)) 558 if (follow_down_one(&path))
555 magic = path.mnt->mnt_sb->s_magic; 559 magic = path.mnt->mnt_sb->s_magic;
556 } 560 }
557 561
@@ -724,6 +728,7 @@ static const struct file_operations _dev_ioctl_fops = {
724 .unlocked_ioctl = autofs_dev_ioctl, 728 .unlocked_ioctl = autofs_dev_ioctl,
725 .compat_ioctl = autofs_dev_ioctl_compat, 729 .compat_ioctl = autofs_dev_ioctl_compat,
726 .owner = THIS_MODULE, 730 .owner = THIS_MODULE,
731 .llseek = noop_llseek,
727}; 732};
728 733
729static struct miscdevice _autofs_dev_ioctl_misc = { 734static struct miscdevice _autofs_dev_ioctl_misc = {
diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c
index a796c9417fb1..450f529a4eae 100644
--- a/fs/autofs4/expire.c
+++ b/fs/autofs4/expire.c
@@ -26,10 +26,6 @@ static inline int autofs4_can_expire(struct dentry *dentry,
26 if (ino == NULL) 26 if (ino == NULL)
27 return 0; 27 return 0;
28 28
29 /* No point expiring a pending mount */
30 if (ino->flags & AUTOFS_INF_PENDING)
31 return 0;
32
33 if (!do_now) { 29 if (!do_now) {
34 /* Too young to die */ 30 /* Too young to die */
35 if (!timeout || time_after(ino->last_used + timeout, now)) 31 if (!timeout || time_after(ino->last_used + timeout, now))
@@ -56,7 +52,7 @@ static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
56 52
57 path_get(&path); 53 path_get(&path);
58 54
59 if (!follow_down(&path)) 55 if (!follow_down_one(&path))
60 goto done; 56 goto done;
61 57
62 if (is_autofs4_dentry(path.dentry)) { 58 if (is_autofs4_dentry(path.dentry)) {
@@ -91,24 +87,116 @@ done:
91} 87}
92 88
93/* 89/*
94 * Calculate next entry in top down tree traversal. 90 * Calculate and dget next entry in the subdirs list under root.
95 * From next_mnt in namespace.c - elegant.
96 */ 91 */
97static struct dentry *next_dentry(struct dentry *p, struct dentry *root) 92static struct dentry *get_next_positive_subdir(struct dentry *prev,
93 struct dentry *root)
98{ 94{
99 struct list_head *next = p->d_subdirs.next; 95 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
96 struct list_head *next;
97 struct dentry *p, *q;
98
99 spin_lock(&sbi->lookup_lock);
100 100
101 if (prev == NULL) {
102 spin_lock(&root->d_lock);
103 prev = dget_dlock(root);
104 next = prev->d_subdirs.next;
105 p = prev;
106 goto start;
107 }
108
109 p = prev;
110 spin_lock(&p->d_lock);
111again:
112 next = p->d_u.d_child.next;
113start:
114 if (next == &root->d_subdirs) {
115 spin_unlock(&p->d_lock);
116 spin_unlock(&sbi->lookup_lock);
117 dput(prev);
118 return NULL;
119 }
120
121 q = list_entry(next, struct dentry, d_u.d_child);
122
123 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
124 /* Negative dentry - try next */
125 if (!simple_positive(q)) {
126 spin_unlock(&p->d_lock);
127 p = q;
128 goto again;
129 }
130 dget_dlock(q);
131 spin_unlock(&q->d_lock);
132 spin_unlock(&p->d_lock);
133 spin_unlock(&sbi->lookup_lock);
134
135 dput(prev);
136
137 return q;
138}
139
140/*
141 * Calculate and dget next entry in top down tree traversal.
142 */
143static struct dentry *get_next_positive_dentry(struct dentry *prev,
144 struct dentry *root)
145{
146 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
147 struct list_head *next;
148 struct dentry *p, *ret;
149
150 if (prev == NULL)
151 return dget(root);
152
153 spin_lock(&sbi->lookup_lock);
154relock:
155 p = prev;
156 spin_lock(&p->d_lock);
157again:
158 next = p->d_subdirs.next;
101 if (next == &p->d_subdirs) { 159 if (next == &p->d_subdirs) {
102 while (1) { 160 while (1) {
103 if (p == root) 161 struct dentry *parent;
162
163 if (p == root) {
164 spin_unlock(&p->d_lock);
165 spin_unlock(&sbi->lookup_lock);
166 dput(prev);
104 return NULL; 167 return NULL;
168 }
169
170 parent = p->d_parent;
171 if (!spin_trylock(&parent->d_lock)) {
172 spin_unlock(&p->d_lock);
173 cpu_relax();
174 goto relock;
175 }
176 spin_unlock(&p->d_lock);
105 next = p->d_u.d_child.next; 177 next = p->d_u.d_child.next;
106 if (next != &p->d_parent->d_subdirs) 178 p = parent;
179 if (next != &parent->d_subdirs)
107 break; 180 break;
108 p = p->d_parent;
109 } 181 }
110 } 182 }
111 return list_entry(next, struct dentry, d_u.d_child); 183 ret = list_entry(next, struct dentry, d_u.d_child);
184
185 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
186 /* Negative dentry - try next */
187 if (!simple_positive(ret)) {
188 spin_unlock(&p->d_lock);
189 p = ret;
190 goto again;
191 }
192 dget_dlock(ret);
193 spin_unlock(&ret->d_lock);
194 spin_unlock(&p->d_lock);
195 spin_unlock(&sbi->lookup_lock);
196
197 dput(prev);
198
199 return ret;
112} 200}
113 201
114/* 202/*
@@ -158,18 +246,11 @@ static int autofs4_tree_busy(struct vfsmount *mnt,
158 if (!simple_positive(top)) 246 if (!simple_positive(top))
159 return 1; 247 return 1;
160 248
161 spin_lock(&dcache_lock); 249 p = NULL;
162 for (p = top; p; p = next_dentry(p, top)) { 250 while ((p = get_next_positive_dentry(p, top))) {
163 /* Negative dentry - give up */
164 if (!simple_positive(p))
165 continue;
166
167 DPRINTK("dentry %p %.*s", 251 DPRINTK("dentry %p %.*s",
168 p, (int) p->d_name.len, p->d_name.name); 252 p, (int) p->d_name.len, p->d_name.name);
169 253
170 p = dget(p);
171 spin_unlock(&dcache_lock);
172
173 /* 254 /*
174 * Is someone visiting anywhere in the subtree ? 255 * Is someone visiting anywhere in the subtree ?
175 * If there's no mount we need to check the usage 256 * If there's no mount we need to check the usage
@@ -198,16 +279,13 @@ static int autofs4_tree_busy(struct vfsmount *mnt,
198 else 279 else
199 ino_count++; 280 ino_count++;
200 281
201 if (atomic_read(&p->d_count) > ino_count) { 282 if (p->d_count > ino_count) {
202 top_ino->last_used = jiffies; 283 top_ino->last_used = jiffies;
203 dput(p); 284 dput(p);
204 return 1; 285 return 1;
205 } 286 }
206 } 287 }
207 dput(p);
208 spin_lock(&dcache_lock);
209 } 288 }
210 spin_unlock(&dcache_lock);
211 289
212 /* Timeout of a tree mount is ultimately determined by its top dentry */ 290 /* Timeout of a tree mount is ultimately determined by its top dentry */
213 if (!autofs4_can_expire(top, timeout, do_now)) 291 if (!autofs4_can_expire(top, timeout, do_now))
@@ -226,32 +304,21 @@ static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
226 DPRINTK("parent %p %.*s", 304 DPRINTK("parent %p %.*s",
227 parent, (int)parent->d_name.len, parent->d_name.name); 305 parent, (int)parent->d_name.len, parent->d_name.name);
228 306
229 spin_lock(&dcache_lock); 307 p = NULL;
230 for (p = parent; p; p = next_dentry(p, parent)) { 308 while ((p = get_next_positive_dentry(p, parent))) {
231 /* Negative dentry - give up */
232 if (!simple_positive(p))
233 continue;
234
235 DPRINTK("dentry %p %.*s", 309 DPRINTK("dentry %p %.*s",
236 p, (int) p->d_name.len, p->d_name.name); 310 p, (int) p->d_name.len, p->d_name.name);
237 311
238 p = dget(p);
239 spin_unlock(&dcache_lock);
240
241 if (d_mountpoint(p)) { 312 if (d_mountpoint(p)) {
242 /* Can we umount this guy */ 313 /* Can we umount this guy */
243 if (autofs4_mount_busy(mnt, p)) 314 if (autofs4_mount_busy(mnt, p))
244 goto cont; 315 continue;
245 316
246 /* Can we expire this guy */ 317 /* Can we expire this guy */
247 if (autofs4_can_expire(p, timeout, do_now)) 318 if (autofs4_can_expire(p, timeout, do_now))
248 return p; 319 return p;
249 } 320 }
250cont:
251 dput(p);
252 spin_lock(&dcache_lock);
253 } 321 }
254 spin_unlock(&dcache_lock);
255 return NULL; 322 return NULL;
256} 323}
257 324
@@ -264,6 +331,7 @@ struct dentry *autofs4_expire_direct(struct super_block *sb,
264 unsigned long timeout; 331 unsigned long timeout;
265 struct dentry *root = dget(sb->s_root); 332 struct dentry *root = dget(sb->s_root);
266 int do_now = how & AUTOFS_EXP_IMMEDIATE; 333 int do_now = how & AUTOFS_EXP_IMMEDIATE;
334 struct autofs_info *ino;
267 335
268 if (!root) 336 if (!root)
269 return NULL; 337 return NULL;
@@ -272,17 +340,18 @@ struct dentry *autofs4_expire_direct(struct super_block *sb,
272 timeout = sbi->exp_timeout; 340 timeout = sbi->exp_timeout;
273 341
274 spin_lock(&sbi->fs_lock); 342 spin_lock(&sbi->fs_lock);
343 ino = autofs4_dentry_ino(root);
344 /* No point expiring a pending mount */
345 if (ino->flags & AUTOFS_INF_PENDING)
346 goto out;
275 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) { 347 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
276 struct autofs_info *ino = autofs4_dentry_ino(root); 348 struct autofs_info *ino = autofs4_dentry_ino(root);
277 if (d_mountpoint(root)) {
278 ino->flags |= AUTOFS_INF_MOUNTPOINT;
279 root->d_mounted--;
280 }
281 ino->flags |= AUTOFS_INF_EXPIRING; 349 ino->flags |= AUTOFS_INF_EXPIRING;
282 init_completion(&ino->expire_complete); 350 init_completion(&ino->expire_complete);
283 spin_unlock(&sbi->fs_lock); 351 spin_unlock(&sbi->fs_lock);
284 return root; 352 return root;
285 } 353 }
354out:
286 spin_unlock(&sbi->fs_lock); 355 spin_unlock(&sbi->fs_lock);
287 dput(root); 356 dput(root);
288 357
@@ -302,8 +371,8 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
302{ 371{
303 unsigned long timeout; 372 unsigned long timeout;
304 struct dentry *root = sb->s_root; 373 struct dentry *root = sb->s_root;
374 struct dentry *dentry;
305 struct dentry *expired = NULL; 375 struct dentry *expired = NULL;
306 struct list_head *next;
307 int do_now = how & AUTOFS_EXP_IMMEDIATE; 376 int do_now = how & AUTOFS_EXP_IMMEDIATE;
308 int exp_leaves = how & AUTOFS_EXP_LEAVES; 377 int exp_leaves = how & AUTOFS_EXP_LEAVES;
309 struct autofs_info *ino; 378 struct autofs_info *ino;
@@ -315,25 +384,13 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
315 now = jiffies; 384 now = jiffies;
316 timeout = sbi->exp_timeout; 385 timeout = sbi->exp_timeout;
317 386
318 spin_lock(&dcache_lock); 387 dentry = NULL;
319 next = root->d_subdirs.next; 388 while ((dentry = get_next_positive_subdir(dentry, root))) {
320
321 /* On exit from the loop expire is set to a dgot dentry
322 * to expire or it's NULL */
323 while ( next != &root->d_subdirs ) {
324 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
325
326 /* Negative dentry - give up */
327 if (!simple_positive(dentry)) {
328 next = next->next;
329 continue;
330 }
331
332 dentry = dget(dentry);
333 spin_unlock(&dcache_lock);
334
335 spin_lock(&sbi->fs_lock); 389 spin_lock(&sbi->fs_lock);
336 ino = autofs4_dentry_ino(dentry); 390 ino = autofs4_dentry_ino(dentry);
391 /* No point expiring a pending mount */
392 if (ino->flags & AUTOFS_INF_PENDING)
393 goto next;
337 394
338 /* 395 /*
339 * Case 1: (i) indirect mount or top level pseudo direct mount 396 * Case 1: (i) indirect mount or top level pseudo direct mount
@@ -347,7 +404,7 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
347 404
348 /* Path walk currently on this dentry? */ 405 /* Path walk currently on this dentry? */
349 ino_count = atomic_read(&ino->count) + 2; 406 ino_count = atomic_read(&ino->count) + 2;
350 if (atomic_read(&dentry->d_count) > ino_count) 407 if (dentry->d_count > ino_count)
351 goto next; 408 goto next;
352 409
353 /* Can we umount this guy */ 410 /* Can we umount this guy */
@@ -369,7 +426,7 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
369 if (!exp_leaves) { 426 if (!exp_leaves) {
370 /* Path walk currently on this dentry? */ 427 /* Path walk currently on this dentry? */
371 ino_count = atomic_read(&ino->count) + 1; 428 ino_count = atomic_read(&ino->count) + 1;
372 if (atomic_read(&dentry->d_count) > ino_count) 429 if (dentry->d_count > ino_count)
373 goto next; 430 goto next;
374 431
375 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) { 432 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
@@ -383,7 +440,7 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
383 } else { 440 } else {
384 /* Path walk currently on this dentry? */ 441 /* Path walk currently on this dentry? */
385 ino_count = atomic_read(&ino->count) + 1; 442 ino_count = atomic_read(&ino->count) + 1;
386 if (atomic_read(&dentry->d_count) > ino_count) 443 if (dentry->d_count > ino_count)
387 goto next; 444 goto next;
388 445
389 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now); 446 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
@@ -394,11 +451,7 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
394 } 451 }
395next: 452next:
396 spin_unlock(&sbi->fs_lock); 453 spin_unlock(&sbi->fs_lock);
397 dput(dentry);
398 spin_lock(&dcache_lock);
399 next = next->next;
400 } 454 }
401 spin_unlock(&dcache_lock);
402 return NULL; 455 return NULL;
403 456
404found: 457found:
@@ -408,9 +461,13 @@ found:
408 ino->flags |= AUTOFS_INF_EXPIRING; 461 ino->flags |= AUTOFS_INF_EXPIRING;
409 init_completion(&ino->expire_complete); 462 init_completion(&ino->expire_complete);
410 spin_unlock(&sbi->fs_lock); 463 spin_unlock(&sbi->fs_lock);
411 spin_lock(&dcache_lock); 464 spin_lock(&sbi->lookup_lock);
465 spin_lock(&expired->d_parent->d_lock);
466 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
412 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child); 467 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
413 spin_unlock(&dcache_lock); 468 spin_unlock(&expired->d_lock);
469 spin_unlock(&expired->d_parent->d_lock);
470 spin_unlock(&sbi->lookup_lock);
414 return expired; 471 return expired;
415} 472}
416 473
@@ -498,11 +555,16 @@ int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
498 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE); 555 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
499 556
500 spin_lock(&sbi->fs_lock); 557 spin_lock(&sbi->fs_lock);
501 if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
502 sb->s_root->d_mounted++;
503 ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
504 }
505 ino->flags &= ~AUTOFS_INF_EXPIRING; 558 ino->flags &= ~AUTOFS_INF_EXPIRING;
559 spin_lock(&dentry->d_lock);
560 if (!ret) {
561 if ((IS_ROOT(dentry) ||
562 (autofs_type_indirect(sbi->type) &&
563 IS_ROOT(dentry->d_parent))) &&
564 !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
565 __managed_dentry_set_automount(dentry);
566 }
567 spin_unlock(&dentry->d_lock);
506 complete_all(&ino->expire_complete); 568 complete_all(&ino->expire_complete);
507 spin_unlock(&sbi->fs_lock); 569 spin_unlock(&sbi->fs_lock);
508 dput(dentry); 570 dput(dentry);
diff --git a/fs/autofs4/init.c b/fs/autofs4/init.c
index 9722e4bd8957..c038727b4050 100644
--- a/fs/autofs4/init.c
+++ b/fs/autofs4/init.c
@@ -14,16 +14,16 @@
14#include <linux/init.h> 14#include <linux/init.h>
15#include "autofs_i.h" 15#include "autofs_i.h"
16 16
17static int autofs_get_sb(struct file_system_type *fs_type, 17static struct dentry *autofs_mount(struct file_system_type *fs_type,
18 int flags, const char *dev_name, void *data, struct vfsmount *mnt) 18 int flags, const char *dev_name, void *data)
19{ 19{
20 return get_sb_nodev(fs_type, flags, data, autofs4_fill_super, mnt); 20 return mount_nodev(fs_type, flags, data, autofs4_fill_super);
21} 21}
22 22
23static struct file_system_type autofs_fs_type = { 23static struct file_system_type autofs_fs_type = {
24 .owner = THIS_MODULE, 24 .owner = THIS_MODULE,
25 .name = "autofs", 25 .name = "autofs",
26 .get_sb = autofs_get_sb, 26 .mount = autofs_mount,
27 .kill_sb = autofs4_kill_sb, 27 .kill_sb = autofs4_kill_sb,
28}; 28};
29 29
diff --git a/fs/autofs4/inode.c b/fs/autofs4/inode.c
index 821b2b955dac..180fa2425e49 100644
--- a/fs/autofs4/inode.c
+++ b/fs/autofs4/inode.c
@@ -22,77 +22,27 @@
22#include "autofs_i.h" 22#include "autofs_i.h"
23#include <linux/module.h> 23#include <linux/module.h>
24 24
25static void ino_lnkfree(struct autofs_info *ino) 25struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
26{ 26{
27 if (ino->u.symlink) { 27 struct autofs_info *ino = kzalloc(sizeof(*ino), GFP_KERNEL);
28 kfree(ino->u.symlink); 28 if (ino) {
29 ino->u.symlink = NULL;
30 }
31}
32
33struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
34 struct autofs_sb_info *sbi, mode_t mode)
35{
36 int reinit = 1;
37
38 if (ino == NULL) {
39 reinit = 0;
40 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
41 }
42
43 if (ino == NULL)
44 return NULL;
45
46 if (!reinit) {
47 ino->flags = 0;
48 ino->inode = NULL;
49 ino->dentry = NULL;
50 ino->size = 0;
51 INIT_LIST_HEAD(&ino->active); 29 INIT_LIST_HEAD(&ino->active);
52 ino->active_count = 0;
53 INIT_LIST_HEAD(&ino->expiring); 30 INIT_LIST_HEAD(&ino->expiring);
54 atomic_set(&ino->count, 0); 31 ino->last_used = jiffies;
32 ino->sbi = sbi;
55 } 33 }
34 return ino;
35}
56 36
37void autofs4_clean_ino(struct autofs_info *ino)
38{
57 ino->uid = 0; 39 ino->uid = 0;
58 ino->gid = 0; 40 ino->gid = 0;
59 ino->mode = mode;
60 ino->last_used = jiffies; 41 ino->last_used = jiffies;
61
62 ino->sbi = sbi;
63
64 if (reinit && ino->free)
65 (ino->free)(ino);
66
67 memset(&ino->u, 0, sizeof(ino->u));
68
69 ino->free = NULL;
70
71 if (S_ISLNK(mode))
72 ino->free = ino_lnkfree;
73
74 return ino;
75} 42}
76 43
77void autofs4_free_ino(struct autofs_info *ino) 44void autofs4_free_ino(struct autofs_info *ino)
78{ 45{
79 struct autofs_info *p_ino;
80
81 if (ino->dentry) {
82 ino->dentry->d_fsdata = NULL;
83 if (ino->dentry->d_inode) {
84 struct dentry *parent = ino->dentry->d_parent;
85 if (atomic_dec_and_test(&ino->count)) {
86 p_ino = autofs4_dentry_ino(parent);
87 if (p_ino && parent != ino->dentry)
88 atomic_dec(&p_ino->count);
89 }
90 dput(ino->dentry);
91 }
92 ino->dentry = NULL;
93 }
94 if (ino->free)
95 (ino->free)(ino);
96 kfree(ino); 46 kfree(ino);
97} 47}
98 48
@@ -148,9 +98,16 @@ static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
148 return 0; 98 return 0;
149} 99}
150 100
101static void autofs4_evict_inode(struct inode *inode)
102{
103 end_writeback(inode);
104 kfree(inode->i_private);
105}
106
151static const struct super_operations autofs4_sops = { 107static const struct super_operations autofs4_sops = {
152 .statfs = simple_statfs, 108 .statfs = simple_statfs,
153 .show_options = autofs4_show_options, 109 .show_options = autofs4_show_options,
110 .evict_inode = autofs4_evict_inode,
154}; 111};
155 112
156enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto, 113enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
@@ -240,21 +197,6 @@ static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
240 return (*pipefd < 0); 197 return (*pipefd < 0);
241} 198}
242 199
243static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
244{
245 struct autofs_info *ino;
246
247 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
248 if (!ino)
249 return NULL;
250
251 return ino;
252}
253
254static const struct dentry_operations autofs4_sb_dentry_operations = {
255 .d_release = autofs4_dentry_release,
256};
257
258int autofs4_fill_super(struct super_block *s, void *data, int silent) 200int autofs4_fill_super(struct super_block *s, void *data, int silent)
259{ 201{
260 struct inode * root_inode; 202 struct inode * root_inode;
@@ -292,15 +234,16 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent)
292 s->s_blocksize_bits = 10; 234 s->s_blocksize_bits = 10;
293 s->s_magic = AUTOFS_SUPER_MAGIC; 235 s->s_magic = AUTOFS_SUPER_MAGIC;
294 s->s_op = &autofs4_sops; 236 s->s_op = &autofs4_sops;
237 s->s_d_op = &autofs4_dentry_operations;
295 s->s_time_gran = 1; 238 s->s_time_gran = 1;
296 239
297 /* 240 /*
298 * Get the root inode and dentry, but defer checking for errors. 241 * Get the root inode and dentry, but defer checking for errors.
299 */ 242 */
300 ino = autofs4_mkroot(sbi); 243 ino = autofs4_new_ino(sbi);
301 if (!ino) 244 if (!ino)
302 goto fail_free; 245 goto fail_free;
303 root_inode = autofs4_get_inode(s, ino); 246 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
304 if (!root_inode) 247 if (!root_inode)
305 goto fail_ino; 248 goto fail_ino;
306 249
@@ -309,7 +252,6 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent)
309 goto fail_iput; 252 goto fail_iput;
310 pipe = NULL; 253 pipe = NULL;
311 254
312 root->d_op = &autofs4_sb_dentry_operations;
313 root->d_fsdata = ino; 255 root->d_fsdata = ino;
314 256
315 /* Can this call block? */ 257 /* Can this call block? */
@@ -320,10 +262,11 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent)
320 goto fail_dput; 262 goto fail_dput;
321 } 263 }
322 264
265 if (autofs_type_trigger(sbi->type))
266 __managed_dentry_set_managed(root);
267
323 root_inode->i_fop = &autofs4_root_operations; 268 root_inode->i_fop = &autofs4_root_operations;
324 root_inode->i_op = autofs_type_trigger(sbi->type) ? 269 root_inode->i_op = &autofs4_dir_inode_operations;
325 &autofs4_direct_root_inode_operations :
326 &autofs4_indirect_root_inode_operations;
327 270
328 /* Couldn't this be tested earlier? */ 271 /* Couldn't this be tested earlier? */
329 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || 272 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
@@ -383,28 +326,26 @@ fail_unlock:
383 return -EINVAL; 326 return -EINVAL;
384} 327}
385 328
386struct inode *autofs4_get_inode(struct super_block *sb, 329struct inode *autofs4_get_inode(struct super_block *sb, mode_t mode)
387 struct autofs_info *inf)
388{ 330{
389 struct inode *inode = new_inode(sb); 331 struct inode *inode = new_inode(sb);
390 332
391 if (inode == NULL) 333 if (inode == NULL)
392 return NULL; 334 return NULL;
393 335
394 inf->inode = inode; 336 inode->i_mode = mode;
395 inode->i_mode = inf->mode;
396 if (sb->s_root) { 337 if (sb->s_root) {
397 inode->i_uid = sb->s_root->d_inode->i_uid; 338 inode->i_uid = sb->s_root->d_inode->i_uid;
398 inode->i_gid = sb->s_root->d_inode->i_gid; 339 inode->i_gid = sb->s_root->d_inode->i_gid;
399 } 340 }
400 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 341 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
342 inode->i_ino = get_next_ino();
401 343
402 if (S_ISDIR(inf->mode)) { 344 if (S_ISDIR(mode)) {
403 inode->i_nlink = 2; 345 inode->i_nlink = 2;
404 inode->i_op = &autofs4_dir_inode_operations; 346 inode->i_op = &autofs4_dir_inode_operations;
405 inode->i_fop = &autofs4_dir_operations; 347 inode->i_fop = &autofs4_dir_operations;
406 } else if (S_ISLNK(inf->mode)) { 348 } else if (S_ISLNK(mode)) {
407 inode->i_size = inf->size;
408 inode->i_op = &autofs4_symlink_inode_operations; 349 inode->i_op = &autofs4_symlink_inode_operations;
409 } 350 }
410 351
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}
diff --git a/fs/autofs4/symlink.c b/fs/autofs4/symlink.c
index b4ea82934d2e..f27c094a1919 100644
--- a/fs/autofs4/symlink.c
+++ b/fs/autofs4/symlink.c
@@ -14,8 +14,7 @@
14 14
15static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) 15static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
16{ 16{
17 struct autofs_info *ino = autofs4_dentry_ino(dentry); 17 nd_set_link(nd, dentry->d_inode->i_private);
18 nd_set_link(nd, (char *)ino->u.symlink);
19 return NULL; 18 return NULL;
20} 19}
21 20
diff --git a/fs/autofs4/waitq.c b/fs/autofs4/waitq.c
index 2341375386f8..25435987d6ae 100644
--- a/fs/autofs4/waitq.c
+++ b/fs/autofs4/waitq.c
@@ -186,16 +186,26 @@ static int autofs4_getpath(struct autofs_sb_info *sbi,
186{ 186{
187 struct dentry *root = sbi->sb->s_root; 187 struct dentry *root = sbi->sb->s_root;
188 struct dentry *tmp; 188 struct dentry *tmp;
189 char *buf = *name; 189 char *buf;
190 char *p; 190 char *p;
191 int len = 0; 191 int len;
192 unsigned seq;
192 193
193 spin_lock(&dcache_lock); 194rename_retry:
195 buf = *name;
196 len = 0;
197
198 seq = read_seqbegin(&rename_lock);
199 rcu_read_lock();
200 spin_lock(&sbi->fs_lock);
194 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent) 201 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
195 len += tmp->d_name.len + 1; 202 len += tmp->d_name.len + 1;
196 203
197 if (!len || --len > NAME_MAX) { 204 if (!len || --len > NAME_MAX) {
198 spin_unlock(&dcache_lock); 205 spin_unlock(&sbi->fs_lock);
206 rcu_read_unlock();
207 if (read_seqretry(&rename_lock, seq))
208 goto rename_retry;
199 return 0; 209 return 0;
200 } 210 }
201 211
@@ -208,7 +218,10 @@ static int autofs4_getpath(struct autofs_sb_info *sbi,
208 p -= tmp->d_name.len; 218 p -= tmp->d_name.len;
209 strncpy(p, tmp->d_name.name, tmp->d_name.len); 219 strncpy(p, tmp->d_name.name, tmp->d_name.len);
210 } 220 }
211 spin_unlock(&dcache_lock); 221 spin_unlock(&sbi->fs_lock);
222 rcu_read_unlock();
223 if (read_seqretry(&rename_lock, seq))
224 goto rename_retry;
212 225
213 return len; 226 return len;
214} 227}
@@ -296,6 +309,9 @@ static int validate_request(struct autofs_wait_queue **wait,
296 * completed while we waited on the mutex ... 309 * completed while we waited on the mutex ...
297 */ 310 */
298 if (notify == NFY_MOUNT) { 311 if (notify == NFY_MOUNT) {
312 struct dentry *new = NULL;
313 int valid = 1;
314
299 /* 315 /*
300 * If the dentry was successfully mounted while we slept 316 * If the dentry was successfully mounted while we slept
301 * on the wait queue mutex we can return success. If it 317 * on the wait queue mutex we can return success. If it
@@ -303,8 +319,20 @@ static int validate_request(struct autofs_wait_queue **wait,
303 * a multi-mount with no mount at it's base) we can 319 * a multi-mount with no mount at it's base) we can
304 * continue on and create a new request. 320 * continue on and create a new request.
305 */ 321 */
322 if (!IS_ROOT(dentry)) {
323 if (dentry->d_inode && d_unhashed(dentry)) {
324 struct dentry *parent = dentry->d_parent;
325 new = d_lookup(parent, &dentry->d_name);
326 if (new)
327 dentry = new;
328 }
329 }
306 if (have_submounts(dentry)) 330 if (have_submounts(dentry))
307 return 0; 331 valid = 0;
332
333 if (new)
334 dput(new);
335 return valid;
308 } 336 }
309 337
310 return 1; 338 return 1;