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