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