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