Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
0004  * Copyright 2005-2006 Ian Kent <raven@themaw.net>
0005  */
0006 
0007 #include <linux/seq_file.h>
0008 #include <linux/pagemap.h>
0009 #include <linux/parser.h>
0010 
0011 #include "autofs_i.h"
0012 
0013 struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi)
0014 {
0015     struct autofs_info *ino;
0016 
0017     ino = kzalloc(sizeof(*ino), GFP_KERNEL);
0018     if (ino) {
0019         INIT_LIST_HEAD(&ino->active);
0020         INIT_LIST_HEAD(&ino->expiring);
0021         ino->last_used = jiffies;
0022         ino->sbi = sbi;
0023         ino->count = 1;
0024     }
0025     return ino;
0026 }
0027 
0028 void autofs_clean_ino(struct autofs_info *ino)
0029 {
0030     ino->uid = GLOBAL_ROOT_UID;
0031     ino->gid = GLOBAL_ROOT_GID;
0032     ino->last_used = jiffies;
0033 }
0034 
0035 void autofs_free_ino(struct autofs_info *ino)
0036 {
0037     kfree_rcu(ino, rcu);
0038 }
0039 
0040 void autofs_kill_sb(struct super_block *sb)
0041 {
0042     struct autofs_sb_info *sbi = autofs_sbi(sb);
0043 
0044     /*
0045      * In the event of a failure in get_sb_nodev the superblock
0046      * info is not present so nothing else has been setup, so
0047      * just call kill_anon_super when we are called from
0048      * deactivate_super.
0049      */
0050     if (sbi) {
0051         /* Free wait queues, close pipe */
0052         autofs_catatonic_mode(sbi);
0053         put_pid(sbi->oz_pgrp);
0054     }
0055 
0056     pr_debug("shutting down\n");
0057     kill_litter_super(sb);
0058     if (sbi)
0059         kfree_rcu(sbi, rcu);
0060 }
0061 
0062 static int autofs_show_options(struct seq_file *m, struct dentry *root)
0063 {
0064     struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
0065     struct inode *root_inode = d_inode(root->d_sb->s_root);
0066 
0067     if (!sbi)
0068         return 0;
0069 
0070     seq_printf(m, ",fd=%d", sbi->pipefd);
0071     if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
0072         seq_printf(m, ",uid=%u",
0073             from_kuid_munged(&init_user_ns, root_inode->i_uid));
0074     if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
0075         seq_printf(m, ",gid=%u",
0076             from_kgid_munged(&init_user_ns, root_inode->i_gid));
0077     seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
0078     seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
0079     seq_printf(m, ",minproto=%d", sbi->min_proto);
0080     seq_printf(m, ",maxproto=%d", sbi->max_proto);
0081 
0082     if (autofs_type_offset(sbi->type))
0083         seq_puts(m, ",offset");
0084     else if (autofs_type_direct(sbi->type))
0085         seq_puts(m, ",direct");
0086     else
0087         seq_puts(m, ",indirect");
0088     if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE)
0089         seq_puts(m, ",strictexpire");
0090     if (sbi->flags & AUTOFS_SBI_IGNORE)
0091         seq_puts(m, ",ignore");
0092 #ifdef CONFIG_CHECKPOINT_RESTORE
0093     if (sbi->pipe)
0094         seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
0095     else
0096         seq_puts(m, ",pipe_ino=-1");
0097 #endif
0098     return 0;
0099 }
0100 
0101 static void autofs_evict_inode(struct inode *inode)
0102 {
0103     clear_inode(inode);
0104     kfree(inode->i_private);
0105 }
0106 
0107 static const struct super_operations autofs_sops = {
0108     .statfs     = simple_statfs,
0109     .show_options   = autofs_show_options,
0110     .evict_inode    = autofs_evict_inode,
0111 };
0112 
0113 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
0114     Opt_indirect, Opt_direct, Opt_offset, Opt_strictexpire,
0115     Opt_ignore};
0116 
0117 static const match_table_t tokens = {
0118     {Opt_fd, "fd=%u"},
0119     {Opt_uid, "uid=%u"},
0120     {Opt_gid, "gid=%u"},
0121     {Opt_pgrp, "pgrp=%u"},
0122     {Opt_minproto, "minproto=%u"},
0123     {Opt_maxproto, "maxproto=%u"},
0124     {Opt_indirect, "indirect"},
0125     {Opt_direct, "direct"},
0126     {Opt_offset, "offset"},
0127     {Opt_strictexpire, "strictexpire"},
0128     {Opt_ignore, "ignore"},
0129     {Opt_err, NULL}
0130 };
0131 
0132 static int parse_options(char *options,
0133              struct inode *root, int *pgrp, bool *pgrp_set,
0134              struct autofs_sb_info *sbi)
0135 {
0136     char *p;
0137     substring_t args[MAX_OPT_ARGS];
0138     int option;
0139     int pipefd = -1;
0140     kuid_t uid;
0141     kgid_t gid;
0142 
0143     root->i_uid = current_uid();
0144     root->i_gid = current_gid();
0145 
0146     sbi->min_proto = AUTOFS_MIN_PROTO_VERSION;
0147     sbi->max_proto = AUTOFS_MAX_PROTO_VERSION;
0148 
0149     sbi->pipefd = -1;
0150 
0151     if (!options)
0152         return 1;
0153 
0154     while ((p = strsep(&options, ",")) != NULL) {
0155         int token;
0156 
0157         if (!*p)
0158             continue;
0159 
0160         token = match_token(p, tokens, args);
0161         switch (token) {
0162         case Opt_fd:
0163             if (match_int(args, &pipefd))
0164                 return 1;
0165             sbi->pipefd = pipefd;
0166             break;
0167         case Opt_uid:
0168             if (match_int(args, &option))
0169                 return 1;
0170             uid = make_kuid(current_user_ns(), option);
0171             if (!uid_valid(uid))
0172                 return 1;
0173             root->i_uid = uid;
0174             break;
0175         case Opt_gid:
0176             if (match_int(args, &option))
0177                 return 1;
0178             gid = make_kgid(current_user_ns(), option);
0179             if (!gid_valid(gid))
0180                 return 1;
0181             root->i_gid = gid;
0182             break;
0183         case Opt_pgrp:
0184             if (match_int(args, &option))
0185                 return 1;
0186             *pgrp = option;
0187             *pgrp_set = true;
0188             break;
0189         case Opt_minproto:
0190             if (match_int(args, &option))
0191                 return 1;
0192             sbi->min_proto = option;
0193             break;
0194         case Opt_maxproto:
0195             if (match_int(args, &option))
0196                 return 1;
0197             sbi->max_proto = option;
0198             break;
0199         case Opt_indirect:
0200             set_autofs_type_indirect(&sbi->type);
0201             break;
0202         case Opt_direct:
0203             set_autofs_type_direct(&sbi->type);
0204             break;
0205         case Opt_offset:
0206             set_autofs_type_offset(&sbi->type);
0207             break;
0208         case Opt_strictexpire:
0209             sbi->flags |= AUTOFS_SBI_STRICTEXPIRE;
0210             break;
0211         case Opt_ignore:
0212             sbi->flags |= AUTOFS_SBI_IGNORE;
0213             break;
0214         default:
0215             return 1;
0216         }
0217     }
0218     return (sbi->pipefd < 0);
0219 }
0220 
0221 int autofs_fill_super(struct super_block *s, void *data, int silent)
0222 {
0223     struct inode *root_inode;
0224     struct dentry *root;
0225     struct file *pipe;
0226     struct autofs_sb_info *sbi;
0227     struct autofs_info *ino;
0228     int pgrp = 0;
0229     bool pgrp_set = false;
0230     int ret = -EINVAL;
0231 
0232     sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
0233     if (!sbi)
0234         return -ENOMEM;
0235     pr_debug("starting up, sbi = %p\n", sbi);
0236 
0237     s->s_fs_info = sbi;
0238     sbi->magic = AUTOFS_SBI_MAGIC;
0239     sbi->pipefd = -1;
0240     sbi->pipe = NULL;
0241     sbi->exp_timeout = 0;
0242     sbi->oz_pgrp = NULL;
0243     sbi->sb = s;
0244     sbi->version = 0;
0245     sbi->sub_version = 0;
0246     sbi->flags = AUTOFS_SBI_CATATONIC;
0247     set_autofs_type_indirect(&sbi->type);
0248     sbi->min_proto = 0;
0249     sbi->max_proto = 0;
0250     mutex_init(&sbi->wq_mutex);
0251     mutex_init(&sbi->pipe_mutex);
0252     spin_lock_init(&sbi->fs_lock);
0253     sbi->queues = NULL;
0254     spin_lock_init(&sbi->lookup_lock);
0255     INIT_LIST_HEAD(&sbi->active_list);
0256     INIT_LIST_HEAD(&sbi->expiring_list);
0257     s->s_blocksize = 1024;
0258     s->s_blocksize_bits = 10;
0259     s->s_magic = AUTOFS_SUPER_MAGIC;
0260     s->s_op = &autofs_sops;
0261     s->s_d_op = &autofs_dentry_operations;
0262     s->s_time_gran = 1;
0263 
0264     /*
0265      * Get the root inode and dentry, but defer checking for errors.
0266      */
0267     ino = autofs_new_ino(sbi);
0268     if (!ino) {
0269         ret = -ENOMEM;
0270         goto fail_free;
0271     }
0272     root_inode = autofs_get_inode(s, S_IFDIR | 0755);
0273     root = d_make_root(root_inode);
0274     if (!root) {
0275         ret = -ENOMEM;
0276         goto fail_ino;
0277     }
0278     pipe = NULL;
0279 
0280     root->d_fsdata = ino;
0281 
0282     /* Can this call block? */
0283     if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) {
0284         pr_err("called with bogus options\n");
0285         goto fail_dput;
0286     }
0287 
0288     /* Test versions first */
0289     if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
0290         sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
0291         pr_err("kernel does not match daemon version "
0292                "daemon (%d, %d) kernel (%d, %d)\n",
0293                sbi->min_proto, sbi->max_proto,
0294                AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
0295         goto fail_dput;
0296     }
0297 
0298     /* Establish highest kernel protocol version */
0299     if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
0300         sbi->version = AUTOFS_MAX_PROTO_VERSION;
0301     else
0302         sbi->version = sbi->max_proto;
0303     sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
0304 
0305     if (pgrp_set) {
0306         sbi->oz_pgrp = find_get_pid(pgrp);
0307         if (!sbi->oz_pgrp) {
0308             pr_err("could not find process group %d\n",
0309                 pgrp);
0310             goto fail_dput;
0311         }
0312     } else {
0313         sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
0314     }
0315 
0316     if (autofs_type_trigger(sbi->type))
0317         __managed_dentry_set_managed(root);
0318 
0319     root_inode->i_fop = &autofs_root_operations;
0320     root_inode->i_op = &autofs_dir_inode_operations;
0321 
0322     pr_debug("pipe fd = %d, pgrp = %u\n",
0323          sbi->pipefd, pid_nr(sbi->oz_pgrp));
0324     pipe = fget(sbi->pipefd);
0325 
0326     if (!pipe) {
0327         pr_err("could not open pipe file descriptor\n");
0328         goto fail_put_pid;
0329     }
0330     ret = autofs_prepare_pipe(pipe);
0331     if (ret < 0)
0332         goto fail_fput;
0333     sbi->pipe = pipe;
0334     sbi->flags &= ~AUTOFS_SBI_CATATONIC;
0335 
0336     /*
0337      * Success! Install the root dentry now to indicate completion.
0338      */
0339     s->s_root = root;
0340     return 0;
0341 
0342     /*
0343      * Failure ... clean up.
0344      */
0345 fail_fput:
0346     pr_err("pipe file descriptor does not contain proper ops\n");
0347     fput(pipe);
0348 fail_put_pid:
0349     put_pid(sbi->oz_pgrp);
0350 fail_dput:
0351     dput(root);
0352     goto fail_free;
0353 fail_ino:
0354     autofs_free_ino(ino);
0355 fail_free:
0356     kfree(sbi);
0357     s->s_fs_info = NULL;
0358     return ret;
0359 }
0360 
0361 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
0362 {
0363     struct inode *inode = new_inode(sb);
0364 
0365     if (inode == NULL)
0366         return NULL;
0367 
0368     inode->i_mode = mode;
0369     if (sb->s_root) {
0370         inode->i_uid = d_inode(sb->s_root)->i_uid;
0371         inode->i_gid = d_inode(sb->s_root)->i_gid;
0372     }
0373     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0374     inode->i_ino = get_next_ino();
0375 
0376     if (S_ISDIR(mode)) {
0377         set_nlink(inode, 2);
0378         inode->i_op = &autofs_dir_inode_operations;
0379         inode->i_fop = &autofs_dir_operations;
0380     } else if (S_ISLNK(mode)) {
0381         inode->i_op = &autofs_symlink_inode_operations;
0382     } else
0383         WARN_ON(1);
0384 
0385     return inode;
0386 }