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