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