| 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 |  | 
| Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 59 | 	if (!follow_down(&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 | /* | 
 | 94 |  * Calculate next entry in top down tree traversal. | 
 | 95 |  * From next_mnt in namespace.c - elegant. | 
 | 96 |  */ | 
 | 97 | static 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 Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 114 | /* | 
 | 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 |  */ | 
 | 120 | static 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 Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 128 | 	/* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | /* Check a directory tree of mount points for busyness | 
 | 144 |  * The tree is not busy iff no mountpoints are busy | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 |  */ | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 146 | static int autofs4_tree_busy(struct vfsmount *mnt, | 
 | 147 | 	       		     struct dentry *top, | 
 | 148 | 			     unsigned long timeout, | 
 | 149 | 			     int do_now) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 151 | 	struct autofs_info *top_ino = autofs4_dentry_ino(top); | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 152 | 	struct dentry *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 |  | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 154 | 	DPRINTK("top %p %.*s", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | 		top, (int)top->d_name.len, top->d_name.name); | 
 | 156 |  | 
 | 157 | 	/* Negative dentry - give up */ | 
 | 158 | 	if (!simple_positive(top)) | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 159 | 		return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | 	spin_lock(&dcache_lock); | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 162 | 	for (p = top; p; p = next_dentry(p, top)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | 		/* Negative dentry - give up */ | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 164 | 		if (!simple_positive(p)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 			continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 |  | 
 | 167 | 		DPRINTK("dentry %p %.*s", | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 168 | 			p, (int) p->d_name.len, p->d_name.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 |  | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 170 | 		p = dget(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | 		spin_unlock(&dcache_lock); | 
 | 172 |  | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 173 | 		/* | 
 | 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 Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 177 | 		 * If the fs is busy update the expiry counter. | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 178 | 		 */ | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 179 | 		if (d_mountpoint(p)) { | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 180 | 			if (autofs4_mount_busy(mnt, p)) { | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 181 | 				top_ino->last_used = jiffies; | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 182 | 				dput(p); | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 183 | 				return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | 			} | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 185 | 		} else { | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 186 | 			struct autofs_info *ino = autofs4_dentry_ino(p); | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 187 | 			unsigned int ino_count = atomic_read(&ino->count); | 
 | 188 |  | 
| Ian Kent | f9022f6 | 2006-06-25 05:48:47 -0700 | [diff] [blame] | 189 | 			/* | 
 | 190 | 			 * Clean stale dentries below that have not been | 
 | 191 | 			 * invalidated after a mount fail during lookup | 
 | 192 | 			 */ | 
 | 193 | 			d_invalidate(p); | 
 | 194 |  | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 195 | 			/* 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 Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 202 | 				top_ino->last_used = jiffies; | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 203 | 				dput(p); | 
 | 204 | 				return 1; | 
 | 205 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 		} | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 207 | 		dput(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | 		spin_lock(&dcache_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | 	} | 
 | 210 | 	spin_unlock(&dcache_lock); | 
| Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 211 |  | 
 | 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 Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 216 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } | 
 | 218 |  | 
 | 219 | static struct dentry *autofs4_check_leaves(struct vfsmount *mnt, | 
 | 220 | 					   struct dentry *parent, | 
 | 221 | 					   unsigned long timeout, | 
 | 222 | 					   int do_now) | 
 | 223 | { | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 224 | 	struct dentry *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 |  | 
 | 226 | 	DPRINTK("parent %p %.*s", | 
 | 227 | 		parent, (int)parent->d_name.len, parent->d_name.name); | 
 | 228 |  | 
 | 229 | 	spin_lock(&dcache_lock); | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 230 | 	for (p = parent; p; p = next_dentry(p, parent)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | 		/* Negative dentry - give up */ | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 232 | 		if (!simple_positive(p)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | 			continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 |  | 
 | 235 | 		DPRINTK("dentry %p %.*s", | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 236 | 			p, (int) p->d_name.len, p->d_name.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 |  | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 238 | 		p = dget(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | 		spin_unlock(&dcache_lock); | 
 | 240 |  | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 241 | 		if (d_mountpoint(p)) { | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 242 | 			/* Can we umount this guy */ | 
 | 243 | 			if (autofs4_mount_busy(mnt, p)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | 				goto cont; | 
 | 245 |  | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 246 | 			/* Can we expire this guy */ | 
 | 247 | 			if (autofs4_can_expire(p, timeout, do_now)) | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 248 | 				return p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | 		} | 
 | 250 | cont: | 
| Ian Kent | 1ce12ba | 2006-03-27 01:14:45 -0800 | [diff] [blame] | 251 | 		dput(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | 		spin_lock(&dcache_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 	} | 
 | 254 | 	spin_unlock(&dcache_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | 	return NULL; | 
 | 256 | } | 
 | 257 |  | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 258 | /* Check if we can expire a direct mount (possibly a tree) */ | 
| Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 259 | struct dentry *autofs4_expire_direct(struct super_block *sb, | 
 | 260 | 				     struct vfsmount *mnt, | 
 | 261 | 				     struct autofs_sb_info *sbi, | 
 | 262 | 				     int how) | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 263 | { | 
 | 264 | 	unsigned long timeout; | 
 | 265 | 	struct dentry *root = dget(sb->s_root); | 
 | 266 | 	int do_now = how & AUTOFS_EXP_IMMEDIATE; | 
 | 267 |  | 
| Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 268 | 	if (!root) | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 269 | 		return NULL; | 
 | 270 |  | 
 | 271 | 	now = jiffies; | 
 | 272 | 	timeout = sbi->exp_timeout; | 
 | 273 |  | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 274 | 	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 Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 277 | 		if (d_mountpoint(root)) { | 
 | 278 | 			ino->flags |= AUTOFS_INF_MOUNTPOINT; | 
 | 279 | 			root->d_mounted--; | 
 | 280 | 		} | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 281 | 		ino->flags |= AUTOFS_INF_EXPIRING; | 
| Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 282 | 		init_completion(&ino->expire_complete); | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 283 | 		spin_unlock(&sbi->fs_lock); | 
 | 284 | 		return root; | 
 | 285 | 	} | 
 | 286 | 	spin_unlock(&sbi->fs_lock); | 
 | 287 | 	dput(root); | 
 | 288 |  | 
 | 289 | 	return NULL; | 
 | 290 | } | 
 | 291 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | /* | 
 | 293 |  * Find an eligible tree to time-out | 
 | 294 |  * A tree is eligible if :- | 
 | 295 |  *  - it is unused by any user process | 
 | 296 |  *  - it has been unused for exp_timeout time | 
 | 297 |  */ | 
| Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 298 | struct dentry *autofs4_expire_indirect(struct super_block *sb, | 
 | 299 | 				       struct vfsmount *mnt, | 
 | 300 | 				       struct autofs_sb_info *sbi, | 
 | 301 | 				       int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { | 
 | 303 | 	unsigned long timeout; | 
 | 304 | 	struct dentry *root = sb->s_root; | 
 | 305 | 	struct dentry *expired = NULL; | 
 | 306 | 	struct list_head *next; | 
 | 307 | 	int do_now = how & AUTOFS_EXP_IMMEDIATE; | 
 | 308 | 	int exp_leaves = how & AUTOFS_EXP_LEAVES; | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 309 | 	struct autofs_info *ino; | 
 | 310 | 	unsigned int ino_count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 |  | 
| Ian Kent | c0ba7e5 | 2006-09-25 16:24:16 -0700 | [diff] [blame] | 312 | 	if (!root) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | 		return NULL; | 
 | 314 |  | 
 | 315 | 	now = jiffies; | 
 | 316 | 	timeout = sbi->exp_timeout; | 
 | 317 |  | 
 | 318 | 	spin_lock(&dcache_lock); | 
 | 319 | 	next = root->d_subdirs.next; | 
 | 320 |  | 
 | 321 | 	/* On exit from the loop expire is set to a dgot dentry | 
 | 322 | 	 * to expire or it's NULL */ | 
 | 323 | 	while ( next != &root->d_subdirs ) { | 
| Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 324 | 		struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 |  | 
 | 326 | 		/* Negative dentry - give up */ | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 327 | 		if (!simple_positive(dentry)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | 			next = next->next; | 
 | 329 | 			continue; | 
 | 330 | 		} | 
 | 331 |  | 
 | 332 | 		dentry = dget(dentry); | 
 | 333 | 		spin_unlock(&dcache_lock); | 
 | 334 |  | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 335 | 		spin_lock(&sbi->fs_lock); | 
 | 336 | 		ino = autofs4_dentry_ino(dentry); | 
 | 337 |  | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 338 | 		/* | 
 | 339 | 		 * Case 1: (i) indirect mount or top level pseudo direct mount | 
 | 340 | 		 *	   (autofs-4.1). | 
 | 341 | 		 *	   (ii) indirect mount with offset mount, check the "/" | 
 | 342 | 		 *	   offset (autofs-5.0+). | 
 | 343 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | 		if (d_mountpoint(dentry)) { | 
 | 345 | 			DPRINTK("checking mountpoint %p %.*s", | 
 | 346 | 				dentry, (int)dentry->d_name.len, dentry->d_name.name); | 
 | 347 |  | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 348 | 			/* Path walk currently on this dentry? */ | 
 | 349 | 			ino_count = atomic_read(&ino->count) + 2; | 
 | 350 | 			if (atomic_read(&dentry->d_count) > ino_count) | 
 | 351 | 				goto next; | 
 | 352 |  | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 353 | 			/* Can we umount this guy */ | 
 | 354 | 			if (autofs4_mount_busy(mnt, dentry)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | 				goto next; | 
 | 356 |  | 
| Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 357 | 			/* Can we expire this guy */ | 
 | 358 | 			if (autofs4_can_expire(dentry, timeout, do_now)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | 				expired = dentry; | 
| Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 360 | 				goto found; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | 			} | 
 | 362 | 			goto next; | 
 | 363 | 		} | 
 | 364 |  | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 365 | 		if (simple_empty(dentry)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | 			goto next; | 
 | 367 |  | 
 | 368 | 		/* Case 2: tree mount, expire iff entire tree is not busy */ | 
 | 369 | 		if (!exp_leaves) { | 
| 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; | 
| Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 374 |  | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 375 | 			if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) { | 
| Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 376 | 				expired = dentry; | 
| Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 377 | 				goto found; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | 			} | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 379 | 		/* | 
 | 380 | 		 * Case 3: pseudo direct mount, expire individual leaves | 
 | 381 | 		 *	   (autofs-4.1). | 
 | 382 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | 		} else { | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 384 | 			/* Path walk currently on this dentry? */ | 
 | 385 | 			ino_count = atomic_read(&ino->count) + 1; | 
 | 386 | 			if (atomic_read(&dentry->d_count) > ino_count) | 
 | 387 | 				goto next; | 
 | 388 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | 			expired = autofs4_check_leaves(mnt, dentry, timeout, do_now); | 
 | 390 | 			if (expired) { | 
 | 391 | 				dput(dentry); | 
| Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 392 | 				goto found; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | 			} | 
 | 394 | 		} | 
 | 395 | next: | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 396 | 		spin_unlock(&sbi->fs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | 		dput(dentry); | 
 | 398 | 		spin_lock(&dcache_lock); | 
 | 399 | 		next = next->next; | 
 | 400 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | 	spin_unlock(&dcache_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | 	return NULL; | 
| Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 403 |  | 
 | 404 | found: | 
 | 405 | 	DPRINTK("returning %p %.*s", | 
 | 406 | 		expired, (int)expired->d_name.len, expired->d_name.name); | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 407 | 	ino = autofs4_dentry_ino(expired); | 
 | 408 | 	ino->flags |= AUTOFS_INF_EXPIRING; | 
| Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 409 | 	init_completion(&ino->expire_complete); | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 410 | 	spin_unlock(&sbi->fs_lock); | 
| Ian Kent | afec570 | 2008-05-01 04:35:06 -0700 | [diff] [blame] | 411 | 	spin_lock(&dcache_lock); | 
 | 412 | 	list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child); | 
 | 413 | 	spin_unlock(&dcache_lock); | 
 | 414 | 	return expired; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } | 
 | 416 |  | 
| Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 417 | int autofs4_expire_wait(struct dentry *dentry) | 
 | 418 | { | 
 | 419 | 	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | 
 | 420 | 	struct autofs_info *ino = autofs4_dentry_ino(dentry); | 
 | 421 | 	int status; | 
 | 422 |  | 
 | 423 | 	/* Block on any pending expire */ | 
 | 424 | 	spin_lock(&sbi->fs_lock); | 
 | 425 | 	if (ino->flags & AUTOFS_INF_EXPIRING) { | 
 | 426 | 		spin_unlock(&sbi->fs_lock); | 
 | 427 |  | 
 | 428 | 		DPRINTK("waiting for expire %p name=%.*s", | 
 | 429 | 			 dentry, dentry->d_name.len, dentry->d_name.name); | 
 | 430 |  | 
 | 431 | 		status = autofs4_wait(sbi, dentry, NFY_NONE); | 
 | 432 | 		wait_for_completion(&ino->expire_complete); | 
 | 433 |  | 
 | 434 | 		DPRINTK("expire done status=%d", status); | 
 | 435 |  | 
| Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 436 | 		if (d_unhashed(dentry)) | 
| Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 437 | 			return -EAGAIN; | 
 | 438 |  | 
 | 439 | 		return status; | 
 | 440 | 	} | 
 | 441 | 	spin_unlock(&sbi->fs_lock); | 
 | 442 |  | 
 | 443 | 	return 0; | 
 | 444 | } | 
 | 445 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* Perform an expiry operation */ | 
 | 447 | int autofs4_expire_run(struct super_block *sb, | 
 | 448 | 		      struct vfsmount *mnt, | 
 | 449 | 		      struct autofs_sb_info *sbi, | 
 | 450 | 		      struct autofs_packet_expire __user *pkt_p) | 
 | 451 | { | 
 | 452 | 	struct autofs_packet_expire pkt; | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 453 | 	struct autofs_info *ino; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | 	struct dentry *dentry; | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 455 | 	int ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 |  | 
 | 457 | 	memset(&pkt,0,sizeof pkt); | 
 | 458 |  | 
 | 459 | 	pkt.hdr.proto_version = sbi->version; | 
 | 460 | 	pkt.hdr.type = autofs_ptype_expire; | 
 | 461 |  | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 462 | 	if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | 		return -EAGAIN; | 
 | 464 |  | 
 | 465 | 	pkt.len = dentry->d_name.len; | 
 | 466 | 	memcpy(pkt.name, dentry->d_name.name, pkt.len); | 
 | 467 | 	pkt.name[pkt.len] = '\0'; | 
 | 468 | 	dput(dentry); | 
 | 469 |  | 
 | 470 | 	if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) ) | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 471 | 		ret = -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 |  | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 473 | 	spin_lock(&sbi->fs_lock); | 
 | 474 | 	ino = autofs4_dentry_ino(dentry); | 
 | 475 | 	ino->flags &= ~AUTOFS_INF_EXPIRING; | 
| Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 476 | 	complete_all(&ino->expire_complete); | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 477 | 	spin_unlock(&sbi->fs_lock); | 
 | 478 |  | 
 | 479 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } | 
 | 481 |  | 
| Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 482 | int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt, | 
 | 483 | 			    struct autofs_sb_info *sbi, int when) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { | 
 | 485 | 	struct dentry *dentry; | 
 | 486 | 	int ret = -EAGAIN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 |  | 
| Ian Kent | a92daf6 | 2009-01-06 14:42:08 -0800 | [diff] [blame] | 488 | 	if (autofs_type_trigger(sbi->type)) | 
| Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 489 | 		dentry = autofs4_expire_direct(sb, mnt, sbi, when); | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 490 | 	else | 
| Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 491 | 		dentry = autofs4_expire_indirect(sb, mnt, sbi, when); | 
| Ian Kent | 3a15e2a | 2006-03-27 01:14:55 -0800 | [diff] [blame] | 492 |  | 
 | 493 | 	if (dentry) { | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 494 | 		struct autofs_info *ino = autofs4_dentry_ino(dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 |  | 
 | 496 | 		/* This is synchronous because it makes the daemon a | 
 | 497 |                    little easier */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | 		ret = autofs4_wait(sbi, dentry, NFY_EXPIRE); | 
| Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 499 |  | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 500 | 		spin_lock(&sbi->fs_lock); | 
| Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 501 | 		if (ino->flags & AUTOFS_INF_MOUNTPOINT) { | 
 | 502 | 			sb->s_root->d_mounted++; | 
 | 503 | 			ino->flags &= ~AUTOFS_INF_MOUNTPOINT; | 
 | 504 | 		} | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 505 | 		ino->flags &= ~AUTOFS_INF_EXPIRING; | 
| Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 506 | 		complete_all(&ino->expire_complete); | 
| Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 507 | 		spin_unlock(&sbi->fs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | 		dput(dentry); | 
 | 509 | 	} | 
| Ian Kent | 1f5f2c3 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 510 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | 	return ret; | 
 | 512 | } | 
 | 513 |  | 
| Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 514 | /* Call repeatedly until it returns -EAGAIN, meaning there's nothing | 
 | 515 |    more to be done */ | 
 | 516 | int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt, | 
 | 517 | 			struct autofs_sb_info *sbi, int __user *arg) | 
 | 518 | { | 
 | 519 | 	int do_now = 0; | 
 | 520 |  | 
 | 521 | 	if (arg && get_user(do_now, arg)) | 
 | 522 | 		return -EFAULT; | 
 | 523 |  | 
 | 524 | 	return autofs4_do_expire_multi(sb, mnt, sbi, do_now); | 
 | 525 | } | 
 | 526 |  |