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