1d6910058SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2ebc921caSIan Kent /*
3ebc921caSIan Kent * Copyright 1997-1998 Transmeta Corporation - All Rights Reserved
4ebc921caSIan Kent * Copyright 2005-2006 Ian Kent <raven@themaw.net>
5ebc921caSIan Kent */
6ebc921caSIan Kent
7ebc921caSIan Kent /* Internal header file for autofs */
8ebc921caSIan Kent
9ebc921caSIan Kent #include <linux/auto_fs.h>
10ebc921caSIan Kent #include <linux/auto_dev-ioctl.h>
11ebc921caSIan Kent
12ebc921caSIan Kent #include <linux/kernel.h>
13ebc921caSIan Kent #include <linux/slab.h>
14ebc921caSIan Kent #include <linux/time.h>
15ebc921caSIan Kent #include <linux/string.h>
16ebc921caSIan Kent #include <linux/wait.h>
17ebc921caSIan Kent #include <linux/sched.h>
182c470475SEric W. Biederman #include <linux/sched/signal.h>
19ebc921caSIan Kent #include <linux/mount.h>
20ebc921caSIan Kent #include <linux/namei.h>
21ebc921caSIan Kent #include <linux/uaccess.h>
22ebc921caSIan Kent #include <linux/mutex.h>
23ebc921caSIan Kent #include <linux/spinlock.h>
24ebc921caSIan Kent #include <linux/list.h>
25ebc921caSIan Kent #include <linux/completion.h>
266471e938SIan Kent #include <linux/file.h>
270633da48SIan Kent #include <linux/magic.h>
28ebc921caSIan Kent
29ebc921caSIan Kent /* This is the range of ioctl() numbers we claim as ours */
30ebc921caSIan Kent #define AUTOFS_IOC_FIRST AUTOFS_IOC_READY
31ebc921caSIan Kent #define AUTOFS_IOC_COUNT 32
32ebc921caSIan Kent
33ebc921caSIan Kent #define AUTOFS_DEV_IOCTL_IOC_FIRST (AUTOFS_DEV_IOCTL_VERSION)
34ebc921caSIan Kent #define AUTOFS_DEV_IOCTL_IOC_COUNT \
35ebc921caSIan Kent (AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD - AUTOFS_DEV_IOCTL_VERSION_CMD)
36ebc921caSIan Kent
37ebc921caSIan Kent #ifdef pr_fmt
38ebc921caSIan Kent #undef pr_fmt
39ebc921caSIan Kent #endif
40ebc921caSIan Kent #define pr_fmt(fmt) KBUILD_MODNAME ":pid:%d:%s: " fmt, current->pid, __func__
41ebc921caSIan Kent
4255f0d820SIan Kent extern struct file_system_type autofs_fs_type;
4355f0d820SIan Kent
44ebc921caSIan Kent /*
45ebc921caSIan Kent * Unified info structure. This is pointed to by both the dentry and
46ebc921caSIan Kent * inode structures. Each file in the filesystem has an instance of this
47ebc921caSIan Kent * structure. It holds a reference to the dentry, so dentries are never
48ebc921caSIan Kent * flushed while the file exists. All name lookups are dealt with at the
49ebc921caSIan Kent * dentry level, although the filesystem can interfere in the validation
50ebc921caSIan Kent * process. Readdir is implemented by traversing the dentry lists.
51ebc921caSIan Kent */
52ebc921caSIan Kent struct autofs_info {
53ebc921caSIan Kent struct dentry *dentry;
54ebc921caSIan Kent int flags;
55ebc921caSIan Kent
56ebc921caSIan Kent struct completion expire_complete;
57ebc921caSIan Kent
58ebc921caSIan Kent struct list_head active;
59ebc921caSIan Kent
60ebc921caSIan Kent struct list_head expiring;
61ebc921caSIan Kent
62ebc921caSIan Kent struct autofs_sb_info *sbi;
63ebc921caSIan Kent unsigned long last_used;
64850d71acSAl Viro int count;
65ebc921caSIan Kent
66ebc921caSIan Kent kuid_t uid;
67ebc921caSIan Kent kgid_t gid;
68ce285c26SAl Viro struct rcu_head rcu;
69ebc921caSIan Kent };
70ebc921caSIan Kent
71ebc921caSIan Kent #define AUTOFS_INF_EXPIRING (1<<0) /* dentry in the process of expiring */
72ebc921caSIan Kent #define AUTOFS_INF_WANT_EXPIRE (1<<1) /* the dentry is being considered
73ebc921caSIan Kent * for expiry, so RCU_walk is
74ebc921caSIan Kent * not permitted. If it progresses to
75ebc921caSIan Kent * actual expiry attempt, the flag is
76ebc921caSIan Kent * not cleared when EXPIRING is set -
77ebc921caSIan Kent * in that case it gets cleared only
78ebc921caSIan Kent * when it comes to clearing EXPIRING.
79ebc921caSIan Kent */
80ebc921caSIan Kent #define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */
81ebc921caSIan Kent
82ebc921caSIan Kent struct autofs_wait_queue {
83ebc921caSIan Kent wait_queue_head_t queue;
84ebc921caSIan Kent struct autofs_wait_queue *next;
85ebc921caSIan Kent autofs_wqt_t wait_queue_token;
86ebc921caSIan Kent /* We use the following to see what we are waiting for */
87ebc921caSIan Kent struct qstr name;
882be7828cSAl Viro u32 offset;
89ebc921caSIan Kent u32 dev;
90ebc921caSIan Kent u64 ino;
91ebc921caSIan Kent kuid_t uid;
92ebc921caSIan Kent kgid_t gid;
93ebc921caSIan Kent pid_t pid;
94ebc921caSIan Kent pid_t tgid;
95ebc921caSIan Kent /* This is for status reporting upon return */
96ebc921caSIan Kent int status;
97ebc921caSIan Kent unsigned int wait_ctr;
98ebc921caSIan Kent };
99ebc921caSIan Kent
100ebc921caSIan Kent #define AUTOFS_SBI_MAGIC 0x6d4a556d
101ebc921caSIan Kent
1029d8719a4SIan Kent #define AUTOFS_SBI_CATATONIC 0x0001
103f5162216SIan Kent #define AUTOFS_SBI_STRICTEXPIRE 0x0002
10460d6d04cSIan Kent #define AUTOFS_SBI_IGNORE 0x0004
1059d8719a4SIan Kent
106ebc921caSIan Kent struct autofs_sb_info {
107ebc921caSIan Kent u32 magic;
108ebc921caSIan Kent int pipefd;
109ebc921caSIan Kent struct file *pipe;
110ebc921caSIan Kent struct pid *oz_pgrp;
111ebc921caSIan Kent int version;
112ebc921caSIan Kent int sub_version;
113ebc921caSIan Kent int min_proto;
114ebc921caSIan Kent int max_proto;
1159d8719a4SIan Kent unsigned int flags;
116ebc921caSIan Kent unsigned long exp_timeout;
117ebc921caSIan Kent unsigned int type;
118ebc921caSIan Kent struct super_block *sb;
119ebc921caSIan Kent struct mutex wq_mutex;
120ebc921caSIan Kent struct mutex pipe_mutex;
121ebc921caSIan Kent spinlock_t fs_lock;
122ebc921caSIan Kent struct autofs_wait_queue *queues; /* Wait queue pointer */
123ebc921caSIan Kent spinlock_t lookup_lock;
124ebc921caSIan Kent struct list_head active_list;
125ebc921caSIan Kent struct list_head expiring_list;
126ebc921caSIan Kent struct rcu_head rcu;
127ebc921caSIan Kent };
128ebc921caSIan Kent
autofs_sbi(struct super_block * sb)129ebc921caSIan Kent static inline struct autofs_sb_info *autofs_sbi(struct super_block *sb)
130ebc921caSIan Kent {
13155f0d820SIan Kent return (struct autofs_sb_info *)(sb->s_fs_info);
132ebc921caSIan Kent }
133ebc921caSIan Kent
autofs_dentry_ino(struct dentry * dentry)134ebc921caSIan Kent static inline struct autofs_info *autofs_dentry_ino(struct dentry *dentry)
135ebc921caSIan Kent {
136ebc921caSIan Kent return (struct autofs_info *)(dentry->d_fsdata);
137ebc921caSIan Kent }
138ebc921caSIan Kent
139ebc921caSIan Kent /* autofs_oz_mode(): do we see the man behind the curtain? (The
140ebc921caSIan Kent * processes which do manipulations for us in user space sees the raw
141ebc921caSIan Kent * filesystem without "magic".)
142ebc921caSIan Kent */
autofs_oz_mode(struct autofs_sb_info * sbi)143ebc921caSIan Kent static inline int autofs_oz_mode(struct autofs_sb_info *sbi)
144ebc921caSIan Kent {
1459d8719a4SIan Kent return ((sbi->flags & AUTOFS_SBI_CATATONIC) ||
1469d8719a4SIan Kent task_pgrp(current) == sbi->oz_pgrp);
147ebc921caSIan Kent }
148ebc921caSIan Kent
autofs_empty(struct autofs_info * ino)149*a4a87303SIan Kent static inline bool autofs_empty(struct autofs_info *ino)
150*a4a87303SIan Kent {
151*a4a87303SIan Kent return ino->count < 2;
152*a4a87303SIan Kent }
153*a4a87303SIan Kent
154ebc921caSIan Kent struct inode *autofs_get_inode(struct super_block *, umode_t);
155ebc921caSIan Kent void autofs_free_ino(struct autofs_info *);
156ebc921caSIan Kent
157ebc921caSIan Kent /* Expiration */
158ebc921caSIan Kent int is_autofs_dentry(struct dentry *);
159ebc921caSIan Kent int autofs_expire_wait(const struct path *path, int rcu_walk);
160ebc921caSIan Kent int autofs_expire_run(struct super_block *, struct vfsmount *,
161ebc921caSIan Kent struct autofs_sb_info *,
162ebc921caSIan Kent struct autofs_packet_expire __user *);
163ebc921caSIan Kent int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
164e5c85e1fSIan Kent struct autofs_sb_info *sbi, unsigned int how);
165ebc921caSIan Kent int autofs_expire_multi(struct super_block *, struct vfsmount *,
166ebc921caSIan Kent struct autofs_sb_info *, int __user *);
167ebc921caSIan Kent
168ebc921caSIan Kent /* Device node initialization */
169ebc921caSIan Kent
170ebc921caSIan Kent int autofs_dev_ioctl_init(void);
171ebc921caSIan Kent void autofs_dev_ioctl_exit(void);
172ebc921caSIan Kent
173ebc921caSIan Kent /* Operations structures */
174ebc921caSIan Kent
175ebc921caSIan Kent extern const struct inode_operations autofs_symlink_inode_operations;
176ebc921caSIan Kent extern const struct inode_operations autofs_dir_inode_operations;
177ebc921caSIan Kent extern const struct file_operations autofs_dir_operations;
178ebc921caSIan Kent extern const struct file_operations autofs_root_operations;
179ebc921caSIan Kent extern const struct dentry_operations autofs_dentry_operations;
180ebc921caSIan Kent
181ebc921caSIan Kent /* VFS automount flags management functions */
__managed_dentry_set_managed(struct dentry * dentry)182ebc921caSIan Kent static inline void __managed_dentry_set_managed(struct dentry *dentry)
183ebc921caSIan Kent {
184ebc921caSIan Kent dentry->d_flags |= (DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
185ebc921caSIan Kent }
186ebc921caSIan Kent
managed_dentry_set_managed(struct dentry * dentry)187ebc921caSIan Kent static inline void managed_dentry_set_managed(struct dentry *dentry)
188ebc921caSIan Kent {
189ebc921caSIan Kent spin_lock(&dentry->d_lock);
190ebc921caSIan Kent __managed_dentry_set_managed(dentry);
191ebc921caSIan Kent spin_unlock(&dentry->d_lock);
192ebc921caSIan Kent }
193ebc921caSIan Kent
__managed_dentry_clear_managed(struct dentry * dentry)194ebc921caSIan Kent static inline void __managed_dentry_clear_managed(struct dentry *dentry)
195ebc921caSIan Kent {
196ebc921caSIan Kent dentry->d_flags &= ~(DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
197ebc921caSIan Kent }
198ebc921caSIan Kent
managed_dentry_clear_managed(struct dentry * dentry)199ebc921caSIan Kent static inline void managed_dentry_clear_managed(struct dentry *dentry)
200ebc921caSIan Kent {
201ebc921caSIan Kent spin_lock(&dentry->d_lock);
202ebc921caSIan Kent __managed_dentry_clear_managed(dentry);
203ebc921caSIan Kent spin_unlock(&dentry->d_lock);
204ebc921caSIan Kent }
205ebc921caSIan Kent
206ebc921caSIan Kent /* Initializing function */
207ebc921caSIan Kent
208ebc921caSIan Kent int autofs_fill_super(struct super_block *, void *, int);
209ebc921caSIan Kent struct autofs_info *autofs_new_ino(struct autofs_sb_info *);
210ebc921caSIan Kent void autofs_clean_ino(struct autofs_info *);
211ebc921caSIan Kent
autofs_prepare_pipe(struct file * pipe)212ebc921caSIan Kent static inline int autofs_prepare_pipe(struct file *pipe)
213ebc921caSIan Kent {
214ebc921caSIan Kent if (!(pipe->f_mode & FMODE_CAN_WRITE))
215ebc921caSIan Kent return -EINVAL;
216ebc921caSIan Kent if (!S_ISFIFO(file_inode(pipe)->i_mode))
217ebc921caSIan Kent return -EINVAL;
218ebc921caSIan Kent /* We want a packet pipe */
219ebc921caSIan Kent pipe->f_flags |= O_DIRECT;
220660c9fc7SNeilBrown /* We don't expect -EAGAIN */
221660c9fc7SNeilBrown pipe->f_flags &= ~O_NONBLOCK;
222ebc921caSIan Kent return 0;
223ebc921caSIan Kent }
224ebc921caSIan Kent
225ebc921caSIan Kent /* Queue management functions */
226ebc921caSIan Kent
227ebc921caSIan Kent int autofs_wait(struct autofs_sb_info *,
228ebc921caSIan Kent const struct path *, enum autofs_notify);
229ebc921caSIan Kent int autofs_wait_release(struct autofs_sb_info *, autofs_wqt_t, int);
230ebc921caSIan Kent void autofs_catatonic_mode(struct autofs_sb_info *);
231ebc921caSIan Kent
autofs_get_dev(struct autofs_sb_info * sbi)232ebc921caSIan Kent static inline u32 autofs_get_dev(struct autofs_sb_info *sbi)
233ebc921caSIan Kent {
234ebc921caSIan Kent return new_encode_dev(sbi->sb->s_dev);
235ebc921caSIan Kent }
236ebc921caSIan Kent
autofs_get_ino(struct autofs_sb_info * sbi)237ebc921caSIan Kent static inline u64 autofs_get_ino(struct autofs_sb_info *sbi)
238ebc921caSIan Kent {
239ebc921caSIan Kent return d_inode(sbi->sb->s_root)->i_ino;
240ebc921caSIan Kent }
241ebc921caSIan Kent
__autofs_add_expiring(struct dentry * dentry)242ebc921caSIan Kent static inline void __autofs_add_expiring(struct dentry *dentry)
243ebc921caSIan Kent {
244ebc921caSIan Kent struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
245ebc921caSIan Kent struct autofs_info *ino = autofs_dentry_ino(dentry);
246ebc921caSIan Kent
247ebc921caSIan Kent if (ino) {
248ebc921caSIan Kent if (list_empty(&ino->expiring))
249ebc921caSIan Kent list_add(&ino->expiring, &sbi->expiring_list);
250ebc921caSIan Kent }
251ebc921caSIan Kent }
252ebc921caSIan Kent
autofs_add_expiring(struct dentry * dentry)253ebc921caSIan Kent static inline void autofs_add_expiring(struct dentry *dentry)
254ebc921caSIan Kent {
255ebc921caSIan Kent struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
256ebc921caSIan Kent struct autofs_info *ino = autofs_dentry_ino(dentry);
257ebc921caSIan Kent
258ebc921caSIan Kent if (ino) {
259ebc921caSIan Kent spin_lock(&sbi->lookup_lock);
260ebc921caSIan Kent if (list_empty(&ino->expiring))
261ebc921caSIan Kent list_add(&ino->expiring, &sbi->expiring_list);
262ebc921caSIan Kent spin_unlock(&sbi->lookup_lock);
263ebc921caSIan Kent }
264ebc921caSIan Kent }
265ebc921caSIan Kent
autofs_del_expiring(struct dentry * dentry)266ebc921caSIan Kent static inline void autofs_del_expiring(struct dentry *dentry)
267ebc921caSIan Kent {
268ebc921caSIan Kent struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
269ebc921caSIan Kent struct autofs_info *ino = autofs_dentry_ino(dentry);
270ebc921caSIan Kent
271ebc921caSIan Kent if (ino) {
272ebc921caSIan Kent spin_lock(&sbi->lookup_lock);
273ebc921caSIan Kent if (!list_empty(&ino->expiring))
274ebc921caSIan Kent list_del_init(&ino->expiring);
275ebc921caSIan Kent spin_unlock(&sbi->lookup_lock);
276ebc921caSIan Kent }
277ebc921caSIan Kent }
278ebc921caSIan Kent
279ebc921caSIan Kent void autofs_kill_sb(struct super_block *);
280