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 | |
| 55 | static void autofs_read_inode(struct inode *inode); |
| 56 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 57 | static const struct super_operations autofs_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | .read_inode = autofs_read_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | .statfs = simple_statfs, |
| 60 | }; |
| 61 | |
| 62 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto}; |
| 63 | |
| 64 | static match_table_t autofs_tokens = { |
| 65 | {Opt_fd, "fd=%u"}, |
| 66 | {Opt_uid, "uid=%u"}, |
| 67 | {Opt_gid, "gid=%u"}, |
| 68 | {Opt_pgrp, "pgrp=%u"}, |
| 69 | {Opt_minproto, "minproto=%u"}, |
| 70 | {Opt_maxproto, "maxproto=%u"}, |
| 71 | {Opt_err, NULL} |
| 72 | }; |
| 73 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 74 | static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, |
| 75 | pid_t *pgrp, int *minproto, int *maxproto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | char *p; |
| 78 | substring_t args[MAX_OPT_ARGS]; |
| 79 | int option; |
| 80 | |
| 81 | *uid = current->uid; |
| 82 | *gid = current->gid; |
| 83 | *pgrp = process_group(current); |
| 84 | |
| 85 | *minproto = *maxproto = AUTOFS_PROTO_VERSION; |
| 86 | |
| 87 | *pipefd = -1; |
| 88 | |
| 89 | if (!options) |
| 90 | return 1; |
| 91 | |
| 92 | while ((p = strsep(&options, ",")) != NULL) { |
| 93 | int token; |
| 94 | if (!*p) |
| 95 | continue; |
| 96 | |
| 97 | token = match_token(p, autofs_tokens, args); |
| 98 | switch (token) { |
| 99 | case Opt_fd: |
| 100 | if (match_int(&args[0], &option)) |
| 101 | return 1; |
| 102 | *pipefd = option; |
| 103 | break; |
| 104 | case Opt_uid: |
| 105 | if (match_int(&args[0], &option)) |
| 106 | return 1; |
| 107 | *uid = option; |
| 108 | break; |
| 109 | case Opt_gid: |
| 110 | if (match_int(&args[0], &option)) |
| 111 | return 1; |
| 112 | *gid = option; |
| 113 | break; |
| 114 | case Opt_pgrp: |
| 115 | if (match_int(&args[0], &option)) |
| 116 | return 1; |
| 117 | *pgrp = option; |
| 118 | break; |
| 119 | case Opt_minproto: |
| 120 | if (match_int(&args[0], &option)) |
| 121 | return 1; |
| 122 | *minproto = option; |
| 123 | break; |
| 124 | case Opt_maxproto: |
| 125 | if (match_int(&args[0], &option)) |
| 126 | return 1; |
| 127 | *maxproto = option; |
| 128 | break; |
| 129 | default: |
| 130 | return 1; |
| 131 | } |
| 132 | } |
| 133 | return (*pipefd < 0); |
| 134 | } |
| 135 | |
| 136 | int autofs_fill_super(struct super_block *s, void *data, int silent) |
| 137 | { |
| 138 | struct inode * root_inode; |
| 139 | struct dentry * root; |
| 140 | struct file * pipe; |
| 141 | int pipefd; |
| 142 | struct autofs_sb_info *sbi; |
| 143 | int minproto, maxproto; |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 144 | pid_t pgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 146 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 147 | if (!sbi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | goto fail_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | DPRINTK(("autofs: starting up, sbi = %p\n",sbi)); |
| 150 | |
| 151 | s->s_fs_info = sbi; |
| 152 | sbi->magic = AUTOFS_SBI_MAGIC; |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 153 | sbi->pipe = NULL; |
| 154 | sbi->catatonic = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | sbi->exp_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | autofs_initialize_hash(&sbi->dirhash); |
| 157 | sbi->queues = NULL; |
| 158 | memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN); |
| 159 | sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO; |
| 160 | s->s_blocksize = 1024; |
| 161 | s->s_blocksize_bits = 10; |
| 162 | s->s_magic = AUTOFS_SUPER_MAGIC; |
| 163 | s->s_op = &autofs_sops; |
| 164 | s->s_time_gran = 1; |
Alexander Krizhanovsky | f76baf9 | 2005-09-09 13:01:59 -0700 | [diff] [blame] | 165 | sbi->sb = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | root_inode = iget(s, AUTOFS_ROOT_INO); |
| 168 | root = d_alloc_root(root_inode); |
| 169 | pipe = NULL; |
| 170 | |
| 171 | if (!root) |
| 172 | goto fail_iput; |
| 173 | |
| 174 | /* Can this call block? - WTF cares? s is locked. */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 175 | if (parse_options(data, &pipefd, &root_inode->i_uid, |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 176 | &root_inode->i_gid, &pgid, &minproto, |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 177 | &maxproto)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | printk("autofs: called with bogus options\n"); |
| 179 | goto fail_dput; |
| 180 | } |
| 181 | |
| 182 | /* Couldn't this be tested earlier? */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 183 | if (minproto > AUTOFS_PROTO_VERSION || |
| 184 | maxproto < AUTOFS_PROTO_VERSION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | printk("autofs: kernel does not match daemon version\n"); |
| 186 | goto fail_dput; |
| 187 | } |
| 188 | |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 189 | DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, pgid)); |
| 190 | sbi->oz_pgrp = find_get_pid(pgid); |
| 191 | |
| 192 | if (!sbi->oz_pgrp) { |
| 193 | printk("autofs: could not find process group %d\n", pgid); |
| 194 | goto fail_dput; |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | pipe = fget(pipefd); |
| 198 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 199 | if (!pipe) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | printk("autofs: could not open pipe file descriptor\n"); |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 201 | goto fail_put_pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 203 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 204 | if (!pipe->f_op || !pipe->f_op->write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | goto fail_fput; |
| 206 | sbi->pipe = pipe; |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 207 | sbi->catatonic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Success! Install the root dentry now to indicate completion. |
| 211 | */ |
| 212 | s->s_root = root; |
| 213 | return 0; |
| 214 | |
| 215 | fail_fput: |
| 216 | printk("autofs: pipe file descriptor does not contain proper ops\n"); |
| 217 | fput(pipe); |
Sukadev Bhattiprolu | fa0334f | 2007-05-10 22:23:08 -0700 | [diff] [blame] | 218 | fail_put_pid: |
| 219 | put_pid(sbi->oz_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | fail_dput: |
| 221 | dput(root); |
| 222 | goto fail_free; |
| 223 | fail_iput: |
| 224 | printk("autofs: get root dentry failed\n"); |
| 225 | iput(root_inode); |
| 226 | fail_free: |
| 227 | kfree(sbi); |
Ian Kent | ba8df43 | 2006-11-14 02:03:29 -0800 | [diff] [blame] | 228 | s->s_fs_info = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | fail_unlock: |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | static void autofs_read_inode(struct inode *inode) |
| 234 | { |
| 235 | ino_t ino = inode->i_ino; |
| 236 | unsigned int n; |
| 237 | struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb); |
| 238 | |
| 239 | /* Initialize to the default case (stub directory) */ |
| 240 | |
| 241 | inode->i_op = &simple_dir_inode_operations; |
| 242 | inode->i_fop = &simple_dir_operations; |
| 243 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 244 | inode->i_nlink = 2; |
| 245 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 246 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 248 | if (ino == AUTOFS_ROOT_INO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; |
| 250 | inode->i_op = &autofs_root_inode_operations; |
| 251 | inode->i_fop = &autofs_root_operations; |
| 252 | inode->i_uid = inode->i_gid = 0; /* Changed in read_super */ |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | inode->i_uid = inode->i_sb->s_root->d_inode->i_uid; |
| 257 | inode->i_gid = inode->i_sb->s_root->d_inode->i_gid; |
| 258 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 259 | if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* Symlink inode - should be in symlink list */ |
| 261 | struct autofs_symlink *sl; |
| 262 | |
| 263 | n = ino - AUTOFS_FIRST_SYMLINK; |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 264 | if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | inode->i_op = &autofs_symlink_inode_operations; |
| 270 | sl = &sbi->symlink[n]; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 271 | inode->i_private = sl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | inode->i_mode = S_IFLNK | S_IRWXUGO; |
| 273 | inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime; |
| 274 | inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; |
| 275 | inode->i_size = sl->len; |
| 276 | inode->i_nlink = 1; |
| 277 | } |
| 278 | } |