Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- c -*- --------------------------------------------------------------- * |
| 2 | * |
| 3 | * linux/fs/autofs/root.c |
| 4 | * |
| 5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved |
| 6 | * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org> |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 7 | * Copyright 2001-2006 Ian Kent <raven@themaw.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This file is part of the Linux kernel and is made available under |
| 10 | * the terms of the GNU General Public License, version 2, or at your |
| 11 | * option, any later version, incorporated herein by reference. |
| 12 | * |
| 13 | * ------------------------------------------------------------------------- */ |
| 14 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 15 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/stat.h> |
| 18 | #include <linux/param.h> |
| 19 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "autofs_i.h" |
| 21 | |
| 22 | static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *); |
| 23 | static int autofs4_dir_unlink(struct inode *,struct dentry *); |
| 24 | static int autofs4_dir_rmdir(struct inode *,struct dentry *); |
| 25 | static int autofs4_dir_mkdir(struct inode *,struct dentry *,int); |
| 26 | static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long); |
| 27 | static int autofs4_dir_open(struct inode *inode, struct file *file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir); |
| 29 | static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 30 | static void *autofs4_follow_link(struct dentry *, struct nameidata *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Ian Kent | 6d5cb92 | 2008-07-23 21:30:15 -0700 | [diff] [blame] | 32 | #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) |
| 33 | #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE) |
| 34 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 35 | const struct file_operations autofs4_root_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | .open = dcache_dir_open, |
| 37 | .release = dcache_dir_close, |
| 38 | .read = generic_read_dir, |
| 39 | .readdir = autofs4_root_readdir, |
| 40 | .ioctl = autofs4_root_ioctl, |
| 41 | }; |
| 42 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 43 | const struct file_operations autofs4_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | .open = autofs4_dir_open, |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 45 | .release = dcache_dir_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | .read = generic_read_dir, |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 47 | .readdir = dcache_readdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 50 | const struct inode_operations autofs4_indirect_root_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | .lookup = autofs4_lookup, |
| 52 | .unlink = autofs4_dir_unlink, |
| 53 | .symlink = autofs4_dir_symlink, |
| 54 | .mkdir = autofs4_dir_mkdir, |
| 55 | .rmdir = autofs4_dir_rmdir, |
| 56 | }; |
| 57 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 58 | const struct inode_operations autofs4_direct_root_inode_operations = { |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 59 | .lookup = autofs4_lookup, |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 60 | .unlink = autofs4_dir_unlink, |
| 61 | .mkdir = autofs4_dir_mkdir, |
| 62 | .rmdir = autofs4_dir_rmdir, |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 63 | .follow_link = autofs4_follow_link, |
| 64 | }; |
| 65 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 66 | const struct inode_operations autofs4_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | .lookup = autofs4_lookup, |
| 68 | .unlink = autofs4_dir_unlink, |
| 69 | .symlink = autofs4_dir_symlink, |
| 70 | .mkdir = autofs4_dir_mkdir, |
| 71 | .rmdir = autofs4_dir_rmdir, |
| 72 | }; |
| 73 | |
| 74 | static int autofs4_root_readdir(struct file *file, void *dirent, |
| 75 | filldir_t filldir) |
| 76 | { |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 77 | struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | int oz_mode = autofs4_oz_mode(sbi); |
| 79 | |
| 80 | DPRINTK("called, filp->f_pos = %lld", file->f_pos); |
| 81 | |
| 82 | /* |
| 83 | * Don't set reghost flag if: |
| 84 | * 1) f_pos is larger than zero -- we've already been here. |
| 85 | * 2) we haven't even enabled reghosting in the 1st place. |
| 86 | * 3) this is the daemon doing a readdir |
| 87 | */ |
| 88 | if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled) |
| 89 | sbi->needs_reghost = 1; |
| 90 | |
| 91 | DPRINTK("needs_reghost = %d", sbi->needs_reghost); |
| 92 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 93 | return dcache_readdir(file, dirent, filldir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static int autofs4_dir_open(struct inode *inode, struct file *file) |
| 97 | { |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 98 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | DPRINTK("file=%p dentry=%p %.*s", |
| 102 | file, dentry, dentry->d_name.len, dentry->d_name.name); |
| 103 | |
| 104 | if (autofs4_oz_mode(sbi)) |
| 105 | goto out; |
| 106 | |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 107 | /* |
| 108 | * An empty directory in an autofs file system is always a |
| 109 | * mount point. The daemon must have failed to mount this |
| 110 | * during lookup so it doesn't exist. This can happen, for |
| 111 | * example, if user space returns an incorrect status for a |
| 112 | * mount request. Otherwise we're doing a readdir on the |
| 113 | * autofs file system so just let the libfs routines handle |
| 114 | * it. |
| 115 | */ |
| 116 | spin_lock(&dcache_lock); |
| 117 | if (!d_mountpoint(dentry) && __simple_empty(dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | spin_unlock(&dcache_lock); |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 119 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 121 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 123 | out: |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 124 | return dcache_dir_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame] | 127 | static int try_to_fill_dentry(struct dentry *dentry, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame] | 129 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 130 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Jeff Moyer | 9d2de6a | 2008-05-01 04:35:09 -0700 | [diff] [blame] | 131 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | DPRINTK("dentry=%p %.*s ino=%p", |
| 134 | dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode); |
| 135 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 136 | /* |
| 137 | * Wait for a pending mount, triggering one if there |
| 138 | * isn't one already |
| 139 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (dentry->d_inode == NULL) { |
| 141 | DPRINTK("waiting for mount name=%.*s", |
| 142 | dentry->d_name.len, dentry->d_name.name); |
| 143 | |
| 144 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | DPRINTK("mount done status=%d", status); |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* Turn this into a real negative dentry? */ |
| 149 | if (status == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | spin_lock(&dentry->d_lock); |
| 151 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 152 | spin_unlock(&dentry->d_lock); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 153 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } else if (status) { |
| 155 | /* Return a negative dentry, but leave it "pending" */ |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 156 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | /* Trigger mount for path component or follow link */ |
Ian Kent | 26e81b3 | 2008-07-23 21:30:25 -0700 | [diff] [blame] | 159 | } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING || |
| 160 | flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | current->link_count) { |
| 162 | DPRINTK("waiting for mount name=%.*s", |
| 163 | dentry->d_name.len, dentry->d_name.name); |
| 164 | |
| 165 | spin_lock(&dentry->d_lock); |
| 166 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; |
| 167 | spin_unlock(&dentry->d_lock); |
| 168 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); |
| 169 | |
| 170 | DPRINTK("mount done status=%d", status); |
| 171 | |
| 172 | if (status) { |
| 173 | spin_lock(&dentry->d_lock); |
| 174 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 175 | spin_unlock(&dentry->d_lock); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 176 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 180 | /* Initialize expiry counter after successful mount */ |
| 181 | if (ino) |
| 182 | ino->last_used = jiffies; |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | spin_lock(&dentry->d_lock); |
| 185 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 186 | spin_unlock(&dentry->d_lock); |
Jeff Moyer | 0337904 | 2008-05-01 04:35:08 -0700 | [diff] [blame] | 187 | |
Jeff Moyer | 9d2de6a | 2008-05-01 04:35:09 -0700 | [diff] [blame] | 188 | return 0; |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* For autofs direct mounts the follow link triggers the mount */ |
| 192 | static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 193 | { |
| 194 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 195 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 196 | int oz_mode = autofs4_oz_mode(sbi); |
| 197 | unsigned int lookup_type; |
| 198 | int status; |
| 199 | |
| 200 | DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d", |
| 201 | dentry, dentry->d_name.len, dentry->d_name.name, oz_mode, |
| 202 | nd->flags); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 203 | /* |
| 204 | * For an expire of a covered direct or offset mount we need |
| 205 | * to beeak out of follow_down() at the autofs mount trigger |
| 206 | * (d_mounted--), so we can see the expiring flag, and manage |
| 207 | * the blocking and following here until the expire is completed. |
| 208 | */ |
| 209 | if (oz_mode) { |
| 210 | spin_lock(&sbi->fs_lock); |
| 211 | if (ino->flags & AUTOFS_INF_EXPIRING) { |
| 212 | spin_unlock(&sbi->fs_lock); |
| 213 | /* Follow down to our covering mount. */ |
| 214 | if (!follow_down(&nd->path.mnt, &nd->path.dentry)) |
| 215 | goto done; |
Ian Kent | ec6e8c7 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 216 | goto follow; |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 217 | } |
| 218 | spin_unlock(&sbi->fs_lock); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 219 | goto done; |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 220 | } |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 221 | |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 222 | /* If an expire request is pending everyone must wait. */ |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame^] | 223 | autofs4_expire_wait(dentry); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 224 | |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 225 | /* We trigger a mount for almost all flags */ |
| 226 | lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS); |
| 227 | if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING)) |
Ian Kent | ec6e8c7 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 228 | goto follow; |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 229 | |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 230 | /* |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 231 | * If the dentry contains directories then it is an autofs |
| 232 | * multi-mount with no root mount offset. So don't try to |
| 233 | * mount it again. |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 234 | */ |
| 235 | spin_lock(&dcache_lock); |
Ian Kent | 26e81b3 | 2008-07-23 21:30:25 -0700 | [diff] [blame] | 236 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING || |
| 237 | (!d_mountpoint(dentry) && __simple_empty(dentry))) { |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 238 | spin_unlock(&dcache_lock); |
| 239 | |
| 240 | status = try_to_fill_dentry(dentry, 0); |
| 241 | if (status) |
| 242 | goto out_error; |
| 243 | |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 244 | goto follow; |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 245 | } |
| 246 | spin_unlock(&dcache_lock); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 247 | follow: |
| 248 | /* |
| 249 | * If there is no root mount it must be an autofs |
| 250 | * multi-mount with no root offset so we don't need |
| 251 | * to follow it. |
| 252 | */ |
| 253 | if (d_mountpoint(dentry)) { |
| 254 | if (!autofs4_follow_mount(&nd->path.mnt, |
| 255 | &nd->path.dentry)) { |
| 256 | status = -ENOENT; |
| 257 | goto out_error; |
| 258 | } |
| 259 | } |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 260 | |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 261 | done: |
| 262 | return NULL; |
| 263 | |
| 264 | out_error: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 265 | path_put(&nd->path); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 266 | return ERR_PTR(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Revalidate is called on every cache lookup. Some of those |
| 271 | * cache lookups may actually happen while the dentry is not |
| 272 | * yet completely filled in, and revalidate has to delay such |
| 273 | * lookups.. |
| 274 | */ |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 275 | static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 277 | struct inode *dir = dentry->d_parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 279 | int oz_mode = autofs4_oz_mode(sbi); |
| 280 | int flags = nd ? nd->flags : 0; |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 281 | int status = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | /* Pending dentry */ |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 284 | spin_lock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (autofs4_ispending(dentry)) { |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 286 | /* The daemon never causes a mount to trigger */ |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 287 | spin_unlock(&sbi->fs_lock); |
| 288 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 289 | if (oz_mode) |
| 290 | return 1; |
| 291 | |
| 292 | /* |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame^] | 293 | * If the directory has gone away due to an expire |
| 294 | * we have been called as ->d_revalidate() and so |
| 295 | * we need to return false and proceed to ->lookup(). |
| 296 | */ |
| 297 | if (autofs4_expire_wait(dentry) == -EAGAIN) |
| 298 | return 0; |
| 299 | |
| 300 | /* |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 301 | * A zero status is success otherwise we have a |
| 302 | * negative error code. |
| 303 | */ |
| 304 | status = try_to_fill_dentry(dentry, flags); |
| 305 | if (status == 0) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 306 | return 1; |
| 307 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 308 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 310 | spin_unlock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* Negative dentry.. invalidate if "old" */ |
| 313 | if (dentry->d_inode == NULL) |
Ian Kent | 2d753e6 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 314 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | /* Check for a non-mountpoint directory with no contents */ |
| 317 | spin_lock(&dcache_lock); |
| 318 | if (S_ISDIR(dentry->d_inode->i_mode) && |
| 319 | !d_mountpoint(dentry) && |
Ian Kent | 90a59c7 | 2006-03-27 01:14:50 -0800 | [diff] [blame] | 320 | __simple_empty(dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | DPRINTK("dentry=%p %.*s, emptydir", |
| 322 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 323 | spin_unlock(&dcache_lock); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 324 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 325 | /* The daemon never causes a mount to trigger */ |
| 326 | if (oz_mode) |
| 327 | return 1; |
| 328 | |
| 329 | /* |
| 330 | * A zero status is success otherwise we have a |
| 331 | * negative error code. |
| 332 | */ |
| 333 | status = try_to_fill_dentry(dentry, flags); |
| 334 | if (status == 0) |
| 335 | return 1; |
| 336 | |
| 337 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | spin_unlock(&dcache_lock); |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return 1; |
| 342 | } |
| 343 | |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 344 | void autofs4_dentry_release(struct dentry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | struct autofs_info *inf; |
| 347 | |
| 348 | DPRINTK("releasing %p", de); |
| 349 | |
| 350 | inf = autofs4_dentry_ino(de); |
| 351 | de->d_fsdata = NULL; |
| 352 | |
| 353 | if (inf) { |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 354 | struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb); |
| 355 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 356 | if (sbi) { |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 357 | spin_lock(&sbi->lookup_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 358 | if (!list_empty(&inf->active)) |
| 359 | list_del(&inf->active); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 360 | if (!list_empty(&inf->expiring)) |
| 361 | list_del(&inf->expiring); |
| 362 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Jeff Mahoney | c3724b1 | 2007-04-11 23:28:46 -0700 | [diff] [blame] | 365 | inf->dentry = NULL; |
| 366 | inf->inode = NULL; |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | autofs4_free_ino(inf); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* For dentries of directories in the root dir */ |
| 373 | static struct dentry_operations autofs4_root_dentry_operations = { |
| 374 | .d_revalidate = autofs4_revalidate, |
| 375 | .d_release = autofs4_dentry_release, |
| 376 | }; |
| 377 | |
| 378 | /* For other dentries */ |
| 379 | static struct dentry_operations autofs4_dentry_operations = { |
| 380 | .d_revalidate = autofs4_revalidate, |
| 381 | .d_release = autofs4_dentry_release, |
| 382 | }; |
| 383 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 384 | static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name) |
| 385 | { |
| 386 | unsigned int len = name->len; |
| 387 | unsigned int hash = name->hash; |
| 388 | const unsigned char *str = name->name; |
| 389 | struct list_head *p, *head; |
| 390 | |
| 391 | spin_lock(&dcache_lock); |
| 392 | spin_lock(&sbi->lookup_lock); |
| 393 | head = &sbi->active_list; |
| 394 | list_for_each(p, head) { |
| 395 | struct autofs_info *ino; |
| 396 | struct dentry *dentry; |
| 397 | struct qstr *qstr; |
| 398 | |
| 399 | ino = list_entry(p, struct autofs_info, active); |
| 400 | dentry = ino->dentry; |
| 401 | |
| 402 | spin_lock(&dentry->d_lock); |
| 403 | |
| 404 | /* Already gone? */ |
| 405 | if (atomic_read(&dentry->d_count) == 0) |
| 406 | goto next; |
| 407 | |
| 408 | qstr = &dentry->d_name; |
| 409 | |
| 410 | if (dentry->d_name.hash != hash) |
| 411 | goto next; |
| 412 | if (dentry->d_parent != parent) |
| 413 | goto next; |
| 414 | |
| 415 | if (qstr->len != len) |
| 416 | goto next; |
| 417 | if (memcmp(qstr->name, str, len)) |
| 418 | goto next; |
| 419 | |
| 420 | if (d_unhashed(dentry)) { |
| 421 | dget(dentry); |
| 422 | spin_unlock(&dentry->d_lock); |
| 423 | spin_unlock(&sbi->lookup_lock); |
| 424 | spin_unlock(&dcache_lock); |
| 425 | return dentry; |
| 426 | } |
| 427 | next: |
| 428 | spin_unlock(&dentry->d_lock); |
| 429 | } |
| 430 | spin_unlock(&sbi->lookup_lock); |
| 431 | spin_unlock(&dcache_lock); |
| 432 | |
| 433 | return NULL; |
| 434 | } |
| 435 | |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 436 | static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 437 | { |
| 438 | unsigned int len = name->len; |
| 439 | unsigned int hash = name->hash; |
| 440 | const unsigned char *str = name->name; |
| 441 | struct list_head *p, *head; |
| 442 | |
| 443 | spin_lock(&dcache_lock); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 444 | spin_lock(&sbi->lookup_lock); |
| 445 | head = &sbi->expiring_list; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 446 | list_for_each(p, head) { |
| 447 | struct autofs_info *ino; |
| 448 | struct dentry *dentry; |
| 449 | struct qstr *qstr; |
| 450 | |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 451 | ino = list_entry(p, struct autofs_info, expiring); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 452 | dentry = ino->dentry; |
| 453 | |
| 454 | spin_lock(&dentry->d_lock); |
| 455 | |
| 456 | /* Bad luck, we've already been dentry_iput */ |
| 457 | if (!dentry->d_inode) |
| 458 | goto next; |
| 459 | |
| 460 | qstr = &dentry->d_name; |
| 461 | |
| 462 | if (dentry->d_name.hash != hash) |
| 463 | goto next; |
| 464 | if (dentry->d_parent != parent) |
| 465 | goto next; |
| 466 | |
| 467 | if (qstr->len != len) |
| 468 | goto next; |
| 469 | if (memcmp(qstr->name, str, len)) |
| 470 | goto next; |
| 471 | |
| 472 | if (d_unhashed(dentry)) { |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 473 | dget(dentry); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 474 | spin_unlock(&dentry->d_lock); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 475 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 476 | spin_unlock(&dcache_lock); |
| 477 | return dentry; |
| 478 | } |
| 479 | next: |
| 480 | spin_unlock(&dentry->d_lock); |
| 481 | } |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 482 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 483 | spin_unlock(&dcache_lock); |
| 484 | |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | /* Lookups in the root directory */ |
| 489 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 490 | { |
| 491 | struct autofs_sb_info *sbi; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 492 | struct autofs_info *ino; |
| 493 | struct dentry *expiring, *unhashed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | int oz_mode; |
| 495 | |
| 496 | DPRINTK("name = %.*s", |
| 497 | dentry->d_name.len, dentry->d_name.name); |
| 498 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 499 | /* File name too long to exist */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (dentry->d_name.len > NAME_MAX) |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 501 | return ERR_PTR(-ENAMETOOLONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | sbi = autofs4_sbi(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | oz_mode = autofs4_oz_mode(sbi); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 507 | current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 509 | expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name); |
| 510 | if (expiring) { |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 511 | /* |
| 512 | * If we are racing with expire the request might not |
| 513 | * be quite complete but the directory has been removed |
| 514 | * so it must have been successful, so just wait for it. |
| 515 | */ |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 516 | ino = autofs4_dentry_ino(expiring); |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame^] | 517 | autofs4_expire_wait(expiring); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 518 | spin_lock(&sbi->lookup_lock); |
| 519 | if (!list_empty(&ino->expiring)) |
| 520 | list_del_init(&ino->expiring); |
| 521 | spin_unlock(&sbi->lookup_lock); |
| 522 | dput(expiring); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 525 | unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name); |
| 526 | if (unhashed) |
| 527 | dentry = unhashed; |
| 528 | else { |
| 529 | /* |
| 530 | * Mark the dentry incomplete but don't hash it. We do this |
| 531 | * to serialize our inode creation operations (symlink and |
| 532 | * mkdir) which prevents deadlock during the callback to |
| 533 | * the daemon. Subsequent user space lookups for the same |
| 534 | * dentry are placed on the wait queue while the daemon |
| 535 | * itself is allowed passage unresticted so the create |
| 536 | * operation itself can then hash the dentry. Finally, |
| 537 | * we check for the hashed dentry and return the newly |
| 538 | * hashed dentry. |
| 539 | */ |
| 540 | dentry->d_op = &autofs4_root_dentry_operations; |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 541 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 542 | /* |
| 543 | * And we need to ensure that the same dentry is used for |
| 544 | * all following lookup calls until it is hashed so that |
| 545 | * the dentry flags are persistent throughout the request. |
| 546 | */ |
| 547 | ino = autofs4_init_ino(NULL, sbi, 0555); |
| 548 | if (!ino) |
| 549 | return ERR_PTR(-ENOMEM); |
| 550 | |
| 551 | dentry->d_fsdata = ino; |
| 552 | ino->dentry = dentry; |
| 553 | |
| 554 | spin_lock(&sbi->lookup_lock); |
| 555 | list_add(&ino->active, &sbi->active_list); |
| 556 | spin_unlock(&sbi->lookup_lock); |
| 557 | |
| 558 | d_instantiate(dentry, NULL); |
| 559 | } |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | if (!oz_mode) { |
| 562 | spin_lock(&dentry->d_lock); |
| 563 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; |
| 564 | spin_unlock(&dentry->d_lock); |
Ian Kent | c432c25 | 2008-07-23 21:30:14 -0700 | [diff] [blame] | 565 | if (dentry->d_op && dentry->d_op->d_revalidate) { |
| 566 | mutex_unlock(&dir->i_mutex); |
| 567 | (dentry->d_op->d_revalidate)(dentry, nd); |
| 568 | mutex_lock(&dir->i_mutex); |
| 569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* |
| 573 | * If we are still pending, check if we had to handle |
| 574 | * a signal. If so we can force a restart.. |
| 575 | */ |
| 576 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { |
| 577 | /* See if we were interrupted */ |
| 578 | if (signal_pending(current)) { |
| 579 | sigset_t *sigset = ¤t->pending.signal; |
| 580 | if (sigismember (sigset, SIGKILL) || |
| 581 | sigismember (sigset, SIGQUIT) || |
| 582 | sigismember (sigset, SIGINT)) { |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 583 | if (unhashed) |
| 584 | dput(unhashed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return ERR_PTR(-ERESTARTNOINTR); |
| 586 | } |
| 587 | } |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 588 | if (!oz_mode) { |
| 589 | spin_lock(&dentry->d_lock); |
| 590 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 591 | spin_unlock(&dentry->d_lock); |
| 592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* |
| 596 | * If this dentry is unhashed, then we shouldn't honour this |
Ian Kent | c9ffec4 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 597 | * lookup. Returning ENOENT here doesn't do the right thing |
| 598 | * for all system calls, but it should be OK for the operations |
| 599 | * we permit from an autofs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | */ |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 601 | if (!oz_mode && d_unhashed(dentry)) { |
Ian Kent | c9ffec4 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 602 | /* |
| 603 | * A user space application can (and has done in the past) |
| 604 | * remove and re-create this directory during the callback. |
| 605 | * This can leave us with an unhashed dentry, but a |
| 606 | * successful mount! So we need to perform another |
| 607 | * cached lookup in case the dentry now exists. |
| 608 | */ |
| 609 | struct dentry *parent = dentry->d_parent; |
| 610 | struct dentry *new = d_lookup(parent, &dentry->d_name); |
| 611 | if (new != NULL) |
| 612 | dentry = new; |
| 613 | else |
| 614 | dentry = ERR_PTR(-ENOENT); |
| 615 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 616 | if (unhashed) |
| 617 | dput(unhashed); |
| 618 | |
Ian Kent | c9ffec4 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 619 | return dentry; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 622 | if (unhashed) |
| 623 | return unhashed; |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | static int autofs4_dir_symlink(struct inode *dir, |
| 629 | struct dentry *dentry, |
| 630 | const char *symname) |
| 631 | { |
| 632 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 633 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 634 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | struct inode *inode; |
| 636 | char *cp; |
| 637 | |
| 638 | DPRINTK("%s <- %.*s", symname, |
| 639 | dentry->d_name.len, dentry->d_name.name); |
| 640 | |
| 641 | if (!autofs4_oz_mode(sbi)) |
| 642 | return -EACCES; |
| 643 | |
| 644 | ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 645 | if (!ino) |
| 646 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 648 | spin_lock(&sbi->lookup_lock); |
| 649 | if (!list_empty(&ino->active)) |
| 650 | list_del_init(&ino->active); |
| 651 | spin_unlock(&sbi->lookup_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Ian Kent | ef581a7 | 2008-07-23 21:30:13 -0700 | [diff] [blame] | 653 | ino->size = strlen(symname); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 654 | cp = kmalloc(ino->size + 1, GFP_KERNEL); |
| 655 | if (!cp) { |
| 656 | if (!dentry->d_fsdata) |
| 657 | kfree(ino); |
| 658 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | strcpy(cp, symname); |
| 662 | |
| 663 | inode = autofs4_get_inode(dir->i_sb, ino); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 664 | if (!inode) { |
| 665 | kfree(cp); |
| 666 | if (!dentry->d_fsdata) |
| 667 | kfree(ino); |
| 668 | return -ENOMEM; |
| 669 | } |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 670 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | if (dir == dir->i_sb->s_root->d_inode) |
| 673 | dentry->d_op = &autofs4_root_dentry_operations; |
| 674 | else |
| 675 | dentry->d_op = &autofs4_dentry_operations; |
| 676 | |
| 677 | dentry->d_fsdata = ino; |
| 678 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 679 | atomic_inc(&ino->count); |
| 680 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 681 | if (p_ino && dentry->d_parent != dentry) |
| 682 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | ino->inode = inode; |
| 684 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 685 | ino->u.symlink = cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | dir->i_mtime = CURRENT_TIME; |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * NOTE! |
| 693 | * |
| 694 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache |
| 695 | * that the file no longer exists. However, doing that means that the |
| 696 | * VFS layer can turn the dentry into a negative dentry. We don't want |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 697 | * this, because the unlink is probably the result of an expire. |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 698 | * We simply d_drop it and add it to a expiring list in the super block, |
| 699 | * which allows the dentry lookup to check for an incomplete expire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | * |
| 701 | * If a process is blocked on the dentry waiting for the expire to finish, |
| 702 | * it will invalidate the dentry and try to mount with a new one. |
| 703 | * |
| 704 | * Also see autofs4_dir_rmdir().. |
| 705 | */ |
| 706 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) |
| 707 | { |
| 708 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 709 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 710 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | /* This allows root to remove symlinks */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 713 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | return -EACCES; |
| 715 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 716 | if (atomic_dec_and_test(&ino->count)) { |
| 717 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 718 | if (p_ino && dentry->d_parent != dentry) |
| 719 | atomic_dec(&p_ino->count); |
| 720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | dput(ino->dentry); |
| 722 | |
| 723 | dentry->d_inode->i_size = 0; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 724 | clear_nlink(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
| 726 | dir->i_mtime = CURRENT_TIME; |
| 727 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 728 | spin_lock(&dcache_lock); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 729 | spin_lock(&sbi->lookup_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 730 | if (list_empty(&ino->expiring)) |
| 731 | list_add(&ino->expiring, &sbi->expiring_list); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 732 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 733 | spin_lock(&dentry->d_lock); |
| 734 | __d_drop(dentry); |
| 735 | spin_unlock(&dentry->d_lock); |
| 736 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) |
| 742 | { |
| 743 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 744 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 745 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 747 | DPRINTK("dentry %p, removing %.*s", |
| 748 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | if (!autofs4_oz_mode(sbi)) |
| 751 | return -EACCES; |
| 752 | |
| 753 | spin_lock(&dcache_lock); |
| 754 | if (!list_empty(&dentry->d_subdirs)) { |
| 755 | spin_unlock(&dcache_lock); |
| 756 | return -ENOTEMPTY; |
| 757 | } |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 758 | spin_lock(&sbi->lookup_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 759 | if (list_empty(&ino->expiring)) |
| 760 | list_add(&ino->expiring, &sbi->expiring_list); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 761 | spin_unlock(&sbi->lookup_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | spin_lock(&dentry->d_lock); |
| 763 | __d_drop(dentry); |
| 764 | spin_unlock(&dentry->d_lock); |
| 765 | spin_unlock(&dcache_lock); |
| 766 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 767 | if (atomic_dec_and_test(&ino->count)) { |
| 768 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 769 | if (p_ino && dentry->d_parent != dentry) |
| 770 | atomic_dec(&p_ino->count); |
| 771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | dput(ino->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | dentry->d_inode->i_size = 0; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 774 | clear_nlink(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | if (dir->i_nlink) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 777 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 783 | { |
| 784 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 785 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 786 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | struct inode *inode; |
| 788 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 789 | if (!autofs4_oz_mode(sbi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | return -EACCES; |
| 791 | |
| 792 | DPRINTK("dentry %p, creating %.*s", |
| 793 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 794 | |
| 795 | ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 796 | if (!ino) |
| 797 | return -ENOMEM; |
| 798 | |
| 799 | spin_lock(&sbi->lookup_lock); |
| 800 | if (!list_empty(&ino->active)) |
| 801 | list_del_init(&ino->active); |
| 802 | spin_unlock(&sbi->lookup_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | inode = autofs4_get_inode(dir->i_sb, ino); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 805 | if (!inode) { |
| 806 | if (!dentry->d_fsdata) |
| 807 | kfree(ino); |
| 808 | return -ENOMEM; |
| 809 | } |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 810 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
| 812 | if (dir == dir->i_sb->s_root->d_inode) |
| 813 | dentry->d_op = &autofs4_root_dentry_operations; |
| 814 | else |
| 815 | dentry->d_op = &autofs4_dentry_operations; |
| 816 | |
| 817 | dentry->d_fsdata = ino; |
| 818 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 819 | atomic_inc(&ino->count); |
| 820 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 821 | if (p_ino && dentry->d_parent != dentry) |
| 822 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | ino->inode = inode; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 824 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | dir->i_mtime = CURRENT_TIME; |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | /* Get/set timeout ioctl() operation */ |
| 831 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, |
| 832 | unsigned long __user *p) |
| 833 | { |
| 834 | int rv; |
| 835 | unsigned long ntimeout; |
| 836 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 837 | if ((rv = get_user(ntimeout, p)) || |
| 838 | (rv = put_user(sbi->exp_timeout/HZ, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return rv; |
| 840 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 841 | if (ntimeout > ULONG_MAX/HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | sbi->exp_timeout = 0; |
| 843 | else |
| 844 | sbi->exp_timeout = ntimeout * HZ; |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* Return protocol version */ |
| 850 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) |
| 851 | { |
| 852 | return put_user(sbi->version, p); |
| 853 | } |
| 854 | |
| 855 | /* Return protocol sub version */ |
| 856 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) |
| 857 | { |
| 858 | return put_user(sbi->sub_version, p); |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Tells the daemon whether we need to reghost or not. Also, clears |
| 863 | * the reghost_needed flag. |
| 864 | */ |
| 865 | static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p) |
| 866 | { |
| 867 | int status; |
| 868 | |
| 869 | DPRINTK("returning %d", sbi->needs_reghost); |
| 870 | |
| 871 | status = put_user(sbi->needs_reghost, p); |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 872 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | return status; |
| 874 | |
| 875 | sbi->needs_reghost = 0; |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Enable / Disable reghosting ioctl() operation |
| 881 | */ |
| 882 | static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p) |
| 883 | { |
| 884 | int status; |
| 885 | int val; |
| 886 | |
| 887 | status = get_user(val, p); |
| 888 | |
| 889 | DPRINTK("reghost = %d", val); |
| 890 | |
| 891 | if (status) |
| 892 | return status; |
| 893 | |
| 894 | /* turn on/off reghosting, with the val */ |
| 895 | sbi->reghost_enabled = val; |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Tells the daemon whether it can umount the autofs mount. |
| 901 | */ |
| 902 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) |
| 903 | { |
| 904 | int status = 0; |
| 905 | |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 906 | if (may_umount(mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | status = 1; |
| 908 | |
| 909 | DPRINTK("returning %d", status); |
| 910 | |
| 911 | status = put_user(status, p); |
| 912 | |
| 913 | return status; |
| 914 | } |
| 915 | |
| 916 | /* Identify autofs4_dentries - this is so we can tell if there's |
| 917 | an extra dentry refcount or not. We only hold a refcount on the |
| 918 | dentry if its non-negative (ie, d_inode != NULL) |
| 919 | */ |
| 920 | int is_autofs4_dentry(struct dentry *dentry) |
| 921 | { |
| 922 | return dentry && dentry->d_inode && |
| 923 | (dentry->d_op == &autofs4_root_dentry_operations || |
| 924 | dentry->d_op == &autofs4_dentry_operations) && |
| 925 | dentry->d_fsdata != NULL; |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * ioctl()'s on the root directory is the chief method for the daemon to |
| 930 | * generate kernel reactions |
| 931 | */ |
| 932 | static int autofs4_root_ioctl(struct inode *inode, struct file *filp, |
| 933 | unsigned int cmd, unsigned long arg) |
| 934 | { |
| 935 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); |
| 936 | void __user *p = (void __user *)arg; |
| 937 | |
| 938 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 939 | cmd,arg,sbi,task_pgrp_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 941 | if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || |
| 942 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | return -ENOTTY; |
| 944 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 945 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return -EPERM; |
| 947 | |
| 948 | switch(cmd) { |
| 949 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ |
| 950 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); |
| 951 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ |
| 952 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); |
| 953 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ |
| 954 | autofs4_catatonic_mode(sbi); |
| 955 | return 0; |
| 956 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ |
| 957 | return autofs4_get_protover(sbi, p); |
| 958 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ |
| 959 | return autofs4_get_protosubver(sbi, p); |
| 960 | case AUTOFS_IOC_SETTIMEOUT: |
| 961 | return autofs4_get_set_timeout(sbi, p); |
| 962 | |
| 963 | case AUTOFS_IOC_TOGGLEREGHOST: |
| 964 | return autofs4_toggle_reghost(sbi, p); |
| 965 | case AUTOFS_IOC_ASKREGHOST: |
| 966 | return autofs4_ask_reghost(sbi, p); |
| 967 | |
| 968 | case AUTOFS_IOC_ASKUMOUNT: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 969 | return autofs4_ask_umount(filp->f_path.mnt, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | /* return a single thing to expire */ |
| 972 | case AUTOFS_IOC_EXPIRE: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 973 | return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | /* same as above, but can send multiple expires through pipe */ |
| 975 | case AUTOFS_IOC_EXPIRE_MULTI: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 976 | return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
| 978 | default: |
| 979 | return -ENOSYS; |
| 980 | } |
| 981 | } |