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