| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- --------------------------------------------------------- * | 
 | 2 |  * | 
 | 3 |  * linux/fs/autofs/root.c | 
 | 4 |  * | 
 | 5 |  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | 
 | 6 |  * | 
 | 7 |  * This file is part of the Linux kernel and is made available under | 
 | 8 |  * the terms of the GNU General Public License, version 2, or at your | 
 | 9 |  * option, any later version, incorporated herein by reference. | 
 | 10 |  * | 
 | 11 |  * ------------------------------------------------------------------------- */ | 
 | 12 |  | 
| Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 13 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> | 
 | 15 | #include <linux/stat.h> | 
 | 16 | #include <linux/param.h> | 
 | 17 | #include <linux/time.h> | 
 | 18 | #include <linux/smp_lock.h> | 
 | 19 | #include "autofs_i.h" | 
 | 20 |  | 
 | 21 | static int autofs_root_readdir(struct file *,void *,filldir_t); | 
 | 22 | static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *); | 
 | 23 | static int autofs_root_symlink(struct inode *,struct dentry *,const char *); | 
 | 24 | static int autofs_root_unlink(struct inode *,struct dentry *); | 
 | 25 | static int autofs_root_rmdir(struct inode *,struct dentry *); | 
 | 26 | static int autofs_root_mkdir(struct inode *,struct dentry *,int); | 
 | 27 | static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long); | 
 | 28 |  | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 29 | const struct file_operations autofs_root_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | 	.read		= generic_read_dir, | 
 | 31 | 	.readdir	= autofs_root_readdir, | 
 | 32 | 	.ioctl		= autofs_root_ioctl, | 
 | 33 | }; | 
 | 34 |  | 
| Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 35 | const struct inode_operations autofs_root_inode_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |         .lookup		= autofs_root_lookup, | 
 | 37 |         .unlink		= autofs_root_unlink, | 
 | 38 |         .symlink	= autofs_root_symlink, | 
 | 39 |         .mkdir		= autofs_root_mkdir, | 
 | 40 |         .rmdir		= autofs_root_rmdir, | 
 | 41 | }; | 
 | 42 |  | 
 | 43 | static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir) | 
 | 44 | { | 
 | 45 | 	struct autofs_dir_ent *ent = NULL; | 
 | 46 | 	struct autofs_dirhash *dirhash; | 
 | 47 | 	struct autofs_sb_info *sbi; | 
| Josef "Jeff" Sipek | 81ed19b | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 48 | 	struct inode * inode = filp->f_path.dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | 	off_t onr, nr; | 
 | 50 |  | 
 | 51 | 	lock_kernel(); | 
 | 52 |  | 
 | 53 | 	sbi = autofs_sbi(inode->i_sb); | 
 | 54 | 	dirhash = &sbi->dirhash; | 
 | 55 | 	nr = filp->f_pos; | 
 | 56 |  | 
 | 57 | 	switch(nr) | 
 | 58 | 	{ | 
 | 59 | 	case 0: | 
 | 60 | 		if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0) | 
 | 61 | 			goto out; | 
 | 62 | 		filp->f_pos = ++nr; | 
 | 63 | 		/* fall through */ | 
 | 64 | 	case 1: | 
 | 65 | 		if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0) | 
 | 66 | 			goto out; | 
 | 67 | 		filp->f_pos = ++nr; | 
 | 68 | 		/* fall through */ | 
 | 69 | 	default: | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 70 | 		while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) { | 
 | 71 | 			if (!ent->dentry || d_mountpoint(ent->dentry)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | 				if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0) | 
 | 73 | 					goto out; | 
 | 74 | 				filp->f_pos = nr; | 
 | 75 | 			} | 
 | 76 | 		} | 
 | 77 | 		break; | 
 | 78 | 	} | 
 | 79 |  | 
 | 80 | out: | 
 | 81 | 	unlock_kernel(); | 
 | 82 | 	return 0; | 
 | 83 | } | 
 | 84 |  | 
 | 85 | static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi) | 
 | 86 | { | 
 | 87 | 	struct inode * inode; | 
 | 88 | 	struct autofs_dir_ent *ent; | 
 | 89 | 	int status = 0; | 
 | 90 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 91 | 	if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | 		do { | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 93 | 			if (status && dentry->d_inode) { | 
 | 94 | 				if (status != -ENOENT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | 					printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name); | 
 | 96 | 				return 0; /* Try to get the kernel to invalidate this dentry */ | 
 | 97 | 			} | 
 | 98 |  | 
 | 99 | 			/* Turn this into a real negative dentry? */ | 
 | 100 | 			if (status == -ENOENT) { | 
 | 101 | 				dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT; | 
 | 102 | 				dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | 
 | 103 | 				return 1; | 
 | 104 | 			} else if (status) { | 
 | 105 | 				/* Return a negative dentry, but leave it "pending" */ | 
 | 106 | 				return 1; | 
 | 107 | 			} | 
 | 108 | 			status = autofs_wait(sbi, &dentry->d_name); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 109 | 		} while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | 	} | 
 | 111 |  | 
 | 112 | 	/* Abuse this field as a pointer to the directory entry, used to | 
 | 113 | 	   find the expire list pointers */ | 
 | 114 | 	dentry->d_time = (unsigned long) ent; | 
 | 115 | 	 | 
 | 116 | 	if (!dentry->d_inode) { | 
 | 117 | 		inode = iget(sb, ent->ino); | 
 | 118 | 		if (!inode) { | 
 | 119 | 			/* Failed, but leave pending for next time */ | 
 | 120 | 			return 1; | 
 | 121 | 		} | 
 | 122 | 		dentry->d_inode = inode; | 
 | 123 | 	} | 
 | 124 |  | 
 | 125 | 	/* If this is a directory that isn't a mount point, bitch at the | 
 | 126 | 	   daemon and fix it in user space */ | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 127 | 	if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | 		return !autofs_wait(sbi, &dentry->d_name); | 
 | 129 | 	} | 
 | 130 |  | 
 | 131 | 	/* We don't update the usages for the autofs daemon itself, this | 
 | 132 | 	   is necessary for recursive autofs mounts */ | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 133 | 	if (!autofs_oz_mode(sbi)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | 		autofs_update_usage(&sbi->dirhash,ent); | 
 | 135 | 	} | 
 | 136 |  | 
 | 137 | 	dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | 
 | 138 | 	return 1; | 
 | 139 | } | 
 | 140 |  | 
 | 141 |  | 
 | 142 | /* | 
 | 143 |  * Revalidate is called on every cache lookup.  Some of those | 
 | 144 |  * cache lookups may actually happen while the dentry is not | 
 | 145 |  * yet completely filled in, and revalidate has to delay such | 
 | 146 |  * lookups.. | 
 | 147 |  */ | 
 | 148 | static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd) | 
 | 149 | { | 
 | 150 | 	struct inode * dir; | 
 | 151 | 	struct autofs_sb_info *sbi; | 
 | 152 | 	struct autofs_dir_ent *ent; | 
 | 153 | 	int res; | 
 | 154 |  | 
 | 155 | 	lock_kernel(); | 
 | 156 | 	dir = dentry->d_parent->d_inode; | 
 | 157 | 	sbi = autofs_sbi(dir->i_sb); | 
 | 158 |  | 
 | 159 | 	/* Pending dentry */ | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 160 | 	if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | 		if (autofs_oz_mode(sbi)) | 
 | 162 | 			res = 1; | 
 | 163 | 		else | 
 | 164 | 			res = try_to_fill_dentry(dentry, dir->i_sb, sbi); | 
 | 165 | 		unlock_kernel(); | 
 | 166 | 		return res; | 
 | 167 | 	} | 
 | 168 |  | 
 | 169 | 	/* Negative dentry.. invalidate if "old" */ | 
 | 170 | 	if (!dentry->d_inode) { | 
 | 171 | 		unlock_kernel(); | 
 | 172 | 		return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT); | 
 | 173 | 	} | 
 | 174 | 		 | 
 | 175 | 	/* Check for a non-mountpoint directory */ | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 176 | 	if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | 		if (autofs_oz_mode(sbi)) | 
 | 178 | 			res = 1; | 
 | 179 | 		else | 
 | 180 | 			res = try_to_fill_dentry(dentry, dir->i_sb, sbi); | 
 | 181 | 		unlock_kernel(); | 
 | 182 | 		return res; | 
 | 183 | 	} | 
 | 184 |  | 
 | 185 | 	/* Update the usage list */ | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 186 | 	if (!autofs_oz_mode(sbi)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | 		ent = (struct autofs_dir_ent *) dentry->d_time; | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 188 | 		if (ent) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | 			autofs_update_usage(&sbi->dirhash,ent); | 
 | 190 | 	} | 
 | 191 | 	unlock_kernel(); | 
 | 192 | 	return 1; | 
 | 193 | } | 
 | 194 |  | 
 | 195 | static struct dentry_operations autofs_dentry_operations = { | 
 | 196 | 	.d_revalidate	= autofs_revalidate, | 
 | 197 | }; | 
 | 198 |  | 
 | 199 | static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | 
 | 200 | { | 
 | 201 | 	struct autofs_sb_info *sbi; | 
 | 202 | 	int oz_mode; | 
 | 203 |  | 
 | 204 | 	DPRINTK(("autofs_root_lookup: name = ")); | 
 | 205 | 	lock_kernel(); | 
 | 206 | 	autofs_say(dentry->d_name.name,dentry->d_name.len); | 
 | 207 |  | 
 | 208 | 	if (dentry->d_name.len > NAME_MAX) { | 
 | 209 | 		unlock_kernel(); | 
 | 210 | 		return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */ | 
 | 211 | 	} | 
 | 212 |  | 
 | 213 | 	sbi = autofs_sbi(dir->i_sb); | 
 | 214 |  | 
 | 215 | 	oz_mode = autofs_oz_mode(sbi); | 
| Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 216 | 	DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, " | 
| Pavel Emelyanov | 69cccb8 | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 217 | 				"oz_mode = %d\n", task_pid_nr(current), | 
| Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 218 | 				task_pgrp_nr(current), sbi->catatonic, | 
| Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 219 | 				oz_mode)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 |  | 
 | 221 | 	/* | 
 | 222 | 	 * Mark the dentry incomplete, but add it. This is needed so | 
 | 223 | 	 * that the VFS layer knows about the dentry, and we can count | 
 | 224 | 	 * on catching any lookups through the revalidate. | 
 | 225 | 	 * | 
 | 226 | 	 * Let all the hard work be done by the revalidate function that | 
 | 227 | 	 * needs to be able to do this anyway.. | 
 | 228 | 	 * | 
 | 229 | 	 * We need to do this before we release the directory semaphore. | 
 | 230 | 	 */ | 
 | 231 | 	dentry->d_op = &autofs_dentry_operations; | 
 | 232 | 	dentry->d_flags |= DCACHE_AUTOFS_PENDING; | 
 | 233 | 	d_add(dentry, NULL); | 
 | 234 |  | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 235 | 	mutex_unlock(&dir->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | 	autofs_revalidate(dentry, nd); | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 237 | 	mutex_lock(&dir->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 |  | 
 | 239 | 	/* | 
 | 240 | 	 * If we are still pending, check if we had to handle | 
 | 241 | 	 * a signal. If so we can force a restart.. | 
 | 242 | 	 */ | 
 | 243 | 	if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { | 
 | 244 | 		/* See if we were interrupted */ | 
 | 245 | 		if (signal_pending(current)) { | 
 | 246 | 			sigset_t *sigset = ¤t->pending.signal; | 
 | 247 | 			if (sigismember (sigset, SIGKILL) || | 
 | 248 | 			    sigismember (sigset, SIGQUIT) || | 
 | 249 | 			    sigismember (sigset, SIGINT)) { | 
 | 250 | 				unlock_kernel(); | 
 | 251 | 				return ERR_PTR(-ERESTARTNOINTR); | 
 | 252 | 			} | 
 | 253 | 		} | 
 | 254 | 	} | 
 | 255 | 	unlock_kernel(); | 
 | 256 |  | 
 | 257 | 	/* | 
 | 258 | 	 * If this dentry is unhashed, then we shouldn't honour this | 
 | 259 | 	 * lookup even if the dentry is positive.  Returning ENOENT here | 
 | 260 | 	 * doesn't do the right thing for all system calls, but it should | 
 | 261 | 	 * be OK for the operations we permit from an autofs. | 
 | 262 | 	 */ | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 263 | 	if (dentry->d_inode && d_unhashed(dentry)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | 		return ERR_PTR(-ENOENT); | 
 | 265 |  | 
 | 266 | 	return NULL; | 
 | 267 | } | 
 | 268 |  | 
 | 269 | static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | 
 | 270 | { | 
 | 271 | 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | 
 | 272 | 	struct autofs_dirhash *dh = &sbi->dirhash; | 
 | 273 | 	struct autofs_dir_ent *ent; | 
 | 274 | 	unsigned int n; | 
 | 275 | 	int slsize; | 
 | 276 | 	struct autofs_symlink *sl; | 
 | 277 |  | 
 | 278 | 	DPRINTK(("autofs_root_symlink: %s <- ", symname)); | 
 | 279 | 	autofs_say(dentry->d_name.name,dentry->d_name.len); | 
 | 280 |  | 
 | 281 | 	lock_kernel(); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 282 | 	if (!autofs_oz_mode(sbi)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 		unlock_kernel(); | 
 | 284 | 		return -EACCES; | 
 | 285 | 	} | 
 | 286 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 287 | 	if (autofs_hash_lookup(dh, &dentry->d_name)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | 		unlock_kernel(); | 
 | 289 | 		return -EEXIST; | 
 | 290 | 	} | 
 | 291 |  | 
 | 292 | 	n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 293 | 	if (n >= AUTOFS_MAX_SYMLINKS) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | 		unlock_kernel(); | 
 | 295 | 		return -ENOSPC; | 
 | 296 | 	} | 
 | 297 |  | 
 | 298 | 	set_bit(n,sbi->symlink_bitmap); | 
 | 299 | 	sl = &sbi->symlink[n]; | 
 | 300 | 	sl->len = strlen(symname); | 
 | 301 | 	sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 302 | 	if (!sl->data) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | 		clear_bit(n,sbi->symlink_bitmap); | 
 | 304 | 		unlock_kernel(); | 
 | 305 | 		return -ENOSPC; | 
 | 306 | 	} | 
 | 307 |  | 
 | 308 | 	ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 309 | 	if (!ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | 		kfree(sl->data); | 
 | 311 | 		clear_bit(n,sbi->symlink_bitmap); | 
 | 312 | 		unlock_kernel(); | 
 | 313 | 		return -ENOSPC; | 
 | 314 | 	} | 
 | 315 |  | 
 | 316 | 	ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 317 | 	if (!ent->name) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | 		kfree(sl->data); | 
 | 319 | 		kfree(ent); | 
 | 320 | 		clear_bit(n,sbi->symlink_bitmap); | 
 | 321 | 		unlock_kernel(); | 
 | 322 | 		return -ENOSPC; | 
 | 323 | 	} | 
 | 324 |  | 
 | 325 | 	memcpy(sl->data,symname,slsize); | 
 | 326 | 	sl->mtime = get_seconds(); | 
 | 327 |  | 
 | 328 | 	ent->ino = AUTOFS_FIRST_SYMLINK + n; | 
 | 329 | 	ent->hash = dentry->d_name.hash; | 
 | 330 | 	memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len)); | 
 | 331 | 	ent->dentry = NULL;	/* We don't keep the dentry for symlinks */ | 
 | 332 |  | 
 | 333 | 	autofs_hash_insert(dh,ent); | 
 | 334 | 	d_instantiate(dentry, iget(dir->i_sb,ent->ino)); | 
 | 335 | 	unlock_kernel(); | 
 | 336 | 	return 0; | 
 | 337 | } | 
 | 338 |  | 
 | 339 | /* | 
 | 340 |  * NOTE! | 
 | 341 |  * | 
 | 342 |  * Normal filesystems would do a "d_delete()" to tell the VFS dcache | 
 | 343 |  * that the file no longer exists. However, doing that means that the | 
 | 344 |  * VFS layer can turn the dentry into a negative dentry, which we | 
 | 345 |  * obviously do not want (we're dropping the entry not because it | 
 | 346 |  * doesn't exist, but because it has timed out). | 
 | 347 |  * | 
 | 348 |  * Also see autofs_root_rmdir().. | 
 | 349 |  */ | 
 | 350 | static int autofs_root_unlink(struct inode *dir, struct dentry *dentry) | 
 | 351 | { | 
 | 352 | 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | 
 | 353 | 	struct autofs_dirhash *dh = &sbi->dirhash; | 
 | 354 | 	struct autofs_dir_ent *ent; | 
 | 355 | 	unsigned int n; | 
 | 356 |  | 
 | 357 | 	/* This allows root to remove symlinks */ | 
 | 358 | 	lock_kernel(); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 359 | 	if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | 		unlock_kernel(); | 
 | 361 | 		return -EACCES; | 
 | 362 | 	} | 
 | 363 |  | 
 | 364 | 	ent = autofs_hash_lookup(dh, &dentry->d_name); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 365 | 	if (!ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | 		unlock_kernel(); | 
 | 367 | 		return -ENOENT; | 
 | 368 | 	} | 
 | 369 |  | 
 | 370 | 	n = ent->ino - AUTOFS_FIRST_SYMLINK; | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 371 | 	if (n >= AUTOFS_MAX_SYMLINKS) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | 		unlock_kernel(); | 
 | 373 | 		return -EISDIR;	/* It's a directory, dummy */ | 
 | 374 | 	} | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 375 | 	if (!test_bit(n,sbi->symlink_bitmap)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | 		unlock_kernel(); | 
 | 377 | 		return -EINVAL;	/* Nonexistent symlink?  Shouldn't happen */ | 
 | 378 | 	} | 
 | 379 | 	 | 
 | 380 | 	dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL; | 
 | 381 | 	autofs_hash_delete(ent); | 
 | 382 | 	clear_bit(n,sbi->symlink_bitmap); | 
 | 383 | 	kfree(sbi->symlink[n].data); | 
 | 384 | 	d_drop(dentry); | 
 | 385 | 	 | 
 | 386 | 	unlock_kernel(); | 
 | 387 | 	return 0; | 
 | 388 | } | 
 | 389 |  | 
 | 390 | static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry) | 
 | 391 | { | 
 | 392 | 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | 
 | 393 | 	struct autofs_dirhash *dh = &sbi->dirhash; | 
 | 394 | 	struct autofs_dir_ent *ent; | 
 | 395 |  | 
 | 396 | 	lock_kernel(); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 397 | 	if (!autofs_oz_mode(sbi)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | 		unlock_kernel(); | 
 | 399 | 		return -EACCES; | 
 | 400 | 	} | 
 | 401 |  | 
 | 402 | 	ent = autofs_hash_lookup(dh, &dentry->d_name); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 403 | 	if (!ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | 		unlock_kernel(); | 
 | 405 | 		return -ENOENT; | 
 | 406 | 	} | 
 | 407 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 408 | 	if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | 		unlock_kernel(); | 
 | 410 | 		return -ENOTDIR; /* Not a directory */ | 
 | 411 | 	} | 
 | 412 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 413 | 	if (ent->dentry != dentry) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | 		printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name); | 
 | 415 | 	} | 
 | 416 |  | 
 | 417 | 	dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL; | 
 | 418 | 	autofs_hash_delete(ent); | 
| Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 419 | 	drop_nlink(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | 	d_drop(dentry); | 
 | 421 | 	unlock_kernel(); | 
 | 422 |  | 
 | 423 | 	return 0; | 
 | 424 | } | 
 | 425 |  | 
 | 426 | static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode) | 
 | 427 | { | 
 | 428 | 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | 
 | 429 | 	struct autofs_dirhash *dh = &sbi->dirhash; | 
 | 430 | 	struct autofs_dir_ent *ent; | 
 | 431 | 	ino_t ino; | 
 | 432 |  | 
 | 433 | 	lock_kernel(); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 434 | 	if (!autofs_oz_mode(sbi)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | 		unlock_kernel(); | 
 | 436 | 		return -EACCES; | 
 | 437 | 	} | 
 | 438 |  | 
 | 439 | 	ent = autofs_hash_lookup(dh, &dentry->d_name); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 440 | 	if (ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | 		unlock_kernel(); | 
 | 442 | 		return -EEXIST; | 
 | 443 | 	} | 
 | 444 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 445 | 	if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | 		printk("autofs: Out of inode numbers -- what the heck did you do??\n"); | 
 | 447 | 		unlock_kernel(); | 
 | 448 | 		return -ENOSPC; | 
 | 449 | 	} | 
 | 450 | 	ino = sbi->next_dir_ino++; | 
 | 451 |  | 
 | 452 | 	ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 453 | 	if (!ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | 		unlock_kernel(); | 
 | 455 | 		return -ENOSPC; | 
 | 456 | 	} | 
 | 457 |  | 
 | 458 | 	ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL); | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 459 | 	if (!ent->name) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | 		kfree(ent); | 
 | 461 | 		unlock_kernel(); | 
 | 462 | 		return -ENOSPC; | 
 | 463 | 	} | 
 | 464 |  | 
 | 465 | 	ent->hash = dentry->d_name.hash; | 
 | 466 | 	memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len)); | 
 | 467 | 	ent->ino = ino; | 
 | 468 | 	ent->dentry = dentry; | 
 | 469 | 	autofs_hash_insert(dh,ent); | 
 | 470 |  | 
| Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 471 | 	inc_nlink(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | 	d_instantiate(dentry, iget(dir->i_sb,ino)); | 
 | 473 | 	unlock_kernel(); | 
 | 474 |  | 
 | 475 | 	return 0; | 
 | 476 | } | 
 | 477 |  | 
 | 478 | /* Get/set timeout ioctl() operation */ | 
 | 479 | static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi, | 
 | 480 | 					 unsigned long __user *p) | 
 | 481 | { | 
 | 482 | 	unsigned long ntimeout; | 
 | 483 |  | 
 | 484 | 	if (get_user(ntimeout, p) || | 
 | 485 | 	    put_user(sbi->exp_timeout / HZ, p)) | 
 | 486 | 		return -EFAULT; | 
 | 487 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 488 | 	if (ntimeout > ULONG_MAX/HZ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | 		sbi->exp_timeout = 0; | 
 | 490 | 	else | 
 | 491 | 		sbi->exp_timeout = ntimeout * HZ; | 
 | 492 |  | 
 | 493 | 	return 0; | 
 | 494 | } | 
 | 495 |  | 
 | 496 | /* Return protocol version */ | 
 | 497 | static inline int autofs_get_protover(int __user *p) | 
 | 498 | { | 
 | 499 | 	return put_user(AUTOFS_PROTO_VERSION, p); | 
 | 500 | } | 
 | 501 |  | 
 | 502 | /* Perform an expiry operation */ | 
 | 503 | static inline int autofs_expire_run(struct super_block *sb, | 
 | 504 | 				    struct autofs_sb_info *sbi, | 
 | 505 | 				    struct vfsmount *mnt, | 
 | 506 | 				    struct autofs_packet_expire __user *pkt_p) | 
 | 507 | { | 
 | 508 | 	struct autofs_dir_ent *ent; | 
 | 509 | 	struct autofs_packet_expire pkt; | 
 | 510 |  | 
 | 511 | 	memset(&pkt,0,sizeof pkt); | 
 | 512 |  | 
 | 513 | 	pkt.hdr.proto_version = AUTOFS_PROTO_VERSION; | 
 | 514 | 	pkt.hdr.type = autofs_ptype_expire; | 
 | 515 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 516 | 	if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | 		return -EAGAIN; | 
 | 518 |  | 
 | 519 | 	pkt.len = ent->len; | 
 | 520 | 	memcpy(pkt.name, ent->name, pkt.len); | 
 | 521 | 	pkt.name[pkt.len] = '\0'; | 
 | 522 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 523 | 	if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | 		return -EFAULT; | 
 | 525 |  | 
 | 526 | 	return 0; | 
 | 527 | } | 
 | 528 |  | 
 | 529 | /* | 
 | 530 |  * ioctl()'s on the root directory is the chief method for the daemon to | 
 | 531 |  * generate kernel reactions | 
 | 532 |  */ | 
 | 533 | static int autofs_root_ioctl(struct inode *inode, struct file *filp, | 
 | 534 | 			     unsigned int cmd, unsigned long arg) | 
 | 535 | { | 
 | 536 | 	struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb); | 
 | 537 | 	void __user *argp = (void __user *)arg; | 
 | 538 |  | 
| Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 539 | 	DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 |  | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 541 | 	if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || | 
 | 542 | 	     _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | 		return -ENOTTY; | 
 | 544 | 	 | 
| Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 545 | 	if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | 		return -EPERM; | 
 | 547 | 	 | 
 | 548 | 	switch(cmd) { | 
 | 549 | 	case AUTOFS_IOC_READY:	/* Wait queue: go ahead and retry */ | 
 | 550 | 		return autofs_wait_release(sbi,(autofs_wqt_t)arg,0); | 
 | 551 | 	case AUTOFS_IOC_FAIL:	/* Wait queue: fail with ENOENT */ | 
 | 552 | 		return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); | 
 | 553 | 	case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ | 
 | 554 | 		autofs_catatonic_mode(sbi); | 
 | 555 | 		return 0; | 
 | 556 | 	case AUTOFS_IOC_PROTOVER: /* Get protocol version */ | 
 | 557 | 		return autofs_get_protover(argp); | 
 | 558 | 	case AUTOFS_IOC_SETTIMEOUT: | 
 | 559 | 		return autofs_get_set_timeout(sbi, argp); | 
 | 560 | 	case AUTOFS_IOC_EXPIRE: | 
| Josef "Jeff" Sipek | 81ed19b | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 561 | 		return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | 					 argp); | 
 | 563 | 	default: | 
 | 564 | 		return -ENOSYS; | 
 | 565 | 	} | 
 | 566 | } |