1 /* 2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved 3 * Copyright 2005-2006 Ian Kent <raven@themaw.net> 4 * 5 * This file is part of the Linux kernel and is made available under 6 * the terms of the GNU General Public License, version 2, or at your 7 * option, any later version, incorporated herein by reference. 8 */ 9 10 #include <linux/seq_file.h> 11 #include <linux/pagemap.h> 12 #include <linux/parser.h> 13 14 #include "autofs_i.h" 15 16 struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi) 17 { 18 struct autofs_info *ino; 19 20 ino = kzalloc(sizeof(*ino), GFP_KERNEL); 21 if (ino) { 22 INIT_LIST_HEAD(&ino->active); 23 INIT_LIST_HEAD(&ino->expiring); 24 ino->last_used = jiffies; 25 ino->sbi = sbi; 26 } 27 return ino; 28 } 29 30 void autofs_clean_ino(struct autofs_info *ino) 31 { 32 ino->uid = GLOBAL_ROOT_UID; 33 ino->gid = GLOBAL_ROOT_GID; 34 ino->last_used = jiffies; 35 } 36 37 void autofs_free_ino(struct autofs_info *ino) 38 { 39 kfree(ino); 40 } 41 42 void autofs_kill_sb(struct super_block *sb) 43 { 44 struct autofs_sb_info *sbi = autofs_sbi(sb); 45 46 /* 47 * In the event of a failure in get_sb_nodev the superblock 48 * info is not present so nothing else has been setup, so 49 * just call kill_anon_super when we are called from 50 * deactivate_super. 51 */ 52 if (sbi) { 53 /* Free wait queues, close pipe */ 54 autofs_catatonic_mode(sbi); 55 put_pid(sbi->oz_pgrp); 56 } 57 58 pr_debug("shutting down\n"); 59 kill_litter_super(sb); 60 if (sbi) 61 kfree_rcu(sbi, rcu); 62 } 63 64 static int autofs_show_options(struct seq_file *m, struct dentry *root) 65 { 66 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb); 67 struct inode *root_inode = d_inode(root->d_sb->s_root); 68 69 if (!sbi) 70 return 0; 71 72 seq_printf(m, ",fd=%d", sbi->pipefd); 73 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID)) 74 seq_printf(m, ",uid=%u", 75 from_kuid_munged(&init_user_ns, root_inode->i_uid)); 76 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID)) 77 seq_printf(m, ",gid=%u", 78 from_kgid_munged(&init_user_ns, root_inode->i_gid)); 79 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp)); 80 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ); 81 seq_printf(m, ",minproto=%d", sbi->min_proto); 82 seq_printf(m, ",maxproto=%d", sbi->max_proto); 83 84 if (autofs_type_offset(sbi->type)) 85 seq_printf(m, ",offset"); 86 else if (autofs_type_direct(sbi->type)) 87 seq_printf(m, ",direct"); 88 else 89 seq_printf(m, ",indirect"); 90 if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE) 91 seq_printf(m, ",strictexpire"); 92 #ifdef CONFIG_CHECKPOINT_RESTORE 93 if (sbi->pipe) 94 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino); 95 else 96 seq_printf(m, ",pipe_ino=-1"); 97 #endif 98 return 0; 99 } 100 101 static void autofs_evict_inode(struct inode *inode) 102 { 103 clear_inode(inode); 104 kfree(inode->i_private); 105 } 106 107 static const struct super_operations autofs_sops = { 108 .statfs = simple_statfs, 109 .show_options = autofs_show_options, 110 .evict_inode = autofs_evict_inode, 111 }; 112 113 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto, 114 Opt_indirect, Opt_direct, Opt_offset, Opt_strictexpire}; 115 116 static const match_table_t tokens = { 117 {Opt_fd, "fd=%u"}, 118 {Opt_uid, "uid=%u"}, 119 {Opt_gid, "gid=%u"}, 120 {Opt_pgrp, "pgrp=%u"}, 121 {Opt_minproto, "minproto=%u"}, 122 {Opt_maxproto, "maxproto=%u"}, 123 {Opt_indirect, "indirect"}, 124 {Opt_direct, "direct"}, 125 {Opt_offset, "offset"}, 126 {Opt_strictexpire, "strictexpire"}, 127 {Opt_err, NULL} 128 }; 129 130 static int parse_options(char *options, 131 struct inode *root, int *pgrp, bool *pgrp_set, 132 struct autofs_sb_info *sbi) 133 { 134 char *p; 135 substring_t args[MAX_OPT_ARGS]; 136 int option; 137 int pipefd = -1; 138 kuid_t uid; 139 kgid_t gid; 140 141 root->i_uid = current_uid(); 142 root->i_gid = current_gid(); 143 144 sbi->min_proto = AUTOFS_MIN_PROTO_VERSION; 145 sbi->max_proto = AUTOFS_MAX_PROTO_VERSION; 146 147 sbi->pipefd = -1; 148 149 if (!options) 150 return 1; 151 152 while ((p = strsep(&options, ",")) != NULL) { 153 int token; 154 155 if (!*p) 156 continue; 157 158 token = match_token(p, tokens, args); 159 switch (token) { 160 case Opt_fd: 161 if (match_int(args, &pipefd)) 162 return 1; 163 sbi->pipefd = pipefd; 164 break; 165 case Opt_uid: 166 if (match_int(args, &option)) 167 return 1; 168 uid = make_kuid(current_user_ns(), option); 169 if (!uid_valid(uid)) 170 return 1; 171 root->i_uid = uid; 172 break; 173 case Opt_gid: 174 if (match_int(args, &option)) 175 return 1; 176 gid = make_kgid(current_user_ns(), option); 177 if (!gid_valid(gid)) 178 return 1; 179 root->i_gid = gid; 180 break; 181 case Opt_pgrp: 182 if (match_int(args, &option)) 183 return 1; 184 *pgrp = option; 185 *pgrp_set = true; 186 break; 187 case Opt_minproto: 188 if (match_int(args, &option)) 189 return 1; 190 sbi->min_proto = option; 191 break; 192 case Opt_maxproto: 193 if (match_int(args, &option)) 194 return 1; 195 sbi->max_proto = option; 196 break; 197 case Opt_indirect: 198 set_autofs_type_indirect(&sbi->type); 199 break; 200 case Opt_direct: 201 set_autofs_type_direct(&sbi->type); 202 break; 203 case Opt_offset: 204 set_autofs_type_offset(&sbi->type); 205 break; 206 case Opt_strictexpire: 207 sbi->flags |= AUTOFS_SBI_STRICTEXPIRE; 208 break; 209 default: 210 return 1; 211 } 212 } 213 return (sbi->pipefd < 0); 214 } 215 216 int autofs_fill_super(struct super_block *s, void *data, int silent) 217 { 218 struct inode *root_inode; 219 struct dentry *root; 220 struct file *pipe; 221 struct autofs_sb_info *sbi; 222 struct autofs_info *ino; 223 int pgrp = 0; 224 bool pgrp_set = false; 225 int ret = -EINVAL; 226 227 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 228 if (!sbi) 229 return -ENOMEM; 230 pr_debug("starting up, sbi = %p\n", sbi); 231 232 s->s_fs_info = sbi; 233 sbi->magic = AUTOFS_SBI_MAGIC; 234 sbi->pipefd = -1; 235 sbi->pipe = NULL; 236 sbi->exp_timeout = 0; 237 sbi->oz_pgrp = NULL; 238 sbi->sb = s; 239 sbi->version = 0; 240 sbi->sub_version = 0; 241 sbi->flags = AUTOFS_SBI_CATATONIC; 242 set_autofs_type_indirect(&sbi->type); 243 sbi->min_proto = 0; 244 sbi->max_proto = 0; 245 mutex_init(&sbi->wq_mutex); 246 mutex_init(&sbi->pipe_mutex); 247 spin_lock_init(&sbi->fs_lock); 248 sbi->queues = NULL; 249 spin_lock_init(&sbi->lookup_lock); 250 INIT_LIST_HEAD(&sbi->active_list); 251 INIT_LIST_HEAD(&sbi->expiring_list); 252 s->s_blocksize = 1024; 253 s->s_blocksize_bits = 10; 254 s->s_magic = AUTOFS_SUPER_MAGIC; 255 s->s_op = &autofs_sops; 256 s->s_d_op = &autofs_dentry_operations; 257 s->s_time_gran = 1; 258 259 /* 260 * Get the root inode and dentry, but defer checking for errors. 261 */ 262 ino = autofs_new_ino(sbi); 263 if (!ino) { 264 ret = -ENOMEM; 265 goto fail_free; 266 } 267 root_inode = autofs_get_inode(s, S_IFDIR | 0755); 268 root = d_make_root(root_inode); 269 if (!root) { 270 ret = -ENOMEM; 271 goto fail_ino; 272 } 273 pipe = NULL; 274 275 root->d_fsdata = ino; 276 277 /* Can this call block? */ 278 if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) { 279 pr_err("called with bogus options\n"); 280 goto fail_dput; 281 } 282 283 /* Test versions first */ 284 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || 285 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { 286 pr_err("kernel does not match daemon version " 287 "daemon (%d, %d) kernel (%d, %d)\n", 288 sbi->min_proto, sbi->max_proto, 289 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); 290 goto fail_dput; 291 } 292 293 /* Establish highest kernel protocol version */ 294 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) 295 sbi->version = AUTOFS_MAX_PROTO_VERSION; 296 else 297 sbi->version = sbi->max_proto; 298 sbi->sub_version = AUTOFS_PROTO_SUBVERSION; 299 300 if (pgrp_set) { 301 sbi->oz_pgrp = find_get_pid(pgrp); 302 if (!sbi->oz_pgrp) { 303 pr_err("could not find process group %d\n", 304 pgrp); 305 goto fail_dput; 306 } 307 } else { 308 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); 309 } 310 311 if (autofs_type_trigger(sbi->type)) 312 __managed_dentry_set_managed(root); 313 314 root_inode->i_fop = &autofs_root_operations; 315 root_inode->i_op = &autofs_dir_inode_operations; 316 317 pr_debug("pipe fd = %d, pgrp = %u\n", 318 sbi->pipefd, pid_nr(sbi->oz_pgrp)); 319 pipe = fget(sbi->pipefd); 320 321 if (!pipe) { 322 pr_err("could not open pipe file descriptor\n"); 323 goto fail_put_pid; 324 } 325 ret = autofs_prepare_pipe(pipe); 326 if (ret < 0) 327 goto fail_fput; 328 sbi->pipe = pipe; 329 sbi->flags &= ~AUTOFS_SBI_CATATONIC; 330 331 /* 332 * Success! Install the root dentry now to indicate completion. 333 */ 334 s->s_root = root; 335 return 0; 336 337 /* 338 * Failure ... clean up. 339 */ 340 fail_fput: 341 pr_err("pipe file descriptor does not contain proper ops\n"); 342 fput(pipe); 343 fail_put_pid: 344 put_pid(sbi->oz_pgrp); 345 fail_dput: 346 dput(root); 347 goto fail_free; 348 fail_ino: 349 autofs_free_ino(ino); 350 fail_free: 351 kfree(sbi); 352 s->s_fs_info = NULL; 353 return ret; 354 } 355 356 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode) 357 { 358 struct inode *inode = new_inode(sb); 359 360 if (inode == NULL) 361 return NULL; 362 363 inode->i_mode = mode; 364 if (sb->s_root) { 365 inode->i_uid = d_inode(sb->s_root)->i_uid; 366 inode->i_gid = d_inode(sb->s_root)->i_gid; 367 } 368 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 369 inode->i_ino = get_next_ino(); 370 371 if (S_ISDIR(mode)) { 372 set_nlink(inode, 2); 373 inode->i_op = &autofs_dir_inode_operations; 374 inode->i_fop = &autofs_dir_operations; 375 } else if (S_ISLNK(mode)) { 376 inode->i_op = &autofs_symlink_inode_operations; 377 } else 378 WARN_ON(1); 379 380 return inode; 381 } 382