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