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 /* Internal header file for autofs */
0008 
0009 #include <linux/auto_fs.h>
0010 #include <linux/auto_dev-ioctl.h>
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/time.h>
0015 #include <linux/string.h>
0016 #include <linux/wait.h>
0017 #include <linux/sched.h>
0018 #include <linux/sched/signal.h>
0019 #include <linux/mount.h>
0020 #include <linux/namei.h>
0021 #include <linux/uaccess.h>
0022 #include <linux/mutex.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/list.h>
0025 #include <linux/completion.h>
0026 #include <linux/file.h>
0027 #include <linux/magic.h>
0028 
0029 /* This is the range of ioctl() numbers we claim as ours */
0030 #define AUTOFS_IOC_FIRST     AUTOFS_IOC_READY
0031 #define AUTOFS_IOC_COUNT     32
0032 
0033 #define AUTOFS_DEV_IOCTL_IOC_FIRST  (AUTOFS_DEV_IOCTL_VERSION)
0034 #define AUTOFS_DEV_IOCTL_IOC_COUNT \
0035     (AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD - AUTOFS_DEV_IOCTL_VERSION_CMD)
0036 
0037 #ifdef pr_fmt
0038 #undef pr_fmt
0039 #endif
0040 #define pr_fmt(fmt) KBUILD_MODNAME ":pid:%d:%s: " fmt, current->pid, __func__
0041 
0042 extern struct file_system_type autofs_fs_type;
0043 
0044 /*
0045  * Unified info structure.  This is pointed to by both the dentry and
0046  * inode structures.  Each file in the filesystem has an instance of this
0047  * structure.  It holds a reference to the dentry, so dentries are never
0048  * flushed while the file exists.  All name lookups are dealt with at the
0049  * dentry level, although the filesystem can interfere in the validation
0050  * process.  Readdir is implemented by traversing the dentry lists.
0051  */
0052 struct autofs_info {
0053     struct dentry   *dentry;
0054     int     flags;
0055 
0056     struct completion expire_complete;
0057 
0058     struct list_head active;
0059 
0060     struct list_head expiring;
0061 
0062     struct autofs_sb_info *sbi;
0063     unsigned long last_used;
0064     int count;
0065 
0066     kuid_t uid;
0067     kgid_t gid;
0068     struct rcu_head rcu;
0069 };
0070 
0071 #define AUTOFS_INF_EXPIRING (1<<0) /* dentry in the process of expiring */
0072 #define AUTOFS_INF_WANT_EXPIRE  (1<<1) /* the dentry is being considered
0073                     * for expiry, so RCU_walk is
0074                     * not permitted.  If it progresses to
0075                     * actual expiry attempt, the flag is
0076                     * not cleared when EXPIRING is set -
0077                     * in that case it gets cleared only
0078                     * when it comes to clearing EXPIRING.
0079                     */
0080 #define AUTOFS_INF_PENDING  (1<<2) /* dentry pending mount */
0081 
0082 struct autofs_wait_queue {
0083     wait_queue_head_t queue;
0084     struct autofs_wait_queue *next;
0085     autofs_wqt_t wait_queue_token;
0086     /* We use the following to see what we are waiting for */
0087     struct qstr name;
0088     u32 offset;
0089     u32 dev;
0090     u64 ino;
0091     kuid_t uid;
0092     kgid_t gid;
0093     pid_t pid;
0094     pid_t tgid;
0095     /* This is for status reporting upon return */
0096     int status;
0097     unsigned int wait_ctr;
0098 };
0099 
0100 #define AUTOFS_SBI_MAGIC 0x6d4a556d
0101 
0102 #define AUTOFS_SBI_CATATONIC    0x0001
0103 #define AUTOFS_SBI_STRICTEXPIRE 0x0002
0104 #define AUTOFS_SBI_IGNORE   0x0004
0105 
0106 struct autofs_sb_info {
0107     u32 magic;
0108     int pipefd;
0109     struct file *pipe;
0110     struct pid *oz_pgrp;
0111     int version;
0112     int sub_version;
0113     int min_proto;
0114     int max_proto;
0115     unsigned int flags;
0116     unsigned long exp_timeout;
0117     unsigned int type;
0118     struct super_block *sb;
0119     struct mutex wq_mutex;
0120     struct mutex pipe_mutex;
0121     spinlock_t fs_lock;
0122     struct autofs_wait_queue *queues; /* Wait queue pointer */
0123     spinlock_t lookup_lock;
0124     struct list_head active_list;
0125     struct list_head expiring_list;
0126     struct rcu_head rcu;
0127 };
0128 
0129 static inline struct autofs_sb_info *autofs_sbi(struct super_block *sb)
0130 {
0131     return (struct autofs_sb_info *)(sb->s_fs_info);
0132 }
0133 
0134 static inline struct autofs_info *autofs_dentry_ino(struct dentry *dentry)
0135 {
0136     return (struct autofs_info *)(dentry->d_fsdata);
0137 }
0138 
0139 /* autofs_oz_mode(): do we see the man behind the curtain?  (The
0140  * processes which do manipulations for us in user space sees the raw
0141  * filesystem without "magic".)
0142  */
0143 static inline int autofs_oz_mode(struct autofs_sb_info *sbi)
0144 {
0145     return ((sbi->flags & AUTOFS_SBI_CATATONIC) ||
0146          task_pgrp(current) == sbi->oz_pgrp);
0147 }
0148 
0149 static inline bool autofs_empty(struct autofs_info *ino)
0150 {
0151     return ino->count < 2;
0152 }
0153 
0154 struct inode *autofs_get_inode(struct super_block *, umode_t);
0155 void autofs_free_ino(struct autofs_info *);
0156 
0157 /* Expiration */
0158 int is_autofs_dentry(struct dentry *);
0159 int autofs_expire_wait(const struct path *path, int rcu_walk);
0160 int autofs_expire_run(struct super_block *, struct vfsmount *,
0161               struct autofs_sb_info *,
0162               struct autofs_packet_expire __user *);
0163 int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
0164                struct autofs_sb_info *sbi, unsigned int how);
0165 int autofs_expire_multi(struct super_block *, struct vfsmount *,
0166             struct autofs_sb_info *, int __user *);
0167 
0168 /* Device node initialization */
0169 
0170 int autofs_dev_ioctl_init(void);
0171 void autofs_dev_ioctl_exit(void);
0172 
0173 /* Operations structures */
0174 
0175 extern const struct inode_operations autofs_symlink_inode_operations;
0176 extern const struct inode_operations autofs_dir_inode_operations;
0177 extern const struct file_operations autofs_dir_operations;
0178 extern const struct file_operations autofs_root_operations;
0179 extern const struct dentry_operations autofs_dentry_operations;
0180 
0181 /* VFS automount flags management functions */
0182 static inline void __managed_dentry_set_managed(struct dentry *dentry)
0183 {
0184     dentry->d_flags |= (DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
0185 }
0186 
0187 static inline void managed_dentry_set_managed(struct dentry *dentry)
0188 {
0189     spin_lock(&dentry->d_lock);
0190     __managed_dentry_set_managed(dentry);
0191     spin_unlock(&dentry->d_lock);
0192 }
0193 
0194 static inline void __managed_dentry_clear_managed(struct dentry *dentry)
0195 {
0196     dentry->d_flags &= ~(DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
0197 }
0198 
0199 static inline void managed_dentry_clear_managed(struct dentry *dentry)
0200 {
0201     spin_lock(&dentry->d_lock);
0202     __managed_dentry_clear_managed(dentry);
0203     spin_unlock(&dentry->d_lock);
0204 }
0205 
0206 /* Initializing function */
0207 
0208 int autofs_fill_super(struct super_block *, void *, int);
0209 struct autofs_info *autofs_new_ino(struct autofs_sb_info *);
0210 void autofs_clean_ino(struct autofs_info *);
0211 
0212 static inline int autofs_prepare_pipe(struct file *pipe)
0213 {
0214     if (!(pipe->f_mode & FMODE_CAN_WRITE))
0215         return -EINVAL;
0216     if (!S_ISFIFO(file_inode(pipe)->i_mode))
0217         return -EINVAL;
0218     /* We want a packet pipe */
0219     pipe->f_flags |= O_DIRECT;
0220     /* We don't expect -EAGAIN */
0221     pipe->f_flags &= ~O_NONBLOCK;
0222     return 0;
0223 }
0224 
0225 /* Queue management functions */
0226 
0227 int autofs_wait(struct autofs_sb_info *,
0228          const struct path *, enum autofs_notify);
0229 int autofs_wait_release(struct autofs_sb_info *, autofs_wqt_t, int);
0230 void autofs_catatonic_mode(struct autofs_sb_info *);
0231 
0232 static inline u32 autofs_get_dev(struct autofs_sb_info *sbi)
0233 {
0234     return new_encode_dev(sbi->sb->s_dev);
0235 }
0236 
0237 static inline u64 autofs_get_ino(struct autofs_sb_info *sbi)
0238 {
0239     return d_inode(sbi->sb->s_root)->i_ino;
0240 }
0241 
0242 static inline void __autofs_add_expiring(struct dentry *dentry)
0243 {
0244     struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
0245     struct autofs_info *ino = autofs_dentry_ino(dentry);
0246 
0247     if (ino) {
0248         if (list_empty(&ino->expiring))
0249             list_add(&ino->expiring, &sbi->expiring_list);
0250     }
0251 }
0252 
0253 static inline void autofs_add_expiring(struct dentry *dentry)
0254 {
0255     struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
0256     struct autofs_info *ino = autofs_dentry_ino(dentry);
0257 
0258     if (ino) {
0259         spin_lock(&sbi->lookup_lock);
0260         if (list_empty(&ino->expiring))
0261             list_add(&ino->expiring, &sbi->expiring_list);
0262         spin_unlock(&sbi->lookup_lock);
0263     }
0264 }
0265 
0266 static inline void autofs_del_expiring(struct dentry *dentry)
0267 {
0268     struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
0269     struct autofs_info *ino = autofs_dentry_ino(dentry);
0270 
0271     if (ino) {
0272         spin_lock(&sbi->lookup_lock);
0273         if (!list_empty(&ino->expiring))
0274             list_del_init(&ino->expiring);
0275         spin_unlock(&sbi->lookup_lock);
0276     }
0277 }
0278 
0279 void autofs_kill_sb(struct super_block *);