blob: af141fc8cfe6d245b53c51d37aef3665f16b8b40 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return 1;
35}
36
Ian Kent1f5f2c32006-03-27 01:14:44 -080037/* Check a mount point for busyness */
38static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Ian Kente0a7aae2006-03-27 01:14:47 -080040 struct dentry *top = dentry;
Al Viro9393bd02009-04-18 13:58:15 -040041 struct path path = {.mnt = mnt, .dentry = dentry};
Ian Kent1f5f2c32006-03-27 01:14:44 -080042 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 DPRINTK("dentry %p %.*s",
45 dentry, (int)dentry->d_name.len, dentry->d_name.name);
46
Al Viro9393bd02009-04-18 13:58:15 -040047 path_get(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David Howellscc53ce52011-01-14 18:45:26 +000049 if (!follow_down_one(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 goto done;
51
Al Viro9393bd02009-04-18 13:58:15 -040052 if (is_autofs4_dentry(path.dentry)) {
53 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
Ian Kentbc9c4062008-11-06 12:53:22 -080054
55 /* This is an autofs submount, we can't expire it */
Ian Kenta92daf62009-01-06 14:42:08 -080056 if (autofs_type_indirect(sbi->type))
Ian Kentbc9c4062008-11-06 12:53:22 -080057 goto done;
Ian Kentbc9c4062008-11-06 12:53:22 -080058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Ian Kente0a7aae2006-03-27 01:14:47 -080060 /* Update the expiry counter if fs is busy */
Ian Kent37d08922009-09-01 11:26:22 +080061 if (!may_umount_tree(path.mnt)) {
Ian Kente0a7aae2006-03-27 01:14:47 -080062 struct autofs_info *ino = autofs4_dentry_ino(top);
63 ino->last_used = jiffies;
64 goto done;
65 }
66
67 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068done:
69 DPRINTK("returning = %d", status);
Al Viro9393bd02009-04-18 13:58:15 -040070 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return status;
72}
73
Ian Kent1ce12ba2006-03-27 01:14:45 -080074/*
Ian Kentd4a85e32011-03-25 01:51:20 +080075 * Calculate and dget next entry in the subdirs list under root.
76 */
77static struct dentry *get_next_positive_subdir(struct dentry *prev,
78 struct dentry *root)
79{
Ian Kente7854722011-03-25 01:51:31 +080080 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
Ian Kentd4a85e32011-03-25 01:51:20 +080081 struct list_head *next;
Ian Kenta45440f2012-08-06 09:37:47 +080082 struct dentry *q;
Ian Kentd4a85e32011-03-25 01:51:20 +080083
Ian Kente7854722011-03-25 01:51:31 +080084 spin_lock(&sbi->lookup_lock);
Ian Kenta45440f2012-08-06 09:37:47 +080085 spin_lock(&root->d_lock);
Ian Kentd4a85e32011-03-25 01:51:20 +080086
Ian Kenta45440f2012-08-06 09:37:47 +080087 if (prev)
88 next = prev->d_u.d_child.next;
89 else {
Ian Kentd4a85e32011-03-25 01:51:20 +080090 prev = dget_dlock(root);
91 next = prev->d_subdirs.next;
Ian Kentd4a85e32011-03-25 01:51:20 +080092 }
93
Ian Kenta45440f2012-08-06 09:37:47 +080094cont:
Ian Kentd4a85e32011-03-25 01:51:20 +080095 if (next == &root->d_subdirs) {
Ian Kenta45440f2012-08-06 09:37:47 +080096 spin_unlock(&root->d_lock);
Ian Kente7854722011-03-25 01:51:31 +080097 spin_unlock(&sbi->lookup_lock);
Ian Kentd4a85e32011-03-25 01:51:20 +080098 dput(prev);
99 return NULL;
100 }
101
102 q = list_entry(next, struct dentry, d_u.d_child);
103
104 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
Ian Kenta45440f2012-08-06 09:37:47 +0800105 /* Already gone or negative dentry (under construction) - try next */
Al Viro84d08fa2013-07-05 18:59:33 +0400106 if (!d_count(q) || !simple_positive(q)) {
Ian Kenta45440f2012-08-06 09:37:47 +0800107 spin_unlock(&q->d_lock);
108 next = q->d_u.d_child.next;
109 goto cont;
Ian Kentd4a85e32011-03-25 01:51:20 +0800110 }
111 dget_dlock(q);
112 spin_unlock(&q->d_lock);
Ian Kenta45440f2012-08-06 09:37:47 +0800113 spin_unlock(&root->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800114 spin_unlock(&sbi->lookup_lock);
Ian Kentd4a85e32011-03-25 01:51:20 +0800115
116 dput(prev);
117
118 return q;
119}
120
121/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100122 * Calculate and dget next entry in top down tree traversal.
Ian Kent1ce12ba2006-03-27 01:14:45 -0800123 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100124static struct dentry *get_next_positive_dentry(struct dentry *prev,
125 struct dentry *root)
Ian Kent1ce12ba2006-03-27 01:14:45 -0800126{
Ian Kente7854722011-03-25 01:51:31 +0800127 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100128 struct list_head *next;
129 struct dentry *p, *ret;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800130
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100131 if (prev == NULL)
Ian Kentc14cc632011-01-18 12:06:04 +0800132 return dget(root);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100133
Ian Kente7854722011-03-25 01:51:31 +0800134 spin_lock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100135relock:
136 p = prev;
137 spin_lock(&p->d_lock);
138again:
139 next = p->d_subdirs.next;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800140 if (next == &p->d_subdirs) {
141 while (1) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100142 struct dentry *parent;
143
144 if (p == root) {
145 spin_unlock(&p->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800146 spin_unlock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100147 dput(prev);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800148 return NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100149 }
150
151 parent = p->d_parent;
152 if (!spin_trylock(&parent->d_lock)) {
153 spin_unlock(&p->d_lock);
154 cpu_relax();
155 goto relock;
156 }
157 spin_unlock(&p->d_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800158 next = p->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100159 p = parent;
160 if (next != &parent->d_subdirs)
Ian Kent1ce12ba2006-03-27 01:14:45 -0800161 break;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800162 }
163 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100164 ret = list_entry(next, struct dentry, d_u.d_child);
165
166 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
167 /* Negative dentry - try next */
168 if (!simple_positive(ret)) {
Ian Kentc14cc632011-01-18 12:06:04 +0800169 spin_unlock(&p->d_lock);
Steven Rostedt1d6f2092011-08-22 11:52:28 +0800170 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100171 p = ret;
172 goto again;
173 }
174 dget_dlock(ret);
175 spin_unlock(&ret->d_lock);
176 spin_unlock(&p->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800177 spin_unlock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100178
179 dput(prev);
180
181 return ret;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800182}
183
Ian Kent3a15e2a2006-03-27 01:14:55 -0800184/*
185 * Check a direct mount point for busyness.
186 * Direct mounts have similar expiry semantics to tree mounts.
187 * The tree is not busy iff no mountpoints are busy and there are no
188 * autofs submounts.
189 */
190static int autofs4_direct_busy(struct vfsmount *mnt,
191 struct dentry *top,
192 unsigned long timeout,
193 int do_now)
194{
195 DPRINTK("top %p %.*s",
196 top, (int) top->d_name.len, top->d_name.name);
197
Ian Kent3a15e2a2006-03-27 01:14:55 -0800198 /* If it's busy update the expiry counters */
199 if (!may_umount_tree(mnt)) {
200 struct autofs_info *ino = autofs4_dentry_ino(top);
201 if (ino)
202 ino->last_used = jiffies;
203 return 1;
204 }
205
206 /* Timeout of a direct mount is determined by its top dentry */
207 if (!autofs4_can_expire(top, timeout, do_now))
208 return 1;
209
210 return 0;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* Check a directory tree of mount points for busyness
214 * The tree is not busy iff no mountpoints are busy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800216static int autofs4_tree_busy(struct vfsmount *mnt,
217 struct dentry *top,
218 unsigned long timeout,
219 int do_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Ian Kente0a7aae2006-03-27 01:14:47 -0800221 struct autofs_info *top_ino = autofs4_dentry_ino(top);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800222 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Ian Kent1f5f2c32006-03-27 01:14:44 -0800224 DPRINTK("top %p %.*s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 top, (int)top->d_name.len, top->d_name.name);
226
227 /* Negative dentry - give up */
228 if (!simple_positive(top))
Ian Kent1f5f2c32006-03-27 01:14:44 -0800229 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100231 p = NULL;
232 while ((p = get_next_positive_dentry(p, top))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800234 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Ian Kent1aff3c82006-03-27 01:14:46 -0800236 /*
237 * Is someone visiting anywhere in the subtree ?
238 * If there's no mount we need to check the usage
239 * count for the autofs dentry.
Ian Kente0a7aae2006-03-27 01:14:47 -0800240 * If the fs is busy update the expiry counter.
Ian Kent1aff3c82006-03-27 01:14:46 -0800241 */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800242 if (d_mountpoint(p)) {
Ian Kent1ce12ba2006-03-27 01:14:45 -0800243 if (autofs4_mount_busy(mnt, p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800244 top_ino->last_used = jiffies;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800245 dput(p);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800246 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800248 } else {
Ian Kente0a7aae2006-03-27 01:14:47 -0800249 struct autofs_info *ino = autofs4_dentry_ino(p);
Ian Kent1aff3c82006-03-27 01:14:46 -0800250 unsigned int ino_count = atomic_read(&ino->count);
251
252 /* allow for dget above and top is already dgot */
253 if (p == top)
254 ino_count += 2;
255 else
256 ino_count++;
257
Al Viro84d08fa2013-07-05 18:59:33 +0400258 if (d_count(p) > ino_count) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800259 top_ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -0800260 dput(p);
261 return 1;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800265
266 /* Timeout of a tree mount is ultimately determined by its top dentry */
267 if (!autofs4_can_expire(top, timeout, do_now))
268 return 1;
269
Ian Kent1f5f2c32006-03-27 01:14:44 -0800270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
274 struct dentry *parent,
275 unsigned long timeout,
276 int do_now)
277{
Ian Kent1ce12ba2006-03-27 01:14:45 -0800278 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 DPRINTK("parent %p %.*s",
281 parent, (int)parent->d_name.len, parent->d_name.name);
282
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100283 p = NULL;
284 while ((p = get_next_positive_dentry(p, parent))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800286 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Ian Kent1ce12ba2006-03-27 01:14:45 -0800288 if (d_mountpoint(p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800289 /* Can we umount this guy */
290 if (autofs4_mount_busy(mnt, p))
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100291 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Ian Kente0a7aae2006-03-27 01:14:47 -0800293 /* Can we expire this guy */
294 if (autofs4_can_expire(p, timeout, do_now))
Ian Kent1ce12ba2006-03-27 01:14:45 -0800295 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return NULL;
299}
300
Ian Kent3a15e2a2006-03-27 01:14:55 -0800301/* Check if we can expire a direct mount (possibly a tree) */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700302struct dentry *autofs4_expire_direct(struct super_block *sb,
303 struct vfsmount *mnt,
304 struct autofs_sb_info *sbi,
305 int how)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800306{
307 unsigned long timeout;
308 struct dentry *root = dget(sb->s_root);
309 int do_now = how & AUTOFS_EXP_IMMEDIATE;
Ian Kentb5b80172011-01-14 18:46:03 +0000310 struct autofs_info *ino;
Ian Kent3a15e2a2006-03-27 01:14:55 -0800311
Ian Kentc0ba7e52006-09-25 16:24:16 -0700312 if (!root)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800313 return NULL;
314
315 now = jiffies;
316 timeout = sbi->exp_timeout;
317
Ian Kent3a15e2a2006-03-27 01:14:55 -0800318 spin_lock(&sbi->fs_lock);
Ian Kentb5b80172011-01-14 18:46:03 +0000319 ino = autofs4_dentry_ino(root);
320 /* No point expiring a pending mount */
Ian Kentf9398c22011-03-25 01:51:14 +0800321 if (ino->flags & AUTOFS_INF_PENDING)
322 goto out;
Ian Kent3a15e2a2006-03-27 01:14:55 -0800323 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
Ian Kent3a15e2a2006-03-27 01:14:55 -0800324 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700325 init_completion(&ino->expire_complete);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800326 spin_unlock(&sbi->fs_lock);
327 return root;
328 }
Ian Kentf9398c22011-03-25 01:51:14 +0800329out:
Ian Kent3a15e2a2006-03-27 01:14:55 -0800330 spin_unlock(&sbi->fs_lock);
331 dput(root);
332
333 return NULL;
334}
335
NeilBrowna5d1dba2014-10-13 15:52:16 -0700336/* Check if 'dentry' should expire, or return a nearby
337 * dentry that is suitable.
338 * If returned dentry is different from arg dentry,
339 * then a dget() reference was taken, else not.
340 */
341static struct dentry *should_expire(struct dentry *dentry,
342 struct vfsmount *mnt,
343 unsigned long timeout,
344 int how)
345{
346 int do_now = how & AUTOFS_EXP_IMMEDIATE;
347 int exp_leaves = how & AUTOFS_EXP_LEAVES;
348 struct autofs_info *ino = autofs4_dentry_ino(dentry);
349 unsigned int ino_count;
350
351 /* No point expiring a pending mount */
352 if (ino->flags & AUTOFS_INF_PENDING)
353 return NULL;
354
355 /*
356 * Case 1: (i) indirect mount or top level pseudo direct mount
357 * (autofs-4.1).
358 * (ii) indirect mount with offset mount, check the "/"
359 * offset (autofs-5.0+).
360 */
361 if (d_mountpoint(dentry)) {
362 DPRINTK("checking mountpoint %p %.*s",
363 dentry, (int)dentry->d_name.len, dentry->d_name.name);
364
365 /* Can we umount this guy */
366 if (autofs4_mount_busy(mnt, dentry))
367 return NULL;
368
369 /* Can we expire this guy */
370 if (autofs4_can_expire(dentry, timeout, do_now))
371 return dentry;
372 return NULL;
373 }
374
375 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)) {
376 DPRINTK("checking symlink %p %.*s",
377 dentry, (int)dentry->d_name.len, dentry->d_name.name);
378 /*
379 * A symlink can't be "busy" in the usual sense so
380 * just check last used for expire timeout.
381 */
382 if (autofs4_can_expire(dentry, timeout, do_now))
383 return dentry;
384 return NULL;
385 }
386
387 if (simple_empty(dentry))
388 return NULL;
389
390 /* Case 2: tree mount, expire iff entire tree is not busy */
391 if (!exp_leaves) {
392 /* Path walk currently on this dentry? */
393 ino_count = atomic_read(&ino->count) + 1;
394 if (d_count(dentry) > ino_count)
395 return NULL;
396
397 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now))
398 return dentry;
399 /*
400 * Case 3: pseudo direct mount, expire individual leaves
401 * (autofs-4.1).
402 */
403 } else {
404 /* Path walk currently on this dentry? */
405 struct dentry *expired;
406 ino_count = atomic_read(&ino->count) + 1;
407 if (d_count(dentry) > ino_count)
408 return NULL;
409
410 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
411 if (expired) {
412 if (expired == dentry)
413 dput(dentry);
414 return expired;
415 }
416 }
417 return NULL;
418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
420 * Find an eligible tree to time-out
421 * A tree is eligible if :-
422 * - it is unused by any user process
423 * - it has been unused for exp_timeout time
424 */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700425struct dentry *autofs4_expire_indirect(struct super_block *sb,
426 struct vfsmount *mnt,
427 struct autofs_sb_info *sbi,
428 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 unsigned long timeout;
431 struct dentry *root = sb->s_root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100432 struct dentry *dentry;
NeilBrowna5d1dba2014-10-13 15:52:16 -0700433 struct dentry *expired;
Ian Kent97e74492008-07-23 21:30:26 -0700434 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Ian Kentc0ba7e52006-09-25 16:24:16 -0700436 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return NULL;
438
439 now = jiffies;
440 timeout = sbi->exp_timeout;
441
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100442 dentry = NULL;
Ian Kentd4a85e32011-03-25 01:51:20 +0800443 while ((dentry = get_next_positive_subdir(dentry, root))) {
Ian Kent97e74492008-07-23 21:30:26 -0700444 spin_lock(&sbi->fs_lock);
NeilBrowna5d1dba2014-10-13 15:52:16 -0700445 expired = should_expire(dentry, mnt, timeout, how);
446 if (expired) {
447 if (expired != dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 dput(dentry);
NeilBrowna5d1dba2014-10-13 15:52:16 -0700449 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Ian Kent97e74492008-07-23 21:30:26 -0700451 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return NULL;
Ian Kentafec5702008-05-01 04:35:06 -0700454
455found:
456 DPRINTK("returning %p %.*s",
457 expired, (int)expired->d_name.len, expired->d_name.name);
Ian Kent97e74492008-07-23 21:30:26 -0700458 ino = autofs4_dentry_ino(expired);
459 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700460 init_completion(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700461 spin_unlock(&sbi->fs_lock);
Ian Kente7854722011-03-25 01:51:31 +0800462 spin_lock(&sbi->lookup_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100463 spin_lock(&expired->d_parent->d_lock);
464 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
Ian Kentafec5702008-05-01 04:35:06 -0700465 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100466 spin_unlock(&expired->d_lock);
467 spin_unlock(&expired->d_parent->d_lock);
Ian Kente7854722011-03-25 01:51:31 +0800468 spin_unlock(&sbi->lookup_lock);
Ian Kentafec5702008-05-01 04:35:06 -0700469 return expired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
NeilBrown23bfc2a2014-10-13 15:52:14 -0700472int autofs4_expire_wait(struct dentry *dentry, int rcu_walk)
Ian Kent06a35982008-07-23 21:30:28 -0700473{
474 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
475 struct autofs_info *ino = autofs4_dentry_ino(dentry);
476 int status;
477
478 /* Block on any pending expire */
479 spin_lock(&sbi->fs_lock);
480 if (ino->flags & AUTOFS_INF_EXPIRING) {
481 spin_unlock(&sbi->fs_lock);
NeilBrown23bfc2a2014-10-13 15:52:14 -0700482 if (rcu_walk)
483 return -ECHILD;
Ian Kent06a35982008-07-23 21:30:28 -0700484
485 DPRINTK("waiting for expire %p name=%.*s",
486 dentry, dentry->d_name.len, dentry->d_name.name);
487
488 status = autofs4_wait(sbi, dentry, NFY_NONE);
489 wait_for_completion(&ino->expire_complete);
490
491 DPRINTK("expire done status=%d", status);
492
Al Viro4b1ae272010-03-03 12:58:31 -0500493 if (d_unhashed(dentry))
Ian Kent06a35982008-07-23 21:30:28 -0700494 return -EAGAIN;
495
496 return status;
497 }
498 spin_unlock(&sbi->fs_lock);
499
500 return 0;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/* Perform an expiry operation */
504int autofs4_expire_run(struct super_block *sb,
505 struct vfsmount *mnt,
506 struct autofs_sb_info *sbi,
507 struct autofs_packet_expire __user *pkt_p)
508{
509 struct autofs_packet_expire pkt;
Ian Kent97e74492008-07-23 21:30:26 -0700510 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct dentry *dentry;
Ian Kent97e74492008-07-23 21:30:26 -0700512 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 memset(&pkt,0,sizeof pkt);
515
516 pkt.hdr.proto_version = sbi->version;
517 pkt.hdr.type = autofs_ptype_expire;
518
Ian Kent3a15e2a2006-03-27 01:14:55 -0800519 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EAGAIN;
521
522 pkt.len = dentry->d_name.len;
523 memcpy(pkt.name, dentry->d_name.name, pkt.len);
524 pkt.name[pkt.len] = '\0';
525 dput(dentry);
526
527 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
Ian Kent97e74492008-07-23 21:30:26 -0700528 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Ian Kent97e74492008-07-23 21:30:26 -0700530 spin_lock(&sbi->fs_lock);
531 ino = autofs4_dentry_ino(dentry);
NeilBrown6ece08e2014-10-13 15:52:18 -0700532 /* avoid rapid-fire expire attempts if expiry fails */
533 ino->last_used = now;
Ian Kent97e74492008-07-23 21:30:26 -0700534 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700535 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700536 spin_unlock(&sbi->fs_lock);
537
538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Ian Kent56fcef72009-03-31 15:24:43 -0700541int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
542 struct autofs_sb_info *sbi, int when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct dentry *dentry;
545 int ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ian Kenta92daf62009-01-06 14:42:08 -0800547 if (autofs_type_trigger(sbi->type))
Ian Kent56fcef72009-03-31 15:24:43 -0700548 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800549 else
Ian Kent56fcef72009-03-31 15:24:43 -0700550 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800551
552 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800553 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* This is synchronous because it makes the daemon a
556 little easier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700558
Ian Kent97e74492008-07-23 21:30:26 -0700559 spin_lock(&sbi->fs_lock);
NeilBrown6ece08e2014-10-13 15:52:18 -0700560 /* avoid rapid-fire expire attempts if expiry fails */
561 ino->last_used = now;
Ian Kent1f5f2c32006-03-27 01:14:44 -0800562 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700563 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700564 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 dput(dentry);
566 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return ret;
569}
570
Ian Kent56fcef72009-03-31 15:24:43 -0700571/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
572 more to be done */
573int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
574 struct autofs_sb_info *sbi, int __user *arg)
575{
576 int do_now = 0;
577
578 if (arg && get_user(do_now, arg))
579 return -EFAULT;
580
581 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
582}
583