Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- 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 Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 7 | * Copyright 2001-2006 Ian Kent <raven@themaw.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 | |
| 17 | static unsigned long now; |
| 18 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 19 | /* Check if a dentry can be expired */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static 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 Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 30 | if (ino->flags & AUTOFS_INF_PENDING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | return 0; |
| 32 | |
| 33 | if (!do_now) { |
| 34 | /* Too young to die */ |
Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 35 | if (!timeout || time_after(ino->last_used + timeout, now)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return 1; |
| 45 | } |
| 46 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 47 | /* Check a mount point for busyness */ |
| 48 | static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 50 | struct dentry *top = dentry; |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 51 | struct path path = {.mnt = mnt, .dentry = dentry}; |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 52 | int status = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | DPRINTK("dentry %p %.*s", |
| 55 | dentry, (int)dentry->d_name.len, dentry->d_name.name); |
| 56 | |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 57 | path_get(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 59 | if (!follow_down_one(&path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | goto done; |
| 61 | |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 62 | if (is_autofs4_dentry(path.dentry)) { |
| 63 | struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb); |
Ian Kent | bc9c406 | 2008-11-06 12:53:22 -0800 | [diff] [blame] | 64 | |
| 65 | /* This is an autofs submount, we can't expire it */ |
Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 66 | if (autofs_type_indirect(sbi->type)) |
Ian Kent | bc9c406 | 2008-11-06 12:53:22 -0800 | [diff] [blame] | 67 | 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 Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 73 | if (!d_mountpoint(path.dentry)) { |
Ian Kent | a8985f3 | 2009-04-30 15:08:09 -0700 | [diff] [blame] | 74 | status = 0; |
Ian Kent | bc9c406 | 2008-11-06 12:53:22 -0800 | [diff] [blame] | 75 | goto done; |
Ian Kent | a8985f3 | 2009-04-30 15:08:09 -0700 | [diff] [blame] | 76 | } |
Ian Kent | bc9c406 | 2008-11-06 12:53:22 -0800 | [diff] [blame] | 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 79 | /* Update the expiry counter if fs is busy */ |
Ian Kent | 37d0892 | 2009-09-01 11:26:22 +0800 | [diff] [blame] | 80 | if (!may_umount_tree(path.mnt)) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 81 | struct autofs_info *ino = autofs4_dentry_ino(top); |
| 82 | ino->last_used = jiffies; |
| 83 | goto done; |
| 84 | } |
| 85 | |
| 86 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | done: |
| 88 | DPRINTK("returning = %d", status); |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 89 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return status; |
| 91 | } |
| 92 | |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 93 | /* |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 94 | * Calculate and dget next entry in top down tree traversal. |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 95 | */ |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 96 | static struct dentry *get_next_positive_dentry(struct dentry *prev, |
| 97 | struct dentry *root) |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 98 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 99 | struct list_head *next; |
| 100 | struct dentry *p, *ret; |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 101 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 102 | if (prev == NULL) |
| 103 | return dget(prev); |
| 104 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 105 | spin_lock(&autofs4_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 106 | relock: |
| 107 | p = prev; |
| 108 | spin_lock(&p->d_lock); |
| 109 | again: |
| 110 | next = p->d_subdirs.next; |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 111 | if (next == &p->d_subdirs) { |
| 112 | while (1) { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 113 | struct dentry *parent; |
| 114 | |
| 115 | if (p == root) { |
| 116 | spin_unlock(&p->d_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 117 | spin_unlock(&autofs4_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 118 | dput(prev); |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 119 | return NULL; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 120 | } |
| 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 Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 129 | next = p->d_u.d_child.next; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 130 | p = parent; |
| 131 | if (next != &parent->d_subdirs) |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 132 | break; |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 135 | 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 Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 147 | spin_unlock(&autofs4_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 148 | |
| 149 | dput(prev); |
| 150 | |
| 151 | return ret; |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 154 | /* |
| 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 | */ |
| 160 | static 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 Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 168 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* Check a directory tree of mount points for busyness |
| 184 | * The tree is not busy iff no mountpoints are busy |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | */ |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 186 | static int autofs4_tree_busy(struct vfsmount *mnt, |
| 187 | struct dentry *top, |
| 188 | unsigned long timeout, |
| 189 | int do_now) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 191 | struct autofs_info *top_ino = autofs4_dentry_ino(top); |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 192 | struct dentry *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 194 | DPRINTK("top %p %.*s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | top, (int)top->d_name.len, top->d_name.name); |
| 196 | |
| 197 | /* Negative dentry - give up */ |
| 198 | if (!simple_positive(top)) |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 199 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 201 | p = NULL; |
| 202 | while ((p = get_next_positive_dentry(p, top))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | DPRINTK("dentry %p %.*s", |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 204 | p, (int) p->d_name.len, p->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 206 | /* |
| 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 Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 210 | * If the fs is busy update the expiry counter. |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 211 | */ |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 212 | if (d_mountpoint(p)) { |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 213 | if (autofs4_mount_busy(mnt, p)) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 214 | top_ino->last_used = jiffies; |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 215 | dput(p); |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 216 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 218 | } else { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 219 | struct autofs_info *ino = autofs4_dentry_ino(p); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 220 | unsigned int ino_count = atomic_read(&ino->count); |
| 221 | |
Ian Kent | f9022f6 | 2006-06-25 05:48:47 -0700 | [diff] [blame] | 222 | /* |
| 223 | * Clean stale dentries below that have not been |
| 224 | * invalidated after a mount fail during lookup |
| 225 | */ |
| 226 | d_invalidate(p); |
| 227 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 228 | /* allow for dget above and top is already dgot */ |
| 229 | if (p == top) |
| 230 | ino_count += 2; |
| 231 | else |
| 232 | ino_count++; |
| 233 | |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 234 | if (p->d_count > ino_count) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 235 | top_ino->last_used = jiffies; |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 236 | dput(p); |
| 237 | return 1; |
| 238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 241 | |
| 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 Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 246 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static struct dentry *autofs4_check_leaves(struct vfsmount *mnt, |
| 250 | struct dentry *parent, |
| 251 | unsigned long timeout, |
| 252 | int do_now) |
| 253 | { |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 254 | struct dentry *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | DPRINTK("parent %p %.*s", |
| 257 | parent, (int)parent->d_name.len, parent->d_name.name); |
| 258 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 259 | p = NULL; |
| 260 | while ((p = get_next_positive_dentry(p, parent))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | DPRINTK("dentry %p %.*s", |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 262 | p, (int) p->d_name.len, p->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 264 | if (d_mountpoint(p)) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 265 | /* Can we umount this guy */ |
| 266 | if (autofs4_mount_busy(mnt, p)) |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 267 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 269 | /* Can we expire this guy */ |
| 270 | if (autofs4_can_expire(p, timeout, do_now)) |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 271 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return NULL; |
| 275 | } |
| 276 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 277 | /* Check if we can expire a direct mount (possibly a tree) */ |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 278 | struct dentry *autofs4_expire_direct(struct super_block *sb, |
| 279 | struct vfsmount *mnt, |
| 280 | struct autofs_sb_info *sbi, |
| 281 | int how) |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 282 | { |
| 283 | unsigned long timeout; |
| 284 | struct dentry *root = dget(sb->s_root); |
| 285 | int do_now = how & AUTOFS_EXP_IMMEDIATE; |
| 286 | |
Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 287 | if (!root) |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 288 | return NULL; |
| 289 | |
| 290 | now = jiffies; |
| 291 | timeout = sbi->exp_timeout; |
| 292 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 293 | 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 Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 296 | if (d_mountpoint(root)) { |
| 297 | ino->flags |= AUTOFS_INF_MOUNTPOINT; |
Nick Piggin | 5f57cbc | 2011-01-07 17:49:54 +1100 | [diff] [blame] | 298 | spin_lock(&root->d_lock); |
| 299 | root->d_flags &= ~DCACHE_MOUNTED; |
| 300 | spin_unlock(&root->d_lock); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 301 | } |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 302 | ino->flags |= AUTOFS_INF_EXPIRING; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame^] | 303 | managed_dentry_set_automount(root); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 304 | init_completion(&ino->expire_complete); |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 305 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | /* |
| 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 Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 320 | struct dentry *autofs4_expire_indirect(struct super_block *sb, |
| 321 | struct vfsmount *mnt, |
| 322 | struct autofs_sb_info *sbi, |
| 323 | int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | unsigned long timeout; |
| 326 | struct dentry *root = sb->s_root; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 327 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | struct dentry *expired = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | int do_now = how & AUTOFS_EXP_IMMEDIATE; |
| 330 | int exp_leaves = how & AUTOFS_EXP_LEAVES; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 331 | struct autofs_info *ino; |
| 332 | unsigned int ino_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 334 | if (!root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return NULL; |
| 336 | |
| 337 | now = jiffies; |
| 338 | timeout = sbi->exp_timeout; |
| 339 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 340 | dentry = NULL; |
| 341 | while ((dentry = get_next_positive_dentry(dentry, root))) { |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 342 | spin_lock(&sbi->fs_lock); |
| 343 | ino = autofs4_dentry_ino(dentry); |
| 344 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 345 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (d_mountpoint(dentry)) { |
| 352 | DPRINTK("checking mountpoint %p %.*s", |
| 353 | dentry, (int)dentry->d_name.len, dentry->d_name.name); |
| 354 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 355 | /* Path walk currently on this dentry? */ |
| 356 | ino_count = atomic_read(&ino->count) + 2; |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 357 | if (dentry->d_count > ino_count) |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 358 | goto next; |
| 359 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 360 | /* Can we umount this guy */ |
| 361 | if (autofs4_mount_busy(mnt, dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | goto next; |
| 363 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 364 | /* Can we expire this guy */ |
| 365 | if (autofs4_can_expire(dentry, timeout, do_now)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | expired = dentry; |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 367 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | goto next; |
| 370 | } |
| 371 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 372 | if (simple_empty(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | goto next; |
| 374 | |
| 375 | /* Case 2: tree mount, expire iff entire tree is not busy */ |
| 376 | if (!exp_leaves) { |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 377 | /* Path walk currently on this dentry? */ |
| 378 | ino_count = atomic_read(&ino->count) + 1; |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 379 | if (dentry->d_count > ino_count) |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 380 | goto next; |
Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 381 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 382 | if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) { |
Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 383 | expired = dentry; |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 384 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 386 | /* |
| 387 | * Case 3: pseudo direct mount, expire individual leaves |
| 388 | * (autofs-4.1). |
| 389 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } else { |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 391 | /* Path walk currently on this dentry? */ |
| 392 | ino_count = atomic_read(&ino->count) + 1; |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 393 | if (dentry->d_count > ino_count) |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 394 | goto next; |
| 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | expired = autofs4_check_leaves(mnt, dentry, timeout, do_now); |
| 397 | if (expired) { |
| 398 | dput(dentry); |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 399 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | next: |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 403 | spin_unlock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return NULL; |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 406 | |
| 407 | found: |
| 408 | DPRINTK("returning %p %.*s", |
| 409 | expired, (int)expired->d_name.len, expired->d_name.name); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 410 | ino = autofs4_dentry_ino(expired); |
| 411 | ino->flags |= AUTOFS_INF_EXPIRING; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame^] | 412 | managed_dentry_set_automount(expired); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 413 | init_completion(&ino->expire_complete); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 414 | spin_unlock(&sbi->fs_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 415 | spin_lock(&autofs4_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 416 | spin_lock(&expired->d_parent->d_lock); |
| 417 | spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED); |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 418 | list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 419 | spin_unlock(&expired->d_lock); |
| 420 | spin_unlock(&expired->d_parent->d_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 421 | spin_unlock(&autofs4_lock); |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 422 | return expired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 425 | int 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 Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 444 | if (d_unhashed(dentry)) |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 445 | return -EAGAIN; |
| 446 | |
| 447 | return status; |
| 448 | } |
| 449 | spin_unlock(&sbi->fs_lock); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /* Perform an expiry operation */ |
| 455 | int 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 Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 461 | struct autofs_info *ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | struct dentry *dentry; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 463 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | memset(&pkt,0,sizeof pkt); |
| 466 | |
| 467 | pkt.hdr.proto_version = sbi->version; |
| 468 | pkt.hdr.type = autofs_ptype_expire; |
| 469 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 470 | if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | 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 Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 479 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 481 | spin_lock(&sbi->fs_lock); |
| 482 | ino = autofs4_dentry_ino(dentry); |
| 483 | ino->flags &= ~AUTOFS_INF_EXPIRING; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame^] | 484 | if (!d_unhashed(dentry)) |
| 485 | managed_dentry_clear_automount(dentry); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 486 | complete_all(&ino->expire_complete); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 487 | spin_unlock(&sbi->fs_lock); |
| 488 | |
| 489 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 492 | int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt, |
| 493 | struct autofs_sb_info *sbi, int when) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct dentry *dentry; |
| 496 | int ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 498 | if (autofs_type_trigger(sbi->type)) |
Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 499 | dentry = autofs4_expire_direct(sb, mnt, sbi, when); |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 500 | else |
Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 501 | dentry = autofs4_expire_indirect(sb, mnt, sbi, when); |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 502 | |
| 503 | if (dentry) { |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 504 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | /* This is synchronous because it makes the daemon a |
| 507 | little easier */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | ret = autofs4_wait(sbi, dentry, NFY_EXPIRE); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 509 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 510 | spin_lock(&sbi->fs_lock); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 511 | if (ino->flags & AUTOFS_INF_MOUNTPOINT) { |
Nick Piggin | 5f57cbc | 2011-01-07 17:49:54 +1100 | [diff] [blame] | 512 | 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 Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 520 | ino->flags &= ~AUTOFS_INF_MOUNTPOINT; |
| 521 | } |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 522 | ino->flags &= ~AUTOFS_INF_EXPIRING; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame^] | 523 | if (ret) |
| 524 | managed_dentry_clear_automount(dentry); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 525 | complete_all(&ino->expire_complete); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 526 | spin_unlock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | dput(dentry); |
| 528 | } |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return ret; |
| 531 | } |
| 532 | |
Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 533 | /* Call repeatedly until it returns -EAGAIN, meaning there's nothing |
| 534 | more to be done */ |
| 535 | int 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 | |