Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- --------------------------------------------------------- * |
| 2 | * |
| 3 | * linux/fs/autofs/inode.c |
| 4 | * |
| 5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved |
| 6 | * |
| 7 | * This file is part of the Linux kernel and is made available under |
| 8 | * the terms of the GNU General Public License, version 2, or at your |
| 9 | * option, any later version, incorporated herein by reference. |
| 10 | * |
| 11 | * ------------------------------------------------------------------------- */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/file.h> |
| 17 | #include <linux/parser.h> |
| 18 | #include <linux/bitops.h> |
Jeff Garzik | e18fa70 | 2006-09-24 11:13:19 -0400 | [diff] [blame] | 19 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "autofs_i.h" |
| 21 | #include <linux/module.h> |
| 22 | |
David Howells | 0e7d738 | 2006-10-19 23:28:36 -0700 | [diff] [blame] | 23 | void autofs_kill_sb(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | struct autofs_sb_info *sbi = autofs_sbi(sb); |
| 26 | unsigned int n; |
| 27 | |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 28 | /* |
| 29 | * In the event of a failure in get_sb_nodev the superblock |
| 30 | * info is not present so nothing else has been setup, so |
Jiri Kosina | c949d4e | 2006-12-06 20:39:38 -0800 | [diff] [blame] | 31 | * just call kill_anon_super when we are called from |
| 32 | * deactivate_super. |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 33 | */ |
| 34 | if (!sbi) |
Jiri Kosina | c949d4e | 2006-12-06 20:39:38 -0800 | [diff] [blame] | 35 | goto out_kill_sb; |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 36 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 37 | if (!sbi->catatonic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */ |
| 39 | |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 40 | put_pid(sbi->oz_pgrp); |
| 41 | |
Alexander Krizhanovsky | f76baf9 | 2005-09-09 13:01:59 -0700 | [diff] [blame] | 42 | autofs_hash_nuke(sbi); |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 43 | for (n = 0; n < AUTOFS_MAX_SYMLINKS; n++) { |
| 44 | if (test_bit(n, sbi->symlink_bitmap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | kfree(sbi->symlink[n].data); |
| 46 | } |
| 47 | |
| 48 | kfree(sb->s_fs_info); |
| 49 | |
Jiri Kosina | c949d4e | 2006-12-06 20:39:38 -0800 | [diff] [blame] | 50 | out_kill_sb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | DPRINTK(("autofs: shutting down\n")); |
David Howells | 0e7d738 | 2006-10-19 23:28:36 -0700 | [diff] [blame] | 52 | kill_anon_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 55 | static const struct super_operations autofs_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | .statfs = simple_statfs, |
| 57 | }; |
| 58 | |
| 59 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto}; |
| 60 | |
| 61 | static match_table_t autofs_tokens = { |
| 62 | {Opt_fd, "fd=%u"}, |
| 63 | {Opt_uid, "uid=%u"}, |
| 64 | {Opt_gid, "gid=%u"}, |
| 65 | {Opt_pgrp, "pgrp=%u"}, |
| 66 | {Opt_minproto, "minproto=%u"}, |
| 67 | {Opt_maxproto, "maxproto=%u"}, |
| 68 | {Opt_err, NULL} |
| 69 | }; |
| 70 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 71 | static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, |
| 72 | pid_t *pgrp, int *minproto, int *maxproto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | char *p; |
| 75 | substring_t args[MAX_OPT_ARGS]; |
| 76 | int option; |
| 77 | |
| 78 | *uid = current->uid; |
| 79 | *gid = current->gid; |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 80 | *pgrp = task_pgrp_nr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | *minproto = *maxproto = AUTOFS_PROTO_VERSION; |
| 83 | |
| 84 | *pipefd = -1; |
| 85 | |
| 86 | if (!options) |
| 87 | return 1; |
| 88 | |
| 89 | while ((p = strsep(&options, ",")) != NULL) { |
| 90 | int token; |
| 91 | if (!*p) |
| 92 | continue; |
| 93 | |
| 94 | token = match_token(p, autofs_tokens, args); |
| 95 | switch (token) { |
| 96 | case Opt_fd: |
| 97 | if (match_int(&args[0], &option)) |
| 98 | return 1; |
| 99 | *pipefd = option; |
| 100 | break; |
| 101 | case Opt_uid: |
| 102 | if (match_int(&args[0], &option)) |
| 103 | return 1; |
| 104 | *uid = option; |
| 105 | break; |
| 106 | case Opt_gid: |
| 107 | if (match_int(&args[0], &option)) |
| 108 | return 1; |
| 109 | *gid = option; |
| 110 | break; |
| 111 | case Opt_pgrp: |
| 112 | if (match_int(&args[0], &option)) |
| 113 | return 1; |
| 114 | *pgrp = option; |
| 115 | break; |
| 116 | case Opt_minproto: |
| 117 | if (match_int(&args[0], &option)) |
| 118 | return 1; |
| 119 | *minproto = option; |
| 120 | break; |
| 121 | case Opt_maxproto: |
| 122 | if (match_int(&args[0], &option)) |
| 123 | return 1; |
| 124 | *maxproto = option; |
| 125 | break; |
| 126 | default: |
| 127 | return 1; |
| 128 | } |
| 129 | } |
| 130 | return (*pipefd < 0); |
| 131 | } |
| 132 | |
| 133 | int autofs_fill_super(struct super_block *s, void *data, int silent) |
| 134 | { |
| 135 | struct inode * root_inode; |
| 136 | struct dentry * root; |
| 137 | struct file * pipe; |
| 138 | int pipefd; |
| 139 | struct autofs_sb_info *sbi; |
| 140 | int minproto, maxproto; |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 141 | pid_t pgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 143 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 144 | if (!sbi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | goto fail_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | DPRINTK(("autofs: starting up, sbi = %p\n",sbi)); |
| 147 | |
| 148 | s->s_fs_info = sbi; |
| 149 | sbi->magic = AUTOFS_SBI_MAGIC; |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 150 | sbi->pipe = NULL; |
| 151 | sbi->catatonic = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | sbi->exp_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | autofs_initialize_hash(&sbi->dirhash); |
| 154 | sbi->queues = NULL; |
| 155 | memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN); |
| 156 | sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO; |
| 157 | s->s_blocksize = 1024; |
| 158 | s->s_blocksize_bits = 10; |
| 159 | s->s_magic = AUTOFS_SUPER_MAGIC; |
| 160 | s->s_op = &autofs_sops; |
| 161 | s->s_time_gran = 1; |
Alexander Krizhanovsky | f76baf9 | 2005-09-09 13:01:59 -0700 | [diff] [blame] | 162 | sbi->sb = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
David Howells | 62328a0 | 2008-02-07 00:15:30 -0800 | [diff] [blame^] | 164 | root_inode = autofs_iget(s, AUTOFS_ROOT_INO); |
| 165 | if (IS_ERR(root_inode)) |
| 166 | goto fail_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | root = d_alloc_root(root_inode); |
| 168 | pipe = NULL; |
| 169 | |
| 170 | if (!root) |
| 171 | goto fail_iput; |
| 172 | |
| 173 | /* Can this call block? - WTF cares? s is locked. */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 174 | if (parse_options(data, &pipefd, &root_inode->i_uid, |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 175 | &root_inode->i_gid, &pgid, &minproto, |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 176 | &maxproto)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | printk("autofs: called with bogus options\n"); |
| 178 | goto fail_dput; |
| 179 | } |
| 180 | |
| 181 | /* Couldn't this be tested earlier? */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 182 | if (minproto > AUTOFS_PROTO_VERSION || |
| 183 | maxproto < AUTOFS_PROTO_VERSION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | printk("autofs: kernel does not match daemon version\n"); |
| 185 | goto fail_dput; |
| 186 | } |
| 187 | |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 188 | DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, pgid)); |
| 189 | sbi->oz_pgrp = find_get_pid(pgid); |
| 190 | |
| 191 | if (!sbi->oz_pgrp) { |
| 192 | printk("autofs: could not find process group %d\n", pgid); |
| 193 | goto fail_dput; |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | pipe = fget(pipefd); |
| 197 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 198 | if (!pipe) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | printk("autofs: could not open pipe file descriptor\n"); |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 200 | goto fail_put_pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 202 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 203 | if (!pipe->f_op || !pipe->f_op->write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | goto fail_fput; |
| 205 | sbi->pipe = pipe; |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 206 | sbi->catatonic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Success! Install the root dentry now to indicate completion. |
| 210 | */ |
| 211 | s->s_root = root; |
| 212 | return 0; |
| 213 | |
| 214 | fail_fput: |
| 215 | printk("autofs: pipe file descriptor does not contain proper ops\n"); |
| 216 | fput(pipe); |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 217 | fail_put_pid: |
| 218 | put_pid(sbi->oz_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | fail_dput: |
| 220 | dput(root); |
| 221 | goto fail_free; |
| 222 | fail_iput: |
| 223 | printk("autofs: get root dentry failed\n"); |
| 224 | iput(root_inode); |
| 225 | fail_free: |
| 226 | kfree(sbi); |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 227 | s->s_fs_info = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | fail_unlock: |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
David Howells | 62328a0 | 2008-02-07 00:15:30 -0800 | [diff] [blame^] | 232 | struct inode *autofs_iget(struct super_block *sb, unsigned long ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | unsigned int n; |
David Howells | 62328a0 | 2008-02-07 00:15:30 -0800 | [diff] [blame^] | 235 | struct autofs_sb_info *sbi = autofs_sbi(sb); |
| 236 | struct inode *inode; |
| 237 | |
| 238 | inode = iget_locked(sb, ino); |
| 239 | if (!inode) |
| 240 | return ERR_PTR(-ENOMEM); |
| 241 | if (!(inode->i_state & I_NEW)) |
| 242 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | /* Initialize to the default case (stub directory) */ |
| 245 | |
| 246 | inode->i_op = &simple_dir_inode_operations; |
| 247 | inode->i_fop = &simple_dir_operations; |
| 248 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 249 | inode->i_nlink = 2; |
| 250 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 251 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 253 | if (ino == AUTOFS_ROOT_INO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; |
| 255 | inode->i_op = &autofs_root_inode_operations; |
| 256 | inode->i_fop = &autofs_root_operations; |
| 257 | inode->i_uid = inode->i_gid = 0; /* Changed in read_super */ |
David Howells | 62328a0 | 2008-02-07 00:15:30 -0800 | [diff] [blame^] | 258 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | inode->i_uid = inode->i_sb->s_root->d_inode->i_uid; |
| 262 | inode->i_gid = inode->i_sb->s_root->d_inode->i_gid; |
| 263 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 264 | if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* Symlink inode - should be in symlink list */ |
| 266 | struct autofs_symlink *sl; |
| 267 | |
| 268 | n = ino - AUTOFS_FIRST_SYMLINK; |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 269 | if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino); |
David Howells | 62328a0 | 2008-02-07 00:15:30 -0800 | [diff] [blame^] | 271 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | inode->i_op = &autofs_symlink_inode_operations; |
| 275 | sl = &sbi->symlink[n]; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 276 | inode->i_private = sl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | inode->i_mode = S_IFLNK | S_IRWXUGO; |
| 278 | inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime; |
| 279 | inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; |
| 280 | inode->i_size = sl->len; |
| 281 | inode->i_nlink = 1; |
| 282 | } |
David Howells | 62328a0 | 2008-02-07 00:15:30 -0800 | [diff] [blame^] | 283 | |
| 284 | done: |
| 285 | unlock_new_inode(inode); |
| 286 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |