blob: 8c0e56d92938c49ad61fef0bfb3888f47429ebc9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Ian Kent3a15e2a2006-03-27 01:14:55 -08007 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
17static unsigned long now;
18
Ian Kent1f5f2c32006-03-27 01:14:44 -080019/* Check if a dentry can be expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 if (!do_now) {
30 /* Too young to die */
Ian Kentc0ba7e52006-09-25 16:24:16 -070031 if (!timeout || time_after(ino->last_used + timeout, now))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return 1;
41}
42
Ian Kent1f5f2c32006-03-27 01:14:44 -080043/* Check a mount point for busyness */
44static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Ian Kente0a7aae2006-03-27 01:14:47 -080046 struct dentry *top = dentry;
Al Viro9393bd02009-04-18 13:58:15 -040047 struct path path = {.mnt = mnt, .dentry = dentry};
Ian Kent1f5f2c32006-03-27 01:14:44 -080048 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 DPRINTK("dentry %p %.*s",
51 dentry, (int)dentry->d_name.len, dentry->d_name.name);
52
Al Viro9393bd02009-04-18 13:58:15 -040053 path_get(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Howellscc53ce52011-01-14 18:45:26 +000055 if (!follow_down_one(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 goto done;
57
Al Viro9393bd02009-04-18 13:58:15 -040058 if (is_autofs4_dentry(path.dentry)) {
59 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
Ian Kentbc9c4062008-11-06 12:53:22 -080060
61 /* This is an autofs submount, we can't expire it */
Ian Kenta92daf62009-01-06 14:42:08 -080062 if (autofs_type_indirect(sbi->type))
Ian Kentbc9c4062008-11-06 12:53:22 -080063 goto done;
64
65 /*
66 * Otherwise it's an offset mount and we need to check
67 * if we can umount its mount, if there is one.
68 */
Al Viro9393bd02009-04-18 13:58:15 -040069 if (!d_mountpoint(path.dentry)) {
Ian Kenta8985f32009-04-30 15:08:09 -070070 status = 0;
Ian Kentbc9c4062008-11-06 12:53:22 -080071 goto done;
Ian Kenta8985f32009-04-30 15:08:09 -070072 }
Ian Kentbc9c4062008-11-06 12:53:22 -080073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Ian Kente0a7aae2006-03-27 01:14:47 -080075 /* Update the expiry counter if fs is busy */
Ian Kent37d08922009-09-01 11:26:22 +080076 if (!may_umount_tree(path.mnt)) {
Ian Kente0a7aae2006-03-27 01:14:47 -080077 struct autofs_info *ino = autofs4_dentry_ino(top);
78 ino->last_used = jiffies;
79 goto done;
80 }
81
82 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083done:
84 DPRINTK("returning = %d", status);
Al Viro9393bd02009-04-18 13:58:15 -040085 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return status;
87}
88
Ian Kent1ce12ba2006-03-27 01:14:45 -080089/*
Ian Kentd4a85e32011-03-25 01:51:20 +080090 * Calculate and dget next entry in the subdirs list under root.
91 */
92static struct dentry *get_next_positive_subdir(struct dentry *prev,
93 struct dentry *root)
94{
Ian Kente7854722011-03-25 01:51:31 +080095 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
Ian Kentd4a85e32011-03-25 01:51:20 +080096 struct list_head *next;
Ian Kenta45440f2012-08-06 09:37:47 +080097 struct dentry *q;
Ian Kentd4a85e32011-03-25 01:51:20 +080098
Ian Kente7854722011-03-25 01:51:31 +080099 spin_lock(&sbi->lookup_lock);
Ian Kenta45440f2012-08-06 09:37:47 +0800100 spin_lock(&root->d_lock);
Ian Kentd4a85e32011-03-25 01:51:20 +0800101
Ian Kenta45440f2012-08-06 09:37:47 +0800102 if (prev)
103 next = prev->d_u.d_child.next;
104 else {
Ian Kentd4a85e32011-03-25 01:51:20 +0800105 prev = dget_dlock(root);
106 next = prev->d_subdirs.next;
Ian Kentd4a85e32011-03-25 01:51:20 +0800107 }
108
Ian Kenta45440f2012-08-06 09:37:47 +0800109cont:
Ian Kentd4a85e32011-03-25 01:51:20 +0800110 if (next == &root->d_subdirs) {
Ian Kenta45440f2012-08-06 09:37:47 +0800111 spin_unlock(&root->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800112 spin_unlock(&sbi->lookup_lock);
Ian Kentd4a85e32011-03-25 01:51:20 +0800113 dput(prev);
114 return NULL;
115 }
116
117 q = list_entry(next, struct dentry, d_u.d_child);
118
119 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
Ian Kenta45440f2012-08-06 09:37:47 +0800120 /* Already gone or negative dentry (under construction) - try next */
121 if (q->d_count == 0 || !simple_positive(q)) {
122 spin_unlock(&q->d_lock);
123 next = q->d_u.d_child.next;
124 goto cont;
Ian Kentd4a85e32011-03-25 01:51:20 +0800125 }
126 dget_dlock(q);
127 spin_unlock(&q->d_lock);
Ian Kenta45440f2012-08-06 09:37:47 +0800128 spin_unlock(&root->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800129 spin_unlock(&sbi->lookup_lock);
Ian Kentd4a85e32011-03-25 01:51:20 +0800130
131 dput(prev);
132
133 return q;
134}
135
136/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100137 * Calculate and dget next entry in top down tree traversal.
Ian Kent1ce12ba2006-03-27 01:14:45 -0800138 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100139static struct dentry *get_next_positive_dentry(struct dentry *prev,
140 struct dentry *root)
Ian Kent1ce12ba2006-03-27 01:14:45 -0800141{
Ian Kente7854722011-03-25 01:51:31 +0800142 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100143 struct list_head *next;
144 struct dentry *p, *ret;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800145
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100146 if (prev == NULL)
Ian Kentc14cc632011-01-18 12:06:04 +0800147 return dget(root);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100148
Ian Kente7854722011-03-25 01:51:31 +0800149 spin_lock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100150relock:
151 p = prev;
152 spin_lock(&p->d_lock);
153again:
154 next = p->d_subdirs.next;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800155 if (next == &p->d_subdirs) {
156 while (1) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100157 struct dentry *parent;
158
159 if (p == root) {
160 spin_unlock(&p->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800161 spin_unlock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100162 dput(prev);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800163 return NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100164 }
165
166 parent = p->d_parent;
167 if (!spin_trylock(&parent->d_lock)) {
168 spin_unlock(&p->d_lock);
169 cpu_relax();
170 goto relock;
171 }
172 spin_unlock(&p->d_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800173 next = p->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100174 p = parent;
175 if (next != &parent->d_subdirs)
Ian Kent1ce12ba2006-03-27 01:14:45 -0800176 break;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800177 }
178 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100179 ret = list_entry(next, struct dentry, d_u.d_child);
180
181 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
182 /* Negative dentry - try next */
183 if (!simple_positive(ret)) {
Ian Kentc14cc632011-01-18 12:06:04 +0800184 spin_unlock(&p->d_lock);
Steven Rostedt1d6f2092011-08-22 11:52:28 +0800185 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100186 p = ret;
187 goto again;
188 }
189 dget_dlock(ret);
190 spin_unlock(&ret->d_lock);
191 spin_unlock(&p->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800192 spin_unlock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100193
194 dput(prev);
195
196 return ret;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800197}
198
Ian Kent3a15e2a2006-03-27 01:14:55 -0800199/*
200 * Check a direct mount point for busyness.
201 * Direct mounts have similar expiry semantics to tree mounts.
202 * The tree is not busy iff no mountpoints are busy and there are no
203 * autofs submounts.
204 */
205static int autofs4_direct_busy(struct vfsmount *mnt,
206 struct dentry *top,
207 unsigned long timeout,
208 int do_now)
209{
210 DPRINTK("top %p %.*s",
211 top, (int) top->d_name.len, top->d_name.name);
212
Ian Kent3a15e2a2006-03-27 01:14:55 -0800213 /* If it's busy update the expiry counters */
214 if (!may_umount_tree(mnt)) {
215 struct autofs_info *ino = autofs4_dentry_ino(top);
216 if (ino)
217 ino->last_used = jiffies;
218 return 1;
219 }
220
221 /* Timeout of a direct mount is determined by its top dentry */
222 if (!autofs4_can_expire(top, timeout, do_now))
223 return 1;
224
225 return 0;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* Check a directory tree of mount points for busyness
229 * The tree is not busy iff no mountpoints are busy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800231static int autofs4_tree_busy(struct vfsmount *mnt,
232 struct dentry *top,
233 unsigned long timeout,
234 int do_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Ian Kente0a7aae2006-03-27 01:14:47 -0800236 struct autofs_info *top_ino = autofs4_dentry_ino(top);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800237 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Ian Kent1f5f2c32006-03-27 01:14:44 -0800239 DPRINTK("top %p %.*s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 top, (int)top->d_name.len, top->d_name.name);
241
242 /* Negative dentry - give up */
243 if (!simple_positive(top))
Ian Kent1f5f2c32006-03-27 01:14:44 -0800244 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100246 p = NULL;
247 while ((p = get_next_positive_dentry(p, top))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800249 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Ian Kent1aff3c82006-03-27 01:14:46 -0800251 /*
252 * Is someone visiting anywhere in the subtree ?
253 * If there's no mount we need to check the usage
254 * count for the autofs dentry.
Ian Kente0a7aae2006-03-27 01:14:47 -0800255 * If the fs is busy update the expiry counter.
Ian Kent1aff3c82006-03-27 01:14:46 -0800256 */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800257 if (d_mountpoint(p)) {
Ian Kent1ce12ba2006-03-27 01:14:45 -0800258 if (autofs4_mount_busy(mnt, p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800259 top_ino->last_used = jiffies;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800260 dput(p);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800261 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800263 } else {
Ian Kente0a7aae2006-03-27 01:14:47 -0800264 struct autofs_info *ino = autofs4_dentry_ino(p);
Ian Kent1aff3c82006-03-27 01:14:46 -0800265 unsigned int ino_count = atomic_read(&ino->count);
266
Ian Kentf9022f62006-06-25 05:48:47 -0700267 /*
268 * Clean stale dentries below that have not been
269 * invalidated after a mount fail during lookup
270 */
271 d_invalidate(p);
272
Ian Kent1aff3c82006-03-27 01:14:46 -0800273 /* allow for dget above and top is already dgot */
274 if (p == top)
275 ino_count += 2;
276 else
277 ino_count++;
278
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100279 if (p->d_count > ino_count) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800280 top_ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -0800281 dput(p);
282 return 1;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800286
287 /* Timeout of a tree mount is ultimately determined by its top dentry */
288 if (!autofs4_can_expire(top, timeout, do_now))
289 return 1;
290
Ian Kent1f5f2c32006-03-27 01:14:44 -0800291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
295 struct dentry *parent,
296 unsigned long timeout,
297 int do_now)
298{
Ian Kent1ce12ba2006-03-27 01:14:45 -0800299 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 DPRINTK("parent %p %.*s",
302 parent, (int)parent->d_name.len, parent->d_name.name);
303
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100304 p = NULL;
305 while ((p = get_next_positive_dentry(p, parent))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800307 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Ian Kent1ce12ba2006-03-27 01:14:45 -0800309 if (d_mountpoint(p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800310 /* Can we umount this guy */
311 if (autofs4_mount_busy(mnt, p))
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100312 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Ian Kente0a7aae2006-03-27 01:14:47 -0800314 /* Can we expire this guy */
315 if (autofs4_can_expire(p, timeout, do_now))
Ian Kent1ce12ba2006-03-27 01:14:45 -0800316 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return NULL;
320}
321
Ian Kent3a15e2a2006-03-27 01:14:55 -0800322/* Check if we can expire a direct mount (possibly a tree) */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700323struct dentry *autofs4_expire_direct(struct super_block *sb,
324 struct vfsmount *mnt,
325 struct autofs_sb_info *sbi,
326 int how)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800327{
328 unsigned long timeout;
329 struct dentry *root = dget(sb->s_root);
330 int do_now = how & AUTOFS_EXP_IMMEDIATE;
Ian Kentb5b80172011-01-14 18:46:03 +0000331 struct autofs_info *ino;
Ian Kent3a15e2a2006-03-27 01:14:55 -0800332
Ian Kentc0ba7e52006-09-25 16:24:16 -0700333 if (!root)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800334 return NULL;
335
336 now = jiffies;
337 timeout = sbi->exp_timeout;
338
Ian Kent3a15e2a2006-03-27 01:14:55 -0800339 spin_lock(&sbi->fs_lock);
Ian Kentb5b80172011-01-14 18:46:03 +0000340 ino = autofs4_dentry_ino(root);
341 /* No point expiring a pending mount */
Ian Kentf9398c22011-03-25 01:51:14 +0800342 if (ino->flags & AUTOFS_INF_PENDING)
343 goto out;
Ian Kent3a15e2a2006-03-27 01:14:55 -0800344 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
345 struct autofs_info *ino = autofs4_dentry_ino(root);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800346 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700347 init_completion(&ino->expire_complete);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800348 spin_unlock(&sbi->fs_lock);
349 return root;
350 }
Ian Kentf9398c22011-03-25 01:51:14 +0800351out:
Ian Kent3a15e2a2006-03-27 01:14:55 -0800352 spin_unlock(&sbi->fs_lock);
353 dput(root);
354
355 return NULL;
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * Find an eligible tree to time-out
360 * A tree is eligible if :-
361 * - it is unused by any user process
362 * - it has been unused for exp_timeout time
363 */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700364struct dentry *autofs4_expire_indirect(struct super_block *sb,
365 struct vfsmount *mnt,
366 struct autofs_sb_info *sbi,
367 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 unsigned long timeout;
370 struct dentry *root = sb->s_root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100371 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct dentry *expired = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int do_now = how & AUTOFS_EXP_IMMEDIATE;
374 int exp_leaves = how & AUTOFS_EXP_LEAVES;
Ian Kent97e74492008-07-23 21:30:26 -0700375 struct autofs_info *ino;
376 unsigned int ino_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Ian Kentc0ba7e52006-09-25 16:24:16 -0700378 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return NULL;
380
381 now = jiffies;
382 timeout = sbi->exp_timeout;
383
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100384 dentry = NULL;
Ian Kentd4a85e32011-03-25 01:51:20 +0800385 while ((dentry = get_next_positive_subdir(dentry, root))) {
Ian Kent97e74492008-07-23 21:30:26 -0700386 spin_lock(&sbi->fs_lock);
387 ino = autofs4_dentry_ino(dentry);
Ian Kentb5b80172011-01-14 18:46:03 +0000388 /* No point expiring a pending mount */
389 if (ino->flags & AUTOFS_INF_PENDING)
Ian Kent3c319982011-03-25 01:51:08 +0800390 goto next;
Ian Kent97e74492008-07-23 21:30:26 -0700391
Ian Kent3a15e2a2006-03-27 01:14:55 -0800392 /*
393 * Case 1: (i) indirect mount or top level pseudo direct mount
394 * (autofs-4.1).
395 * (ii) indirect mount with offset mount, check the "/"
396 * offset (autofs-5.0+).
397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (d_mountpoint(dentry)) {
399 DPRINTK("checking mountpoint %p %.*s",
400 dentry, (int)dentry->d_name.len, dentry->d_name.name);
401
Ian Kent97e74492008-07-23 21:30:26 -0700402 /* Path walk currently on this dentry? */
403 ino_count = atomic_read(&ino->count) + 2;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100404 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700405 goto next;
406
Ian Kente0a7aae2006-03-27 01:14:47 -0800407 /* Can we umount this guy */
408 if (autofs4_mount_busy(mnt, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto next;
410
Ian Kente0a7aae2006-03-27 01:14:47 -0800411 /* Can we expire this guy */
412 if (autofs4_can_expire(dentry, timeout, do_now)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700414 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 goto next;
417 }
418
Ian Kent1f5f2c32006-03-27 01:14:44 -0800419 if (simple_empty(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto next;
421
422 /* Case 2: tree mount, expire iff entire tree is not busy */
423 if (!exp_leaves) {
Ian Kent97e74492008-07-23 21:30:26 -0700424 /* Path walk currently on this dentry? */
425 ino_count = atomic_read(&ino->count) + 1;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100426 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700427 goto next;
Ian Kent3a9720c2005-05-01 08:59:17 -0700428
Ian Kent97e74492008-07-23 21:30:26 -0700429 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700430 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700431 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Ian Kent3a15e2a2006-03-27 01:14:55 -0800433 /*
434 * Case 3: pseudo direct mount, expire individual leaves
435 * (autofs-4.1).
436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else {
Ian Kent97e74492008-07-23 21:30:26 -0700438 /* Path walk currently on this dentry? */
439 ino_count = atomic_read(&ino->count) + 1;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100440 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700441 goto next;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
444 if (expired) {
445 dput(dentry);
Ian Kentafec5702008-05-01 04:35:06 -0700446 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 }
449next:
Ian Kent97e74492008-07-23 21:30:26 -0700450 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return NULL;
Ian Kentafec5702008-05-01 04:35:06 -0700453
454found:
455 DPRINTK("returning %p %.*s",
456 expired, (int)expired->d_name.len, expired->d_name.name);
Ian Kent97e74492008-07-23 21:30:26 -0700457 ino = autofs4_dentry_ino(expired);
458 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700459 init_completion(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700460 spin_unlock(&sbi->fs_lock);
Ian Kente7854722011-03-25 01:51:31 +0800461 spin_lock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100462 spin_lock(&expired->d_parent->d_lock);
463 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
Ian Kentafec5702008-05-01 04:35:06 -0700464 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100465 spin_unlock(&expired->d_lock);
466 spin_unlock(&expired->d_parent->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800467 spin_unlock(&sbi->lookup_lock);
Ian Kentafec5702008-05-01 04:35:06 -0700468 return expired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Ian Kent06a35982008-07-23 21:30:28 -0700471int autofs4_expire_wait(struct dentry *dentry)
472{
473 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
474 struct autofs_info *ino = autofs4_dentry_ino(dentry);
475 int status;
476
477 /* Block on any pending expire */
478 spin_lock(&sbi->fs_lock);
479 if (ino->flags & AUTOFS_INF_EXPIRING) {
480 spin_unlock(&sbi->fs_lock);
481
482 DPRINTK("waiting for expire %p name=%.*s",
483 dentry, dentry->d_name.len, dentry->d_name.name);
484
485 status = autofs4_wait(sbi, dentry, NFY_NONE);
486 wait_for_completion(&ino->expire_complete);
487
488 DPRINTK("expire done status=%d", status);
489
Al Viro4b1ae272010-03-03 12:58:31 -0500490 if (d_unhashed(dentry))
Ian Kent06a35982008-07-23 21:30:28 -0700491 return -EAGAIN;
492
493 return status;
494 }
495 spin_unlock(&sbi->fs_lock);
496
497 return 0;
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/* Perform an expiry operation */
501int autofs4_expire_run(struct super_block *sb,
502 struct vfsmount *mnt,
503 struct autofs_sb_info *sbi,
504 struct autofs_packet_expire __user *pkt_p)
505{
506 struct autofs_packet_expire pkt;
Ian Kent97e74492008-07-23 21:30:26 -0700507 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct dentry *dentry;
Ian Kent97e74492008-07-23 21:30:26 -0700509 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 memset(&pkt,0,sizeof pkt);
512
513 pkt.hdr.proto_version = sbi->version;
514 pkt.hdr.type = autofs_ptype_expire;
515
Ian Kent3a15e2a2006-03-27 01:14:55 -0800516 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EAGAIN;
518
519 pkt.len = dentry->d_name.len;
520 memcpy(pkt.name, dentry->d_name.name, pkt.len);
521 pkt.name[pkt.len] = '\0';
522 dput(dentry);
523
524 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
Ian Kent97e74492008-07-23 21:30:26 -0700525 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Ian Kent97e74492008-07-23 21:30:26 -0700527 spin_lock(&sbi->fs_lock);
528 ino = autofs4_dentry_ino(dentry);
529 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700530 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700531 spin_unlock(&sbi->fs_lock);
532
533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Ian Kent56fcef72009-03-31 15:24:43 -0700536int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
537 struct autofs_sb_info *sbi, int when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct dentry *dentry;
540 int ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Ian Kenta92daf62009-01-06 14:42:08 -0800542 if (autofs_type_trigger(sbi->type))
Ian Kent56fcef72009-03-31 15:24:43 -0700543 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800544 else
Ian Kent56fcef72009-03-31 15:24:43 -0700545 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800546
547 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800548 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* This is synchronous because it makes the daemon a
551 little easier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700553
Ian Kent97e74492008-07-23 21:30:26 -0700554 spin_lock(&sbi->fs_lock);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800555 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kentb5b80172011-01-14 18:46:03 +0000556 spin_lock(&dentry->d_lock);
Ian Kent3c319982011-03-25 01:51:08 +0800557 if (!ret) {
Ian Kentb5b80172011-01-14 18:46:03 +0000558 if ((IS_ROOT(dentry) ||
559 (autofs_type_indirect(sbi->type) &&
560 IS_ROOT(dentry->d_parent))) &&
561 !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
562 __managed_dentry_set_automount(dentry);
563 }
564 spin_unlock(&dentry->d_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700565 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700566 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dput(dentry);
568 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return ret;
571}
572
Ian Kent56fcef72009-03-31 15:24:43 -0700573/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
574 more to be done */
575int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
576 struct autofs_sb_info *sbi, int __user *arg)
577{
578 int do_now = 0;
579
580 if (arg && get_user(do_now, arg))
581 return -EFAULT;
582
583 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
584}
585