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