blob: 74bc9aa6df310d4ccdf0d312dd43976c02f93d1e [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
Al Viro9393bd02009-04-18 13:58:15 -040059 if (!follow_down(&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/*
94 * Calculate next entry in top down tree traversal.
95 * From next_mnt in namespace.c - elegant.
96 */
97static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
98{
99 struct list_head *next = p->d_subdirs.next;
100
101 if (next == &p->d_subdirs) {
102 while (1) {
103 if (p == root)
104 return NULL;
105 next = p->d_u.d_child.next;
106 if (next != &p->d_parent->d_subdirs)
107 break;
108 p = p->d_parent;
109 }
110 }
111 return list_entry(next, struct dentry, d_u.d_child);
112}
113
Ian Kent3a15e2a2006-03-27 01:14:55 -0800114/*
115 * Check a direct mount point for busyness.
116 * Direct mounts have similar expiry semantics to tree mounts.
117 * The tree is not busy iff no mountpoints are busy and there are no
118 * autofs submounts.
119 */
120static int autofs4_direct_busy(struct vfsmount *mnt,
121 struct dentry *top,
122 unsigned long timeout,
123 int do_now)
124{
125 DPRINTK("top %p %.*s",
126 top, (int) top->d_name.len, top->d_name.name);
127
Ian Kent3a15e2a2006-03-27 01:14:55 -0800128 /* If it's busy update the expiry counters */
129 if (!may_umount_tree(mnt)) {
130 struct autofs_info *ino = autofs4_dentry_ino(top);
131 if (ino)
132 ino->last_used = jiffies;
133 return 1;
134 }
135
136 /* Timeout of a direct mount is determined by its top dentry */
137 if (!autofs4_can_expire(top, timeout, do_now))
138 return 1;
139
140 return 0;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* Check a directory tree of mount points for busyness
144 * The tree is not busy iff no mountpoints are busy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800146static int autofs4_tree_busy(struct vfsmount *mnt,
147 struct dentry *top,
148 unsigned long timeout,
149 int do_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Ian Kente0a7aae2006-03-27 01:14:47 -0800151 struct autofs_info *top_ino = autofs4_dentry_ino(top);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800152 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ian Kent1f5f2c32006-03-27 01:14:44 -0800154 DPRINTK("top %p %.*s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 top, (int)top->d_name.len, top->d_name.name);
156
157 /* Negative dentry - give up */
158 if (!simple_positive(top))
Ian Kent1f5f2c32006-03-27 01:14:44 -0800159 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_lock(&dcache_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800162 for (p = top; p; p = next_dentry(p, top)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* Negative dentry - give up */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800164 if (!simple_positive(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800168 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Ian Kent1ce12ba2006-03-27 01:14:45 -0800170 p = dget(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_unlock(&dcache_lock);
172
Ian Kent1aff3c82006-03-27 01:14:46 -0800173 /*
174 * Is someone visiting anywhere in the subtree ?
175 * If there's no mount we need to check the usage
176 * count for the autofs dentry.
Ian Kente0a7aae2006-03-27 01:14:47 -0800177 * If the fs is busy update the expiry counter.
Ian Kent1aff3c82006-03-27 01:14:46 -0800178 */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800179 if (d_mountpoint(p)) {
Ian Kent1ce12ba2006-03-27 01:14:45 -0800180 if (autofs4_mount_busy(mnt, p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800181 top_ino->last_used = jiffies;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800182 dput(p);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800183 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800185 } else {
Ian Kente0a7aae2006-03-27 01:14:47 -0800186 struct autofs_info *ino = autofs4_dentry_ino(p);
Ian Kent1aff3c82006-03-27 01:14:46 -0800187 unsigned int ino_count = atomic_read(&ino->count);
188
Ian Kentf9022f62006-06-25 05:48:47 -0700189 /*
190 * Clean stale dentries below that have not been
191 * invalidated after a mount fail during lookup
192 */
193 d_invalidate(p);
194
Ian Kent1aff3c82006-03-27 01:14:46 -0800195 /* allow for dget above and top is already dgot */
196 if (p == top)
197 ino_count += 2;
198 else
199 ino_count++;
200
201 if (atomic_read(&p->d_count) > ino_count) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800202 top_ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -0800203 dput(p);
204 return 1;
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Ian Kent1ce12ba2006-03-27 01:14:45 -0800207 dput(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 spin_unlock(&dcache_lock);
Ian Kent1aff3c82006-03-27 01:14:46 -0800211
212 /* Timeout of a tree mount is ultimately determined by its top dentry */
213 if (!autofs4_can_expire(top, timeout, do_now))
214 return 1;
215
Ian Kent1f5f2c32006-03-27 01:14:44 -0800216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
220 struct dentry *parent,
221 unsigned long timeout,
222 int do_now)
223{
Ian Kent1ce12ba2006-03-27 01:14:45 -0800224 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 DPRINTK("parent %p %.*s",
227 parent, (int)parent->d_name.len, parent->d_name.name);
228
229 spin_lock(&dcache_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800230 for (p = parent; p; p = next_dentry(p, parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Negative dentry - give up */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800232 if (!simple_positive(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800236 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Ian Kent1ce12ba2006-03-27 01:14:45 -0800238 p = dget(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 spin_unlock(&dcache_lock);
240
Ian Kent1ce12ba2006-03-27 01:14:45 -0800241 if (d_mountpoint(p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800242 /* Can we umount this guy */
243 if (autofs4_mount_busy(mnt, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto cont;
245
Ian Kente0a7aae2006-03-27 01:14:47 -0800246 /* Can we expire this guy */
247 if (autofs4_can_expire(p, timeout, do_now))
Ian Kent1ce12ba2006-03-27 01:14:45 -0800248 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250cont:
Ian Kent1ce12ba2006-03-27 01:14:45 -0800251 dput(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return NULL;
256}
257
Ian Kent3a15e2a2006-03-27 01:14:55 -0800258/* Check if we can expire a direct mount (possibly a tree) */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700259struct dentry *autofs4_expire_direct(struct super_block *sb,
260 struct vfsmount *mnt,
261 struct autofs_sb_info *sbi,
262 int how)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800263{
264 unsigned long timeout;
265 struct dentry *root = dget(sb->s_root);
266 int do_now = how & AUTOFS_EXP_IMMEDIATE;
267
Ian Kentc0ba7e52006-09-25 16:24:16 -0700268 if (!root)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800269 return NULL;
270
271 now = jiffies;
272 timeout = sbi->exp_timeout;
273
Ian Kent3a15e2a2006-03-27 01:14:55 -0800274 spin_lock(&sbi->fs_lock);
275 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
276 struct autofs_info *ino = autofs4_dentry_ino(root);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700277 if (d_mountpoint(root)) {
278 ino->flags |= AUTOFS_INF_MOUNTPOINT;
279 root->d_mounted--;
280 }
Ian Kent3a15e2a2006-03-27 01:14:55 -0800281 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent213614d2009-12-15 16:45:51 -0800282 autofs4_add_expiring(root);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700283 init_completion(&ino->expire_complete);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800284 spin_unlock(&sbi->fs_lock);
285 return root;
286 }
287 spin_unlock(&sbi->fs_lock);
288 dput(root);
289
290 return NULL;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * Find an eligible tree to time-out
295 * A tree is eligible if :-
296 * - it is unused by any user process
297 * - it has been unused for exp_timeout time
298 */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700299struct dentry *autofs4_expire_indirect(struct super_block *sb,
300 struct vfsmount *mnt,
301 struct autofs_sb_info *sbi,
302 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 unsigned long timeout;
305 struct dentry *root = sb->s_root;
306 struct dentry *expired = NULL;
307 struct list_head *next;
308 int do_now = how & AUTOFS_EXP_IMMEDIATE;
309 int exp_leaves = how & AUTOFS_EXP_LEAVES;
Ian Kent97e74492008-07-23 21:30:26 -0700310 struct autofs_info *ino;
311 unsigned int ino_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Ian Kentc0ba7e52006-09-25 16:24:16 -0700313 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return NULL;
315
316 now = jiffies;
317 timeout = sbi->exp_timeout;
318
319 spin_lock(&dcache_lock);
320 next = root->d_subdirs.next;
321
322 /* On exit from the loop expire is set to a dgot dentry
323 * to expire or it's NULL */
324 while ( next != &root->d_subdirs ) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800325 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Negative dentry - give up */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800328 if (!simple_positive(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 next = next->next;
330 continue;
331 }
332
333 dentry = dget(dentry);
334 spin_unlock(&dcache_lock);
335
Ian Kent97e74492008-07-23 21:30:26 -0700336 spin_lock(&sbi->fs_lock);
337 ino = autofs4_dentry_ino(dentry);
338
Ian Kent3a15e2a2006-03-27 01:14:55 -0800339 /*
340 * Case 1: (i) indirect mount or top level pseudo direct mount
341 * (autofs-4.1).
342 * (ii) indirect mount with offset mount, check the "/"
343 * offset (autofs-5.0+).
344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (d_mountpoint(dentry)) {
346 DPRINTK("checking mountpoint %p %.*s",
347 dentry, (int)dentry->d_name.len, dentry->d_name.name);
348
Ian Kent97e74492008-07-23 21:30:26 -0700349 /* Path walk currently on this dentry? */
350 ino_count = atomic_read(&ino->count) + 2;
351 if (atomic_read(&dentry->d_count) > ino_count)
352 goto next;
353
Ian Kente0a7aae2006-03-27 01:14:47 -0800354 /* Can we umount this guy */
355 if (autofs4_mount_busy(mnt, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto next;
357
Ian Kente0a7aae2006-03-27 01:14:47 -0800358 /* Can we expire this guy */
359 if (autofs4_can_expire(dentry, timeout, do_now)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700361 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 goto next;
364 }
365
Ian Kent1f5f2c32006-03-27 01:14:44 -0800366 if (simple_empty(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto next;
368
369 /* Case 2: tree mount, expire iff entire tree is not busy */
370 if (!exp_leaves) {
Ian Kent97e74492008-07-23 21:30:26 -0700371 /* Path walk currently on this dentry? */
372 ino_count = atomic_read(&ino->count) + 1;
373 if (atomic_read(&dentry->d_count) > ino_count)
374 goto next;
Ian Kent3a9720c2005-05-01 08:59:17 -0700375
Ian Kent97e74492008-07-23 21:30:26 -0700376 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700377 expired = dentry;
Ian Kentafec5702008-05-01 04:35:06 -0700378 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Ian Kent3a15e2a2006-03-27 01:14:55 -0800380 /*
381 * Case 3: pseudo direct mount, expire individual leaves
382 * (autofs-4.1).
383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
Ian Kent97e74492008-07-23 21:30:26 -0700385 /* Path walk currently on this dentry? */
386 ino_count = atomic_read(&ino->count) + 1;
387 if (atomic_read(&dentry->d_count) > ino_count)
388 goto next;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
391 if (expired) {
392 dput(dentry);
Ian Kentafec5702008-05-01 04:35:06 -0700393 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 }
396next:
Ian Kent97e74492008-07-23 21:30:26 -0700397 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dput(dentry);
399 spin_lock(&dcache_lock);
400 next = next->next;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return NULL;
Ian Kentafec5702008-05-01 04:35:06 -0700404
405found:
406 DPRINTK("returning %p %.*s",
407 expired, (int)expired->d_name.len, expired->d_name.name);
Ian Kent97e74492008-07-23 21:30:26 -0700408 ino = autofs4_dentry_ino(expired);
409 ino->flags |= AUTOFS_INF_EXPIRING;
Ian Kent213614d2009-12-15 16:45:51 -0800410 autofs4_add_expiring(expired);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700411 init_completion(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700412 spin_unlock(&sbi->fs_lock);
Ian Kentafec5702008-05-01 04:35:06 -0700413 spin_lock(&dcache_lock);
414 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
415 spin_unlock(&dcache_lock);
416 return expired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Ian Kent06a35982008-07-23 21:30:28 -0700419int autofs4_expire_wait(struct dentry *dentry)
420{
421 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
422 struct autofs_info *ino = autofs4_dentry_ino(dentry);
423 int status;
424
425 /* Block on any pending expire */
426 spin_lock(&sbi->fs_lock);
427 if (ino->flags & AUTOFS_INF_EXPIRING) {
428 spin_unlock(&sbi->fs_lock);
429
430 DPRINTK("waiting for expire %p name=%.*s",
431 dentry, dentry->d_name.len, dentry->d_name.name);
432
433 status = autofs4_wait(sbi, dentry, NFY_NONE);
434 wait_for_completion(&ino->expire_complete);
435
436 DPRINTK("expire done status=%d", status);
437
Ian Kent213614d2009-12-15 16:45:51 -0800438 if (d_unhashed(dentry) && IS_DEADDIR(dentry->d_inode))
Ian Kent06a35982008-07-23 21:30:28 -0700439 return -EAGAIN;
440
441 return status;
442 }
443 spin_unlock(&sbi->fs_lock);
444
445 return 0;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/* Perform an expiry operation */
449int autofs4_expire_run(struct super_block *sb,
450 struct vfsmount *mnt,
451 struct autofs_sb_info *sbi,
452 struct autofs_packet_expire __user *pkt_p)
453{
454 struct autofs_packet_expire pkt;
Ian Kent97e74492008-07-23 21:30:26 -0700455 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct dentry *dentry;
Ian Kent97e74492008-07-23 21:30:26 -0700457 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 memset(&pkt,0,sizeof pkt);
460
461 pkt.hdr.proto_version = sbi->version;
462 pkt.hdr.type = autofs_ptype_expire;
463
Ian Kent3a15e2a2006-03-27 01:14:55 -0800464 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EAGAIN;
466
467 pkt.len = dentry->d_name.len;
468 memcpy(pkt.name, dentry->d_name.name, pkt.len);
469 pkt.name[pkt.len] = '\0';
470 dput(dentry);
471
472 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
Ian Kent97e74492008-07-23 21:30:26 -0700473 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Ian Kent97e74492008-07-23 21:30:26 -0700475 spin_lock(&sbi->fs_lock);
476 ino = autofs4_dentry_ino(dentry);
477 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent213614d2009-12-15 16:45:51 -0800478 autofs4_del_expiring(dentry);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700479 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700480 spin_unlock(&sbi->fs_lock);
481
482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Ian Kent56fcef72009-03-31 15:24:43 -0700485int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
486 struct autofs_sb_info *sbi, int when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct dentry *dentry;
489 int ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Ian Kenta92daf62009-01-06 14:42:08 -0800491 if (autofs_type_trigger(sbi->type))
Ian Kent56fcef72009-03-31 15:24:43 -0700492 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800493 else
Ian Kent56fcef72009-03-31 15:24:43 -0700494 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800495
496 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800497 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* This is synchronous because it makes the daemon a
500 little easier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700502
Ian Kent97e74492008-07-23 21:30:26 -0700503 spin_lock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700504 if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
505 sb->s_root->d_mounted++;
506 ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
507 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800508 ino->flags &= ~AUTOFS_INF_EXPIRING;
Ian Kent213614d2009-12-15 16:45:51 -0800509 autofs4_del_expiring(dentry);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700510 complete_all(&ino->expire_complete);
Ian Kent97e74492008-07-23 21:30:26 -0700511 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dput(dentry);
513 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return ret;
516}
517
Ian Kent56fcef72009-03-31 15:24:43 -0700518/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
519 more to be done */
520int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
521 struct autofs_sb_info *sbi, int __user *arg)
522{
523 int do_now = 0;
524
525 if (arg && get_user(do_now, arg))
526 return -EFAULT;
527
528 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
529}
530