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