blob: 01443ce43ee75f072158afbc313a6e206bb2f10d [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 Kente0a7aae2006-03-27 01:14:47 -0800402 /* Can we umount this guy */
403 if (autofs4_mount_busy(mnt, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto next;
405
Ian Kente0a7aae2006-03-27 01:14:47 -0800406 /* Can we expire this guy */
407 if (autofs4_can_expire(dentry, timeout, do_now)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700409 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 goto next;
412 }
413
Ian Kent1f5f2c32006-03-27 01:14:44 -0800414 if (simple_empty(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto next;
416
417 /* Case 2: tree mount, expire iff entire tree is not busy */
418 if (!exp_leaves) {
Ian Kent97e74492008-07-23 21:30:26 -0700419 /* Path walk currently on this dentry? */
420 ino_count = atomic_read(&ino->count) + 1;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100421 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700422 goto next;
Ian Kent3a9720c2005-05-01 08:59:17 -0700423
Ian Kent97e74492008-07-23 21:30:26 -0700424 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700425 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700426 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Ian Kent3a15e2a2006-03-27 01:14:55 -0800428 /*
429 * Case 3: pseudo direct mount, expire individual leaves
430 * (autofs-4.1).
431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 } else {
Ian Kent97e74492008-07-23 21:30:26 -0700433 /* Path walk currently on this dentry? */
434 ino_count = atomic_read(&ino->count) + 1;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100435 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700436 goto next;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
439 if (expired) {
440 dput(dentry);
Ian Kentafec5702008-05-01 04:35:06 -0700441 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444next:
Ian Kent97e74492008-07-23 21:30:26 -0700445 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return NULL;
Ian Kentafec5702008-05-01 04:35:06 -0700448
449found:
450 DPRINTK("returning %p %.*s",
451 expired, (int)expired->d_name.len, expired->d_name.name);
Ian Kent97e74492008-07-23 21:30:26 -0700452 ino = autofs4_dentry_ino(expired);
453 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700454 init_completion(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700455 spin_unlock(&sbi->fs_lock);
Ian Kente7854722011-03-25 01:51:31 +0800456 spin_lock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100457 spin_lock(&expired->d_parent->d_lock);
458 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
Ian Kentafec5702008-05-01 04:35:06 -0700459 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100460 spin_unlock(&expired->d_lock);
461 spin_unlock(&expired->d_parent->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800462 spin_unlock(&sbi->lookup_lock);
Ian Kentafec5702008-05-01 04:35:06 -0700463 return expired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Ian Kent06a35982008-07-23 21:30:28 -0700466int autofs4_expire_wait(struct dentry *dentry)
467{
468 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
469 struct autofs_info *ino = autofs4_dentry_ino(dentry);
470 int status;
471
472 /* Block on any pending expire */
473 spin_lock(&sbi->fs_lock);
474 if (ino->flags & AUTOFS_INF_EXPIRING) {
475 spin_unlock(&sbi->fs_lock);
476
477 DPRINTK("waiting for expire %p name=%.*s",
478 dentry, dentry->d_name.len, dentry->d_name.name);
479
480 status = autofs4_wait(sbi, dentry, NFY_NONE);
481 wait_for_completion(&ino->expire_complete);
482
483 DPRINTK("expire done status=%d", status);
484
Al Viro4b1ae272010-03-03 12:58:31 -0500485 if (d_unhashed(dentry))
Ian Kent06a35982008-07-23 21:30:28 -0700486 return -EAGAIN;
487
488 return status;
489 }
490 spin_unlock(&sbi->fs_lock);
491
492 return 0;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/* Perform an expiry operation */
496int autofs4_expire_run(struct super_block *sb,
497 struct vfsmount *mnt,
498 struct autofs_sb_info *sbi,
499 struct autofs_packet_expire __user *pkt_p)
500{
501 struct autofs_packet_expire pkt;
Ian Kent97e74492008-07-23 21:30:26 -0700502 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct dentry *dentry;
Ian Kent97e74492008-07-23 21:30:26 -0700504 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 memset(&pkt,0,sizeof pkt);
507
508 pkt.hdr.proto_version = sbi->version;
509 pkt.hdr.type = autofs_ptype_expire;
510
Ian Kent3a15e2a2006-03-27 01:14:55 -0800511 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EAGAIN;
513
514 pkt.len = dentry->d_name.len;
515 memcpy(pkt.name, dentry->d_name.name, pkt.len);
516 pkt.name[pkt.len] = '\0';
517 dput(dentry);
518
519 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
Ian Kent97e74492008-07-23 21:30:26 -0700520 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Ian Kent97e74492008-07-23 21:30:26 -0700522 spin_lock(&sbi->fs_lock);
523 ino = autofs4_dentry_ino(dentry);
524 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700525 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700526 spin_unlock(&sbi->fs_lock);
527
528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Ian Kent56fcef72009-03-31 15:24:43 -0700531int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
532 struct autofs_sb_info *sbi, int when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct dentry *dentry;
535 int ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Ian Kenta92daf62009-01-06 14:42:08 -0800537 if (autofs_type_trigger(sbi->type))
Ian Kent56fcef72009-03-31 15:24:43 -0700538 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800539 else
Ian Kent56fcef72009-03-31 15:24:43 -0700540 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800541
542 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800543 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* This is synchronous because it makes the daemon a
546 little easier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700548
Ian Kent97e74492008-07-23 21:30:26 -0700549 spin_lock(&sbi->fs_lock);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800550 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700551 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700552 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 dput(dentry);
554 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return ret;
557}
558
Ian Kent56fcef72009-03-31 15:24:43 -0700559/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
560 more to be done */
561int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
562 struct autofs_sb_info *sbi, int __user *arg)
563{
564 int do_now = 0;
565
566 if (arg && get_user(do_now, arg))
567 return -EFAULT;
568
569 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
570}
571