blob: 0571ec8352b747a6fced1313d0ece2c5c00cb0b1 [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
29 /* No point expiring a pending mount */
Ian Kentaa952eb2009-12-15 16:45:45 -080030 if (ino->flags & AUTOFS_INF_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 0;
32
33 if (!do_now) {
34 /* Too young to die */
Ian Kentc0ba7e52006-09-25 16:24:16 -070035 if (!timeout || time_after(ino->last_used + timeout, now))
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return 1;
45}
46
Ian Kent1f5f2c32006-03-27 01:14:44 -080047/* Check a mount point for busyness */
48static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Ian Kente0a7aae2006-03-27 01:14:47 -080050 struct dentry *top = dentry;
Al Viro9393bd02009-04-18 13:58:15 -040051 struct path path = {.mnt = mnt, .dentry = dentry};
Ian Kent1f5f2c32006-03-27 01:14:44 -080052 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 DPRINTK("dentry %p %.*s",
55 dentry, (int)dentry->d_name.len, dentry->d_name.name);
56
Al Viro9393bd02009-04-18 13:58:15 -040057 path_get(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Howellscc53ce52011-01-14 18:45:26 +000059 if (!follow_down_one(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 goto done;
61
Al Viro9393bd02009-04-18 13:58:15 -040062 if (is_autofs4_dentry(path.dentry)) {
63 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
Ian Kentbc9c4062008-11-06 12:53:22 -080064
65 /* This is an autofs submount, we can't expire it */
Ian Kenta92daf62009-01-06 14:42:08 -080066 if (autofs_type_indirect(sbi->type))
Ian Kentbc9c4062008-11-06 12:53:22 -080067 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 */
Al Viro9393bd02009-04-18 13:58:15 -040073 if (!d_mountpoint(path.dentry)) {
Ian Kenta8985f32009-04-30 15:08:09 -070074 status = 0;
Ian Kentbc9c4062008-11-06 12:53:22 -080075 goto done;
Ian Kenta8985f32009-04-30 15:08:09 -070076 }
Ian Kentbc9c4062008-11-06 12:53:22 -080077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Ian Kente0a7aae2006-03-27 01:14:47 -080079 /* Update the expiry counter if fs is busy */
Ian Kent37d08922009-09-01 11:26:22 +080080 if (!may_umount_tree(path.mnt)) {
Ian Kente0a7aae2006-03-27 01:14:47 -080081 struct autofs_info *ino = autofs4_dentry_ino(top);
82 ino->last_used = jiffies;
83 goto done;
84 }
85
86 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087done:
88 DPRINTK("returning = %d", status);
Al Viro9393bd02009-04-18 13:58:15 -040089 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return status;
91}
92
Ian Kent1ce12ba2006-03-27 01:14:45 -080093/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110094 * Calculate and dget next entry in top down tree traversal.
Ian Kent1ce12ba2006-03-27 01:14:45 -080095 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110096static struct dentry *get_next_positive_dentry(struct dentry *prev,
97 struct dentry *root)
Ian Kent1ce12ba2006-03-27 01:14:45 -080098{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110099 struct list_head *next;
100 struct dentry *p, *ret;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800101
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100102 if (prev == NULL)
103 return dget(prev);
104
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100105 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100106relock:
107 p = prev;
108 spin_lock(&p->d_lock);
109again:
110 next = p->d_subdirs.next;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800111 if (next == &p->d_subdirs) {
112 while (1) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100113 struct dentry *parent;
114
115 if (p == root) {
116 spin_unlock(&p->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100117 spin_unlock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100118 dput(prev);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800119 return NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100120 }
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);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800129 next = p->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100130 p = parent;
131 if (next != &parent->d_subdirs)
Ian Kent1ce12ba2006-03-27 01:14:45 -0800132 break;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800133 }
134 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100135 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);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100147 spin_unlock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100148
149 dput(prev);
150
151 return ret;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800152}
153
Ian Kent3a15e2a2006-03-27 01:14:55 -0800154/*
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 */
160static 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
Ian Kent3a15e2a2006-03-27 01:14:55 -0800168 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* Check a directory tree of mount points for busyness
184 * The tree is not busy iff no mountpoints are busy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800186static int autofs4_tree_busy(struct vfsmount *mnt,
187 struct dentry *top,
188 unsigned long timeout,
189 int do_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Ian Kente0a7aae2006-03-27 01:14:47 -0800191 struct autofs_info *top_ino = autofs4_dentry_ino(top);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800192 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Ian Kent1f5f2c32006-03-27 01:14:44 -0800194 DPRINTK("top %p %.*s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 top, (int)top->d_name.len, top->d_name.name);
196
197 /* Negative dentry - give up */
198 if (!simple_positive(top))
Ian Kent1f5f2c32006-03-27 01:14:44 -0800199 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100201 p = NULL;
202 while ((p = get_next_positive_dentry(p, top))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800204 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Ian Kent1aff3c82006-03-27 01:14:46 -0800206 /*
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.
Ian Kente0a7aae2006-03-27 01:14:47 -0800210 * If the fs is busy update the expiry counter.
Ian Kent1aff3c82006-03-27 01:14:46 -0800211 */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800212 if (d_mountpoint(p)) {
Ian Kent1ce12ba2006-03-27 01:14:45 -0800213 if (autofs4_mount_busy(mnt, p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800214 top_ino->last_used = jiffies;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800215 dput(p);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800216 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800218 } else {
Ian Kente0a7aae2006-03-27 01:14:47 -0800219 struct autofs_info *ino = autofs4_dentry_ino(p);
Ian Kent1aff3c82006-03-27 01:14:46 -0800220 unsigned int ino_count = atomic_read(&ino->count);
221
Ian Kentf9022f62006-06-25 05:48:47 -0700222 /*
223 * Clean stale dentries below that have not been
224 * invalidated after a mount fail during lookup
225 */
226 d_invalidate(p);
227
Ian Kent1aff3c82006-03-27 01:14:46 -0800228 /* allow for dget above and top is already dgot */
229 if (p == top)
230 ino_count += 2;
231 else
232 ino_count++;
233
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100234 if (p->d_count > ino_count) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800235 top_ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -0800236 dput(p);
237 return 1;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800241
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
Ian Kent1f5f2c32006-03-27 01:14:44 -0800246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
250 struct dentry *parent,
251 unsigned long timeout,
252 int do_now)
253{
Ian Kent1ce12ba2006-03-27 01:14:45 -0800254 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 DPRINTK("parent %p %.*s",
257 parent, (int)parent->d_name.len, parent->d_name.name);
258
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100259 p = NULL;
260 while ((p = get_next_positive_dentry(p, parent))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800262 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Ian Kent1ce12ba2006-03-27 01:14:45 -0800264 if (d_mountpoint(p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800265 /* Can we umount this guy */
266 if (autofs4_mount_busy(mnt, p))
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100267 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Ian Kente0a7aae2006-03-27 01:14:47 -0800269 /* Can we expire this guy */
270 if (autofs4_can_expire(p, timeout, do_now))
Ian Kent1ce12ba2006-03-27 01:14:45 -0800271 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return NULL;
275}
276
Ian Kent3a15e2a2006-03-27 01:14:55 -0800277/* Check if we can expire a direct mount (possibly a tree) */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700278struct dentry *autofs4_expire_direct(struct super_block *sb,
279 struct vfsmount *mnt,
280 struct autofs_sb_info *sbi,
281 int how)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800282{
283 unsigned long timeout;
284 struct dentry *root = dget(sb->s_root);
285 int do_now = how & AUTOFS_EXP_IMMEDIATE;
286
Ian Kentc0ba7e52006-09-25 16:24:16 -0700287 if (!root)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800288 return NULL;
289
290 now = jiffies;
291 timeout = sbi->exp_timeout;
292
Ian Kent3a15e2a2006-03-27 01:14:55 -0800293 spin_lock(&sbi->fs_lock);
294 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
295 struct autofs_info *ino = autofs4_dentry_ino(root);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700296 if (d_mountpoint(root)) {
297 ino->flags |= AUTOFS_INF_MOUNTPOINT;
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100298 spin_lock(&root->d_lock);
299 root->d_flags &= ~DCACHE_MOUNTED;
300 spin_unlock(&root->d_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700301 }
Ian Kent3a15e2a2006-03-27 01:14:55 -0800302 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent10584212011-01-14 18:45:58 +0000303 managed_dentry_set_automount(root);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700304 init_completion(&ino->expire_complete);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800305 spin_unlock(&sbi->fs_lock);
306 return root;
307 }
308 spin_unlock(&sbi->fs_lock);
309 dput(root);
310
311 return NULL;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*
315 * Find an eligible tree to time-out
316 * A tree is eligible if :-
317 * - it is unused by any user process
318 * - it has been unused for exp_timeout time
319 */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700320struct dentry *autofs4_expire_indirect(struct super_block *sb,
321 struct vfsmount *mnt,
322 struct autofs_sb_info *sbi,
323 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 unsigned long timeout;
326 struct dentry *root = sb->s_root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100327 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct dentry *expired = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int do_now = how & AUTOFS_EXP_IMMEDIATE;
330 int exp_leaves = how & AUTOFS_EXP_LEAVES;
Ian Kent97e74492008-07-23 21:30:26 -0700331 struct autofs_info *ino;
332 unsigned int ino_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Ian Kentc0ba7e52006-09-25 16:24:16 -0700334 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return NULL;
336
337 now = jiffies;
338 timeout = sbi->exp_timeout;
339
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100340 dentry = NULL;
341 while ((dentry = get_next_positive_dentry(dentry, root))) {
Ian Kent97e74492008-07-23 21:30:26 -0700342 spin_lock(&sbi->fs_lock);
343 ino = autofs4_dentry_ino(dentry);
344
Ian Kent3a15e2a2006-03-27 01:14:55 -0800345 /*
346 * Case 1: (i) indirect mount or top level pseudo direct mount
347 * (autofs-4.1).
348 * (ii) indirect mount with offset mount, check the "/"
349 * offset (autofs-5.0+).
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (d_mountpoint(dentry)) {
352 DPRINTK("checking mountpoint %p %.*s",
353 dentry, (int)dentry->d_name.len, dentry->d_name.name);
354
Ian Kent97e74492008-07-23 21:30:26 -0700355 /* Path walk currently on this dentry? */
356 ino_count = atomic_read(&ino->count) + 2;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100357 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700358 goto next;
359
Ian Kente0a7aae2006-03-27 01:14:47 -0800360 /* Can we umount this guy */
361 if (autofs4_mount_busy(mnt, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto next;
363
Ian Kente0a7aae2006-03-27 01:14:47 -0800364 /* Can we expire this guy */
365 if (autofs4_can_expire(dentry, timeout, do_now)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700367 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 goto next;
370 }
371
Ian Kent1f5f2c32006-03-27 01:14:44 -0800372 if (simple_empty(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto next;
374
375 /* Case 2: tree mount, expire iff entire tree is not busy */
376 if (!exp_leaves) {
Ian Kent97e74492008-07-23 21:30:26 -0700377 /* Path walk currently on this dentry? */
378 ino_count = atomic_read(&ino->count) + 1;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100379 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700380 goto next;
Ian Kent3a9720c2005-05-01 08:59:17 -0700381
Ian Kent97e74492008-07-23 21:30:26 -0700382 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700383 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700384 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Ian Kent3a15e2a2006-03-27 01:14:55 -0800386 /*
387 * Case 3: pseudo direct mount, expire individual leaves
388 * (autofs-4.1).
389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else {
Ian Kent97e74492008-07-23 21:30:26 -0700391 /* Path walk currently on this dentry? */
392 ino_count = atomic_read(&ino->count) + 1;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100393 if (dentry->d_count > ino_count)
Ian Kent97e74492008-07-23 21:30:26 -0700394 goto next;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
397 if (expired) {
398 dput(dentry);
Ian Kentafec5702008-05-01 04:35:06 -0700399 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 }
402next:
Ian Kent97e74492008-07-23 21:30:26 -0700403 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return NULL;
Ian Kentafec5702008-05-01 04:35:06 -0700406
407found:
408 DPRINTK("returning %p %.*s",
409 expired, (int)expired->d_name.len, expired->d_name.name);
Ian Kent97e74492008-07-23 21:30:26 -0700410 ino = autofs4_dentry_ino(expired);
411 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent10584212011-01-14 18:45:58 +0000412 managed_dentry_set_automount(expired);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700413 init_completion(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700414 spin_unlock(&sbi->fs_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100415 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100416 spin_lock(&expired->d_parent->d_lock);
417 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
Ian Kentafec5702008-05-01 04:35:06 -0700418 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100419 spin_unlock(&expired->d_lock);
420 spin_unlock(&expired->d_parent->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100421 spin_unlock(&autofs4_lock);
Ian Kentafec5702008-05-01 04:35:06 -0700422 return expired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Ian Kent06a35982008-07-23 21:30:28 -0700425int autofs4_expire_wait(struct dentry *dentry)
426{
427 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
428 struct autofs_info *ino = autofs4_dentry_ino(dentry);
429 int status;
430
431 /* Block on any pending expire */
432 spin_lock(&sbi->fs_lock);
433 if (ino->flags & AUTOFS_INF_EXPIRING) {
434 spin_unlock(&sbi->fs_lock);
435
436 DPRINTK("waiting for expire %p name=%.*s",
437 dentry, dentry->d_name.len, dentry->d_name.name);
438
439 status = autofs4_wait(sbi, dentry, NFY_NONE);
440 wait_for_completion(&ino->expire_complete);
441
442 DPRINTK("expire done status=%d", status);
443
Al Viro4b1ae272010-03-03 12:58:31 -0500444 if (d_unhashed(dentry))
Ian Kent06a35982008-07-23 21:30:28 -0700445 return -EAGAIN;
446
447 return status;
448 }
449 spin_unlock(&sbi->fs_lock);
450
451 return 0;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* Perform an expiry operation */
455int autofs4_expire_run(struct super_block *sb,
456 struct vfsmount *mnt,
457 struct autofs_sb_info *sbi,
458 struct autofs_packet_expire __user *pkt_p)
459{
460 struct autofs_packet_expire pkt;
Ian Kent97e74492008-07-23 21:30:26 -0700461 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct dentry *dentry;
Ian Kent97e74492008-07-23 21:30:26 -0700463 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 memset(&pkt,0,sizeof pkt);
466
467 pkt.hdr.proto_version = sbi->version;
468 pkt.hdr.type = autofs_ptype_expire;
469
Ian Kent3a15e2a2006-03-27 01:14:55 -0800470 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EAGAIN;
472
473 pkt.len = dentry->d_name.len;
474 memcpy(pkt.name, dentry->d_name.name, pkt.len);
475 pkt.name[pkt.len] = '\0';
476 dput(dentry);
477
478 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
Ian Kent97e74492008-07-23 21:30:26 -0700479 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Ian Kent97e74492008-07-23 21:30:26 -0700481 spin_lock(&sbi->fs_lock);
482 ino = autofs4_dentry_ino(dentry);
483 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent10584212011-01-14 18:45:58 +0000484 if (!d_unhashed(dentry))
485 managed_dentry_clear_automount(dentry);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700486 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700487 spin_unlock(&sbi->fs_lock);
488
489 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Ian Kent56fcef72009-03-31 15:24:43 -0700492int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
493 struct autofs_sb_info *sbi, int when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct dentry *dentry;
496 int ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Ian Kenta92daf62009-01-06 14:42:08 -0800498 if (autofs_type_trigger(sbi->type))
Ian Kent56fcef72009-03-31 15:24:43 -0700499 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800500 else
Ian Kent56fcef72009-03-31 15:24:43 -0700501 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800502
503 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800504 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* This is synchronous because it makes the daemon a
507 little easier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700509
Ian Kent97e74492008-07-23 21:30:26 -0700510 spin_lock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700511 if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100512 spin_lock(&sb->s_root->d_lock);
513 /*
514 * If we haven't been expired away, then reset
515 * mounted status.
516 */
517 if (mnt->mnt_parent != mnt)
518 sb->s_root->d_flags |= DCACHE_MOUNTED;
519 spin_unlock(&sb->s_root->d_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700520 ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
521 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800522 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent10584212011-01-14 18:45:58 +0000523 if (ret)
524 managed_dentry_clear_automount(dentry);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 dput(dentry);
528 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return ret;
531}
532
Ian Kent56fcef72009-03-31 15:24:43 -0700533/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
534 more to be done */
535int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
536 struct autofs_sb_info *sbi, int __user *arg)
537{
538 int do_now = 0;
539
540 if (arg && get_user(do_now, arg))
541 return -EFAULT;
542
543 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
544}
545