| 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> | 
 | 7 |  *  Copyright 2001-2003 Ian Kent <raven@themaw.net> | 
 | 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 |  | 
 | 19 | /* Check if a dentry can be expired return 1 if it can else return 0 */ | 
 | 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 */ | 
 | 35 | 		if (time_after(ino->last_used + timeout, now)) | 
 | 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 | 	} | 
 | 44 |  | 
 | 45 | 	return 1; | 
 | 46 | } | 
 | 47 |  | 
 | 48 | /* Check a mount point for busyness return 1 if not busy, otherwise */ | 
 | 49 | static int autofs4_check_mount(struct vfsmount *mnt, struct dentry *dentry) | 
 | 50 | { | 
 | 51 | 	int status = 0; | 
 | 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 |  | 
 | 59 | 	if (!follow_down(&mnt, &dentry)) | 
 | 60 | 		goto done; | 
 | 61 |  | 
 | 62 | 	while (d_mountpoint(dentry) && follow_down(&mnt, &dentry)) | 
 | 63 | 		; | 
 | 64 |  | 
 | 65 | 	/* This is an autofs submount, we can't expire it */ | 
 | 66 | 	if (is_autofs4_dentry(dentry)) | 
 | 67 | 		goto done; | 
 | 68 |  | 
 | 69 | 	/* The big question */ | 
 | 70 | 	if (may_umount_tree(mnt) == 0) | 
 | 71 | 		status = 1; | 
 | 72 | done: | 
 | 73 | 	DPRINTK("returning = %d", status); | 
 | 74 | 	mntput(mnt); | 
 | 75 | 	dput(dentry); | 
 | 76 | 	return status; | 
 | 77 | } | 
 | 78 |  | 
 | 79 | /* Check a directory tree of mount points for busyness | 
 | 80 |  * The tree is not busy iff no mountpoints are busy | 
 | 81 |  * Return 1 if the tree is busy or 0 otherwise | 
 | 82 |  */ | 
 | 83 | static int autofs4_check_tree(struct vfsmount *mnt, | 
 | 84 | 	       		      struct dentry *top, | 
 | 85 | 			      unsigned long timeout, | 
 | 86 | 			      int do_now) | 
 | 87 | { | 
 | 88 | 	struct dentry *this_parent = top; | 
 | 89 | 	struct list_head *next; | 
 | 90 |  | 
 | 91 | 	DPRINTK("parent %p %.*s", | 
 | 92 | 		top, (int)top->d_name.len, top->d_name.name); | 
 | 93 |  | 
 | 94 | 	/* Negative dentry - give up */ | 
 | 95 | 	if (!simple_positive(top)) | 
 | 96 | 		return 0; | 
 | 97 |  | 
 | 98 | 	/* Timeout of a tree mount is determined by its top dentry */ | 
 | 99 | 	if (!autofs4_can_expire(top, timeout, do_now)) | 
 | 100 | 		return 0; | 
 | 101 |  | 
| Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 102 | 	/* Is someone visiting anywhere in the tree ? */ | 
 | 103 | 	if (may_umount_tree(mnt)) | 
 | 104 | 		return 0; | 
 | 105 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | 	spin_lock(&dcache_lock); | 
 | 107 | repeat: | 
 | 108 | 	next = this_parent->d_subdirs.next; | 
 | 109 | resume: | 
 | 110 | 	while (next != &this_parent->d_subdirs) { | 
 | 111 | 		struct dentry *dentry = list_entry(next, struct dentry, d_child); | 
 | 112 |  | 
 | 113 | 		/* Negative dentry - give up */ | 
 | 114 | 		if (!simple_positive(dentry)) { | 
 | 115 | 			next = next->next; | 
 | 116 | 			continue; | 
 | 117 | 		} | 
 | 118 |  | 
 | 119 | 		DPRINTK("dentry %p %.*s", | 
 | 120 | 			dentry, (int)dentry->d_name.len, dentry->d_name.name); | 
 | 121 |  | 
 | 122 | 		if (!simple_empty_nolock(dentry)) { | 
 | 123 | 			this_parent = dentry; | 
 | 124 | 			goto repeat; | 
 | 125 | 		} | 
 | 126 |  | 
 | 127 | 		dentry = dget(dentry); | 
 | 128 | 		spin_unlock(&dcache_lock); | 
 | 129 |  | 
 | 130 | 		if (d_mountpoint(dentry)) { | 
 | 131 | 			/* First busy => tree busy */ | 
 | 132 | 			if (!autofs4_check_mount(mnt, dentry)) { | 
 | 133 | 				dput(dentry); | 
 | 134 | 				return 0; | 
 | 135 | 			} | 
 | 136 | 		} | 
 | 137 |  | 
 | 138 | 		dput(dentry); | 
 | 139 | 		spin_lock(&dcache_lock); | 
 | 140 | 		next = next->next; | 
 | 141 | 	} | 
 | 142 |  | 
 | 143 | 	if (this_parent != top) { | 
 | 144 | 		next = this_parent->d_child.next; | 
 | 145 | 		this_parent = this_parent->d_parent; | 
 | 146 | 		goto resume; | 
 | 147 | 	} | 
 | 148 | 	spin_unlock(&dcache_lock); | 
 | 149 |  | 
 | 150 | 	return 1; | 
 | 151 | } | 
 | 152 |  | 
 | 153 | static struct dentry *autofs4_check_leaves(struct vfsmount *mnt, | 
 | 154 | 					   struct dentry *parent, | 
 | 155 | 					   unsigned long timeout, | 
 | 156 | 					   int do_now) | 
 | 157 | { | 
 | 158 | 	struct dentry *this_parent = parent; | 
 | 159 | 	struct list_head *next; | 
 | 160 |  | 
 | 161 | 	DPRINTK("parent %p %.*s", | 
 | 162 | 		parent, (int)parent->d_name.len, parent->d_name.name); | 
 | 163 |  | 
 | 164 | 	spin_lock(&dcache_lock); | 
 | 165 | repeat: | 
 | 166 | 	next = this_parent->d_subdirs.next; | 
 | 167 | resume: | 
 | 168 | 	while (next != &this_parent->d_subdirs) { | 
 | 169 | 		struct dentry *dentry = list_entry(next, struct dentry, d_child); | 
 | 170 |  | 
 | 171 | 		/* Negative dentry - give up */ | 
 | 172 | 		if (!simple_positive(dentry)) { | 
 | 173 | 			next = next->next; | 
 | 174 | 			continue; | 
 | 175 | 		} | 
 | 176 |  | 
 | 177 | 		DPRINTK("dentry %p %.*s", | 
 | 178 | 			dentry, (int)dentry->d_name.len, dentry->d_name.name); | 
 | 179 |  | 
 | 180 | 		if (!list_empty(&dentry->d_subdirs)) { | 
 | 181 | 			this_parent = dentry; | 
 | 182 | 			goto repeat; | 
 | 183 | 		} | 
 | 184 |  | 
 | 185 | 		dentry = dget(dentry); | 
 | 186 | 		spin_unlock(&dcache_lock); | 
 | 187 |  | 
 | 188 | 		if (d_mountpoint(dentry)) { | 
 | 189 | 			/* Can we expire this guy */ | 
 | 190 | 			if (!autofs4_can_expire(dentry, timeout, do_now)) | 
 | 191 | 				goto cont; | 
 | 192 |  | 
 | 193 | 			/* Can we umount this guy */ | 
 | 194 | 			if (autofs4_check_mount(mnt, dentry)) | 
 | 195 | 				return dentry; | 
 | 196 |  | 
 | 197 | 		} | 
 | 198 | cont: | 
 | 199 | 		dput(dentry); | 
 | 200 | 		spin_lock(&dcache_lock); | 
 | 201 | 		next = next->next; | 
 | 202 | 	} | 
 | 203 |  | 
 | 204 | 	if (this_parent != parent) { | 
 | 205 | 		next = this_parent->d_child.next; | 
 | 206 | 		this_parent = this_parent->d_parent; | 
 | 207 | 		goto resume; | 
 | 208 | 	} | 
 | 209 | 	spin_unlock(&dcache_lock); | 
 | 210 |  | 
 | 211 | 	return NULL; | 
 | 212 | } | 
 | 213 |  | 
 | 214 | /* | 
 | 215 |  * Find an eligible tree to time-out | 
 | 216 |  * A tree is eligible if :- | 
 | 217 |  *  - it is unused by any user process | 
 | 218 |  *  - it has been unused for exp_timeout time | 
 | 219 |  */ | 
 | 220 | static struct dentry *autofs4_expire(struct super_block *sb, | 
 | 221 | 				     struct vfsmount *mnt, | 
 | 222 | 				     struct autofs_sb_info *sbi, | 
 | 223 | 				     int how) | 
 | 224 | { | 
 | 225 | 	unsigned long timeout; | 
 | 226 | 	struct dentry *root = sb->s_root; | 
 | 227 | 	struct dentry *expired = NULL; | 
 | 228 | 	struct list_head *next; | 
 | 229 | 	int do_now = how & AUTOFS_EXP_IMMEDIATE; | 
 | 230 | 	int exp_leaves = how & AUTOFS_EXP_LEAVES; | 
 | 231 |  | 
 | 232 | 	if ( !sbi->exp_timeout || !root ) | 
 | 233 | 		return NULL; | 
 | 234 |  | 
 | 235 | 	now = jiffies; | 
 | 236 | 	timeout = sbi->exp_timeout; | 
 | 237 |  | 
 | 238 | 	spin_lock(&dcache_lock); | 
 | 239 | 	next = root->d_subdirs.next; | 
 | 240 |  | 
 | 241 | 	/* On exit from the loop expire is set to a dgot dentry | 
 | 242 | 	 * to expire or it's NULL */ | 
 | 243 | 	while ( next != &root->d_subdirs ) { | 
 | 244 | 		struct dentry *dentry = list_entry(next, struct dentry, d_child); | 
 | 245 |  | 
 | 246 | 		/* Negative dentry - give up */ | 
 | 247 | 		if ( !simple_positive(dentry) ) { | 
 | 248 | 			next = next->next; | 
 | 249 | 			continue; | 
 | 250 | 		} | 
 | 251 |  | 
 | 252 | 		dentry = dget(dentry); | 
 | 253 | 		spin_unlock(&dcache_lock); | 
 | 254 |  | 
 | 255 | 		/* Case 1: indirect mount or top level direct mount */ | 
 | 256 | 		if (d_mountpoint(dentry)) { | 
 | 257 | 			DPRINTK("checking mountpoint %p %.*s", | 
 | 258 | 				dentry, (int)dentry->d_name.len, dentry->d_name.name); | 
 | 259 |  | 
 | 260 | 			/* Can we expire this guy */ | 
 | 261 | 			if (!autofs4_can_expire(dentry, timeout, do_now)) | 
 | 262 | 				goto next; | 
 | 263 |  | 
 | 264 | 			/* Can we umount this guy */ | 
 | 265 | 			if (autofs4_check_mount(mnt, dentry)) { | 
 | 266 | 				expired = dentry; | 
 | 267 | 				break; | 
 | 268 | 			} | 
 | 269 | 			goto next; | 
 | 270 | 		} | 
 | 271 |  | 
 | 272 | 		if ( simple_empty(dentry) ) | 
 | 273 | 			goto next; | 
 | 274 |  | 
 | 275 | 		/* Case 2: tree mount, expire iff entire tree is not busy */ | 
 | 276 | 		if (!exp_leaves) { | 
| Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 277 | 			/* Lock the tree as we must expire as a whole */ | 
 | 278 | 			spin_lock(&sbi->fs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | 			if (autofs4_check_tree(mnt, dentry, timeout, do_now)) { | 
| Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 280 | 				struct autofs_info *inf = autofs4_dentry_ino(dentry); | 
 | 281 |  | 
 | 282 | 				/* Set this flag early to catch sys_chdir and the like */ | 
 | 283 | 				inf->flags |= AUTOFS_INF_EXPIRING; | 
 | 284 | 				spin_unlock(&sbi->fs_lock); | 
 | 285 | 				expired = dentry; | 
 | 286 | 				break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | 			} | 
| Ian Kent | 3a9720c | 2005-05-01 08:59:17 -0700 | [diff] [blame] | 288 | 			spin_unlock(&sbi->fs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | 		/* Case 3: direct mount, expire individual leaves */ | 
 | 290 | 		} else { | 
 | 291 | 			expired = autofs4_check_leaves(mnt, dentry, timeout, do_now); | 
 | 292 | 			if (expired) { | 
 | 293 | 				dput(dentry); | 
 | 294 | 				break; | 
 | 295 | 			} | 
 | 296 | 		} | 
 | 297 | next: | 
 | 298 | 		dput(dentry); | 
 | 299 | 		spin_lock(&dcache_lock); | 
 | 300 | 		next = next->next; | 
 | 301 | 	} | 
 | 302 |  | 
 | 303 | 	if ( expired ) { | 
 | 304 | 		DPRINTK("returning %p %.*s", | 
 | 305 | 			expired, (int)expired->d_name.len, expired->d_name.name); | 
 | 306 | 		spin_lock(&dcache_lock); | 
 | 307 | 		list_del(&expired->d_parent->d_subdirs); | 
 | 308 | 		list_add(&expired->d_parent->d_subdirs, &expired->d_child); | 
 | 309 | 		spin_unlock(&dcache_lock); | 
 | 310 | 		return expired; | 
 | 311 | 	} | 
 | 312 | 	spin_unlock(&dcache_lock); | 
 | 313 |  | 
 | 314 | 	return NULL; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | /* Perform an expiry operation */ | 
 | 318 | int autofs4_expire_run(struct super_block *sb, | 
 | 319 | 		      struct vfsmount *mnt, | 
 | 320 | 		      struct autofs_sb_info *sbi, | 
 | 321 | 		      struct autofs_packet_expire __user *pkt_p) | 
 | 322 | { | 
 | 323 | 	struct autofs_packet_expire pkt; | 
 | 324 | 	struct dentry *dentry; | 
 | 325 |  | 
 | 326 | 	memset(&pkt,0,sizeof pkt); | 
 | 327 |  | 
 | 328 | 	pkt.hdr.proto_version = sbi->version; | 
 | 329 | 	pkt.hdr.type = autofs_ptype_expire; | 
 | 330 |  | 
 | 331 | 	if ((dentry = autofs4_expire(sb, mnt, sbi, 0)) == NULL) | 
 | 332 | 		return -EAGAIN; | 
 | 333 |  | 
 | 334 | 	pkt.len = dentry->d_name.len; | 
 | 335 | 	memcpy(pkt.name, dentry->d_name.name, pkt.len); | 
 | 336 | 	pkt.name[pkt.len] = '\0'; | 
 | 337 | 	dput(dentry); | 
 | 338 |  | 
 | 339 | 	if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) ) | 
 | 340 | 		return -EFAULT; | 
 | 341 |  | 
 | 342 | 	return 0; | 
 | 343 | } | 
 | 344 |  | 
 | 345 | /* Call repeatedly until it returns -EAGAIN, meaning there's nothing | 
 | 346 |    more to be done */ | 
 | 347 | int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt, | 
 | 348 | 			struct autofs_sb_info *sbi, int __user *arg) | 
 | 349 | { | 
 | 350 | 	struct dentry *dentry; | 
 | 351 | 	int ret = -EAGAIN; | 
 | 352 | 	int do_now = 0; | 
 | 353 |  | 
 | 354 | 	if (arg && get_user(do_now, arg)) | 
 | 355 | 		return -EFAULT; | 
 | 356 |  | 
 | 357 | 	if ((dentry = autofs4_expire(sb, mnt, sbi, do_now)) != NULL) { | 
 | 358 | 		struct autofs_info *de_info = autofs4_dentry_ino(dentry); | 
 | 359 |  | 
 | 360 | 		/* This is synchronous because it makes the daemon a | 
 | 361 |                    little easier */ | 
 | 362 | 		de_info->flags |= AUTOFS_INF_EXPIRING; | 
 | 363 | 		ret = autofs4_wait(sbi, dentry, NFY_EXPIRE); | 
 | 364 | 		de_info->flags &= ~AUTOFS_INF_EXPIRING; | 
 | 365 | 		dput(dentry); | 
 | 366 | 	} | 
 | 367 | 		 | 
 | 368 | 	return ret; | 
 | 369 | } | 
 | 370 |  |