move d_rcu from overlapping d_child to overlapping d_alias
[pandora-kernel.git] / fs / autofs4 / expire.c
1 /* -*- c -*- --------------------------------------------------------------- *
2  *
3  * linux/fs/autofs/expire.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *  Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7  *  Copyright 2001-2006 Ian Kent <raven@themaw.net>
8  *
9  * This file is part of the Linux kernel and is made available under
10  * the terms of the GNU General Public License, version 2, or at your
11  * option, any later version, incorporated herein by reference.
12  *
13  * ------------------------------------------------------------------------- */
14
15 #include "autofs_i.h"
16
17 static unsigned long now;
18
19 /* Check if a dentry can be expired */
20 static inline int autofs4_can_expire(struct dentry *dentry,
21                                         unsigned long timeout, int do_now)
22 {
23         struct autofs_info *ino = autofs4_dentry_ino(dentry);
24
25         /* dentry in the process of being deleted */
26         if (ino == NULL)
27                 return 0;
28
29         if (!do_now) {
30                 /* Too young to die */
31                 if (!timeout || time_after(ino->last_used + timeout, now))
32                         return 0;
33
34                 /* update last_used here :-
35                    - obviously makes sense if it is in use now
36                    - less obviously, prevents rapid-fire expire
37                      attempts if expire fails the first time */
38                 ino->last_used = now;
39         }
40         return 1;
41 }
42
43 /* Check a mount point for busyness */
44 static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
45 {
46         struct dentry *top = dentry;
47         struct path path = {.mnt = mnt, .dentry = dentry};
48         int status = 1;
49
50         DPRINTK("dentry %p %.*s",
51                 dentry, (int)dentry->d_name.len, dentry->d_name.name);
52
53         path_get(&path);
54
55         if (!follow_down_one(&path))
56                 goto done;
57
58         if (is_autofs4_dentry(path.dentry)) {
59                 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
60
61                 /* This is an autofs submount, we can't expire it */
62                 if (autofs_type_indirect(sbi->type))
63                         goto done;
64         }
65
66         /* Update the expiry counter if fs is busy */
67         if (!may_umount_tree(path.mnt)) {
68                 struct autofs_info *ino = autofs4_dentry_ino(top);
69                 ino->last_used = jiffies;
70                 goto done;
71         }
72
73         status = 0;
74 done:
75         DPRINTK("returning = %d", status);
76         path_put(&path);
77         return status;
78 }
79
80 /*
81  * Calculate and dget next entry in the subdirs list under root.
82  */
83 static struct dentry *get_next_positive_subdir(struct dentry *prev,
84                                                 struct dentry *root)
85 {
86         struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
87         struct list_head *next;
88         struct dentry *p, *q;
89
90         spin_lock(&sbi->lookup_lock);
91
92         if (prev == NULL) {
93                 spin_lock(&root->d_lock);
94                 prev = dget_dlock(root);
95                 next = prev->d_subdirs.next;
96                 p = prev;
97                 goto start;
98         }
99
100         p = prev;
101         spin_lock(&p->d_lock);
102 again:
103         next = p->d_child.next;
104 start:
105         if (next == &root->d_subdirs) {
106                 spin_unlock(&p->d_lock);
107                 spin_unlock(&sbi->lookup_lock);
108                 dput(prev);
109                 return NULL;
110         }
111
112         q = list_entry(next, struct dentry, d_child);
113
114         spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
115         /* Negative dentry - try next */
116         if (!simple_positive(q)) {
117                 spin_unlock(&p->d_lock);
118                 p = q;
119                 goto again;
120         }
121         dget_dlock(q);
122         spin_unlock(&q->d_lock);
123         spin_unlock(&p->d_lock);
124         spin_unlock(&sbi->lookup_lock);
125
126         dput(prev);
127
128         return q;
129 }
130
131 /*
132  * Calculate and dget next entry in top down tree traversal.
133  */
134 static struct dentry *get_next_positive_dentry(struct dentry *prev,
135                                                 struct dentry *root)
136 {
137         struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
138         struct list_head *next;
139         struct dentry *p, *ret;
140
141         if (prev == NULL)
142                 return dget(root);
143
144         spin_lock(&sbi->lookup_lock);
145 relock:
146         p = prev;
147         spin_lock(&p->d_lock);
148 again:
149         next = p->d_subdirs.next;
150         if (next == &p->d_subdirs) {
151                 while (1) {
152                         struct dentry *parent;
153
154                         if (p == root) {
155                                 spin_unlock(&p->d_lock);
156                                 spin_unlock(&sbi->lookup_lock);
157                                 dput(prev);
158                                 return NULL;
159                         }
160
161                         parent = p->d_parent;
162                         if (!spin_trylock(&parent->d_lock)) {
163                                 spin_unlock(&p->d_lock);
164                                 cpu_relax();
165                                 goto relock;
166                         }
167                         spin_unlock(&p->d_lock);
168                         next = p->d_child.next;
169                         p = parent;
170                         if (next != &parent->d_subdirs)
171                                 break;
172                 }
173         }
174         ret = list_entry(next, struct dentry, d_child);
175
176         spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
177         /* Negative dentry - try next */
178         if (!simple_positive(ret)) {
179                 spin_unlock(&p->d_lock);
180                 p = ret;
181                 goto again;
182         }
183         dget_dlock(ret);
184         spin_unlock(&ret->d_lock);
185         spin_unlock(&p->d_lock);
186         spin_unlock(&sbi->lookup_lock);
187
188         dput(prev);
189
190         return ret;
191 }
192
193 /*
194  * Check a direct mount point for busyness.
195  * Direct mounts have similar expiry semantics to tree mounts.
196  * The tree is not busy iff no mountpoints are busy and there are no
197  * autofs submounts.
198  */
199 static int autofs4_direct_busy(struct vfsmount *mnt,
200                                 struct dentry *top,
201                                 unsigned long timeout,
202                                 int do_now)
203 {
204         DPRINTK("top %p %.*s",
205                 top, (int) top->d_name.len, top->d_name.name);
206
207         /* If it's busy update the expiry counters */
208         if (!may_umount_tree(mnt)) {
209                 struct autofs_info *ino = autofs4_dentry_ino(top);
210                 if (ino)
211                         ino->last_used = jiffies;
212                 return 1;
213         }
214
215         /* Timeout of a direct mount is determined by its top dentry */
216         if (!autofs4_can_expire(top, timeout, do_now))
217                 return 1;
218
219         return 0;
220 }
221
222 /* Check a directory tree of mount points for busyness
223  * The tree is not busy iff no mountpoints are busy
224  */
225 static int autofs4_tree_busy(struct vfsmount *mnt,
226                              struct dentry *top,
227                              unsigned long timeout,
228                              int do_now)
229 {
230         struct autofs_info *top_ino = autofs4_dentry_ino(top);
231         struct dentry *p;
232
233         DPRINTK("top %p %.*s",
234                 top, (int)top->d_name.len, top->d_name.name);
235
236         /* Negative dentry - give up */
237         if (!simple_positive(top))
238                 return 1;
239
240         p = NULL;
241         while ((p = get_next_positive_dentry(p, top))) {
242                 DPRINTK("dentry %p %.*s",
243                         p, (int) p->d_name.len, p->d_name.name);
244
245                 /*
246                  * Is someone visiting anywhere in the subtree ?
247                  * If there's no mount we need to check the usage
248                  * count for the autofs dentry.
249                  * If the fs is busy update the expiry counter.
250                  */
251                 if (d_mountpoint(p)) {
252                         if (autofs4_mount_busy(mnt, p)) {
253                                 top_ino->last_used = jiffies;
254                                 dput(p);
255                                 return 1;
256                         }
257                 } else {
258                         struct autofs_info *ino = autofs4_dentry_ino(p);
259                         unsigned int ino_count = atomic_read(&ino->count);
260
261                         /*
262                          * Clean stale dentries below that have not been
263                          * invalidated after a mount fail during lookup
264                          */
265                         d_invalidate(p);
266
267                         /* allow for dget above and top is already dgot */
268                         if (p == top)
269                                 ino_count += 2;
270                         else
271                                 ino_count++;
272
273                         if (p->d_count > ino_count) {
274                                 top_ino->last_used = jiffies;
275                                 dput(p);
276                                 return 1;
277                         }
278                 }
279         }
280
281         /* Timeout of a tree mount is ultimately determined by its top dentry */
282         if (!autofs4_can_expire(top, timeout, do_now))
283                 return 1;
284
285         return 0;
286 }
287
288 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
289                                            struct dentry *parent,
290                                            unsigned long timeout,
291                                            int do_now)
292 {
293         struct dentry *p;
294
295         DPRINTK("parent %p %.*s",
296                 parent, (int)parent->d_name.len, parent->d_name.name);
297
298         p = NULL;
299         while ((p = get_next_positive_dentry(p, parent))) {
300                 DPRINTK("dentry %p %.*s",
301                         p, (int) p->d_name.len, p->d_name.name);
302
303                 if (d_mountpoint(p)) {
304                         /* Can we umount this guy */
305                         if (autofs4_mount_busy(mnt, p))
306                                 continue;
307
308                         /* Can we expire this guy */
309                         if (autofs4_can_expire(p, timeout, do_now))
310                                 return p;
311                 }
312         }
313         return NULL;
314 }
315
316 /* Check if we can expire a direct mount (possibly a tree) */
317 struct dentry *autofs4_expire_direct(struct super_block *sb,
318                                      struct vfsmount *mnt,
319                                      struct autofs_sb_info *sbi,
320                                      int how)
321 {
322         unsigned long timeout;
323         struct dentry *root = dget(sb->s_root);
324         int do_now = how & AUTOFS_EXP_IMMEDIATE;
325         struct autofs_info *ino;
326
327         if (!root)
328                 return NULL;
329
330         now = jiffies;
331         timeout = sbi->exp_timeout;
332
333         spin_lock(&sbi->fs_lock);
334         ino = autofs4_dentry_ino(root);
335         /* No point expiring a pending mount */
336         if (ino->flags & AUTOFS_INF_PENDING)
337                 goto out;
338         if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
339                 struct autofs_info *ino = autofs4_dentry_ino(root);
340                 ino->flags |= AUTOFS_INF_EXPIRING;
341                 init_completion(&ino->expire_complete);
342                 spin_unlock(&sbi->fs_lock);
343                 return root;
344         }
345 out:
346         spin_unlock(&sbi->fs_lock);
347         dput(root);
348
349         return NULL;
350 }
351
352 /*
353  * Find an eligible tree to time-out
354  * A tree is eligible if :-
355  *  - it is unused by any user process
356  *  - it has been unused for exp_timeout time
357  */
358 struct dentry *autofs4_expire_indirect(struct super_block *sb,
359                                        struct vfsmount *mnt,
360                                        struct autofs_sb_info *sbi,
361                                        int how)
362 {
363         unsigned long timeout;
364         struct dentry *root = sb->s_root;
365         struct dentry *dentry;
366         struct dentry *expired = NULL;
367         int do_now = how & AUTOFS_EXP_IMMEDIATE;
368         int exp_leaves = how & AUTOFS_EXP_LEAVES;
369         struct autofs_info *ino;
370         unsigned int ino_count;
371
372         if (!root)
373                 return NULL;
374
375         now = jiffies;
376         timeout = sbi->exp_timeout;
377
378         dentry = NULL;
379         while ((dentry = get_next_positive_subdir(dentry, root))) {
380                 spin_lock(&sbi->fs_lock);
381                 ino = autofs4_dentry_ino(dentry);
382                 /* No point expiring a pending mount */
383                 if (ino->flags & AUTOFS_INF_PENDING)
384                         goto next;
385
386                 /*
387                  * Case 1: (i) indirect mount or top level pseudo direct mount
388                  *         (autofs-4.1).
389                  *         (ii) indirect mount with offset mount, check the "/"
390                  *         offset (autofs-5.0+).
391                  */
392                 if (d_mountpoint(dentry)) {
393                         DPRINTK("checking mountpoint %p %.*s",
394                                 dentry, (int)dentry->d_name.len, dentry->d_name.name);
395
396                         /* Path walk currently on this dentry? */
397                         ino_count = atomic_read(&ino->count) + 2;
398                         if (dentry->d_count > ino_count)
399                                 goto next;
400
401                         /* Can we umount this guy */
402                         if (autofs4_mount_busy(mnt, dentry))
403                                 goto next;
404
405                         /* Can we expire this guy */
406                         if (autofs4_can_expire(dentry, timeout, do_now)) {
407                                 expired = dentry;
408                                 goto found;
409                         }
410                         goto next;
411                 }
412
413                 if (simple_empty(dentry))
414                         goto next;
415
416                 /* Case 2: tree mount, expire iff entire tree is not busy */
417                 if (!exp_leaves) {
418                         /* Path walk currently on this dentry? */
419                         ino_count = atomic_read(&ino->count) + 1;
420                         if (dentry->d_count > ino_count)
421                                 goto next;
422
423                         if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
424                                 expired = dentry;
425                                 goto found;
426                         }
427                 /*
428                  * Case 3: pseudo direct mount, expire individual leaves
429                  *         (autofs-4.1).
430                  */
431                 } else {
432                         /* Path walk currently on this dentry? */
433                         ino_count = atomic_read(&ino->count) + 1;
434                         if (dentry->d_count > ino_count)
435                                 goto next;
436
437                         expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
438                         if (expired) {
439                                 dput(dentry);
440                                 goto found;
441                         }
442                 }
443 next:
444                 spin_unlock(&sbi->fs_lock);
445         }
446         return NULL;
447
448 found:
449         DPRINTK("returning %p %.*s",
450                 expired, (int)expired->d_name.len, expired->d_name.name);
451         ino = autofs4_dentry_ino(expired);
452         ino->flags |= AUTOFS_INF_EXPIRING;
453         init_completion(&ino->expire_complete);
454         spin_unlock(&sbi->fs_lock);
455         spin_lock(&sbi->lookup_lock);
456         spin_lock(&expired->d_parent->d_lock);
457         spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
458         list_move(&expired->d_parent->d_subdirs, &expired->d_child);
459         spin_unlock(&expired->d_lock);
460         spin_unlock(&expired->d_parent->d_lock);
461         spin_unlock(&sbi->lookup_lock);
462         return expired;
463 }
464
465 int autofs4_expire_wait(struct dentry *dentry)
466 {
467         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
468         struct autofs_info *ino = autofs4_dentry_ino(dentry);
469         int status;
470
471         /* Block on any pending expire */
472         spin_lock(&sbi->fs_lock);
473         if (ino->flags & AUTOFS_INF_EXPIRING) {
474                 spin_unlock(&sbi->fs_lock);
475
476                 DPRINTK("waiting for expire %p name=%.*s",
477                          dentry, dentry->d_name.len, dentry->d_name.name);
478
479                 status = autofs4_wait(sbi, dentry, NFY_NONE);
480                 wait_for_completion(&ino->expire_complete);
481
482                 DPRINTK("expire done status=%d", status);
483
484                 if (d_unhashed(dentry))
485                         return -EAGAIN;
486
487                 return status;
488         }
489         spin_unlock(&sbi->fs_lock);
490
491         return 0;
492 }
493
494 /* Perform an expiry operation */
495 int autofs4_expire_run(struct super_block *sb,
496                       struct vfsmount *mnt,
497                       struct autofs_sb_info *sbi,
498                       struct autofs_packet_expire __user *pkt_p)
499 {
500         struct autofs_packet_expire pkt;
501         struct autofs_info *ino;
502         struct dentry *dentry;
503         int ret = 0;
504
505         memset(&pkt,0,sizeof pkt);
506
507         pkt.hdr.proto_version = sbi->version;
508         pkt.hdr.type = autofs_ptype_expire;
509
510         if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
511                 return -EAGAIN;
512
513         pkt.len = dentry->d_name.len;
514         memcpy(pkt.name, dentry->d_name.name, pkt.len);
515         pkt.name[pkt.len] = '\0';
516         dput(dentry);
517
518         if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
519                 ret = -EFAULT;
520
521         spin_lock(&sbi->fs_lock);
522         ino = autofs4_dentry_ino(dentry);
523         ino->flags &= ~AUTOFS_INF_EXPIRING;
524         complete_all(&ino->expire_complete);
525         spin_unlock(&sbi->fs_lock);
526
527         return ret;
528 }
529
530 int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
531                             struct autofs_sb_info *sbi, int when)
532 {
533         struct dentry *dentry;
534         int ret = -EAGAIN;
535
536         if (autofs_type_trigger(sbi->type))
537                 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
538         else
539                 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
540
541         if (dentry) {
542                 struct autofs_info *ino = autofs4_dentry_ino(dentry);
543
544                 /* This is synchronous because it makes the daemon a
545                    little easier */
546                 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
547
548                 spin_lock(&sbi->fs_lock);
549                 ino->flags &= ~AUTOFS_INF_EXPIRING;
550                 spin_lock(&dentry->d_lock);
551                 if (!ret) {
552                         if ((IS_ROOT(dentry) ||
553                             (autofs_type_indirect(sbi->type) &&
554                              IS_ROOT(dentry->d_parent))) &&
555                             !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
556                                 __managed_dentry_set_automount(dentry);
557                 }
558                 spin_unlock(&dentry->d_lock);
559                 complete_all(&ino->expire_complete);
560                 spin_unlock(&sbi->fs_lock);
561                 dput(dentry);
562         }
563
564         return ret;
565 }
566
567 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
568    more to be done */
569 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
570                         struct autofs_sb_info *sbi, int __user *arg)
571 {
572         int do_now = 0;
573
574         if (arg && get_user(do_now, arg))
575                 return -EFAULT;
576
577         return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
578 }
579