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 */ |
| 30 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING) |
| 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; |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 51 | int status = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | DPRINTK("dentry %p %.*s", |
| 54 | dentry, (int)dentry->d_name.len, dentry->d_name.name); |
| 55 | |
| 56 | mntget(mnt); |
| 57 | dget(dentry); |
| 58 | |
Ian Kent | 9b1e3af | 2005-06-21 17:16:38 -0700 | [diff] [blame] | 59 | if (!autofs4_follow_mount(&mnt, &dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | goto done; |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* This is an autofs submount, we can't expire it */ |
| 63 | if (is_autofs4_dentry(dentry)) |
| 64 | goto done; |
| 65 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 66 | /* Update the expiry counter if fs is busy */ |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 67 | if (!may_umount_tree(mnt)) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 68 | struct autofs_info *ino = autofs4_dentry_ino(top); |
| 69 | ino->last_used = jiffies; |
| 70 | goto done; |
| 71 | } |
| 72 | |
| 73 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | done: |
| 75 | DPRINTK("returning = %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | dput(dentry); |
Jan Blunck | 868eb7a | 2008-05-01 04:35:10 -0700 | [diff] [blame] | 77 | mntput(mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return status; |
| 79 | } |
| 80 | |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Calculate next entry in top down tree traversal. |
| 83 | * From next_mnt in namespace.c - elegant. |
| 84 | */ |
| 85 | static struct dentry *next_dentry(struct dentry *p, struct dentry *root) |
| 86 | { |
| 87 | struct list_head *next = p->d_subdirs.next; |
| 88 | |
| 89 | if (next == &p->d_subdirs) { |
| 90 | while (1) { |
| 91 | if (p == root) |
| 92 | return NULL; |
| 93 | next = p->d_u.d_child.next; |
| 94 | if (next != &p->d_parent->d_subdirs) |
| 95 | break; |
| 96 | p = p->d_parent; |
| 97 | } |
| 98 | } |
| 99 | return list_entry(next, struct dentry, d_u.d_child); |
| 100 | } |
| 101 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 102 | /* |
| 103 | * Check a direct mount point for busyness. |
| 104 | * Direct mounts have similar expiry semantics to tree mounts. |
| 105 | * The tree is not busy iff no mountpoints are busy and there are no |
| 106 | * autofs submounts. |
| 107 | */ |
| 108 | static int autofs4_direct_busy(struct vfsmount *mnt, |
| 109 | struct dentry *top, |
| 110 | unsigned long timeout, |
| 111 | int do_now) |
| 112 | { |
| 113 | DPRINTK("top %p %.*s", |
| 114 | top, (int) top->d_name.len, top->d_name.name); |
| 115 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 116 | /* If it's busy update the expiry counters */ |
| 117 | if (!may_umount_tree(mnt)) { |
| 118 | struct autofs_info *ino = autofs4_dentry_ino(top); |
| 119 | if (ino) |
| 120 | ino->last_used = jiffies; |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | /* Timeout of a direct mount is determined by its top dentry */ |
| 125 | if (!autofs4_can_expire(top, timeout, do_now)) |
| 126 | return 1; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* Check a directory tree of mount points for busyness |
| 132 | * The tree is not busy iff no mountpoints are busy |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | */ |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 134 | static int autofs4_tree_busy(struct vfsmount *mnt, |
| 135 | struct dentry *top, |
| 136 | unsigned long timeout, |
| 137 | int do_now) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 139 | struct autofs_info *top_ino = autofs4_dentry_ino(top); |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 140 | struct dentry *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 142 | DPRINTK("top %p %.*s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | top, (int)top->d_name.len, top->d_name.name); |
| 144 | |
| 145 | /* Negative dentry - give up */ |
| 146 | if (!simple_positive(top)) |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 147 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | spin_lock(&dcache_lock); |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 150 | for (p = top; p; p = next_dentry(p, top)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* Negative dentry - give up */ |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 152 | if (!simple_positive(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | DPRINTK("dentry %p %.*s", |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 156 | p, (int) p->d_name.len, p->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 158 | p = dget(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | spin_unlock(&dcache_lock); |
| 160 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 161 | /* |
| 162 | * Is someone visiting anywhere in the subtree ? |
| 163 | * If there's no mount we need to check the usage |
| 164 | * count for the autofs dentry. |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 165 | * If the fs is busy update the expiry counter. |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 166 | */ |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 167 | if (d_mountpoint(p)) { |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 168 | if (autofs4_mount_busy(mnt, p)) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 169 | top_ino->last_used = jiffies; |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 170 | dput(p); |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 171 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 173 | } else { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 174 | struct autofs_info *ino = autofs4_dentry_ino(p); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 175 | unsigned int ino_count = atomic_read(&ino->count); |
| 176 | |
Ian Kent | f9022f6 | 2006-06-25 05:48:47 -0700 | [diff] [blame] | 177 | /* |
| 178 | * Clean stale dentries below that have not been |
| 179 | * invalidated after a mount fail during lookup |
| 180 | */ |
| 181 | d_invalidate(p); |
| 182 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 183 | /* allow for dget above and top is already dgot */ |
| 184 | if (p == top) |
| 185 | ino_count += 2; |
| 186 | else |
| 187 | ino_count++; |
| 188 | |
| 189 | if (atomic_read(&p->d_count) > ino_count) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 190 | top_ino->last_used = jiffies; |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 191 | dput(p); |
| 192 | return 1; |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 195 | dput(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | spin_lock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | spin_unlock(&dcache_lock); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 199 | |
| 200 | /* Timeout of a tree mount is ultimately determined by its top dentry */ |
| 201 | if (!autofs4_can_expire(top, timeout, do_now)) |
| 202 | return 1; |
| 203 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 204 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static struct dentry *autofs4_check_leaves(struct vfsmount *mnt, |
| 208 | struct dentry *parent, |
| 209 | unsigned long timeout, |
| 210 | int do_now) |
| 211 | { |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 212 | struct dentry *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | DPRINTK("parent %p %.*s", |
| 215 | parent, (int)parent->d_name.len, parent->d_name.name); |
| 216 | |
| 217 | spin_lock(&dcache_lock); |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 218 | for (p = parent; p; p = next_dentry(p, parent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* Negative dentry - give up */ |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 220 | if (!simple_positive(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | DPRINTK("dentry %p %.*s", |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 224 | p, (int) p->d_name.len, p->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 226 | p = dget(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | spin_unlock(&dcache_lock); |
| 228 | |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 229 | if (d_mountpoint(p)) { |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 230 | /* Can we umount this guy */ |
| 231 | if (autofs4_mount_busy(mnt, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | goto cont; |
| 233 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 234 | /* Can we expire this guy */ |
| 235 | if (autofs4_can_expire(p, timeout, do_now)) |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 236 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | cont: |
Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 239 | dput(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | spin_lock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 246 | /* Check if we can expire a direct mount (possibly a tree) */ |
| 247 | static struct dentry *autofs4_expire_direct(struct super_block *sb, |
| 248 | struct vfsmount *mnt, |
| 249 | struct autofs_sb_info *sbi, |
| 250 | int how) |
| 251 | { |
| 252 | unsigned long timeout; |
| 253 | struct dentry *root = dget(sb->s_root); |
| 254 | int do_now = how & AUTOFS_EXP_IMMEDIATE; |
| 255 | |
Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 256 | if (!root) |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 257 | return NULL; |
| 258 | |
| 259 | now = jiffies; |
| 260 | timeout = sbi->exp_timeout; |
| 261 | |
| 262 | /* Lock the tree as we must expire as a whole */ |
| 263 | spin_lock(&sbi->fs_lock); |
| 264 | if (!autofs4_direct_busy(mnt, root, timeout, do_now)) { |
| 265 | struct autofs_info *ino = autofs4_dentry_ino(root); |
| 266 | |
| 267 | /* Set this flag early to catch sys_chdir and the like */ |
| 268 | ino->flags |= AUTOFS_INF_EXPIRING; |
| 269 | spin_unlock(&sbi->fs_lock); |
| 270 | return root; |
| 271 | } |
| 272 | spin_unlock(&sbi->fs_lock); |
| 273 | dput(root); |
| 274 | |
| 275 | return NULL; |
| 276 | } |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* |
| 279 | * Find an eligible tree to time-out |
| 280 | * A tree is eligible if :- |
| 281 | * - it is unused by any user process |
| 282 | * - it has been unused for exp_timeout time |
| 283 | */ |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 284 | static struct dentry *autofs4_expire_indirect(struct super_block *sb, |
| 285 | struct vfsmount *mnt, |
| 286 | struct autofs_sb_info *sbi, |
| 287 | int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | unsigned long timeout; |
| 290 | struct dentry *root = sb->s_root; |
| 291 | struct dentry *expired = NULL; |
| 292 | struct list_head *next; |
| 293 | int do_now = how & AUTOFS_EXP_IMMEDIATE; |
| 294 | int exp_leaves = how & AUTOFS_EXP_LEAVES; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 295 | struct autofs_info *ino; |
| 296 | unsigned int ino_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 298 | if (!root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return NULL; |
| 300 | |
| 301 | now = jiffies; |
| 302 | timeout = sbi->exp_timeout; |
| 303 | |
| 304 | spin_lock(&dcache_lock); |
| 305 | next = root->d_subdirs.next; |
| 306 | |
| 307 | /* On exit from the loop expire is set to a dgot dentry |
| 308 | * to expire or it's NULL */ |
| 309 | while ( next != &root->d_subdirs ) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 310 | struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* Negative dentry - give up */ |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 313 | if (!simple_positive(dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | next = next->next; |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | dentry = dget(dentry); |
| 319 | spin_unlock(&dcache_lock); |
| 320 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 321 | spin_lock(&sbi->fs_lock); |
| 322 | ino = autofs4_dentry_ino(dentry); |
| 323 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 324 | /* |
| 325 | * Case 1: (i) indirect mount or top level pseudo direct mount |
| 326 | * (autofs-4.1). |
| 327 | * (ii) indirect mount with offset mount, check the "/" |
| 328 | * offset (autofs-5.0+). |
| 329 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (d_mountpoint(dentry)) { |
| 331 | DPRINTK("checking mountpoint %p %.*s", |
| 332 | dentry, (int)dentry->d_name.len, dentry->d_name.name); |
| 333 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 334 | /* Path walk currently on this dentry? */ |
| 335 | ino_count = atomic_read(&ino->count) + 2; |
| 336 | if (atomic_read(&dentry->d_count) > ino_count) |
| 337 | goto next; |
| 338 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 339 | /* Can we umount this guy */ |
| 340 | if (autofs4_mount_busy(mnt, dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | goto next; |
| 342 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 343 | /* Can we expire this guy */ |
| 344 | if (autofs4_can_expire(dentry, timeout, do_now)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | expired = dentry; |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 346 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | goto next; |
| 349 | } |
| 350 | |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 351 | if (simple_empty(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | goto next; |
| 353 | |
| 354 | /* Case 2: tree mount, expire iff entire tree is not busy */ |
| 355 | if (!exp_leaves) { |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 356 | /* Path walk currently on this dentry? */ |
| 357 | ino_count = atomic_read(&ino->count) + 1; |
| 358 | if (atomic_read(&dentry->d_count) > ino_count) |
| 359 | goto next; |
Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 360 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 361 | if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) { |
Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 362 | expired = dentry; |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 363 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 365 | /* |
| 366 | * Case 3: pseudo direct mount, expire individual leaves |
| 367 | * (autofs-4.1). |
| 368 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } else { |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 370 | /* Path walk currently on this dentry? */ |
| 371 | ino_count = atomic_read(&ino->count) + 1; |
| 372 | if (atomic_read(&dentry->d_count) > ino_count) |
| 373 | goto next; |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | expired = autofs4_check_leaves(mnt, dentry, timeout, do_now); |
| 376 | if (expired) { |
| 377 | dput(dentry); |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 378 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | next: |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 382 | spin_unlock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | dput(dentry); |
| 384 | spin_lock(&dcache_lock); |
| 385 | next = next->next; |
| 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return NULL; |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 389 | |
| 390 | found: |
| 391 | DPRINTK("returning %p %.*s", |
| 392 | expired, (int)expired->d_name.len, expired->d_name.name); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 393 | ino = autofs4_dentry_ino(expired); |
| 394 | ino->flags |= AUTOFS_INF_EXPIRING; |
| 395 | spin_unlock(&sbi->fs_lock); |
Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 396 | spin_lock(&dcache_lock); |
| 397 | list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child); |
| 398 | spin_unlock(&dcache_lock); |
| 399 | return expired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* Perform an expiry operation */ |
| 403 | int autofs4_expire_run(struct super_block *sb, |
| 404 | struct vfsmount *mnt, |
| 405 | struct autofs_sb_info *sbi, |
| 406 | struct autofs_packet_expire __user *pkt_p) |
| 407 | { |
| 408 | struct autofs_packet_expire pkt; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 409 | struct autofs_info *ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | struct dentry *dentry; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 411 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | memset(&pkt,0,sizeof pkt); |
| 414 | |
| 415 | pkt.hdr.proto_version = sbi->version; |
| 416 | pkt.hdr.type = autofs_ptype_expire; |
| 417 | |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 418 | if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return -EAGAIN; |
| 420 | |
| 421 | pkt.len = dentry->d_name.len; |
| 422 | memcpy(pkt.name, dentry->d_name.name, pkt.len); |
| 423 | pkt.name[pkt.len] = '\0'; |
| 424 | dput(dentry); |
| 425 | |
| 426 | if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) ) |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 427 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 429 | spin_lock(&sbi->fs_lock); |
| 430 | ino = autofs4_dentry_ino(dentry); |
| 431 | ino->flags &= ~AUTOFS_INF_EXPIRING; |
| 432 | spin_unlock(&sbi->fs_lock); |
| 433 | |
| 434 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* Call repeatedly until it returns -EAGAIN, meaning there's nothing |
| 438 | more to be done */ |
| 439 | int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt, |
| 440 | struct autofs_sb_info *sbi, int __user *arg) |
| 441 | { |
| 442 | struct dentry *dentry; |
| 443 | int ret = -EAGAIN; |
| 444 | int do_now = 0; |
| 445 | |
| 446 | if (arg && get_user(do_now, arg)) |
| 447 | return -EFAULT; |
| 448 | |
Ian Kent | 44d53eb | 2006-03-27 01:14:56 -0800 | [diff] [blame] | 449 | if (sbi->type & AUTOFS_TYPE_DIRECT) |
Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 450 | dentry = autofs4_expire_direct(sb, mnt, sbi, do_now); |
| 451 | else |
| 452 | dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now); |
| 453 | |
| 454 | if (dentry) { |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 455 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | /* This is synchronous because it makes the daemon a |
| 458 | little easier */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | ret = autofs4_wait(sbi, dentry, NFY_EXPIRE); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 460 | spin_lock(&sbi->fs_lock); |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 461 | ino->flags &= ~AUTOFS_INF_EXPIRING; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame^] | 462 | spin_unlock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | dput(dentry); |
| 464 | } |
Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return ret; |
| 467 | } |
| 468 | |